Skip to content

Commit bf1db92

Browse files
committed
Fix PNG iTXt encoding issue
This is a port of a fix from the .NET library in drewnoakes/metadata-extractor-dotnet#329 PNG chunks of type `iTXt` should have keywords and values decoded using UTF-8, not Latin1 encoding.
1 parent 5754a0d commit bf1db92

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Source/com/drew/imaging/png/PngMetadataReader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class PngMetadataReader
5858
* For more guidance: http://www.w3.org/TR/PNG-Decoders.html#D.Text-chunk-processing
5959
*/
6060
private static Charset _latin1Encoding = Charsets.ISO_8859_1;
61+
private static Charset _utf8Encoding = Charsets.UTF_8;
6162

6263
static
6364
{
@@ -249,7 +250,7 @@ private static void processChunk(@NotNull Metadata metadata, @NotNull PngChunk c
249250
SequentialReader reader = new SequentialByteArrayReader(bytes);
250251

251252
// Keyword is 1-79 bytes, followed by the 1 byte null character
252-
StringValue keywordsv = reader.getNullTerminatedStringValue(79 + 1, _latin1Encoding);
253+
StringValue keywordsv = reader.getNullTerminatedStringValue(79 + 1, _utf8Encoding);
253254
String keyword = keywordsv.toString();
254255
byte compressionFlag = reader.getInt8();
255256
byte compressionMethod = reader.getInt8();
@@ -289,7 +290,7 @@ private static void processChunk(@NotNull Metadata metadata, @NotNull PngChunk c
289290
new XmpReader().extract(textBytes, metadata);
290291
} else {
291292
List<KeyValuePair> textPairs = new ArrayList<KeyValuePair>();
292-
textPairs.add(new KeyValuePair(keyword, new StringValue(textBytes, _latin1Encoding)));
293+
textPairs.add(new KeyValuePair(keyword, new StringValue(textBytes, _utf8Encoding)));
293294
PngDirectory directory = new PngDirectory(PngChunkType.iTXt);
294295
directory.setObject(PngDirectory.TAG_TEXTUAL_DATA, textPairs);
295296
metadata.addDirectory(directory);

0 commit comments

Comments
 (0)