Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Convert ImageInput-errorchecking examples from docs to tests #4456

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading