mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-05 04:25:29 -04:00
TGAImageReader no longer reads single byte 0-terminator as Image Identification
(cherry picked from commit 8a187f6657ce47660c68e19de17642541e4f611a)
This commit is contained in:
parent
a5e2226a5a
commit
9213da3184
@ -33,10 +33,10 @@ package com.twelvemonkeys.imageio.plugins.tga;
|
||||
import javax.imageio.IIOException;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Calendar;
|
||||
|
||||
import static com.twelvemonkeys.imageio.plugins.tga.TGA.EXT_AREA_SIZE;
|
||||
import static com.twelvemonkeys.imageio.plugins.tga.TGAHeader.readString;
|
||||
|
||||
/**
|
||||
* TGAExtensions.
|
||||
@ -149,26 +149,6 @@ final class TGAExtensions {
|
||||
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() {
|
||||
switch (attributeType) {
|
||||
case 3:
|
||||
|
@ -253,10 +253,7 @@ final class TGAHeader {
|
||||
|
||||
// Image ID section, not *really* part of the header, but let's get rid of it...
|
||||
if (imageIdLength > 0) {
|
||||
byte[] idBytes = new byte[imageIdLength];
|
||||
imageInput.readFully(idBytes);
|
||||
|
||||
header.identification = new String(idBytes, StandardCharsets.US_ASCII);
|
||||
header.identification = readString(imageInput, imageIdLength);
|
||||
}
|
||||
|
||||
// Color map, not *really* part of the header
|
||||
@ -267,6 +264,26 @@ final class TGAHeader {
|
||||
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 {
|
||||
int size = header.colorMapSize;
|
||||
int depth = header.colorMapDepth;
|
||||
|
@ -291,9 +291,7 @@ final class TGAMetadata extends AbstractMetadata {
|
||||
IIOMetadataNode text = new IIOMetadataNode("Text");
|
||||
|
||||
// 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) {
|
||||
appendTextEntry(text, "Software", extensions.getSoftwareVersion() == null ? extensions.getSoftware() : (extensions.getSoftware() + " " + extensions.getSoftwareVersion()));
|
||||
|
Loading…
x
Reference in New Issue
Block a user