TMI-117: Fix for "componentId attribute out of range" issue.

This commit is contained in:
Harald Kuhr 2015-03-12 12:55:55 +01:00
parent 493edada59
commit 02a4277413
3 changed files with 66 additions and 1 deletions

View File

@ -233,6 +233,26 @@ final class JPEGImage10MetadataCleaner {
markerSequence.insertBefore(unknown, next); markerSequence.insertBefore(unknown, next);
} }
// Known issues in the com.sun classes, if sof/sos component id or selector is negative,
// setFromTree will fail. We'll fix the range from -128...127 to be 0...255.
NodeList sofs = markerSequence.getElementsByTagName("sof");
if (sofs.getLength() > 0) {
NodeList components = sofs.item(0).getChildNodes();
for (int i = 0; i < components.getLength(); i++) {
forceComponentIdInRange((IIOMetadataNode) components.item(i), "componentId");
}
}
NodeList sos = markerSequence.getElementsByTagName("sos");
for (int i = 0; i < sos.getLength(); i++) {
NodeList specs = sos.item(i).getChildNodes();
for (int j = 0; j < specs.getLength(); j++) {
forceComponentIdInRange((IIOMetadataNode) specs.item(j), "componentSelector");
}
}
// Inconsistency issue in the com.sun classes, it can read metadata with dht containing // Inconsistency issue in the com.sun classes, it can read metadata with dht containing
// more than 4 children, but will not allow setting such a tree... // more than 4 children, but will not allow setting such a tree...
// We'll split AC/DC tables into separate dht nodes. // We'll split AC/DC tables into separate dht nodes.
@ -277,4 +297,29 @@ final class JPEGImage10MetadataCleaner {
return imageMetadata; return imageMetadata;
} }
private void forceComponentIdInRange(final IIOMetadataNode component, final String attributeName) {
String attribute = component.getAttribute(attributeName);
if (attribute != null) {
try {
int componentId = Integer.parseInt(attribute);
if (componentId < 0) {
// Metadata doesn't like negative component ids/specs
// We'll convert to the positive value it probably should have been
componentId = ((byte) componentId) & 0xff;
component.setAttribute(attributeName, String.valueOf(componentId));
}
}
catch (NumberFormatException ignore) {
if ("scanComponentSpec".equals(component.getNodeName())) {
reader.processWarningOccurred("Bad SOS component selector: " + attribute);
}
else {
reader.processWarningOccurred("Bad SOF component id: " + attribute);
}
}
}
}
} }

View File

@ -1349,8 +1349,9 @@ public class JPEGImageReaderTest extends ImageReaderAbstractTestCase<JPEGImageRe
@Test @Test
public void testInvalidDHTIssue() throws IOException { public void testInvalidDHTIssue() throws IOException {
// Image has JFIF with DHT that is okay on read, but not when you set back from tree... // Image has JFIF, and DHT that is okay on read, but not when you set back from tree...
JPEGImageReader reader = createReader(); JPEGImageReader reader = createReader();
try { try {
reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/jfif-progressive-invalid-dht.jpg"))); reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/jfif-progressive-invalid-dht.jpg")));
@ -1360,7 +1361,26 @@ public class JPEGImageReaderTest extends ImageReaderAbstractTestCase<JPEGImageRe
Node tree = metadata.getAsTree(metadata.getNativeMetadataFormatName()); Node tree = metadata.getAsTree(metadata.getNativeMetadataFormatName());
assertNotNull(tree); assertNotNull(tree);
assertThat(tree, new IsInstanceOf(IIOMetadataNode.class)); assertThat(tree, new IsInstanceOf(IIOMetadataNode.class));
}
finally {
reader.dispose();
}
}
@Test
public void testComponentIdOutOfRange() throws IOException {
// Image has JFIF, but SOF and SOS component ids are off, but not when you set back from tree...
JPEGImageReader reader = createReader();
try {
reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/jfif-component-id-out-of-range.jpg")));
IIOMetadata metadata = reader.getImageMetadata(0);
assertNotNull(metadata);
Node tree = metadata.getAsTree(metadata.getNativeMetadataFormatName());
assertNotNull(tree);
assertThat(tree, new IsInstanceOf(IIOMetadataNode.class));
} }
finally { finally {
reader.dispose(); reader.dispose();

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB