mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Use !?? to denote coroutines
This commit is contained in:
1 parent
0a47966e72
commit
2100a9f837
8 files changed
+149
-149
No files matched your search
+31
-31
@@ -133,21 +133,21 @@ var Funcs = []string{
|
||||
"io_reader.can_undo_byte() bool",
|
||||
"io_reader.undo_byte!()",
|
||||
|
||||
"io_reader.read_u8?() u8",
|
||||
"io_reader.read_u16be?() u16",
|
||||
"io_reader.read_u16le?() u16",
|
||||
"io_reader.read_u24be?() u32[..0xFFFFFF]",
|
||||
"io_reader.read_u24le?() u32[..0xFFFFFF]",
|
||||
"io_reader.read_u32be?() u32",
|
||||
"io_reader.read_u32le?() u32",
|
||||
"io_reader.read_u40be?() u64[..0xFFFFFFFFFF]",
|
||||
"io_reader.read_u40le?() u64[..0xFFFFFFFFFF]",
|
||||
"io_reader.read_u48be?() u64[..0xFFFFFFFFFFFF]",
|
||||
"io_reader.read_u48le?() u64[..0xFFFFFFFFFFFF]",
|
||||
"io_reader.read_u56be?() u64[..0xFFFFFFFFFFFFFF]",
|
||||
"io_reader.read_u56le?() u64[..0xFFFFFFFFFFFFFF]",
|
||||
"io_reader.read_u64be?() u64",
|
||||
"io_reader.read_u64le?() u64",
|
||||
"io_reader.read_u8!??() u8",
|
||||
"io_reader.read_u16be!??() u16",
|
||||
"io_reader.read_u16le!??() u16",
|
||||
"io_reader.read_u24be!??() u32[..0xFFFFFF]",
|
||||
"io_reader.read_u24le!??() u32[..0xFFFFFF]",
|
||||
"io_reader.read_u32be!??() u32",
|
||||
"io_reader.read_u32le!??() u32",
|
||||
"io_reader.read_u40be!??() u64[..0xFFFFFFFFFF]",
|
||||
"io_reader.read_u40le!??() u64[..0xFFFFFFFFFF]",
|
||||
"io_reader.read_u48be!??() u64[..0xFFFFFFFFFFFF]",
|
||||
"io_reader.read_u48le!??() u64[..0xFFFFFFFFFFFF]",
|
||||
"io_reader.read_u56be!??() u64[..0xFFFFFFFFFFFFFF]",
|
||||
"io_reader.read_u56le!??() u64[..0xFFFFFFFFFFFFFF]",
|
||||
"io_reader.read_u64be!??() u64",
|
||||
"io_reader.read_u64le!??() u64",
|
||||
|
||||
// TODO: these should have an explicit pre-condition "available() >= N".
|
||||
// For now, that's implicitly checked (i.e. hard coded).
|
||||
@@ -177,7 +177,7 @@ var Funcs = []string{
|
||||
"io_reader.set_mark!()",
|
||||
"io_reader.since_mark() slice u8",
|
||||
|
||||
"io_reader.skip?(n u32)",
|
||||
"io_reader.skip!??(n u32)",
|
||||
|
||||
// TODO: this should have explicit pre-conditions "actual <= worst_case"
|
||||
// and "worst_case <= available()". As an implementation restriction, we
|
||||
@@ -187,21 +187,21 @@ var Funcs = []string{
|
||||
|
||||
// ---- io_writer
|
||||
|
||||
"io_writer.write_u8?(x u8)",
|
||||
"io_writer.write_u16be?(x u16)",
|
||||
"io_writer.write_u16le?(x u16)",
|
||||
"io_writer.write_u24be?(x u32[..0xFFFFFF])",
|
||||
"io_writer.write_u24le?(x u32[..0xFFFFFF])",
|
||||
"io_writer.write_u32be?(x u32)",
|
||||
"io_writer.write_u32le?(x u32)",
|
||||
"io_writer.write_u40be?(x u64[..0xFFFFFFFFFF])",
|
||||
"io_writer.write_u40le?(x u64[..0xFFFFFFFFFF])",
|
||||
"io_writer.write_u48be?(x u64[..0xFFFFFFFFFFFF])",
|
||||
"io_writer.write_u48le?(x u64[..0xFFFFFFFFFFFF])",
|
||||
"io_writer.write_u56be?(x u64[..0xFFFFFFFFFFFFFF])",
|
||||
"io_writer.write_u56le?(x u64[..0xFFFFFFFFFFFFFF])",
|
||||
"io_writer.write_u64be?(x u64)",
|
||||
"io_writer.write_u64le?(x u64)",
|
||||
"io_writer.write_u8!??(x u8)",
|
||||
"io_writer.write_u16be!??(x u16)",
|
||||
"io_writer.write_u16le!??(x u16)",
|
||||
"io_writer.write_u24be!??(x u32[..0xFFFFFF])",
|
||||
"io_writer.write_u24le!??(x u32[..0xFFFFFF])",
|
||||
"io_writer.write_u32be!??(x u32)",
|
||||
"io_writer.write_u32le!??(x u32)",
|
||||
"io_writer.write_u40be!??(x u64[..0xFFFFFFFFFF])",
|
||||
"io_writer.write_u40le!??(x u64[..0xFFFFFFFFFF])",
|
||||
"io_writer.write_u48be!??(x u64[..0xFFFFFFFFFFFF])",
|
||||
"io_writer.write_u48le!??(x u64[..0xFFFFFFFFFFFF])",
|
||||
"io_writer.write_u56be!??(x u64[..0xFFFFFFFFFFFFFF])",
|
||||
"io_writer.write_u56le!??(x u64[..0xFFFFFFFFFFFFFF])",
|
||||
"io_writer.write_u64be!??(x u64)",
|
||||
"io_writer.write_u64le!??(x u64)",
|
||||
|
||||
// TODO: these should have an explicit pre-condition "available() >= N".
|
||||
// For now, that's implicitly checked (i.e. hard coded).
|
||||
|
||||
@@ -123,10 +123,10 @@ pub struct decoder?(
|
||||
end_of_block base.bool,
|
||||
)
|
||||
|
||||
pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
|
||||
while true {
|
||||
args.dst.set_mark!()
|
||||
var z base.status = try this.decode_blocks?(dst:args.dst, src:args.src)
|
||||
var z base.status = try this.decode_blocks!??(dst:args.dst, src:args.src)
|
||||
if not z.is_suspension() {
|
||||
return z
|
||||
}
|
||||
@@ -175,13 +175,13 @@ pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.decode_blocks?(dst base.io_writer, src base.io_reader) {
|
||||
pri func decoder.decode_blocks!??(dst base.io_writer, src base.io_reader) {
|
||||
var final base.u32
|
||||
while final == 0 {
|
||||
while this.n_bits < 3,
|
||||
post this.n_bits >= 3,
|
||||
{
|
||||
this.bits |= (args.src.read_u8?() as base.u32) << this.n_bits
|
||||
this.bits |= (args.src.read_u8!??() as base.u32) << this.n_bits
|
||||
this.n_bits += 8
|
||||
}
|
||||
final = this.bits & 0x01
|
||||
@@ -190,22 +190,22 @@ pri func decoder.decode_blocks?(dst base.io_writer, src base.io_reader) {
|
||||
this.n_bits -= 3
|
||||
|
||||
if type == 0 {
|
||||
this.decode_uncompressed?(dst:args.dst, src:args.src)
|
||||
this.decode_uncompressed!??(dst:args.dst, src:args.src)
|
||||
continue
|
||||
} else if type == 1 {
|
||||
this.init_fixed_huffman?()
|
||||
this.init_fixed_huffman!??()
|
||||
} else if type == 2 {
|
||||
this.init_dynamic_huffman?(src:args.src)
|
||||
this.init_dynamic_huffman!??(src:args.src)
|
||||
} else {
|
||||
return error "bad block"
|
||||
}
|
||||
|
||||
this.end_of_block = false
|
||||
this.decode_huffman_fast?(dst:args.dst, src:args.src)
|
||||
this.decode_huffman_fast!??(dst:args.dst, src:args.src)
|
||||
if this.end_of_block {
|
||||
continue
|
||||
}
|
||||
this.decode_huffman_slow?(dst:args.dst, src:args.src)
|
||||
this.decode_huffman_slow!??(dst:args.dst, src:args.src)
|
||||
if this.end_of_block {
|
||||
continue
|
||||
}
|
||||
@@ -215,7 +215,7 @@ pri func decoder.decode_blocks?(dst base.io_writer, src base.io_reader) {
|
||||
|
||||
// decode_uncompressed decodes an uncompresed block as per the RFC section
|
||||
// 3.2.4.
|
||||
pri func decoder.decode_uncompressed?(dst base.io_writer, src base.io_reader) {
|
||||
pri func decoder.decode_uncompressed!??(dst base.io_writer, src base.io_reader) {
|
||||
// TODO: make this "if" into a function invariant?
|
||||
//
|
||||
// Ditto for decode_huffman_slow and decode_huffman_fast.
|
||||
@@ -225,7 +225,7 @@ pri func decoder.decode_uncompressed?(dst base.io_writer, src base.io_reader) {
|
||||
this.n_bits = 0
|
||||
this.bits = 0
|
||||
|
||||
var length base.u32 = args.src.read_u32le?()
|
||||
var length base.u32 = args.src.read_u32le!??()
|
||||
if (length.low_bits(n:16) + length.high_bits(n:16)) != 0xFFFF {
|
||||
return error "inconsistent stored block length"
|
||||
}
|
||||
@@ -246,7 +246,7 @@ pri func decoder.decode_uncompressed?(dst base.io_writer, src base.io_reader) {
|
||||
}
|
||||
|
||||
// init_fixed_huffman initializes this.huffs as per the RFC section 3.2.6.
|
||||
pri func decoder.init_fixed_huffman?() {
|
||||
pri func decoder.init_fixed_huffman!??() {
|
||||
var i base.u32
|
||||
while i < 144 {
|
||||
this.code_lengths[i] = 8
|
||||
@@ -268,18 +268,18 @@ pri func decoder.init_fixed_huffman?() {
|
||||
this.code_lengths[i] = 5
|
||||
i += 1
|
||||
}
|
||||
this.init_huff?(which:0, n_codes0:0, n_codes1:288, base_symbol:257)
|
||||
this.init_huff?(which:1, n_codes0:288, n_codes1:320, base_symbol:0)
|
||||
this.init_huff!??(which:0, n_codes0:0, n_codes1:288, base_symbol:257)
|
||||
this.init_huff!??(which:1, n_codes0:288, n_codes1:320, base_symbol:0)
|
||||
}
|
||||
|
||||
// init_dynamic_huffman initializes this.huffs as per the RFC section 3.2.7.
|
||||
pri func decoder.init_dynamic_huffman?(src base.io_reader) {
|
||||
pri func decoder.init_dynamic_huffman!??(src base.io_reader) {
|
||||
var bits base.u32 = this.bits
|
||||
var n_bits base.u32 = this.n_bits
|
||||
while n_bits < 14,
|
||||
post n_bits >= 14,
|
||||
{
|
||||
bits |= (args.src.read_u8?() as base.u32) << n_bits
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
var n_lit base.u32[257..288] = bits.low_bits(n:5) + 257
|
||||
@@ -303,7 +303,7 @@ pri func decoder.init_dynamic_huffman?(src base.io_reader) {
|
||||
inv i < n_clen,
|
||||
post n_bits >= 3,
|
||||
{
|
||||
bits |= (args.src.read_u8?() as base.u32) << n_bits
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
assert i < 19 via "a < b: a < c; c <= b"(c:n_clen)
|
||||
@@ -316,7 +316,7 @@ pri func decoder.init_dynamic_huffman?(src base.io_reader) {
|
||||
this.code_lengths[code_order[i]] = 0
|
||||
i += 1
|
||||
}
|
||||
this.init_huff?(which:0, n_codes0:0, n_codes1:19, base_symbol:0xFFF)
|
||||
this.init_huff!??(which:0, n_codes0:0, n_codes1:19, base_symbol:0xFFF)
|
||||
|
||||
// Decode the code lengths for the next two Huffman tables.
|
||||
var mask base.u32[..511] = ((1 as base.u32) << this.n_huffs_bits[0]) - 1
|
||||
@@ -337,7 +337,7 @@ pri func decoder.init_dynamic_huffman?(src base.io_reader) {
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (args.src.read_u8?() as base.u32) << n_bits
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
// For H-CL, there should be no redirections and all symbols should be
|
||||
@@ -385,7 +385,7 @@ pri func decoder.init_dynamic_huffman?(src base.io_reader) {
|
||||
post n_bits >= n_extra_bits,
|
||||
{
|
||||
assert n_bits < 7 via "a < b: a < c; c <= b"(c:n_extra_bits)
|
||||
bits |= (args.src.read_u8?() as base.u32) << n_bits
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
rep_count += bits.low_bits(n:n_extra_bits)
|
||||
@@ -411,8 +411,8 @@ pri func decoder.init_dynamic_huffman?(src base.io_reader) {
|
||||
return error "missing end-of-block code"
|
||||
}
|
||||
|
||||
this.init_huff?(which:0, n_codes0:0, n_codes1:n_lit, base_symbol:257)
|
||||
this.init_huff?(which:1, n_codes0:n_lit, n_codes1:n_lit + n_dist, base_symbol:0)
|
||||
this.init_huff!??(which:0, n_codes0:0, n_codes1:n_lit, base_symbol:257)
|
||||
this.init_huff!??(which:1, n_codes0:n_lit, n_codes1:n_lit + n_dist, base_symbol:0)
|
||||
|
||||
this.bits = bits
|
||||
this.n_bits = n_bits
|
||||
@@ -423,7 +423,7 @@ pri func decoder.init_dynamic_huffman?(src base.io_reader) {
|
||||
// TODO: skip generating WUFFS_COROUTINE_SUSPENSION_POINTs for *callers* of
|
||||
// this function, as it can only return errors, not suspensions.
|
||||
|
||||
pri func decoder.init_huff?(which base.u32[..1], n_codes0 base.u32[..320], n_codes1 base.u32[..320], base_symbol base.u32) {
|
||||
pri func decoder.init_huff!??(which base.u32[..1], n_codes0 base.u32[..320], n_codes1 base.u32[..320], base_symbol base.u32) {
|
||||
// For the clcode example in this package's README.md:
|
||||
// - n_codes0 = 0
|
||||
// - n_codes1 = 19
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
// TODO: describe how the xxx_fast version differs from the xxx_slow one, the
|
||||
// assumptions that xxx_fast makes, and how that makes it fast.
|
||||
pri func decoder.decode_huffman_fast?(dst base.io_writer, src base.io_reader) {
|
||||
pri func decoder.decode_huffman_fast!??(dst base.io_writer, src base.io_reader) {
|
||||
// When editing this function, consider making the equivalent change to the
|
||||
// decode_huffman_slow function. Keep the diff between the two
|
||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) {
|
||||
pri func decoder.decode_huffman_slow!??(dst base.io_writer, src base.io_reader) {
|
||||
// When editing this function, consider making the equivalent change to the
|
||||
// decode_huffman_fast function. Keep the diff between the two
|
||||
// decode_huffman_*.wuffs files as small as possible, while retaining both
|
||||
@@ -41,13 +41,13 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) {
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (args.src.read_u8?() as base.u32) << n_bits
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
|
||||
if (table_entry >> 31) != 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as base.u8)
|
||||
args.dst.write_u8!??(x:((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue:loop
|
||||
} else if (table_entry >> 30) != 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
@@ -71,13 +71,13 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) {
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (args.src.read_u8?() as base.u32) << n_bits
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
|
||||
if (table_entry >> 31) != 0 {
|
||||
// Literal.
|
||||
args.dst.write_u8?(x:((table_entry >> 8) & 0xFF) as base.u8)
|
||||
args.dst.write_u8!??(x:((table_entry >> 8) & 0xFF) as base.u8)
|
||||
continue:loop
|
||||
} else if (table_entry >> 30) != 0 {
|
||||
// No-op; code continues past the if-else chain.
|
||||
@@ -107,7 +107,7 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) {
|
||||
post n_bits >= table_entry_n_bits,
|
||||
{
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (args.src.read_u8?() as base.u32) << n_bits
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
length = (length + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF
|
||||
@@ -125,7 +125,7 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) {
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (args.src.read_u8?() as base.u32) << n_bits
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
// Check for a redirect.
|
||||
@@ -144,7 +144,7 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) {
|
||||
break
|
||||
}
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (args.src.read_u8?() as base.u32) << n_bits
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
}
|
||||
@@ -170,7 +170,7 @@ pri func decoder.decode_huffman_slow?(dst base.io_writer, src base.io_reader) {
|
||||
post n_bits >= table_entry_n_bits,
|
||||
{
|
||||
assert n_bits < 15 via "a < b: a < c; c <= b"(c:table_entry_n_bits)
|
||||
bits |= (args.src.read_u8?() as base.u32) << n_bits
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
dist_minus_1 = (dist_minus_1 + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF
|
||||
|
||||
+64
-64
@@ -122,13 +122,13 @@ pub struct decoder?(
|
||||
lzw lzw.decoder,
|
||||
)
|
||||
|
||||
pub func decoder.decode_image_config?(dst nptr base.image_config, src base.io_reader) {
|
||||
pub func decoder.decode_image_config!??(dst nptr base.image_config, src base.io_reader) {
|
||||
if this.call_sequence >= 1 {
|
||||
return error "invalid call sequence"
|
||||
}
|
||||
this.decode_header?(src:args.src)
|
||||
this.decode_lsd?(src:args.src)
|
||||
this.decode_up_to_id_part1?(src:args.src)
|
||||
this.decode_header!??(src:args.src)
|
||||
this.decode_lsd!??(src:args.src)
|
||||
this.decode_up_to_id_part1!??(src:args.src)
|
||||
|
||||
// TODO: if this.end_of_data, return an error and/or set dst to zero?
|
||||
|
||||
@@ -168,15 +168,15 @@ pub func decoder.work_buffer_size() base.range_ii_u64 {
|
||||
return this.util.make_range_ii_u64(min_incl:0, max_incl:0)
|
||||
}
|
||||
|
||||
pub func decoder.decode_frame_config?(dst nptr base.frame_config, src base.io_reader) {
|
||||
pub func decoder.decode_frame_config!??(dst nptr base.frame_config, src base.io_reader) {
|
||||
if not this.end_of_data {
|
||||
if this.call_sequence == 0 {
|
||||
this.decode_image_config?(dst:nullptr, src:args.src)
|
||||
this.decode_image_config!??(dst:nullptr, src:args.src)
|
||||
} else if this.call_sequence != 1 {
|
||||
if this.call_sequence == 2 {
|
||||
this.skip_frame?(src:args.src)
|
||||
this.skip_frame!??(src:args.src)
|
||||
}
|
||||
this.decode_up_to_id_part1?(src:args.src)
|
||||
this.decode_up_to_id_part1!??(src:args.src)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,9 +215,9 @@ pub func decoder.decode_frame_config?(dst nptr base.frame_config, src base.io_re
|
||||
this.call_sequence = 2
|
||||
}
|
||||
|
||||
pri func decoder.skip_frame?(src base.io_reader) {
|
||||
var lw base.u8 = args.src.read_u8?()
|
||||
this.skip_blocks?(src:args.src)
|
||||
pri func decoder.skip_frame!??(src base.io_reader) {
|
||||
var lw base.u8 = args.src.read_u8!??()
|
||||
this.skip_blocks!??(src:args.src)
|
||||
|
||||
// The Image Descriptor is mandatory, but the Graphic Control extension is
|
||||
// optional. Reset the GC related fields for the next decode_frame call.
|
||||
@@ -232,12 +232,12 @@ pri func decoder.skip_frame?(src base.io_reader) {
|
||||
}
|
||||
|
||||
// TODO: honor pixbuf_origin_x and pixbuf_origin_y args.
|
||||
pub func decoder.decode_frame?(dst ptr base.pixel_buffer, pixbuf_origin_x base.u32, pixbuf_origin_y base.u32, src base.io_reader, work_buffer slice base.u8) {
|
||||
pub func decoder.decode_frame!??(dst ptr base.pixel_buffer, pixbuf_origin_x base.u32, pixbuf_origin_y base.u32, src base.io_reader, work_buffer slice base.u8) {
|
||||
if this.call_sequence != 2 {
|
||||
this.decode_frame_config?(dst:nullptr, src:args.src)
|
||||
this.decode_frame_config!??(dst:nullptr, src:args.src)
|
||||
}
|
||||
|
||||
this.decode_id_part1?(dst:args.dst, src:args.src)
|
||||
this.decode_id_part1!??(dst:args.dst, src:args.src)
|
||||
|
||||
// The Image Descriptor is mandatory, but the Graphic Control extension is
|
||||
// optional. Reset the GC related fields for the next decode_frame call.
|
||||
@@ -251,13 +251,13 @@ pub func decoder.decode_frame?(dst ptr base.pixel_buffer, pixbuf_origin_x base.u
|
||||
this.call_sequence = 3
|
||||
}
|
||||
|
||||
pri func decoder.decode_up_to_id_part1?(src base.io_reader) {
|
||||
pri func decoder.decode_up_to_id_part1!??(src base.io_reader) {
|
||||
while true {
|
||||
var block_type base.u8 = args.src.read_u8?()
|
||||
var block_type base.u8 = args.src.read_u8!??()
|
||||
if block_type == 0x21 { // The spec calls 0x21 the "Extension Introducer".
|
||||
this.decode_extension?(src:args.src)
|
||||
this.decode_extension!??(src:args.src)
|
||||
} else if block_type == 0x2C { // The spec calls 0x2C the "Image Separator".
|
||||
this.decode_id_part0?(src:args.src)
|
||||
this.decode_id_part0!??(src:args.src)
|
||||
break
|
||||
} else if block_type == 0x3B { // The spec calls 0x3B the "Trailer".
|
||||
this.end_of_data = true
|
||||
@@ -271,11 +271,11 @@ pri func decoder.decode_up_to_id_part1?(src base.io_reader) {
|
||||
// decode_header reads either "GIF87a" or "GIF89a".
|
||||
//
|
||||
// See the spec section 17 "Header" on page 7.
|
||||
pri func decoder.decode_header?(src base.io_reader) {
|
||||
pri func decoder.decode_header!??(src base.io_reader) {
|
||||
var c array[6] base.u8
|
||||
var i base.u32
|
||||
while i < 6 {
|
||||
c[i] = args.src.read_u8?()
|
||||
c[i] = args.src.read_u8!??()
|
||||
i += 1
|
||||
}
|
||||
if (c[0] != 0x47) or (c[1] != 0x49) or (c[2] != 0x46) or (c[3] != 0x38) or
|
||||
@@ -287,12 +287,12 @@ pri func decoder.decode_header?(src base.io_reader) {
|
||||
// decode_lsd reads the Logical Screen Descriptor.
|
||||
//
|
||||
// See the spec section 18 "Logical Screen Descriptor" on page 8.
|
||||
pri func decoder.decode_lsd?(src base.io_reader) {
|
||||
this.width = args.src.read_u16le?() as base.u32
|
||||
this.height = args.src.read_u16le?() as base.u32
|
||||
var flags base.u8 = args.src.read_u8?()
|
||||
pri func decoder.decode_lsd!??(src base.io_reader) {
|
||||
this.width = args.src.read_u16le!??() as base.u32
|
||||
this.height = args.src.read_u16le!??() as base.u32
|
||||
var flags base.u8 = args.src.read_u8!??()
|
||||
// Ignore the Background Color Index and Pixel Aspect Ratio bytes.
|
||||
args.src.skip?(n:2)
|
||||
args.src.skip!??(n:2)
|
||||
|
||||
// Read the optional Global Color Table.
|
||||
if (flags & 0x80) != 0 {
|
||||
@@ -302,7 +302,7 @@ pri func decoder.decode_lsd?(src base.io_reader) {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c:num_palette_entries)
|
||||
// Convert from RGB (in memory order) to ARGB (in native u32 order)
|
||||
// to BGRA (in memory order).
|
||||
var argb base.u32 = args.src.read_u24be?() | 0xFF000000
|
||||
var argb base.u32 = args.src.read_u24be!??() | 0xFF000000
|
||||
this.palettes[0][(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
|
||||
this.palettes[0][(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
|
||||
this.palettes[0][(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
|
||||
@@ -329,27 +329,27 @@ pri func decoder.decode_lsd?(src base.io_reader) {
|
||||
// - section 24 "Comment Extension" on page 17.
|
||||
// - section 25 "Plain Text Extension" on page 18.
|
||||
// - section 26 "Application Extension" on page 21.
|
||||
pri func decoder.decode_extension?(src base.io_reader) {
|
||||
var label base.u8 = args.src.read_u8?()
|
||||
pri func decoder.decode_extension!??(src base.io_reader) {
|
||||
var label base.u8 = args.src.read_u8!??()
|
||||
if label == 0xF9 { // The spec calls 0xF9 the "Graphic Control Label".
|
||||
this.decode_gc?(src:args.src)
|
||||
this.decode_gc!??(src:args.src)
|
||||
return
|
||||
} else if label == 0xFF { // The spec calls 0xFF the "Application Extension Label".
|
||||
this.decode_ae?(src:args.src)
|
||||
this.decode_ae!??(src:args.src)
|
||||
return
|
||||
}
|
||||
// We skip over all other extensions, including 0x01 "Plain Text Label" and
|
||||
// 0xFE "Comment Label".
|
||||
this.skip_blocks?(src:args.src)
|
||||
this.skip_blocks!??(src:args.src)
|
||||
}
|
||||
|
||||
pri func decoder.skip_blocks?(src base.io_reader) {
|
||||
pri func decoder.skip_blocks!??(src base.io_reader) {
|
||||
while true {
|
||||
var block_size base.u8 = args.src.read_u8?()
|
||||
var block_size base.u8 = args.src.read_u8!??()
|
||||
if block_size == 0 {
|
||||
return
|
||||
}
|
||||
args.src.skip?(n:block_size as base.u32)
|
||||
args.src.skip!??(n:block_size as base.u32)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,12 +364,12 @@ pri const netscape2dot0 array[11] base.u8 = $(
|
||||
)
|
||||
|
||||
// decode_ae reads an Application Extension.
|
||||
pri func decoder.decode_ae?(src base.io_reader) {
|
||||
pri func decoder.decode_ae!??(src base.io_reader) {
|
||||
// This "while true" always executes exactly once, as it ends with a
|
||||
// "break", but using "break"s throughout simplifies the control flow.
|
||||
while true {
|
||||
var c base.u8
|
||||
var block_size base.u8 = args.src.read_u8?()
|
||||
var block_size base.u8 = args.src.read_u8!??()
|
||||
if block_size == 0 {
|
||||
return
|
||||
}
|
||||
@@ -379,14 +379,14 @@ pri func decoder.decode_ae?(src base.io_reader) {
|
||||
// - http://www.vurdalakov.net/misc/gif/animexts-looping-application-extension
|
||||
// - http://www.vurdalakov.net/misc/gif/netscape-looping-application-extension
|
||||
if block_size != 11 {
|
||||
args.src.skip?(n:block_size as base.u32)
|
||||
args.src.skip!??(n:block_size as base.u32)
|
||||
break
|
||||
}
|
||||
var not_animexts base.bool
|
||||
var not_netscape base.bool
|
||||
block_size = 0 // Re-purpose the block_size variable as a counter.
|
||||
while block_size < 11 {
|
||||
c = args.src.read_u8?()
|
||||
c = args.src.read_u8!??()
|
||||
not_animexts = not_animexts or (c != animexts1dot0[block_size])
|
||||
not_netscape = not_netscape or (c != netscape2dot0[block_size])
|
||||
block_size += 1
|
||||
@@ -397,17 +397,17 @@ pri func decoder.decode_ae?(src base.io_reader) {
|
||||
|
||||
// Those 11 bytes should be followed by 0x03, 0x01 and then the loop
|
||||
// count.
|
||||
block_size = args.src.read_u8?()
|
||||
block_size = args.src.read_u8!??()
|
||||
if block_size != 3 {
|
||||
args.src.skip?(n:block_size as base.u32)
|
||||
args.src.skip!??(n:block_size as base.u32)
|
||||
break
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
c = args.src.read_u8!??()
|
||||
if c != 0x01 {
|
||||
args.src.skip?(n:2)
|
||||
args.src.skip!??(n:2)
|
||||
break
|
||||
}
|
||||
this.num_loops = args.src.read_u16le?() as base.u32
|
||||
this.num_loops = args.src.read_u16le!??() as base.u32
|
||||
this.seen_num_loops = true
|
||||
|
||||
// A loop count of N, in the wire format, actually means "repeat N
|
||||
@@ -426,20 +426,20 @@ pri func decoder.decode_ae?(src base.io_reader) {
|
||||
|
||||
break
|
||||
}
|
||||
this.skip_blocks?(src:args.src)
|
||||
this.skip_blocks!??(src:args.src)
|
||||
}
|
||||
|
||||
// decode_gc reads a Graphic Control.
|
||||
pri func decoder.decode_gc?(src base.io_reader) {
|
||||
pri func decoder.decode_gc!??(src base.io_reader) {
|
||||
if this.seen_graphic_control {
|
||||
return error "bad graphic control"
|
||||
}
|
||||
var c base.u8 = args.src.read_u8?()
|
||||
var c base.u8 = args.src.read_u8!??()
|
||||
if c != 4 {
|
||||
return error "bad graphic control"
|
||||
}
|
||||
|
||||
var flags base.u8 = args.src.read_u8?()
|
||||
var flags base.u8 = args.src.read_u8!??()
|
||||
this.gc_has_transparent_index = (flags & 0x01) != 0
|
||||
|
||||
// Convert the disposal method from GIF's wire format to Wuffs constants.
|
||||
@@ -461,10 +461,10 @@ pri func decoder.decode_gc?(src base.io_reader) {
|
||||
}
|
||||
|
||||
// There are 7056000 flicks per centisecond.
|
||||
this.gc_duration = (args.src.read_u16le?() as base.u64) * 7056000
|
||||
this.gc_transparent_index = args.src.read_u8?()
|
||||
this.gc_duration = (args.src.read_u16le!??() as base.u64) * 7056000
|
||||
this.gc_transparent_index = args.src.read_u8!??()
|
||||
|
||||
c = args.src.read_u8?()
|
||||
c = args.src.read_u8!??()
|
||||
if c != 0 {
|
||||
return error "bad graphic control"
|
||||
}
|
||||
@@ -479,11 +479,11 @@ pri func decoder.decode_gc?(src base.io_reader) {
|
||||
// The code is split into two parts (part0 and part1) because determining the
|
||||
// overall image's width and height also requires decoding the first frame's
|
||||
// bounds (but doesn't require decoding the first frame's pixels).
|
||||
pri func decoder.decode_id_part0?(src base.io_reader) {
|
||||
this.frame_rect_x0 = args.src.read_u16le?() as base.u32
|
||||
this.frame_rect_y0 = args.src.read_u16le?() as base.u32
|
||||
this.frame_rect_x1 = this.frame_rect_x0 ~mod+ (args.src.read_u16le?() as base.u32)
|
||||
this.frame_rect_y1 = this.frame_rect_y0 ~mod+ (args.src.read_u16le?() as base.u32)
|
||||
pri func decoder.decode_id_part0!??(src base.io_reader) {
|
||||
this.frame_rect_x0 = args.src.read_u16le!??() as base.u32
|
||||
this.frame_rect_y0 = args.src.read_u16le!??() as base.u32
|
||||
this.frame_rect_x1 = this.frame_rect_x0 ~mod+ (args.src.read_u16le!??() as base.u32)
|
||||
this.frame_rect_y1 = this.frame_rect_y0 ~mod+ (args.src.read_u16le!??() as base.u32)
|
||||
this.dst_x = this.frame_rect_x0
|
||||
this.dst_y = this.frame_rect_y0
|
||||
|
||||
@@ -497,7 +497,7 @@ pri func decoder.decode_id_part0?(src base.io_reader) {
|
||||
this.height = this.height.max(x:this.frame_rect_y1)
|
||||
}
|
||||
|
||||
var flags base.u8 = args.src.read_u8?()
|
||||
var flags base.u8 = args.src.read_u8!??()
|
||||
if (flags & 0x40) != 0 {
|
||||
this.interlace = 4
|
||||
} else {
|
||||
@@ -515,7 +515,7 @@ pri func decoder.decode_id_part0?(src base.io_reader) {
|
||||
assert i < 256 via "a < b: a < c; c <= b"(c:num_palette_entries)
|
||||
// Convert from RGB (in memory order) to ARGB (in native u32 order)
|
||||
// to BGRA (in memory order).
|
||||
var argb base.u32 = args.src.read_u24be?() | 0xFF000000
|
||||
var argb base.u32 = args.src.read_u24be!??() | 0xFF000000
|
||||
this.palettes[1][(4 * i) + 0] = ((argb >> 0) & 0xFF) as base.u8
|
||||
this.palettes[1][(4 * i) + 1] = ((argb >> 8) & 0xFF) as base.u8
|
||||
this.palettes[1][(4 * i) + 2] = ((argb >> 16) & 0xFF) as base.u8
|
||||
@@ -534,7 +534,7 @@ pri func decoder.decode_id_part0?(src base.io_reader) {
|
||||
}
|
||||
}
|
||||
|
||||
pri func decoder.decode_id_part1?(dst ptr base.pixel_buffer, src base.io_reader) {
|
||||
pri func decoder.decode_id_part1!??(dst ptr base.pixel_buffer, src base.io_reader) {
|
||||
if this.gc_has_transparent_index {
|
||||
if not this.use_local_palette {
|
||||
this.palettes[1][:].copy_from_slice(s:this.palettes[0][:])
|
||||
@@ -567,7 +567,7 @@ pri func decoder.decode_id_part1?(dst ptr base.pixel_buffer, src base.io_reader)
|
||||
this.lzw.reset()
|
||||
}
|
||||
|
||||
var lw base.u8 = args.src.read_u8?()
|
||||
var lw base.u8 = args.src.read_u8!??()
|
||||
if (lw < 2) or (8 < lw) {
|
||||
return error "bad literal width"
|
||||
}
|
||||
@@ -575,7 +575,7 @@ pri func decoder.decode_id_part1?(dst ptr base.pixel_buffer, src base.io_reader)
|
||||
|
||||
this.previous_lzw_decode_ended_abruptly = true
|
||||
while true {
|
||||
var block_size base.u64 = args.src.read_u8?() as base.u64
|
||||
var block_size base.u64 = args.src.read_u8!??() as base.u64
|
||||
if block_size == 0 {
|
||||
break
|
||||
}
|
||||
@@ -593,13 +593,13 @@ pri func decoder.decode_id_part1?(dst ptr base.pixel_buffer, src base.io_reader)
|
||||
// Should calling r.set_mark also require r.is_bound()?
|
||||
args.src.set_limit!(l:block_size)
|
||||
args.src.set_mark!()
|
||||
var z base.status = try this.lzw.decode?(dst:w, src:args.src)
|
||||
var z base.status = try this.lzw.decode!??(dst:w, src:args.src)
|
||||
this.uncompressed_wi = 4096 - (w.available().min(x:4096) as base.u32)
|
||||
block_size ~sat-= args.src.since_mark().length()
|
||||
}
|
||||
|
||||
if z.is_ok() or (z == suspension "short write") {
|
||||
this.copy_to_image_buffer?(pb:args.dst)
|
||||
this.copy_to_image_buffer!??(pb:args.dst)
|
||||
|
||||
if z == suspension "short write" {
|
||||
continue:inner
|
||||
@@ -616,13 +616,13 @@ pri func decoder.decode_id_part1?(dst ptr base.pixel_buffer, src base.io_reader)
|
||||
}
|
||||
|
||||
if this.uncompressed_ri != this.uncompressed_wi {
|
||||
this.copy_to_image_buffer?(pb:args.dst)
|
||||
this.copy_to_image_buffer!??(pb:args.dst)
|
||||
}
|
||||
|
||||
// TODO: check for "not enough pixel data".
|
||||
}
|
||||
|
||||
pri func decoder.copy_to_image_buffer?(pb ptr base.pixel_buffer) {
|
||||
pri func decoder.copy_to_image_buffer!??(pb ptr base.pixel_buffer) {
|
||||
// TODO: don't assume a packed pixel format.
|
||||
var dst slice base.u8
|
||||
var src slice base.u8
|
||||
|
||||
+14
-14
@@ -32,28 +32,28 @@ pub func decoder.set_ignore_checksum!(ic base.bool) {
|
||||
this.ignore_checksum = args.ic
|
||||
}
|
||||
|
||||
pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
|
||||
// Read the header.
|
||||
var c base.u8 = args.src.read_u8?()
|
||||
var c base.u8 = args.src.read_u8!??()
|
||||
if c != 0x1F {
|
||||
return error "bad header"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
c = args.src.read_u8!??()
|
||||
if c != 0x8B {
|
||||
return error "bad header"
|
||||
}
|
||||
c = args.src.read_u8?()
|
||||
c = args.src.read_u8!??()
|
||||
if c != 0x08 {
|
||||
return error "bad compression method"
|
||||
}
|
||||
var flags base.u8 = args.src.read_u8?()
|
||||
var flags base.u8 = args.src.read_u8!??()
|
||||
// TODO: API for returning the header's MTIME field.
|
||||
args.src.skip?(n:6)
|
||||
args.src.skip!??(n:6)
|
||||
|
||||
// Handle FEXTRA.
|
||||
if (flags & 0x04) != 0 {
|
||||
var xlen base.u16 = args.src.read_u16le?()
|
||||
args.src.skip?(n:xlen as base.u32)
|
||||
var xlen base.u16 = args.src.read_u16le!??()
|
||||
args.src.skip!??(n:xlen as base.u32)
|
||||
}
|
||||
|
||||
// Handle FNAME.
|
||||
@@ -63,7 +63,7 @@ pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
// filename length to NAME_MAX, which is 255.
|
||||
if (flags & 0x08) != 0 {
|
||||
while true {
|
||||
c = args.src.read_u8?()
|
||||
c = args.src.read_u8!??()
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
@@ -73,7 +73,7 @@ pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
// Handle FCOMMENT.
|
||||
if (flags & 0x10) != 0 {
|
||||
while true {
|
||||
c = args.src.read_u8?()
|
||||
c = args.src.read_u8!??()
|
||||
if c == 0 {
|
||||
break
|
||||
}
|
||||
@@ -82,7 +82,7 @@ pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
|
||||
// Handle FHCRC.
|
||||
if (flags & 0x02) != 0 {
|
||||
args.src.skip?(n:2)
|
||||
args.src.skip!??(n:2)
|
||||
}
|
||||
|
||||
// Reserved flags bits must be zero.
|
||||
@@ -95,7 +95,7 @@ pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
var decoded_length_got base.u32
|
||||
while true {
|
||||
args.dst.set_mark!()
|
||||
var z base.status = try this.flate.decode?(dst:args.dst, src:args.src)
|
||||
var z base.status = try this.flate.decode!??(dst:args.dst, src:args.src)
|
||||
if not this.ignore_checksum {
|
||||
checksum_got = this.checksum.update!(x:args.dst.since_mark())
|
||||
decoded_length_got ~mod+= ((args.dst.since_mark().length() & 0xFFFFFFFF) as base.u32)
|
||||
@@ -105,8 +105,8 @@ pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
}
|
||||
yield z
|
||||
}
|
||||
var checksum_want base.u32 = args.src.read_u32le?()
|
||||
var decoded_length_want base.u32 = args.src.read_u32le?()
|
||||
var checksum_want base.u32 = args.src.read_u32le!??()
|
||||
var decoded_length_want base.u32 = args.src.read_u32le!??()
|
||||
if (not this.ignore_checksum) and
|
||||
((checksum_got != checksum_want) or (decoded_length_got != decoded_length_want)) {
|
||||
return error "bad checksum"
|
||||
|
||||
@@ -28,7 +28,7 @@ pub func decoder.set_literal_width!(lw base.u32[2..8]) {
|
||||
this.literal_width = args.lw
|
||||
}
|
||||
|
||||
pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
|
||||
// These variables don't change over the lifetime of this func.
|
||||
var literal_width base.u32[2..8] = 8
|
||||
if this.literal_width >= 2 {
|
||||
@@ -59,7 +59,7 @@ pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
post n_bits >= width,
|
||||
{
|
||||
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
|
||||
bits |= (args.src.read_u8?() as base.u32) << n_bits
|
||||
bits |= (args.src.read_u8!??() as base.u32) << n_bits
|
||||
n_bits += 8
|
||||
}
|
||||
var code base.u32[..4095] = bits.low_bits(n:width)
|
||||
@@ -68,7 +68,7 @@ pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
|
||||
if code < clear_code {
|
||||
assert code < 256 via "a < b: a < c; c <= b"(c:clear_code)
|
||||
args.dst.write_u8?(x:code as base.u8)
|
||||
args.dst.write_u8!??(x:code as base.u8)
|
||||
if save_code <= 4095 {
|
||||
this.suffixes[save_code] = code as base.u8
|
||||
this.prefixes[save_code] = prev_code as base.u16
|
||||
|
||||
@@ -34,8 +34,8 @@ pub func decoder.set_ignore_checksum!(ic base.bool) {
|
||||
this.ignore_checksum = args.ic
|
||||
}
|
||||
|
||||
pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
var x base.u16 = args.src.read_u16be?()
|
||||
pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
|
||||
var x base.u16 = args.src.read_u16be!??()
|
||||
if ((x >> 8) & 0x0F) != 0x08 {
|
||||
return error "bad compression method"
|
||||
}
|
||||
@@ -53,7 +53,7 @@ pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
var checksum_got base.u32
|
||||
while true {
|
||||
args.dst.set_mark!()
|
||||
var z base.status = try this.flate.decode?(dst:args.dst, src:args.src)
|
||||
var z base.status = try this.flate.decode!??(dst:args.dst, src:args.src)
|
||||
if not this.ignore_checksum {
|
||||
checksum_got = this.checksum.update!(x:args.dst.since_mark())
|
||||
}
|
||||
@@ -62,7 +62,7 @@ pub func decoder.decode?(dst base.io_writer, src base.io_reader) {
|
||||
}
|
||||
yield z
|
||||
}
|
||||
var checksum_want base.u32 = args.src.read_u32be?()
|
||||
var checksum_want base.u32 = args.src.read_u32be!??()
|
||||
if (not this.ignore_checksum) and (checksum_got != checksum_want) {
|
||||
return error "bad checksum"
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user