Skip to content

Commit

Permalink
fix(webp): handle grayscale images
Browse files Browse the repository at this point in the history
  • Loading branch information
gotson committed Oct 19, 2023
1 parent cca7a1f commit 4223a0c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.ImageInputStream;
import java.awt.color.ColorSpace;
import java.awt.color.ICC_ColorSpace;
import java.awt.color.ICC_Profile;
import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -47,19 +48,23 @@ private void readInfo() throws IOException {
info = WebP.getBasicInfo((ImageInputStream) input);
if (info.iccProfile() != null) {
ICC_ColorSpace colorSpace = ColorSpaces.createColorSpace(info.iccProfile());
imageTypes = List.of(
ImageTypeSpecifiers.createPacked(colorSpace, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, DataBuffer.TYPE_INT, false),
ImageTypeSpecifier.createFromBufferedImageType(info.hasAlpha() ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB)
);
} else if (info.hasAnimation()) {
if (colorSpace.getType() == ColorSpace.TYPE_RGB) {
imageTypes = List.of(
ImageTypeSpecifiers.createPacked(colorSpace, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, DataBuffer.TYPE_INT, false),
ImageTypeSpecifier.createFromBufferedImageType(info.hasAlpha() ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB)
);
return;
}
}
if (info.hasAnimation()) {
imageTypes = List.of(
ImageTypeSpecifiers.createPacked(ColorSpaces.getColorSpace(CS_sRGB), 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff, DataBuffer.TYPE_INT, false)
);
} else {
imageTypes = List.of(
ImageTypeSpecifier.createFromBufferedImageType(info.hasAlpha() ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB)
);
return;
}
imageTypes = List.of(
ImageTypeSpecifier.createFromBufferedImageType(info.hasAlpha() ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB)
);
} catch (WebpException e) {
throw new IOException(e);
}
Expand Down Expand Up @@ -165,6 +170,9 @@ protected void resetMembers() {

private void applyICCProfileIfNeeded(final BufferedImage destination) {
if (info.iccProfile() != null) {
ICC_ColorSpace colorSpace = ColorSpaces.createColorSpace(info.iccProfile());
if (colorSpace.getType() != ColorSpace.TYPE_RGB) return;

ColorModel colorModel = destination.getColorModel();
ICC_Profile destinationProfile = ((ICC_ColorSpace) colorModel.getColorSpace()).getProfile();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ protected List<TestData> getTestData() {
new TestData(getClassLoaderResource("/webp/1_webp_a.webp"), new Dimension(400, 301)),
// Extendad format: Anim
new TestData(getClassLoaderResource("/webp/animated-webp-supported.webp"), Collections.nCopies(12, new Dimension(400, 400)), null),
new TestData(getClassLoaderResource("/webp/animated-banana.webp"), Collections.nCopies(8, new Dimension(990, 1050)), null)
new TestData(getClassLoaderResource("/webp/animated-banana.webp"), Collections.nCopies(8, new Dimension(990, 1050)), null),
// Grayscale
new TestData(getClassLoaderResource("/webp/gray.webp"), new Dimension(766, 1100))
);
}

Expand Down
Binary file added imageio-webp/src/test/resources/webp/gray.webp
Binary file not shown.

0 comments on commit 4223a0c

Please sign in to comment.