Files
wuffs/std/jpeg/decode_mcu_progressive_dc_high_bits.wuffs
T
Nigel Tao 74a6ea2fcd all: relicense from Apache2 to dual Apache2/MIT
The LICENSE has changed from a single license (Apache2) to a dual
license (Apache2 or MIT, at your option).

If you've previously used Wuffs under the Apache2 license, you are able
to keep doing so. No further action required by you.

The two files listed below (previous releases) are exceptions, as they
are older snapshots, frozen in time. Their license has not changed:
release/c/wuffs-v0.2.c
release/c/wuffs-v0.3.c

Every name in the AUTHORS file, other than Google Inc., has signed
Google's CLA (Contributor License Agreement).
2023-10-06 10:00:59 +11:00

137 lines
5.4 KiB
Plaintext

// Copyright 2023 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.
pri func decoder.decode_mcu_progressive_dc_high_bits!(workbuf: slice base.u8, mx: base.u32[..= 0x1FFF], my: base.u32[..= 0x1FFF]) base.u32 {
var ret : base.u32
var bits : base.u64
var n_bits : base.u32
var csel : base.u8[..= 3]
var r : base.io_reader
var pos : base.u32
var dc_h : base.u8[..= 3]
var dc_symbol : base.u32[..= 0x0F]
var dc_ht_fast : base.u32
var dc_bl : base.u32
var dc_code : base.u32
var dc_blm1 : base.u32[..= 15]
var dc_ht_slow : base.u32
var dc_value : base.u16
var dc_extend : base.u16
bits = this.bitstream_bits
n_bits = this.bitstream_n_bits
if this.bitstream_ri > this.bitstream_wi {
return 2 // Internal error.
}
io_bind (io: r, data: this.bitstream_buffer[this.bitstream_ri .. this.bitstream_wi], history_position: this.bitstream_ri as base.u64) {
while.goto_done true {{
while.block this.mcu_current_block < this.mcu_num_blocks {
assert this.mcu_current_block < 10 via "a < b: a < c; c <= b"(c: this.mcu_num_blocks)
// Ensure that we have enough bits for this iteration of the
// while.block loop body. Worst case, there are 64 components and
// each one needs (16 + 15) bits (round that up to 4 bytes), so we
// need (64 * 4) = 256 bytes available. 8 more bytes of slack means
// that we can always call peek_u64be.
if r.length() < 264 {
ret = 1 // Request another fill_bitstream call.
break.goto_done
}
while.dc_component true,
inv this.mcu_current_block < 10,
{
// Load at least 56 bits.
if r.length() < 8 {
ret = 2 // Internal error.
break.goto_done
}
bits |= r.peek_u64be() >> (n_bits & 63)
r.skip_u32_fast!(actual: (63 - (n_bits & 63)) >> 3, worst_case: 8)
n_bits |= 56
// Read the Huffman-encoded dc_symbol, up to 16 bits long.
dc_h = this.mcu_blocks_dc_hselector[this.mcu_current_block]
dc_ht_fast = this.huff_tables_fast[dc_h][bits >> 56] as base.u32
dc_bl = dc_ht_fast >> 8
if n_bits >= dc_bl {
dc_symbol = 0x0F & dc_ht_fast
dc_extend = EXTEND[dc_symbol]
bits ~mod<<= (dc_bl & 63)
n_bits -= dc_bl
} else {
dc_code = (bits >> 55) as base.u32
dc_blm1 = 8
bits ~mod<<= 9
n_bits ~mod-= 9
while true,
inv this.mcu_current_block < 10,
{
dc_ht_slow = this.huff_tables_slow[dc_h][dc_blm1]
if dc_code < (dc_ht_slow >> 8) {
dc_symbol = 0x0F & (this.huff_tables_symbols[dc_h][0xFF & (dc_code ~mod+ dc_ht_slow)] as base.u32)
dc_extend = EXTEND[dc_symbol]
break
}
dc_code = (dc_code ~mod<< 1) | ((bits >> 63) as base.u32)
bits ~mod<<= 1
n_bits ~mod-= 1
dc_blm1 = (dc_blm1 + 1) & 15
if dc_blm1 == 0 {
dc_symbol = 0
dc_extend = EXTEND[dc_symbol]
break
}
} endwhile
}
// Process the dc_value in the next dc_symbol (up to 15) bits.
//
// The dc_value is shifted by (64 - dc_symbol) in two steps,
// because we want to shift by 64 (not 0) when dc_symbol is 0.
dc_value = (((bits >> 32) >> (32 - dc_symbol)) & 0xFFFF) as base.u16
dc_value ~mod+= dc_extend &
(((this.util.sign_extend_rshift_u64(a: bits, n: 63) & 0xFFFF) as base.u16) ^ 0xFFFF)
bits ~mod<<= dc_symbol
n_bits ~mod-= dc_symbol
csel = this.scan_comps_cselector[this.mcu_blocks_sselector[this.mcu_current_block]]
this.mcu_previous_dc_values[csel] ~mod+= dc_value
this.mcu_blocks[this.mcu_current_block][0] =
this.mcu_previous_dc_values[csel] ~mod<< this.scan_al
break.dc_component
} endwhile.dc_component
assert this.mcu_current_block < 10
this.mcu_current_block += 1
} endwhile.block
this.mcu_current_block = 0
break.goto_done
}} endwhile.goto_done
pos = (r.position() & 0xFFFF_FFFF) as base.u32
if pos > this.bitstream_wi {
ret = 2 // Internal error.
} else {
assert pos <= 0x800 via "a <= b: a <= c; c <= b"(c: this.bitstream_wi)
this.bitstream_ri = pos
}
}
this.bitstream_bits = bits
this.bitstream_n_bits = n_bits
return ret
}