Skip to content

Commit b7fda76

Browse files
committed
fix(oiiotool): Fix expression BOTTOM when there are exactly two images (#5046)
Fixes #5044 Oops, the logic was a little mixed up when there were exactly two images. One reason that this was a special case is that conceptually, there is just a stack, but the implementation is that there is a separate variable for the top item, and then the actual stack is all the other items. Also add more thorough testing of TOP/BOTTOM, including what happens for 2, 1, and also 0 items on the image stack (errors in that last case). Signed-off-by: Larry Gritz <lg@larrygritz.com>
1 parent a82bae9 commit b7fda76

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/oiiotool/expressions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Oiiotool::express_parse_atom(const string_view expr, string_view& s,
156156
if (Strutil::parse_prefix(s, "TOP")) {
157157
img = curimg;
158158
} else if (Strutil::parse_prefix(s, "BOTTOM")) {
159-
img = (image_stack.size() <= 1) ? curimg : image_stack[0];
159+
img = image_stack.empty() ? curimg : image_stack[0];
160160
} else if (Strutil::parse_prefix(s, "IMG[")) {
161161
std::string until_bracket = Strutil::parse_until(s, "]");
162162
if (until_bracket.empty() || !Strutil::parse_char(s, ']')) {

testsuite/oiiotool-control/ref/out.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
Stack holds [0] = d.tif, [1] = c.tif, [2] = b.tif
1+
Stack holds [0] = d.tif, [1] = c.tif, [2] = b.tif , [3] = a.tif
22
TOP = d.tif, BOTTOM = a.tif
3+
Stack holds [0] = b.tif, [1] = a.tif
4+
TOP = b.tif, BOTTOM = a.tif
5+
Stack holds [0] = a.tif
6+
TOP = a.tif, BOTTOM = a.tif
7+
Stack is empty
8+
oiiotool ERROR: expression : not a valid image at char 4 of 'TOP.filename'
9+
Full command line was:
10+
> oiiotool --echo "Stack is empty" --echo "TOP = {TOP.filename}"
11+
TOP = TOP.filename
12+
Stack is empty
13+
oiiotool ERROR: expression : not a valid image at char 7 of 'BOTTOM.filename'
14+
Full command line was:
15+
> oiiotool --echo "Stack is empty" --echo "BOTTOM = {BOTTOM.filename}"
16+
BOTTOM = BOTTOM.filename
317
Stack bottom to top:
418
a.tif
519
b.tif

testsuite/oiiotool-control/run.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,26 @@
2323
# Test TOP, BOTTOM, IMG[]
2424
# TOP should be c.tif, BOTTOM should be a.tif
2525
command += oiiotool ("a.tif b.tif c.tif d.tif " +
26-
"--echo \"Stack holds [0] = {IMG[0].filename}, [1] = {IMG[1].filename}, [2] = {IMG[2].filename}\" " +
26+
"--echo \"Stack holds [0] = {IMG[0].filename}, [1] = {IMG[1].filename}, [2] = {IMG[2].filename} , [3] = {IMG[3].filename}\" " +
27+
"--echo \"TOP = {TOP.filename}, BOTTOM = {BOTTOM.filename}\" "
28+
)
29+
# Regression test (Issue #5044): make sure BOTTOM works correctly for 0-2 images
30+
command += oiiotool ("a.tif b.tif " +
31+
"--echo \"Stack holds [0] = {IMG[0].filename}, [1] = {IMG[1].filename}\" " +
32+
"--echo \"TOP = {TOP.filename}, BOTTOM = {BOTTOM.filename}\" "
33+
)
34+
command += oiiotool ("a.tif " +
35+
"--echo \"Stack holds [0] = {IMG[0].filename}\" " +
2736
"--echo \"TOP = {TOP.filename}, BOTTOM = {BOTTOM.filename}\" "
2837
)
38+
# Empty -- should get an error about TOP and BOTTOM not being available
39+
command += oiiotool ("--echo \"Stack is empty\" " +
40+
"--echo \"TOP = {TOP.filename}\" "
41+
)
42+
command += oiiotool ("--echo \"Stack is empty\" " +
43+
"--echo \"BOTTOM = {BOTTOM.filename}\" "
44+
)
45+
2946
# Test --pop, --popbottom, --stackreverse, --stackclear, --stackextract
3047
command += oiiotool (
3148
"a.tif b.tif c.tif d.tif "

0 commit comments

Comments
 (0)