mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-04 20:15:28 -04:00
Fix bug in 0-terminated ASCII string parsing + test.
(cherry picked from commit d50fb1a51ec86ef5e0615386476dad53d3ac6b3b)
This commit is contained in:
parent
6bcc17a020
commit
773bedccca
@ -162,6 +162,7 @@ final class TGAExtensions {
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
if (data[i] == 0) {
|
||||
len = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -393,27 +393,22 @@ final class TGAImageReader extends ImageReaderBase {
|
||||
|
||||
// Read header
|
||||
header = TGAHeader.read(imageInput);
|
||||
|
||||
// System.err.println("header: " + header);
|
||||
|
||||
imageInput.flushBefore(imageInput.getStreamPosition());
|
||||
|
||||
// Read footer, if 2.0 format (ends with TRUEVISION-XFILE\0)
|
||||
skipToEnd(imageInput);
|
||||
imageInput.seek(imageInput.getStreamPosition() - 26);
|
||||
|
||||
long extOffset = imageInput.readInt();
|
||||
/*long devOffset = */imageInput.readInt(); // Ignored for now
|
||||
long extOffset = imageInput.readUnsignedInt();
|
||||
/*long devOffset = */imageInput.readUnsignedInt(); // Ignored for now
|
||||
|
||||
byte[] magic = new byte[18];
|
||||
imageInput.readFully(magic);
|
||||
|
||||
if (Arrays.equals(magic, TGA.MAGIC)) {
|
||||
if (extOffset > 0) {
|
||||
imageInput.seek(extOffset);
|
||||
int extSize = imageInput.readUnsignedShort();
|
||||
extensions = extSize == 0 ? null : TGAExtensions.read(imageInput, extSize);
|
||||
}
|
||||
if (Arrays.equals(magic, TGA.MAGIC) && extOffset > 0) {
|
||||
imageInput.seek(extOffset);
|
||||
int extSize = imageInput.readUnsignedShort();
|
||||
extensions = extSize == 0 ? null : TGAExtensions.read(imageInput, extSize);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -296,7 +296,7 @@ final class TGAMetadata extends AbstractMetadata {
|
||||
}
|
||||
|
||||
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, "UserComment", extensions.getAuthorComments());
|
||||
}
|
||||
@ -305,7 +305,7 @@ final class TGAMetadata extends AbstractMetadata {
|
||||
}
|
||||
|
||||
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");
|
||||
parent.appendChild(textEntry);
|
||||
textEntry.setAttribute("keyword", keyword);
|
||||
|
@ -33,9 +33,14 @@ package com.twelvemonkeys.imageio.plugins.tga;
|
||||
import com.twelvemonkeys.imageio.util.ImageReaderAbstractTest;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReadParam;
|
||||
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.stream.ImageInputStream;
|
||||
import java.awt.*;
|
||||
@ -43,7 +48,7 @@ import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* TGAImageReaderTest
|
||||
@ -137,4 +142,33 @@ public class TGAImageReaderTest extends ImageReaderAbstractTest<TGAImageReader>
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user