@@ -90,6 +90,7 @@ public static DirectoryList ReadMetadata(Stream stream)
90
90
/// For more guidance: http://www.w3.org/TR/PNG-Decoders.html#D.Text-chunk-processing
91
91
/// </summary>
92
92
private static readonly Encoding _latin1Encoding = Encoding . GetEncoding ( "ISO-8859-1" ) ;
93
+ private static readonly Encoding _utf8Encoding = Encoding . UTF8 ;
93
94
94
95
/// <exception cref="PngProcessingException"/>
95
96
/// <exception cref="IOException"/>
@@ -248,15 +249,18 @@ private static IEnumerable<Directory> ProcessChunk(PngChunk chunk)
248
249
else if ( chunkType == PngChunkType . iTXt )
249
250
{
250
251
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 ) ;
252
254
var compressionFlag = reader . GetSByte ( ) ;
253
255
var compressionMethod = reader . GetSByte ( ) ;
254
256
255
257
// TODO we currently ignore languageTagBytes and translatedKeywordBytes
256
258
var languageTagBytes = reader . GetNullTerminatedBytes ( bytes . Length ) ;
257
259
var translatedKeywordBytes = reader . GetNullTerminatedBytes ( bytes . Length ) ;
258
260
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 ;
260
264
byte [ ] ? textBytes = null ;
261
265
if ( compressionFlag == 0 )
262
266
{
@@ -432,7 +436,14 @@ IEnumerable<Directory> ProcessTextChunk(string keyword, byte[] textBytes)
432
436
433
437
static PngDirectory ReadTextDirectory ( string keyword , byte [ ] textBytes , PngChunkType pngChunkType )
434
438
{
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 ) ) } ;
436
447
var directory = new PngDirectory ( pngChunkType ) ;
437
448
directory . Set ( PngDirectory . TagTextualData , textPairs ) ;
438
449
return directory ;
0 commit comments