Skip to content

Commit

Permalink
docs/testing: Convert docs examples to tests in ImageInput chapter (#…
Browse files Browse the repository at this point in the history
…4444)

Convert scanline C++ and Python examples from the "imageinput" chapter
into tests within the "docs-examples" testsuites
(#3992).

---------

Signed-off-by: Ziad Khouri <[email protected]>
  • Loading branch information
ziadkhouri authored Sep 27, 2024
1 parent e45a625 commit e5ad995
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
39 changes: 13 additions & 26 deletions src/doc/imageinput.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,32 +145,19 @@ Individual scanlines may be read using the ``read_scanline()`` API call:

.. tabs::

.. code-tab:: c++

auto inp = ImageInput::open (filename);
const ImageSpec &spec = inp->spec();
if (spec.tile_width == 0) {
auto scanline = std::unique_ptr<unsigned char[]>(new unsigned char[spec.width * spec.nchannels]);
for (int y = 0; y < yres; ++y) {
inp->read_scanline (y, 0, TypeDesc::UINT8, &scanline[0]);
// ... process data in scanline[0..width*channels-1] ...
}
} else {
//... handle tiles, or reject the file ...
}
inp->close ();

.. code-tab:: py

inp = ImageInput.open (filename)
spec = inp.spec()
if spec.tile_width == 0 :
for y in range(yres) :
scanline = inp.read_scanline (y, 0, "uint8")
# ... process data in scanline[0..width*channels-1] ...
else :
# ... handle tiles, or reject the file ...
inp.close ()
.. tab:: C++
.. literalinclude:: ../../testsuite/docs-examples-cpp/src/docs-examples-imageinput.cpp
:language: c++
:start-after: BEGIN-imageinput-scanlines
:end-before: END-imageinput-scanlines
:dedent: 4

.. tab:: Python
.. literalinclude:: ../../testsuite/docs-examples-python/src/docs-examples-imageinput.py
:language: py
:start-after: BEGIN-imageinput-scanlines
:end-before: END-imageinput-scanlines
:dedent: 8

The first two arguments to ``read_scanline()`` specify which scanline
is being read by its vertical (``y``) scanline number (beginning with 0)
Expand Down
20 changes: 19 additions & 1 deletion testsuite/docs-examples-cpp/src/docs-examples-imageinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,33 @@ void simple_read()
inp->read_image(0, 0, 0, nchannels, TypeDesc::UINT8, &pixels[0]);
inp->close();
}

// END-imageinput-simple

void scanlines_read()
{
const char* filename = "scanlines.tif";

// BEGIN-imageinput-scanlines
auto inp = ImageInput::open (filename);
const ImageSpec &spec = inp->spec();
if (spec.tile_width == 0) {
auto scanline = std::unique_ptr<unsigned char[]>(new unsigned char[spec.width * spec.nchannels]);
for (int y = 0; y < spec.height; ++y) {
inp->read_scanline (y, 0, TypeDesc::UINT8, &scanline[0]);
// ... process data in scanline[0..width*channels-1] ...
}
} else {
//... handle tiles, or reject the file ...
}
inp->close ();
// END-imageinput-scanlines
}

int main(int /*argc*/, char** /*argv*/)
{
// Each example function needs to get called here, or it won't execute
// as part of the test.
simple_read();
scanlines_read();
return 0;
}
15 changes: 15 additions & 0 deletions testsuite/docs-examples-python/src/docs-examples-imageinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,23 @@ def simple_read():
# END-imageinput-simple


def scanlines_read() :
filename = "scanlines.tif"

# BEGIN-imageinput-scanlines
inp = oiio.ImageInput.open (filename)
spec = inp.spec()
if spec.tile_width == 0 :
for y in range(spec.height) :
scanline = inp.read_scanline (y, 0, "uint8")
# ... process data in scanline[0..width*channels-1] ...
# else :
# ... handle tiles, or reject the file ...
inp.close ()
# END-imageinput-scanlines

if __name__ == '__main__':
# Each example function needs to get called here, or it won't execute
# as part of the test.
simple_read()
scanlines_read()

0 comments on commit e5ad995

Please sign in to comment.