Skip to content

Commit 7da96dd

Browse files
authored
Merge pull request #329 from RupertAvery/fix-328
Fixes Issue #328
2 parents e8d035a + 02750ea commit 7da96dd

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

MetadataExtractor/Formats/Png/PngMetadataReader.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public static DirectoryList ReadMetadata(Stream stream)
9090
/// For more guidance: http://www.w3.org/TR/PNG-Decoders.html#D.Text-chunk-processing
9191
/// </summary>
9292
private static readonly Encoding _latin1Encoding = Encoding.GetEncoding("ISO-8859-1");
93+
private static readonly Encoding _utf8Encoding = Encoding.UTF8;
9394

9495
/// <exception cref="PngProcessingException"/>
9596
/// <exception cref="IOException"/>
@@ -248,15 +249,18 @@ private static IEnumerable<Directory> ProcessChunk(PngChunk chunk)
248249
else if (chunkType == PngChunkType.iTXt)
249250
{
250251
var reader = new SequentialByteArrayReader(bytes);
251-
var keyword = reader.GetNullTerminatedStringValue(maxLengthBytes: 79).ToString(_latin1Encoding);
252+
var keywordStringValue = reader.GetNullTerminatedStringValue(maxLengthBytes: 79);
253+
var keyword = keywordStringValue.ToString(_utf8Encoding);
252254
var compressionFlag = reader.GetSByte();
253255
var compressionMethod = reader.GetSByte();
254256

255257
// TODO we currently ignore languageTagBytes and translatedKeywordBytes
256258
var languageTagBytes = reader.GetNullTerminatedBytes(bytes.Length);
257259
var translatedKeywordBytes = reader.GetNullTerminatedBytes(bytes.Length);
258260

259-
var bytesLeft = bytes.Length - keyword.Length - 1 - 1 - 1 - languageTagBytes.Length - 1 - translatedKeywordBytes.Length - 1;
261+
// bytes left for compressed text is:
262+
// total bytes length - (Keyword length + null byte + comp flag byte + comp method byte + lang length + null byte + translated length + null byte)
263+
var bytesLeft = bytes.Length - keywordStringValue.Bytes.Length - 1 - 1 - 1 - languageTagBytes.Length - 1 - translatedKeywordBytes.Length - 1;
260264
byte[]? textBytes = null;
261265
if (compressionFlag == 0)
262266
{
@@ -432,7 +436,14 @@ IEnumerable<Directory> ProcessTextChunk(string keyword, byte[] textBytes)
432436

433437
static PngDirectory ReadTextDirectory(string keyword, byte[] textBytes, PngChunkType pngChunkType)
434438
{
435-
var textPairs = new[] { new KeyValuePair(keyword, new StringValue(textBytes, _latin1Encoding)) };
439+
var encoding = _latin1Encoding;
440+
441+
if (pngChunkType == PngChunkType.iTXt)
442+
{
443+
encoding = _utf8Encoding;
444+
}
445+
446+
var textPairs = new[] { new KeyValuePair(keyword, new StringValue(textBytes, encoding)) };
436447
var directory = new PngDirectory(pngChunkType);
437448
directory.Set(PngDirectory.TagTextualData, textPairs);
438449
return directory;

0 commit comments

Comments
 (0)