mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-03 19:45:28 -04:00
#609 Fixed ICC Profile handling in WebP.
(cherry picked from commit ff50180d866d201c351c31520cc884dbe7b03a47)
This commit is contained in:
parent
44a2066b79
commit
9dcf53d985
@ -53,6 +53,7 @@ import javax.imageio.spi.ImageReaderSpi;
|
||||
import java.awt.color.ICC_ColorSpace;
|
||||
import java.awt.color.ICC_Profile;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.ColorConvertOp;
|
||||
import java.awt.image.DataBuffer;
|
||||
import java.awt.image.WritableRaster;
|
||||
import java.io.IOException;
|
||||
@ -218,7 +219,7 @@ final class WebPImageReader extends ImageReaderBase {
|
||||
|
||||
if (header.containsICCP) {
|
||||
// ICCP chunk must be first chunk, if present
|
||||
while (iccProfile != null && imageInput.getStreamPosition() < imageInput.length()) {
|
||||
while (iccProfile == null && imageInput.getStreamPosition() < imageInput.length()) {
|
||||
int nextChunk = imageInput.readInt();
|
||||
long chunkLength = imageInput.readUnsignedInt();
|
||||
long chunkStart = imageInput.getStreamPosition();
|
||||
@ -280,10 +281,6 @@ final class WebPImageReader extends ImageReaderBase {
|
||||
public ImageTypeSpecifier getRawImageType(int imageIndex) throws IOException {
|
||||
readHeader(imageIndex);
|
||||
|
||||
// TODO: Spec says:
|
||||
// "alpha value is codified in bits 31..24, red in bits 23..16, green in bits 15..8 and blue in bits 7..0,
|
||||
// but implementations of the format are free to use another representation internally."
|
||||
// TODO: Doc says alpha flag is "hint only" :-P
|
||||
if (iccProfile != null && !ColorSpaces.isCS_sRGB(iccProfile)) {
|
||||
ICC_ColorSpace colorSpace = ColorSpaces.createColorSpace(iccProfile);
|
||||
int[] bandOffsets = header.containsALPH ? new int[] {0, 1, 2, 3} : new int[] {0, 1, 2};
|
||||
@ -298,6 +295,11 @@ final class WebPImageReader extends ImageReaderBase {
|
||||
ImageTypeSpecifier rawImageType = getRawImageType(imageIndex);
|
||||
|
||||
List<ImageTypeSpecifier> types = new ArrayList<>();
|
||||
|
||||
if (rawImageType.getBufferedImageType() == BufferedImage.TYPE_CUSTOM) {
|
||||
types.add(ImageTypeSpecifiers.createFromBufferedImageType(header.containsALPH ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_3BYTE_BGR));
|
||||
}
|
||||
|
||||
types.add(rawImageType);
|
||||
types.add(ImageTypeSpecifiers.createFromBufferedImageType(header.containsALPH ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB));
|
||||
types.add(ImageTypeSpecifiers.createFromBufferedImageType(header.containsALPH ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_BGR));
|
||||
@ -376,19 +378,22 @@ final class WebPImageReader extends ImageReaderBase {
|
||||
|
||||
case WebP.CHUNK_VP8_:
|
||||
readVP8(RasterUtils.asByteRaster(destination.getRaster())
|
||||
.createWritableChild(0, 0, width, height, 0, 0, new int[]{0, 1, 2}), param);
|
||||
|
||||
.createWritableChild(0, 0, destination.getWidth(), destination.getHeight(), 0, 0, new int[]{0, 1, 2}), param);
|
||||
break;
|
||||
|
||||
case WebP.CHUNK_VP8L:
|
||||
readVP8Lossless(RasterUtils.asByteRaster(destination.getRaster()), param);
|
||||
break;
|
||||
|
||||
case WebP.CHUNK_ICCP:
|
||||
// Ignore, we already read this
|
||||
break;
|
||||
|
||||
case WebP.CHUNK_ANIM:
|
||||
case WebP.CHUNK_ANMF:
|
||||
processWarningOccurred("Ignoring unsupported chunk: " + fourCC(nextChunk));
|
||||
break;
|
||||
|
||||
default:
|
||||
processWarningOccurred("Ignoring unexpected chunk: " + fourCC(nextChunk));
|
||||
break;
|
||||
@ -403,6 +408,8 @@ final class WebPImageReader extends ImageReaderBase {
|
||||
throw new IIOException("Unknown first chunk for WebP: " + fourCC(header.fourCC));
|
||||
}
|
||||
|
||||
applyICCProfileIfNeeded(destination);
|
||||
|
||||
if (abortRequested()) {
|
||||
processReadAborted();
|
||||
} else {
|
||||
@ -412,6 +419,21 @@ final class WebPImageReader extends ImageReaderBase {
|
||||
return destination;
|
||||
}
|
||||
|
||||
private void applyICCProfileIfNeeded(final BufferedImage destination) {
|
||||
if (iccProfile != null) {
|
||||
ICC_Profile destinationProfile = ((ICC_ColorSpace) destination.getColorModel().getColorSpace()).getProfile();
|
||||
|
||||
if (!iccProfile.equals(destinationProfile)) {
|
||||
if (DEBUG) {
|
||||
System.err.println("Converting from " + iccProfile + " to " + (ColorSpaces.isCS_sRGB(destinationProfile) ? "sRGB" : destinationProfile));
|
||||
}
|
||||
|
||||
new ColorConvertOp(new ICC_Profile[] {iccProfile, destinationProfile}, null)
|
||||
.filter(destination.getRaster(), destination.getRaster());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void opaqueAlpha(final WritableRaster alphaRaster) {
|
||||
int h = alphaRaster.getHeight();
|
||||
int w = alphaRaster.getWidth();
|
||||
|
@ -2,8 +2,16 @@ package com.twelvemonkeys.imageio.plugins.webp;
|
||||
|
||||
import com.twelvemonkeys.imageio.util.ImageReaderAbstractTest;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReadParam;
|
||||
import javax.imageio.ImageTypeSpecifier;
|
||||
import javax.imageio.spi.ImageReaderSpi;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
@ -59,4 +67,24 @@ public class WebPImageReaderTest extends ImageReaderAbstractTest<WebPImageReader
|
||||
protected List<String> getMIMETypes() {
|
||||
return asList("image/webp", "image/x-webp");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadAndApplyICCProfile() throws IOException {
|
||||
WebPImageReader reader = createReader();
|
||||
|
||||
try (ImageInputStream stream = ImageIO.createImageInputStream(getClassLoaderResource("/webp/photo-iccp-adobergb.webp"))) {
|
||||
reader.setInput(stream);
|
||||
|
||||
// We'll read a small portion of the image into a a destination type that use sRGB
|
||||
ImageReadParam param = new ImageReadParam();
|
||||
param.setDestinationType(ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_3BYTE_BGR));
|
||||
param.setSourceRegion(new Rectangle(20, 20));
|
||||
|
||||
BufferedImage image = reader.read(0, param);
|
||||
assertRGBEquals("RGB values differ, incorrect ICC profile or conversion?", 0XFFDC9100, image.getRGB(10, 10), 10);
|
||||
}
|
||||
finally {
|
||||
reader.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 102 KiB |
Loading…
x
Reference in New Issue
Block a user