Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jpeg cropping edge case #127

Merged
merged 2 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ public ByteBuffer transform(byte[] jpegData, Info info, Rectangle region, int ro
|| ((region.y + region.height) != height && region.height % mcuSize.height != 0)) {
throw new IllegalArgumentException(
String.format(
"Invalid cropping region, width must be divisible by %d, height by %d",
"Invalid cropping region %d×%d, width must be divisible by %d, height by %d",
region.width, region.height,
mcuSize.width, mcuSize.height));
}
width = region.width;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public int getNumImages(boolean allowSearch) throws IOException {
return info.getAvailableSizes().size();
}

private Dimension getDimension(int imageIndex) {
checkIndex(imageIndex);
return info.getAvailableSizes().get(imageIndex);
}

@Override
public int getWidth(int imageIndex) throws IOException {
checkIndex(imageIndex);
Expand All @@ -113,24 +118,28 @@ public Iterator<ImageTypeSpecifier> getImageTypes(int imageIndex) throws IOExcep

/**
* Since TurboJPEG can only crop to values divisible by the MCU size, we may need to expand the
* cropping area to get a suitable rectangle. Thus, cropping becomes a two-stage process: - Crop
* to to nearest MCU boundaries (TurboJPEG) - Crop to the actual region (Java)
* cropping area to get a suitable rectangle. Thus, cropping becomes a two-stage process: 1. Crop
* to to nearest MCU boundaries (TurboJPEG) 2. Crop to the actual region (Java).
* <strong>This method <em>mutates</em> the region!</strong>
*
* <p>Additionally, since TurboJPEG applies rotation **before** cropping, but the ImageIO API is
* based on the assumption that rotation occurs **after** cropping, we have to transform the
* cropping region accordingly.
*
* @param mcuSize The size of the MCUs
* @param region The source region to be cropped
* @param rotation Degrees the image is supposed to be rotated.
* @param imageSize Dimensions of the image the cropping region targets
* @return The region that needs to be cropped from the image cropped to the expanded rectangle
*/
private Rectangle adjustRegion(Dimension mcuSize, Rectangle region, int rotation)
throws IOException {
Rectangle adjustRegion(Dimension mcuSize, Rectangle region, int rotation, Dimension imageSize) {
if (region == null) {
return null;
}
final int originalWidth = getWidth(0);
final int originalHeight = getHeight(0);
final int originalWidth = imageSize.width;
final int originalHeight = imageSize.height;

// Recalculate the cropping region based on the desired rotation.
final Rectangle originalRegion = (Rectangle) region.clone();
if (rotation == 90) {
int x = region.x;
Expand All @@ -151,38 +160,43 @@ private Rectangle adjustRegion(Dimension mcuSize, Rectangle region, int rotation
region.width = region.height;
region.height = w;
}

// Calculate how much of the region returned from libjpeg has to be cropped on the JVM-side
Rectangle extraCrop =
new Rectangle(
0,
0,
region.width == 0 ? originalWidth - region.x : region.width,
region.height == 0 ? originalHeight - region.y : region.height);
// X-Offset + Width
if (region.x % mcuSize.width != 0) {
extraCrop.x = region.x % mcuSize.width;
region.x -= extraCrop.x;
if (region.width > 0) {
region.width = Math.min(region.width + extraCrop.x, originalWidth - region.x);
}
}
// Y-Offset + Height
if (region.y % mcuSize.height != 0) {
extraCrop.y = region.y % mcuSize.height;
region.y -= extraCrop.y;
if (region.height > 0) {
region.height = Math.min(region.height + extraCrop.y, originalHeight - region.y);
}
}

if ((region.x + region.width) != originalWidth && region.width % mcuSize.width != 0) {
region.width = (int) (mcuSize.width * (Math.ceil(region.getWidth() / mcuSize.width)));
region.width = Math.min(
(int) (mcuSize.width * (Math.ceil(region.getWidth() / mcuSize.width))),
imageSize.width - region.x);
}

if ((region.y + region.height) != originalHeight && region.height % mcuSize.height != 0) {
region.height = (int) (mcuSize.height * (Math.ceil(region.getHeight() / mcuSize.height)));
}
if (region.height > originalHeight) {
region.height = originalHeight;
}
if (region.width > originalWidth) {
region.width = originalWidth;
region.height = Math.min(
(int) (mcuSize.height * (Math.ceil(region.getHeight() / mcuSize.height))),
imageSize.height - region.y);
}

boolean modified =
originalRegion.x != region.x
|| originalRegion.y != region.y
Expand Down Expand Up @@ -259,7 +273,8 @@ public BufferedImage read(int imageIndex, ImageReadParam param) throws IOExcepti
region = param.getSourceRegion();
if (!isRegionFullImage(imageIndex, region)) {
scaleRegion(imageIndex, region);
extraCrop = adjustRegion(info.getMCUSize(), region, rotation);
// adjustments need native image size → imageIndex == 0
extraCrop = adjustRegion(info.getMCUSize(), region, rotation, getDimension(0));
} else {
region = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static de.digitalcollections.turbojpeg.imageio.CustomAssertions.assertThat;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -249,4 +250,21 @@ public void testCroppingRequiresReallocation() throws IOException {
assertThat(img.getWidth()).isEqualTo(365);
assertThat(img.getHeight()).isEqualTo(10);
}

@Test
void testAdjustMCURegion() {
TurboJpegImageReader reader = new TurboJpegImageReader(null, null);

Dimension mcuSize = new Dimension(16, 16);
Rectangle region = new Rectangle(1185, 327, 309, 36);
int rotation = 0;
Dimension imageSize = new Dimension(1500, 2260);

Rectangle extraCrop = reader.adjustRegion(mcuSize, region, rotation, imageSize);
Rectangle regionExpected = new Rectangle(1184, 320, 316, 48);
Rectangle extraCropExpected = new Rectangle(1, 7, 309, 36);

assertThat(region).isEqualTo(regionExpected);
assertThat(extraCrop).isEqualTo(extraCropExpected);
}
}