#621 Don't add ICC profile for default gray images

(cherry picked from commit 105a1ee466f07d6b3397eac61f17543bdb26fb93)
This commit is contained in:
Harald Kuhr 2021-08-31 22:16:08 +02:00
parent ed46305d31
commit 821965df0d
4 changed files with 62 additions and 3 deletions

View File

@ -239,6 +239,7 @@ public final class ColorSpaces {
* @return {@code true} if {@code profile} is equal to the default sRGB profile. * @return {@code true} if {@code profile} is equal to the default sRGB profile.
* @throws IllegalArgumentException if {@code profile} is {@code null} * @throws IllegalArgumentException if {@code profile} is {@code null}
* *
* @see java.awt.color.ColorSpace#CS_sRGB
* @see java.awt.color.ColorSpace#isCS_sRGB() * @see java.awt.color.ColorSpace#isCS_sRGB()
*/ */
public static boolean isCS_sRGB(final ICC_Profile profile) { public static boolean isCS_sRGB(final ICC_Profile profile) {
@ -247,6 +248,21 @@ public final class ColorSpaces {
return profile.getColorSpaceType() == ColorSpace.TYPE_RGB && Arrays.equals(getProfileHeaderWithProfileId(profile), sRGB.header); return profile.getColorSpaceType() == ColorSpace.TYPE_RGB && Arrays.equals(getProfileHeaderWithProfileId(profile), sRGB.header);
} }
/**
* Tests whether an ICC color profile is equal to the default GRAY profile.
*
* @param profile the ICC profile to test. May not be {@code null}.
* @return {@code true} if {@code profile} is equal to the default GRAY profile.
* @throws IllegalArgumentException if {@code profile} is {@code null}
*
* @see java.awt.color.ColorSpace#CS_GRAY
*/
public static boolean isCS_GRAY(final ICC_Profile profile) {
Validate.notNull(profile, "profile");
return profile.getColorSpaceType() == ColorSpace.TYPE_GRAY && Arrays.equals(getProfileHeaderWithProfileId(profile), GRAY.header);
}
/** /**
* Tests whether an ICC color profile is known to cause problems for {@link java.awt.image.ColorConvertOp}. * Tests whether an ICC color profile is known to cause problems for {@link java.awt.image.ColorConvertOp}.
* <p> * <p>

View File

@ -184,6 +184,24 @@ public class ColorSpacesTest {
ColorSpaces.isCS_sRGB(null); ColorSpaces.isCS_sRGB(null);
} }
@Test
public void testIsCS_GRAYTrue() {
assertTrue(ColorSpaces.isCS_GRAY(ICC_Profile.getInstance(ColorSpace.CS_GRAY)));
}
@Test
public void testIsCS_GRAYFalse() {
assertFalse(ColorSpaces.isCS_GRAY(ICC_Profile.getInstance(ColorSpace.CS_sRGB)));
assertFalse(ColorSpaces.isCS_GRAY(ICC_Profile.getInstance(ColorSpace.CS_LINEAR_RGB)));
assertFalse(ColorSpaces.isCS_GRAY(ICC_Profile.getInstance(ColorSpace.CS_CIEXYZ)));
assertFalse(ColorSpaces.isCS_GRAY(ICC_Profile.getInstance(ColorSpace.CS_PYCC)));
}
@Test(expected = IllegalArgumentException.class)
public void testIsCS_GRAYNull() {
ColorSpaces.isCS_GRAY(null);
}
@Test @Test
public void testEqualHeadersDifferentProfile() throws IOException { public void testEqualHeadersDifferentProfile() throws IOException {
// These profiles are extracted from various JPEGs, and have the exact same profile header... // These profiles are extracted from various JPEGs, and have the exact same profile header...

View File

@ -32,6 +32,7 @@ package com.twelvemonkeys.imageio.plugins.tiff;
import com.twelvemonkeys.image.ImageUtil; import com.twelvemonkeys.image.ImageUtil;
import com.twelvemonkeys.imageio.ImageWriterBase; import com.twelvemonkeys.imageio.ImageWriterBase;
import com.twelvemonkeys.imageio.color.ColorSpaces;
import com.twelvemonkeys.imageio.metadata.Directory; import com.twelvemonkeys.imageio.metadata.Directory;
import com.twelvemonkeys.imageio.metadata.Entry; import com.twelvemonkeys.imageio.metadata.Entry;
import com.twelvemonkeys.imageio.metadata.tiff.Rational; import com.twelvemonkeys.imageio.metadata.tiff.Rational;
@ -849,9 +850,11 @@ public final class TIFFImageWriter extends ImageWriterBase {
else { else {
entries.put(TIFF.TAG_SAMPLES_PER_PIXEL, new TIFFEntry(TIFF.TAG_SAMPLES_PER_PIXEL, numBands)); entries.put(TIFF.TAG_SAMPLES_PER_PIXEL, new TIFFEntry(TIFF.TAG_SAMPLES_PER_PIXEL, numBands));
// Note: Assuming sRGB to be the default RGB interpretation // Embed ICC profile if we have one that:
// * is not sRGB (assuming sRGB to be the default RGB interpretation), and
// * is not gray scale (assuming photometric either BlackIsZero or WhiteIsZero)
ColorSpace colorSpace = colorModel.getColorSpace(); ColorSpace colorSpace = colorModel.getColorSpace();
if (colorSpace instanceof ICC_ColorSpace && !colorSpace.isCS_sRGB()) { if (colorSpace instanceof ICC_ColorSpace && !colorSpace.isCS_sRGB() && !ColorSpaces.isCS_GRAY(((ICC_ColorSpace) colorSpace).getProfile())) {
entries.put(TIFF.TAG_ICC_PROFILE, new TIFFEntry(TIFF.TAG_ICC_PROFILE, ((ICC_ColorSpace) colorSpace).getProfile().getData())); entries.put(TIFF.TAG_ICC_PROFILE, new TIFFEntry(TIFF.TAG_ICC_PROFILE, ((ICC_ColorSpace) colorSpace).getProfile().getData()));
} }
} }

View File

@ -554,6 +554,28 @@ public class TIFFImageWriterTest extends ImageWriterAbstractTest<TIFFImageWriter
} }
} }
@Test
public void testWriteGrayNoProfile() throws IOException {
ImageWriter writer = createWriter();
FastByteArrayOutputStream bytes = new FastByteArrayOutputStream(512);
try (ImageOutputStream output = ImageIO.createImageOutputStream(bytes)) {
writer.setOutput(output);
writer.write(new BufferedImage(10, 10, BufferedImage.TYPE_BYTE_GRAY));
}
try (ImageInputStream input = ImageIO.createImageInputStream(bytes.createInputStream())) {
ImageReader reader = ImageIO.getImageReaders(input).next();
reader.setInput(input);
TIFFImageMetadata metadata = (TIFFImageMetadata) reader.getImageMetadata(0);
Directory ifd = metadata.getIFD();
assertNull("Unexpected ICC profile for default gray", ifd.getEntryById(TIFF.TAG_ICC_PROFILE));
}
}
@Test @Test
public void testWriteParamJPEGQuality() throws IOException { public void testWriteParamJPEGQuality() throws IOException {
ImageWriter writer = createWriter(); ImageWriter writer = createWriter();
@ -583,7 +605,7 @@ public class TIFFImageWriterTest extends ImageWriterAbstractTest<TIFFImageWriter
// Read original LZW compressed TIFF // Read original LZW compressed TIFF
IIOImage original; IIOImage original;
try (ImageInputStream input = ImageIO.createImageInputStream(getClass().getResource("/tiff/a33.tif"))) { try (ImageInputStream input = ImageIO.createImageInputStream(getClassLoaderResource("/tiff/a33.tif"))) {
ImageReader reader = ImageIO.getImageReaders(input).next(); ImageReader reader = ImageIO.getImageReaders(input).next();
reader.setInput(input); reader.setInput(input);