@@ -6,6 +6,7 @@ package png
6
6
7
7
import (
8
8
"bufio"
9
+ "bytes"
9
10
"fmt"
10
11
"image"
11
12
"image/color"
@@ -319,6 +320,64 @@ func TestPalettedDecodeConfig(t *testing.T) {
319
320
}
320
321
}
321
322
323
+ func TestMultipletRNSChunks (t * testing.T ) {
324
+ /*
325
+ The following is a valid 1x1 paletted PNG image with a 1-element palette
326
+ containing color.NRGBA{0xff, 0x00, 0x00, 0x7f}:
327
+ 0000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452 .PNG........IHDR
328
+ 0000010: 0000 0001 0000 0001 0803 0000 0028 cb34 .............(.4
329
+ 0000020: bb00 0000 0350 4c54 45ff 0000 19e2 0937 .....PLTE......7
330
+ 0000030: 0000 0001 7452 4e53 7f80 5cb4 cb00 0000 ....tRNS..\.....
331
+ 0000040: 0e49 4441 5478 9c62 6200 0400 00ff ff00 .IDATx.bb.......
332
+ 0000050: 0600 03fa d059 ae00 0000 0049 454e 44ae .....Y.....IEND.
333
+ 0000060: 4260 82 B`.
334
+ Dropping the tRNS chunk makes that color's alpha 0xff instead of 0x7f.
335
+ */
336
+ const (
337
+ ihdr = "\x00 \x00 \x00 \x0d IHDR\x00 \x00 \x00 \x01 \x00 \x00 \x00 \x01 \x08 \x03 \x00 \x00 \x00 \x28 \xcb \x34 \xbb "
338
+ plte = "\x00 \x00 \x00 \x03 PLTE\xff \x00 \x00 \x19 \xe2 \x09 \x37 "
339
+ trns = "\x00 \x00 \x00 \x01 tRNS\x7f \x80 \x5c \xb4 \xcb "
340
+ idat = "\x00 \x00 \x00 \x0e IDAT\x78 \x9c \x62 \x62 \x00 \x04 \x00 \x00 \xff \xff \x00 \x06 \x00 \x03 \xfa \xd0 \x59 \xae "
341
+ iend = "\x00 \x00 \x00 \x00 IEND\xae \x42 \x60 \x82 "
342
+ )
343
+ for i := 0 ; i < 4 ; i ++ {
344
+ var b []byte
345
+ b = append (b , pngHeader ... )
346
+ b = append (b , ihdr ... )
347
+ b = append (b , plte ... )
348
+ for j := 0 ; j < i ; j ++ {
349
+ b = append (b , trns ... )
350
+ }
351
+ b = append (b , idat ... )
352
+ b = append (b , iend ... )
353
+
354
+ var want color.Color
355
+ m , err := Decode (bytes .NewReader (b ))
356
+ switch i {
357
+ case 0 :
358
+ if err != nil {
359
+ t .Errorf ("%d tRNS chunks: %v" , i , err )
360
+ continue
361
+ }
362
+ want = color.RGBA {0xff , 0x00 , 0x00 , 0xff }
363
+ case 1 :
364
+ if err != nil {
365
+ t .Errorf ("%d tRNS chunks: %v" , i , err )
366
+ continue
367
+ }
368
+ want = color.NRGBA {0xff , 0x00 , 0x00 , 0x7f }
369
+ default :
370
+ if err == nil {
371
+ t .Errorf ("%d tRNS chunks: got nil error, want non-nil" , i )
372
+ }
373
+ continue
374
+ }
375
+ if got := m .At (0 , 0 ); got != want {
376
+ t .Errorf ("%d tRNS chunks: got %T %v, want %T %v" , i , got , got , want , want )
377
+ }
378
+ }
379
+ }
380
+
322
381
func benchmarkDecode (b * testing.B , filename string , bytesPerPixel int ) {
323
382
b .StopTimer ()
324
383
data , err := ioutil .ReadFile (filename )
0 commit comments