diff --git a/src/oiiotool/oiiotool.cpp b/src/oiiotool/oiiotool.cpp index c0ae72f9a2..b28a04f152 100644 --- a/src/oiiotool/oiiotool.cpp +++ b/src/oiiotool/oiiotool.cpp @@ -4077,14 +4077,17 @@ nonzero_region_all_subimages(ImageRecRef A) ROI nonzero_region; for (int s = 0; s < A->subimages(); ++s) { ROI roi = ImageBufAlgo::nonzero_region((*A)(s)); - if (roi.npixels() == 0) { - // Special case -- all zero; but doctor to make it 1 zero pixel - roi = (*A)(s).roi(); - roi.xend = roi.xbegin + 1; - roi.yend = roi.ybegin + 1; - roi.zend = roi.zbegin + 1; + if (roi.npixels() != 0) { + nonzero_region = roi_union(nonzero_region, roi); } - nonzero_region = roi_union(nonzero_region, roi); + } + if (!nonzero_region.defined()) { + // Special case -- all zero + // fallback to 1st subimage ROI, but doctor to make it 1 zero pixel + nonzero_region = (*A)(0).roi(); + nonzero_region.xend = nonzero_region.xbegin + 1; + nonzero_region.yend = nonzero_region.ybegin + 1; + nonzero_region.zend = nonzero_region.zbegin + 1; } return nonzero_region; } diff --git a/testsuite/oiiotool/ref/out.txt b/testsuite/oiiotool/ref/out.txt index 646c659e8d..0bd3534c6d 100644 --- a/testsuite/oiiotool/ref/out.txt +++ b/testsuite/oiiotool/ref/out.txt @@ -68,8 +68,12 @@ Comparing "autotrim.tif" and "ref/autotrim.tif" PASS Comparing "trim.tif" and "ref/trim.tif" PASS +Comparing "trimempty.tif" and "ref/trimempty.tif" +PASS Comparing "trimsubimages.tif" and "ref/trimsubimages.tif" PASS +Comparing "trimemptysubimages.tif" and "ref/trimemptysubimages.tif" +PASS Comparing "add.exr" and "ref/add.exr" PASS Comparing "cadd1.exr" and "ref/cadd1.exr" diff --git a/testsuite/oiiotool/ref/trimempty.tif b/testsuite/oiiotool/ref/trimempty.tif new file mode 100644 index 0000000000..6fb90bc288 Binary files /dev/null and b/testsuite/oiiotool/ref/trimempty.tif differ diff --git a/testsuite/oiiotool/ref/trimemptysubimages.tif b/testsuite/oiiotool/ref/trimemptysubimages.tif new file mode 100644 index 0000000000..7cf421062b Binary files /dev/null and b/testsuite/oiiotool/ref/trimemptysubimages.tif differ diff --git a/testsuite/oiiotool/run.py b/testsuite/oiiotool/run.py index f0d0c3c037..e54b4db546 100755 --- a/testsuite/oiiotool/run.py +++ b/testsuite/oiiotool/run.py @@ -105,11 +105,21 @@ command += oiiotool ("--create 320x240 3 -fill:color=.1,.5,.1 120x80+50+70 " + " -rotate 30 -trim -origin +0+0 -fullpixels -d uint8 -o trim.tif") +# test --trim on empty image +command += oiiotool ("--create 320x240 3 " + + " -trim -origin +0+0 -fullpixels -d uint8 -o trimempty.tif") + # test --trim, tricky case of multiple subimages command += oiiotool ( "-a --create 320x240 3 -fill:color=.1,.5,.1 120x80+50+70 -rotate 30 " + "--create 320x240 3 -fill:color=.5,.5,.1 100x10+70+70 -rotate 140 " + "--siappend -trim -origin +0+0 -fullpixels -d uint8 -o trimsubimages.tif") +# test --trim, tricky case of multiple subimages including one empty one +command += oiiotool ( "-a --create 320x240 3 -fill:color=.1,.5,.1 120x80+50+70 -rotate 30 " + + "--create 320x240 3 -fill:color=.5,.5,.1 100x10+70+70 -rotate 140 " + + "--create 320x240 3 " + + "--siappendall -trim -origin +0+0 -fullpixels -d uint8 -o trimemptysubimages.tif") + # test hole filling command += oiiotool ("ref/hole.tif --fillholes -o tahoe-filled.tif") # test hole filling for a cropped image @@ -262,7 +272,8 @@ outputs = [ "filled.tif", "autotrim.tif", - "trim.tif", "trimsubimages.tif", + "trim.tif", "trimempty.tif", + "trimsubimages.tif", "trimemptysubimages.tif", "add.exr", "cadd1.exr", "cadd2.exr", "sub.exr", "subc.exr", "mul.exr", "cmul1.exr", "cmul2.exr",