wuffsfmt: indent with 4 spaces (not a tab)

This commit is contained in:
Nigel Tao committed 2023-04-09 15:28:07 +10:00
1 parent 9c03b8c71d
commit f5eb38d76e
40 files changed
+12586 -12580

No files matched your search

+64 -64
View File
@@ -58,87 +58,87 @@ pri const ANCILLARY_BIT : base.u32 = 0x0000_0020
// 4: y_stride - y_offset - 1
// 5: y_offset
pri const INTERLACING : array[8] array[6] base.u8[..= 7] = [
[0, 0, 0, 0, 0, 0], // non-interlaced; xy_stride=1, xy_offset=0
[3, 7, 0, 3, 7, 0], // interlace_pass == 1
[3, 3, 4, 3, 7, 0], // interlace_pass == 2
[2, 3, 0, 3, 3, 4], // interlace_pass == 3
[2, 1, 2, 2, 3, 0], // interlace_pass == 4
[1, 1, 0, 2, 1, 2], // interlace_pass == 5
[1, 0, 1, 1, 1, 0], // interlace_pass == 6
[0, 0, 0, 1, 0, 1], // interlace_pass == 7
[0, 0, 0, 0, 0, 0], // non-interlaced; xy_stride=1, xy_offset=0
[3, 7, 0, 3, 7, 0], // interlace_pass == 1
[3, 3, 4, 3, 7, 0], // interlace_pass == 2
[2, 3, 0, 3, 3, 4], // interlace_pass == 3
[2, 1, 2, 2, 3, 0], // interlace_pass == 4
[1, 1, 0, 2, 1, 2], // interlace_pass == 5
[1, 0, 1, 1, 1, 0], // interlace_pass == 6
[0, 0, 0, 1, 0, 1], // interlace_pass == 7
]
// LOW_BIT_DEPTH_MULTIPLIERS holds multipliers that convert D-bit values into
// 8-bit values, for depth D.
pri const LOW_BIT_DEPTH_MULTIPLIERS : array[8] base.u8 = [
0,
0b1111_1111, // depth == 1
0b0101_0101, // depth == 2
0,
0b0001_0001, // depth == 4
0,
0,
0,
0,
0b1111_1111, // depth == 1
0b0101_0101, // depth == 2
0,
0b0001_0001, // depth == 4
0,
0,
0,
]
// LOW_BIT_DEPTH_NUM_PACKS holds the number of smaller-than-1-byte units that
// are packed into each byte.
pri const LOW_BIT_DEPTH_NUM_PACKS : array[8] base.u8 = [
0,
8, // depth == 1
4, // depth == 2
0,
2, // depth == 4
0,
0,
0,
0,
8, // depth == 1
4, // depth == 2
0,
2, // depth == 4
0,
0,
0,
]
// NUM_CHANNELS holds the number of channels for each valid color type.
pri const NUM_CHANNELS : array[8] base.u8[..= 4] = [
1, // color_type == 0: Y
0,
3, // color_type == 2: RGB
1, // color_type == 3: indexed
2, // color_type == 4: YA
0,
4, // color_type == 6: RGBA
0,
1, // color_type == 0: Y
0,
3, // color_type == 2: RGB
1, // color_type == 3: indexed
2, // color_type == 4: YA
0,
4, // color_type == 6: RGBA
0,
]
// LATIN_1 converts from printable Latin-1 (ISO/IEC 8859-1) to UTF-8 (encoded
// as little-endian u16). It was created by script/print-png-latin-1.go.
pri const LATIN_1 : array[256] base.u16 = [
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0xA1C2, 0xA2C2, 0xA3C2, 0xA4C2, 0xA5C2, 0xA6C2, 0xA7C2,
0xA8C2, 0xA9C2, 0xAAC2, 0xABC2, 0xACC2, 0xADC2, 0xAEC2, 0xAFC2,
0xB0C2, 0xB1C2, 0xB2C2, 0xB3C2, 0xB4C2, 0xB5C2, 0xB6C2, 0xB7C2,
0xB8C2, 0xB9C2, 0xBAC2, 0xBBC2, 0xBCC2, 0xBDC2, 0xBEC2, 0xBFC2,
0x80C3, 0x81C3, 0x82C3, 0x83C3, 0x84C3, 0x85C3, 0x86C3, 0x87C3,
0x88C3, 0x89C3, 0x8AC3, 0x8BC3, 0x8CC3, 0x8DC3, 0x8EC3, 0x8FC3,
0x90C3, 0x91C3, 0x92C3, 0x93C3, 0x94C3, 0x95C3, 0x96C3, 0x97C3,
0x98C3, 0x99C3, 0x9AC3, 0x9BC3, 0x9CC3, 0x9DC3, 0x9EC3, 0x9FC3,
0xA0C3, 0xA1C3, 0xA2C3, 0xA3C3, 0xA4C3, 0xA5C3, 0xA6C3, 0xA7C3,
0xA8C3, 0xA9C3, 0xAAC3, 0xABC3, 0xACC3, 0xADC3, 0xAEC3, 0xAFC3,
0xB0C3, 0xB1C3, 0xB2C3, 0xB3C3, 0xB4C3, 0xB5C3, 0xB6C3, 0xB7C3,
0xB8C3, 0xB9C3, 0xBAC3, 0xBBC3, 0xBCC3, 0xBDC3, 0xBEC3, 0xBFC3,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F,
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F,
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F,
0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F,
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0xA1C2, 0xA2C2, 0xA3C2, 0xA4C2, 0xA5C2, 0xA6C2, 0xA7C2,
0xA8C2, 0xA9C2, 0xAAC2, 0xABC2, 0xACC2, 0xADC2, 0xAEC2, 0xAFC2,
0xB0C2, 0xB1C2, 0xB2C2, 0xB3C2, 0xB4C2, 0xB5C2, 0xB6C2, 0xB7C2,
0xB8C2, 0xB9C2, 0xBAC2, 0xBBC2, 0xBCC2, 0xBDC2, 0xBEC2, 0xBFC2,
0x80C3, 0x81C3, 0x82C3, 0x83C3, 0x84C3, 0x85C3, 0x86C3, 0x87C3,
0x88C3, 0x89C3, 0x8AC3, 0x8BC3, 0x8CC3, 0x8DC3, 0x8EC3, 0x8FC3,
0x90C3, 0x91C3, 0x92C3, 0x93C3, 0x94C3, 0x95C3, 0x96C3, 0x97C3,
0x98C3, 0x99C3, 0x9AC3, 0x9BC3, 0x9CC3, 0x9DC3, 0x9EC3, 0x9FC3,
0xA0C3, 0xA1C3, 0xA2C3, 0xA3C3, 0xA4C3, 0xA5C3, 0xA6C3, 0xA7C3,
0xA8C3, 0xA9C3, 0xAAC3, 0xABC3, 0xACC3, 0xADC3, 0xAEC3, 0xAFC3,
0xB0C3, 0xB1C3, 0xB2C3, 0xB3C3, 0xB4C3, 0xB5C3, 0xB6C3, 0xB7C3,
0xB8C3, 0xB9C3, 0xBAC3, 0xBBC3, 0xBCC3, 0xBDC3, 0xBEC3, 0xBFC3,
]
+137 -137
View File
@@ -29,20 +29,20 @@
// Filter 1: Sub.
pri func decoder.filter_1_distance_4_arm_neon!(curr: slice base.u8),
choose cpu_arch >= arm_neon,
choose cpu_arch >= arm_neon,
{
var curr : slice base.u8
var curr : slice base.u8
var util : base.arm_neon_utility
var fa : base.arm_neon_u8x8
var fx : base.arm_neon_u8x8
var util : base.arm_neon_utility
var fa : base.arm_neon_u8x8
var fx : base.arm_neon_u8x8
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
fx = fx.vadd_u8(b: fa)
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
fa = fx
}
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
fx = fx.vadd_u8(b: fa)
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
fa = fx
}
}
// --------
@@ -50,33 +50,33 @@ pri func decoder.filter_1_distance_4_arm_neon!(curr: slice base.u8),
// Filter 3: Average.
pri func decoder.filter_3_distance_4_arm_neon!(curr: slice base.u8, prev: slice base.u8),
choose cpu_arch >= arm_neon,
choose cpu_arch >= arm_neon,
{
var curr : slice base.u8
var prev : slice base.u8
var curr : slice base.u8
var prev : slice base.u8
var util : base.arm_neon_utility
var fa : base.arm_neon_u8x8
var fb : base.arm_neon_u8x8
var fx : base.arm_neon_u8x8
var util : base.arm_neon_utility
var fa : base.arm_neon_u8x8
var fb : base.arm_neon_u8x8
var fx : base.arm_neon_u8x8
if args.prev.length() == 0 {
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
fx = fx.vadd_u8(b: fa.vhadd_u8(b: fb))
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
fa = fx
}
if args.prev.length() == 0 {
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
fx = fx.vadd_u8(b: fa.vhadd_u8(b: fb))
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
fa = fx
}
} else {
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
fb = util.make_u32x2_repeat(a: prev.peek_u32le()).as_u8x8()
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
fx = fx.vadd_u8(b: fa.vhadd_u8(b: fb))
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
fa = fx
}
}
} else {
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
fb = util.make_u32x2_repeat(a: prev.peek_u32le()).as_u8x8()
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
fx = fx.vadd_u8(b: fa.vhadd_u8(b: fb))
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
fa = fx
}
}
}
// --------
@@ -84,123 +84,123 @@ pri func decoder.filter_3_distance_4_arm_neon!(curr: slice base.u8, prev: slice
// Filter 4: Paeth.
pri func decoder.filter_4_distance_3_arm_neon!(curr: slice base.u8, prev: slice base.u8),
choose cpu_arch >= arm_neon,
choose cpu_arch >= arm_neon,
{
// See the comments in filter_4_distance_4_arm_neon for an explanation of
// how this works. That function's single loop is copied twice here, once
// with "length: 4" and once with "length: 3". It's generally faster to
// load 4 bytes at a time instead of 3.
//
// Differences between that function and this one are marked with a §.
// See the comments in filter_4_distance_4_arm_neon for an explanation of
// how this works. That function's single loop is copied twice here, once
// with "length: 4" and once with "length: 3". It's generally faster to
// load 4 bytes at a time instead of 3.
//
// Differences between that function and this one are marked with a §.
var curr : slice base.u8
var prev : slice base.u8
var curr : slice base.u8
var prev : slice base.u8
var util : base.arm_neon_utility
var fa : base.arm_neon_u8x8
var fb : base.arm_neon_u8x8
var fc : base.arm_neon_u8x8
var fx : base.arm_neon_u8x8
var fafb : base.arm_neon_u16x8
var fcfc : base.arm_neon_u16x8
var pa : base.arm_neon_u16x8
var pb : base.arm_neon_u16x8
var pc : base.arm_neon_u16x8
var cmpab : base.arm_neon_u16x8
var cmpac : base.arm_neon_u16x8
var picka : base.arm_neon_u8x8
var pickb : base.arm_neon_u8x8
var util : base.arm_neon_utility
var fa : base.arm_neon_u8x8
var fb : base.arm_neon_u8x8
var fc : base.arm_neon_u8x8
var fx : base.arm_neon_u8x8
var fafb : base.arm_neon_u16x8
var fcfc : base.arm_neon_u16x8
var pa : base.arm_neon_u16x8
var pb : base.arm_neon_u16x8
var pc : base.arm_neon_u16x8
var cmpab : base.arm_neon_u16x8
var cmpac : base.arm_neon_u16x8
var picka : base.arm_neon_u8x8
var pickb : base.arm_neon_u8x8
// § The advance is 3, not 4.
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 3, unroll: 2) {
fb = util.make_u32x2_repeat(a: prev.peek_u32le()).as_u8x8()
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
fafb = fa.vaddl_u8(b: fb)
fcfc = fc.vaddl_u8(b: fc)
pa = fb.vabdl_u8(b: fc)
pb = fa.vabdl_u8(b: fc)
pc = fafb.vabdq_u16(b: fcfc)
cmpab = pa.vcleq_u16(b: pb)
cmpac = pa.vcleq_u16(b: pc)
picka = cmpab.vandq_u16(b: cmpac).vmovn_u16()
pickb = pb.vcleq_u16(b: pc).vmovn_u16()
fx = fx.vadd_u8(
b: picka.vbsl_u8(b: fa,
c: pickb.vbsl_u8(b: fb, c: fc)))
// § poke_u24le replaces poke_u32le.
curr.poke_u24le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
fc = fb
fa = fx
} else (length: 3, advance: 3, unroll: 1) {
// § peek_u24le_as_u32 replaces peek_u32le.
fb = util.make_u32x2_repeat(a: prev.peek_u24le_as_u32()).as_u8x8()
// § peek_u24le_as_u32 replaces peek_u32le.
fx = util.make_u32x2_repeat(a: curr.peek_u24le_as_u32()).as_u8x8()
fafb = fa.vaddl_u8(b: fb)
fcfc = fc.vaddl_u8(b: fc)
pa = fb.vabdl_u8(b: fc)
pb = fa.vabdl_u8(b: fc)
pc = fafb.vabdq_u16(b: fcfc)
cmpab = pa.vcleq_u16(b: pb)
cmpac = pa.vcleq_u16(b: pc)
picka = cmpab.vandq_u16(b: cmpac).vmovn_u16()
pickb = pb.vcleq_u16(b: pc).vmovn_u16()
fx = fx.vadd_u8(
b: picka.vbsl_u8(b: fa,
c: pickb.vbsl_u8(b: fb, c: fc)))
// § poke_u24le replaces poke_u32le.
curr.poke_u24le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
// § These assignments are unnecessary; this is the last iteration.
// fc = fb
// fa = fx
}
// § The advance is 3, not 4.
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 3, unroll: 2) {
fb = util.make_u32x2_repeat(a: prev.peek_u32le()).as_u8x8()
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
fafb = fa.vaddl_u8(b: fb)
fcfc = fc.vaddl_u8(b: fc)
pa = fb.vabdl_u8(b: fc)
pb = fa.vabdl_u8(b: fc)
pc = fafb.vabdq_u16(b: fcfc)
cmpab = pa.vcleq_u16(b: pb)
cmpac = pa.vcleq_u16(b: pc)
picka = cmpab.vandq_u16(b: cmpac).vmovn_u16()
pickb = pb.vcleq_u16(b: pc).vmovn_u16()
fx = fx.vadd_u8(
b: picka.vbsl_u8(b: fa,
c: pickb.vbsl_u8(b: fb, c: fc)))
// § poke_u24le replaces poke_u32le.
curr.poke_u24le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
fc = fb
fa = fx
} else (length: 3, advance: 3, unroll: 1) {
// § peek_u24le_as_u32 replaces peek_u32le.
fb = util.make_u32x2_repeat(a: prev.peek_u24le_as_u32()).as_u8x8()
// § peek_u24le_as_u32 replaces peek_u32le.
fx = util.make_u32x2_repeat(a: curr.peek_u24le_as_u32()).as_u8x8()
fafb = fa.vaddl_u8(b: fb)
fcfc = fc.vaddl_u8(b: fc)
pa = fb.vabdl_u8(b: fc)
pb = fa.vabdl_u8(b: fc)
pc = fafb.vabdq_u16(b: fcfc)
cmpab = pa.vcleq_u16(b: pb)
cmpac = pa.vcleq_u16(b: pc)
picka = cmpab.vandq_u16(b: cmpac).vmovn_u16()
pickb = pb.vcleq_u16(b: pc).vmovn_u16()
fx = fx.vadd_u8(
b: picka.vbsl_u8(b: fa,
c: pickb.vbsl_u8(b: fb, c: fc)))
// § poke_u24le replaces poke_u32le.
curr.poke_u24le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
// § These assignments are unnecessary; this is the last iteration.
// fc = fb
// fa = fx
}
}
pri func decoder.filter_4_distance_4_arm_neon!(curr: slice base.u8, prev: slice base.u8),
choose cpu_arch >= arm_neon,
choose cpu_arch >= arm_neon,
{
var curr : slice base.u8
var prev : slice base.u8
var curr : slice base.u8
var prev : slice base.u8
var util : base.arm_neon_utility
var fa : base.arm_neon_u8x8
var fb : base.arm_neon_u8x8
var fc : base.arm_neon_u8x8
var fx : base.arm_neon_u8x8
var fafb : base.arm_neon_u16x8
var fcfc : base.arm_neon_u16x8
var pa : base.arm_neon_u16x8
var pb : base.arm_neon_u16x8
var pc : base.arm_neon_u16x8
var cmpab : base.arm_neon_u16x8
var cmpac : base.arm_neon_u16x8
var picka : base.arm_neon_u8x8
var pickb : base.arm_neon_u8x8
var util : base.arm_neon_utility
var fa : base.arm_neon_u8x8
var fb : base.arm_neon_u8x8
var fc : base.arm_neon_u8x8
var fx : base.arm_neon_u8x8
var fafb : base.arm_neon_u16x8
var fcfc : base.arm_neon_u16x8
var pa : base.arm_neon_u16x8
var pb : base.arm_neon_u16x8
var pc : base.arm_neon_u16x8
var cmpab : base.arm_neon_u16x8
var cmpac : base.arm_neon_u16x8
var picka : base.arm_neon_u8x8
var pickb : base.arm_neon_u8x8
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
fb = util.make_u32x2_repeat(a: prev.peek_u32le()).as_u8x8()
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
fb = util.make_u32x2_repeat(a: prev.peek_u32le()).as_u8x8()
fx = util.make_u32x2_repeat(a: curr.peek_u32le()).as_u8x8()
fafb = fa.vaddl_u8(b: fb) // fafb = (fa + fb)
fcfc = fc.vaddl_u8(b: fc) // fcfc = (fc + fc)
fafb = fa.vaddl_u8(b: fb) // fafb = (fa + fb)
fcfc = fc.vaddl_u8(b: fc) // fcfc = (fc + fc)
pa = fb.vabdl_u8(b: fc) // pa = abs(fa + fb - fc - fa)
pb = fa.vabdl_u8(b: fc) // pb = abs(fa + fb - fc - fb)
pc = fafb.vabdq_u16(b: fcfc) // pc = abs(fa + fb - fc - fc)
pa = fb.vabdl_u8(b: fc) // pa = abs(fa + fb - fc - fa)
pb = fa.vabdl_u8(b: fc) // pb = abs(fa + fb - fc - fb)
pc = fafb.vabdq_u16(b: fcfc) // pc = abs(fa + fb - fc - fc)
cmpab = pa.vcleq_u16(b: pb) // cmpab = (pa <= pb)
cmpac = pa.vcleq_u16(b: pc) // cmpac = (pa <= pc)
cmpab = pa.vcleq_u16(b: pb) // cmpab = (pa <= pb)
cmpac = pa.vcleq_u16(b: pc) // cmpac = (pa <= pc)
picka = cmpab.vandq_u16(b: cmpac).vmovn_u16() // picka = ((pa <= pb) && (pa <= pc))
pickb = pb.vcleq_u16(b: pc).vmovn_u16() // pickb = (pb <= pc)
picka = cmpab.vandq_u16(b: cmpac).vmovn_u16() // picka = ((pa <= pb) && (pa <= pc))
pickb = pb.vcleq_u16(b: pc).vmovn_u16() // pickb = (pb <= pc)
// Add the predictor to the residual.
fx = fx.vadd_u8(
b: picka.vbsl_u8(b: fa,
c: pickb.vbsl_u8(b: fb, c: fc)))
// Add the predictor to the residual.
fx = fx.vadd_u8(
b: picka.vbsl_u8(b: fa,
c: pickb.vbsl_u8(b: fb, c: fc)))
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
fc = fb
fa = fx
}
curr.poke_u32le!(a: fx.as_u32x2().vget_lane_u32(b: 0))
fc = fb
fa = fx
}
}
+450 -450
View File
@@ -17,517 +17,517 @@
// Filter 1: Sub.
pri func decoder.filter_1!(curr: slice base.u8),
choosy,
choosy,
{
var filter_distance : base.u64[..= 8]
var fa : base.u8
var i_start : base.u64
var i : base.u64
var filter_distance : base.u64[..= 8]
var fa : base.u8
var i_start : base.u64
var i : base.u64
filter_distance = this.filter_distance as base.u64
i_start = 0
while i_start < filter_distance {
assert i_start < 8 via "a < b: a < c; c <= b"(c: filter_distance)
fa = 0
i = i_start
while i < args.curr.length(),
inv i_start < 8,
{
args.curr[i] = args.curr[i] ~mod+ fa
fa = args.curr[i]
i ~mod+= filter_distance
} endwhile
i_start += 1
} endwhile
filter_distance = this.filter_distance as base.u64
i_start = 0
while i_start < filter_distance {
assert i_start < 8 via "a < b: a < c; c <= b"(c: filter_distance)
fa = 0
i = i_start
while i < args.curr.length(),
inv i_start < 8,
{
args.curr[i] = args.curr[i] ~mod+ fa
fa = args.curr[i]
i ~mod+= filter_distance
} endwhile
i_start += 1
} endwhile
}
pri func decoder.filter_1_distance_3_fallback!(curr: slice base.u8) {
var curr : slice base.u8
var curr : slice base.u8
var fa0 : base.u8
var fa1 : base.u8
var fa2 : base.u8
var fa0 : base.u8
var fa1 : base.u8
var fa2 : base.u8
iterate (curr = args.curr)(length: 3, advance: 3, unroll: 2) {
fa0 = fa0 ~mod+ curr[0]
curr[0] = fa0
fa1 = fa1 ~mod+ curr[1]
curr[1] = fa1
fa2 = fa2 ~mod+ curr[2]
curr[2] = fa2
}
iterate (curr = args.curr)(length: 3, advance: 3, unroll: 2) {
fa0 = fa0 ~mod+ curr[0]
curr[0] = fa0
fa1 = fa1 ~mod+ curr[1]
curr[1] = fa1
fa2 = fa2 ~mod+ curr[2]
curr[2] = fa2
}
}
pri func decoder.filter_1_distance_4_fallback!(curr: slice base.u8) {
var curr : slice base.u8
var curr : slice base.u8
var fa0 : base.u8
var fa1 : base.u8
var fa2 : base.u8
var fa3 : base.u8
var fa0 : base.u8
var fa1 : base.u8
var fa2 : base.u8
var fa3 : base.u8
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 1) {
fa0 = fa0 ~mod+ curr[0]
curr[0] = fa0
fa1 = fa1 ~mod+ curr[1]
curr[1] = fa1
fa2 = fa2 ~mod+ curr[2]
curr[2] = fa2
fa3 = fa3 ~mod+ curr[3]
curr[3] = fa3
}
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 1) {
fa0 = fa0 ~mod+ curr[0]
curr[0] = fa0
fa1 = fa1 ~mod+ curr[1]
curr[1] = fa1
fa2 = fa2 ~mod+ curr[2]
curr[2] = fa2
fa3 = fa3 ~mod+ curr[3]
curr[3] = fa3
}
}
// Filter 2: Up.
pri func decoder.filter_2!(curr: slice base.u8, prev: slice base.u8) {
var n : base.u64
var i : base.u64
var n : base.u64
var i : base.u64
n = args.curr.length().min(a: args.prev.length())
i = 0
while i < n,
inv n <= args.curr.length(),
inv n <= args.prev.length(),
{
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
args.curr[i] = args.curr[i] ~mod+ (args.prev[i])
i += 1
} endwhile
n = args.curr.length().min(a: args.prev.length())
i = 0
while i < n,
inv n <= args.curr.length(),
inv n <= args.prev.length(),
{
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
args.curr[i] = args.curr[i] ~mod+ (args.prev[i])
i += 1
} endwhile
}
// Filter 3: Average.
pri func decoder.filter_3!(curr: slice base.u8, prev: slice base.u8),
choosy,
choosy,
{
var filter_distance : base.u64[..= 8]
var n : base.u64
var i : base.u64
var filter_distance : base.u64[..= 8]
var n : base.u64
var i : base.u64
filter_distance = this.filter_distance as base.u64
if args.prev.length() == 0 {
i = filter_distance
assert i >= filter_distance via "a >= b: a == b"()
while i < args.curr.length(),
inv i >= filter_distance,
{
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: args.curr.length())
assert (i - filter_distance) < args.curr.length() via "(a - b) < c: a < c; 0 <= b"()
args.curr[i] = args.curr[i] ~mod+ (args.curr[i - filter_distance] / 2)
i += 1
assert i >= filter_distance via "a >= b: a >= (b + c); 0 <= c"(c: 1)
} endwhile
filter_distance = this.filter_distance as base.u64
if args.prev.length() == 0 {
i = filter_distance
assert i >= filter_distance via "a >= b: a == b"()
while i < args.curr.length(),
inv i >= filter_distance,
{
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: args.curr.length())
assert (i - filter_distance) < args.curr.length() via "(a - b) < c: a < c; 0 <= b"()
args.curr[i] = args.curr[i] ~mod+ (args.curr[i - filter_distance] / 2)
i += 1
assert i >= filter_distance via "a >= b: a >= (b + c); 0 <= c"(c: 1)
} endwhile
} else {
n = args.curr.length().min(a: args.prev.length())
i = 0
while (i < n) and (i < filter_distance),
inv n <= args.curr.length(),
inv n <= args.prev.length(),
{
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
args.curr[i] = args.curr[i] ~mod+ (args.prev[i] / 2)
i += 1
} endwhile
} else {
n = args.curr.length().min(a: args.prev.length())
i = 0
while (i < n) and (i < filter_distance),
inv n <= args.curr.length(),
inv n <= args.prev.length(),
{
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
args.curr[i] = args.curr[i] ~mod+ (args.prev[i] / 2)
i += 1
} endwhile
i = filter_distance
assert i >= filter_distance via "a >= b: a == b"()
while i < n,
inv i >= filter_distance,
inv n <= args.curr.length(),
inv n <= args.prev.length(),
{
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
assert (i - filter_distance) < args.curr.length() via "(a - b) < c: a < c; 0 <= b"()
args.curr[i] = args.curr[i] ~mod+ (((
(args.curr[i - filter_distance] as base.u32) +
(args.prev[i] as base.u32)) / 2) as base.u8)
i += 1
assert i >= filter_distance via "a >= b: a >= (b + c); 0 <= c"(c: 1)
} endwhile
}
i = filter_distance
assert i >= filter_distance via "a >= b: a == b"()
while i < n,
inv i >= filter_distance,
inv n <= args.curr.length(),
inv n <= args.prev.length(),
{
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
assert (i - filter_distance) < args.curr.length() via "(a - b) < c: a < c; 0 <= b"()
args.curr[i] = args.curr[i] ~mod+ (((
(args.curr[i - filter_distance] as base.u32) +
(args.prev[i] as base.u32)) / 2) as base.u8)
i += 1
assert i >= filter_distance via "a >= b: a >= (b + c); 0 <= c"(c: 1)
} endwhile
}
}
pri func decoder.filter_3_distance_3_fallback!(curr: slice base.u8, prev: slice base.u8) {
var curr : slice base.u8
var prev : slice base.u8
var curr : slice base.u8
var prev : slice base.u8
var fa0 : base.u8
var fa1 : base.u8
var fa2 : base.u8
var fa0 : base.u8
var fa1 : base.u8
var fa2 : base.u8
if args.prev.length() == 0 {
iterate (curr = args.curr)(length: 3, advance: 3, unroll: 2) {
fa0 = (fa0 / 2) ~mod+ curr[0]
curr[0] = fa0
fa1 = (fa1 / 2) ~mod+ curr[1]
curr[1] = fa1
fa2 = (fa2 / 2) ~mod+ curr[2]
curr[2] = fa2
}
if args.prev.length() == 0 {
iterate (curr = args.curr)(length: 3, advance: 3, unroll: 2) {
fa0 = (fa0 / 2) ~mod+ curr[0]
curr[0] = fa0
fa1 = (fa1 / 2) ~mod+ curr[1]
curr[1] = fa1
fa2 = (fa2 / 2) ~mod+ curr[2]
curr[2] = fa2
}
} else {
iterate (curr = args.curr, prev = args.prev)(length: 3, advance: 3, unroll: 2) {
fa0 = ((((fa0 as base.u32) + (prev[0] as base.u32)) / 2) as base.u8) ~mod+ curr[0]
curr[0] = fa0
fa1 = ((((fa1 as base.u32) + (prev[1] as base.u32)) / 2) as base.u8) ~mod+ curr[1]
curr[1] = fa1
fa2 = ((((fa2 as base.u32) + (prev[2] as base.u32)) / 2) as base.u8) ~mod+ curr[2]
curr[2] = fa2
}
}
} else {
iterate (curr = args.curr, prev = args.prev)(length: 3, advance: 3, unroll: 2) {
fa0 = ((((fa0 as base.u32) + (prev[0] as base.u32)) / 2) as base.u8) ~mod+ curr[0]
curr[0] = fa0
fa1 = ((((fa1 as base.u32) + (prev[1] as base.u32)) / 2) as base.u8) ~mod+ curr[1]
curr[1] = fa1
fa2 = ((((fa2 as base.u32) + (prev[2] as base.u32)) / 2) as base.u8) ~mod+ curr[2]
curr[2] = fa2
}
}
}
pri func decoder.filter_3_distance_4_fallback!(curr: slice base.u8, prev: slice base.u8) {
var curr : slice base.u8
var prev : slice base.u8
var curr : slice base.u8
var prev : slice base.u8
var fa0 : base.u8
var fa1 : base.u8
var fa2 : base.u8
var fa3 : base.u8
var fa0 : base.u8
var fa1 : base.u8
var fa2 : base.u8
var fa3 : base.u8
if args.prev.length() == 0 {
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 1) {
fa0 = (fa0 / 2) ~mod+ curr[0]
curr[0] = fa0
fa1 = (fa1 / 2) ~mod+ curr[1]
curr[1] = fa1
fa2 = (fa2 / 2) ~mod+ curr[2]
curr[2] = fa2
fa3 = (fa3 / 2) ~mod+ curr[3]
curr[3] = fa3
}
if args.prev.length() == 0 {
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 1) {
fa0 = (fa0 / 2) ~mod+ curr[0]
curr[0] = fa0
fa1 = (fa1 / 2) ~mod+ curr[1]
curr[1] = fa1
fa2 = (fa2 / 2) ~mod+ curr[2]
curr[2] = fa2
fa3 = (fa3 / 2) ~mod+ curr[3]
curr[3] = fa3
}
} else {
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 1) {
fa0 = ((((fa0 as base.u32) + (prev[0] as base.u32)) / 2) as base.u8) ~mod+ curr[0]
curr[0] = fa0
fa1 = ((((fa1 as base.u32) + (prev[1] as base.u32)) / 2) as base.u8) ~mod+ curr[1]
curr[1] = fa1
fa2 = ((((fa2 as base.u32) + (prev[2] as base.u32)) / 2) as base.u8) ~mod+ curr[2]
curr[2] = fa2
fa3 = ((((fa3 as base.u32) + (prev[3] as base.u32)) / 2) as base.u8) ~mod+ curr[3]
curr[3] = fa3
}
}
} else {
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 1) {
fa0 = ((((fa0 as base.u32) + (prev[0] as base.u32)) / 2) as base.u8) ~mod+ curr[0]
curr[0] = fa0
fa1 = ((((fa1 as base.u32) + (prev[1] as base.u32)) / 2) as base.u8) ~mod+ curr[1]
curr[1] = fa1
fa2 = ((((fa2 as base.u32) + (prev[2] as base.u32)) / 2) as base.u8) ~mod+ curr[2]
curr[2] = fa2
fa3 = ((((fa3 as base.u32) + (prev[3] as base.u32)) / 2) as base.u8) ~mod+ curr[3]
curr[3] = fa3
}
}
}
// Filter 4: Paeth.
pri func decoder.filter_4!(curr: slice base.u8, prev: slice base.u8),
choosy,
choosy,
{
var filter_distance : base.u64[..= 8]
var n : base.u64
var i : base.u64
var filter_distance : base.u64[..= 8]
var n : base.u64
var i : base.u64
var fa : base.u32
var fb : base.u32
var fc : base.u32
var pp : base.u32
var pa : base.u32
var pb : base.u32
var pc : base.u32
var fa : base.u32
var fb : base.u32
var fc : base.u32
var pp : base.u32
var pa : base.u32
var pb : base.u32
var pc : base.u32
filter_distance = this.filter_distance as base.u64
n = args.curr.length().min(a: args.prev.length())
i = 0
while (i < n) and (i < filter_distance),
inv n <= args.curr.length(),
inv n <= args.prev.length(),
{
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
args.curr[i] = args.curr[i] ~mod+ args.prev[i]
i += 1
} endwhile
filter_distance = this.filter_distance as base.u64
n = args.curr.length().min(a: args.prev.length())
i = 0
while (i < n) and (i < filter_distance),
inv n <= args.curr.length(),
inv n <= args.prev.length(),
{
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
args.curr[i] = args.curr[i] ~mod+ args.prev[i]
i += 1
} endwhile
i = filter_distance
assert i >= filter_distance via "a >= b: a == b"()
while i < n,
inv i >= filter_distance,
inv n <= args.curr.length(),
inv n <= args.prev.length(),
{
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
assert (i - filter_distance) < args.curr.length() via "(a - b) < c: a < c; 0 <= b"()
assert (i - filter_distance) < args.prev.length() via "(a - b) < c: a < c; 0 <= b"()
fa = args.curr[i - filter_distance] as base.u32
fb = args.prev[i] as base.u32
fc = args.prev[i - filter_distance] as base.u32
pp = (fa ~mod+ fb) ~mod- fc
pa = pp ~mod- fa
if pa >= 0x8000_0000 {
pa = 0 ~mod- pa
}
pb = pp ~mod- fb
if pb >= 0x8000_0000 {
pb = 0 ~mod- pb
}
pc = pp ~mod- fc
if pc >= 0x8000_0000 {
pc = 0 ~mod- pc
}
if (pa <= pb) and (pa <= pc) {
// No-op.
} else if pb <= pc {
fa = fb
} else {
fa = fc
}
args.curr[i] = args.curr[i] ~mod+ ((fa & 0xFF) as base.u8)
i += 1
assert i >= filter_distance via "a >= b: a >= (b + c); 0 <= c"(c: 1)
} endwhile
i = filter_distance
assert i >= filter_distance via "a >= b: a == b"()
while i < n,
inv i >= filter_distance,
inv n <= args.curr.length(),
inv n <= args.prev.length(),
{
assert i < 0xFFFF_FFFF_FFFF_FFFF via "a < b: a < c; c <= b"(c: n)
assert i < args.curr.length() via "a < b: a < c; c <= b"(c: n)
assert i < args.prev.length() via "a < b: a < c; c <= b"(c: n)
assert (i - filter_distance) < args.curr.length() via "(a - b) < c: a < c; 0 <= b"()
assert (i - filter_distance) < args.prev.length() via "(a - b) < c: a < c; 0 <= b"()
fa = args.curr[i - filter_distance] as base.u32
fb = args.prev[i] as base.u32
fc = args.prev[i - filter_distance] as base.u32
pp = (fa ~mod+ fb) ~mod- fc
pa = pp ~mod- fa
if pa >= 0x8000_0000 {
pa = 0 ~mod- pa
}
pb = pp ~mod- fb
if pb >= 0x8000_0000 {
pb = 0 ~mod- pb
}
pc = pp ~mod- fc
if pc >= 0x8000_0000 {
pc = 0 ~mod- pc
}
if (pa <= pb) and (pa <= pc) {
// No-op.
} else if pb <= pc {
fa = fb
} else {
fa = fc
}
args.curr[i] = args.curr[i] ~mod+ ((fa & 0xFF) as base.u8)
i += 1
assert i >= filter_distance via "a >= b: a >= (b + c); 0 <= c"(c: 1)
} endwhile
}
pri func decoder.filter_4_distance_3_fallback!(curr: slice base.u8, prev: slice base.u8) {
var curr : slice base.u8
var prev : slice base.u8
var curr : slice base.u8
var prev : slice base.u8
var fa0 : base.u32
var fa1 : base.u32
var fa2 : base.u32
var fb0 : base.u32
var fb1 : base.u32
var fb2 : base.u32
var fc0 : base.u32
var fc1 : base.u32
var fc2 : base.u32
var pp0 : base.u32
var pp1 : base.u32
var pp2 : base.u32
var pa0 : base.u32
var pa1 : base.u32
var pa2 : base.u32
var pb0 : base.u32
var pb1 : base.u32
var pb2 : base.u32
var pc0 : base.u32
var pc1 : base.u32
var pc2 : base.u32
var fa0 : base.u32
var fa1 : base.u32
var fa2 : base.u32
var fb0 : base.u32
var fb1 : base.u32
var fb2 : base.u32
var fc0 : base.u32
var fc1 : base.u32
var fc2 : base.u32
var pp0 : base.u32
var pp1 : base.u32
var pp2 : base.u32
var pa0 : base.u32
var pa1 : base.u32
var pa2 : base.u32
var pb0 : base.u32
var pb1 : base.u32
var pb2 : base.u32
var pc0 : base.u32
var pc1 : base.u32
var pc2 : base.u32
iterate (curr = args.curr, prev = args.prev)(length: 3, advance: 3, unroll: 1) {
fb0 = prev[0] as base.u32
pp0 = (fa0 ~mod+ fb0) ~mod- fc0
pa0 = pp0 ~mod- fa0
if pa0 >= 0x8000_0000 {
pa0 = 0 ~mod- pa0
}
pb0 = pp0 ~mod- fb0
if pb0 >= 0x8000_0000 {
pb0 = 0 ~mod- pb0
}
pc0 = pp0 ~mod- fc0
if pc0 >= 0x8000_0000 {
pc0 = 0 ~mod- pc0
}
if (pa0 <= pb0) and (pa0 <= pc0) {
// No-op.
} else if pb0 <= pc0 {
fa0 = fb0
} else {
fa0 = fc0
}
curr[0] = curr[0] ~mod+ ((fa0 & 0xFF) as base.u8)
fa0 = curr[0] as base.u32
fc0 = fb0
iterate (curr = args.curr, prev = args.prev)(length: 3, advance: 3, unroll: 1) {
fb0 = prev[0] as base.u32
pp0 = (fa0 ~mod+ fb0) ~mod- fc0
pa0 = pp0 ~mod- fa0
if pa0 >= 0x8000_0000 {
pa0 = 0 ~mod- pa0
}
pb0 = pp0 ~mod- fb0
if pb0 >= 0x8000_0000 {
pb0 = 0 ~mod- pb0
}
pc0 = pp0 ~mod- fc0
if pc0 >= 0x8000_0000 {
pc0 = 0 ~mod- pc0
}
if (pa0 <= pb0) and (pa0 <= pc0) {
// No-op.
} else if pb0 <= pc0 {
fa0 = fb0
} else {
fa0 = fc0
}
curr[0] = curr[0] ~mod+ ((fa0 & 0xFF) as base.u8)
fa0 = curr[0] as base.u32
fc0 = fb0
fb1 = prev[1] as base.u32
pp1 = (fa1 ~mod+ fb1) ~mod- fc1
pa1 = pp1 ~mod- fa1
if pa1 >= 0x8000_0000 {
pa1 = 0 ~mod- pa1
}
pb1 = pp1 ~mod- fb1
if pb1 >= 0x8000_0000 {
pb1 = 0 ~mod- pb1
}
pc1 = pp1 ~mod- fc1
if pc1 >= 0x8000_0000 {
pc1 = 0 ~mod- pc1
}
if (pa1 <= pb1) and (pa1 <= pc1) {
// No-op.
} else if pb1 <= pc1 {
fa1 = fb1
} else {
fa1 = fc1
}
curr[1] = curr[1] ~mod+ ((fa1 & 0xFF) as base.u8)
fa1 = curr[1] as base.u32
fc1 = fb1
fb1 = prev[1] as base.u32
pp1 = (fa1 ~mod+ fb1) ~mod- fc1
pa1 = pp1 ~mod- fa1
if pa1 >= 0x8000_0000 {
pa1 = 0 ~mod- pa1
}
pb1 = pp1 ~mod- fb1
if pb1 >= 0x8000_0000 {
pb1 = 0 ~mod- pb1
}
pc1 = pp1 ~mod- fc1
if pc1 >= 0x8000_0000 {
pc1 = 0 ~mod- pc1
}
if (pa1 <= pb1) and (pa1 <= pc1) {
// No-op.
} else if pb1 <= pc1 {
fa1 = fb1
} else {
fa1 = fc1
}
curr[1] = curr[1] ~mod+ ((fa1 & 0xFF) as base.u8)
fa1 = curr[1] as base.u32
fc1 = fb1
fb2 = prev[2] as base.u32
pp2 = (fa2 ~mod+ fb2) ~mod- fc2
pa2 = pp2 ~mod- fa2
if pa2 >= 0x8000_0000 {
pa2 = 0 ~mod- pa2
}
pb2 = pp2 ~mod- fb2
if pb2 >= 0x8000_0000 {
pb2 = 0 ~mod- pb2
}
pc2 = pp2 ~mod- fc2
if pc2 >= 0x8000_0000 {
pc2 = 0 ~mod- pc2
}
if (pa2 <= pb2) and (pa2 <= pc2) {
// No-op.
} else if pb2 <= pc2 {
fa2 = fb2
} else {
fa2 = fc2
}
curr[2] = curr[2] ~mod+ ((fa2 & 0xFF) as base.u8)
fa2 = curr[2] as base.u32
fc2 = fb2
}
fb2 = prev[2] as base.u32
pp2 = (fa2 ~mod+ fb2) ~mod- fc2
pa2 = pp2 ~mod- fa2
if pa2 >= 0x8000_0000 {
pa2 = 0 ~mod- pa2
}
pb2 = pp2 ~mod- fb2
if pb2 >= 0x8000_0000 {
pb2 = 0 ~mod- pb2
}
pc2 = pp2 ~mod- fc2
if pc2 >= 0x8000_0000 {
pc2 = 0 ~mod- pc2
}
if (pa2 <= pb2) and (pa2 <= pc2) {
// No-op.
} else if pb2 <= pc2 {
fa2 = fb2
} else {
fa2 = fc2
}
curr[2] = curr[2] ~mod+ ((fa2 & 0xFF) as base.u8)
fa2 = curr[2] as base.u32
fc2 = fb2
}
}
pri func decoder.filter_4_distance_4_fallback!(curr: slice base.u8, prev: slice base.u8) {
var curr : slice base.u8
var prev : slice base.u8
var curr : slice base.u8
var prev : slice base.u8
var fa0 : base.u32
var fa1 : base.u32
var fa2 : base.u32
var fa3 : base.u32
var fb0 : base.u32
var fb1 : base.u32
var fb2 : base.u32
var fb3 : base.u32
var fc0 : base.u32
var fc1 : base.u32
var fc2 : base.u32
var fc3 : base.u32
var pp0 : base.u32
var pp1 : base.u32
var pp2 : base.u32
var pp3 : base.u32
var pa0 : base.u32
var pa1 : base.u32
var pa2 : base.u32
var pa3 : base.u32
var pb0 : base.u32
var pb1 : base.u32
var pb2 : base.u32
var pb3 : base.u32
var pc0 : base.u32
var pc1 : base.u32
var pc2 : base.u32
var pc3 : base.u32
var fa0 : base.u32
var fa1 : base.u32
var fa2 : base.u32
var fa3 : base.u32
var fb0 : base.u32
var fb1 : base.u32
var fb2 : base.u32
var fb3 : base.u32
var fc0 : base.u32
var fc1 : base.u32
var fc2 : base.u32
var fc3 : base.u32
var pp0 : base.u32
var pp1 : base.u32
var pp2 : base.u32
var pp3 : base.u32
var pa0 : base.u32
var pa1 : base.u32
var pa2 : base.u32
var pa3 : base.u32
var pb0 : base.u32
var pb1 : base.u32
var pb2 : base.u32
var pb3 : base.u32
var pc0 : base.u32
var pc1 : base.u32
var pc2 : base.u32
var pc3 : base.u32
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 1) {
fb0 = prev[0] as base.u32
pp0 = (fa0 ~mod+ fb0) ~mod- fc0
pa0 = pp0 ~mod- fa0
if pa0 >= 0x8000_0000 {
pa0 = 0 ~mod- pa0
}
pb0 = pp0 ~mod- fb0
if pb0 >= 0x8000_0000 {
pb0 = 0 ~mod- pb0
}
pc0 = pp0 ~mod- fc0
if pc0 >= 0x8000_0000 {
pc0 = 0 ~mod- pc0
}
if (pa0 <= pb0) and (pa0 <= pc0) {
// No-op.
} else if pb0 <= pc0 {
fa0 = fb0
} else {
fa0 = fc0
}
curr[0] = curr[0] ~mod+ ((fa0 & 0xFF) as base.u8)
fa0 = curr[0] as base.u32
fc0 = fb0
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 1) {
fb0 = prev[0] as base.u32
pp0 = (fa0 ~mod+ fb0) ~mod- fc0
pa0 = pp0 ~mod- fa0
if pa0 >= 0x8000_0000 {
pa0 = 0 ~mod- pa0
}
pb0 = pp0 ~mod- fb0
if pb0 >= 0x8000_0000 {
pb0 = 0 ~mod- pb0
}
pc0 = pp0 ~mod- fc0
if pc0 >= 0x8000_0000 {
pc0 = 0 ~mod- pc0
}
if (pa0 <= pb0) and (pa0 <= pc0) {
// No-op.
} else if pb0 <= pc0 {
fa0 = fb0
} else {
fa0 = fc0
}
curr[0] = curr[0] ~mod+ ((fa0 & 0xFF) as base.u8)
fa0 = curr[0] as base.u32
fc0 = fb0
fb1 = prev[1] as base.u32
pp1 = (fa1 ~mod+ fb1) ~mod- fc1
pa1 = pp1 ~mod- fa1
if pa1 >= 0x8000_0000 {
pa1 = 0 ~mod- pa1
}
pb1 = pp1 ~mod- fb1
if pb1 >= 0x8000_0000 {
pb1 = 0 ~mod- pb1
}
pc1 = pp1 ~mod- fc1
if pc1 >= 0x8000_0000 {
pc1 = 0 ~mod- pc1
}
if (pa1 <= pb1) and (pa1 <= pc1) {
// No-op.
} else if pb1 <= pc1 {
fa1 = fb1
} else {
fa1 = fc1
}
curr[1] = curr[1] ~mod+ ((fa1 & 0xFF) as base.u8)
fa1 = curr[1] as base.u32
fc1 = fb1
fb1 = prev[1] as base.u32
pp1 = (fa1 ~mod+ fb1) ~mod- fc1
pa1 = pp1 ~mod- fa1
if pa1 >= 0x8000_0000 {
pa1 = 0 ~mod- pa1
}
pb1 = pp1 ~mod- fb1
if pb1 >= 0x8000_0000 {
pb1 = 0 ~mod- pb1
}
pc1 = pp1 ~mod- fc1
if pc1 >= 0x8000_0000 {
pc1 = 0 ~mod- pc1
}
if (pa1 <= pb1) and (pa1 <= pc1) {
// No-op.
} else if pb1 <= pc1 {
fa1 = fb1
} else {
fa1 = fc1
}
curr[1] = curr[1] ~mod+ ((fa1 & 0xFF) as base.u8)
fa1 = curr[1] as base.u32
fc1 = fb1
fb2 = prev[2] as base.u32
pp2 = (fa2 ~mod+ fb2) ~mod- fc2
pa2 = pp2 ~mod- fa2
if pa2 >= 0x8000_0000 {
pa2 = 0 ~mod- pa2
}
pb2 = pp2 ~mod- fb2
if pb2 >= 0x8000_0000 {
pb2 = 0 ~mod- pb2
}
pc2 = pp2 ~mod- fc2
if pc2 >= 0x8000_0000 {
pc2 = 0 ~mod- pc2
}
if (pa2 <= pb2) and (pa2 <= pc2) {
// No-op.
} else if pb2 <= pc2 {
fa2 = fb2
} else {
fa2 = fc2
}
curr[2] = curr[2] ~mod+ ((fa2 & 0xFF) as base.u8)
fa2 = curr[2] as base.u32
fc2 = fb2
fb2 = prev[2] as base.u32
pp2 = (fa2 ~mod+ fb2) ~mod- fc2
pa2 = pp2 ~mod- fa2
if pa2 >= 0x8000_0000 {
pa2 = 0 ~mod- pa2
}
pb2 = pp2 ~mod- fb2
if pb2 >= 0x8000_0000 {
pb2 = 0 ~mod- pb2
}
pc2 = pp2 ~mod- fc2
if pc2 >= 0x8000_0000 {
pc2 = 0 ~mod- pc2
}
if (pa2 <= pb2) and (pa2 <= pc2) {
// No-op.
} else if pb2 <= pc2 {
fa2 = fb2
} else {
fa2 = fc2
}
curr[2] = curr[2] ~mod+ ((fa2 & 0xFF) as base.u8)
fa2 = curr[2] as base.u32
fc2 = fb2
fb3 = prev[3] as base.u32
pp3 = (fa3 ~mod+ fb3) ~mod- fc3
pa3 = pp3 ~mod- fa3
if pa3 >= 0x8000_0000 {
pa3 = 0 ~mod- pa3
}
pb3 = pp3 ~mod- fb3
if pb3 >= 0x8000_0000 {
pb3 = 0 ~mod- pb3
}
pc3 = pp3 ~mod- fc3
if pc3 >= 0x8000_0000 {
pc3 = 0 ~mod- pc3
}
if (pa3 <= pb3) and (pa3 <= pc3) {
// No-op.
} else if pb3 <= pc3 {
fa3 = fb3
} else {
fa3 = fc3
}
curr[3] = curr[3] ~mod+ ((fa3 & 0xFF) as base.u8)
fa3 = curr[3] as base.u32
fc3 = fb3
}
fb3 = prev[3] as base.u32
pp3 = (fa3 ~mod+ fb3) ~mod- fc3
pa3 = pp3 ~mod- fa3
if pa3 >= 0x8000_0000 {
pa3 = 0 ~mod- pa3
}
pb3 = pp3 ~mod- fb3
if pb3 >= 0x8000_0000 {
pb3 = 0 ~mod- pb3
}
pc3 = pp3 ~mod- fc3
if pc3 >= 0x8000_0000 {
pc3 = 0 ~mod- pc3
}
if (pa3 <= pb3) and (pa3 <= pc3) {
// No-op.
} else if pb3 <= pc3 {
fa3 = fb3
} else {
fa3 = fc3
}
curr[3] = curr[3] ~mod+ ((fa3 & 0xFF) as base.u8)
fa3 = curr[3] as base.u32
fc3 = fb3
}
}
+181 -181
View File
@@ -45,20 +45,20 @@
// "Have std/png filter_1_distance_? use more SIMD" for a pessimizing example.
pri func decoder.filter_1_distance_4_x86_sse42!(curr: slice base.u8),
choose cpu_arch >= x86_sse42,
choose cpu_arch >= x86_sse42,
{
var curr : slice base.u8
var curr : slice base.u8
var util : base.x86_sse42_utility
var x128 : base.x86_m128i
var a128 : base.x86_m128i
var util : base.x86_sse42_utility
var x128 : base.x86_m128i
var a128 : base.x86_m128i
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
x128 = x128._mm_add_epi8(b: a128)
a128 = x128
curr.poke_u32le!(a: x128.truncate_u32())
}
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
x128 = x128._mm_add_epi8(b: a128)
a128 = x128
curr.poke_u32le!(a: x128.truncate_u32())
}
}
// --------
@@ -75,58 +75,58 @@ pri func decoder.filter_1_distance_4_x86_sse42!(curr: slice base.u8),
// }
pri func decoder.filter_3_distance_4_x86_sse42!(curr: slice base.u8, prev: slice base.u8),
choose cpu_arch >= x86_sse42,
choose cpu_arch >= x86_sse42,
{
var curr : slice base.u8
var prev : slice base.u8
var curr : slice base.u8
var prev : slice base.u8
var util : base.x86_sse42_utility
var x128 : base.x86_m128i
var a128 : base.x86_m128i
var b128 : base.x86_m128i
var p128 : base.x86_m128i
var k128 : base.x86_m128i
var util : base.x86_sse42_utility
var x128 : base.x86_m128i
var a128 : base.x86_m128i
var b128 : base.x86_m128i
var p128 : base.x86_m128i
var k128 : base.x86_m128i
if args.prev.length() == 0 {
k128 = util.make_m128i_repeat_u8(a: 0xFE)
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
// The predictor, p128, is just half (rounded down) of the previous
// pixel, a128. In this branch, b128 stays zero so the average of
// a128 and b128 is just half of a128. _mm_avg_epu8 rounds up, but
// (a128 & 0xFE_repeated) takes out the low bits of a128's bytes.
p128 = a128._mm_and_si128(b: k128)._mm_avg_epu8(b: b128)
if args.prev.length() == 0 {
k128 = util.make_m128i_repeat_u8(a: 0xFE)
iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) {
// The predictor, p128, is just half (rounded down) of the previous
// pixel, a128. In this branch, b128 stays zero so the average of
// a128 and b128 is just half of a128. _mm_avg_epu8 rounds up, but
// (a128 & 0xFE_repeated) takes out the low bits of a128's bytes.
p128 = a128._mm_and_si128(b: k128)._mm_avg_epu8(b: b128)
// Add the predictor to the residual and, for the next iteration,
// set its previous pixel, a128, to this one, x128.
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
x128 = x128._mm_add_epi8(b: p128)
a128 = x128
curr.poke_u32le!(a: x128.truncate_u32())
}
// Add the predictor to the residual and, for the next iteration,
// set its previous pixel, a128, to this one, x128.
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
x128 = x128._mm_add_epi8(b: p128)
a128 = x128
curr.poke_u32le!(a: x128.truncate_u32())
}
} else {
k128 = util.make_m128i_repeat_u8(a: 0x01)
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
// Load the pixel from the row above.
b128 = util.make_m128i_single_u32(a: prev.peek_u32le())
} else {
k128 = util.make_m128i_repeat_u8(a: 0x01)
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
// Load the pixel from the row above.
b128 = util.make_m128i_single_u32(a: prev.peek_u32le())
// The predictor, p128, is the average (rounded down) of the
// previous pixel, a128, and the pixel above, b128.
p128 = a128._mm_avg_epu8(b: b128)
// The predictor, p128, is the average (rounded down) of the
// previous pixel, a128, and the pixel above, b128.
p128 = a128._mm_avg_epu8(b: b128)
// Subtract a correction term because _mm_avg_epu8 rounds up but
// the PNG filter rounds down. The correction term is the low bit
// of each byte of (a128 ^ b128).
p128 = p128._mm_sub_epi8(b: k128._mm_and_si128(b: a128._mm_xor_si128(b: b128)))
// Subtract a correction term because _mm_avg_epu8 rounds up but
// the PNG filter rounds down. The correction term is the low bit
// of each byte of (a128 ^ b128).
p128 = p128._mm_sub_epi8(b: k128._mm_and_si128(b: a128._mm_xor_si128(b: b128)))
// Add the predictor to the residual and, for the next iteration,
// set its previous pixel, a128, to this one, x128.
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
x128 = x128._mm_add_epi8(b: p128)
a128 = x128
curr.poke_u32le!(a: x128.truncate_u32())
}
}
// Add the predictor to the residual and, for the next iteration,
// set its previous pixel, a128, to this one, x128.
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
x128 = x128._mm_add_epi8(b: p128)
a128 = x128
curr.poke_u32le!(a: x128.truncate_u32())
}
}
}
// --------
@@ -134,146 +134,146 @@ pri func decoder.filter_3_distance_4_x86_sse42!(curr: slice base.u8, prev: slice
// Filter 4: Paeth.
pri func decoder.filter_4_distance_3_x86_sse42!(curr: slice base.u8, prev: slice base.u8),
choose cpu_arch >= x86_sse42,
choose cpu_arch >= x86_sse42,
{
// See the comments in filter_4_distance_4_x86_sse42 for an explanation of
// how this works. That function's single loop is copied twice here, once
// with "length: 4" and once with "length: 3". It's generally faster to
// load 4 bytes at a time instead of 3.
//
// Differences between that function and this one are marked with a §.
// See the comments in filter_4_distance_4_x86_sse42 for an explanation of
// how this works. That function's single loop is copied twice here, once
// with "length: 4" and once with "length: 3". It's generally faster to
// load 4 bytes at a time instead of 3.
//
// Differences between that function and this one are marked with a §.
var curr : slice base.u8
var prev : slice base.u8
var curr : slice base.u8
var prev : slice base.u8
var util : base.x86_sse42_utility
var x128 : base.x86_m128i
var a128 : base.x86_m128i
var b128 : base.x86_m128i
var c128 : base.x86_m128i
var p128 : base.x86_m128i
var pa128 : base.x86_m128i
var pb128 : base.x86_m128i
var pc128 : base.x86_m128i
var smallest128 : base.x86_m128i
var z128 : base.x86_m128i
var util : base.x86_sse42_utility
var x128 : base.x86_m128i
var a128 : base.x86_m128i
var b128 : base.x86_m128i
var c128 : base.x86_m128i
var p128 : base.x86_m128i
var pa128 : base.x86_m128i
var pb128 : base.x86_m128i
var pc128 : base.x86_m128i
var smallest128 : base.x86_m128i
var z128 : base.x86_m128i
// § The advance is 3, not 4.
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 3, unroll: 2) {
b128 = util.make_m128i_single_u32(a: prev.peek_u32le())
b128 = b128._mm_unpacklo_epi8(b: z128)
pa128 = b128._mm_sub_epi16(b: c128)
pb128 = a128._mm_sub_epi16(b: c128)
pc128 = pa128._mm_add_epi16(b: pb128)
pa128 = pa128._mm_abs_epi16()
pb128 = pb128._mm_abs_epi16()
pc128 = pc128._mm_abs_epi16()
smallest128 = pc128._mm_min_epi16(b: pb128._mm_min_epi16(b: pa128))
p128 = c128._mm_blendv_epi8(
b: b128,
mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8(
b: a128,
mask: smallest128._mm_cmpeq_epi16(b: pa128))
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
x128 = x128._mm_unpacklo_epi8(b: z128)
x128 = x128._mm_add_epi8(b: p128)
a128 = x128
c128 = b128
x128 = x128._mm_packus_epi16(b: x128)
// § poke_u24le replaces poke_u32le.
curr.poke_u24le!(a: x128.truncate_u32())
// § The advance is 3, not 4.
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 3, unroll: 2) {
b128 = util.make_m128i_single_u32(a: prev.peek_u32le())
b128 = b128._mm_unpacklo_epi8(b: z128)
pa128 = b128._mm_sub_epi16(b: c128)
pb128 = a128._mm_sub_epi16(b: c128)
pc128 = pa128._mm_add_epi16(b: pb128)
pa128 = pa128._mm_abs_epi16()
pb128 = pb128._mm_abs_epi16()
pc128 = pc128._mm_abs_epi16()
smallest128 = pc128._mm_min_epi16(b: pb128._mm_min_epi16(b: pa128))
p128 = c128._mm_blendv_epi8(
b: b128,
mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8(
b: a128,
mask: smallest128._mm_cmpeq_epi16(b: pa128))
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
x128 = x128._mm_unpacklo_epi8(b: z128)
x128 = x128._mm_add_epi8(b: p128)
a128 = x128
c128 = b128
x128 = x128._mm_packus_epi16(b: x128)
// § poke_u24le replaces poke_u32le.
curr.poke_u24le!(a: x128.truncate_u32())
// § The length and advance are both 3, not 4.
} else (length: 3, advance: 3, unroll: 1) {
// § peek_u24le_as_u32 replaces peek_u32le.
b128 = util.make_m128i_single_u32(a: prev.peek_u24le_as_u32())
b128 = b128._mm_unpacklo_epi8(b: z128)
pa128 = b128._mm_sub_epi16(b: c128)
pb128 = a128._mm_sub_epi16(b: c128)
pc128 = pa128._mm_add_epi16(b: pb128)
pa128 = pa128._mm_abs_epi16()
pb128 = pb128._mm_abs_epi16()
pc128 = pc128._mm_abs_epi16()
smallest128 = pc128._mm_min_epi16(b: pb128._mm_min_epi16(b: pa128))
p128 = c128._mm_blendv_epi8(
b: b128,
mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8(
b: a128,
mask: smallest128._mm_cmpeq_epi16(b: pa128))
// § peek_u24le_as_u32 replaces peek_u32le.
x128 = util.make_m128i_single_u32(a: curr.peek_u24le_as_u32())
x128 = x128._mm_unpacklo_epi8(b: z128)
x128 = x128._mm_add_epi8(b: p128)
// § These assignments are unnecessary; this is the last iteration.
// a128 = x128
// c128 = b128
x128 = x128._mm_packus_epi16(b: x128)
// § poke_u24le replaces poke_u32le.
curr.poke_u24le!(a: x128.truncate_u32())
}
// § The length and advance are both 3, not 4.
} else (length: 3, advance: 3, unroll: 1) {
// § peek_u24le_as_u32 replaces peek_u32le.
b128 = util.make_m128i_single_u32(a: prev.peek_u24le_as_u32())
b128 = b128._mm_unpacklo_epi8(b: z128)
pa128 = b128._mm_sub_epi16(b: c128)
pb128 = a128._mm_sub_epi16(b: c128)
pc128 = pa128._mm_add_epi16(b: pb128)
pa128 = pa128._mm_abs_epi16()
pb128 = pb128._mm_abs_epi16()
pc128 = pc128._mm_abs_epi16()
smallest128 = pc128._mm_min_epi16(b: pb128._mm_min_epi16(b: pa128))
p128 = c128._mm_blendv_epi8(
b: b128,
mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8(
b: a128,
mask: smallest128._mm_cmpeq_epi16(b: pa128))
// § peek_u24le_as_u32 replaces peek_u32le.
x128 = util.make_m128i_single_u32(a: curr.peek_u24le_as_u32())
x128 = x128._mm_unpacklo_epi8(b: z128)
x128 = x128._mm_add_epi8(b: p128)
// § These assignments are unnecessary; this is the last iteration.
// a128 = x128
// c128 = b128
x128 = x128._mm_packus_epi16(b: x128)
// § poke_u24le replaces poke_u32le.
curr.poke_u24le!(a: x128.truncate_u32())
}
}
pri func decoder.filter_4_distance_4_x86_sse42!(curr: slice base.u8, prev: slice base.u8),
choose cpu_arch >= x86_sse42,
choose cpu_arch >= x86_sse42,
{
var curr : slice base.u8
var prev : slice base.u8
var curr : slice base.u8
var prev : slice base.u8
var util : base.x86_sse42_utility
var x128 : base.x86_m128i
var a128 : base.x86_m128i
var b128 : base.x86_m128i
var c128 : base.x86_m128i
var p128 : base.x86_m128i
var pa128 : base.x86_m128i
var pb128 : base.x86_m128i
var pc128 : base.x86_m128i
var smallest128 : base.x86_m128i
var z128 : base.x86_m128i
var util : base.x86_sse42_utility
var x128 : base.x86_m128i
var a128 : base.x86_m128i
var b128 : base.x86_m128i
var c128 : base.x86_m128i
var p128 : base.x86_m128i
var pa128 : base.x86_m128i
var pb128 : base.x86_m128i
var pc128 : base.x86_m128i
var smallest128 : base.x86_m128i
var z128 : base.x86_m128i
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
// Load the pixel from the row above.
b128 = util.make_m128i_single_u32(a: prev.peek_u32le())
iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) {
// Load the pixel from the row above.
b128 = util.make_m128i_single_u32(a: prev.peek_u32le())
// Convert from u8 to i16 by unpacking it with zeroes.
b128 = b128._mm_unpacklo_epi8(b: z128)
// Convert from u8 to i16 by unpacking it with zeroes.
b128 = b128._mm_unpacklo_epi8(b: z128)
// Compute:
// - pa128 = (m128 - a128)
// - pb128 = (m128 - b128)
// - pc128 = (m128 - c128)
// where m128 = (a128 + b128 - c128).
pa128 = b128._mm_sub_epi16(b: c128)
pb128 = a128._mm_sub_epi16(b: c128)
pc128 = pa128._mm_add_epi16(b: pb128)
// Compute:
// - pa128 = (m128 - a128)
// - pb128 = (m128 - b128)
// - pc128 = (m128 - c128)
// where m128 = (a128 + b128 - c128).
pa128 = b128._mm_sub_epi16(b: c128)
pb128 = a128._mm_sub_epi16(b: c128)
pc128 = pa128._mm_add_epi16(b: pb128)
// Compute the smallest absolute value of pa128, pb128 and pc128.
pa128 = pa128._mm_abs_epi16()
pb128 = pb128._mm_abs_epi16()
pc128 = pc128._mm_abs_epi16()
smallest128 = pc128._mm_min_epi16(b: pb128._mm_min_epi16(b: pa128))
// Compute the smallest absolute value of pa128, pb128 and pc128.
pa128 = pa128._mm_abs_epi16()
pb128 = pb128._mm_abs_epi16()
pc128 = pc128._mm_abs_epi16()
smallest128 = pc128._mm_min_epi16(b: pb128._mm_min_epi16(b: pa128))
// The predictor, p128, is whichever of a128, b128 or c128 such that
// pa128, pb128 or pc128 matches this smallest absolute value. Ties are
// broken in favor of a128 then b128 then c128.
//
// The a._mm_blendv_epi8(b, mask) method picks b when mask is true.
p128 = c128._mm_blendv_epi8(
b: b128,
mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8(
b: a128,
mask: smallest128._mm_cmpeq_epi16(b: pa128))
// The predictor, p128, is whichever of a128, b128 or c128 such that
// pa128, pb128 or pc128 matches this smallest absolute value. Ties are
// broken in favor of a128 then b128 then c128.
//
// The a._mm_blendv_epi8(b, mask) method picks b when mask is true.
p128 = c128._mm_blendv_epi8(
b: b128,
mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8(
b: a128,
mask: smallest128._mm_cmpeq_epi16(b: pa128))
// Add the predictor to the residual and, for the next iteration, set
// its previous pixels, a128 and c128, to x128 and b128.
//
// Again, convert between u8 and i16 by unpacking and re-packing.
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
x128 = x128._mm_unpacklo_epi8(b: z128)
x128 = x128._mm_add_epi8(b: p128)
a128 = x128
c128 = b128
x128 = x128._mm_packus_epi16(b: x128)
curr.poke_u32le!(a: x128.truncate_u32())
}
// Add the predictor to the residual and, for the next iteration, set
// its previous pixels, a128 and c128, to x128 and b128.
//
// Again, convert between u8 and i16 by unpacking and re-packing.
x128 = util.make_m128i_single_u32(a: curr.peek_u32le())
x128 = x128._mm_unpacklo_epi8(b: z128)
x128 = x128._mm_add_epi8(b: p128)
a128 = x128
c128 = b128
x128 = x128._mm_packus_epi16(b: x128)
curr.poke_u32le!(a: x128.truncate_u32())
}
}
+1463 -1463
View File
@@ -16,1708 +16,1708 @@ use "std/crc32"
use "std/zlib"
pub struct decoder? implements base.image_decoder(
// The 0x00FF_FFFF limit is arbitrary (the PNG spec says 0x7FFF_FFFF) but
// it means that (width * height * bytes_per_pixel) doesn't overflow a u64.
width : base.u32[..= 0x00FF_FFFF],
height : base.u32[..= 0x00FF_FFFF],
// The 0x00FF_FFFF limit is arbitrary (the PNG spec says 0x7FFF_FFFF) but
// it means that (width * height * bytes_per_pixel) doesn't overflow a u64.
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],
// 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,
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 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,
// 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,
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,
ignore_checksum : base.bool,
depth : base.u8[..= 16],
color_type : base.u8[..= 6],
filter_distance : base.u8[..= 8],
interlace_pass : base.u8[..= 7],
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,
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,
metadata_is_zlib_compressed : base.bool,
zlib_is_dirty : base.bool,
zlib_is_dirty : base.bool,
chunk_type : base.u32,
chunk_type_array : array[4] base.u8,
chunk_length : base.u32,
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,
// 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,
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,
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,
// 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,
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,
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,
// 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,
swizzler : base.pixel_swizzler,
util : base.utility,
)(
crc32 : crc32.ieee_hasher,
zlib : zlib.decoder,
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,
// 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.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
if args.quirk == base.QUIRK_IGNORE_CHECKSUM {
this.ignore_checksum = args.enabled
this.zlib.set_quirk_enabled!(quirk: args.quirk, enabled: args.enabled)
}
if args.quirk == base.QUIRK_IGNORE_CHECKSUM {
this.ignore_checksum = args.enabled
this.zlib.set_quirk_enabled!(quirk: args.quirk, enabled: args.enabled)
}
}
pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
var status : base.status
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
} endwhile
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
} endwhile
}
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
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[..])
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
} endwhile
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
} endwhile
// 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"
}
// 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
}
// 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)
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[..])
}
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
} endwhile
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
} endwhile
// 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"
}
// 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"
}
} endwhile
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"
}
} endwhile
if (this.color_type == 3) and (not this.seen_plte) {
return "#missing palette"
}
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
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))
}
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
}
// 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
this.call_sequence = 0x20
}
pri func decoder.decode_ihdr?(src: base.io_reader) {
var a32 : base.u32
var a8 : base.u8
var a32 : base.u32
var a8 : base.u8
a32 = args.src.read_u32be?()
if (a32 == 0) or (a32 >= 0x8000_0000) {
return "#bad header"
} else if a32 >= 0x0100_0000 {
return "#unsupported PNG file"
}
this.width = a32
a32 = args.src.read_u32be?()
if (a32 == 0) or (a32 >= 0x8000_0000) {
return "#bad header"
} else if a32 >= 0x0100_0000 {
return "#unsupported PNG file"
}
this.width = a32
a32 = args.src.read_u32be?()
if (a32 == 0) or (a32 >= 0x8000_0000) {
return "#bad header"
} else if a32 >= 0x0100_0000 {
return "#unsupported PNG file"
}
this.height = a32
a32 = args.src.read_u32be?()
if (a32 == 0) or (a32 >= 0x8000_0000) {
return "#bad header"
} else if a32 >= 0x0100_0000 {
return "#unsupported PNG file"
}
this.height = a32
// Depth.
a8 = args.src.read_u8?()
if a8 > 16 {
return "#bad header"
}
this.depth = a8
// 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
// 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"
}
// 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"
}
// 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"
}
// 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!()
// 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
}
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]
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 == 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 == 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 == 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__BGRA_NONPREMUL
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
this.filter_distance = 2
choose filter_and_swizzle = [filter_and_swizzle_tricky]
} 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 == 4 {
if this.depth == 8 {
this.dst_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
this.src_pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
this.filter_distance = 2
choose filter_and_swizzle = [filter_and_swizzle_tricky]
} 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]
}
}
} 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]
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)
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]
}
// 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
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"
}
}
} 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
}
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 (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 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 == '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 == '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 == '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 == '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 == '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) or
((this.color_type == 3) and (not this.seen_plte)) {
return "#bad chunk"
}
this.decode_trns?(src: args.src)
this.seen_trns = true
}
}
} else if this.chunk_type == 'tRNS'le {
if this.seen_trns or (this.color_type > 3) or
((this.color_type == 3) and (not this.seen_plte)) {
return "#bad chunk"
}
this.decode_trns?(src: args.src)
this.seen_trns = true
}
}
if this.metadata_fourcc == 0 {
args.src.skip_u32?(n: this.chunk_length)
}
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?()
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
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
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
// 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
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
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
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
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
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)
}
// 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_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"
}
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
}
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
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 c : base.u8
var c : base.u8
// Skip the NUL-terminated color profile name.
while true {
if this.chunk_length <= 0 {
return "#bad chunk"
}
this.chunk_length -= 1
c = args.src.read_u8?()
if c == 0 {
break
}
} endwhile
// Skip the NUL-terminated color profile name.
while true {
if this.chunk_length <= 0 {
return "#bad chunk"
}
this.chunk_length -= 1
c = args.src.read_u8?()
if c == 0 {
break
}
} endwhile
// Compression method.
if this.chunk_length <= 0 {
return "#bad chunk"
}
this.chunk_length -= 1
c = args.src.read_u8?()
if c <> 0 {
return "#unsupported PNG compression method"
}
// Compression method.
if this.chunk_length <= 0 {
return "#bad chunk"
}
this.chunk_length -= 1
c = args.src.read_u8?()
if c <> 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
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
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
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
} endwhile
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
} endwhile
// 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
} endwhile
// 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
} endwhile
}
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
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
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
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
}
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
} 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
}
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
} endwhile
} 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
} endwhile
} else {
return "#bad chunk"
}
} else {
return "#bad chunk"
}
}
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
var status : base.status
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
} endwhile
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
} endwhile
}
pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
var checksum_have : base.u32
var checksum_have : base.u32
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.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.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
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
} else {
// Decode the next IEND or fcTL chunk.
while true {
this.chunk_length = args.src.read_u32be?()
this.chunk_type = args.src.read_u32le?()
if this.chunk_type == 'IEND'le {
if this.chunk_length <> 0 {
return "#bad chunk"
}
checksum_have = args.src.read_u32le?()
if (not this.ignore_checksum) and (checksum_have <> 0x8260_42AE) {
return "#bad checksum"
}
this.call_sequence = 0x60
return base."@end of data"
} else if this.chunk_type == 'fdAT'le {
return "#bad chunk"
} else if this.chunk_type == 'fcTL'le {
this.frame_config_io_position = args.src.position() ~mod- 8
this.decode_fctl?(src: args.src)
args.src.skip?(n: 4) // Skip the checksum.
break
}
} else {
// Decode the next IEND or fcTL chunk.
while true {
this.chunk_length = args.src.read_u32be?()
this.chunk_type = args.src.read_u32le?()
if this.chunk_type == 'IEND'le {
if this.chunk_length <> 0 {
return "#bad chunk"
}
checksum_have = args.src.read_u32le?()
if (not this.ignore_checksum) and (checksum_have <> 0x8260_42AE) {
return "#bad checksum"
}
this.call_sequence = 0x60
return base."@end of data"
} else if this.chunk_type == 'fdAT'le {
return "#bad chunk"
} else if this.chunk_type == 'fcTL'le {
this.frame_config_io_position = args.src.position() ~mod- 8
this.decode_fctl?(src: args.src)
args.src.skip?(n: 4) // Skip the checksum.
break
}
this.decode_other_chunk?(src: args.src, framy: true)
if this.metadata_fourcc <> 0 {
this.call_sequence = 0x30
return base."@metadata reported"
}
args.src.skip_u32?(n: 4) // Skip the checksum.
this.chunk_length = 0
} endwhile
}
this.decode_other_chunk?(src: args.src, framy: true)
if this.metadata_fourcc <> 0 {
this.call_sequence = 0x30
return base."@metadata reported"
}
args.src.skip_u32?(n: 4) // Skip the checksum.
this.chunk_length = 0
} endwhile
}
if args.dst <> nullptr {
args.dst.set!(bounds: this.util.make_rect_ie_u32(
min_incl_x: this.frame_rect_x0,
min_incl_y: this.frame_rect_y0,
max_excl_x: this.frame_rect_x1,
max_excl_y: this.frame_rect_y1),
duration: this.frame_duration,
index: this.num_decoded_frame_configs_value as base.u64,
io_position: this.frame_config_io_position,
disposal: this.frame_disposal,
opaque_within_bounds: (this.color_type <= 3) and (not this.seen_trns),
overwrite_instead_of_blend: this.frame_overwrite_instead_of_blend,
background_color: 0x0000_0000)
}
if args.dst <> nullptr {
args.dst.set!(bounds: this.util.make_rect_ie_u32(
min_incl_x: this.frame_rect_x0,
min_incl_y: this.frame_rect_y0,
max_excl_x: this.frame_rect_x1,
max_excl_y: this.frame_rect_y1),
duration: this.frame_duration,
index: this.num_decoded_frame_configs_value as base.u64,
io_position: this.frame_config_io_position,
disposal: this.frame_disposal,
opaque_within_bounds: (this.color_type <= 3) and (not this.seen_trns),
overwrite_instead_of_blend: this.frame_overwrite_instead_of_blend,
background_color: 0x0000_0000)
}
this.num_decoded_frame_configs_value ~sat+= 1
this.call_sequence = 0x40
this.num_decoded_frame_configs_value ~sat+= 1
this.call_sequence = 0x40
}
pri func decoder.skip_frame?(src: base.io_reader) {
var seq_num : base.u32
var seq_num : base.u32
this.chunk_type_array[0] = 0
this.chunk_type_array[1] = 0
this.chunk_type_array[2] = 0
this.chunk_type_array[3] = 0
this.chunk_type_array[0] = 0
this.chunk_type_array[1] = 0
this.chunk_type_array[2] = 0
this.chunk_type_array[3] = 0
while true {
if args.src.length() < 8 {
yield? base."$short read"
continue
}
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
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 this.chunk_type_array[0] == 'f' {
return "#bad chunk"
}
this.chunk_type_array[0] = 'I'
this.chunk_type_array[1] = 'D'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
if this.chunk_type == 'IDAT'le {
if this.chunk_type_array[0] == 'f' {
return "#bad chunk"
}
this.chunk_type_array[0] = 'I'
this.chunk_type_array[1] = 'D'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
} else if this.chunk_type == 'fdAT'le {
if this.chunk_type_array[0] == 'I' {
return "#bad chunk"
}
this.chunk_type_array[0] = 'f'
this.chunk_type_array[1] = 'd'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
if this.chunk_length < 4 {
return "#bad chunk"
}
this.chunk_length -= 4
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
seq_num = args.src.read_u32be?()
if seq_num <> 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
args.src.skip?(n: (this.chunk_length as base.u64) + 4) // +4 for the checksum.
this.chunk_length = 0
continue
} else if this.chunk_type == 'fdAT'le {
if this.chunk_type_array[0] == 'I' {
return "#bad chunk"
}
this.chunk_type_array[0] = 'f'
this.chunk_type_array[1] = 'd'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
if this.chunk_length < 4 {
return "#bad chunk"
}
this.chunk_length -= 4
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
seq_num = args.src.read_u32be?()
if seq_num <> 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
args.src.skip?(n: (this.chunk_length as base.u64) + 4) // +4 for the checksum.
this.chunk_length = 0
continue
} else if this.chunk_type_array[0] <> 0 {
break
} else if this.chunk_type_array[0] <> 0 {
break
} else if this.chunk_type == 'fcTL'le {
return "#bad chunk"
}
} else if this.chunk_type == 'fcTL'le {
return "#bad chunk"
}
// +12 for chunk length, chunk type and checksum.
args.src.skip?(n: (this.chunk_length as base.u64) + 12)
this.chunk_length = 0
} endwhile
// +12 for chunk length, chunk type and checksum.
args.src.skip?(n: (this.chunk_length as base.u64) + 12)
this.chunk_length = 0
} endwhile
this.num_decoded_frames_value ~sat+= 1
this.call_sequence = 0x20
this.num_decoded_frames_value ~sat+= 1
this.call_sequence = 0x20
}
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
var status : base.status
var status : base.status
while true {
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
yield? status
} endwhile
while true {
status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts)
if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
yield? status
} endwhile
}
pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
var seq_num : base.u32
var status : base.status
var pass_width : base.u32[..= 0x00FF_FFFF]
var pass_height : base.u32[..= 0x00FF_FFFF]
var seq_num : base.u32
var status : base.status
var pass_width : base.u32[..= 0x00FF_FFFF]
var pass_height : base.u32[..= 0x00FF_FFFF]
if (this.call_sequence & 0x10) <> 0 {
return base."#bad call sequence"
} else if this.call_sequence >= 0x60 {
return base."@end of data"
} else if this.call_sequence <> 0x40 {
this.do_decode_frame_config?(dst: nullptr, src: args.src)
}
if (this.call_sequence & 0x10) <> 0 {
return base."#bad call sequence"
} else if this.call_sequence >= 0x60 {
return base."@end of data"
} else if this.call_sequence <> 0x40 {
this.do_decode_frame_config?(dst: nullptr, src: args.src)
}
while true {
if args.src.length() < 8 {
yield? base."$short read"
continue
}
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
this.chunk_length = args.src.peek_u32be()
this.chunk_type = (args.src.peek_u64le() >> 32) as base.u32
if this.chunk_type == 'IDAT'le {
this.chunk_type_array[0] = 'I'
this.chunk_type_array[1] = 'D'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
if not this.ignore_checksum {
this.crc32.reset!()
this.crc32.update_u32!(x: this.chunk_type_array[..])
}
break
if this.chunk_type == 'IDAT'le {
this.chunk_type_array[0] = 'I'
this.chunk_type_array[1] = 'D'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
if not this.ignore_checksum {
this.crc32.reset!()
this.crc32.update_u32!(x: this.chunk_type_array[..])
}
break
} else if this.chunk_type == 'fdAT'le {
this.chunk_type_array[0] = 'f'
this.chunk_type_array[1] = 'd'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
if this.chunk_length < 4 {
return "#bad chunk"
}
this.chunk_length -= 4
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
seq_num = args.src.read_u32be?()
if seq_num <> 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
break
} else if this.chunk_type == 'fdAT'le {
this.chunk_type_array[0] = 'f'
this.chunk_type_array[1] = 'd'
this.chunk_type_array[2] = 'A'
this.chunk_type_array[3] = 'T'
if this.chunk_length < 4 {
return "#bad chunk"
}
this.chunk_length -= 4
args.src.skip_u32_fast!(actual: 8, worst_case: 8)
seq_num = args.src.read_u32be?()
if seq_num <> 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
break
} else if this.chunk_type == 'fcTL'le {
return "#bad chunk"
}
} else if this.chunk_type == 'fcTL'le {
return "#bad chunk"
}
// +12 for chunk length, chunk type and checksum.
args.src.skip?(n: (this.chunk_length as base.u64) + 12)
this.chunk_length = 0
} endwhile
// +12 for chunk length, chunk type and checksum.
args.src.skip?(n: (this.chunk_length as base.u64) + 12)
this.chunk_length = 0
} endwhile
if this.zlib_is_dirty {
this.zlib.reset!()
if this.ignore_checksum {
this.zlib.set_quirk_enabled!(quirk: base.QUIRK_IGNORE_CHECKSUM, enabled: true)
}
}
this.zlib_is_dirty = true
if this.zlib_is_dirty {
this.zlib.reset!()
if this.ignore_checksum {
this.zlib.set_quirk_enabled!(quirk: base.QUIRK_IGNORE_CHECKSUM, enabled: true)
}
}
this.zlib_is_dirty = true
status = this.swizzler.prepare!(
dst_pixfmt: args.dst.pixel_format(),
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt),
src_palette: this.src_palette[..],
blend: args.blend)
if not status.is_ok() {
return status
}
status = this.swizzler.prepare!(
dst_pixfmt: args.dst.pixel_format(),
dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]),
src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt),
src_palette: this.src_palette[..],
blend: args.blend)
if not status.is_ok() {
return status
}
this.workbuf_hist_pos_base = 0
while true {
if (this.chunk_type_array[0] == 'I') {
pass_width = 0x00FF_FFFF &
(((INTERLACING[this.interlace_pass][1] as base.u32) + this.width) >>
INTERLACING[this.interlace_pass][0])
pass_height = 0x00FF_FFFF &
(((INTERLACING[this.interlace_pass][4] as base.u32) + this.height) >>
INTERLACING[this.interlace_pass][3])
} else {
pass_width = 0x00FF_FFFF & (this.frame_rect_x1 ~mod- this.frame_rect_x0)
pass_height = 0x00FF_FFFF & (this.frame_rect_y1 ~mod- this.frame_rect_y0)
}
this.workbuf_hist_pos_base = 0
while true {
if (this.chunk_type_array[0] == 'I') {
pass_width = 0x00FF_FFFF &
(((INTERLACING[this.interlace_pass][1] as base.u32) + this.width) >>
INTERLACING[this.interlace_pass][0])
pass_height = 0x00FF_FFFF &
(((INTERLACING[this.interlace_pass][4] as base.u32) + this.height) >>
INTERLACING[this.interlace_pass][3])
} else {
pass_width = 0x00FF_FFFF & (this.frame_rect_x1 ~mod- this.frame_rect_x0)
pass_height = 0x00FF_FFFF & (this.frame_rect_y1 ~mod- this.frame_rect_y0)
}
if (pass_width > 0) and (pass_height > 0) {
this.pass_bytes_per_row = this.calculate_bytes_per_row(width: pass_width)
this.pass_workbuf_length = (pass_height as base.u64) * (1 + this.pass_bytes_per_row)
while true {
status =? this.decode_pass?(src: args.src, workbuf: args.workbuf)
if status.is_ok() {
break
} else if status.is_error() or
((status == base."$short read") and args.src.is_closed()) {
// The input was invalid or truncated. Produce whatever
// pixels we can.
if this.workbuf_wi <= args.workbuf.length() {
// This might return "#internal error: inconsistent
// workbuf length" because of the ".. this.workbuf_wi".
// We just ignore the error.
this.filter_and_swizzle!(dst: args.dst, workbuf: args.workbuf[.. this.workbuf_wi])
}
if status == base."$short read" {
return "#truncated input"
}
}
yield? status
} endwhile
status = this.filter_and_swizzle!(dst: args.dst, workbuf: args.workbuf)
if not status.is_ok() {
return status
}
this.workbuf_hist_pos_base ~mod+= this.pass_workbuf_length
}
if (pass_width > 0) and (pass_height > 0) {
this.pass_bytes_per_row = this.calculate_bytes_per_row(width: pass_width)
this.pass_workbuf_length = (pass_height as base.u64) * (1 + this.pass_bytes_per_row)
while true {
status =? this.decode_pass?(src: args.src, workbuf: args.workbuf)
if status.is_ok() {
break
} else if status.is_error() or
((status == base."$short read") and args.src.is_closed()) {
// The input was invalid or truncated. Produce whatever
// pixels we can.
if this.workbuf_wi <= args.workbuf.length() {
// This might return "#internal error: inconsistent
// workbuf length" because of the ".. this.workbuf_wi".
// We just ignore the error.
this.filter_and_swizzle!(dst: args.dst, workbuf: args.workbuf[.. this.workbuf_wi])
}
if status == base."$short read" {
return "#truncated input"
}
}
yield? status
} endwhile
status = this.filter_and_swizzle!(dst: args.dst, workbuf: args.workbuf)
if not status.is_ok() {
return status
}
this.workbuf_hist_pos_base ~mod+= this.pass_workbuf_length
}
if (this.interlace_pass == 0) or (this.interlace_pass >= 7) {
break
}
this.interlace_pass += 1
} endwhile
if (this.interlace_pass == 0) or (this.interlace_pass >= 7) {
break
}
this.interlace_pass += 1
} endwhile
this.num_decoded_frames_value ~sat+= 1
this.call_sequence = 0x20
this.num_decoded_frames_value ~sat+= 1
this.call_sequence = 0x20
}
pri func decoder.decode_pass?(src: base.io_reader, workbuf: slice base.u8) {
var w : base.io_writer
var w_mark : base.u64
var r_mark : base.u64
var zlib_status : base.status
var checksum_have : base.u32
var checksum_want : base.u32
var seq_num : base.u32
var w : base.io_writer
var w_mark : base.u64
var r_mark : base.u64
var zlib_status : base.status
var checksum_have : base.u32
var checksum_want : base.u32
var seq_num : base.u32
this.workbuf_wi = 0
while true {
if (this.workbuf_wi > this.pass_workbuf_length) or (
this.pass_workbuf_length > args.workbuf.length()) {
return base."#bad workbuf length"
}
io_bind (io: w, data: args.workbuf[this.workbuf_wi .. this.pass_workbuf_length], history_position: this.workbuf_hist_pos_base ~mod+ this.workbuf_wi) {
io_limit (io: args.src, limit: (this.chunk_length as base.u64)) {
w_mark = w.mark()
r_mark = args.src.mark()
zlib_status =? this.zlib.transform_io?(
dst: w, src: args.src, workbuf: this.util.empty_slice_u8())
if not this.ignore_checksum {
this.crc32.update_u32!(x: args.src.since(mark: r_mark))
}
this.chunk_length ~sat-= (args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
this.workbuf_wi ~sat+= w.count_since(mark: w_mark)
}
}
this.workbuf_wi = 0
while true {
if (this.workbuf_wi > this.pass_workbuf_length) or (
this.pass_workbuf_length > args.workbuf.length()) {
return base."#bad workbuf length"
}
io_bind (io: w, data: args.workbuf[this.workbuf_wi .. this.pass_workbuf_length], history_position: this.workbuf_hist_pos_base ~mod+ this.workbuf_wi) {
io_limit (io: args.src, limit: (this.chunk_length as base.u64)) {
w_mark = w.mark()
r_mark = args.src.mark()
zlib_status =? this.zlib.transform_io?(
dst: w, src: args.src, workbuf: this.util.empty_slice_u8())
if not this.ignore_checksum {
this.crc32.update_u32!(x: args.src.since(mark: r_mark))
}
this.chunk_length ~sat-= (args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
this.workbuf_wi ~sat+= w.count_since(mark: w_mark)
}
}
if zlib_status.is_ok() {
if this.chunk_length > 0 {
// TODO: should this really be a fatal error?
return base."#too much data"
}
checksum_want = args.src.read_u32be?()
// Verify the final IDAT chunk's CRC-32 checksum.
if (not this.ignore_checksum) and (this.chunk_type_array[0] == 'I') {
checksum_have = this.crc32.update_u32!(x: this.util.empty_slice_u8())
if checksum_have <> checksum_want {
return "#bad checksum"
}
}
break
} else if zlib_status == base."$short write" {
if (1 <= this.interlace_pass) and (this.interlace_pass <= 6) {
break
}
return base."#too much data"
} else if zlib_status <> base."$short read" {
return zlib_status
} else if this.chunk_length == 0 {
// Verify the non-final IDAT chunk's CRC-32 checksum.
checksum_want = args.src.read_u32be?()
if (not this.ignore_checksum) and (this.chunk_type_array[0] == 'I') {
checksum_have = this.crc32.update_u32!(x: this.util.empty_slice_u8())
if checksum_have <> checksum_want {
return "#bad checksum"
}
}
if zlib_status.is_ok() {
if this.chunk_length > 0 {
// TODO: should this really be a fatal error?
return base."#too much data"
}
checksum_want = args.src.read_u32be?()
// Verify the final IDAT chunk's CRC-32 checksum.
if (not this.ignore_checksum) and (this.chunk_type_array[0] == 'I') {
checksum_have = this.crc32.update_u32!(x: this.util.empty_slice_u8())
if checksum_have <> checksum_want {
return "#bad checksum"
}
}
break
} else if zlib_status == base."$short write" {
if (1 <= this.interlace_pass) and (this.interlace_pass <= 6) {
break
}
return base."#too much data"
} else if zlib_status <> base."$short read" {
return zlib_status
} else if this.chunk_length == 0 {
// Verify the non-final IDAT chunk's CRC-32 checksum.
checksum_want = args.src.read_u32be?()
if (not this.ignore_checksum) and (this.chunk_type_array[0] == 'I') {
checksum_have = this.crc32.update_u32!(x: this.util.empty_slice_u8())
if checksum_have <> checksum_want {
return "#bad checksum"
}
}
// The next chunk should be another IDAT or fdAT.
this.chunk_length = args.src.read_u32be?()
this.chunk_type = args.src.read_u32le?()
if (this.chunk_type_array[0] == 'I') {
if this.chunk_type <> 'IDAT'le {
return "#bad chunk"
}
// The IDAT is part of the next CRC-32 checksum's input.
if not this.ignore_checksum {
this.crc32.reset!()
this.crc32.update_u32!(x: this.chunk_type_array[..])
}
} else {
if (this.chunk_type <> 'fdAT'le) or (this.chunk_length < 4) {
return "#bad chunk"
}
this.chunk_length -= 4
seq_num = args.src.read_u32be?()
if seq_num <> 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
}
continue
} else if args.src.length() > 0 {
return "#internal error: zlib decoder did not exhaust its input"
}
yield? base."$short read"
} endwhile
// The next chunk should be another IDAT or fdAT.
this.chunk_length = args.src.read_u32be?()
this.chunk_type = args.src.read_u32le?()
if (this.chunk_type_array[0] == 'I') {
if this.chunk_type <> 'IDAT'le {
return "#bad chunk"
}
// The IDAT is part of the next CRC-32 checksum's input.
if not this.ignore_checksum {
this.crc32.reset!()
this.crc32.update_u32!(x: this.chunk_type_array[..])
}
} else {
if (this.chunk_type <> 'fdAT'le) or (this.chunk_length < 4) {
return "#bad chunk"
}
this.chunk_length -= 4
seq_num = args.src.read_u32be?()
if seq_num <> 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
}
continue
} else if args.src.length() > 0 {
return "#internal error: zlib decoder did not exhaust its input"
}
yield? base."$short read"
} endwhile
if this.workbuf_wi <> this.pass_workbuf_length {
return base."#not enough data"
} else if 0 < args.workbuf.length() {
// For the top row, the Paeth filter (4) is equivalent to the Sub
// filter (1), but the Paeth implementation is simpler if it can assume
// that there is a previous row.
if args.workbuf[0] == 4 {
args.workbuf[0] = 1
}
}
if this.workbuf_wi <> this.pass_workbuf_length {
return base."#not enough data"
} else if 0 < args.workbuf.length() {
// For the top row, the Paeth filter (4) is equivalent to the Sub
// filter (1), but the Paeth implementation is simpler if it can assume
// that there is a previous row.
if args.workbuf[0] == 4 {
args.workbuf[0] = 1
}
}
}
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
return this.util.make_rect_ie_u32(
min_incl_x: this.frame_rect_x0,
min_incl_y: this.frame_rect_y0,
max_excl_x: this.frame_rect_x1,
max_excl_y: this.frame_rect_y1)
return this.util.make_rect_ie_u32(
min_incl_x: this.frame_rect_x0,
min_incl_y: this.frame_rect_y0,
max_excl_x: this.frame_rect_x1,
max_excl_y: this.frame_rect_y1)
}
pub func decoder.num_animation_loops() base.u32 {
return this.num_animation_loops_value
return this.num_animation_loops_value
}
pub func decoder.num_decoded_frame_configs() base.u64 {
return this.num_decoded_frame_configs_value as base.u64
return this.num_decoded_frame_configs_value as base.u64
}
pub func decoder.num_decoded_frames() base.u64 {
return this.num_decoded_frames_value as base.u64
return this.num_decoded_frames_value as base.u64
}
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
if this.call_sequence < 0x20 {
return base."#bad call sequence"
} else if (args.index >= (this.num_animation_frames_value as base.u64)) or
((args.index == 0) and (args.io_position <> this.first_config_io_position)) {
return base."#bad argument"
}
this.call_sequence = 0x28
if this.interlace_pass >= 1 {
this.interlace_pass = 1
}
this.frame_config_io_position = args.io_position
this.num_decoded_frame_configs_value = (args.index & 0xFFFF_FFFF) as base.u32
this.num_decoded_frames_value = this.num_decoded_frame_configs_value
return ok
if this.call_sequence < 0x20 {
return base."#bad call sequence"
} else if (args.index >= (this.num_animation_frames_value as base.u64)) or
((args.index == 0) and (args.io_position <> this.first_config_io_position)) {
return base."#bad argument"
}
this.call_sequence = 0x28
if this.interlace_pass >= 1 {
this.interlace_pass = 1
}
this.frame_config_io_position = args.io_position
this.num_decoded_frame_configs_value = (args.index & 0xFFFF_FFFF) as base.u32
this.num_decoded_frames_value = this.num_decoded_frame_configs_value
return ok
}
pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) {
if args.fourcc == 'CHRM'be {
this.report_metadata_chrm = args.report
} else if args.fourcc == 'EXIF'be {
this.report_metadata_exif = args.report
} else if args.fourcc == 'GAMA'be {
this.report_metadata_gama = args.report
} else if args.fourcc == 'ICCP'be {
this.report_metadata_iccp = args.report
} else if args.fourcc == 'KVP 'be {
this.report_metadata_kvp = args.report
} else if args.fourcc == 'SRGB'be {
this.report_metadata_srgb = args.report
}
if args.fourcc == 'CHRM'be {
this.report_metadata_chrm = args.report
} else if args.fourcc == 'EXIF'be {
this.report_metadata_exif = args.report
} else if args.fourcc == 'GAMA'be {
this.report_metadata_gama = args.report
} else if args.fourcc == 'ICCP'be {
this.report_metadata_iccp = args.report
} else if args.fourcc == 'KVP 'be {
this.report_metadata_kvp = args.report
} else if args.fourcc == 'SRGB'be {
this.report_metadata_srgb = args.report
}
}
pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
var status : base.status
var status : base.status
while true {
status =? this.do_tell_me_more?(dst: args.dst, minfo: args.minfo, src: args.src)
if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
yield? status
} endwhile
while true {
status =? this.do_tell_me_more?(dst: args.dst, minfo: args.minfo, src: args.src)
if (status == base."$short read") and args.src.is_closed() {
return "#truncated input"
}
yield? status
} endwhile
}
pri func decoder.do_tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
var c : base.u8
var c2 : base.u16
var w : base.io_writer
var num_written : base.u64
var w_mark : base.u64
var r_mark : base.u64
var zlib_status : base.status
var c : base.u8
var c2 : base.u16
var w : base.io_writer
var num_written : base.u64
var w_mark : base.u64
var r_mark : base.u64
var zlib_status : base.status
if (this.call_sequence & 0x10) == 0 {
return base."#bad call sequence"
}
if this.metadata_fourcc == 0 {
return base."#no more information"
}
if (this.call_sequence & 0x10) == 0 {
return base."#bad call sequence"
}
if this.metadata_fourcc == 0 {
return base."#no more information"
}
while.goto_done true {{
if this.metadata_flavor == base.MORE_INFORMATION__FLAVOR__METADATA_RAW_PASSTHROUGH {
while true {
if args.src.position() <> this.metadata_y {
return base."#bad I/O position"
} else if args.minfo <> nullptr {
args.minfo.set!(
flavor: this.metadata_flavor,
w: this.metadata_fourcc,
x: this.metadata_x,
y: this.metadata_y,
z: this.metadata_z)
}
if this.metadata_y >= this.metadata_z {
break.goto_done
}
this.metadata_y = this.metadata_z
yield? base."$even more information"
} endwhile
}
while.goto_done true {{
if this.metadata_flavor == base.MORE_INFORMATION__FLAVOR__METADATA_RAW_PASSTHROUGH {
while true {
if args.src.position() <> this.metadata_y {
return base."#bad I/O position"
} else if args.minfo <> nullptr {
args.minfo.set!(
flavor: this.metadata_flavor,
w: this.metadata_fourcc,
x: this.metadata_x,
y: this.metadata_y,
z: this.metadata_z)
}
if this.metadata_y >= this.metadata_z {
break.goto_done
}
this.metadata_y = this.metadata_z
yield? base."$even more information"
} endwhile
}
if this.metadata_is_zlib_compressed {
if this.zlib_is_dirty {
this.zlib.reset!()
if this.ignore_checksum {
this.zlib.set_quirk_enabled!(quirk: base.QUIRK_IGNORE_CHECKSUM, enabled: true)
}
}
this.zlib_is_dirty = true
this.ztxt_hist_pos = 0
}
if this.metadata_is_zlib_compressed {
if this.zlib_is_dirty {
this.zlib.reset!()
if this.ignore_checksum {
this.zlib.set_quirk_enabled!(quirk: base.QUIRK_IGNORE_CHECKSUM, enabled: true)
}
}
this.zlib_is_dirty = true
this.ztxt_hist_pos = 0
}
while.loop true {
if args.minfo <> nullptr {
args.minfo.set!(
flavor: this.metadata_flavor,
w: this.metadata_fourcc,
x: this.metadata_x,
y: this.metadata_y,
z: this.metadata_z)
}
while.loop true {
if args.minfo <> nullptr {
args.minfo.set!(
flavor: this.metadata_flavor,
w: this.metadata_fourcc,
x: this.metadata_x,
y: this.metadata_y,
z: this.metadata_z)
}
if this.metadata_flavor <> base.MORE_INFORMATION__FLAVOR__METADATA_RAW_TRANSFORM {
break.loop
}
if this.metadata_flavor <> base.MORE_INFORMATION__FLAVOR__METADATA_RAW_TRANSFORM {
break.loop
}
if this.metadata_is_zlib_compressed {
if this.chunk_type == 'iCCP'le {
io_limit (io: args.src, limit: this.chunk_length as base.u64) {
r_mark = args.src.mark()
zlib_status =? this.zlib.transform_io?(
dst: args.dst, src: args.src, workbuf: this.util.empty_slice_u8())
this.chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
if this.metadata_is_zlib_compressed {
if this.chunk_type == 'iCCP'le {
io_limit (io: args.src, limit: this.chunk_length as base.u64) {
r_mark = args.src.mark()
zlib_status =? this.zlib.transform_io?(
dst: args.dst, src: args.src, workbuf: this.util.empty_slice_u8())
this.chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
if zlib_status.is_ok() {
this.metadata_is_zlib_compressed = false
break.loop
} else if not zlib_status.is_suspension() {
return zlib_status
}
yield? zlib_status
if zlib_status.is_ok() {
this.metadata_is_zlib_compressed = false
break.loop
} else if not zlib_status.is_suspension() {
return zlib_status
}
yield? zlib_status
} else if this.chunk_type == 'iTXt'le {
// TODO: verify uncompressed data is UTF-8.
io_limit (io: args.src, limit: this.chunk_length as base.u64) {
r_mark = args.src.mark()
zlib_status =? this.zlib.transform_io?(
dst: args.dst, src: args.src, workbuf: this.util.empty_slice_u8())
this.chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
} else if this.chunk_type == 'iTXt'le {
// TODO: verify uncompressed data is UTF-8.
io_limit (io: args.src, limit: this.chunk_length as base.u64) {
r_mark = args.src.mark()
zlib_status =? this.zlib.transform_io?(
dst: args.dst, src: args.src, workbuf: this.util.empty_slice_u8())
this.chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
if zlib_status.is_ok() {
this.metadata_is_zlib_compressed = false
break.loop
} else if not zlib_status.is_suspension() {
return zlib_status
}
yield? zlib_status
if zlib_status.is_ok() {
this.metadata_is_zlib_compressed = false
break.loop
} else if not zlib_status.is_suspension() {
return zlib_status
}
yield? zlib_status
} else if this.chunk_type == 'zTXt'le {
// Fill this.dst_palette, zlib-uncompressing producing Latin-1.
if this.ztxt_ri == this.ztxt_wi {
io_bind (io: w, data: this.dst_palette[..], history_position: this.ztxt_hist_pos) {
io_limit (io: args.src, limit: this.chunk_length as base.u64) {
w_mark = w.mark()
r_mark = args.src.mark()
zlib_status =? this.zlib.transform_io?(
dst: w, src: args.src, workbuf: this.util.empty_slice_u8())
this.chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
num_written = w.count_since(mark: w_mark)
}
}
if num_written > 1024 {
return "#internal error: inconsistent I/O"
}
this.ztxt_ri = 0
this.ztxt_wi = num_written as base.u32
this.ztxt_hist_pos ~sat+= num_written
}
} else if this.chunk_type == 'zTXt'le {
// Fill this.dst_palette, zlib-uncompressing producing Latin-1.
if this.ztxt_ri == this.ztxt_wi {
io_bind (io: w, data: this.dst_palette[..], history_position: this.ztxt_hist_pos) {
io_limit (io: args.src, limit: this.chunk_length as base.u64) {
w_mark = w.mark()
r_mark = args.src.mark()
zlib_status =? this.zlib.transform_io?(
dst: w, src: args.src, workbuf: this.util.empty_slice_u8())
this.chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
num_written = w.count_since(mark: w_mark)
}
}
if num_written > 1024 {
return "#internal error: inconsistent I/O"
}
this.ztxt_ri = 0
this.ztxt_wi = num_written as base.u32
this.ztxt_hist_pos ~sat+= num_written
}
// Drain this.dst_palette, converting from Latin-1 to UTF-8.
while this.ztxt_ri < this.ztxt_wi {
assert this.ztxt_ri < 1024 via "a < b: a < c; c <= b"(c: this.ztxt_wi)
c2 = LATIN_1[this.dst_palette[this.ztxt_ri]]
if c2 == 0 {
return "#bad text chunk (not Latin-1)"
} else if c2 <= 0x7F {
if args.dst.length() <= 0 {
yield? base."$short write"
continue.loop
}
this.ztxt_ri += 1
args.dst.write_u8_fast!(a: c2 as base.u8)
} else {
if args.dst.length() <= 1 {
yield? base."$short write"
continue.loop
}
this.ztxt_ri += 1
args.dst.write_u16le_fast!(a: c2)
}
} endwhile
// Drain this.dst_palette, converting from Latin-1 to UTF-8.
while this.ztxt_ri < this.ztxt_wi {
assert this.ztxt_ri < 1024 via "a < b: a < c; c <= b"(c: this.ztxt_wi)
c2 = LATIN_1[this.dst_palette[this.ztxt_ri]]
if c2 == 0 {
return "#bad text chunk (not Latin-1)"
} else if c2 <= 0x7F {
if args.dst.length() <= 0 {
yield? base."$short write"
continue.loop
}
this.ztxt_ri += 1
args.dst.write_u8_fast!(a: c2 as base.u8)
} else {
if args.dst.length() <= 1 {
yield? base."$short write"
continue.loop
}
this.ztxt_ri += 1
args.dst.write_u16le_fast!(a: c2)
}
} endwhile
if zlib_status.is_ok() {
this.metadata_is_zlib_compressed = false
break.loop
} else if not zlib_status.is_suspension() {
return zlib_status
} else if zlib_status <> base."$short write" {
yield? zlib_status
}
if zlib_status.is_ok() {
this.metadata_is_zlib_compressed = false
break.loop
} else if not zlib_status.is_suspension() {
return zlib_status
} else if zlib_status <> base."$short write" {
yield? zlib_status
}
} else {
return "#internal error: inconsistent chunk type"
}
} else {
return "#internal error: inconsistent chunk type"
}
} else if (this.chunk_type == 'iTXt'le) and (this.metadata_fourcc == 'KVPV'be) {
// iTXt value is UTF-8.
//
// TODO: verify data is UTF-8.
while true {
if this.chunk_length <= 0 {
break.loop
} else if args.src.length() <= 0 {
yield? base."$short read"
continue.loop
} else if args.dst.length() <= 0 {
yield? base."$short write"
continue.loop
}
this.chunk_length -= 1
c = args.src.peek_u8()
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
args.dst.write_u8_fast!(a: c)
} endwhile
} else if (this.chunk_type == 'iTXt'le) and (this.metadata_fourcc == 'KVPV'be) {
// iTXt value is UTF-8.
//
// TODO: verify data is UTF-8.
while true {
if this.chunk_length <= 0 {
break.loop
} else if args.src.length() <= 0 {
yield? base."$short read"
continue.loop
} else if args.dst.length() <= 0 {
yield? base."$short write"
continue.loop
}
this.chunk_length -= 1
c = args.src.peek_u8()
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
args.dst.write_u8_fast!(a: c)
} endwhile
} else {
// Other uncompressed keys and values are Latin-1.
while true {
if this.chunk_length <= 0 {
// Keys are NUL-terminated but values are not.
if this.metadata_fourcc == 'KVPK'be {
return "#bad chunk"
}
break.loop
} else if args.src.length() <= 0 {
yield? base."$short read"
continue.loop
}
c = args.src.peek_u8()
if c == 0 {
this.chunk_length -= 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
break.loop
}
c2 = LATIN_1[c]
if c2 == 0 {
return "#bad text chunk (not Latin-1)"
} else if c2 <= 0x7F {
if args.dst.length() <= 0 {
yield? base."$short write"
continue.loop
}
this.chunk_length -= 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
args.dst.write_u8_fast!(a: c2 as base.u8)
} else {
if args.dst.length() <= 1 {
yield? base."$short write"
continue.loop
}
this.chunk_length -= 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
args.dst.write_u16le_fast!(a: c2)
}
} endwhile
}
} endwhile.loop
} else {
// Other uncompressed keys and values are Latin-1.
while true {
if this.chunk_length <= 0 {
// Keys are NUL-terminated but values are not.
if this.metadata_fourcc == 'KVPK'be {
return "#bad chunk"
}
break.loop
} else if args.src.length() <= 0 {
yield? base."$short read"
continue.loop
}
c = args.src.peek_u8()
if c == 0 {
this.chunk_length -= 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
break.loop
}
c2 = LATIN_1[c]
if c2 == 0 {
return "#bad text chunk (not Latin-1)"
} else if c2 <= 0x7F {
if args.dst.length() <= 0 {
yield? base."$short write"
continue.loop
}
this.chunk_length -= 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
args.dst.write_u8_fast!(a: c2 as base.u8)
} else {
if args.dst.length() <= 1 {
yield? base."$short write"
continue.loop
}
this.chunk_length -= 1
args.src.skip_u32_fast!(actual: 1, worst_case: 1)
args.dst.write_u16le_fast!(a: c2)
}
} endwhile
}
} endwhile.loop
// Key-value pairs come in... pairs.
if this.metadata_fourcc == 'KVPK'be {
this.metadata_fourcc = 'KVPV'be
if this.chunk_type == 'iTXt'le {
// Compression flag, compression method.
if this.chunk_length <= 1 {
return "#bad chunk"
}
this.chunk_length -= 2
c = args.src.read_u8?()
if c == 0 {
this.metadata_is_zlib_compressed = false
} else if c == 1 {
this.metadata_is_zlib_compressed = true
} else {
return "#bad chunk"
}
c = args.src.read_u8?()
if (c <> 0) and this.metadata_is_zlib_compressed {
return "#unsupported PNG compression method"
}
// Key-value pairs come in... pairs.
if this.metadata_fourcc == 'KVPK'be {
this.metadata_fourcc = 'KVPV'be
if this.chunk_type == 'iTXt'le {
// Compression flag, compression method.
if this.chunk_length <= 1 {
return "#bad chunk"
}
this.chunk_length -= 2
c = args.src.read_u8?()
if c == 0 {
this.metadata_is_zlib_compressed = false
} else if c == 1 {
this.metadata_is_zlib_compressed = true
} else {
return "#bad chunk"
}
c = args.src.read_u8?()
if (c <> 0) and this.metadata_is_zlib_compressed {
return "#unsupported PNG compression method"
}
// Skip the language tag and translated keyword: two iterations
// looking for a NUL terminator.
this.metadata_fourcc ~mod-= 2
while this.metadata_fourcc <> 'KVPV'be {
this.metadata_fourcc ~mod+= 1
while true {
if this.chunk_length <= 0 {
return "#bad chunk"
}
this.chunk_length -= 1
c = args.src.read_u8?()
if c == 0 {
break
}
} endwhile
} endwhile
// Skip the language tag and translated keyword: two iterations
// looking for a NUL terminator.
this.metadata_fourcc ~mod-= 2
while this.metadata_fourcc <> 'KVPV'be {
this.metadata_fourcc ~mod+= 1
while true {
if this.chunk_length <= 0 {
return "#bad chunk"
}
this.chunk_length -= 1
c = args.src.read_u8?()
if c == 0 {
break
}
} endwhile
} endwhile
} else if this.chunk_type == 'zTXt'le {
// Compression method.
if this.chunk_length <= 0 {
return "#bad chunk"
}
this.chunk_length -= 1
c = args.src.read_u8?()
if c <> 0 {
return "#unsupported PNG compression method"
}
this.metadata_is_zlib_compressed = true
}
} else if this.chunk_type == 'zTXt'le {
// Compression method.
if this.chunk_length <= 0 {
return "#bad chunk"
}
this.chunk_length -= 1
c = args.src.read_u8?()
if c <> 0 {
return "#unsupported PNG compression method"
}
this.metadata_is_zlib_compressed = true
}
this.call_sequence &= 0xEF
return ok
}
break.goto_done
}} endwhile.goto_done
this.call_sequence &= 0xEF
return ok
}
break.goto_done
}} endwhile.goto_done
if this.chunk_length <> 0 {
return "#bad chunk"
}
if this.chunk_length <> 0 {
return "#bad chunk"
}
// Skip the ancillary chunk's CRC-32 checksum.
args.src.skip?(n: 4)
// Skip the ancillary chunk's CRC-32 checksum.
args.src.skip?(n: 4)
this.metadata_flavor = 0
this.metadata_fourcc = 0
this.metadata_x = 0
this.metadata_y = 0
this.metadata_z = 0
this.metadata_flavor = 0
this.metadata_fourcc = 0
this.metadata_x = 0
this.metadata_y = 0
this.metadata_z = 0
this.call_sequence &= 0xEF
return ok
this.call_sequence &= 0xEF
return ok
}
pub func decoder.workbuf_len() base.range_ii_u64 {
return this.util.make_range_ii_u64(
min_incl: this.overall_workbuf_length,
max_incl: this.overall_workbuf_length)
return this.util.make_range_ii_u64(
min_incl: this.overall_workbuf_length,
max_incl: this.overall_workbuf_length)
}
+80 -80
View File
@@ -13,95 +13,95 @@
// limitations under the License.
pri func decoder.filter_and_swizzle!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) base.status,
choosy,
choosy,
{
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u64[..= 32]
var dst_bytes_per_row0 : base.u64
var dst_bytes_per_row1 : base.u64
var dst_palette : slice base.u8
var tab : table base.u8
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u64[..= 32]
var dst_bytes_per_row0 : base.u64
var dst_bytes_per_row1 : base.u64
var dst_palette : slice base.u8
var tab : table base.u8
var y : base.u32
var dst : slice base.u8
var filter : base.u8
var curr_row : slice base.u8
var prev_row : slice base.u8
var y : base.u32
var dst : slice base.u8
var filter : base.u8
var curr_row : slice base.u8
var prev_row : slice base.u8
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
dst_pixfmt = args.dst.pixel_format()
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
if (dst_bits_per_pixel & 7) <> 0 {
return base."#unsupported option"
}
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
dst_bytes_per_row0 = (this.frame_rect_x0 as base.u64) * dst_bytes_per_pixel
dst_bytes_per_row1 = (this.frame_rect_x1 as base.u64) * dst_bytes_per_pixel
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
tab = args.dst.plane(p: 0)
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
dst_pixfmt = args.dst.pixel_format()
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
if (dst_bits_per_pixel & 7) <> 0 {
return base."#unsupported option"
}
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
dst_bytes_per_row0 = (this.frame_rect_x0 as base.u64) * dst_bytes_per_pixel
dst_bytes_per_row1 = (this.frame_rect_x1 as base.u64) * dst_bytes_per_pixel
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
tab = args.dst.plane(p: 0)
if dst_bytes_per_row1 < tab.width() {
tab = tab.subtable(
min_incl_x: 0,
min_incl_y: 0,
max_incl_x: dst_bytes_per_row1,
max_incl_y: tab.height())
}
if dst_bytes_per_row1 < tab.width() {
tab = tab.subtable(
min_incl_x: 0,
min_incl_y: 0,
max_incl_x: dst_bytes_per_row1,
max_incl_y: tab.height())
}
if dst_bytes_per_row0 < tab.width() {
tab = tab.subtable(
min_incl_x: dst_bytes_per_row0,
min_incl_y: 0,
max_incl_x: tab.width(),
max_incl_y: tab.height())
} else {
tab = tab.subtable(
min_incl_x: 0,
min_incl_y: 0,
max_incl_x: 0,
max_incl_y: 0)
}
if dst_bytes_per_row0 < tab.width() {
tab = tab.subtable(
min_incl_x: dst_bytes_per_row0,
min_incl_y: 0,
max_incl_x: tab.width(),
max_incl_y: tab.height())
} else {
tab = tab.subtable(
min_incl_x: 0,
min_incl_y: 0,
max_incl_x: 0,
max_incl_y: 0)
}
y = this.frame_rect_y0
while y < this.frame_rect_y1 {
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_y1)
dst = tab.row_u32(y: y)
y = this.frame_rect_y0
while y < this.frame_rect_y1 {
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_y1)
dst = tab.row_u32(y: y)
if 1 > args.workbuf.length() {
return "#internal error: inconsistent workbuf length"
}
filter = args.workbuf[0]
args.workbuf = args.workbuf[1 ..]
if this.pass_bytes_per_row > args.workbuf.length() {
return "#internal error: inconsistent workbuf length"
}
curr_row = args.workbuf[.. this.pass_bytes_per_row]
args.workbuf = args.workbuf[this.pass_bytes_per_row ..]
if 1 > args.workbuf.length() {
return "#internal error: inconsistent workbuf length"
}
filter = args.workbuf[0]
args.workbuf = args.workbuf[1 ..]
if this.pass_bytes_per_row > args.workbuf.length() {
return "#internal error: inconsistent workbuf length"
}
curr_row = args.workbuf[.. this.pass_bytes_per_row]
args.workbuf = args.workbuf[this.pass_bytes_per_row ..]
if filter == 0 {
// No-op.
} else if filter == 1 {
this.filter_1!(curr: curr_row)
} else if filter == 2 {
this.filter_2!(curr: curr_row, prev: prev_row)
} else if filter == 3 {
this.filter_3!(curr: curr_row, prev: prev_row)
} else if filter == 4 {
this.filter_4!(curr: curr_row, prev: prev_row)
} else {
return "#bad filter"
}
if filter == 0 {
// No-op.
} else if filter == 1 {
this.filter_1!(curr: curr_row)
} else if filter == 2 {
this.filter_2!(curr: curr_row, prev: prev_row)
} else if filter == 3 {
this.filter_3!(curr: curr_row, prev: prev_row)
} else if filter == 4 {
this.filter_4!(curr: curr_row, prev: prev_row)
} else {
return "#bad filter"
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst,
dst_palette: dst_palette,
src: curr_row)
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst,
dst_palette: dst_palette,
src: curr_row)
prev_row = curr_row
y += 1
} endwhile
prev_row = curr_row
y += 1
} endwhile
return ok
return ok
}
+304 -304
View File
@@ -13,330 +13,330 @@
// limitations under the License.
pri func decoder.filter_and_swizzle_tricky!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) base.status {
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u64[..= 32]
var dst_bytes_per_row1 : base.u64
var dst_palette : slice base.u8
var tab : table base.u8
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u64[..= 32]
var dst_bytes_per_row1 : base.u64
var dst_palette : slice base.u8
var tab : table base.u8
var src_bytes_per_pixel : base.u64[..= 8]
var src_bytes_per_pixel : base.u64[..= 8]
var x : base.u32
var y : base.u32
var i : base.u64[..= 0x1FFF_FFC0]
var dst : slice base.u8
var filter : base.u8
var s : slice base.u8
var curr_row : slice base.u8
var prev_row : slice base.u8
var x : base.u32
var y : base.u32
var i : base.u64[..= 0x1FFF_FFC0]
var dst : slice base.u8
var filter : base.u8
var s : slice base.u8
var curr_row : slice base.u8
var prev_row : slice base.u8
var bits_unpacked : array[8] base.u8
var bits_packed : base.u8
var packs_remaining : base.u8
var multiplier : base.u8
var shift : base.u8[..= 7]
var bits_unpacked : array[8] base.u8
var bits_packed : base.u8
var packs_remaining : base.u8
var multiplier : base.u8
var shift : base.u8[..= 7]
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
dst_pixfmt = args.dst.pixel_format()
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
if (dst_bits_per_pixel & 7) <> 0 {
return base."#unsupported option"
}
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
dst_bytes_per_row1 = (this.frame_rect_x1 as base.u64) * dst_bytes_per_pixel
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
tab = args.dst.plane(p: 0)
// TODO: the dst_pixfmt variable shouldn't be necessary. We should be able
// to chain the two calls: "args.dst.pixel_format().bits_per_pixel()".
dst_pixfmt = args.dst.pixel_format()
dst_bits_per_pixel = dst_pixfmt.bits_per_pixel()
if (dst_bits_per_pixel & 7) <> 0 {
return base."#unsupported option"
}
dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64
dst_bytes_per_row1 = (this.frame_rect_x1 as base.u64) * dst_bytes_per_pixel
dst_palette = args.dst.palette_or_else(fallback: this.dst_palette[..])
tab = args.dst.plane(p: 0)
src_bytes_per_pixel = 1
if this.depth >= 8 {
src_bytes_per_pixel = (NUM_CHANNELS[this.color_type] as base.u64) *
((this.depth >> 3) as base.u64)
}
src_bytes_per_pixel = 1
if this.depth >= 8 {
src_bytes_per_pixel = (NUM_CHANNELS[this.color_type] as base.u64) *
((this.depth >> 3) as base.u64)
}
if (this.chunk_type_array[0] == 'I') {
y = INTERLACING[this.interlace_pass][5] as base.u32
} else {
y = this.frame_rect_y0
}
while y < this.frame_rect_y1 {
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_y1)
dst = tab.row_u32(y: y)
if dst_bytes_per_row1 < dst.length() {
dst = dst[.. dst_bytes_per_row1]
}
if (this.chunk_type_array[0] == 'I') {
y = INTERLACING[this.interlace_pass][5] as base.u32
} else {
y = this.frame_rect_y0
}
while y < this.frame_rect_y1 {
assert y < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_y1)
dst = tab.row_u32(y: y)
if dst_bytes_per_row1 < dst.length() {
dst = dst[.. dst_bytes_per_row1]
}
if 1 > args.workbuf.length() {
return "#internal error: inconsistent workbuf length"
}
filter = args.workbuf[0]
args.workbuf = args.workbuf[1 ..]
if this.pass_bytes_per_row > args.workbuf.length() {
return "#internal error: inconsistent workbuf length"
}
curr_row = args.workbuf[.. this.pass_bytes_per_row]
args.workbuf = args.workbuf[this.pass_bytes_per_row ..]
if 1 > args.workbuf.length() {
return "#internal error: inconsistent workbuf length"
}
filter = args.workbuf[0]
args.workbuf = args.workbuf[1 ..]
if this.pass_bytes_per_row > args.workbuf.length() {
return "#internal error: inconsistent workbuf length"
}
curr_row = args.workbuf[.. this.pass_bytes_per_row]
args.workbuf = args.workbuf[this.pass_bytes_per_row ..]
if filter == 0 {
// No-op.
} else if filter == 1 {
this.filter_1!(curr: curr_row)
} else if filter == 2 {
this.filter_2!(curr: curr_row, prev: prev_row)
} else if filter == 3 {
this.filter_3!(curr: curr_row, prev: prev_row)
} else if filter == 4 {
this.filter_4!(curr: curr_row, prev: prev_row)
} else {
return "#bad filter"
}
if filter == 0 {
// No-op.
} else if filter == 1 {
this.filter_1!(curr: curr_row)
} else if filter == 2 {
this.filter_2!(curr: curr_row, prev: prev_row)
} else if filter == 3 {
this.filter_3!(curr: curr_row, prev: prev_row)
} else if filter == 4 {
this.filter_4!(curr: curr_row, prev: prev_row)
} else {
return "#bad filter"
}
s = curr_row
if (this.chunk_type_array[0] == 'I') {
x = INTERLACING[this.interlace_pass][2] as base.u32
} else {
x = this.frame_rect_x0
}
if this.depth == 8 {
while x < this.frame_rect_x1,
inv y < 0x00FF_FFFF,
{
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
i = (x as base.u64) * dst_bytes_per_pixel
if i <= dst.length() {
if this.color_type == 4 {
if 2 <= s.length() {
bits_unpacked[0] = s[0]
bits_unpacked[1] = s[0]
bits_unpacked[2] = s[0]
bits_unpacked[3] = s[1]
s = s[2 ..]
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 4])
}
s = curr_row
if (this.chunk_type_array[0] == 'I') {
x = INTERLACING[this.interlace_pass][2] as base.u32
} else {
x = this.frame_rect_x0
}
if this.depth == 8 {
while x < this.frame_rect_x1,
inv y < 0x00FF_FFFF,
{
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
i = (x as base.u64) * dst_bytes_per_pixel
if i <= dst.length() {
if this.color_type == 4 {
if 2 <= s.length() {
bits_unpacked[0] = s[0]
bits_unpacked[1] = s[0]
bits_unpacked[2] = s[0]
bits_unpacked[3] = s[1]
s = s[2 ..]
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 4])
}
} else if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) <> 0 {
if this.color_type == 0 {
if 1 <= s.length() {
bits_unpacked[0] = s[0]
bits_unpacked[1] = s[0]
bits_unpacked[2] = s[0]
bits_unpacked[3] = 0xFF
s = s[1 ..]
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
((bits_unpacked[0] as base.u32) << 0) |
((bits_unpacked[1] as base.u32) << 8) |
((bits_unpacked[2] as base.u32) << 16) |
((bits_unpacked[3] as base.u32) << 24)) {
bits_unpacked[0] = 0
bits_unpacked[1] = 0
bits_unpacked[2] = 0
bits_unpacked[3] = 0
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 4])
}
} else {
if 3 <= s.length() {
bits_unpacked[0] = s[2]
bits_unpacked[1] = s[1]
bits_unpacked[2] = s[0]
bits_unpacked[3] = 0xFF
s = s[3 ..]
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
((bits_unpacked[0] as base.u32) << 0) |
((bits_unpacked[1] as base.u32) << 8) |
((bits_unpacked[2] as base.u32) << 16) |
((bits_unpacked[3] as base.u32) << 24)) {
bits_unpacked[0] = 0
bits_unpacked[1] = 0
bits_unpacked[2] = 0
bits_unpacked[3] = 0
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 4])
}
}
} else if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) <> 0 {
if this.color_type == 0 {
if 1 <= s.length() {
bits_unpacked[0] = s[0]
bits_unpacked[1] = s[0]
bits_unpacked[2] = s[0]
bits_unpacked[3] = 0xFF
s = s[1 ..]
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
((bits_unpacked[0] as base.u32) << 0) |
((bits_unpacked[1] as base.u32) << 8) |
((bits_unpacked[2] as base.u32) << 16) |
((bits_unpacked[3] as base.u32) << 24)) {
bits_unpacked[0] = 0
bits_unpacked[1] = 0
bits_unpacked[2] = 0
bits_unpacked[3] = 0
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 4])
}
} else {
if 3 <= s.length() {
bits_unpacked[0] = s[2]
bits_unpacked[1] = s[1]
bits_unpacked[2] = s[0]
bits_unpacked[3] = 0xFF
s = s[3 ..]
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
((bits_unpacked[0] as base.u32) << 0) |
((bits_unpacked[1] as base.u32) << 8) |
((bits_unpacked[2] as base.u32) << 16) |
((bits_unpacked[3] as base.u32) << 24)) {
bits_unpacked[0] = 0
bits_unpacked[1] = 0
bits_unpacked[2] = 0
bits_unpacked[3] = 0
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 4])
}
}
} else if src_bytes_per_pixel <= s.length() {
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: s[.. src_bytes_per_pixel])
s = s[src_bytes_per_pixel ..]
}
}
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
} endwhile
} else if src_bytes_per_pixel <= s.length() {
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: s[.. src_bytes_per_pixel])
s = s[src_bytes_per_pixel ..]
}
}
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
} endwhile
} else if this.depth < 8 {
multiplier = 1
if this.color_type == 0 { // Color type 0 means base.PIXEL_FORMAT__Y.
multiplier = LOW_BIT_DEPTH_MULTIPLIERS[this.depth]
}
shift = (8 - this.depth) & 7
packs_remaining = 0
} else if this.depth < 8 {
multiplier = 1
if this.color_type == 0 { // Color type 0 means base.PIXEL_FORMAT__Y.
multiplier = LOW_BIT_DEPTH_MULTIPLIERS[this.depth]
}
shift = (8 - this.depth) & 7
packs_remaining = 0
while x < this.frame_rect_x1,
inv y < 0x00FF_FFFF,
inv this.depth < 8,
{
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
i = (x as base.u64) * dst_bytes_per_pixel
if i <= dst.length() {
if (packs_remaining == 0) and (1 <= s.length()) {
packs_remaining = LOW_BIT_DEPTH_NUM_PACKS[this.depth]
bits_packed = s[0]
s = s[1 ..]
}
bits_unpacked[0] = (bits_packed >> shift) ~mod* multiplier
bits_packed = bits_packed ~mod<< this.depth
packs_remaining = packs_remaining ~mod- 1
while x < this.frame_rect_x1,
inv y < 0x00FF_FFFF,
inv this.depth < 8,
{
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
i = (x as base.u64) * dst_bytes_per_pixel
if i <= dst.length() {
if (packs_remaining == 0) and (1 <= s.length()) {
packs_remaining = LOW_BIT_DEPTH_NUM_PACKS[this.depth]
bits_packed = s[0]
s = s[1 ..]
}
bits_unpacked[0] = (bits_packed >> shift) ~mod* multiplier
bits_packed = bits_packed ~mod<< this.depth
packs_remaining = packs_remaining ~mod- 1
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) <> 0 {
bits_unpacked[1] = bits_unpacked[0]
bits_unpacked[2] = bits_unpacked[0]
bits_unpacked[3] = 0xFF
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
((bits_unpacked[0] as base.u32) << 0) |
((bits_unpacked[1] as base.u32) << 8) |
((bits_unpacked[2] as base.u32) << 16) |
((bits_unpacked[3] as base.u32) << 24)) {
bits_unpacked[0] = 0
bits_unpacked[1] = 0
bits_unpacked[2] = 0
bits_unpacked[3] = 0
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 4])
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) <> 0 {
bits_unpacked[1] = bits_unpacked[0]
bits_unpacked[2] = bits_unpacked[0]
bits_unpacked[3] = 0xFF
if ((this.remap_transparency & 0xFFFF_FFFF) as base.u32) == (
((bits_unpacked[0] as base.u32) << 0) |
((bits_unpacked[1] as base.u32) << 8) |
((bits_unpacked[2] as base.u32) << 16) |
((bits_unpacked[3] as base.u32) << 24)) {
bits_unpacked[0] = 0
bits_unpacked[1] = 0
bits_unpacked[2] = 0
bits_unpacked[3] = 0
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 4])
} else {
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 1])
}
}
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
} endwhile
} else {
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 1])
}
}
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
} endwhile
} else {
while x < this.frame_rect_x1,
inv y < 0x00FF_FFFF,
{
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
i = (x as base.u64) * dst_bytes_per_pixel
if i <= dst.length() {
if this.color_type == 0 {
if 2 <= s.length() {
bits_unpacked[0] = s[1]
bits_unpacked[1] = s[0]
bits_unpacked[2] = s[1]
bits_unpacked[3] = s[0]
bits_unpacked[4] = s[1]
bits_unpacked[5] = s[0]
bits_unpacked[6] = 0xFF
bits_unpacked[7] = 0xFF
s = s[2 ..]
if this.remap_transparency == (
((bits_unpacked[0] as base.u64) << 0) |
((bits_unpacked[1] as base.u64) << 8) |
((bits_unpacked[2] as base.u64) << 16) |
((bits_unpacked[3] as base.u64) << 24) |
((bits_unpacked[4] as base.u64) << 32) |
((bits_unpacked[5] as base.u64) << 40) |
((bits_unpacked[6] as base.u64) << 48) |
((bits_unpacked[7] as base.u64) << 56)) {
bits_unpacked[0] = 0
bits_unpacked[1] = 0
bits_unpacked[2] = 0
bits_unpacked[3] = 0
bits_unpacked[4] = 0
bits_unpacked[5] = 0
bits_unpacked[6] = 0
bits_unpacked[7] = 0
}
}
} else {
while x < this.frame_rect_x1,
inv y < 0x00FF_FFFF,
{
assert x < 0x00FF_FFFF via "a < b: a < c; c <= b"(c: this.frame_rect_x1)
i = (x as base.u64) * dst_bytes_per_pixel
if i <= dst.length() {
if this.color_type == 0 {
if 2 <= s.length() {
bits_unpacked[0] = s[1]
bits_unpacked[1] = s[0]
bits_unpacked[2] = s[1]
bits_unpacked[3] = s[0]
bits_unpacked[4] = s[1]
bits_unpacked[5] = s[0]
bits_unpacked[6] = 0xFF
bits_unpacked[7] = 0xFF
s = s[2 ..]
if this.remap_transparency == (
((bits_unpacked[0] as base.u64) << 0) |
((bits_unpacked[1] as base.u64) << 8) |
((bits_unpacked[2] as base.u64) << 16) |
((bits_unpacked[3] as base.u64) << 24) |
((bits_unpacked[4] as base.u64) << 32) |
((bits_unpacked[5] as base.u64) << 40) |
((bits_unpacked[6] as base.u64) << 48) |
((bits_unpacked[7] as base.u64) << 56)) {
bits_unpacked[0] = 0
bits_unpacked[1] = 0
bits_unpacked[2] = 0
bits_unpacked[3] = 0
bits_unpacked[4] = 0
bits_unpacked[5] = 0
bits_unpacked[6] = 0
bits_unpacked[7] = 0
}
}
} else if this.color_type == 2 {
if 6 <= s.length() {
bits_unpacked[0] = s[5]
bits_unpacked[1] = s[4]
bits_unpacked[2] = s[3]
bits_unpacked[3] = s[2]
bits_unpacked[4] = s[1]
bits_unpacked[5] = s[0]
bits_unpacked[6] = 0xFF
bits_unpacked[7] = 0xFF
s = s[6 ..]
if this.remap_transparency == (
((bits_unpacked[0] as base.u64) << 0) |
((bits_unpacked[1] as base.u64) << 8) |
((bits_unpacked[2] as base.u64) << 16) |
((bits_unpacked[3] as base.u64) << 24) |
((bits_unpacked[4] as base.u64) << 32) |
((bits_unpacked[5] as base.u64) << 40) |
((bits_unpacked[6] as base.u64) << 48) |
((bits_unpacked[7] as base.u64) << 56)) {
bits_unpacked[0] = 0
bits_unpacked[1] = 0
bits_unpacked[2] = 0
bits_unpacked[3] = 0
bits_unpacked[4] = 0
bits_unpacked[5] = 0
bits_unpacked[6] = 0
bits_unpacked[7] = 0
}
}
} else if this.color_type == 2 {
if 6 <= s.length() {
bits_unpacked[0] = s[5]
bits_unpacked[1] = s[4]
bits_unpacked[2] = s[3]
bits_unpacked[3] = s[2]
bits_unpacked[4] = s[1]
bits_unpacked[5] = s[0]
bits_unpacked[6] = 0xFF
bits_unpacked[7] = 0xFF
s = s[6 ..]
if this.remap_transparency == (
((bits_unpacked[0] as base.u64) << 0) |
((bits_unpacked[1] as base.u64) << 8) |
((bits_unpacked[2] as base.u64) << 16) |
((bits_unpacked[3] as base.u64) << 24) |
((bits_unpacked[4] as base.u64) << 32) |
((bits_unpacked[5] as base.u64) << 40) |
((bits_unpacked[6] as base.u64) << 48) |
((bits_unpacked[7] as base.u64) << 56)) {
bits_unpacked[0] = 0
bits_unpacked[1] = 0
bits_unpacked[2] = 0
bits_unpacked[3] = 0
bits_unpacked[4] = 0
bits_unpacked[5] = 0
bits_unpacked[6] = 0
bits_unpacked[7] = 0
}
}
} else if this.color_type == 4 {
if 4 <= s.length() {
bits_unpacked[0] = s[1]
bits_unpacked[1] = s[0]
bits_unpacked[2] = s[1]
bits_unpacked[3] = s[0]
bits_unpacked[4] = s[1]
bits_unpacked[5] = s[0]
bits_unpacked[6] = s[3]
bits_unpacked[7] = s[2]
s = s[4 ..]
}
} else if this.color_type == 4 {
if 4 <= s.length() {
bits_unpacked[0] = s[1]
bits_unpacked[1] = s[0]
bits_unpacked[2] = s[1]
bits_unpacked[3] = s[0]
bits_unpacked[4] = s[1]
bits_unpacked[5] = s[0]
bits_unpacked[6] = s[3]
bits_unpacked[7] = s[2]
s = s[4 ..]
}
} else {
if 8 <= s.length() {
bits_unpacked[0] = s[5]
bits_unpacked[1] = s[4]
bits_unpacked[2] = s[3]
bits_unpacked[3] = s[2]
bits_unpacked[4] = s[1]
bits_unpacked[5] = s[0]
bits_unpacked[6] = s[7]
bits_unpacked[7] = s[6]
s = s[8 ..]
}
}
} else {
if 8 <= s.length() {
bits_unpacked[0] = s[5]
bits_unpacked[1] = s[4]
bits_unpacked[2] = s[3]
bits_unpacked[3] = s[2]
bits_unpacked[4] = s[1]
bits_unpacked[5] = s[0]
bits_unpacked[6] = s[7]
bits_unpacked[7] = s[6]
s = s[8 ..]
}
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 8])
}
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
} endwhile
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst[i ..],
dst_palette: dst_palette,
src: bits_unpacked[.. 8])
}
x += (1 as base.u32) << INTERLACING[this.interlace_pass][0]
} endwhile
}
prev_row = curr_row
y += (1 as base.u32) << INTERLACING[this.interlace_pass][3]
} endwhile
prev_row = curr_row
y += (1 as base.u32) << INTERLACING[this.interlace_pass][3]
} endwhile
return ok
return ok
}