Support writing ASCII array in TIFF metadata (#656)

* Support writing ASCII array in TIFF metadata

* corrected formatting and extracted string writing to method
This commit is contained in:
Oliver Schmidtmer 2022-01-12 18:56:22 +01:00 committed by GitHub
parent b8614eca4d
commit 74611e4e52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 5 deletions

View File

@ -292,7 +292,19 @@ public final class TIFFWriter extends MetadataWriter {
private int getCount(final Entry entry) { private int getCount(final Entry entry) {
Object value = entry.getValue(); Object value = entry.getValue();
return value instanceof String ? ((String) value).getBytes(StandardCharsets.UTF_8).length + 1 : entry.valueCount(); if (value instanceof String) {
return ((String) value).getBytes(StandardCharsets.UTF_8).length + 1;
}
else if (value instanceof String[]) {
int sum = 0;
for (String string : (String[]) value) {
sum += string.getBytes(StandardCharsets.UTF_8).length + 1;
}
return sum;
}
else {
return entry.valueCount();
}
} }
private void writeValueInline(final Object value, final short type, final ImageOutputStream stream) throws IOException { private void writeValueInline(final Object value, final short type, final ImageOutputStream stream) throws IOException {
@ -410,7 +422,12 @@ public final class TIFFWriter extends MetadataWriter {
break; break;
} }
case TIFF.TYPE_ASCII:
String[] strings = (String[]) value;
for (String string : strings) {
writeString(stream, string);
}
break;
default: default:
throw new IllegalArgumentException("Unsupported TIFF type: " + type); throw new IllegalArgumentException("Unsupported TIFF type: " + type);
} }
@ -423,9 +440,7 @@ public final class TIFFWriter extends MetadataWriter {
stream.writeByte(((Number) value).intValue()); stream.writeByte(((Number) value).intValue());
break; break;
case TIFF.TYPE_ASCII: case TIFF.TYPE_ASCII:
byte[] bytes = ((String) value).getBytes(StandardCharsets.UTF_8); writeString(stream, (String) value);
stream.write(bytes);
stream.write(0);
break; break;
case TIFF.TYPE_SHORT: case TIFF.TYPE_SHORT:
case TIFF.TYPE_SSHORT: case TIFF.TYPE_SSHORT:
@ -462,6 +477,11 @@ public final class TIFFWriter extends MetadataWriter {
} }
} }
private void writeString(ImageOutputStream stream, String value) throws IOException {
stream.write(value.getBytes(StandardCharsets.UTF_8));
stream.write(0);
}
private void writeValueAt(final long dataOffset, final Object value, final short type, final ImageOutputStream stream) throws IOException { private void writeValueAt(final long dataOffset, final Object value, final short type, final ImageOutputStream stream) throws IOException {
writeOffset(stream, dataOffset); writeOffset(stream, dataOffset);
long position = stream.getStreamPosition(); long position = stream.getStreamPosition();

View File

@ -47,8 +47,10 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/** /**
* TIFFWriterTest * TIFFWriterTest
@ -254,6 +256,27 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
assertEquals(98, stream.getStreamPosition()); assertEquals(98, stream.getStreamPosition());
} }
@Test
public void testWriteASCIIArray() throws IOException {
ArrayList<Entry> entries = new ArrayList<>();
String[] strings = new String []{"Twelve", "Monkeys", "ImageIO"};
entries.add(new AbstractEntry(TIFF.TAG_SOFTWARE, strings) {});
Directory directory = new AbstractDirectory(entries) {};
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
ImageOutputStream imageStream = ImageIO.createImageOutputStream(output);
imageStream.setByteOrder(ByteOrder.LITTLE_ENDIAN); // LE = Intel
createWriter().write(directory, imageStream);
imageStream.flush();
byte[] data = output.toByteArray();
Directory read = new TIFFReader().read(new ByteArrayImageInputStream(data));
assertNotNull(read.getEntryById(TIFF.TAG_SOFTWARE));
assertTrue("value not an string array", read.getEntryById(TIFF.TAG_SOFTWARE).getValue() instanceof String[]);
assertArrayEquals("", strings, (String[]) read.getEntryById(TIFF.TAG_SOFTWARE).getValue());
}
@Test @Test
public void testComputeIFDSizeNested() throws IOException { public void testComputeIFDSizeNested() throws IOException {
TIFFEntry artist = new TIFFEntry(TIFF.TAG_SOFTWARE, TIFF.TYPE_ASCII, "TwelveMonkeys ImageIO"); TIFFEntry artist = new TIFFEntry(TIFF.TAG_SOFTWARE, TIFF.TYPE_ASCII, "TwelveMonkeys ImageIO");