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); 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..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 @@ -354,4 +354,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); + + 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 0000000..47b23bd Binary files /dev/null and b/imageio-turbojpeg/src/test/resources/mock-page-106245331.jpg differ