Skip to content

Commit

Permalink
doc_imageinput-errorchecking
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Franz <[email protected]>
  • Loading branch information
pfranz committed Sep 27, 2024
1 parent f875327 commit 24b2b4b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 53 deletions.
63 changes: 10 additions & 53 deletions src/doc/imageinput.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1043,61 +1043,18 @@ Section :ref:`sec-imageinput-made-simple`, but this time it is fully
elaborated with error checking and reporting:

.. tabs::

.. code-tab:: c++

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

const char *filename = "foo.jpg";
auto inp = ImageInput::open (filename);
if (! inp) {
std::cerr << "Could not open " << filename
<< ", error = " << OIIO::geterror() << "\n";
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]);
if (! inp->read_image(0, 0, 0, nchannels, TypeDesc::UINT8, &pixels[0])) {
std::cerr << "Could not read pixels from " << filename
<< ", error = " << inp->geterror() << "\n";
return;
}

if (! inp->close ()) {
std::cerr << "Error closing " << filename
<< ", error = " << inp->geterror() << "\n";
return;
}

.. code-tab:: py

import OpenImageIO as oiio
import numpy as np

filename = "foo.jpg"
inp = ImageInput::open(filename)
if inp is None :
print("Could not open", filename, ", error =", oiio.geterror())
return
spec = inp.spec()
xres = spec.width
yres = spec.height
nchannels = spec.nchannels

pixels = inp.read_image(0, 0, 0, nchannels, "uint8")
if pixels is None :
print("Could not read pixels from", filename, ", error =", inp.geterror())
return
.. tab:: C++
.. literalinclude:: ../../testsuite/docs-examples-cpp/src/docs-examples-imageinput.cpp
:language: c++
:start-after: BEGIN-imageinput-errorchecking
:end-before: END-imageinput-errorchecking

if not inp.close() :
print("Error closing", filename, ", error =", inp.geterror())
return
.. tab:: Python
.. literalinclude:: ../../testsuite/docs-examples-python/src/docs-examples-imageinput.py
:language: py
:start-after: BEGIN-imageinput-errorchecking
:end-before: END-imageinput-errorchecking


.. _sec-imageinput-class-reference:
Expand Down
36 changes: 36 additions & 0 deletions testsuite/docs-examples-cpp/src/docs-examples-imageinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,47 @@ void scanlines_read()
// END-imageinput-scanlines
}


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

void error_checking()
{
const char *filename = "tahoe.tif";
auto inp = ImageInput::open (filename);
if (! inp) {
std::cerr << "Could not open " << filename
<< ", error = " << OIIO::geterror() << "\n";
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]);

if (! inp->read_image(0, 0, 0, nchannels, TypeDesc::UINT8, &pixels[0])) {
std::cerr << "Could not read pixels from " << filename
<< ", error = " << inp->geterror() << "\n";
return;
}

if (! inp->close ()) {
std::cerr << "Error closing " << filename
<< ", error = " << inp->geterror() << "\n";
return;
}
}
// END-imageinput-errorchecking


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();
error_checking();
return 0;
}
26 changes: 26 additions & 0 deletions testsuite/docs-examples-python/src/docs-examples-imageinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,34 @@ def scanlines_read() :
inp.close ()
# END-imageinput-scanlines

def error_checking():
# BEGIN-imageinput-errorchecking
import OpenImageIO as oiio
import numpy as np

filename = "tahoe.tif"
inp = oiio.ImageInput.open(filename)
if inp is None :
print("Could not open", filename, ", error =", oiio.geterror())
return
spec = inp.spec()
xres = spec.width
yres = spec.height
nchannels = spec.nchannels

pixels = inp.read_image(0, 0, 0, nchannels, "uint8")
if pixels is None :
print("Could not read pixels from", filename, ", error =", inp.geterror())
return

if not inp.close() :
print("Error closing", filename, ", error =", inp.geterror())
return
# END-imageinput-errorchecking

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()
error_checking()

0 comments on commit 24b2b4b

Please sign in to comment.