mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-10-04 11:26:44 -04:00
Fix bug in 0-terminated ASCII string parsing + test.
This commit is contained in:
@@ -162,6 +162,7 @@ final class TGAExtensions {
|
|||||||
for (int i = 0; i < data.length; i++) {
|
for (int i = 0; i < data.length; i++) {
|
||||||
if (data[i] == 0) {
|
if (data[i] == 0) {
|
||||||
len = i;
|
len = i;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -393,27 +393,22 @@ final class TGAImageReader extends ImageReaderBase {
|
|||||||
|
|
||||||
// Read header
|
// Read header
|
||||||
header = TGAHeader.read(imageInput);
|
header = TGAHeader.read(imageInput);
|
||||||
|
|
||||||
// System.err.println("header: " + header);
|
|
||||||
|
|
||||||
imageInput.flushBefore(imageInput.getStreamPosition());
|
imageInput.flushBefore(imageInput.getStreamPosition());
|
||||||
|
|
||||||
// Read footer, if 2.0 format (ends with TRUEVISION-XFILE\0)
|
// Read footer, if 2.0 format (ends with TRUEVISION-XFILE\0)
|
||||||
skipToEnd(imageInput);
|
skipToEnd(imageInput);
|
||||||
imageInput.seek(imageInput.getStreamPosition() - 26);
|
imageInput.seek(imageInput.getStreamPosition() - 26);
|
||||||
|
|
||||||
long extOffset = imageInput.readInt();
|
long extOffset = imageInput.readUnsignedInt();
|
||||||
/*long devOffset = */imageInput.readInt(); // Ignored for now
|
/*long devOffset = */imageInput.readUnsignedInt(); // Ignored for now
|
||||||
|
|
||||||
byte[] magic = new byte[18];
|
byte[] magic = new byte[18];
|
||||||
imageInput.readFully(magic);
|
imageInput.readFully(magic);
|
||||||
|
|
||||||
if (Arrays.equals(magic, TGA.MAGIC)) {
|
if (Arrays.equals(magic, TGA.MAGIC) && extOffset > 0) {
|
||||||
if (extOffset > 0) {
|
imageInput.seek(extOffset);
|
||||||
imageInput.seek(extOffset);
|
int extSize = imageInput.readUnsignedShort();
|
||||||
int extSize = imageInput.readUnsignedShort();
|
extensions = extSize == 0 ? null : TGAExtensions.read(imageInput, extSize);
|
||||||
extensions = extSize == 0 ? null : TGAExtensions.read(imageInput, extSize);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -296,7 +296,7 @@ final class TGAMetadata extends AbstractMetadata {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (extensions != null) {
|
if (extensions != null) {
|
||||||
appendTextEntry(text, "Software", extensions.getSoftwareVersion() == null ? extensions.getSoftware() : extensions.getSoftware() + " " + extensions.getSoftwareVersion());
|
appendTextEntry(text, "Software", extensions.getSoftwareVersion() == null ? extensions.getSoftware() : (extensions.getSoftware() + " " + extensions.getSoftwareVersion()));
|
||||||
appendTextEntry(text, "Artist", extensions.getAuthorName());
|
appendTextEntry(text, "Artist", extensions.getAuthorName());
|
||||||
appendTextEntry(text, "UserComment", extensions.getAuthorComments());
|
appendTextEntry(text, "UserComment", extensions.getAuthorComments());
|
||||||
}
|
}
|
||||||
@@ -305,7 +305,7 @@ final class TGAMetadata extends AbstractMetadata {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void appendTextEntry(final IIOMetadataNode parent, final String keyword, final String value) {
|
private void appendTextEntry(final IIOMetadataNode parent, final String keyword, final String value) {
|
||||||
if (value != null) {
|
if (value != null && !value.isEmpty()) {
|
||||||
IIOMetadataNode textEntry = new IIOMetadataNode("TextEntry");
|
IIOMetadataNode textEntry = new IIOMetadataNode("TextEntry");
|
||||||
parent.appendChild(textEntry);
|
parent.appendChild(textEntry);
|
||||||
textEntry.setAttribute("keyword", keyword);
|
textEntry.setAttribute("keyword", keyword);
|
||||||
|
@@ -33,9 +33,14 @@ package com.twelvemonkeys.imageio.plugins.tga;
|
|||||||
import com.twelvemonkeys.imageio.util.ImageReaderAbstractTest;
|
import com.twelvemonkeys.imageio.util.ImageReaderAbstractTest;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
import javax.imageio.ImageReadParam;
|
import javax.imageio.ImageReadParam;
|
||||||
import javax.imageio.ImageReader;
|
import javax.imageio.ImageReader;
|
||||||
|
import javax.imageio.metadata.IIOMetadata;
|
||||||
|
import javax.imageio.metadata.IIOMetadataFormatImpl;
|
||||||
|
import javax.imageio.metadata.IIOMetadataNode;
|
||||||
import javax.imageio.spi.ImageReaderSpi;
|
import javax.imageio.spi.ImageReaderSpi;
|
||||||
import javax.imageio.stream.ImageInputStream;
|
import javax.imageio.stream.ImageInputStream;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
@@ -43,7 +48,7 @@ import java.io.IOException;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TGAImageReaderTest
|
* TGAImageReaderTest
|
||||||
@@ -137,4 +142,33 @@ public class TGAImageReaderTest extends ImageReaderAbstractTest<TGAImageReader>
|
|||||||
|
|
||||||
reader.dispose();
|
reader.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMetadataTextEntries() throws IOException {
|
||||||
|
ImageReader reader = createReader();
|
||||||
|
|
||||||
|
try (ImageInputStream input = ImageIO.createImageInputStream(getClassLoaderResource("/tga/autodesk-3dsmax-extsize494.tga"))) {
|
||||||
|
reader.setInput(input);
|
||||||
|
IIOMetadata metadata = reader.getImageMetadata(0);
|
||||||
|
IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);
|
||||||
|
|
||||||
|
NodeList textEntries = root.getElementsByTagName("TextEntry");
|
||||||
|
boolean softwareFound = false;
|
||||||
|
|
||||||
|
for (int i = 0; i < root.getLength(); i++) {
|
||||||
|
IIOMetadataNode software = (IIOMetadataNode) textEntries.item(i);
|
||||||
|
|
||||||
|
if ("Software".equals(software.getAttribute("keyword"))) {
|
||||||
|
assertEquals("Autodesk 3ds max 1.0", software.getAttribute("value"));
|
||||||
|
softwareFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue("No Software TextEntry", softwareFound);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
reader.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user