std/webp: implement VP8 Lossless

This commit is contained in:
Nigel Tao committed 2024-07-07 22:13:50 +10:00
1 parent 09f4500e19
commit 00e6e71672
8 files changed
+5778 -83

No files matched your search

+531
View File
@@ -0,0 +1,531 @@
// Copyright 2024 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
pri func decoder.decode_huffman_groups?(src: base.io_reader, n_huffman_groups: base.u32[..= 256]) {
var hg : base.u32
var ht : base.u32
hg = 0
while hg < args.n_huffman_groups {
assert hg < 256 via "a < b: a < c; c <= b"(c: args.n_huffman_groups)
ht = 0
while ht < 5,
inv hg < 256,
{
this.decode_huffman_tree?(src: args.src, hg: hg, ht: ht)
ht += 1
} endwhile
hg += 1
} endwhile
}
pri func decoder.decode_huffman_tree?(src: base.io_reader, hg: base.u32[..= 255], ht: base.u32[..= 4]) {
var c8 : base.u8
var use_simple : base.u32[..= 1]
var status : base.status
if args.ht >= 4 {
this.ht_n_symbols = 40
} else if args.ht > 0 {
this.ht_n_symbols = 256
} else if this.color_cache_bits == 0 {
this.ht_n_symbols = 280
} else {
this.ht_n_symbols = 280 + ((1 as base.u32) << this.color_cache_bits)
}
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
use_simple = this.bits & 1
this.bits >>= 1
this.n_bits -= 1
if use_simple <> 0 {
this.decode_huffman_tree_simple?(src: args.src, hg: args.hg, ht: args.ht)
} else {
this.decode_code_length_code_lengths?(src: args.src)
status = this.build_code_lengths_huffman_nodes!()
if not status.is_ok() {
return status
}
this.build_code_lengths?(src: args.src)
status = this.build_huffman_nodes!(hg: args.hg, ht: args.ht)
if not status.is_ok() {
return status
}
}
}
pri func decoder.decode_huffman_tree_simple?(src: base.io_reader, hg: base.u32[..= 255], ht: base.u32[..= 4]) {
var c8 : base.u8
var use_second_symbol : base.u32[..= 1]
var first_symbol_n_bits : base.u32[..= 8]
var symbol0 : base.u32[..= 0xFF]
var symbol1 : base.u32[..= 0xFF]
var base_offset : base.u32[..= 0x064C]
if this.n_bits < 2 {
c8 = args.src.read_u8?()
if this.n_bits >= 2 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 2
}
use_second_symbol = this.bits & 1
first_symbol_n_bits = (((this.bits & 2) >> 1) * 7) + 1
this.bits >>= 2
this.n_bits -= 2
if this.n_bits < first_symbol_n_bits {
c8 = args.src.read_u8?()
if this.n_bits >= first_symbol_n_bits {
return "#internal error: inconsistent n_bits"
}
assert this.n_bits < 8 via "a < b: a < c; c <= b"(c: first_symbol_n_bits)
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 8
assert first_symbol_n_bits <= this.n_bits via "a <= b: a <= c; c <= b"(c: 8)
assert this.n_bits >= first_symbol_n_bits via "a >= b: b <= a"()
}
symbol0 = this.bits & (((1 as base.u32) << first_symbol_n_bits) - 1)
this.bits >>= first_symbol_n_bits
this.n_bits -= first_symbol_n_bits
base_offset = HUFFMAN_TABLE_BASE_OFFSETS[args.ht] as base.u32
if use_second_symbol <> 0 {
if this.n_bits < 8 {
c8 = args.src.read_u8?()
if this.n_bits >= 8 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 8
}
symbol1 = this.bits & 0xFF
this.bits >>= 8
this.n_bits -= 8
this.huffman_nodes[args.hg][base_offset + 0] = (base_offset + 1) as base.u16
this.huffman_nodes[args.hg][base_offset + 1] = (symbol0 | 0x8000) as base.u16
this.huffman_nodes[args.hg][base_offset + 2] = (symbol1 | 0x8000) as base.u16
} else {
this.huffman_nodes[args.hg][base_offset] = (symbol0 | 0x8000) as base.u16
}
}
pri func decoder.decode_code_length_code_lengths?(src: base.io_reader) {
var c8 : base.u8
var n_codes : base.u32[..= 19]
var i : base.u32
if this.n_bits < 4 {
c8 = args.src.read_u8?()
if this.n_bits >= 4 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 4
}
n_codes = (this.bits & 15) + 4
this.bits >>= 4
this.n_bits -= 4
i = 0
while i < n_codes {
assert i < 19 via "a < b: a < c; c <= b"(c: n_codes)
if this.n_bits < 3 {
c8 = args.src.read_u8?()
if this.n_bits >= 3 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 3
}
this.code_length_code_lengths[CODE_LENGTH_CODE_ORDER[i]] = (this.bits & 7) as base.u8
this.bits >>= 3
this.n_bits -= 3
i += 1
} endwhile
while i < 19 {
this.code_length_code_lengths[CODE_LENGTH_CODE_ORDER[i]] = 0
i += 1
} endwhile
}
pri func decoder.build_code_lengths_huffman_nodes!() base.status {
var code_bits : base.u32
var code_len : base.u32[..= 7] // The "code length code length".
var symbol : base.u32[..= 19] // The "code length or 16, 17, 18".
var histogram : array[8] base.u32
var n_used_symbols : base.u32
var last_used_symbol : base.u32[..= 18]
var subscription_weight : base.u32[..= 0x8000]
var subscription_total : base.u32
var curr_code : base.u32
var next_codes : array[9] base.u32
var n_branches : base.u32
var h : base.u32[..= 36]
var children : base.u32
var node : base.u16
symbol = 0
while symbol < 19 {
code_len = this.code_length_code_lengths[symbol] as base.u32
if code_len <> 0 {
histogram[code_len] ~mod+= 1
n_used_symbols ~mod+= 1
last_used_symbol = symbol
}
symbol += 1
} endwhile
if n_used_symbols < 1 {
return "#bad Huffman code"
} else if n_used_symbols == 1 {
this.code_lengths_huffman_nodes[0] = (last_used_symbol | 0x8000) as base.u16
return ok
}
subscription_weight = 1 << 14
code_len = 1
while true {
curr_code = (curr_code ~mod+ histogram[code_len]) ~mod<< 1
next_codes[code_len + 1] = curr_code
subscription_total ~mod+= subscription_weight ~mod* histogram[code_len]
subscription_weight >>= 1
if code_len >= 7 {
break
}
code_len += 1
} endwhile
if subscription_total > (1 << 15) {
return "#bad Huffman code (over-subscribed)"
} else if subscription_total < (1 << 15) {
return "#bad Huffman code (under-subscribed)"
}
this.code_lengths_huffman_nodes[0] = 0
symbol = 0
while symbol < 19 {
code_len = this.code_length_code_lengths[symbol] as base.u32
if code_len > 0 {
code_bits = next_codes[code_len]
next_codes[code_len] ~mod+= 1
// Insert {symbol, code_bits, code_len} into the node tree.
code_bits ~mod<<= 32 - code_len
h = 0
while code_len > 0,
inv symbol < 19,
{
node = this.code_lengths_huffman_nodes[h]
if node == 0 {
children = 1 ~mod+ (2 ~mod* n_branches)
children = children.min(no_more_than: 35)
this.code_lengths_huffman_nodes[h] = children as base.u16
this.code_lengths_huffman_nodes[children + 0] = 0
this.code_lengths_huffman_nodes[children + 1] = 0
h = children + (code_bits >> 31)
n_branches ~mod+= 1
} else {
children = node as base.u32
h = children.min(no_more_than: 35) + (code_bits >> 31)
}
code_bits ~mod<<= 1
code_len -= 1
} endwhile
this.code_lengths_huffman_nodes[h] = (symbol | 0x8000) as base.u16
}
symbol += 1
} endwhile
return ok
}
pri func decoder.build_huffman_nodes!(hg: base.u32[..= 255], ht: base.u32[..= 4]) base.status {
var base_offset : base.u32[..= 0x064C]
var code_bits : base.u32
var code_len : base.u32[..= 15]
var symbol : base.u32[..= 2328]
var histogram : array[16] base.u32
var n_used_symbols : base.u32
var last_used_symbol : base.u32[..= 2328]
var subscription_weight : base.u32[..= 0x8000]
var subscription_total : base.u32
var curr_code : base.u32
var next_codes : array[17] base.u32
var n_branches : base.u32
var h : base.u32[..= 0x187A]
var children : base.u32
var node : base.u16
base_offset = HUFFMAN_TABLE_BASE_OFFSETS[args.ht] as base.u32
symbol = 0
while symbol < this.ht_n_symbols {
assert symbol < 2328 via "a < b: a < c; c <= b"(c: this.ht_n_symbols)
code_len = (this.code_lengths[symbol] & 15) as base.u32
if code_len <> 0 {
histogram[code_len] ~mod+= 1
n_used_symbols ~mod+= 1
last_used_symbol = symbol
}
symbol += 1
} endwhile
if n_used_symbols < 1 {
return "#bad Huffman code"
} else if n_used_symbols == 1 {
this.huffman_nodes[args.hg][base_offset] = (last_used_symbol | 0x8000) as base.u16
return ok
}
subscription_weight = 1 << 14
code_len = 1
while true {
curr_code = (curr_code ~mod+ histogram[code_len]) ~mod<< 1
next_codes[code_len + 1] = curr_code
subscription_total ~mod+= subscription_weight ~mod* histogram[code_len]
subscription_weight >>= 1
if code_len >= 15 {
break
}
code_len += 1
} endwhile
if subscription_total > (1 << 15) {
return "#bad Huffman code (over-subscribed)"
} else if subscription_total < (1 << 15) {
return "#bad Huffman code (under-subscribed)"
}
this.huffman_nodes[args.hg][base_offset] = 0
symbol = 0
while symbol < this.ht_n_symbols {
assert symbol < 2328 via "a < b: a < c; c <= b"(c: this.ht_n_symbols)
code_len = (this.code_lengths[symbol] & 15) as base.u32
if code_len <> 0 {
code_bits = next_codes[code_len]
next_codes[code_len] ~mod+= 1
// Insert {symbol, code_bits, code_len} into the node tree.
code_bits ~mod<<= 32 - code_len
h = base_offset
while code_len > 0,
inv symbol < 2328,
{
node = this.huffman_nodes[args.hg][h]
if node == 0 {
children = base_offset ~mod+ (1 ~mod+ (2 ~mod* n_branches))
children = children.min(no_more_than: 0x1879)
this.huffman_nodes[args.hg][h] = children as base.u16
this.huffman_nodes[args.hg][children + 0] = 0
this.huffman_nodes[args.hg][children + 1] = 0
h = children + (code_bits >> 31)
n_branches ~mod+= 1
} else {
children = node as base.u32
h = children.min(no_more_than: 0x1879) + (code_bits >> 31)
}
code_bits ~mod<<= 1
code_len -= 1
} endwhile
this.huffman_nodes[args.hg][h] = (symbol | 0x8000) as base.u16
}
symbol += 1
} endwhile
return ok
}
pri func decoder.build_code_lengths?(src: base.io_reader) {
var c8 : base.u8
var use_length : base.u32[..= 1]
var length_n_bits : base.u32[..= 16]
var length : base.u32[..= 65537]
var prev_code_length : base.u16[..= 15]
var h : base.u32[..= 36]
var s : base.u32[..= 2328]
var s_max : base.u32
var node : base.u16
var symbol : base.u32[..= 0x7FFF]
var repeat_value : base.u16[..= 15]
var repeat_n_bits : base.u32[..= 7]
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
use_length = this.bits & 1
this.bits >>= 1
this.n_bits -= 1
this.ht_code_lengths_remaining = this.ht_n_symbols
if use_length <> 0 {
if this.n_bits < 3 {
c8 = args.src.read_u8?()
if this.n_bits >= 3 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 3
}
length_n_bits = ((this.bits & 7) * 2) + 2
this.bits >>= 3
this.n_bits -= 3
while this.n_bits < length_n_bits,
post this.n_bits >= length_n_bits,
{
c8 = args.src.read_u8?()
if this.n_bits >= length_n_bits {
return "#internal error: inconsistent n_bits"
}
assert this.n_bits < 16 via "a < b: a < c; c <= b"(c: length_n_bits)
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
} endwhile
length = (this.bits & (((1 as base.u32) << length_n_bits) - 1)) + 2
this.bits >>= length_n_bits
this.n_bits -= length_n_bits
if length > this.ht_n_symbols {
return "#bad Huffman code"
}
this.ht_code_lengths_remaining = length
}
prev_code_length = 8
while s < this.ht_n_symbols {
assert s < 2328 via "a < b: a < c; c <= b"(c: this.ht_n_symbols)
if this.ht_code_lengths_remaining <= 0 {
while s < this.ht_n_symbols {
assert s < 2328 via "a < b: a < c; c <= b"(c: this.ht_n_symbols)
this.code_lengths[s] = 0
s += 1
} endwhile
break
}
this.ht_code_lengths_remaining -= 1
h = 0
while true,
inv s < 2328,
{
node = this.code_lengths_huffman_nodes[h]
if node >= 0x8000 {
break
} else if node > 35 {
return "#internal error: inconsistent Huffman code"
}
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
h = (node as base.u32) + (this.bits & 1)
this.bits >>= 1
this.n_bits -= 1
} endwhile
symbol = (node & 0x7FFF) as base.u32
if symbol == 0 {
this.code_lengths[s] = 0
s += 1
continue
} else if symbol < 16 {
prev_code_length = symbol as base.u16
this.code_lengths[s] = prev_code_length
s += 1
continue
} else if symbol == 16 {
repeat_value = prev_code_length
} else {
repeat_value = 0
}
repeat_n_bits = REPEAT_N_BITS[symbol & 3] as base.u32
s_max = (REPEAT_COUNTS[symbol & 3] as base.u32) ~mod+ s
if this.n_bits < repeat_n_bits {
c8 = args.src.read_u8?()
if this.n_bits >= repeat_n_bits {
return "#internal error: inconsistent n_bits"
}
assert this.n_bits < 7 via "a < b: a < c; c <= b"(c: repeat_n_bits)
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 8
assert repeat_n_bits <= this.n_bits via "a <= b: a <= c; c <= b"(c: 8)
assert this.n_bits >= repeat_n_bits via "a >= b: b <= a"()
}
s_max ~mod+= this.bits & (((1 as base.u32) << repeat_n_bits) - 1)
this.bits >>= repeat_n_bits
this.n_bits -= repeat_n_bits
if s_max > this.ht_n_symbols {
return "#bad Huffman code"
}
assert s_max <= 2328 via "a <= b: a <= c; c <= b"(c: this.ht_n_symbols)
while s < s_max,
inv s_max <= 2328,
{
assert s < 2328 via "a < b: a < c; c <= b"(c: s_max)
this.code_lengths[s] = repeat_value
s += 1
} endwhile
} endwhile
}
pri const CODE_LENGTH_CODE_ORDER : roarray[19] base.u8[..= 18] = [
17, 18, 0, 1, 2, 3, 4, 5, 16, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
]
pri const REPEAT_N_BITS : roarray[4] base.u8[..= 7] = [2, 3, 7, 0]
pri const REPEAT_COUNTS : roarray[4] base.u8[..= 11] = [3, 3, 11, 0]
// See the decoder.huffman_nodes comment re five (5) Huffman trees.
pri const HUFFMAN_TABLE_BASE_OFFSETS : roarray[5] base.u16[..= 0x064C] = [
0x064C,
0x0000,
0x01FF,
0x03FE,
0x05FD,
]
+308
View File
@@ -0,0 +1,308 @@
// Copyright 2024 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
pri func decoder.decode_pixels_slow?(dst: slice base.u8, src: base.io_reader, width: base.u32[..= 0x4000], height: base.u32[..= 0x4000], tile_data: roslice base.u8, tile_size_log2: base.u32[..= 9]) {
var c8 : base.u8
var p : base.u64
var p_max : base.u64[..= 0x4000_0000]
var tile_size_log2 : base.u32[..= 31]
var width_in_tiles : base.u32[..= 0x20FF]
var x : base.u32
var y : base.u32
var i : base.u32
var hg : base.u32[..= 0xFF]
var h : base.u32[..= 0x187A]
var node : base.u16
var pixel_g : base.u32[..= 0x7FFF]
var color : base.u32 // u32 0xAARR_GGBB, non-premultiplied alpha.
var dst_pixel : slice base.u8
var back_ref_len_n_bits : base.u32[..= 11]
var back_ref_len_minus_1 : base.u32[..= 0x1FFF] // 0x1FFF = 8191.
var back_ref_dist_n_bits : base.u32[..= 18]
var back_ref_dist_sym : base.u32[..= 0x7FFF]
var back_ref_dist_premap_minus_1 : base.u32[..= 0xF_FFFF] // 0xF_FFFF = 1048575.
var back_ref_dist_minus_1 : base.u32
var dm : base.u32[..= 0xFF]
var dx : base.u32
var dy : base.u32
var p_end : base.u64[..= 0x4000_8000]
var dist4 : base.u64
var q : base.u64
var color_cache_pixels : slice base.u8
var color_cache_p : base.u64
var color_cache_shift : base.u32[..= 31]
p_max = (4 * args.width * args.height) as base.u64
if args.dst.length() < p_max {
return "#internal error: inconsistent dst buffer"
}
if args.tile_size_log2 <> 0 {
tile_size_log2 = args.tile_size_log2
width_in_tiles = (args.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2
} else {
tile_size_log2 = 31
width_in_tiles = 1
}
while p < p_max {
// The "~mod+ 1" selects the green pixel of the BGRA 4-byte group.
i = ((((y >> tile_size_log2) ~mod* width_in_tiles) ~mod+ (x >> tile_size_log2)) ~mod* 4) ~mod+ 1
if (i as base.u64) < args.tile_data.length() {
hg = args.tile_data[i as base.u64] as base.u32
}
// Decode the Green+etc symbol.
h = HUFFMAN_TABLE_BASE_OFFSETS[0] as base.u32
while true,
inv p < p_max,
{
node = this.huffman_nodes[hg][h]
if node >= 0x8000 {
break
} else if node > 0x1879 {
return "#internal error: inconsistent Huffman code"
}
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
h = (node as base.u32) + (this.bits & 1)
this.bits >>= 1
this.n_bits -= 1
} endwhile
pixel_g = (node & 0x7FFF) as base.u32
if pixel_g < 0x100 { // Literal pixel.
color = pixel_g << 8
// Decode the Red symbol.
h = HUFFMAN_TABLE_BASE_OFFSETS[1] as base.u32
while true,
inv p < p_max,
{
node = this.huffman_nodes[hg][h]
if node >= 0x8000 {
break
}
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
h = ((node as base.u32) & 0xFFF) + (this.bits & 1)
this.bits >>= 1
this.n_bits -= 1
} endwhile
color |= ((node & 0xFF) as base.u32) << 16
// Decode the Blue symbol.
h = HUFFMAN_TABLE_BASE_OFFSETS[2] as base.u32
while true,
inv p < p_max,
{
node = this.huffman_nodes[hg][h]
if node >= 0x8000 {
break
}
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
h = ((node as base.u32) & 0xFFF) + (this.bits & 1)
this.bits >>= 1
this.n_bits -= 1
} endwhile
color |= ((node & 0xFF) as base.u32) << 0
// Decode the Alpha symbol.
h = HUFFMAN_TABLE_BASE_OFFSETS[3] as base.u32
while true,
inv p < p_max,
{
node = this.huffman_nodes[hg][h]
if node >= 0x8000 {
break
}
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
h = ((node as base.u32) & 0xFFF) + (this.bits & 1)
this.bits >>= 1
this.n_bits -= 1
} endwhile
color |= ((node & 0xFF) as base.u32) << 24
} else if pixel_g < 0x118 { // Back-ref pixel.
// Decode the back-ref length.
if pixel_g < 0x104 {
back_ref_len_minus_1 = pixel_g - 0x100
} else {
back_ref_len_n_bits = (pixel_g - 0x102) >> 1
back_ref_len_minus_1 = ((2 as base.u32) + (pixel_g & 1)) << back_ref_len_n_bits
assert back_ref_len_minus_1 <= 6144
while this.n_bits < back_ref_len_n_bits,
inv p < p_max,
inv back_ref_len_minus_1 <= 6144,
post this.n_bits >= back_ref_len_n_bits,
{
c8 = args.src.read_u8?()
if this.n_bits >= back_ref_len_n_bits {
return "#internal error: inconsistent n_bits"
}
assert this.n_bits < 12 via "a < b: a < c; c <= b"(c: back_ref_len_n_bits)
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
} endwhile
back_ref_len_minus_1 += this.bits & (((1 as base.u32) << back_ref_len_n_bits) - 1)
this.bits >>= back_ref_len_n_bits
this.n_bits -= back_ref_len_n_bits
}
// Decode the back-ref distance.
h = HUFFMAN_TABLE_BASE_OFFSETS[4] as base.u32
while true,
inv p < p_max,
{
node = this.huffman_nodes[hg][h]
if node >= 0x8000 {
break
}
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
h = ((node as base.u32) & 0xFFF) + (this.bits & 1)
this.bits >>= 1
this.n_bits -= 1
} endwhile
back_ref_dist_sym = (node & 0x7FFF) as base.u32
if back_ref_dist_sym < 4 {
back_ref_dist_premap_minus_1 = back_ref_dist_sym
} else if back_ref_dist_sym < 40 {
back_ref_dist_n_bits = (back_ref_dist_sym - 2) >> 1
back_ref_dist_premap_minus_1 = ((2 as base.u32) + (back_ref_dist_sym & 1)) << back_ref_dist_n_bits
assert back_ref_dist_premap_minus_1 <= 786432
while this.n_bits < back_ref_dist_n_bits,
inv p < p_max,
inv back_ref_dist_premap_minus_1 <= 786432,
post this.n_bits >= back_ref_dist_n_bits,
{
c8 = args.src.read_u8?()
if this.n_bits >= back_ref_dist_n_bits {
return "#internal error: inconsistent n_bits"
}
assert this.n_bits < 24 via "a < b: a < c; c <= b"(c: back_ref_dist_n_bits)
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
} endwhile
back_ref_dist_premap_minus_1 += this.bits & (((1 as base.u32) << back_ref_dist_n_bits) - 1)
this.bits >>= back_ref_dist_n_bits
this.n_bits -= back_ref_dist_n_bits
}
if back_ref_dist_premap_minus_1 >= 120 {
back_ref_dist_minus_1 = back_ref_dist_premap_minus_1 - 120
} else {
dm = DISTANCE_MAP[back_ref_dist_premap_minus_1] as base.u32
dy = dm >> 4
dx = 7 ~mod- (dm & 15)
back_ref_dist_minus_1 = (args.width * dy) ~mod+ dx
}
// Apply the (back_ref_len_minus_1, back_ref_dist_minus_1) pair.
assert p < 0x4000_0000 via "a < b: a < c; c <= b"(c: p_max)
p_end = p + (((back_ref_len_minus_1 + 1) * 4) as base.u64)
dist4 = ((back_ref_dist_minus_1 as base.u64) * 4) + 4
if (p_end > p_max) or (p_end > args.dst.length()) or (p < dist4) {
return "#bad back-reference"
}
q = p - dist4
while (q < p) and (p < p_end),
inv p_end <= args.dst.length(),
{
assert q < p_end via "a < b: a < c; c < b"(c: p)
assert p < 0x4000_8000 via "a < b: a < c; c <= b"(c: p_end)
assert q < 0x4000_8000 via "a < b: a < c; c <= b"(c: p_end)
assert p < args.dst.length() via "a < b: a < c; c <= b"(c: p_end)
assert q < args.dst.length() via "a < b: a < c; c <= b"(c: p_end)
args.dst[p] = args.dst[q]
p += 1
q += 1
} endwhile
// Update (x, y).
x ~mod+= back_ref_len_minus_1 + 1
while x >= args.width {
x -= args.width
y ~mod+= 1
} endwhile
continue
} else { // Color cache pixel.
// Insert previous pixels into this.color_cache.
if (color_cache_p > p) or (p > args.dst.length()) {
return "#internal error: inconsistent dst buffer"
}
color_cache_pixels = args.dst[color_cache_p .. p]
color_cache_p = p
color_cache_shift = (32 - this.color_cache_bits) & 31
while color_cache_pixels.length() >= 4,
inv pixel_g >= 0x118,
{
color = color_cache_pixels.peek_u32le()
this.color_cache[((color ~mod* 0x1E35_A7BD) >> color_cache_shift) & 2047] = color
color_cache_pixels = color_cache_pixels[4 ..]
} endwhile
// Look up this.color_cache.
color = this.color_cache[(pixel_g - 0x118) & 2047]
}
// Set the dst pixel to the color.
if p > args.dst.length() {
return "#internal error: inconsistent dst buffer"
}
dst_pixel = args.dst[p ..]
if dst_pixel.length() < 4 {
return "#internal error: inconsistent dst buffer"
}
dst_pixel.poke_u32le!(a: color)
p ~mod+= 4
// Update (x, y).
x ~mod+= 1
if x == args.width {
x = 0
y ~mod+= 1
}
} endwhile
}
+464
View File
@@ -0,0 +1,464 @@
// Copyright 2024 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
pri func decoder.apply_transform_predictor!(pix: slice base.u8, tile_data: roslice base.u8) {
var w4 : base.u64[..= 0x1_0000]
var prev_row : roslice base.u8
var curr_row : slice base.u8
var tile_size_log2 : base.u32[..= 9]
var tiles_per_row : base.u32[..= 16895]
var mask : base.u32
var y : base.u32[..= 0x4000]
var x : base.u32[..= 0x4000]
var t : base.u64
var tile_data : roslice base.u8
var mode : base.u8[..= 0x0F]
var l0 : base.u32[..= 0xFF]
var l1 : base.u32[..= 0xFF]
var l2 : base.u32[..= 0xFF]
var l3 : base.u32[..= 0xFF]
var c0 : base.u32[..= 0xFF]
var c1 : base.u32[..= 0xFF]
var c2 : base.u32[..= 0xFF]
var c3 : base.u32[..= 0xFF]
var t0 : base.u32[..= 0xFF]
var t1 : base.u32[..= 0xFF]
var t2 : base.u32[..= 0xFF]
var t3 : base.u32[..= 0xFF]
var sum_l : base.u32
var sum_t : base.u32
if (this.width <= 0) or (this.height <= 0) {
return nothing
}
w4 = (this.width * 4) as base.u64
curr_row = this.util.empty_slice_u8()
if w4 <= args.pix.length() {
curr_row = args.pix[.. w4]
}
// The first pixel's predictor is mode 0 (opaque black).
if curr_row.length() >= 4 {
curr_row[3] ~mod+= 0xFF
}
// The rest of the first row's predictor is mode 1 (L).
while curr_row.length() >= 8 {
curr_row[4] ~mod+= curr_row[0]
curr_row[5] ~mod+= curr_row[1]
curr_row[6] ~mod+= curr_row[2]
curr_row[7] ~mod+= curr_row[3]
curr_row = curr_row[4 ..]
} endwhile
tile_size_log2 = this.transform_tile_size_log2[0] as base.u32
tiles_per_row = (this.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2
mask = ((1 as base.u32) << tile_size_log2) - 1
y = 1
while y < this.height {
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
t = (4 * (y >> tile_size_log2) * tiles_per_row) as base.u64
tile_data = this.util.empty_slice_u8()
if t <= args.tile_data.length() {
tile_data = args.tile_data[t ..]
if tile_data.length() >= 4 {
mode = tile_data[1] & 0x0F
tile_data = tile_data[4 ..]
}
}
if w4 <= args.pix.length() {
prev_row = args.pix
args.pix = args.pix[w4 ..]
curr_row = args.pix
}
// The first column's predictor is mode 2 (T).
if (prev_row.length() >= 4) and (curr_row.length() >= 4) {
curr_row[0] ~mod+= prev_row[0]
curr_row[1] ~mod+= prev_row[1]
curr_row[2] ~mod+= prev_row[2]
curr_row[3] ~mod+= prev_row[3]
}
x = 1
while x < this.width,
inv y < 0x4000,
{
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
if ((x & mask) == 0) and (tile_data.length() >= 4) {
mode = tile_data[1] & 0x0F
tile_data = tile_data[4 ..]
}
if (prev_row.length() < 12) or (curr_row.length() < 8) {
break
}
if mode == 0 { // Opaque black.
curr_row[7] ~mod+= 0xFF
} else if mode == 1 { // L
curr_row[4] ~mod+= curr_row[0]
curr_row[5] ~mod+= curr_row[1]
curr_row[6] ~mod+= curr_row[2]
curr_row[7] ~mod+= curr_row[3]
} else if mode == 2 { // T
curr_row[4] ~mod+= prev_row[4]
curr_row[5] ~mod+= prev_row[5]
curr_row[6] ~mod+= prev_row[6]
curr_row[7] ~mod+= prev_row[7]
} else if mode == 3 { // TR
curr_row[4] ~mod+= prev_row[8]
curr_row[5] ~mod+= prev_row[9]
curr_row[6] ~mod+= prev_row[10]
curr_row[7] ~mod+= prev_row[11]
} else if mode == 4 { // TL
curr_row[4] ~mod+= prev_row[0]
curr_row[5] ~mod+= prev_row[1]
curr_row[6] ~mod+= prev_row[2]
curr_row[7] ~mod+= prev_row[3]
} else if mode == 5 { // Average2(Average2(L, TR), T).
l0 = ((curr_row[0] as base.u32) + (prev_row[8] as base.u32)) / 2
l1 = ((curr_row[1] as base.u32) + (prev_row[9] as base.u32)) / 2
l2 = ((curr_row[2] as base.u32) + (prev_row[10] as base.u32)) / 2
l3 = ((curr_row[3] as base.u32) + (prev_row[11] as base.u32)) / 2
curr_row[4] ~mod+= ((l0 + (prev_row[4] as base.u32)) / 2) as base.u8
curr_row[5] ~mod+= ((l1 + (prev_row[5] as base.u32)) / 2) as base.u8
curr_row[6] ~mod+= ((l2 + (prev_row[6] as base.u32)) / 2) as base.u8
curr_row[7] ~mod+= ((l3 + (prev_row[7] as base.u32)) / 2) as base.u8
} else if mode == 6 { // Average2(L, TL).
curr_row[4] ~mod+= (((curr_row[0] as base.u32) + (prev_row[0] as base.u32)) / 2) as base.u8
curr_row[5] ~mod+= (((curr_row[1] as base.u32) + (prev_row[1] as base.u32)) / 2) as base.u8
curr_row[6] ~mod+= (((curr_row[2] as base.u32) + (prev_row[2] as base.u32)) / 2) as base.u8
curr_row[7] ~mod+= (((curr_row[3] as base.u32) + (prev_row[3] as base.u32)) / 2) as base.u8
} else if mode == 7 { // Average2(L, T).
curr_row[4] ~mod+= (((curr_row[0] as base.u32) + (prev_row[4] as base.u32)) / 2) as base.u8
curr_row[5] ~mod+= (((curr_row[1] as base.u32) + (prev_row[5] as base.u32)) / 2) as base.u8
curr_row[6] ~mod+= (((curr_row[2] as base.u32) + (prev_row[6] as base.u32)) / 2) as base.u8
curr_row[7] ~mod+= (((curr_row[3] as base.u32) + (prev_row[7] as base.u32)) / 2) as base.u8
} else if mode == 8 { // Average2(TL, T).
curr_row[4] ~mod+= (((prev_row[0] as base.u32) + (prev_row[4] as base.u32)) / 2) as base.u8
curr_row[5] ~mod+= (((prev_row[1] as base.u32) + (prev_row[5] as base.u32)) / 2) as base.u8
curr_row[6] ~mod+= (((prev_row[2] as base.u32) + (prev_row[6] as base.u32)) / 2) as base.u8
curr_row[7] ~mod+= (((prev_row[3] as base.u32) + (prev_row[7] as base.u32)) / 2) as base.u8
} else if mode == 9 { // Average2(T, TR).
curr_row[4] ~mod+= (((prev_row[4] as base.u32) + (prev_row[8] as base.u32)) / 2) as base.u8
curr_row[5] ~mod+= (((prev_row[5] as base.u32) + (prev_row[9] as base.u32)) / 2) as base.u8
curr_row[6] ~mod+= (((prev_row[6] as base.u32) + (prev_row[10] as base.u32)) / 2) as base.u8
curr_row[7] ~mod+= (((prev_row[7] as base.u32) + (prev_row[11] as base.u32)) / 2) as base.u8
} else if mode == 10 { // Average2(Average2(L, TL), Average2(T, TR)).
l0 = ((curr_row[0] as base.u32) + (prev_row[0] as base.u32)) / 2
l1 = ((curr_row[1] as base.u32) + (prev_row[1] as base.u32)) / 2
l2 = ((curr_row[2] as base.u32) + (prev_row[2] as base.u32)) / 2
l3 = ((curr_row[3] as base.u32) + (prev_row[3] as base.u32)) / 2
t0 = ((prev_row[4] as base.u32) + (prev_row[8] as base.u32)) / 2
t1 = ((prev_row[5] as base.u32) + (prev_row[9] as base.u32)) / 2
t2 = ((prev_row[6] as base.u32) + (prev_row[10] as base.u32)) / 2
t3 = ((prev_row[7] as base.u32) + (prev_row[11] as base.u32)) / 2
curr_row[4] ~mod+= ((l0 + t0) / 2) as base.u8
curr_row[5] ~mod+= ((l1 + t1) / 2) as base.u8
curr_row[6] ~mod+= ((l2 + t2) / 2) as base.u8
curr_row[7] ~mod+= ((l3 + t3) / 2) as base.u8
} else if mode == 11 { // Select(L, T, TL).
l0 = curr_row[0] as base.u32
l1 = curr_row[1] as base.u32
l2 = curr_row[2] as base.u32
l3 = curr_row[3] as base.u32
c0 = prev_row[0] as base.u32
c1 = prev_row[1] as base.u32
c2 = prev_row[2] as base.u32
c3 = prev_row[3] as base.u32
t0 = prev_row[4] as base.u32
t1 = prev_row[5] as base.u32
t2 = prev_row[6] as base.u32
t3 = prev_row[7] as base.u32
sum_l = this.absolute_difference(a: c0, b: t0) +
this.absolute_difference(a: c1, b: t1) +
this.absolute_difference(a: c2, b: t2) +
this.absolute_difference(a: c3, b: t3)
sum_t = this.absolute_difference(a: c0, b: l0) +
this.absolute_difference(a: c1, b: l1) +
this.absolute_difference(a: c2, b: l2) +
this.absolute_difference(a: c3, b: l3)
if sum_l < sum_t {
curr_row[4] ~mod+= l0 as base.u8
curr_row[5] ~mod+= l1 as base.u8
curr_row[6] ~mod+= l2 as base.u8
curr_row[7] ~mod+= l3 as base.u8
} else {
curr_row[4] ~mod+= t0 as base.u8
curr_row[5] ~mod+= t1 as base.u8
curr_row[6] ~mod+= t2 as base.u8
curr_row[7] ~mod+= t3 as base.u8
}
} else if mode == 12 { // ClampAddSubtractFull(L, T, TL).
curr_row[4] ~mod+= this.mode12(l: curr_row[0], t: prev_row[4], tl: prev_row[0])
curr_row[5] ~mod+= this.mode12(l: curr_row[1], t: prev_row[5], tl: prev_row[1])
curr_row[6] ~mod+= this.mode12(l: curr_row[2], t: prev_row[6], tl: prev_row[2])
curr_row[7] ~mod+= this.mode12(l: curr_row[3], t: prev_row[7], tl: prev_row[3])
} else if mode == 13 { // ClampAddSubtractHalf(Average2(L, T), TL).
curr_row[4] ~mod+= this.mode13(l: curr_row[0], t: prev_row[4], tl: prev_row[0])
curr_row[5] ~mod+= this.mode13(l: curr_row[1], t: prev_row[5], tl: prev_row[1])
curr_row[6] ~mod+= this.mode13(l: curr_row[2], t: prev_row[6], tl: prev_row[2])
curr_row[7] ~mod+= this.mode13(l: curr_row[3], t: prev_row[7], tl: prev_row[3])
}
curr_row = curr_row[4 ..]
prev_row = prev_row[4 ..]
x += 1
} endwhile
y += 1
} endwhile
}
pri func decoder.absolute_difference(a: base.u32[..= 0xFF], b: base.u32[..= 0xFF]) base.u32[..= 0xFF] {
if args.a < args.b {
assert args.b > args.a via "a > b: b < a"()
return args.b - args.a
}
return args.a - args.b
}
pri func decoder.mode12(l: base.u8, t: base.u8, tl: base.u8) base.u8 {
var v : base.u32
v = ((args.l as base.u32) + (args.t as base.u32)) ~mod- (args.tl as base.u32)
// Return clamp(v), where v is a signed i32.
if v < 256 {
return v as base.u8
} else if v < 512 {
return 255
}
return 0
}
pri func decoder.mode13(l: base.u8, t: base.u8, tl: base.u8) base.u8 {
var x : base.u32[..= 0xFF]
var y : base.u32[..= 0xFF]
var z : base.u32
var v : base.u32
x = ((args.l as base.u32) + (args.t as base.u32)) / 2
y = args.tl as base.u32
// Calculate "v = x + ((x - y) / 2)" but using signed (i32) arithmetic
// instead of unsigned (u32) arithmetic.
z = x ~mod- y
v = x ~mod+ this.util.sign_extend_rshift_u32(a: z ~mod+ (z >> 31), n: 1)
// Return clamp(v), where v is a signed i32.
if v < 256 {
return v as base.u8
} else if v < 512 {
return 255
}
return 0
}
pri func decoder.apply_transform_cross_color!(pix: slice base.u8, tile_data: roslice base.u8) {
var tile_size_log2 : base.u32[..= 9]
var tiles_per_row : base.u32[..= 16895]
var mask : base.u32
var y : base.u32[..= 0x4000]
var x : base.u32[..= 0x4000]
var t : base.u64
var tile_data : roslice base.u8
var g2r : base.u32
var g2b : base.u32
var r2b : base.u32
var b : base.u8
var g : base.u8
var r : base.u8
tile_size_log2 = this.transform_tile_size_log2[1] as base.u32
tiles_per_row = (this.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2
mask = ((1 as base.u32) << tile_size_log2) - 1
y = 0
while y < this.height {
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
t = (4 * (y >> tile_size_log2) * tiles_per_row) as base.u64
tile_data = this.util.empty_slice_u8()
if t <= args.tile_data.length() {
tile_data = args.tile_data[t ..]
}
x = 0
while x < this.width,
inv y < 0x4000,
{
assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width)
if ((x & mask) == 0) and (tile_data.length() >= 4) {
g2r = SIGN_EXTEND_I8_I32[tile_data[0]]
g2b = SIGN_EXTEND_I8_I32[tile_data[1]]
r2b = SIGN_EXTEND_I8_I32[tile_data[2]]
tile_data = tile_data[4 ..]
}
if args.pix.length() >= 4 {
b = args.pix[0]
g = args.pix[1]
r = args.pix[2]
r ~mod+= (((SIGN_EXTEND_I8_I32[g] ~mod* g2r) >> 5) & 0xFF) as base.u8
b ~mod+= (((SIGN_EXTEND_I8_I32[g] ~mod* g2b) >> 5) & 0xFF) as base.u8
b ~mod+= (((SIGN_EXTEND_I8_I32[r] ~mod* r2b) >> 5) & 0xFF) as base.u8
args.pix[0] = b
args.pix[2] = r
args.pix = args.pix[4 ..]
}
x += 1
} endwhile
y += 1
} endwhile
}
pri func decoder.apply_transform_subtract_green!(pix: slice base.u8) {
var p : slice base.u8
var g : base.u8
iterate (p = args.pix)(length: 4, advance: 4, unroll: 1) {
g = p[1]
p[0] ~mod+= g
p[2] ~mod+= g
}
}
pri func decoder.apply_transform_color_indexing!(pix: slice base.u8) {
var tile_size_log2 : base.u32[..= 9]
var bits_per_pixel : base.u32[..= 8]
var x_mask : base.u32
var s_mask : base.u32[..= 0xFF]
var src_index : base.u64
var y : base.u32[..= 0x4000]
var di : base.u64
var dj : base.u64
var dst : slice base.u8
var x : base.u32
var s : base.u32[..= 0xFF]
var p : base.u32[..= 0x3FC]
var p0 : base.u8
var p1 : base.u8
var p2 : base.u8
var p3 : base.u8
tile_size_log2 = this.transform_tile_size_log2[3] as base.u32
if tile_size_log2 == 0 {
// TODO: the code below is correct, but we could possibly simplify the
// case where the src and dst pixel counts are 1:1.
}
bits_per_pixel = (8 as base.u32) >> tile_size_log2
x_mask = ((1 as base.u32) << tile_size_log2) - 1
s_mask = ((1 as base.u32) << bits_per_pixel) - 1
// The "+ 1" selects the green pixel of the BGRA 4-byte group.
src_index = (this.workbuf_offset_for_color_indexing + 1) as base.u64
y = 0
while y < this.height {
assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height)
di = (4 * (y + 0) * this.width) as base.u64
dj = (4 * (y + 1) * this.width) as base.u64
if (di > dj) or (dj > args.pix.length()) {
break
}
dst = args.pix[di .. dj]
x = 0
while dst.length() >= 4,
inv y < 0x4000,
{
if ((x & x_mask) == 0) and (src_index < args.pix.length()) {
s = args.pix[src_index] as base.u32
src_index ~mod+= 4
}
p = (s & s_mask) * 4
s >>= bits_per_pixel
p0 = this.palette[p + 0]
p1 = this.palette[p + 1]
p2 = this.palette[p + 2]
p3 = this.palette[p + 3]
dst[0] = p0
dst[1] = p1
dst[2] = p2
dst[3] = p3
dst = dst[4 ..]
x ~mod+= 1
} endwhile
y += 1
} endwhile
}
// TODO: we shouldn't need a lookup table for converting i8 to i32.
pri const SIGN_EXTEND_I8_I32 : roarray[256] base.u32 = [
0x0000_0000, 0x0000_0001, 0x0000_0002, 0x0000_0003, 0x0000_0004, 0x0000_0005, 0x0000_0006, 0x0000_0007,
0x0000_0008, 0x0000_0009, 0x0000_000A, 0x0000_000B, 0x0000_000C, 0x0000_000D, 0x0000_000E, 0x0000_000F,
0x0000_0010, 0x0000_0011, 0x0000_0012, 0x0000_0013, 0x0000_0014, 0x0000_0015, 0x0000_0016, 0x0000_0017,
0x0000_0018, 0x0000_0019, 0x0000_001A, 0x0000_001B, 0x0000_001C, 0x0000_001D, 0x0000_001E, 0x0000_001F,
0x0000_0020, 0x0000_0021, 0x0000_0022, 0x0000_0023, 0x0000_0024, 0x0000_0025, 0x0000_0026, 0x0000_0027,
0x0000_0028, 0x0000_0029, 0x0000_002A, 0x0000_002B, 0x0000_002C, 0x0000_002D, 0x0000_002E, 0x0000_002F,
0x0000_0030, 0x0000_0031, 0x0000_0032, 0x0000_0033, 0x0000_0034, 0x0000_0035, 0x0000_0036, 0x0000_0037,
0x0000_0038, 0x0000_0039, 0x0000_003A, 0x0000_003B, 0x0000_003C, 0x0000_003D, 0x0000_003E, 0x0000_003F,
0x0000_0040, 0x0000_0041, 0x0000_0042, 0x0000_0043, 0x0000_0044, 0x0000_0045, 0x0000_0046, 0x0000_0047,
0x0000_0048, 0x0000_0049, 0x0000_004A, 0x0000_004B, 0x0000_004C, 0x0000_004D, 0x0000_004E, 0x0000_004F,
0x0000_0050, 0x0000_0051, 0x0000_0052, 0x0000_0053, 0x0000_0054, 0x0000_0055, 0x0000_0056, 0x0000_0057,
0x0000_0058, 0x0000_0059, 0x0000_005A, 0x0000_005B, 0x0000_005C, 0x0000_005D, 0x0000_005E, 0x0000_005F,
0x0000_0060, 0x0000_0061, 0x0000_0062, 0x0000_0063, 0x0000_0064, 0x0000_0065, 0x0000_0066, 0x0000_0067,
0x0000_0068, 0x0000_0069, 0x0000_006A, 0x0000_006B, 0x0000_006C, 0x0000_006D, 0x0000_006E, 0x0000_006F,
0x0000_0070, 0x0000_0071, 0x0000_0072, 0x0000_0073, 0x0000_0074, 0x0000_0075, 0x0000_0076, 0x0000_0077,
0x0000_0078, 0x0000_0079, 0x0000_007A, 0x0000_007B, 0x0000_007C, 0x0000_007D, 0x0000_007E, 0x0000_007F,
0xFFFF_FF80, 0xFFFF_FF81, 0xFFFF_FF82, 0xFFFF_FF83, 0xFFFF_FF84, 0xFFFF_FF85, 0xFFFF_FF86, 0xFFFF_FF87,
0xFFFF_FF88, 0xFFFF_FF89, 0xFFFF_FF8A, 0xFFFF_FF8B, 0xFFFF_FF8C, 0xFFFF_FF8D, 0xFFFF_FF8E, 0xFFFF_FF8F,
0xFFFF_FF90, 0xFFFF_FF91, 0xFFFF_FF92, 0xFFFF_FF93, 0xFFFF_FF94, 0xFFFF_FF95, 0xFFFF_FF96, 0xFFFF_FF97,
0xFFFF_FF98, 0xFFFF_FF99, 0xFFFF_FF9A, 0xFFFF_FF9B, 0xFFFF_FF9C, 0xFFFF_FF9D, 0xFFFF_FF9E, 0xFFFF_FF9F,
0xFFFF_FFA0, 0xFFFF_FFA1, 0xFFFF_FFA2, 0xFFFF_FFA3, 0xFFFF_FFA4, 0xFFFF_FFA5, 0xFFFF_FFA6, 0xFFFF_FFA7,
0xFFFF_FFA8, 0xFFFF_FFA9, 0xFFFF_FFAA, 0xFFFF_FFAB, 0xFFFF_FFAC, 0xFFFF_FFAD, 0xFFFF_FFAE, 0xFFFF_FFAF,
0xFFFF_FFB0, 0xFFFF_FFB1, 0xFFFF_FFB2, 0xFFFF_FFB3, 0xFFFF_FFB4, 0xFFFF_FFB5, 0xFFFF_FFB6, 0xFFFF_FFB7,
0xFFFF_FFB8, 0xFFFF_FFB9, 0xFFFF_FFBA, 0xFFFF_FFBB, 0xFFFF_FFBC, 0xFFFF_FFBD, 0xFFFF_FFBE, 0xFFFF_FFBF,
0xFFFF_FFC0, 0xFFFF_FFC1, 0xFFFF_FFC2, 0xFFFF_FFC3, 0xFFFF_FFC4, 0xFFFF_FFC5, 0xFFFF_FFC6, 0xFFFF_FFC7,
0xFFFF_FFC8, 0xFFFF_FFC9, 0xFFFF_FFCA, 0xFFFF_FFCB, 0xFFFF_FFCC, 0xFFFF_FFCD, 0xFFFF_FFCE, 0xFFFF_FFCF,
0xFFFF_FFD0, 0xFFFF_FFD1, 0xFFFF_FFD2, 0xFFFF_FFD3, 0xFFFF_FFD4, 0xFFFF_FFD5, 0xFFFF_FFD6, 0xFFFF_FFD7,
0xFFFF_FFD8, 0xFFFF_FFD9, 0xFFFF_FFDA, 0xFFFF_FFDB, 0xFFFF_FFDC, 0xFFFF_FFDD, 0xFFFF_FFDE, 0xFFFF_FFDF,
0xFFFF_FFE0, 0xFFFF_FFE1, 0xFFFF_FFE2, 0xFFFF_FFE3, 0xFFFF_FFE4, 0xFFFF_FFE5, 0xFFFF_FFE6, 0xFFFF_FFE7,
0xFFFF_FFE8, 0xFFFF_FFE9, 0xFFFF_FFEA, 0xFFFF_FFEB, 0xFFFF_FFEC, 0xFFFF_FFED, 0xFFFF_FFEE, 0xFFFF_FFEF,
0xFFFF_FFF0, 0xFFFF_FFF1, 0xFFFF_FFF2, 0xFFFF_FFF3, 0xFFFF_FFF4, 0xFFFF_FFF5, 0xFFFF_FFF6, 0xFFFF_FFF7,
0xFFFF_FFF8, 0xFFFF_FFF9, 0xFFFF_FFFA, 0xFFFF_FFFB, 0xFFFF_FFFC, 0xFFFF_FFFD, 0xFFFF_FFFE, 0xFFFF_FFFF,
]
+679 -6
View File
@@ -10,23 +10,131 @@
use "std/vp8"
pub status "#bad Huffman code (over-subscribed)"
pub status "#bad Huffman code (under-subscribed)"
pub status "#bad Huffman code"
pub status "#bad back-reference"
pub status "#bad color cache"
pub status "#bad header"
pub status "#bad transform"
pub status "#short chunk"
pub status "#truncated input"
pub status "#unsupported number of Huffman groups"
pub status "#unsupported transform after color indexing transform"
pub status "#unsupported WebP file"
pri status "#internal error: inconsistent Huffman code"
pri status "#internal error: inconsistent dst buffer"
pri status "#internal error: inconsistent n_bits"
pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0
pub struct decoder? implements base.image_decoder(
pixfmt : base.u32,
width : base.u32[..= 0xFF_FFFF],
height : base.u32[..= 0xFF_FFFF],
width : base.u32[..= 0x4000],
height : base.u32[..= 0x4000],
// The call sequence state machine is discussed in
// (/doc/std/image-decoders-call-sequence.md).
call_sequence : base.u8,
code_length_code_lengths : array[19] base.u8[..= 7],
sub_chunk_has_padding : base.bool,
frame_config_io_position : base.u64,
riff_chunk_length : base.u32,
sub_chunk_length : base.u32,
bits : base.u32,
n_bits : base.u32[..= 31],
seen_transform : array[4] base.bool,
transform_type : array[4] base.u8[..= 3],
transform_tile_size_log2 : array[4] base.u8[..= 9],
n_transforms : base.u32[..= 4],
color_cache_bits : base.u32[..= 11],
overall_color_cache_bits : base.u32[..= 11],
overall_tile_size_log2 : base.u32[..= 9],
overall_n_huffman_groups : base.u32[..= 256],
ht_n_symbols : base.u32[..= 2328],
ht_code_lengths_remaining : base.u32,
color_indexing_palette_size : base.u32[..= 256],
color_indexing_width : base.u32[..= 0x4000],
// The 0th element is for the Meta (Huffman Group) selectors.
// The 1st element is for the Predictor transform.
// The 2nd element is for the Cross Color transform.
// The 3rd element is overall workbuf length.
workbuf_offset_for_transform : array[4] base.u32[..= 0x4C00_0000],
// If the Color Indexing transform is present then workbuf[i .. j]
// holds the pre-transformed pixel data (the Green component of the
// pixel's bgra data holds color indexes) and workbuf[0 .. j] holds the
// post-transformed data:
//
// - i is workbuf_offset_for_color_indexing
// - j is workbuf_offset_for_transform[0]
//
// i is calculated as:
//
// - if there are 8 bits per color index then i is 0.
// - if there are 4 bits per color index then i is j*1/2, roughly.
// - if there are 2 bits per color index then i is j*3/4, roughly.
// - if there are 1 bits per color index then i is j*7/8, roughly.
//
// The two slices (workbuf[i .. j] and workbuf[0 .. j]) overlap.
// apply_transform_color_indexing modifies the pixel buffer in-place.
workbuf_offset_for_color_indexing : base.u32[..= 0x4000_0000],
swizzler : base.pixel_swizzler,
util : base.utility,
) + (
palette : array[4 * 256] base.u8,
color_cache : array[2048] base.u32,
codes : array[2328] base.u16,
code_lengths : array[2328] base.u16,
// Each node is a base.u16 whose bit patterns are:
//
// - 0000_0000_0000_0000 Invalid.
// - 0CCC_CCCC_CCCC_CCCC Branch node (children_offset is CCC).
// - 1SSS_SSSS_SSSS_SSSS Leaf node (symbol is SSS, it may be zero).
code_lengths_huffman_nodes : array[37] base.u16,
// A Huffman group has five (5) Huffman trees.
//
// Start .. End Size
// 0x064C .. 0x187B 0x122F (1) Green, back-ref length, color cache.
// 0x0000 .. 0x01FF 0x01FF (2) Red.
// 0x01FF .. 0x03FE 0x01FF (3) Blue.
// 0x03FE .. 0x05FD 0x01FF (4) Alpha.
// 0x05FD .. 0x064C 0x004F (5) Back-ref distance.
//
// The Green+etc tree is last (in terms of start offset) because it's
// by far the largest (worst case) and its color cache component has
// variable length.
//
// 0x122F = 4655 = ((2 * 2328) - 1) and the same 2328 turns up in func
// decoder.decode_huffman_tree.
//
// 2328 is (256 + 24 + 2048), combining 256 Green values, 24 back-ref
// lengths and up to 2048 = (1 << 11) color cache keys.
//
// 0x01FF = 511 = ((2 * 256) - 1) is for 256 Red (Blue, Alpha) values.
//
// 0x004F = 79 = ((2 * 40) - 1) is for 40 back-ref distances.
//
// 0x187B = 6267 and (0x187B * sizeof(u16)) = 12534. Overall, this
// field takes 0x30_F600 = 3_208704 bytes of memory.
//
// The base.u16's bits are the same as for code_lengths_huffman_nodes.
huffman_nodes : array[256] array[0x187B] base.u16,
)
pub func decoder.get_quirk(key: base.u32) base.u64 {
@@ -50,13 +158,42 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
}
pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
var c32 : base.u32
var r_mark : base.u64
var status : base.status
if this.call_sequence <> 0x00 {
return base."#bad call sequence"
}
this.pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
this.width = 160
this.height = 120
c32 = args.src.read_u32le?()
if c32 <> 'RIFF'le {
return "#bad header"
}
this.riff_chunk_length = args.src.read_u32le?()
if (this.riff_chunk_length & 1) <> 0 {
return "#bad header"
}
while true {
io_limit (io: args.src, limit: this.riff_chunk_length as base.u64) {
r_mark = args.src.mark()
status =? this.do_decode_image_config_limited?(src: args.src)
this.riff_chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
if status.is_ok() {
break
} else if not status.is_suspension() {
return status
} else if (status == base."$short read") and (this.riff_chunk_length == 0) {
return "#short chunk"
}
yield? status
} endwhile
this.frame_config_io_position = args.src.position()
if args.dst <> nullptr {
args.dst.set!(
@@ -71,6 +208,82 @@ pri func decoder.do_decode_image_config?(dst: nptr base.image_config, src: base.
this.call_sequence = 0x20
}
pri func decoder.do_decode_image_config_limited?(src: base.io_reader) {
var c32 : base.u32
var r_mark : base.u64
var status : base.status
c32 = args.src.read_u32le?()
if c32 <> 'WEBP'le {
return "#bad header"
}
c32 = args.src.read_u32le?()
if c32 == 'VP8 'le {
return "#unsupported WebP file"
} else if c32 == 'VP8L'le {
this.sub_chunk_length = args.src.read_u32le?()
if this.sub_chunk_length < 4 {
return "#bad header"
}
this.sub_chunk_has_padding = (this.sub_chunk_length & 1) <> 0
while true {
io_limit (io: args.src, limit: this.sub_chunk_length as base.u64) {
r_mark = args.src.mark()
status =? this.do_decode_image_config_limited_vp8l?(src: args.src)
this.sub_chunk_length ~sat-=
(args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32
}
if status.is_ok() {
break
} else if not status.is_suspension() {
return status
} else if (status == base."$short read") and (this.sub_chunk_length == 0) {
return "#short chunk"
}
yield? status
} endwhile
} else if c32 == 'VP8X'le {
return "#unsupported WebP file"
} else {
return "#bad header"
}
}
pri func decoder.do_decode_image_config_limited_vp8l?(src: base.io_reader) {
var c32 : base.u32
var transform_size : base.u32[..= 0x400_0000]
c32 = args.src.read_u8_as_u32?()
if c32 <> 0x2F {
return "#bad header"
}
c32 = args.src.read_u32le?()
this.width = (c32 & 0x3FFF) + 1
c32 >>= 14
this.height = (c32 & 0x3FFF) + 1
c32 >>= 14
this.pixfmt = base.PIXEL_FORMAT__BGRX
if (c32 & 1) <> 0 {
this.pixfmt = base.PIXEL_FORMAT__BGRA_NONPREMUL
}
c32 >>= 1
if c32 <> 0 {
return "#bad header"
}
transform_size = 4 * ((this.width + 3) >> 2) * ((this.height + 3) >> 2)
this.workbuf_offset_for_transform[0] = (4 * this.width * this.height) + (0 * transform_size)
this.workbuf_offset_for_transform[1] = (4 * this.width * this.height) + (1 * transform_size)
this.workbuf_offset_for_transform[2] = (4 * this.width * this.height) + (2 * transform_size)
this.workbuf_offset_for_transform[3] = (4 * this.width * this.height) + (3 * transform_size)
}
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
var status : base.status
@@ -130,9 +343,452 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
}
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 c8 : base.u8
var has_more : base.u32[..= 1]
var width : base.u32[..= 0x4000]
var dst : slice base.u8
var tile_data : roslice base.u8
var status : base.status
var pix : slice base.u8
var which : base.u32[..= 4]
var transform_type : base.u32[..= 3]
var ti : base.u64
var tj : base.u64
if this.call_sequence == 0x40 {
// No-op.
} else if this.call_sequence < 0x40 {
this.do_decode_frame_config?(dst: nullptr, src: args.src)
} else {
return base."@end of data"
}
this.seen_transform[0] = false
this.seen_transform[1] = false
this.seen_transform[2] = false
this.seen_transform[3] = false
this.n_transforms = 0
while true {
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
has_more = this.bits & 1
this.bits >>= 1
this.n_bits -= 1
if has_more == 0 {
break
}
this.decode_transform?(src: args.src, workbuf: args.workbuf)
} endwhile
width = this.width
if this.seen_transform[3] {
width = this.color_indexing_width
}
this.decode_color_cache_parameters?(src: args.src)
this.overall_color_cache_bits = this.color_cache_bits
this.decode_hg_table?(src: args.src, width: width, workbuf: args.workbuf)
this.color_cache_bits = this.overall_color_cache_bits
this.decode_huffman_groups?(src: args.src, n_huffman_groups: this.overall_n_huffman_groups)
while true {
if ((this.workbuf_offset_for_color_indexing as base.u64) > (this.workbuf_offset_for_transform[0] as base.u64)) or
((this.workbuf_offset_for_transform[0] as base.u64) > (this.workbuf_offset_for_transform[1] as base.u64)) or
((this.workbuf_offset_for_transform[1] as base.u64) > args.workbuf.length()) or
((this.workbuf_offset_for_transform[0] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
dst = args.workbuf[
(this.workbuf_offset_for_color_indexing as base.u64) ..
(this.workbuf_offset_for_transform[0] as base.u64)]
tile_data = args.workbuf[
(this.workbuf_offset_for_transform[0] as base.u64) ..
(this.workbuf_offset_for_transform[1] as base.u64)]
status =? this.decode_pixels?(
dst: dst,
src: args.src,
width: width,
height: this.height,
tile_data: tile_data,
tile_size_log2: this.overall_tile_size_log2)
if status.is_ok() {
break
}
yield? status
} endwhile
if (this.workbuf_offset_for_transform[0] as base.u64) > args.workbuf.length() {
return base."#bad workbuf length"
}
pix = args.workbuf[.. (this.workbuf_offset_for_transform[0] as base.u64)]
which = this.n_transforms
while which > 0 {
which -= 1
transform_type = this.transform_type[which] as base.u32
tile_data = this.util.empty_slice_u8()
if transform_type < 2 {
ti = this.workbuf_offset_for_transform[transform_type + 1] as base.u64
tj = this.workbuf_offset_for_transform[transform_type + 2] as base.u64
if (ti <= tj) and (tj <= args.workbuf.length()) {
tile_data = args.workbuf[ti .. tj]
}
}
if transform_type == 0 {
this.apply_transform_predictor!(pix: pix, tile_data: tile_data)
} else if transform_type == 1 {
this.apply_transform_cross_color!(pix: pix, tile_data: tile_data)
} else if transform_type == 2 {
this.apply_transform_subtract_green!(pix: pix)
} else {
this.apply_transform_color_indexing!(pix: pix)
width = this.width
}
} endwhile
status = this.swizzle!(
dst: args.dst,
src: pix,
blend: args.blend)
if not status.is_ok() {
return status
}
this.call_sequence = 0x60
}
pri func decoder.decode_transform?(src: base.io_reader, workbuf: slice base.u8) {
var c8 : base.u8
var transform_type : base.u32[..= 3]
var tile_size_log2 : base.u32[..= 9]
var p : slice base.u8
if this.n_bits < 2 {
c8 = args.src.read_u8?()
if this.n_bits >= 2 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 2
}
transform_type = this.bits & 3
this.bits >>= 2
this.n_bits -= 2
if this.seen_transform[transform_type] or (this.n_transforms >= 4) {
return "#bad transform"
} else if this.seen_transform[3] {
return "#unsupported transform after color indexing transform"
}
this.seen_transform[transform_type] = true
this.transform_type[this.n_transforms] = transform_type as base.u8
this.n_transforms += 1
if transform_type < 2 { // Predictor, Cross Color transform.
if this.n_bits < 3 {
c8 = args.src.read_u8?()
if this.n_bits >= 3 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 3
}
tile_size_log2 = (this.bits & 7) + 2
this.transform_tile_size_log2[transform_type] = tile_size_log2 as base.u8
this.bits >>= 3
this.n_bits -= 3
this.decode_color_cache_parameters?(src: args.src)
this.decode_huffman_groups?(src: args.src, n_huffman_groups: 1)
if ((this.workbuf_offset_for_transform[transform_type + 1] as base.u64) > (this.workbuf_offset_for_transform[transform_type + 2] as base.u64)) or
((this.workbuf_offset_for_transform[transform_type + 2] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
this.decode_pixels?(
dst: args.workbuf[
(this.workbuf_offset_for_transform[transform_type + 1] as base.u64) ..
(this.workbuf_offset_for_transform[transform_type + 2] as base.u64)],
src: args.src,
width: (this.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2,
height: (this.height + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2,
tile_data: this.util.empty_slice_u8(),
tile_size_log2: 0)
} else if transform_type == 2 { // Subtract Green transform.
// No-op.
} else { // Color Indexing transform.
if this.n_bits < 8 {
c8 = args.src.read_u8?()
if this.n_bits >= 8 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 8
}
this.color_indexing_palette_size = (this.bits & 0xFF) + 1
this.bits >>= 8
this.n_bits -= 8
if this.color_indexing_palette_size <= 2 {
this.color_indexing_width = (this.width + 7) / 8
this.transform_tile_size_log2[3] = 3
} else if this.color_indexing_palette_size <= 4 {
this.color_indexing_width = (this.width + 3) / 4
this.transform_tile_size_log2[3] = 2
} else if this.color_indexing_palette_size <= 16 {
this.color_indexing_width = (this.width + 1) / 2
this.transform_tile_size_log2[3] = 1
} else {
this.color_indexing_width = this.width
this.transform_tile_size_log2[3] = 0
}
if this.width >= this.color_indexing_width {
this.workbuf_offset_for_color_indexing = 4 * (this.width - this.color_indexing_width) * this.height
}
this.decode_color_cache_parameters?(src: args.src)
this.decode_huffman_groups?(src: args.src, n_huffman_groups: 1)
this.decode_pixels?(
dst: this.palette[.. 4 * this.color_indexing_palette_size],
src: args.src,
width: this.color_indexing_palette_size,
height: 1,
tile_data: this.util.empty_slice_u8(),
tile_size_log2: 0)
this.palette[4 * this.color_indexing_palette_size .. 1024].bulk_memset!(byte_value: 0)
p = this.palette[.. 4 * this.color_indexing_palette_size]
while p.length() >= 8 {
p[4] ~mod+= p[0]
p[5] ~mod+= p[1]
p[6] ~mod+= p[2]
p[7] ~mod+= p[3]
p = p[4 ..]
} endwhile
}
}
pri func decoder.decode_color_cache_parameters?(src: base.io_reader) {
var c8 : base.u8
var use_color_cache : base.u32[..= 1]
var color_cache_bits : base.u32[..= 15]
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
use_color_cache = this.bits & 1
this.bits >>= 1
this.n_bits -= 1
this.color_cache_bits = 0
if use_color_cache <> 0 {
if this.n_bits < 4 {
c8 = args.src.read_u8?()
if this.n_bits >= 4 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 4
}
color_cache_bits = this.bits & 15
this.bits >>= 4
this.n_bits -= 4
if (color_cache_bits < 1) or (11 < color_cache_bits) {
return "#bad color cache"
}
this.color_cache_bits = color_cache_bits
}
}
pri func decoder.decode_hg_table?(src: base.io_reader, width: base.u32[..= 0x4000], workbuf: slice base.u8) {
var c8 : base.u8
var use_hg_table : base.u32[..= 1]
var tile_size_log2 : base.u32[..= 9]
var hg_pixels : slice base.u8
var n : base.u64
var p : roslice base.u8
var hg_plus_1 : base.u32[..= 256]
if this.n_bits < 1 {
c8 = args.src.read_u8?()
this.bits = (c8 as base.u32)
this.n_bits = 8
assert this.n_bits >= 1
}
use_hg_table = this.bits & 1
this.bits >>= 1
this.n_bits -= 1
if use_hg_table == 0 {
this.overall_n_huffman_groups = 1
this.overall_tile_size_log2 = 0
if ((this.workbuf_offset_for_transform[0] as base.u64) > (this.workbuf_offset_for_transform[1] as base.u64)) or
((this.workbuf_offset_for_transform[1] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
hg_pixels = args.workbuf[
this.workbuf_offset_for_transform[0] as base.u64 ..
this.workbuf_offset_for_transform[1] as base.u64]
if hg_pixels.length() >= 4 {
hg_pixels[0] = 0x00
hg_pixels[1] = 0x00
hg_pixels[2] = 0x00
hg_pixels[3] = 0x00
}
return ok
}
if this.n_bits < 3 {
c8 = args.src.read_u8?()
if this.n_bits >= 3 {
return "#internal error: inconsistent n_bits"
}
this.bits |= (c8 as base.u32) << this.n_bits
this.n_bits += 8
assert this.n_bits >= 3
}
tile_size_log2 = (this.bits & 7) + 2
this.bits >>= 3
this.n_bits -= 3
this.overall_tile_size_log2 = tile_size_log2
this.decode_color_cache_parameters?(src: args.src)
this.decode_huffman_groups?(src: args.src, n_huffman_groups: 1)
if ((this.workbuf_offset_for_transform[0] as base.u64) > (this.workbuf_offset_for_transform[1] as base.u64)) or
((this.workbuf_offset_for_transform[1] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
this.decode_pixels?(
dst: args.workbuf[
(this.workbuf_offset_for_transform[0] as base.u64) ..
(this.workbuf_offset_for_transform[1] as base.u64)],
src: args.src,
width: (args.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2,
height: (this.height + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2,
tile_data: this.util.empty_slice_u8(),
tile_size_log2: 0)
this.overall_n_huffman_groups = 1
if ((this.workbuf_offset_for_transform[0] as base.u64) > (this.workbuf_offset_for_transform[1] as base.u64)) or
((this.workbuf_offset_for_transform[1] as base.u64) > args.workbuf.length()) {
return base."#bad workbuf length"
}
hg_pixels = args.workbuf[
this.workbuf_offset_for_transform[0] as base.u64 ..
this.workbuf_offset_for_transform[1] as base.u64]
n = (((args.width + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2) *
((this.height + (((1 as base.u32) << tile_size_log2) - 1)) >> tile_size_log2) *
4) as base.u64
if n > hg_pixels.length() {
return base."#bad workbuf length"
}
p = hg_pixels[.. n]
while p.length() >= 4 {
if p[2] <> 0 {
return "#unsupported number of Huffman groups"
}
hg_plus_1 = (p[1] as base.u32) + 1
if this.overall_n_huffman_groups < hg_plus_1 {
this.overall_n_huffman_groups = hg_plus_1
}
p = p[4 ..]
} endwhile
}
pri func decoder.decode_pixels?(dst: slice base.u8, src: base.io_reader, width: base.u32[..= 0x4000], height: base.u32[..= 0x4000], tile_data: roslice base.u8, tile_size_log2: base.u32[..= 9]) {
var i : base.u32
var n : base.u32[..= 2048]
i = 0
n = (1 as base.u32) << this.color_cache_bits
while i < n {
assert i < 2048 via "a < b: a < c; c <= b"(c: n)
this.color_cache[i] = 0
i += 1
} endwhile
this.decode_pixels_slow?(
dst: args.dst,
src: args.src,
width: args.width,
height: args.height,
tile_data: args.tile_data,
tile_size_log2: args.tile_size_log2)
}
pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: roslice base.u8, blend: base.pixel_blend) base.status {
var status : base.status
var dst_pixfmt : base.pixel_format
var dst_bits_per_pixel : base.u32[..= 256]
var dst_bytes_per_pixel : base.u32[..= 32]
var dst_bytes_per_row : base.u64
var dst_palette : slice base.u8
var tab : table base.u8
var src_bytes_per_row : base.u64
var dst : slice base.u8
var y : base.u32
status = this.swizzler.prepare!(
dst_pixfmt: args.dst.pixel_format(),
dst_palette: args.dst.palette_or_else(fallback: this.palette[..]),
src_pixfmt: this.util.make_pixel_format(repr: this.pixfmt),
src_palette: this.util.empty_slice_u8(),
blend: args.blend)
if not status.is_ok() {
return status
}
// 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
dst_bytes_per_row = (this.width * dst_bytes_per_pixel) as base.u64
dst_palette = args.dst.palette_or_else(fallback: this.palette[..])
tab = args.dst.plane(p: 0)
src_bytes_per_row = (this.width * 4) as base.u64
while.outer src_bytes_per_row <= args.src.length() {
dst = tab.row_u32(y: y)
if dst_bytes_per_row < dst.length() {
dst = dst[.. dst_bytes_per_row]
}
this.swizzler.swizzle_interleaved_from_slice!(
dst: dst,
dst_palette: dst_palette,
src: args.src[.. src_bytes_per_row])
args.src = args.src[src_bytes_per_row ..]
y ~mod+= 1
} endwhile.outer
return ok
}
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
return this.util.make_rect_ie_u32(
min_incl_x: 0,
@@ -179,5 +835,22 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform
}
pub func decoder.workbuf_len() base.range_ii_u64 {
return this.util.make_range_ii_u64(min_incl: 0, max_incl: 0)
return this.util.make_range_ii_u64(
min_incl: this.workbuf_offset_for_transform[3] as base.u64,
max_incl: this.workbuf_offset_for_transform[3] as base.u64)
}
pri const DISTANCE_MAP : roarray[120] base.u8 = [
0x18, 0x07, 0x17, 0x19, 0x28, 0x06, 0x27, 0x29, 0x16, 0x1A,
0x26, 0x2A, 0x38, 0x05, 0x37, 0x39, 0x15, 0x1B, 0x36, 0x3A,
0x25, 0x2B, 0x48, 0x04, 0x47, 0x49, 0x14, 0x1C, 0x35, 0x3B,
0x46, 0x4A, 0x24, 0x2C, 0x58, 0x45, 0x4B, 0x34, 0x3C, 0x03,
0x57, 0x59, 0x13, 0x1D, 0x56, 0x5A, 0x23, 0x2D, 0x44, 0x4C,
0x55, 0x5B, 0x33, 0x3D, 0x68, 0x02, 0x67, 0x69, 0x12, 0x1E,
0x66, 0x6A, 0x22, 0x2E, 0x54, 0x5C, 0x43, 0x4D, 0x65, 0x6B,
0x32, 0x3E, 0x78, 0x01, 0x77, 0x79, 0x53, 0x5D, 0x11, 0x1F,
0x64, 0x6C, 0x42, 0x4E, 0x76, 0x7A, 0x21, 0x2F, 0x75, 0x7B,
0x31, 0x3F, 0x63, 0x6D, 0x52, 0x5E, 0x00, 0x74, 0x7C, 0x41,
0x4F, 0x10, 0x20, 0x62, 0x6E, 0x30, 0x73, 0x7D, 0x51, 0x5F,
0x40, 0x72, 0x7E, 0x61, 0x6F, 0x50, 0x71, 0x7F, 0x60, 0x70,
]