TMI-104: Fixed metadata inconsistency, 1 channel SOF now always interpreted as color space Gray, regardless of Adobe App14 marker.

This commit is contained in:
Harald Kuhr 2015-03-03 14:44:24 +01:00
parent d6f90b0b52
commit 37d4c03548
4 changed files with 38 additions and 8 deletions

View File

@ -156,10 +156,12 @@ final class JPEGImage10MetadataCleaner {
} }
// Special case: Broken AdobeDCT segment, inconsistent with SOF, use values from SOF // Special case: Broken AdobeDCT segment, inconsistent with SOF, use values from SOF
if (adobeDCT != null && adobeDCT.getTransform() == AdobeDCTSegment.YCCK && sof.componentsInFrame() < 4) { if (adobeDCT != null && (adobeDCT.getTransform() == AdobeDCTSegment.YCCK && sof.componentsInFrame() < 4 ||
adobeDCT.getTransform() == AdobeDCTSegment.YCC && sof.componentsInFrame() < 3)) {
reader.processWarningOccurred(String.format( reader.processWarningOccurred(String.format(
"Invalid Adobe App14 marker. Indicates YCCK/CMYK data, but SOF%d has %d color components. " + "Invalid Adobe App14 marker. Indicates %s data, but SOF%d has %d color component(s). " +
"Ignoring Adobe App14 marker.", "Ignoring Adobe App14 marker.",
adobeDCT.getTransform() == AdobeDCTSegment.YCCK ? "YCCK/CMYK" : "YCC/RGB",
sof.marker & 0xf, sof.componentsInFrame() sof.marker & 0xf, sof.componentsInFrame()
)); ));

View File

@ -323,7 +323,7 @@ public class JPEGImageReader extends ImageReaderBase {
if (adobeDCT != null && (adobeDCT.getTransform() == AdobeDCTSegment.YCC && sof.componentsInFrame() != 3 || if (adobeDCT != null && (adobeDCT.getTransform() == AdobeDCTSegment.YCC && sof.componentsInFrame() != 3 ||
adobeDCT.getTransform() == AdobeDCTSegment.YCCK && sof.componentsInFrame() != 4)) { adobeDCT.getTransform() == AdobeDCTSegment.YCCK && sof.componentsInFrame() != 4)) {
processWarningOccurred(String.format( processWarningOccurred(String.format(
"Invalid Adobe App14 marker. Indicates %s data, but SOF%d has %d color components. " + "Invalid Adobe App14 marker. Indicates %s data, but SOF%d has %d color component(s). " +
"Ignoring Adobe App14 marker.", "Ignoring Adobe App14 marker.",
adobeDCT.getTransform() == AdobeDCTSegment.YCCK ? "YCCK/CMYK" : "YCC/RGB", adobeDCT.getTransform() == AdobeDCTSegment.YCCK ? "YCCK/CMYK" : "YCC/RGB",
sof.marker & 0xf, sof.componentsInFrame() sof.marker & 0xf, sof.componentsInFrame()
@ -1075,8 +1075,7 @@ public class JPEGImageReader extends ImageReaderBase {
@Override @Override
public IIOMetadata getImageMetadata(int imageIndex) throws IOException { public IIOMetadata getImageMetadata(int imageIndex) throws IOException {
// TMI-101: As we catch the IndexOutOfBoundsException below, we need to make sure that we don't catch the // checkBounds needed, as we catch the IndexOutOfBoundsException below.
// IndexOutOfBoundsException that should be thrown, if (imageIndex < 0 || imageIndex > numImages).
checkBounds(imageIndex); checkBounds(imageIndex);
IIOMetadata imageMetadata; IIOMetadata imageMetadata;

View File

@ -80,6 +80,7 @@ public class JPEGImageReaderTest extends ImageReaderAbstractTestCase<JPEGImageRe
@Override @Override
protected List<TestData> getTestData() { protected List<TestData> getTestData() {
// While a lot of these files don't conform to any spec (Exif/JFIF), we will read these.
return Arrays.asList( return Arrays.asList(
new TestData(getClassLoaderResource("/jpeg/cmm-exception-adobe-rgb.jpg"), new Dimension(626, 76)), new TestData(getClassLoaderResource("/jpeg/cmm-exception-adobe-rgb.jpg"), new Dimension(626, 76)),
new TestData(getClassLoaderResource("/jpeg/cmm-exception-srgb.jpg"), new Dimension(1800, 1200)), new TestData(getClassLoaderResource("/jpeg/cmm-exception-srgb.jpg"), new Dimension(1800, 1200)),
@ -96,10 +97,11 @@ public class JPEGImageReaderTest extends ImageReaderAbstractTestCase<JPEGImageRe
} }
protected List<TestData> getBrokenTestData() { protected List<TestData> getBrokenTestData() {
// These files are considered too broken to be read (ie. most other software does not read them either).
return Arrays.asList( return Arrays.asList(
new TestData(getClassLoaderResource("/broken-jpeg/broken-bogus-segment-length.jpg"), new Dimension(467, 612)), new TestData(getClassLoaderResource("/broken-jpeg/broken-bogus-segment-length.jpg"), new Dimension(467, 612)), // Semi-readable, parts missing
new TestData(getClassLoaderResource("/broken-jpeg/broken-adobe-marker-bad-length.jpg"), new Dimension(1800, 1200)), new TestData(getClassLoaderResource("/broken-jpeg/broken-adobe-marker-bad-length.jpg"), new Dimension(1800, 1200)), // Unreadable, segment lengths are wrong
new TestData(getClassLoaderResource("/broken-jpeg/broken-invalid-adobe-ycc-gray.jpg"), new Dimension(11, 440)) new TestData(getClassLoaderResource("/broken-jpeg/broken-invalid-adobe-ycc-gray.jpg"), new Dimension(11, 440)) // Image readable, broken metadata (fixable?)
); );
// More test data in specific tests below // More test data in specific tests below
@ -475,6 +477,33 @@ public class JPEGImageReaderTest extends ImageReaderAbstractTestCase<JPEGImageRe
} }
} }
@Test
public void testImageMetadata1ChannelGrayWithBogusAdobeYCC() throws IOException {
JPEGImageReader reader = createReader();
try {
// Any sample should do here
reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/invalid-adobe-ycc-gray-with-metadata.jpg")));
IIOMetadata metadata = reader.getImageMetadata(0);
IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);
IIOMetadataNode chroma = getSingleElementByName(root, "Chroma");
IIOMetadataNode numChannels = getSingleElementByName(chroma, "NumChannels");
assertEquals("1", numChannels.getAttribute("value"));
IIOMetadataNode colorSpaceType = getSingleElementByName(chroma, "ColorSpaceType");
assertEquals("GRAY", colorSpaceType.getAttribute("name"));
}
finally {
reader.dispose();
}
}
private IIOMetadataNode getSingleElementByName(final IIOMetadataNode root, final String name) {
NodeList elements = root.getElementsByTagName(name);
assertEquals(1, elements.getLength());
return (IIOMetadataNode) elements.item(0);
}
@Test(expected = IndexOutOfBoundsException.class) @Test(expected = IndexOutOfBoundsException.class)
public void testGetImageMetadataOutOfBounds() throws IOException { public void testGetImageMetadataOutOfBounds() throws IOException {
JPEGImageReader reader = createReader(); JPEGImageReader reader = createReader();

Binary file not shown.

After

Width:  |  Height:  |  Size: 299 B