Skip to content

Commit 19e3dee

Browse files
kookiCTlgritz
authored andcommitted
fix(oiiotool): Ignore empty subimage(s) when calculating non-zero region (#4909)
Fixes #4799 When trimming an image that contains an empty subimage, the resulting ROI unexpectedly included the top-left pixel even if zero in all subimages. To avoid this, empty ROIs are ignored when computing the overall non-zero region. If all subimages are empty, the first subimage ROI is used and doctored to be 1 pixel. Two tests have been added to the oiiotool testsuite: - 1 to check the trimming of an empty image - 1 to check the trimming of a non-empty image with an empty subimage Signed-off-by: Carine Touraille <[email protected]>
1 parent 71248f7 commit 19e3dee

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

src/oiiotool/oiiotool.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4077,14 +4077,17 @@ nonzero_region_all_subimages(ImageRecRef A)
40774077
ROI nonzero_region;
40784078
for (int s = 0; s < A->subimages(); ++s) {
40794079
ROI roi = ImageBufAlgo::nonzero_region((*A)(s));
4080-
if (roi.npixels() == 0) {
4081-
// Special case -- all zero; but doctor to make it 1 zero pixel
4082-
roi = (*A)(s).roi();
4083-
roi.xend = roi.xbegin + 1;
4084-
roi.yend = roi.ybegin + 1;
4085-
roi.zend = roi.zbegin + 1;
4080+
if (roi.npixels() != 0) {
4081+
nonzero_region = roi_union(nonzero_region, roi);
40864082
}
4087-
nonzero_region = roi_union(nonzero_region, roi);
4083+
}
4084+
if (!nonzero_region.defined()) {
4085+
// Special case -- all zero
4086+
// fallback to 1st subimage ROI, but doctor to make it 1 zero pixel
4087+
nonzero_region = (*A)(0).roi();
4088+
nonzero_region.xend = nonzero_region.xbegin + 1;
4089+
nonzero_region.yend = nonzero_region.ybegin + 1;
4090+
nonzero_region.zend = nonzero_region.zbegin + 1;
40884091
}
40894092
return nonzero_region;
40904093
}

testsuite/oiiotool/ref/out.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,12 @@ Comparing "autotrim.tif" and "ref/autotrim.tif"
6868
PASS
6969
Comparing "trim.tif" and "ref/trim.tif"
7070
PASS
71+
Comparing "trimempty.tif" and "ref/trimempty.tif"
72+
PASS
7173
Comparing "trimsubimages.tif" and "ref/trimsubimages.tif"
7274
PASS
75+
Comparing "trimemptysubimages.tif" and "ref/trimemptysubimages.tif"
76+
PASS
7377
Comparing "add.exr" and "ref/add.exr"
7478
PASS
7579
Comparing "cadd1.exr" and "ref/cadd1.exr"
306 Bytes
Binary file not shown.
11.2 KB
Binary file not shown.

testsuite/oiiotool/run.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,21 @@
105105
command += oiiotool ("--create 320x240 3 -fill:color=.1,.5,.1 120x80+50+70 "
106106
+ " -rotate 30 -trim -origin +0+0 -fullpixels -d uint8 -o trim.tif")
107107

108+
# test --trim on empty image
109+
command += oiiotool ("--create 320x240 3 "
110+
+ " -trim -origin +0+0 -fullpixels -d uint8 -o trimempty.tif")
111+
108112
# test --trim, tricky case of multiple subimages
109113
command += oiiotool ( "-a --create 320x240 3 -fill:color=.1,.5,.1 120x80+50+70 -rotate 30 "
110114
+ "--create 320x240 3 -fill:color=.5,.5,.1 100x10+70+70 -rotate 140 "
111115
+ "--siappend -trim -origin +0+0 -fullpixels -d uint8 -o trimsubimages.tif")
112116

117+
# test --trim, tricky case of multiple subimages including one empty one
118+
command += oiiotool ( "-a --create 320x240 3 -fill:color=.1,.5,.1 120x80+50+70 -rotate 30 "
119+
+ "--create 320x240 3 -fill:color=.5,.5,.1 100x10+70+70 -rotate 140 "
120+
+ "--create 320x240 3 "
121+
+ "--siappendall -trim -origin +0+0 -fullpixels -d uint8 -o trimemptysubimages.tif")
122+
113123
# test hole filling
114124
command += oiiotool ("ref/hole.tif --fillholes -o tahoe-filled.tif")
115125
# test hole filling for a cropped image
@@ -262,7 +272,8 @@
262272
outputs = [
263273
"filled.tif",
264274
"autotrim.tif",
265-
"trim.tif", "trimsubimages.tif",
275+
"trim.tif", "trimempty.tif",
276+
"trimsubimages.tif", "trimemptysubimages.tif",
266277
"add.exr", "cadd1.exr", "cadd2.exr",
267278
"sub.exr", "subc.exr",
268279
"mul.exr", "cmul1.exr", "cmul2.exr",

0 commit comments

Comments
 (0)