Use !?? to denote coroutines

This commit is contained in:
Nigel Tao committed 2018-07-29 13:57:13 +10:00
1 parent 0a47966e72
commit 2100a9f837
8 files changed
+149 -149

No files matched your search

+64 -64
View File
@@ -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