Skip to content

Commit 51b9e2f

Browse files
lydia-zhengscott-wilson
authored andcommitted
feat(oiiotool): oiiotool new expression eval tokens IS_CONSTANT, IS_BLACK (AcademySoftwareFoundation#4610)
Included two additional metadata boolean checks for constant and black frame images, so that handling for those image cases are facilitated. IS_CONSTANT and IS_BLACK return 1 when true, and 0 when false. Signed-off-by: Lydia Zheng <[email protected]> Signed-off-by: Scott Wilson <[email protected]>
1 parent 8c863e2 commit 51b9e2f

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

src/doc/oiiotool.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ contents of an expression may be any of:
163163
information from when the file was read from disk.
164164
* `STATS` : a multi-line string containing the image statistics that would
165165
be printed with `oiiotool -stats`.
166-
166+
* `IS_CONSTANT`: metadata to check if the image pixels are of constant color, returns 1 if true, and 0 if false.
167+
* `IS_BLACK`: metadata to check if the image pixels are all black, a subset of IS_CONSTANT. Also returns 1 if true, and 0 if false.
168+
167169
* *imagename.'metadata'*
168170

169171
If the metadata name is not a "C identifier" (initial letter followed by

src/oiiotool/expressions.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,28 @@ Oiiotool::express_parse_atom(const string_view expr, string_view& s,
317317
result = out.str();
318318
if (result.size() && result.back() == '\n')
319319
result.pop_back();
320+
} else if (metadata == "IS_CONSTANT") {
321+
std::vector<float> color((*img)(0, 0).nchannels());
322+
if (ImageBufAlgo::isConstantColor((*img)(0, 0), 0.0f, color)) {
323+
result = "1";
324+
} else {
325+
result = "0";
326+
}
327+
} else if (metadata == "IS_BLACK") {
328+
std::vector<float> color((*img)(0, 0).nchannels());
329+
// Check constant first to guard against false positive average of 0 with negative values i.e. -2, 1, 1
330+
if (ImageBufAlgo::isConstantColor((*img)(0, 0), 0.0f, color)) {
331+
// trusting that the constantcolor check means all channels have the same value, so we only check the first channel
332+
if (color[0] == 0.0f) {
333+
result = "1";
334+
} else {
335+
result = "0";
336+
}
337+
} else {
338+
// Not even constant color case -> We don't want those to count as black frames.
339+
result = "0";
340+
}
341+
320342
} else if (using_bracket) {
321343
// For the TOP[meta] syntax, if the metadata doesn't exist,
322344
// return the empty string, and do not make an error.

testsuite/oiiotool-control/ref/out.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ TOP = ../common/grid.tif, BOTTOM = ../common/tahoe-tiny.tif
326326
Stack holds [1] = ../common/tahoe-small.tif
327327
filename=../common/tahoe-tiny.tif file_extension=.tif file_noextension=../common/tahoe-tiny
328328
MINCOLOR=0,0,0 MAXCOLOR=0.745098,1,1 AVGCOLOR=0.101942,0.216695,0.425293
329+
Testing expressions IS_BLACK, IS_CONSTANT:
330+
grey is-black? 0 is-constant? 1
331+
black is-black? 1 is-constant? 1
332+
gradient is-black? 0 is-constant? 0
329333
Testing NIMAGES:
330334
0
331335
1

testsuite/oiiotool-control/run.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@
183183
"--echo \"filename={TOP.filename} file_extension={TOP.file_extension} file_noextension={TOP.file_noextension}\" " +
184184
"--echo \"MINCOLOR={TOP.MINCOLOR} MAXCOLOR={TOP.MAXCOLOR} AVGCOLOR={TOP.AVGCOLOR}\"")
185185

186+
command += oiiotool ("--echo \"Testing expressions IS_BLACK, IS_CONSTANT:\" " +
187+
"--pattern:type=uint16 constant:color=0.5,0.5,0.5 4x4 3 " +
188+
"--echo \" grey is-black? {TOP.IS_BLACK} is-constant? {TOP.IS_CONSTANT}\" " +
189+
"--pattern:type=uint16 constant:color=0,0,0 4x4 3 " +
190+
"--echo \" black is-black? {TOP.IS_BLACK} is-constant? {TOP.IS_CONSTANT}\" " +
191+
"--pattern:type=uint16 fill:left=0,0,0:right=1,1,1 4x4 3 " +
192+
"--echo \" gradient is-black? {TOP.IS_BLACK} is-constant? {TOP.IS_CONSTANT}\" "
193+
)
186194
command += oiiotool (
187195
"--echo \"Testing NIMAGES:\" " +
188196
"--echo \" {NIMAGES}\" " +

0 commit comments

Comments
 (0)