From 70d32cc4c0fd43613de0e9c9e36059fc584e0b4c Mon Sep 17 00:00:00 2001 From: Stefan Schweter Date: Thu, 2 Mar 2023 14:43:44 +0100 Subject: [PATCH 1/4] reader: fix region boundary check assertion --- .../turbojpeg/imageio/TurboJpegImageReader.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/imageio-turbojpeg/src/main/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReader.java b/imageio-turbojpeg/src/main/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReader.java index 9830add..83f9780 100644 --- a/imageio-turbojpeg/src/main/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReader.java +++ b/imageio-turbojpeg/src/main/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReader.java @@ -317,12 +317,16 @@ public BufferedImage read(int imageIndex, ImageReadParam param) throws IOExcepti finalWidth = getHeight(0); } - if (region != null - && (region.x + region.width > finalWidth || region.y + region.height > finalHeight)) { - throw new IllegalArgumentException( - String.format( - "Selected region (%dx%d+%d+%d) exceeds the image boundaries (%dx%d).", - region.width, region.height, region.x, region.y, finalWidth, finalHeight)); + if (region != null) { + int selectedWidth = extraCrop != null ? extraCrop.width : region.width; + int selectedHeight = extraCrop != null ? extraCrop.height : region.height; + + if (region.x + selectedWidth > finalWidth || region.y + selectedHeight > finalHeight) { + throw new IllegalArgumentException( + String.format( + "Selected region (%dx%d+%d+%d) exceeds the image boundaries (%dx%d).", + selectedWidth, selectedHeight, region.x, region.y, finalWidth, finalHeight)); + } } if (region != null || rotation != 0) { data = lib.transform(data.array(), info, region, rotation); From 8c8b36d069f707536bb8e23ac4f4cfbe8cd8ecd8 Mon Sep 17 00:00:00 2001 From: Stefan Schweter Date: Thu, 2 Mar 2023 14:44:43 +0100 Subject: [PATCH 2/4] test: add new test case for region boundary check --- .../imageio/TurboJpegImageReaderTest.java | 12 ++++++++++++ .../src/test/resources/mock-page-106245331.jpg | Bin 0 -> 19050 bytes 2 files changed, 12 insertions(+) create mode 100644 imageio-turbojpeg/src/test/resources/mock-page-106245331.jpg diff --git a/imageio-turbojpeg/src/test/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReaderTest.java b/imageio-turbojpeg/src/test/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReaderTest.java index 620f64c..bc4ac40 100644 --- a/imageio-turbojpeg/src/test/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReaderTest.java +++ b/imageio-turbojpeg/src/test/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReaderTest.java @@ -13,6 +13,7 @@ import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; +import org.assertj.core.api.Assertions; import org.assertj.core.util.Lists; import org.junit.jupiter.api.Test; @@ -354,4 +355,15 @@ public void testReadRotatedAndCroppedSpecial() throws IOException { assertThat(rotatedCroppedImage.getHeight()).isEqualTo(50); assertThat(rotatedCroppedImage.getWidth()).isEqualTo(100); } + + @Test + public void testRegionSelect() throws IOException { + ImageReader reader = getReader("mock-page-106245331.jpg"); + TurboJpegImageReadParam param = (TurboJpegImageReadParam) reader.getDefaultReadParam(); + param.setSourceRegion(new Rectangle(0, 0, 750, 1024)); + param.setRotationDegree(90); + BufferedImage image = reader.read(4, param); + + Assertions.assertThat(image.getHeight()).isEqualTo(750); + } } diff --git a/imageio-turbojpeg/src/test/resources/mock-page-106245331.jpg b/imageio-turbojpeg/src/test/resources/mock-page-106245331.jpg new file mode 100644 index 0000000000000000000000000000000000000000..47b23bd785b760b24eb93addab958a8b50e0463d GIT binary patch literal 19050 zcmeI&SxggA6b9fsowCSM7%FbX28k(Ppm9JzC6q-hibz>p*c1i#MiGccS?i06Mj9}p zRzZUi6@geSq@blDBAVC`tyn4q5JV)1hz%eZ9g7CSt1nv2x$|;o&fIhU`JRVrp*oRu zFn=u{VHk$i=r2UIqE*OpmYKPksinD@IfG$o#k99&+So81?bvhexz6NLuCuF)`^sRR zyC>hv#no@KKR+ZiA~M2_7qdMkY+G=6gkYix#$Yg*1e0TH%Mp0EdIaGEhbS=Sc{y3Hce$e-!p7QQ{% z_?Yyw{>|Cb8~pAx@syZ6Hr1SZgDmvTXar6mU-aFpRJy04yEHMGHAqq@gpB)|l;6@< zP@u>vZ7AO5d$6-=o_J$}M5a4FBoyk_2aWd+k*ygn@n*x%8ao%v%DqhOrK2;0+R-m zABy4^kebC`NWS+GS-XdC>$|Zd3cjqIe0%v&NvtX%&r_pHYRsk3?i-I9o;KE(S8r6s zoi8u0Ce@{{hFW8;*pKcS&MkbyJJ3AX+i`cGS0g6b7x&heU8sx|Dg!gT`N~+eg6A(? z!3m00ck)1V8`;KmY_l z00ck)1V8`;KmY_l00ck)1VCU03y6xHW^j6l4gw$m0w4eaAOHd&00JNY0w4eaAOHd& z@RtQt<2CzV45#A(LE@$w(O(`8<3IoeKmY_l00ck)1V8`;KmY_l00ck)1V8`;KmY{( z69MBVI~KG&UuH00L&GWq)HjFjY|zHj9~#Vmm21(gPhYb0Qpbu4>ncL6*r`30UDBvC zBWZmrM@=GM-b+axKVZ?;mTl#8o0*n*U0XGNZM9=P6A6+S zgn*xANq3vBTh7`d38RM@L?ncWoj!FODKA>m&dWJ@!k)3bLXf^HTzObTk1~*iCY1Ik D?sqTQ literal 0 HcmV?d00001 From d5eda378a17bd546d9ff4f25cc583cafce3eaaca Mon Sep 17 00:00:00 2001 From: Stefan Schweter Date: Thu, 2 Mar 2023 17:03:43 +0100 Subject: [PATCH 3/4] test: fix assertThat statement --- .../imageio/TurboJpegImageReaderTest.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/imageio-turbojpeg/src/test/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReaderTest.java b/imageio-turbojpeg/src/test/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReaderTest.java index bc4ac40..e4558ff 100644 --- a/imageio-turbojpeg/src/test/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReaderTest.java +++ b/imageio-turbojpeg/src/test/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReaderTest.java @@ -1,7 +1,12 @@ package de.digitalcollections.turbojpeg.imageio; -import static de.digitalcollections.turbojpeg.imageio.CustomAssertions.assertThat; +import org.assertj.core.util.Lists; +import org.junit.jupiter.api.Test; +import javax.imageio.ImageIO; +import javax.imageio.ImageReadParam; +import javax.imageio.ImageReader; +import javax.imageio.stream.ImageInputStream; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; @@ -9,13 +14,8 @@ import java.io.InputStream; import java.util.List; import java.util.function.Supplier; -import javax.imageio.ImageIO; -import javax.imageio.ImageReadParam; -import javax.imageio.ImageReader; -import javax.imageio.stream.ImageInputStream; -import org.assertj.core.api.Assertions; -import org.assertj.core.util.Lists; -import org.junit.jupiter.api.Test; + +import static de.digitalcollections.turbojpeg.imageio.CustomAssertions.assertThat; class TurboJpegImageReaderTest { @Test @@ -364,6 +364,6 @@ public void testRegionSelect() throws IOException { param.setRotationDegree(90); BufferedImage image = reader.read(4, param); - Assertions.assertThat(image.getHeight()).isEqualTo(750); + assertThat(image.getHeight()).isEqualTo(750); } } From b7576e06d3edb62cee274bf8dc44d8898ad7e12c Mon Sep 17 00:00:00 2001 From: Stefan Schweter Date: Thu, 2 Mar 2023 17:05:43 +0100 Subject: [PATCH 4/4] test: fix assertThat statement --- .../imageio/TurboJpegImageReaderTest.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/imageio-turbojpeg/src/test/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReaderTest.java b/imageio-turbojpeg/src/test/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReaderTest.java index e4558ff..eff6ab1 100644 --- a/imageio-turbojpeg/src/test/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReaderTest.java +++ b/imageio-turbojpeg/src/test/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReaderTest.java @@ -1,12 +1,7 @@ package de.digitalcollections.turbojpeg.imageio; -import org.assertj.core.util.Lists; -import org.junit.jupiter.api.Test; +import static de.digitalcollections.turbojpeg.imageio.CustomAssertions.assertThat; -import javax.imageio.ImageIO; -import javax.imageio.ImageReadParam; -import javax.imageio.ImageReader; -import javax.imageio.stream.ImageInputStream; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; @@ -14,8 +9,12 @@ import java.io.InputStream; import java.util.List; import java.util.function.Supplier; - -import static de.digitalcollections.turbojpeg.imageio.CustomAssertions.assertThat; +import javax.imageio.ImageIO; +import javax.imageio.ImageReadParam; +import javax.imageio.ImageReader; +import javax.imageio.stream.ImageInputStream; +import org.assertj.core.util.Lists; +import org.junit.jupiter.api.Test; class TurboJpegImageReaderTest { @Test