-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathdecode_png.wuffs
1736 lines (1560 loc) · 62.6 KB
/
decode_png.wuffs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
use "std/crc32"
use "std/zlib"
pub struct decoder? implements base.image_decoder(
width : base.u32[..= 0x00FF_FFFF],
height : base.u32[..= 0x00FF_FFFF],
// pass_bytes_per_row doesn't include the 1 byte for the per-row filter.
pass_bytes_per_row : base.u64[..= 0x07FF_FFF8],
workbuf_wi : base.u64,
workbuf_hist_pos_base : base.u64,
// The inclusive upper bound, DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE, is
// derived from the width and height upper bounds at up to 8 bytes per
// pixel. It equals (((8 * M) + 1) * M), where M is 0x00FF_FFFF.
overall_workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007],
pass_workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007],
// The call sequence state machine is discussed in
// (/doc/std/image-decoders-call-sequence.md).
call_sequence : base.u8,
report_metadata_chrm : base.bool,
report_metadata_exif : base.bool,
report_metadata_gama : base.bool,
report_metadata_iccp : base.bool,
report_metadata_kvp : base.bool,
report_metadata_srgb : base.bool,
ignore_checksum : base.bool,
depth : base.u8[..= 16],
color_type : base.u8[..= 6],
filter_distance : base.u8[..= 8],
interlace_pass : base.u8[..= 7],
seen_actl : base.bool,
seen_chrm : base.bool,
seen_fctl : base.bool,
seen_exif : base.bool,
seen_gama : base.bool,
seen_iccp : base.bool,
seen_idat : base.bool,
seen_ihdr : base.bool,
seen_plte : base.bool,
seen_srgb : base.bool,
seen_trns : base.bool,
metadata_is_zlib_compressed : base.bool,
zlib_is_dirty : base.bool,
chunk_type : base.u32,
chunk_type_array : array[4] base.u8,
chunk_length : base.u32,
// remap_transparency, if non-zero, is the 32-bit or 64-bit (depending on
// this.depth) argb_nonpremul color that is nominally opaque but remapped
// to transparent black.
//
// "Remapped" is an unofficial term. This is where the IHDR color type is 0
// (Y) or 2 (RGB) but there's also a tRNS chunk.
//
// PNG transparency that isn't "remapped" is color type 3 (indexed) with
// tRNS, color type 4 (YA) or color type 6 (RGBA).
remap_transparency : base.u64,
dst_pixfmt : base.u32,
src_pixfmt : base.u32,
num_animation_frames_value : base.u32,
num_animation_loops_value : base.u32,
num_decoded_frame_configs_value : base.u32,
num_decoded_frames_value : base.u32,
// The frame_etc fields correspond to the base.frame_config argument passed
// to decode_frame_config. For animated APNG, these are explicitly given in
// the file's fcTL chunks. For still PNG, these are implicitly given in the
// file's IHDR chunk.
//
// Either way, decode_image_config has to read all the way to the start of
// the first IDAT / fdAT chunk (e.g. to see if there's a PLTE chunk). While
// later frame's restart io_positions are located just before fcTL chunks,
// the first frame's restart io_position is located just before the IDAT /
// fdAT chunk, so we also have to cache the first frame's configuration as
// we cannot re-read the first fcTL. Hence, there are first_etc fields for
// every frame_etc field.
//
// For the etc_duration fields, there are 705_600000 flicks per second and
// the maximum frame duration is 65535 seconds.
//
// The fields are ordered to minimize alignment wastage.
frame_rect_x0 : base.u32[..= 0x00FF_FFFF],
frame_rect_y0 : base.u32[..= 0x00FF_FFFF],
frame_rect_x1 : base.u32[..= 0x00FF_FFFF],
frame_rect_y1 : base.u32[..= 0x00FF_FFFF],
first_rect_x0 : base.u32[..= 0x00FF_FFFF],
first_rect_y0 : base.u32[..= 0x00FF_FFFF],
first_rect_x1 : base.u32[..= 0x00FF_FFFF],
first_rect_y1 : base.u32[..= 0x00FF_FFFF],
frame_config_io_position : base.u64,
first_config_io_position : base.u64,
frame_duration : base.u64[..= 0x2A0E_6FF1_6600],
first_duration : base.u64[..= 0x2A0E_6FF1_6600],
frame_disposal : base.u8,
first_disposal : base.u8,
frame_overwrite_instead_of_blend : base.bool,
first_overwrite_instead_of_blend : base.bool,
next_animation_seq_num : base.u32,
metadata_flavor : base.u32,
metadata_fourcc : base.u32,
metadata_x : base.u64,
metadata_y : base.u64,
metadata_z : base.u64,
// ztxt_ri and ztxt_wi are read and write indexes into the dst_palette
// buffer, re-purposed as a zlib uncompression buffer for zTXt chunks. The
// upper bound, 1024, is the same as the dst_palette length.
ztxt_ri : base.u32[..= 1024],
ztxt_wi : base.u32[..= 1024],
// ztxt_hist_pos is the history position: how many uncompressed bytes have
// been generated.
ztxt_hist_pos : base.u64,
swizzler : base.pixel_swizzler,
util : base.utility,
) + (
crc32 : crc32.ieee_hasher,
zlib : zlib.decoder,
// dst_palette and src_palette are used by the swizzler, during
// decode_frame. src_palette is initialized by processing the PLTE chunk.
// dst_palette is also re-purposed as a zlib uncompression buffer for zTXt
// chunks, during decode_image_config.
dst_palette : array[4 * 256] base.u8,
src_palette : array[4 * 256] base.u8,
)
pub func decoder.get_quirk(key: base.u32) base.u64 {
if (args.key == base.QUIRK_IGNORE_CHECKSUM) and this.ignore_checksum {
return 1
}
return 0
}
pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status {
if args.key == base.QUIRK_IGNORE_CHECKSUM {
this.ignore_checksum = args.value > 0
this.zlib.set_quirk!(key: args.key, value: args.value)
return ok
}
return base."#unsupported option"
}
pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
var status : base.status
while true {
status =? this.do_decode_image_config?(dst: args.dst, src: args.src)
if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
yield? status
}
}
pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
var magic : base.u64
var mark : base.u64
var checksum_have : base.u32
var checksum_want : base.u32
var status : base.status
if this.call_sequence <> 0x00 {
return base."#bad call sequence"
} else if not this.seen_ihdr {
magic = args.src.read_u64le?()
if magic <> '\x89PNG\x0D\x0A\x1A\x0A'le {
return "#bad header"
}
magic = args.src.read_u64le?()
if magic <> '\x00\x00\x00\x0DIHDR'le {
if magic == '\x00\x00\x00\x04CgBI'le {
// TODO: add support for Apple's unofficial CgBI extension to
// PNG, once there exists good documentation for it. See
// https://github.com/w3c/PNG-spec/issues/45
return "#unsupported CgBI extension"
}
return "#bad header"
}
this.chunk_type_array[0] = 'I'
this.chunk_type_array[1] = 'H'
this.chunk_type_array[2] = 'D'
this.chunk_type_array[3] = 'R'
this.crc32.reset!()
this.crc32.update_u32!(x: this.chunk_type_array[..])
while true {
mark = args.src.mark()
status =? this.decode_ihdr?(src: args.src)
if not this.ignore_checksum {
checksum_have = this.crc32.update_u32!(x: args.src.since(mark: mark))
}
if status.is_ok() {
break
}
yield? status
}
// Verify CRC-32 checksum.
checksum_want = args.src.read_u32be?()
if (not this.ignore_checksum) and (checksum_have <> checksum_want) {
return "#bad checksum"
}
this.seen_ihdr = true
} else if this.metadata_fourcc <> 0 {
this.call_sequence = 0x10
return base."@metadata reported"
}
// Read up until an IDAT or fdAT chunk.
//
// By default, libpng "warns and discards" when seeing ancillary chunk
// checksum failures (as opposed to critical chunk checksum failures) but
// it still continues to decode the image. Wuffs' decoder is similar,
// simply always ignoring ancillary chunks' CRC-32 checksums.
//
// https://github.com/glennrp/libpng/blob/dbe3e0c43e549a1602286144d94b0666549b18e6/png.h#L1436
//
// We've already seen the IHDR chunk. We're not expecting an IEND chunk. An
// IDAT chunk breaks the loop. The only other possible critical chunk is a
// PLTE chunk. We verify PLTE checksums here but ignore other checksums.
while true {
if args.src.length() < 8 {
yield? base."$short read"
continue
}
this.chunk_length = args.src.peek_u32be()
this.chunk_type = (args.src.peek_u64le() >> 32) as base.u32
if this.chunk_type == 'IDAT'le {
if (not this.seen_actl) or this.seen_fctl {
break
}
this.seen_idat = true
} else if this.chunk_type == 'fdAT'le {
if this.seen_idat and this.seen_fctl {
break
}
return "#bad chunk"
}
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
if (not this.ignore_checksum) and ((this.chunk_type & ANCILLARY_BIT) == 0) {
this.chunk_type_array[0] = ((this.chunk_type >> 0) & 0xFF) as base.u8
this.chunk_type_array[1] = ((this.chunk_type >> 8) & 0xFF) as base.u8
this.chunk_type_array[2] = ((this.chunk_type >> 16) & 0xFF) as base.u8
this.chunk_type_array[3] = ((this.chunk_type >> 24) & 0xFF) as base.u8
this.crc32.reset!()
this.crc32.update_u32!(x: this.chunk_type_array[..])
}
while true {
mark = args.src.mark()
status =? this.decode_other_chunk?(src: args.src, framy: false)
if (not this.ignore_checksum) and ((this.chunk_type & ANCILLARY_BIT) == 0) {
checksum_have = this.crc32.update_u32!(x: args.src.since(mark: mark))
}
if status.is_ok() {
break
}
yield? status
}
// If we have metadata, delay reading (skipping) the ancillary chunk's
// CRC-32 checksum until the end of tell_me_more.
if this.metadata_fourcc <> 0 {
this.call_sequence = 0x10
return base."@metadata reported"
}
checksum_want = args.src.read_u32be?()
if (not this.ignore_checksum) and ((this.chunk_type & ANCILLARY_BIT) == 0) and
(checksum_have <> checksum_want) {
return "#bad checksum"
}
}
if (this.color_type == 3) and (not this.seen_plte) {
return "#missing palette"
}
this.frame_config_io_position = args.src.position()
this.first_config_io_position = this.frame_config_io_position
if args.dst <> nullptr {
args.dst.set!(
pixfmt: this.dst_pixfmt,
pixsub: 0,
width: this.width,
height: this.height,
first_frame_io_position: this.first_config_io_position,
first_frame_is_opaque: (this.color_type <= 3) and (not this.seen_trns))
}
// For still (non-animated) PNGs, the first and only frame's configuration
// is implied by the IHDR chunk instead of an explicit fcTL chunk.
if not this.seen_actl {
this.num_animation_frames_value = 1
this.first_rect_x0 = 0
this.first_rect_y0 = 0
this.first_rect_x1 = this.width
this.first_rect_y1 = this.height
this.first_duration = 0
this.first_disposal = base.ANIMATION_DISPOSAL__NONE
this.first_overwrite_instead_of_blend = false
}
this.call_sequence = 0x20
}
pri func decoder.decode_ihdr?(src: base.io_reader) {
var a32 : base.u32
var a8 : base.u8
a32 = args.src.read_u32be?()
if (a32 == 0) or (a32 > 0x7FFF_FFFF) {
return "#bad header"
} else if a32 > 0xFF_FFFF {
return base."#unsupported image dimension"
}
this.width = a32
a32 = args.src.read_u32be?()
if (a32 == 0) or (a32 > 0x7FFF_FFFF) {
return "#bad header"
} else if a32 > 0xFF_FFFF {
return base."#unsupported image dimension"
}
this.height = a32
// Depth.
a8 = args.src.read_u8?()
if a8 > 16 {
return "#bad header"
}
this.depth = a8
// Color.
a8 = args.src.read_u8?()
if (a8 == 1) or (a8 == 5) or (a8 > 6) {
return "#bad header"
}
this.color_type = a8
// Compression.
a8 = args.src.read_u8?()
if a8 <> 0 {
return "#unsupported PNG compression method"
}
// Filter.
a8 = args.src.read_u8?()
if a8 <> 0 {
return "#bad header"
}
// Interlace.
a8 = args.src.read_u8?()
if a8 == 0 {
this.interlace_pass = 0
} else if a8 == 1 {
this.interlace_pass = 1
choose filter_and_swizzle = [filter_and_swizzle_tricky]
} else {
return "#bad header"
}
// Derived fields.
this.filter_distance = 0
this.assign_filter_distance!()
if this.filter_distance == 0 {
return "#bad header"
}
this.overall_workbuf_length = (this.height as base.u64) *
(1 + this.calculate_bytes_per_row(width: this.width))
this.choose_filter_implementations!()
}
pri func decoder.assign_filter_distance!() {
if this.depth < 8 {
if (this.depth <> 1) and (this.depth <> 2) and (this.depth <> 4) {
return nothing
} else if this.color_type == 0 {
this.dst_pixfmt = base.PIXEL_FORMAT__Y
this.src_pixfmt = base.PIXEL_FORMAT__Y
} else if this.color_type == 3 {
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
} else {
return nothing
}
this.filter_distance = 1
choose filter_and_swizzle = [filter_and_swizzle_tricky]
} else if this.color_type == 0 {
if this.depth == 8 {
this.dst_pixfmt = base.PIXEL_FORMAT__Y
this.src_pixfmt = base.PIXEL_FORMAT__Y
this.filter_distance = 1
} else if this.depth == 16 {
if this.interlace_pass == 0 {
this.dst_pixfmt = base.PIXEL_FORMAT__Y_16LE
this.src_pixfmt = base.PIXEL_FORMAT__Y_16BE
} else {
// Interlaced means choosing filter_and_swizzle_tricky.
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
}
this.filter_distance = 2
}
} else if this.color_type == 2 {
if this.depth == 8 {
this.dst_pixfmt = base.PIXEL_FORMAT__BGR
this.src_pixfmt = base.PIXEL_FORMAT__RGB
this.filter_distance = 3
} else if this.depth == 16 {
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
this.filter_distance = 6
choose filter_and_swizzle = [filter_and_swizzle_tricky]
}
} else if this.color_type == 3 {
if this.depth == 8 {
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_BINARY
this.filter_distance = 1
}
} else if this.color_type == 4 {
if this.depth == 8 {
this.dst_pixfmt = base.PIXEL_FORMAT__YA_NONPREMUL
this.src_pixfmt = base.PIXEL_FORMAT__YA_NONPREMUL
this.filter_distance = 2
} else if this.depth == 16 {
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
this.filter_distance = 4
choose filter_and_swizzle = [filter_and_swizzle_tricky]
}
} else if this.color_type == 6 {
if this.depth == 8 {
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
this.src_pixfmt = base.PIXEL_FORMAT__RGBA_NONPREMUL
this.filter_distance = 4
} else if this.depth == 16 {
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
this.filter_distance = 8
choose filter_and_swizzle = [filter_and_swizzle_tricky]
}
}
}
pri func decoder.calculate_bytes_per_row(width: base.u32[..= 0x00FF_FFFF]) base.u64[..= 0x07FF_FFF8] {
var bytes_per_channel : base.u64[..= 2]
if this.depth == 1 {
return ((args.width + 7) / 8) as base.u64
} else if this.depth == 2 {
return ((args.width + 3) / 4) as base.u64
} else if this.depth == 4 {
return ((args.width + 1) / 2) as base.u64
}
bytes_per_channel = (this.depth >> 3) as base.u64
return (args.width as base.u64) * bytes_per_channel *
(NUM_CHANNELS[this.color_type] as base.u64)
}
pri func decoder.choose_filter_implementations!() {
// Filter 0 is a no-op. Filter 2, the up filter, should already vectorize
// easily by a good optimizing C compiler.
if this.filter_distance == 3 {
choose filter_1 = [filter_1_distance_3_fallback]
choose filter_3 = [filter_3_distance_3_fallback]
choose filter_4 = [
filter_4_distance_3_arm_neon,
filter_4_distance_3_x86_sse42,
filter_4_distance_3_fallback]
} else if this.filter_distance == 4 {
choose filter_1 = [
filter_1_distance_4_arm_neon,
filter_1_distance_4_x86_sse42,
filter_1_distance_4_fallback]
choose filter_3 = [
filter_3_distance_4_arm_neon,
filter_3_distance_4_x86_sse42,
filter_3_distance_4_fallback]
choose filter_4 = [
filter_4_distance_4_arm_neon,
filter_4_distance_4_x86_sse42,
filter_4_distance_4_fallback]
}
}
// framy is:
// - false when coming from decode_image_config.
// - true when coming from decode_frame_config.
pri func decoder.decode_other_chunk?(src: base.io_reader, framy: base.bool) {
if (this.chunk_type == 'PLTE'le) and (not args.framy) {
if this.seen_plte {
return "#bad chunk"
} else if this.color_type == 3 {
// Color type 3 means paletted.
this.decode_plte?(src: args.src)
} else if (this.color_type == 2) or (this.color_type == 6) {
// Color types 2 and 6 means RGB and RGBA. In these cases, the PLTE
// chunk is merely a hint, like a sPLT "suggested palette" chunk.
// We ignore it.
} else {
return "#bad chunk"
}
this.seen_plte = true
} else if (this.chunk_type & ANCILLARY_BIT) == 0 {
if this.chunk_type <> 'IDAT'le {
return "#bad chunk"
}
}
if this.chunk_type == 'eXIf'le {
if this.report_metadata_exif {
if this.seen_exif {
return "#bad chunk"
}
this.decode_exif?(src: args.src)
this.seen_exif = true
}
} else if (this.chunk_type == 'iTXt'le) or
(this.chunk_type == 'tEXt'le) or
(this.chunk_type == 'zTXt'le) {
if this.report_metadata_kvp {
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_RAW_TRANSFORM
this.metadata_fourcc = 'KVPK'be
this.metadata_x = 0
this.metadata_y = 0
this.metadata_z = 0
}
} else if not args.framy {
if this.chunk_type == 'acTL'le {
if this.seen_actl {
return "#bad chunk"
}
this.decode_actl?(src: args.src)
this.seen_actl = true
} else if this.chunk_type == 'cHRM'le {
if this.report_metadata_chrm {
if this.seen_chrm {
return "#bad chunk"
}
this.decode_chrm?(src: args.src)
this.seen_chrm = true
}
} else if this.chunk_type == 'fcTL'le {
if this.seen_fctl {
return "#bad chunk"
}
this.decode_fctl?(src: args.src)
this.seen_fctl = true
} else if this.chunk_type == 'gAMA'le {
if this.report_metadata_gama {
if this.seen_gama {
return "#bad chunk"
}
this.decode_gama?(src: args.src)
this.seen_gama = true
}
} else if this.chunk_type == 'iCCP'le {
if this.report_metadata_iccp {
if this.seen_iccp {
return "#bad chunk"
}
this.decode_iccp?(src: args.src)
this.seen_iccp = true
}
} else if this.chunk_type == 'sRGB'le {
if this.report_metadata_srgb {
if this.seen_srgb {
return "#bad chunk"
}
this.decode_srgb?(src: args.src)
this.seen_srgb = true
}
} else if this.chunk_type == 'tRNS'le {
if this.seen_trns or
((this.color_type == 3) and (not this.seen_plte)) {
return "#bad chunk"
} else if this.color_type > 3 {
// No-op, ignoring the chunk. The PNG spec section "11.3.2.1
// tRNS Transparency" says that "A tRNS chunk shall not appear
// for colour types 4 and 6, since a full alpha channel is
// already present in those cases." However, some real-world
// PNG images (incorrectly) have these. libpng treats this as a
// "benign error", not a fatal one. See
// https://github.com/google/wuffs/issues/146
} else {
this.decode_trns?(src: args.src)
}
this.seen_trns = true
}
}
if this.metadata_fourcc == 0 {
args.src.skip_u32?(n: this.chunk_length)
}
}
pri func decoder.decode_actl?(src: base.io_reader) {
if this.chunk_length <> 8 {
return "#bad chunk"
} else if this.interlace_pass > 0 {
// https://wiki.mozilla.org/APNG_Specification doesn't say whether the
// interlace pattern starts at the image or frame top-left corner. We
// avoid the question by returning "unsupported" for now.
return "#unsupported PNG file"
}
this.chunk_length = 0
this.num_animation_frames_value = args.src.read_u32be?()
if this.num_animation_frames_value == 0 {
return "#bad chunk"
}
this.num_animation_loops_value = args.src.read_u32be?()
}
pri func decoder.decode_chrm?(src: base.io_reader) {
var u : base.u64
if this.chunk_length <> 32 {
return "#bad chunk"
}
this.chunk_length = 0
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_PARSED
this.metadata_fourcc = 'CHRM'be
this.metadata_x = 0
this.metadata_y = 0
this.metadata_z = 0
// See the wuffs_base__more_information__metadata_parsed__chrm comments for
// how we pack the eight chromaticity values into three u64 fields. This
// admittedly truncates chromaticity values from 32 to 24 bits, but in
// practice they only range between 0 and 100000 (which is 0x01_86A0).
u = args.src.read_u32be_as_u64?()
this.metadata_x |= (0xFF_FFFF & u) << 0
u = args.src.read_u32be_as_u64?()
this.metadata_x |= (0xFF_FFFF & u) << 24
u = args.src.read_u32be_as_u64?()
this.metadata_x |= (0xFF_FFFF & u) ~mod<< 48
this.metadata_y |= (0xFF_FFFF & u) >> 16
u = args.src.read_u32be_as_u64?()
this.metadata_y |= (0xFF_FFFF & u) << 8
u = args.src.read_u32be_as_u64?()
this.metadata_y |= (0xFF_FFFF & u) << 32
u = args.src.read_u32be_as_u64?()
this.metadata_y |= (0xFF_FFFF & u) ~mod<< 56
this.metadata_z |= (0xFF_FFFF & u) >> 8
u = args.src.read_u32be_as_u64?()
this.metadata_z |= (0xFF_FFFF & u) << 16
u = args.src.read_u32be_as_u64?()
this.metadata_z |= (0xFF_FFFF & u) << 40
}
pri func decoder.decode_exif?(src: base.io_reader) {
if this.chunk_length < 4 {
return "#bad chunk"
}
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_RAW_PASSTHROUGH
this.metadata_fourcc = 'EXIF'be
this.metadata_x = 0
this.metadata_y = args.src.position()
this.metadata_z = this.metadata_y ~sat+ (this.chunk_length as base.u64)
this.chunk_length = 0
}
pri func decoder.decode_fctl?(src: base.io_reader) {
var x0 : base.u32
var y0 : base.u32
var x1 : base.u32
var y1 : base.u32
if this.chunk_length <> 26 {
return "#bad chunk"
}
this.chunk_length = 0
x0 = args.src.read_u32be?()
if x0 <> this.next_animation_seq_num {
return "#bad animation sequence number"
} else if this.next_animation_seq_num >= 0xFFFF_FFFF {
return "#unsupported PNG file"
}
this.next_animation_seq_num += 1
x1 = args.src.read_u32be?()
y1 = args.src.read_u32be?()
x0 = args.src.read_u32be?()
y0 = args.src.read_u32be?()
x1 ~mod+= x0
y1 ~mod+= y0
if (x0 >= x1) or (x0 > this.width) or (x1 > this.width) or
(y0 >= y1) or (y0 > this.height) or (y1 > this.height) {
return "#bad chunk"
}
assert x1 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: this.width)
assert y1 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: this.height)
assert x0 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: x1)
assert y0 <= 0x00FF_FFFF via "a <= b: a <= c; c <= b"(c: y1)
this.frame_rect_x0 = x0
this.frame_rect_y0 = y0
this.frame_rect_x1 = x1
this.frame_rect_y1 = y1
// There are 705_600000 flicks per second. A nominally zero denominator
// means to use 100 instead (the units are centiseconds, the same as GIF).
x0 = args.src.read_u16be_as_u32?()
x1 = args.src.read_u16be_as_u32?()
if x1 <= 0 {
this.frame_duration = (x0 as base.u64) * 7_056000
} else {
this.frame_duration = ((x0 as base.u64) * 705_600000) / (x1 as base.u64)
}
x0 = args.src.read_u8_as_u32?()
if x0 == 0 {
this.frame_disposal = base.ANIMATION_DISPOSAL__NONE
} else if x0 == 1 {
this.frame_disposal = base.ANIMATION_DISPOSAL__RESTORE_BACKGROUND
} else if x0 == 2 {
this.frame_disposal = base.ANIMATION_DISPOSAL__RESTORE_PREVIOUS
} else {
return "#bad chunk"
}
x0 = args.src.read_u8_as_u32?()
if x0 == 0 {
this.frame_overwrite_instead_of_blend = true
} else if x0 == 1 {
this.frame_overwrite_instead_of_blend = false
} else {
return "#bad chunk"
}
if this.num_decoded_frame_configs_value == 0 {
this.first_rect_x0 = this.frame_rect_x0
this.first_rect_y0 = this.frame_rect_y0
this.first_rect_x1 = this.frame_rect_x1
this.first_rect_y1 = this.frame_rect_y1
this.first_duration = this.frame_duration
this.first_disposal = this.frame_disposal
this.first_overwrite_instead_of_blend = this.frame_overwrite_instead_of_blend
}
}
pri func decoder.decode_gama?(src: base.io_reader) {
if this.chunk_length <> 4 {
return "#bad chunk"
}
this.chunk_length = 0
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_PARSED
this.metadata_fourcc = 'GAMA'be
this.metadata_x = args.src.read_u32be_as_u64?()
this.metadata_y = 0
this.metadata_z = 0
}
pri func decoder.decode_iccp?(src: base.io_reader) {
var c8 : base.u8
// Skip the NUL-terminated color profile name.
while true {
if this.chunk_length <= 0 {
return "#bad chunk"
}
this.chunk_length -= 1
c8 = args.src.read_u8?()
if c8 == 0 {
break
}
}
// Compression method.
if this.chunk_length <= 0 {
return "#bad chunk"
}
this.chunk_length -= 1
c8 = args.src.read_u8?()
if c8 <> 0 {
return "#unsupported PNG compression method"
}
this.metadata_is_zlib_compressed = true
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_RAW_TRANSFORM
this.metadata_fourcc = 'ICCP'be
this.metadata_x = 0
this.metadata_y = 0
this.metadata_z = 0
}
pri func decoder.decode_plte?(src: base.io_reader) {
var num_entries : base.u32[..= 256]
var i : base.u32
var argb : base.u32
if (this.chunk_length > 768) or ((this.chunk_length % 3) <> 0) {
return "#bad chunk"
}
num_entries = (this.chunk_length as base.u32) / 3
this.chunk_length = 0
while i < num_entries {
assert i < 256 via "a < b: a < c; c <= b"(c: num_entries)
// Convert from RGB (in memory order) to ARGB (in native u32 order)
// to BGRA (in memory order).
argb = args.src.read_u24be_as_u32?()
argb |= 0xFF00_0000
this.src_palette[(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
this.src_palette[(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
this.src_palette[(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
this.src_palette[(4 * i) + 3] = ((argb >> 24) & 0xFF) as base.u8
i += 1
}
// Set the remaining palette entries to opaque black.
while i < 256 {
this.src_palette[(4 * i) + 0] = 0x00
this.src_palette[(4 * i) + 1] = 0x00
this.src_palette[(4 * i) + 2] = 0x00
this.src_palette[(4 * i) + 3] = 0xFF
i += 1
}
}
pri func decoder.decode_srgb?(src: base.io_reader) {
if this.chunk_length <> 1 {
return "#bad chunk"
}
this.chunk_length = 0
this.metadata_flavor = base.MORE_INFORMATION__FLAVOR__METADATA_PARSED
this.metadata_fourcc = 'SRGB'be
this.metadata_x = args.src.read_u8_as_u64?()
this.metadata_y = 0
this.metadata_z = 0
}
pri func decoder.decode_trns?(src: base.io_reader) {
var i : base.u32
var n : base.u32[..= 256]
var u : base.u64
if this.color_type == 0 {
choose filter_and_swizzle = [filter_and_swizzle_tricky]
if this.depth <= 8 {
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
} else {
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
}
if this.chunk_length <> 2 {
return "#bad chunk"
}
this.chunk_length = 0
u = args.src.read_u16be_as_u64?()
if this.depth <= 1 {
this.remap_transparency = ((u & 0x01) * 0xFF_FFFF) | 0xFF00_0000
} else if this.depth <= 2 {
this.remap_transparency = ((u & 0x03) * 0x55_5555) | 0xFF00_0000
} else if this.depth <= 4 {
this.remap_transparency = ((u & 0x0F) * 0x11_1111) | 0xFF00_0000
} else if this.depth <= 8 {
this.remap_transparency = ((u & 0xFF) * 0x01_0101) | 0xFF00_0000
} else {
this.remap_transparency = (u * 0x0001_0001_0001) | 0xFFFF_0000_0000_0000
}
} else if this.color_type == 2 {
choose filter_and_swizzle = [filter_and_swizzle_tricky]
if this.depth <= 8 {
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
} else {
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL_4X16LE
}
if this.chunk_length <> 6 {
return "#bad chunk"
}
this.chunk_length = 0
u = args.src.read_u48be_as_u64?()
if this.depth <= 8 {
this.remap_transparency =
(0x0000_00FF & (u >> 0)) |
(0x0000_FF00 & (u >> 8)) |
(0x00FF_0000 & (u >> 16)) |
0xFF00_0000
} else {
this.remap_transparency = u | 0xFFFF_0000_0000_0000
}
} else if this.color_type == 3 {
this.dst_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
this.src_pixfmt = base.PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL
if this.chunk_length > 256 {
return "#bad chunk"
}
n = this.chunk_length as base.u32
this.chunk_length = 0
while i < n {
assert i < 256 via "a < b: a < c; c <= b"(c: n)
this.src_palette[(4 * i) + 3] = args.src.read_u8?()
i += 1
}
} else {
return "#bad chunk"
}
}
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
var status : base.status
while true {
status =? this.do_decode_frame_config?(dst: args.dst, src: args.src)
if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
yield? status
}
}
pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
var checksum_have : base.u32
var pixfmt : base.pixel_format
if (this.call_sequence & 0x10) <> 0 {
return base."#bad call sequence"
} else if this.call_sequence == 0x20 {
// No-op.
} else if this.call_sequence < 0x20 {
this.do_decode_image_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 0x28 {
if this.frame_config_io_position <> args.src.position() {
return base."#bad restart"
}
} else if this.call_sequence == 0x40 {
this.skip_frame?(src: args.src)
} else {
return base."@end of data"
}
if this.metadata_fourcc <> 0 {
this.call_sequence = 0x30
return base."@metadata reported"
}
if this.num_decoded_frame_configs_value == 0 {
this.frame_rect_x0 = this.first_rect_x0
this.frame_rect_y0 = this.first_rect_y0
this.frame_rect_x1 = this.first_rect_x1
this.frame_rect_y1 = this.first_rect_y1
this.frame_config_io_position = this.first_config_io_position
this.frame_duration = this.first_duration
this.frame_disposal = this.first_disposal
this.frame_overwrite_instead_of_blend = this.first_overwrite_instead_of_blend