TGAImageReader no longer reads single byte 0-terminator as Image Identification

(cherry picked from commit 8a187f6657ce47660c68e19de17642541e4f611a)
This commit is contained in:
Harald Kuhr 2022-05-18 22:47:45 +02:00
parent a5e2226a5a
commit 9213da3184
3 changed files with 23 additions and 28 deletions

View File

@ -33,10 +33,10 @@ package com.twelvemonkeys.imageio.plugins.tga;
import javax.imageio.IIOException; import javax.imageio.IIOException;
import javax.imageio.stream.ImageInputStream; import javax.imageio.stream.ImageInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Calendar; import java.util.Calendar;
import static com.twelvemonkeys.imageio.plugins.tga.TGA.EXT_AREA_SIZE; import static com.twelvemonkeys.imageio.plugins.tga.TGA.EXT_AREA_SIZE;
import static com.twelvemonkeys.imageio.plugins.tga.TGAHeader.readString;
/** /**
* TGAExtensions. * TGAExtensions.
@ -149,26 +149,6 @@ final class TGAExtensions {
return calendar; return calendar;
} }
private static String readString(final ImageInputStream stream, final int maxLength) throws IOException {
byte[] data = new byte[maxLength];
stream.readFully(data);
return asZeroTerminatedASCIIString(data);
}
private static String asZeroTerminatedASCIIString(final byte[] data) {
int len = data.length;
for (int i = 0; i < data.length; i++) {
if (data[i] == 0) {
len = i;
break;
}
}
return new String(data, 0, len, StandardCharsets.US_ASCII);
}
public boolean hasAlpha() { public boolean hasAlpha() {
switch (attributeType) { switch (attributeType) {
case 3: case 3:

View File

@ -253,10 +253,7 @@ final class TGAHeader {
// Image ID section, not *really* part of the header, but let's get rid of it... // Image ID section, not *really* part of the header, but let's get rid of it...
if (imageIdLength > 0) { if (imageIdLength > 0) {
byte[] idBytes = new byte[imageIdLength]; header.identification = readString(imageInput, imageIdLength);
imageInput.readFully(idBytes);
header.identification = new String(idBytes, StandardCharsets.US_ASCII);
} }
// Color map, not *really* part of the header // Color map, not *really* part of the header
@ -267,6 +264,26 @@ final class TGAHeader {
return header; return header;
} }
static String readString(final ImageInputStream stream, final int maxLength) throws IOException {
byte[] data = new byte[maxLength];
stream.readFully(data);
return asZeroTerminatedASCIIString(data);
}
private static String asZeroTerminatedASCIIString(final byte[] data) {
int len = data.length;
for (int i = 0; i < data.length; i++) {
if (data[i] == 0) {
len = i;
break;
}
}
return new String(data, 0, len, StandardCharsets.US_ASCII);
}
private static IndexColorModel readColorMap(final DataInput stream, final TGAHeader header) throws IOException { private static IndexColorModel readColorMap(final DataInput stream, final TGAHeader header) throws IOException {
int size = header.colorMapSize; int size = header.colorMapSize;
int depth = header.colorMapDepth; int depth = header.colorMapDepth;

View File

@ -291,9 +291,7 @@ final class TGAMetadata extends AbstractMetadata {
IIOMetadataNode text = new IIOMetadataNode("Text"); IIOMetadataNode text = new IIOMetadataNode("Text");
// NOTE: Names corresponds to equivalent fields in TIFF // NOTE: Names corresponds to equivalent fields in TIFF
if (header.getIdentification() != null && !header.getIdentification().isEmpty()) {
appendTextEntry(text, "DocumentName", header.getIdentification()); appendTextEntry(text, "DocumentName", header.getIdentification());
}
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()));