Skip to content

Commit

Permalink
#815: Filter metadata and thumbnails before passing to JPEG delegate.
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldk committed Sep 23, 2023
1 parent 4513b0c commit e3cb923
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ else if (sampleModel instanceof MultiPixelPackedSampleModel) {
ListenerDelegate listener = new ListenerDelegate(imageIndex);
jpegWriter.addIIOWriteProgressListener(listener);
jpegWriter.addIIOWriteWarningListener(listener);
jpegWriter.write(null, image, copyParams(param, jpegWriter));
jpegWriter.write(null, imageOnly(image), copyParams(param, jpegWriter));
}
finally {
jpegWriter.dispose();
Expand Down Expand Up @@ -285,6 +285,17 @@ else if (sampleModel instanceof MultiPixelPackedSampleModel) {
return nextIFDPointerOffset;
}

private IIOImage imageOnly(final IIOImage image) {
if (image.getMetadata() == null && image.getNumThumbnails() == 0) {
// Just image data here, no need to copy
return image;
}

return image.hasRaster()
? new IIOImage(image.getRaster(), null, null)
: new IIOImage(image.getRenderedImage(), null, null);
}

// TODO: Candidate util method
private ImageWriteParam copyParams(final ImageWriteParam param, final ImageWriter writer) {
if (param == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.twelvemonkeys.imageio.metadata.Entry;
import com.twelvemonkeys.imageio.metadata.tiff.Rational;
import com.twelvemonkeys.imageio.metadata.tiff.TIFF;
import com.twelvemonkeys.imageio.metadata.tiff.TIFFEntry;
import com.twelvemonkeys.imageio.metadata.tiff.TIFFReader;
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
import com.twelvemonkeys.imageio.util.ImageTypeSpecifiers;
Expand Down Expand Up @@ -73,6 +74,8 @@
import java.util.Arrays;
import java.util.List;

import static com.twelvemonkeys.imageio.metadata.tiff.TIFF.TAG_X_RESOLUTION;
import static com.twelvemonkeys.imageio.metadata.tiff.TIFF.TAG_Y_RESOLUTION;
import static com.twelvemonkeys.imageio.plugins.tiff.TIFFImageMetadataFormat.SUN_NATIVE_IMAGE_METADATA_FORMAT_NAME;
import static com.twelvemonkeys.imageio.plugins.tiff.TIFFImageMetadataTest.createTIFFFieldNode;
import static com.twelvemonkeys.imageio.util.ImageReaderAbstractTest.assertRGBEquals;
Expand Down Expand Up @@ -133,8 +136,8 @@ public void testWriteWithCustomResolutionNative() throws IOException {
customMeta.appendChild(ifd);

createTIFFFieldNode(ifd, TIFF.TAG_RESOLUTION_UNIT, TIFF.TYPE_SHORT, resolutionUnitValue);
createTIFFFieldNode(ifd, TIFF.TAG_X_RESOLUTION, TIFF.TYPE_RATIONAL, resolutionValue);
createTIFFFieldNode(ifd, TIFF.TAG_Y_RESOLUTION, TIFF.TYPE_RATIONAL, resolutionValue);
createTIFFFieldNode(ifd, TAG_X_RESOLUTION, TIFF.TYPE_RATIONAL, resolutionValue);
createTIFFFieldNode(ifd, TAG_Y_RESOLUTION, TIFF.TYPE_RATIONAL, resolutionValue);

metadata.mergeTree(nativeFormat, customMeta);

Expand All @@ -153,11 +156,11 @@ public void testWriteWithCustomResolutionNative() throws IOException {
assertNotNull(resolutionUnit);
assertEquals(resolutionUnitValue, ((Number) resolutionUnit.getValue()).intValue());

Entry xResolution = ifds.getEntryById(TIFF.TAG_X_RESOLUTION);
Entry xResolution = ifds.getEntryById(TAG_X_RESOLUTION);
assertNotNull(xResolution);
assertEquals(resolutionValue, xResolution.getValue());

Entry yResolution = ifds.getEntryById(TIFF.TAG_Y_RESOLUTION);
Entry yResolution = ifds.getEntryById(TAG_Y_RESOLUTION);
assertNotNull(yResolution);
assertEquals(resolutionValue, yResolution.getValue());
}
Expand Down Expand Up @@ -272,11 +275,11 @@ public void testWriteWithCustomResolutionStandard() throws IOException {
assertNotNull(resolutionUnit);
assertEquals(resolutionUnitValue, ((Number) resolutionUnit.getValue()).intValue());

Entry xResolution = ifds.getEntryById(TIFF.TAG_X_RESOLUTION);
Entry xResolution = ifds.getEntryById(TAG_X_RESOLUTION);
assertNotNull(xResolution);
assertEquals(expectedResolutionValue, xResolution.getValue());

Entry yResolution = ifds.getEntryById(TIFF.TAG_Y_RESOLUTION);
Entry yResolution = ifds.getEntryById(TAG_Y_RESOLUTION);
assertNotNull(yResolution);
assertEquals(expectedResolutionValue, yResolution.getValue());
}
Expand Down Expand Up @@ -1358,6 +1361,25 @@ public void testWriteBinaryPalette() throws IOException {
assertEquals(TIFFBaseline.PHOTOMETRIC_PALETTE, directory.getEntryById(TIFF.TAG_PHOTOMETRIC_INTERPRETATION).getValue());
}

@Test
public void testWriteJPEGCompressedShouldNotPassMetadata() throws IOException {
BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR);

try (ImageOutputStream output = new NullImageOutputStream()) {
ImageWriter imageWriter = createWriter();
imageWriter.setOutput(output);

ImageWriteParam param = imageWriter.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("JPEG");

// From #815
// Prior to fix, this would throw IIOException: Metadata components != number of destination bands
// (empty metadata defaults to 1 channel gray, while image data is 3 channel BGR)
imageWriter.write(null, new IIOImage(image, null, new TIFFImageMetadata()), param);
}
}

@Test
public void testShortOverflowHuge() throws IOException {
int width = 34769;
Expand Down

0 comments on commit e3cb923

Please sign in to comment.