Skip to content

Commit

Permalink
docs: Added tests for Simple Image input and updated rst (#4019)
Browse files Browse the repository at this point in the history
Added the simple input tests from the ImageInput chapter of the docs as
C++ and Python examples, a part of the larger issue #3992 . Followed
prototype
(https://github.com/AcademySoftwareFoundation/OpenImageIO/wiki/Converting-documentation-examples-to-tests)
approach.

---------

Signed-off-by: CalvinSch <[email protected]>
  • Loading branch information
CalvinSch authored Oct 15, 2023
1 parent 1484315 commit a7367a0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
37 changes: 9 additions & 28 deletions src/doc/imageinput.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,16 @@ memory, even if that's not the way they're stored in the file):

.. tabs::

.. code-tab:: c++

#include <OpenImageIO/imageio.h>
using namespace OIIO;
...

auto inp = ImageInput::open(filename);
if (! inp)
return;
const ImageSpec &spec = inp->spec();
int xres = spec.width;
int yres = spec.height;
int nchannels = spec.nchannels;
auto pixels = std::unique_ptr<unsigned char[]>(new unsigned char[xres * yres * nchannels]);
inp->read_image(0, 0, 0, nchannels, TypeDesc::UINT8, &pixels[0]);
inp->close();

.. tab:: C++
.. literalinclude:: ../../testsuite/docs-examples-cpp/src/docs-examples-imageinput.cpp
:language: c++
:start-after: BEGIN-imageinput-simple
:end-before: END-imageinput-simple
.. code-tab:: py

import OpenImageIO as oiio

inp = oiio.ImageInput.open(filename)
if inp :
spec = inp.spec()
xres = spec.width
yres = spec.height
nchannels = spec.nchannels
pixels = inp.read_image(0, 0, 0, nchannels, "uint8")
inp.close()
.. literalinclude:: ../../testsuite/docs-examples-python/src/docs-examples-imageinput.py
:language: py
:start-after: BEGIN-imageinput-simple
:end-before: END-imageinput-simple

Here is a breakdown of what work this code is doing:

Expand Down
26 changes: 25 additions & 1 deletion testsuite/docs-examples-cpp/src/docs-examples-imageinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,34 @@ void example1()



// BEGIN-imageinput-simple
#include <OpenImageIO/imageio.h>
using namespace OIIO;

void simple_read()
{
const char* filename = "tahoe.tif";

auto inp = ImageInput::open(filename);
if (! inp)
return;
const ImageSpec &spec = inp->spec();
int xres = spec.width;
int yres = spec.height;
int nchannels = spec.nchannels;
auto pixels = std::unique_ptr<unsigned char[]>(new unsigned char[xres * yres * nchannels]);
inp->read_image(0, 0, 0, nchannels, TypeDesc::UINT8, &pixels[0]);
inp->close();
}

// END-imageinput-simple



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


# BEGIN-impageinput-simple
import OpenImageIO as oiio
def simple_read():
filename = "tahoe.tif"

inp = oiio.ImageInput.open(filename)
if inp :
spec = inp.spec()
xres = spec.width
yres = spec.height
nchannels = spec.nchannels
pixels = inp.read_image(0, 0, 0, nchannels, "uint8")
inp.close()
# END-imageinput-simple



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

0 comments on commit a7367a0

Please sign in to comment.