Re-do image decoders' call sequence state machine

This commit is contained in:
Nigel Tao committed 2022-05-28 15:06:56 +10:00
1 parent a2e3a5a272
commit 8d03a67e6e
12 files changed
+606 -511

No files matched your search

+19 -40
View File
@@ -33,31 +33,8 @@ pub struct decoder? implements base.image_decoder(
width : base.u32[..= 0x7FFF_FFFF],
height : base.u32[..= 0x7FFF_FFFF],
// Call sequence states:
// - 0x00: initial state.
// - 0x03: image config decoded.
// - 0x04: frame config decoded.
// - 0xFF: end-of-data, usually after (the non-animated) frame decoded.
//
// State transitions:
//
// - 0x00 -> 0x03: via DIC
// - 0x00 -> 0x04: via DFC with implicit DIC
// - 0x00 -> 0xFF: via DF with implicit DIC and DFC
//
// - 0x03 -> 0x04: via DFC
// - 0x03 -> 0xFF: via DF with implicit DFC
//
// - 0x04 -> 0xFF: via DFC
// - 0x04 -> 0xFF: via DF
//
// - ???? -> 0x03: via RF for ???? > 0x00
//
// Where:
// - DF is decode_frame
// - DFC is decode_frame_config, implicit means nullptr args.dst
// - DIC is decode_image_config, implicit means nullptr args.dst
// - RF is restart_frame
// The call sequence state machine is discussed in
// (/doc/std/image-decoders-call-sequence.md).
call_sequence : base.u8,
top_down : base.bool,
@@ -115,7 +92,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
var dst_pixfmt : base.u32
var byte_width : base.u32
if (this.call_sequence <> 0) or (this.io_redirect_fourcc == 1) {
if (this.call_sequence <> 0x00) or (this.io_redirect_fourcc == 1) {
return base."#bad call sequence"
} else if this.io_redirect_fourcc <> 0 {
return base."@I/O redirect"
@@ -389,18 +366,20 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
first_frame_is_opaque: this.channel_masks[3] == 0)
}
this.call_sequence = 3
this.call_sequence = 0x20
}
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
if this.call_sequence < 3 {
if this.call_sequence == 0x20 {
// No-op.
} else if this.call_sequence < 0x20 {
this.decode_image_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 3 {
} else if this.call_sequence == 0x28 {
if this.frame_config_io_position <> args.src.position() {
return base."#bad restart"
}
} else if this.call_sequence == 4 {
this.call_sequence = 0xFF
} else if this.call_sequence == 0x40 {
this.call_sequence = 0x60
return base."@end of data"
} else {
return base."@end of data"
@@ -421,16 +400,16 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_
background_color: 0xFF00_0000)
}
this.call_sequence = 4
this.call_sequence = 0x40
}
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
var status : base.status
if this.call_sequence < 4 {
this.decode_frame_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 4 {
if this.call_sequence == 0x40 {
// No-op.
} else if this.call_sequence < 0x40 {
this.decode_frame_config?(dst: nullptr, src: args.src)
} else {
return base."@end of data"
}
@@ -480,7 +459,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
this.pending_pad = 0
}
this.call_sequence = 0xFF
this.call_sequence = 0x60
}
pri func decoder.swizzle_none!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status {
@@ -1116,27 +1095,27 @@ pub func decoder.num_animation_loops() base.u32 {
}
pub func decoder.num_decoded_frame_configs() base.u64 {
if this.call_sequence > 3 {
if this.call_sequence > 0x20 {
return 1
}
return 0
}
pub func decoder.num_decoded_frames() base.u64 {
if this.call_sequence > 4 {
if this.call_sequence > 0x40 {
return 1
}
return 0
}
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
if this.call_sequence < 3 {
if this.call_sequence < 0x20 {
return base."#bad call sequence"
}
if args.index <> 0 {
return base."#bad argument"
}
this.call_sequence = 3
this.call_sequence = 0x28
this.frame_config_io_position = args.io_position
return ok
}
+54 -84
View File
@@ -16,50 +16,13 @@ pub struct decoder? implements base.image_decoder(
width : base.u32,
height : base.u32,
// Call sequence states:
// - 0x00: initial state.
// - 0x01: metadata reported; image config decode is in progress.
// - 0x02: metadata finished; image config decode is in progress.
// - 0x03: image config decoded, including the first frame's bounds, but
// not the first frame's pixels.
// - 0x04: frame config decoded.
// - 0x05: frame decoded.
// The call sequence state machine is discussed in
// (/doc/std/image-decoders-call-sequence.md).
//
// GIF is unusual (distinguishing state 0x03 / state 0x05) in that the
// first frame's bounds can affect the overall image bounds (as per
// A GIF's first frame's bounds affects the overall image bounds (per
// test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt).
// Decoding the first frame's bounds is part of DIC even though,
// conceptually, it should be part of the first DFC.
//
// State transitions:
//
// - 0x00 -> 0x01: via DIC (metadata reported)
// - 0x00 -> 0x03: via DIC (metadata not reported)
// - 0x00 -> 0x04: via DFC with implicit DIC
// - 0x00 -> 0x05: via DF with implicit DIC and DFC
//
// - 0x01 -> 0x02: via TMM
//
// - 0x02 -> 0x01: via DIC (metadata reported)
// - 0x02 -> 0x03: via DIC (metadata not reported)
//
// - 0x03 -> 0x04: via DFC
// - 0x03 -> 0x05: via DF with implicit DFC
//
// - 0x04 -> 0x04: via DFC with implicit DF
// - 0x04 -> 0x05: via DF
//
// - 0x05 -> 0x04: via DFC
// - 0x05 -> 0x05: via DF with implicit DFC
//
// - ???? -> 0x05: via RF for ???? > 0x00
//
// Where:
// - DF is decode_frame, implicit means skip_frame
// - DFC is decode_frame_config, implicit means nullptr args.dst
// - DIC is decode_image_config, implicit means nullptr args.dst
// - RF is restart_frame
// - TMM is tell_me_more
// Decoding the first frame's bounds is part of decode_image_config even
// though it's conceptually part of the first decode_frame_config.
call_sequence : base.u8,
ignore_metadata : base.bool,
@@ -71,9 +34,8 @@ pub struct decoder? implements base.image_decoder(
quirks : array[QUIRKS_COUNT] base.bool,
delayed_num_decoded_frames : base.bool,
end_of_data : base.bool,
restarted : base.bool,
previous_lzw_decode_ended_abruptly : base.bool,
seen_header : base.bool,
has_global_palette : base.bool,
@@ -128,7 +90,7 @@ pub struct decoder? implements base.image_decoder(
)
pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
if (this.call_sequence == 0) and (args.quirk >= QUIRKS_BASE) {
if (this.call_sequence == 0x00) and (args.quirk >= QUIRKS_BASE) {
args.quirk -= QUIRKS_BASE
if args.quirk < QUIRKS_COUNT {
this.quirks[args.quirk] = args.enabled
@@ -139,17 +101,16 @@ pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
var ffio : base.bool
if this.call_sequence == 0 {
if this.call_sequence <> 0x00 {
return base."#bad call sequence"
} else if not this.seen_header {
this.decode_header?(src: args.src)
this.decode_lsd?(src: args.src)
} else if this.call_sequence <> 2 {
return base."#bad call sequence"
this.seen_header = true
}
this.decode_up_to_id_part1?(src: args.src)
// TODO: if this.end_of_data, return an error and/or set dst to zero?
ffio = not this.gc_has_transparent_index
if not this.quirks[QUIRK_HONOR_BACKGROUND_COLOR - QUIRKS_BASE] {
ffio = ffio and
@@ -176,7 +137,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
first_frame_is_opaque: ffio)
}
this.call_sequence = 3
this.call_sequence = 0x20
}
pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) {
@@ -190,7 +151,7 @@ pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) {
pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) {
var chunk_length : base.u64
if this.call_sequence <> 1 {
if (this.call_sequence & 0x10) == 0 {
return base."#bad call sequence"
}
if this.metadata_fourcc == 0 {
@@ -267,7 +228,7 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform
y: this.metadata_io_position,
z: this.metadata_io_position)
}
this.call_sequence = 2
this.call_sequence &= 0xEF
this.metadata_fourcc = 0
this.metadata_io_position = 0
return ok
@@ -319,16 +280,17 @@ pub func decoder.workbuf_len() base.range_ii_u64 {
}
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
if this.call_sequence < 3 {
if this.call_sequence < 0x20 {
return base."#bad call sequence"
} else if args.io_position == 0 {
return base."#bad argument"
}
this.delayed_num_decoded_frames = false
this.end_of_data = false
this.restarted = true
this.frame_config_io_position = args.io_position
this.num_decoded_frame_configs_value = args.index
this.num_decoded_frames_value = args.index
this.reset_gc!()
this.call_sequence = 0x28
return ok
}
@@ -340,21 +302,30 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_
this.dirty_max_excl_y = 0
if not this.end_of_data {
if this.call_sequence == 0 {
this.decode_image_config?(dst: nullptr, src: args.src)
} else if this.call_sequence <> 3 {
if this.call_sequence == 4 {
this.skip_frame?(src: args.src)
}
this.decode_up_to_id_part1?(src: args.src)
if (this.call_sequence & 0x10) <> 0 {
return base."#bad call sequence"
} else if this.call_sequence == 0x20 {
// No-op.
} else if this.call_sequence < 0x20 {
this.decode_image_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 0x28 {
if this.frame_config_io_position <> args.src.position() {
return base."#bad restart"
}
} else if this.call_sequence == 0x40 {
this.skip_frame?(src: args.src)
if this.call_sequence >= 0x60 {
return base."@end of data"
}
} else {
return base."@end of data"
}
// This is a new "if", not an "else", because the calls above can modify
// this.end_of_data.
if this.end_of_data {
return base."@end of data"
if (this.num_decoded_frame_configs_value > 0) or (this.call_sequence == 0x28) {
this.decode_up_to_id_part1?(src: args.src)
if this.call_sequence >= 0x60 {
return base."@end of data"
}
}
background_color = this.black_color_u32_argb_premul
@@ -396,7 +367,7 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_
}
this.num_decoded_frame_configs_value ~sat+= 1
this.call_sequence = 4
this.call_sequence = 0x40
}
pri func decoder.skip_frame?(src: base.io_reader) {
@@ -424,13 +395,18 @@ pri func decoder.skip_frame?(src: base.io_reader) {
this.num_decoded_frames_value ~sat+= 1
}
this.reset_gc!()
this.call_sequence = 0x20
}
// TODO: honor args.opts.
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
this.ignore_metadata = true
if this.call_sequence <> 4 {
if this.call_sequence == 0x40 {
// No-op.
} else if this.call_sequence < 0x40 {
this.decode_frame_config?(dst: nullptr, src: args.src)
} else {
return base."@end of data"
}
if this.quirks[QUIRK_REJECT_EMPTY_FRAME - QUIRKS_BASE] and
((this.frame_rect_x0 == this.frame_rect_x1) or (this.frame_rect_y0 == this.frame_rect_y1)) {
@@ -441,10 +417,10 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
this.num_decoded_frames_value ~sat+= 1
this.reset_gc!()
this.call_sequence = 0x20
}
pri func decoder.reset_gc!() {
this.call_sequence = 5
// The Image Descriptor is mandatory, but the Graphic Control extension is
// optional. Reset the GC related fields for the next decode_frame call.
this.gc_has_transparent_index = false
@@ -456,14 +432,8 @@ pri func decoder.reset_gc!() {
pri func decoder.decode_up_to_id_part1?(src: base.io_reader) {
var block_type : base.u8
if not this.restarted {
if this.call_sequence <> 2 {
this.frame_config_io_position = args.src.position()
}
} else if this.frame_config_io_position <> args.src.position() {
return base."#bad restart"
} else {
this.restarted = false
if (this.frame_config_io_position == 0) or (this.num_decoded_frame_configs_value > 0) {
this.frame_config_io_position = args.src.position()
}
while true {
@@ -494,7 +464,7 @@ pri func decoder.decode_up_to_id_part1?(src: base.io_reader) {
this.delayed_num_decoded_frames = false
this.num_decoded_frames_value ~sat+= 1
}
this.end_of_data = true
this.call_sequence = 0x60
break
}
} endwhile
@@ -698,13 +668,13 @@ pri func decoder.decode_ae?(src: base.io_reader) {
} else if is_iccp and this.report_metadata_iccp {
this.metadata_fourcc = 'ICCP'be
this.metadata_io_position = args.src.position()
this.call_sequence = 1
this.call_sequence = 0x10
return base."@metadata reported"
} else if is_xmp and this.report_metadata_xmp {
this.metadata_fourcc = 'XMP 'be
this.metadata_io_position = args.src.position()
this.call_sequence = 1
this.call_sequence = 0x10
return base."@metadata reported"
}
@@ -759,7 +729,7 @@ pri func decoder.decode_gc?(src: base.io_reader) {
//
// See the spec section 20 "Image Descriptor" on page 11.
//
// The code is split into three parts (part0, part 1 and part12 because
// The code is split into three parts (part0, part 1 and part2 because
// determining the overall image's width and height also requires decoding the
// first frame's bounds (part0), but doesn't require decoding the first frame's
// pixels (the other two parts). Decoding the actual pixels is split into two
@@ -782,7 +752,7 @@ pri func decoder.decode_id_part0?(src: base.io_reader) {
// and the bottom right extent of the first frame. See
// test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt for
// more discussion.
if (this.call_sequence == 0) and (not this.quirks[QUIRK_IMAGE_BOUNDS_ARE_STRICT - QUIRKS_BASE]) {
if (this.num_decoded_frame_configs_value == 0) and (not this.quirks[QUIRK_IMAGE_BOUNDS_ARE_STRICT - QUIRKS_BASE]) {
this.width = this.width.max(a: this.frame_rect_x1)
this.height = this.height.max(a: this.frame_rect_y1)
}
+19 -40
View File
@@ -24,31 +24,8 @@ pub struct decoder? implements base.image_decoder(
width : base.u32[..= 0x7FFF_FFFF],
height : base.u32[..= 0x7FFF_FFFF],
// Call sequence states:
// - 0x00: initial state.
// - 0x03: image config decoded.
// - 0x04: frame config decoded.
// - 0xFF: end-of-data, usually after (the non-animated) frame decoded.
//
// State transitions:
//
// - 0x00 -> 0x03: via DIC
// - 0x00 -> 0x04: via DFC with implicit DIC
// - 0x00 -> 0xFF: via DF with implicit DIC and DFC
//
// - 0x03 -> 0x04: via DFC
// - 0x03 -> 0xFF: via DF with implicit DFC
//
// - 0x04 -> 0xFF: via DFC
// - 0x04 -> 0xFF: via DF
//
// - ???? -> 0x03: via RF for ???? > 0x00
//
// Where:
// - DF is decode_frame
// - DFC is decode_frame_config, implicit means nullptr args.dst
// - DIC is decode_image_config, implicit means nullptr args.dst
// - RF is restart_frame
// The call sequence state machine is discussed in
// (/doc/std/image-decoders-call-sequence.md).
call_sequence : base.u8,
dst_x : base.u32,
@@ -64,7 +41,7 @@ pub func decoder.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_reader) {
var a : base.u32
if this.call_sequence <> 0 {
if this.call_sequence <> 0x00 {
return base."#bad call sequence"
}
@@ -108,18 +85,20 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
first_frame_is_opaque: false)
}
this.call_sequence = 3
this.call_sequence = 0x20
}
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
if this.call_sequence < 3 {
if this.call_sequence == 0x20 {
// No-op.
} else if this.call_sequence < 0x20 {
this.decode_image_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 3 {
} else if this.call_sequence == 0x28 {
if 16 <> args.src.position() {
return base."#bad restart"
}
} else if this.call_sequence == 4 {
this.call_sequence = 0xFF
} else if this.call_sequence == 0x40 {
this.call_sequence = 0x60
return base."@end of data"
} else {
return base."@end of data"
@@ -140,16 +119,16 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_
background_color: 0x0000_0000)
}
this.call_sequence = 4
this.call_sequence = 0x40
}
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
var status : base.status
if this.call_sequence < 4 {
this.decode_frame_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 4 {
if this.call_sequence == 0x40 {
// No-op.
} else if this.call_sequence < 0x40 {
this.decode_frame_config?(dst: nullptr, src: args.src)
} else {
return base."@end of data"
}
@@ -177,7 +156,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
yield? base."$short read"
} endwhile
this.call_sequence = 0xFF
this.call_sequence = 0x60
}
pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: base.io_reader) base.status {
@@ -246,27 +225,27 @@ pub func decoder.num_animation_loops() base.u32 {
}
pub func decoder.num_decoded_frame_configs() base.u64 {
if this.call_sequence > 3 {
if this.call_sequence > 0x20 {
return 1
}
return 0
}
pub func decoder.num_decoded_frames() base.u64 {
if this.call_sequence > 4 {
if this.call_sequence > 0x40 {
return 1
}
return 0
}
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
if this.call_sequence < 3 {
if this.call_sequence < 0x20 {
return base."#bad call sequence"
}
if (args.index <> 0) or (args.io_position <> 16) {
return base."#bad argument"
}
this.call_sequence = 3
this.call_sequence = 0x28
return ok
}
+35 -70
View File
@@ -33,49 +33,8 @@ pub struct decoder? implements base.image_decoder(
overall_workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007],
pass_workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007],
// Call sequence states:
// - 0x00: initial state.
// - 0x01: metadata reported; image config decode is in progress.
// - 0x02: metadata finished; image config decode is in progress.
// - 0x03: image config decoded.
// - 0x04: frame config decoded.
// - 0x05: frame decoded.
// - 0xFF: end-of-data, after the final frame decoded.
//
// State transitions:
//
// - 0x00 -> 0x01: via DIC (metadata reported)
// - 0x00 -> 0x03: via DIC (metadata not reported)
// - 0x00 -> 0x04: via DFC with implicit DIC
// - 0x00 -> 0x05: via DF with implicit DIC and DFC
// - 0x00 -> 0xFF: via DF with implicit DIC and DFC
//
// - 0x01 -> 0x02: via TMM
//
// - 0x02 -> 0x01: via DIC (metadata reported)
// - 0x02 -> 0x03: via DIC (metadata not reported)
//
// - 0x03 -> 0x04: via DFC
// - 0x03 -> 0x05: via DF with implicit DFC
// - 0x03 -> 0xFF: via DF with implicit DFC
//
// - 0x04 -> 0x04: via DFC with implicit DF
// - 0x04 -> 0x05: via DF
// - 0x04 -> 0xFF: via DFC with implicit DF
// - 0x04 -> 0xFF: via DF
//
// - 0x05 -> 0x04: via DFC
// - 0x05 -> 0x05: via DF with implicit DFC
// - 0x05 -> 0xFF: via DF with implicit DFC
//
// - ???? -> 0x03: via RF for ???? > 0x00
//
// Where:
// - DF is decode_frame, implicit means skip_frame
// - DFC is decode_frame_config, implicit means nullptr args.dst
// - DIC is decode_image_config, implicit means nullptr args.dst
// - RF is restart_frame
// - TMM is tell_me_more
// The call sequence state machine is discussed in
// (/doc/std/image-decoders-call-sequence.md).
call_sequence : base.u8,
report_metadata_chrm : base.bool,
@@ -99,6 +58,7 @@ pub struct decoder? implements base.image_decoder(
seen_gama : base.bool,
seen_iccp : base.bool,
seen_idat : base.bool,
seen_ihdr : base.bool,
seen_plte : base.bool,
seen_srgb : base.bool,
seen_trns : base.bool,
@@ -209,14 +169,9 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
var checksum_want : base.u32
var status : base.status
if this.call_sequence == 2 {
if this.metadata_fourcc <> 0 {
this.call_sequence = 1
return base."@metadata reported"
}
} else if this.call_sequence <> 0 {
if this.call_sequence <> 0x00 {
return base."#bad call sequence"
} else {
} else if not this.seen_ihdr {
magic = args.src.read_u64le?()
if magic <> '\x89PNG\x0D\x0A\x1A\x0A'le {
return "#bad header"
@@ -255,6 +210,10 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
if (not this.ignore_checksum) and (checksum_have <> checksum_want) {
return "#bad checksum"
}
this.seen_ihdr = true
} else if this.metadata_fourcc <> 0 {
this.call_sequence = 0x10
return base."@metadata reported"
}
// Read up until an IDAT or fdAT chunk.
@@ -318,7 +277,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
// If we have metadata, delay reading (skipping) the ancillary chunk's
// CRC-32 checksum until the end of tell_me_more.
if this.metadata_fourcc <> 0 {
this.call_sequence = 1
this.call_sequence = 0x10
return base."@metadata reported"
}
@@ -359,7 +318,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
this.first_overwrite_instead_of_blend = false
}
this.call_sequence = 3
this.call_sequence = 0x20
}
pri func decoder.decode_ihdr?(src: base.io_reader) {
@@ -969,19 +928,23 @@ pri func decoder.decode_trns?(src: base.io_reader) {
}
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
if this.call_sequence == 0xFF {
return base."@end of data"
} else if this.call_sequence < 3 {
if (this.call_sequence & 0x10) <> 0 {
return base."#bad call sequence"
} else if this.call_sequence == 0x20 {
// No-op.
} else if this.call_sequence < 0x20 {
this.decode_image_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 3 {
} else if this.call_sequence == 0x28 {
if this.frame_config_io_position <> args.src.position() {
return base."#bad restart"
}
} else if this.call_sequence == 4 {
} else if this.call_sequence == 0x40 {
this.skip_frame?(src: args.src)
if this.call_sequence == 0xFF {
if this.call_sequence >= 0x60 {
return base."@end of data"
}
} else {
return base."@end of data"
}
if this.num_decoded_frame_configs_value == 0 {
@@ -1028,7 +991,7 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_
}
this.num_decoded_frame_configs_value ~sat+= 1
this.call_sequence = 4
this.call_sequence = 0x40
}
pri func decoder.skip_frame?(src: base.io_reader) {
@@ -1099,9 +1062,9 @@ pri func decoder.skip_frame?(src: base.io_reader) {
this.num_decoded_frames_value ~sat+= 1
if this.num_decoded_frames_value < this.num_animation_frames_value {
this.call_sequence = 5
this.call_sequence = 0x20
} else {
this.call_sequence = 0xFF
this.call_sequence = 0x60
}
}
@@ -1111,9 +1074,11 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
var pass_width : base.u32[..= 0x00FF_FFFF]
var pass_height : base.u32[..= 0x00FF_FFFF]
if this.call_sequence == 0xFF {
if (this.call_sequence & 0x10) <> 0 {
return base."#bad call sequence"
} else if this.call_sequence >= 0x60 {
return base."@end of data"
} else if this.call_sequence <> 4 {
} else if this.call_sequence <> 0x40 {
this.decode_frame_config?(dst: nullptr, src: args.src)
}
@@ -1243,9 +1208,9 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
this.num_decoded_frames_value ~sat+= 1
if this.num_decoded_frames_value < this.num_animation_frames_value {
this.call_sequence = 5
this.call_sequence = 0x20
} else {
this.call_sequence = 0xFF
this.call_sequence = 0x60
}
}
@@ -1374,13 +1339,13 @@ pub func decoder.num_decoded_frames() base.u64 {
}
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
if this.call_sequence < 3 {
if this.call_sequence < 0x20 {
return base."#bad call sequence"
} else if (args.index >= (this.num_animation_frames_value as base.u64)) or
((args.index == 0) and (args.io_position <> this.first_config_io_position)) {
return base."#bad argument"
}
this.call_sequence = 3
this.call_sequence = 0x28
if this.interlace_pass >= 1 {
this.interlace_pass = 1
}
@@ -1415,7 +1380,7 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform
var r_mark : base.u64
var zlib_status : base.status
if this.call_sequence <> 1 {
if (this.call_sequence & 0x10) == 0 {
return base."#bad call sequence"
}
if this.metadata_fourcc == 0 {
@@ -1677,7 +1642,7 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform
this.metadata_is_zlib_compressed = true
}
this.call_sequence = 2
this.call_sequence &= 0xEF
return ok
}
break.goto_done
@@ -1696,7 +1661,7 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform
this.metadata_y = 0
this.metadata_z = 0
this.call_sequence = 2
this.call_sequence &= 0xEF
return ok
}
+19 -40
View File
@@ -22,31 +22,8 @@ pub struct decoder? implements base.image_decoder(
width : base.u32[..= 0xFFFF],
height : base.u32[..= 0xFFFF],
// Call sequence states:
// - 0x00: initial state.
// - 0x03: image config decoded.
// - 0x04: frame config decoded.
// - 0xFF: end-of-data, usually after (the non-animated) frame decoded.
//
// State transitions:
//
// - 0x00 -> 0x03: via DIC
// - 0x00 -> 0x04: via DFC with implicit DIC
// - 0x00 -> 0xFF: via DF with implicit DIC and DFC
//
// - 0x03 -> 0x04: via DFC
// - 0x03 -> 0xFF: via DF with implicit DFC
//
// - 0x04 -> 0xFF: via DFC
// - 0x04 -> 0xFF: via DF
//
// - ???? -> 0x03: via RF for ???? > 0x00
//
// Where:
// - DF is decode_frame
// - DFC is decode_frame_config, implicit means nullptr args.dst
// - DIC is decode_image_config, implicit means nullptr args.dst
// - RF is restart_frame
// The call sequence state machine is discussed in
// (/doc/std/image-decoders-call-sequence.md).
call_sequence : base.u8,
header_id_length : base.u8,
@@ -82,7 +59,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
var c5 : base.u32[..= 0x1F]
var i : base.u32
if this.call_sequence <> 0 {
if this.call_sequence <> 0x00 {
return base."#bad call sequence"
}
@@ -245,18 +222,20 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
first_frame_is_opaque: this.opaque)
}
this.call_sequence = 3
this.call_sequence = 0x20
}
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
if this.call_sequence < 3 {
if this.call_sequence == 0x20 {
// No-op.
} else if this.call_sequence < 0x20 {
this.decode_image_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 3 {
} else if this.call_sequence == 0x28 {
if this.frame_config_io_position <> args.src.position() {
return base."#bad restart"
}
} else if this.call_sequence == 4 {
this.call_sequence = 0xFF
} else if this.call_sequence == 0x40 {
this.call_sequence = 0x60
return base."@end of data"
} else {
return base."@end of data"
@@ -277,7 +256,7 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_
background_color: 0xFF00_0000)
}
this.call_sequence = 4
this.call_sequence = 0x40
}
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
@@ -302,10 +281,10 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
var c : base.u32
var c5 : base.u32[..= 0x1F]
if this.call_sequence < 4 {
this.decode_frame_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 4 {
if this.call_sequence == 0x40 {
// No-op.
} else if this.call_sequence < 0x40 {
this.decode_frame_config?(dst: nullptr, src: args.src)
} else {
return base."@end of data"
}
@@ -545,7 +524,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
break.resume
} endwhile.resume
this.call_sequence = 0xFF
this.call_sequence = 0x60
}
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
@@ -561,27 +540,27 @@ pub func decoder.num_animation_loops() base.u32 {
}
pub func decoder.num_decoded_frame_configs() base.u64 {
if this.call_sequence > 3 {
if this.call_sequence > 0x20 {
return 1
}
return 0
}
pub func decoder.num_decoded_frames() base.u64 {
if this.call_sequence > 4 {
if this.call_sequence > 0x40 {
return 1
}
return 0
}
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
if this.call_sequence < 3 {
if this.call_sequence < 0x20 {
return base."#bad call sequence"
}
if args.index <> 0 {
return base."#bad argument"
}
this.call_sequence = 3
this.call_sequence = 0x28
this.frame_config_io_position = args.io_position
return ok
}
+19 -40
View File
@@ -20,31 +20,8 @@ pub struct decoder? implements base.image_decoder(
width : base.u32,
height : base.u32,
// Call sequence states:
// - 0x00: initial state.
// - 0x03: image config decoded.
// - 0x04: frame config decoded.
// - 0xFF: end-of-data, usually after (the non-animated) frame decoded.
//
// State transitions:
//
// - 0x00 -> 0x03: via DIC
// - 0x00 -> 0x04: via DFC with implicit DIC
// - 0x00 -> 0xFF: via DF with implicit DIC and DFC
//
// - 0x03 -> 0x04: via DFC
// - 0x03 -> 0xFF: via DF with implicit DFC
//
// - 0x04 -> 0xFF: via DFC
// - 0x04 -> 0xFF: via DF
//
// - ???? -> 0x03: via RF for ???? > 0x00
//
// Where:
// - DF is decode_frame
// - DFC is decode_frame_config, implicit means nullptr args.dst
// - DIC is decode_image_config, implicit means nullptr args.dst
// - RF is restart_frame
// The call sequence state machine is discussed in
// (/doc/std/image-decoders-call-sequence.md).
call_sequence : base.u8,
frame_config_io_position : base.u64,
@@ -62,7 +39,7 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
var x32 : base.u32
var x64 : base.u64
if this.call_sequence <> 0 {
if this.call_sequence <> 0x00 {
return base."#bad call sequence"
}
@@ -116,18 +93,20 @@ pub func decoder.decode_image_config?(dst: nptr base.image_config, src: base.io_
first_frame_is_opaque: true)
}
this.call_sequence = 3
this.call_sequence = 0x20
}
pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) {
if this.call_sequence < 3 {
if this.call_sequence == 0x20 {
// No-op.
} else if this.call_sequence < 0x20 {
this.decode_image_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 3 {
} else if this.call_sequence == 0x28 {
if this.frame_config_io_position <> args.src.position() {
return base."#bad restart"
}
} else if this.call_sequence == 4 {
this.call_sequence = 0xFF
} else if this.call_sequence == 0x40 {
this.call_sequence = 0x60
return base."@end of data"
} else {
return base."@end of data"
@@ -148,7 +127,7 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_
background_color: 0xFF00_0000)
}
this.call_sequence = 4
this.call_sequence = 0x40
}
pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) {
@@ -164,10 +143,10 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
var src : array[1] base.u8
var c : base.u8
if this.call_sequence < 4 {
this.decode_frame_config?(dst: nullptr, src: args.src)
} else if this.call_sequence == 4 {
if this.call_sequence == 0x40 {
// No-op.
} else if this.call_sequence < 0x40 {
this.decode_frame_config?(dst: nullptr, src: args.src)
} else {
return base."@end of data"
}
@@ -246,7 +225,7 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader,
} endwhile
}
this.call_sequence = 0xFF
this.call_sequence = 0x60
}
pub func decoder.frame_dirty_rect() base.rect_ie_u32 {
@@ -262,27 +241,27 @@ pub func decoder.num_animation_loops() base.u32 {
}
pub func decoder.num_decoded_frame_configs() base.u64 {
if this.call_sequence > 3 {
if this.call_sequence > 0x20 {
return 1
}
return 0
}
pub func decoder.num_decoded_frames() base.u64 {
if this.call_sequence > 4 {
if this.call_sequence > 0x40 {
return 1
}
return 0
}
pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status {
if this.call_sequence < 3 {
if this.call_sequence < 0x20 {
return base."#bad call sequence"
}
if args.index <> 0 {
return base."#bad argument"
}
this.call_sequence = 3
this.call_sequence = 0x28
this.frame_config_io_position = args.io_position
return ok
}