Skip to content

Commit 735bba9

Browse files
pfranzlgritz
authored andcommitted
docs: Convert ImageInput-unassociatedalpha examples from docs to tests (#4468)
Convert error checking C++ and Python examples from the "imageinput" chapter into tests within the "docs-examples" testsuites (#3992). I grabbed unpremut.tif from the output of another test. -- Signed-off-by: Paul Franz <pfranz@laika.com>
1 parent 4aaaa73 commit 735bba9

10 files changed

Lines changed: 60 additions & 31 deletions

File tree

src/doc/imageinput.rst

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -902,37 +902,19 @@ Examples:
902902
automatically be converted to associated alpha.
903903

904904
.. tabs::
905-
906-
.. code-tab:: c++
907-
908-
// Set up an ImageSpec that holds the configuration hints.
909-
ImageSpec config;
910-
config["oiio:UnassociatedAlpha"] = 1;
911-
912-
// Open the file, passing in the config.
913-
auto inp = ImageInput::open (filename, config);
914-
const ImageSpec &spec = inp->spec();
915-
auto pixels = std::unique_ptr<unsigned char[]>(new unsigned char[spec.image_pixels() * spec.nchannels]);
916-
inp->read_image (0, 0, 0, spec.nchannels, TypeDesc::UINT8, pixels.data());
917-
if (spec.get_int_attribute("oiio:UnassociatedAlpha"))
918-
printf("pixels holds unassociated alpha\n");
919-
else
920-
printf("pixels holds associated alpha\n");
921-
922-
.. code-tab:: py
923-
924-
# Set up an ImageSpec that holds the configuration hints.
925-
config = ImageSpec()
926-
config["oiio:UnassociatedAlpha"] = 1
927-
928-
# Open the file, passing in the config.
929-
inp = ImageInput.open (filename, config)
930-
spec = inp.spec()
931-
pixels = inp.read_image (0, 0, 0, spec.nchannels, "uint8")
932-
if (spec["oiio:UnassociatedAlpha"] == 1)
933-
print("pixels holds unassociated alpha")
934-
else
935-
print("pixels holds associated alpha")
905+
906+
.. tab:: C++
907+
.. literalinclude:: ../../testsuite/docs-examples-cpp/src/docs-examples-imageinput.cpp
908+
:language: c++
909+
:start-after: BEGIN-imageinput-unassociatedalpha
910+
:end-before: END-imageinput-unassociatedalpha
911+
912+
.. tab:: Python
913+
.. literalinclude:: ../../testsuite/docs-examples-python/src/docs-examples-imageinput.py
914+
:language: py
915+
:start-after: BEGIN-imageinput-unassociatedalpha
916+
:end-before: END-imageinput-unassociatedalpha
917+
936918

937919
.. _sec-imageinput-ioproxy:
938920

testsuite/common/unpremult.tif

1.63 KB
Binary file not shown.

testsuite/docs-examples-cpp/ref/out-arm.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pixels holds unassociated alpha
12
example_output_error1
23
error: Uninitialized input image
34
example_output_error2

testsuite/docs-examples-cpp/ref/out.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pixels holds unassociated alpha
12
example_output_error1
23
error: Uninitialized input image
34
example_output_error2

testsuite/docs-examples-cpp/run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
command += run_app("cmake -E copy " + test_source_dir + "/../common/grid-small.exr B.exr")
1818
command += run_app("cmake -E copy " + test_source_dir + "/../common/with_nans.tif with_nans.tif")
1919
command += run_app("cmake -E copy " + test_source_dir + "/../common/checker_with_alpha.exr checker_with_alpha.exr")
20+
command += run_app("cmake -E copy " + test_source_dir + "/../common/unpremult.tif unpremult.tif")
2021

2122
# Copy the grid to both a tiled and scanline version
2223
command += oiio_app("iconvert") + "../common/grid.tif --scanline scanline.tif > out.txt ;"

testsuite/docs-examples-cpp/src/docs-examples-imageinput.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,27 @@ void tiles_read()
9999
// END-imageinput-tiles
100100
}
101101

102+
void unassociatedalpha()
103+
{
104+
const char* filename = "unpremult.tif";
105+
106+
// BEGIN-imageinput-unassociatedalpha
107+
// Set up an ImageSpec that holds the configuration hints.
108+
ImageSpec config;
109+
config["oiio:UnassociatedAlpha"] = 1;
110+
111+
// Open the file, passing in the config.
112+
auto inp = ImageInput::open (filename, &config);
113+
const ImageSpec &spec = inp->spec();
114+
auto pixels = std::unique_ptr<unsigned char[]>(new unsigned char[spec.image_pixels() * spec.nchannels]);
115+
inp->read_image (0, 0, 0, spec.nchannels, TypeDesc::UINT8, &pixels[0]);
116+
if (spec.get_int_attribute("oiio:UnassociatedAlpha"))
117+
printf("pixels holds unassociated alpha\n");
118+
else
119+
printf("pixels holds associated alpha\n");
120+
// END-imageinput-unassociatedalpha
121+
}
122+
102123

103124
// BEGIN-imageinput-errorchecking
104125
#include <OpenImageIO/imageio.h>
@@ -142,6 +163,7 @@ int main(int /*argc*/, char** /*argv*/)
142163
simple_read();
143164
scanlines_read();
144165
tiles_read();
166+
unassociatedalpha();
145167
error_checking();
146168
return 0;
147169
}

testsuite/docs-examples-python/ref/out-arm.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pixels holds unassociated alpha
12
example1
23
example_output_error1
34
error: Uninitialized input image

testsuite/docs-examples-python/ref/out.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pixels holds unassociated alpha
12
example1
23
example_output_error1
34
error: Uninitialized input image

testsuite/docs-examples-python/run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
command += run_app("cmake -E copy " + test_source_dir + "/../common/grid-small.exr B.exr")
1818
command += run_app("cmake -E copy " + test_source_dir + "/../common/with_nans.tif with_nans.tif")
1919
command += run_app("cmake -E copy " + test_source_dir + "/../common/checker_with_alpha.exr checker_with_alpha.exr")
20+
command += run_app("cmake -E copy " + test_source_dir + "/../common/unpremult.tif unpremult.tif")
2021

2122
# Copy the grid to both a tiled and scanline version
2223
command += oiio_app("iconvert") + "../common/grid.tif --scanline scanline.tif > out.txt ;"

testsuite/docs-examples-python/src/docs-examples-imageinput.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@ def tiles_read() :
8686
# END-imageinput-tiles
8787

8888

89+
def unassociated_alpha():
90+
filename = "unpremult.tif"
91+
92+
# BEGIN-imageinput-unassociatedalpha
93+
# Set up an ImageSpec that holds the configuration hints.
94+
config = oiio.ImageSpec()
95+
config["oiio:UnassociatedAlpha"] = 1
96+
97+
# Open the file, passing in the config.
98+
inp = oiio.ImageInput.open (filename, config)
99+
spec = inp.spec()
100+
pixels = inp.read_image (0, 0, 0, spec.nchannels, "uint8")
101+
if (spec["oiio:UnassociatedAlpha"] == 1):
102+
print("pixels holds unassociated alpha")
103+
else:
104+
print("pixels holds associated alpha")
105+
# END-imageinput-unassociatedalpha
106+
89107
def error_checking():
90108
# BEGIN-imageinput-errorchecking
91109
import OpenImageIO as oiio
@@ -119,4 +137,5 @@ def error_checking():
119137
simple_read()
120138
scanlines_read()
121139
tiles_read()
140+
unassociated_alpha()
122141
error_checking()

0 commit comments

Comments
 (0)