mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 00:47:28 +01:00
Re-do image decoders' call sequence state machine
This commit is contained in:
1 parent
a2e3a5a272
commit
8d03a67e6e
12 files changed
+606
-511
No files matched your search
+54
-84
@@ -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)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user