diff --git a/doc/std/image-decoders-call-sequence-diagram.png b/doc/std/image-decoders-call-sequence-diagram.png new file mode 100644 index 00000000..cf4816b6 Binary files /dev/null and b/doc/std/image-decoders-call-sequence-diagram.png differ diff --git a/doc/std/image-decoders-call-sequence-diagram.svg b/doc/std/image-decoders-call-sequence-diagram.svg new file mode 100644 index 00000000..f5123d35 --- /dev/null +++ b/doc/std/image-decoders-call-sequence-diagram.svg @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/doc/std/image-decoders-call-sequence.md b/doc/std/image-decoders-call-sequence.md new file mode 100644 index 00000000..884b26d0 --- /dev/null +++ b/doc/std/image-decoders-call-sequence.md @@ -0,0 +1,118 @@ +# Image Decoders Call Sequence + +As discussed in the [Image Decoders](./image-decoders.md) document, here's the +canonical sequence of API calls to decode an animated image (with N frames): + +1. DIC (`decode_image_config`) +1. DFC (`decode_frame_config`) #0 +1. DF (`decode_frame`) #0 +1. DFC (`decode_frame_config`) #1 +1. DF (`decode_frame`) #1 +1. ... +1. DFC (`decode_frame_config`) #N-1 +1. DF (`decode_frame`) #N-1 +1. DFC (`decode_frame_config`) #N returning an `"@end of data"` status value. + +The final DFC call returns `"@end of data"` even if the animation loops. + +Decoding a still (non-animated) image is equivalent to N being 1. + +These three methods (DIC, DFC, DF) are all +[coroutines](/doc/note/coroutines.md) and may return I/O related suspension +status values. Those coroutines should be resumed by calling the same method +again, before moving to the next one in the sequence. + +Encountering an error (such as the input not being valid in that image file +format) is sticky (any further calls will return that same error). + + +## Implicit Calls + +These three methods (DIC, DFC, DF) are all also optional in some sense. +Referring to the ordered list at the top of this page, any `decode_etc` call +jumps forward to the next matching method in that canonical sequence, +implicitly calling any jumped over method with the same `io_reader` but null +values for other arguments. + +For example, starting with a freshly initialized image decoder, if it's a first +party image and you already know its width, height and pixel format (so you can +prepare a perfectly sized pixel buffer without having to call DIC or DFC), it +is valid to call DF, going straight to the DF #0 stage. This implicitly calls +DIC and DFC #0. + +Even if you didn't know the width, height and pixel format, it is still valid +to call DF with a smaller-dimensioned pixel buffer (which just means the +decoded image will be clipped) or with a different pixel format (the decoder +will convert). + +After that, it is valid to call DFC, going to the DFC #1 stage (or get "@end of +data" for still images), which would not make any implicit calls. + +After that, it is valid to call DFC, going to the DFC #2 stage, implicitly +calling DF #1. + +And so on. + +For example, starting at the DF #N-1 stage, it is valid to call DF again, which +will return "@end of data" because it first implicitly calls DF #N, which +returns "@end of data". + +Implicitly calling DF (skipping over the pixel data) is typically much faster +than explicitly calling DF (decompressing the pixel data). For example, +starting with a freshly initialized image decoder, if you just want to *count* +the number of animation frames, without needing each frame's decoded pixels, +just call DFC (and not DF) in a loop until "#end of data". + + +## State Machine + +It's not part of Wuffs' public API, but Wuffs' standard library's image decoder +implementations all have a private `call_sequence` field to track this state +machine. Broadly speaking, an image decoder starts in an initial state (labeled +`0x00`). The DIC call moves to state `0x20`. Subsequent DFC and DF calls bounce +between states `0x40` and `0x20` until the last DFC moves the final state +`0x60`. These are the bold-labeled states in the left hand column of the +diagram below: + +![Image Decoders Call Sequence +Diagram](./image-decoders-call-sequence-diagram.png) + +The original Google Docs document for that diagram [is +here](https://docs.google.com/document/d/1bbH9cpgxsMP8M5U4NsTotr-wSlyH85tiDKSJSF1HVXc/edit?usp=sharing). + +The italic-labeled states (whose numeric labels all have the `0x10` bit set) in +the right hand column are side-track states when [metadata](./metadata.md) is +encountered, and are discussed below. If the caller never opts in to seeing +metadata (opting in by calling `set_report_metadata`) then the decoder never +enters these `(base_value | 0x10)` states. + +The `0x08` bit is used when calling `restart_frame`, discussed below. The low +three bits are reserved but currently unused. + +The primary purposes of the `call_sequence` field is track when we've reached +"@end of data" and to reject method calls that occur out of sequence. Such +method calls result in the `"#bad call sequence"` error status value. For +example, a DIC call is illegal after a DIC, DFC or DF call. Similarly, a TMM +(`tell_me_more`) call is illegal unless the decoder is in a right hand column +state. + + +## Restarts + +Image decoding typically runs sequentially: the first frame (#0) is decoded, +then the second (#1), then the third (#2), etc. However, decoding can sometimes +happen in random access instead of sequential order. Playing a looping +animation may require rewinding back to the first frame. Replaying an animation +may be able to re-use previousy cached frames at times, so a decoder may be +asked to jump ahead. While Wuffs is designed for sequential decoding (as it +does not assume that its input byte stream is rewindable or seekable), using +Wuffs to implement other graphics APIs can involve [random access to animation +frames](https://github.com/google/skia/blob/ee6f41262b2ef86b5f06b33473379ac887401451/include/codec/SkCodec.h#L314-L319). + +The RF `restart_frame` method supports these usage patterns, setting up the +decoder so that it's just about to decode the I'th frame configuration (when +the `io_reader` is also placed at `io_position` previously returned by the I'th +DFC). + +The RF method can only be called when the image configuration has already been +decoded. Equivalently, only when the `call_sequence` is at least `0x20`. diff --git a/doc/std/image-decoders.md b/doc/std/image-decoders.md index 6a59abac..74f9fb6a 100644 --- a/doc/std/image-decoders.md +++ b/doc/std/image-decoders.md @@ -61,7 +61,9 @@ will implicitly parse and verify the image header, before parsing the first frame's header. Similarly, you can call only `decode_frame` N times, without calling `decode_image_config` or `decode_frame_config`, if you already know metadata like N and each frame's rectangle bounds by some other means (e.g. -this is a first party, statically known image). +this is a first party, statically known image). The [Image Decoders Call +Sequence](./image-decoders-call-sequence.md) document has more implementation +information. Specifically, starting with an unknown (but re-windable) animated image, if you want to just find N (i.e. count the number of frames), you can loop calling @@ -89,52 +91,8 @@ associated stream will also need to be rewound. ## Metadata -Images can also contain metadata (e.g. color profiles, time stamps). By -default, Wuffs' image decoders skip over metadata, but calling -`set_report_metadata` will opt in to having `decode_image_config` return early -when encountering metadata in the file. Calling `set_report_metadata` can be -done multiple times, each with a different -[FourCC](/doc/note/base38-and-fourcc.md) code such as `0x49434350` "ICCP" or -`0x584D5020` "XMP ", to indicate what sorts of metadata the caller is -interested in. Conversely, when the parser encounters metadata (and returns a -"@metadata reported" [status](/doc/note/statuses.md)), call `tell_me_more` to -see what sort of metadata it is. - -Some metadata is short and `tell_me_more` will also fill in its `minfo: nptr -more_information` out parameter with "parsed metadata". For example, Gamma -Correction metadata is a single numerical value and Primary Chromaticities -metadata is only eight numbers. From C++ code, call the `metadata_parsed__gama` -or `metadata_parsed__chrm` methods to retrieve these value. - -Some metadata is long (or at least variable length) and needs processing by a -separate parser. For example, processing XMP metadata usually involves some -sort of XML parser, regardless of what particular image format that XMP -metadata was embedded in. Wuffs decoders will return "raw metadata" instead of -parsing it themselves. Raw metadata is further sub-divided into two categories: -"raw passthrough" and "raw transform". Either way, note that raw metadata might -also be arranged in multiple (non-contiguous) chunks of the source data. - -"Raw passthrough" means that the bytes in the wire format can be sent "as is" -to the separate parser. Call `tell_me_more` in a loop (the `dst: io_writer` -argument will be ignored) and `metadata_raw_passthrough__range` to identify a -range (a chunk) of the input stream. Divert the bytes in that range to the -separate parser (which should advance the `io_buffer` to the end of that range) -and repeat the loop as long as `tell_me_more` returned a "$even more -information" status. - -"Raw transform" means that the bytes in the wire format have to be further -processed (e.g. decompressed) before sending them on to the separate parser. -That processing is done by the `image_decoder` callee, not the caller. Pass a -writable `dst io_buffer` to `tell_me_more` (where writable means that it has a -positive `writer_length`) and implementations should fill in that buffer with -post-transformed (e.g. decompressed) data to send onwards. Like any -`io_transformer`-like coroutine, this should be in a loop, as it may suspend -with "$short read" and "$short write" statuses. - -Either way, break the loop (after handling the `dst` and `minfo` out -parameters) when `tell_me_more` returns a NULL status, meaning ok, when the -metadata is complete. Afterwards, call the original action (e.g. -`decode_image_config`) again to resume after the "@metadata reported" detour. +The [Metadata](./metadata.md) document has more API information, applicable to +Wuffs' decoders but not specific to Wuffs' image decoders. ## API Listing @@ -171,6 +129,7 @@ In Wuffs syntax, the `base.image_decoder` methods are: - [example/gifplayer](/example/gifplayer) - [example/imageviewer](/example/imageviewer) - [example/sdl-imageviewer](/example/sdl-imageviewer) +- [script/print-average-pixel](/script/print-average-pixel.cc) Examples in other repositories: diff --git a/doc/std/metadata.md b/doc/std/metadata.md new file mode 100644 index 00000000..2ec954ee --- /dev/null +++ b/doc/std/metadata.md @@ -0,0 +1,48 @@ +# Metadata + +Files can also contain metadata (e.g. for image files, EXIF information, color +profiles and time stamps). By default, Wuffs' decoders skip over metadata, but +calling `set_report_metadata` will opt in to having `decode_etc` methods return +early when encountering metadata in the file. Calling `set_report_metadata` can +be done multiple times, each with a different +[FourCC](/doc/note/base38-and-fourcc.md) code such as `0x49434350` "ICCP" or +`0x584D5020` "XMP ", to indicate what sorts of metadata the caller is +interested in. Conversely, when the parser encounters metadata (and returns a +"@metadata reported" [status](/doc/note/statuses.md)), call `tell_me_more` to +see what sort of metadata it is. + +Some metadata is short and `tell_me_more` will also fill in its `minfo: nptr +more_information` out parameter with "parsed metadata". For example, Gamma +Correction metadata is a single numerical value and Primary Chromaticities +metadata is only eight numbers. From C++ code, call the `metadata_parsed__gama` +or `metadata_parsed__chrm` methods to retrieve these value. + +Some metadata is long (or at least variable length) and needs processing by a +separate parser. For example, processing XMP metadata usually involves some +sort of XML parser, regardless of what particular image format that XMP +metadata was embedded in. Wuffs decoders will return "raw metadata" instead of +parsing it themselves. Raw metadata is further sub-divided into two categories: +"raw passthrough" and "raw transform". Either way, note that raw metadata might +also be arranged in multiple (non-contiguous) chunks of the source data. + +"Raw passthrough" means that the bytes in the wire format can be sent "as is" +to the separate parser. Call `tell_me_more` in a loop (the `dst: io_writer` +argument will be ignored) and `metadata_raw_passthrough__range` to identify a +range (a chunk) of the input stream. Divert the bytes in that range to the +separate parser (which should advance the `io_buffer` to the end of that range) +and repeat the loop as long as `tell_me_more` returned a "$even more +information" status. + +"Raw transform" means that the bytes in the wire format have to be further +processed (e.g. decompressed) before sending them on to the separate parser. +That processing is done by the decoder callee, not the caller. Pass a writable +`dst io_buffer` to `tell_me_more` (where writable means that it has a positive +`writer_length`) and implementations should fill in that buffer with +post-transformed (e.g. decompressed) data to send onwards. Like any +`io_transformer`-like coroutine, this should be in a loop, as it may suspend +with "$short read" and "$short write" statuses. + +Either way, break the loop (after handling the `dst` and `minfo` out +parameters) when `tell_me_more` returns a NULL status, meaning ok, when the +metadata is complete. Afterwards, call the original action (e.g. `decode_etc`) +again to resume after the "@metadata reported" detour. diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 2b5971e3..a34439e5 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -8178,9 +8178,8 @@ struct wuffs_gif__decoder__struct { uint64_t f_metadata_io_position; bool f_quirks[7]; bool f_delayed_num_decoded_frames; - bool f_end_of_data; - bool f_restarted; bool f_previous_lzw_decode_ended_abruptly; + bool f_seen_header; bool f_has_global_palette; uint8_t f_interlace; bool f_seen_num_animation_loops_value; @@ -9649,6 +9648,7 @@ struct wuffs_png__decoder__struct { bool f_seen_gama; bool f_seen_iccp; bool f_seen_idat; + bool f_seen_ihdr; bool f_seen_plte; bool f_seen_srgb; bool f_seen_trns; @@ -23507,7 +23507,7 @@ wuffs_bmp__decoder__decode_image_config( self->private_impl.f_frame_config_io_position, (self->private_impl.f_channel_masks[3] == 0)); } - self->private_impl.f_call_sequence = 3; + self->private_impl.f_call_sequence = 32; ok: self->private_impl.p_decode_image_config[0] = 0; @@ -23574,7 +23574,8 @@ wuffs_bmp__decoder__decode_frame_config( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence < 3) { + if (self->private_impl.f_call_sequence == 32) { + } else if (self->private_impl.f_call_sequence < 32) { if (a_src) { a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); } @@ -23586,13 +23587,13 @@ wuffs_bmp__decoder__decode_frame_config( if (status.repr) { goto suspend; } - } else if (self->private_impl.f_call_sequence == 3) { + } else if (self->private_impl.f_call_sequence == 40) { if (self->private_impl.f_frame_config_io_position != wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)))) { status = wuffs_base__make_status(wuffs_base__error__bad_restart); goto exit; } - } else if (self->private_impl.f_call_sequence == 4) { - self->private_impl.f_call_sequence = 255; + } else if (self->private_impl.f_call_sequence == 64) { + self->private_impl.f_call_sequence = 96; status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; } else { @@ -23615,7 +23616,7 @@ wuffs_bmp__decoder__decode_frame_config( false, 4278190080); } - self->private_impl.f_call_sequence = 4; + self->private_impl.f_call_sequence = 64; ok: self->private_impl.p_decode_frame_config[0] = 0; @@ -23687,7 +23688,8 @@ wuffs_bmp__decoder__decode_frame( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence < 4) { + if (self->private_impl.f_call_sequence == 64) { + } else if (self->private_impl.f_call_sequence < 64) { if (a_src) { a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); } @@ -23699,7 +23701,6 @@ wuffs_bmp__decoder__decode_frame( if (status.repr) { goto suspend; } - } else if (self->private_impl.f_call_sequence == 4) { } else { status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; @@ -23799,7 +23800,7 @@ wuffs_bmp__decoder__decode_frame( iop_a_src += self->private_data.s_decode_frame[0].scratch; self->private_impl.f_pending_pad = 0; } - self->private_impl.f_call_sequence = 255; + self->private_impl.f_call_sequence = 96; ok: self->private_impl.p_decode_frame[0] = 0; @@ -24542,7 +24543,7 @@ wuffs_bmp__decoder__num_decoded_frame_configs( return 0; } - if (self->private_impl.f_call_sequence > 3) { + if (self->private_impl.f_call_sequence > 32) { return 1; } return 0; @@ -24561,7 +24562,7 @@ wuffs_bmp__decoder__num_decoded_frames( return 0; } - if (self->private_impl.f_call_sequence > 4) { + if (self->private_impl.f_call_sequence > 64) { return 1; } return 0; @@ -24584,13 +24585,13 @@ wuffs_bmp__decoder__restart_frame( : wuffs_base__error__initialize_not_called); } - if (self->private_impl.f_call_sequence < 3) { + if (self->private_impl.f_call_sequence < 32) { return wuffs_base__make_status(wuffs_base__error__bad_call_sequence); } if (a_index != 0) { return wuffs_base__make_status(wuffs_base__error__bad_argument); } - self->private_impl.f_call_sequence = 3; + self->private_impl.f_call_sequence = 40; self->private_impl.f_frame_config_io_position = a_io_position; return wuffs_base__make_status(NULL); } @@ -31379,7 +31380,10 @@ wuffs_gif__decoder__decode_image_config( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence == 0) { + if (self->private_impl.f_call_sequence != 0) { + status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence); + goto exit; + } else if ( ! self->private_impl.f_seen_header) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); status = wuffs_gif__decoder__decode_header(self, a_src); if (status.repr) { @@ -31390,9 +31394,7 @@ wuffs_gif__decoder__decode_image_config( if (status.repr) { goto suspend; } - } else if (self->private_impl.f_call_sequence != 2) { - status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence); - goto exit; + self->private_impl.f_seen_header = true; } WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); status = wuffs_gif__decoder__decode_up_to_id_part1(self, a_src); @@ -31422,7 +31424,7 @@ wuffs_gif__decoder__decode_image_config( self->private_impl.f_frame_config_io_position, v_ffio); } - self->private_impl.f_call_sequence = 3; + self->private_impl.f_call_sequence = 32; goto ok; ok: @@ -31511,7 +31513,7 @@ wuffs_gif__decoder__tell_me_more( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence != 1) { + if ((self->private_impl.f_call_sequence & 16) == 0) { status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence); goto exit; } @@ -31582,7 +31584,7 @@ wuffs_gif__decoder__tell_me_more( self->private_impl.f_metadata_io_position, self->private_impl.f_metadata_io_position); } - self->private_impl.f_call_sequence = 2; + self->private_impl.f_call_sequence &= 239; self->private_impl.f_metadata_fourcc = 0; self->private_impl.f_metadata_io_position = 0; status = wuffs_base__make_status(NULL); @@ -31717,16 +31719,17 @@ wuffs_gif__decoder__restart_frame( : wuffs_base__error__initialize_not_called); } - if (self->private_impl.f_call_sequence < 3) { + if (self->private_impl.f_call_sequence < 32) { return wuffs_base__make_status(wuffs_base__error__bad_call_sequence); + } else if (a_io_position == 0) { + return wuffs_base__make_status(wuffs_base__error__bad_argument); } self->private_impl.f_delayed_num_decoded_frames = false; - self->private_impl.f_end_of_data = false; - self->private_impl.f_restarted = true; self->private_impl.f_frame_config_io_position = a_io_position; self->private_impl.f_num_decoded_frame_configs_value = a_index; self->private_impl.f_num_decoded_frames_value = a_index; wuffs_gif__decoder__reset_gc(self); + self->private_impl.f_call_sequence = 40; return wuffs_base__make_status(NULL); } @@ -31781,50 +31784,64 @@ wuffs_gif__decoder__decode_frame_config( self->private_impl.f_ignore_metadata = true; self->private_impl.f_dirty_max_excl_y = 0; - if ( ! self->private_impl.f_end_of_data) { - if (self->private_impl.f_call_sequence == 0) { - if (a_src) { - a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); - } - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); - status = wuffs_gif__decoder__decode_image_config(self, NULL, a_src); - if (a_src) { - iop_a_src = a_src->data.ptr + a_src->meta.ri; - } - if (status.repr) { - goto suspend; - } - } else if (self->private_impl.f_call_sequence != 3) { - if (self->private_impl.f_call_sequence == 4) { - if (a_src) { - a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); - } - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); - status = wuffs_gif__decoder__skip_frame(self, a_src); - if (a_src) { - iop_a_src = a_src->data.ptr + a_src->meta.ri; - } - if (status.repr) { - goto suspend; - } - } - if (a_src) { - a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); - } - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); - status = wuffs_gif__decoder__decode_up_to_id_part1(self, a_src); - if (a_src) { - iop_a_src = a_src->data.ptr + a_src->meta.ri; - } - if (status.repr) { - goto suspend; - } + if ((self->private_impl.f_call_sequence & 16) != 0) { + status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence); + goto exit; + } else if (self->private_impl.f_call_sequence == 32) { + } else if (self->private_impl.f_call_sequence < 32) { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); } - } - if (self->private_impl.f_end_of_data) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + status = wuffs_gif__decoder__decode_image_config(self, NULL, a_src); + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } + if (status.repr) { + goto suspend; + } + } else if (self->private_impl.f_call_sequence == 40) { + if (self->private_impl.f_frame_config_io_position != wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)))) { + status = wuffs_base__make_status(wuffs_base__error__bad_restart); + goto exit; + } + } else if (self->private_impl.f_call_sequence == 64) { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + status = wuffs_gif__decoder__skip_frame(self, a_src); + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } + if (status.repr) { + goto suspend; + } + if (self->private_impl.f_call_sequence >= 96) { + status = wuffs_base__make_status(wuffs_base__note__end_of_data); + goto ok; + } + } else { status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; } + if ((self->private_impl.f_num_decoded_frame_configs_value > 0) || (self->private_impl.f_call_sequence == 40)) { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); + status = wuffs_gif__decoder__decode_up_to_id_part1(self, a_src); + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } + if (status.repr) { + goto suspend; + } + if (self->private_impl.f_call_sequence >= 96) { + status = wuffs_base__make_status(wuffs_base__note__end_of_data); + goto ok; + } + } v_background_color = self->private_impl.f_black_color_u32_argb_premul; if ( ! self->private_impl.f_gc_has_transparent_index) { v_background_color = self->private_impl.f_background_color_u32_argb_premul; @@ -31856,7 +31873,7 @@ wuffs_gif__decoder__decode_frame_config( v_background_color); } wuffs_base__u64__sat_add_indirect(&self->private_impl.f_num_decoded_frame_configs_value, 1); - self->private_impl.f_call_sequence = 4; + self->private_impl.f_call_sequence = 64; ok: self->private_impl.p_decode_frame_config[0] = 0; @@ -31957,6 +31974,7 @@ wuffs_gif__decoder__skip_frame( wuffs_base__u64__sat_add_indirect(&self->private_impl.f_num_decoded_frames_value, 1); } wuffs_gif__decoder__reset_gc(self); + self->private_impl.f_call_sequence = 32; goto ok; ok: @@ -32013,12 +32031,16 @@ wuffs_gif__decoder__decode_frame( WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; self->private_impl.f_ignore_metadata = true; - if (self->private_impl.f_call_sequence != 4) { + if (self->private_impl.f_call_sequence == 64) { + } else if (self->private_impl.f_call_sequence < 64) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); status = wuffs_gif__decoder__decode_frame_config(self, NULL, a_src); if (status.repr) { goto suspend; } + } else { + status = wuffs_base__make_status(wuffs_base__note__end_of_data); + goto ok; } if (self->private_impl.f_quirks[5] && ((self->private_impl.f_frame_rect_x0 == self->private_impl.f_frame_rect_x1) || (self->private_impl.f_frame_rect_y0 == self->private_impl.f_frame_rect_y1))) { status = wuffs_base__make_status(wuffs_gif__error__bad_frame_size); @@ -32036,8 +32058,8 @@ wuffs_gif__decoder__decode_frame( } wuffs_base__u64__sat_add_indirect(&self->private_impl.f_num_decoded_frames_value, 1); wuffs_gif__decoder__reset_gc(self); + self->private_impl.f_call_sequence = 32; - goto ok; ok: self->private_impl.p_decode_frame[0] = 0; goto exit; @@ -32061,7 +32083,6 @@ wuffs_gif__decoder__decode_frame( static wuffs_base__empty_struct wuffs_gif__decoder__reset_gc( wuffs_gif__decoder* self) { - self->private_impl.f_call_sequence = 5; self->private_impl.f_gc_has_transparent_index = false; self->private_impl.f_gc_transparent_index = 0; self->private_impl.f_gc_disposal = 0; @@ -32094,15 +32115,8 @@ wuffs_gif__decoder__decode_up_to_id_part1( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if ( ! self->private_impl.f_restarted) { - if (self->private_impl.f_call_sequence != 2) { - self->private_impl.f_frame_config_io_position = wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src))); - } - } else if (self->private_impl.f_frame_config_io_position != wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)))) { - status = wuffs_base__make_status(wuffs_base__error__bad_restart); - goto exit; - } else { - self->private_impl.f_restarted = false; + if ((self->private_impl.f_frame_config_io_position == 0) || (self->private_impl.f_num_decoded_frame_configs_value > 0)) { + self->private_impl.f_frame_config_io_position = wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src))); } while (true) { { @@ -32148,7 +32162,7 @@ wuffs_gif__decoder__decode_up_to_id_part1( self->private_impl.f_delayed_num_decoded_frames = false; wuffs_base__u64__sat_add_indirect(&self->private_impl.f_num_decoded_frames_value, 1); } - self->private_impl.f_end_of_data = true; + self->private_impl.f_call_sequence = 96; goto label__0__break; } } @@ -32784,13 +32798,13 @@ wuffs_gif__decoder__decode_ae( } else if (v_is_iccp && self->private_impl.f_report_metadata_iccp) { self->private_impl.f_metadata_fourcc = 1229144912; self->private_impl.f_metadata_io_position = wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src))); - self->private_impl.f_call_sequence = 1; + self->private_impl.f_call_sequence = 16; status = wuffs_base__make_status(wuffs_base__note__metadata_reported); goto ok; } else if (v_is_xmp && self->private_impl.f_report_metadata_xmp) { self->private_impl.f_metadata_fourcc = 1481461792; self->private_impl.f_metadata_io_position = wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src))); - self->private_impl.f_call_sequence = 1; + self->private_impl.f_call_sequence = 16; status = wuffs_base__make_status(wuffs_base__note__metadata_reported); goto ok; } @@ -33105,7 +33119,7 @@ wuffs_gif__decoder__decode_id_part0( self->private_impl.f_frame_rect_y1 += self->private_impl.f_frame_rect_y0; self->private_impl.f_dst_x = self->private_impl.f_frame_rect_x0; self->private_impl.f_dst_y = self->private_impl.f_frame_rect_y0; - if ((self->private_impl.f_call_sequence == 0) && ! self->private_impl.f_quirks[4]) { + if ((self->private_impl.f_num_decoded_frame_configs_value == 0) && ! self->private_impl.f_quirks[4]) { self->private_impl.f_width = wuffs_base__u32__max(self->private_impl.f_width, self->private_impl.f_frame_rect_x1); self->private_impl.f_height = wuffs_base__u32__max(self->private_impl.f_height, self->private_impl.f_frame_rect_y1); } @@ -36694,7 +36708,7 @@ wuffs_nie__decoder__decode_image_config( 16, false); } - self->private_impl.f_call_sequence = 3; + self->private_impl.f_call_sequence = 32; goto ok; ok: @@ -36762,7 +36776,8 @@ wuffs_nie__decoder__decode_frame_config( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence < 3) { + if (self->private_impl.f_call_sequence == 32) { + } else if (self->private_impl.f_call_sequence < 32) { if (a_src) { a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); } @@ -36774,13 +36789,13 @@ wuffs_nie__decoder__decode_frame_config( if (status.repr) { goto suspend; } - } else if (self->private_impl.f_call_sequence == 3) { + } else if (self->private_impl.f_call_sequence == 40) { if (16 != wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)))) { status = wuffs_base__make_status(wuffs_base__error__bad_restart); goto exit; } - } else if (self->private_impl.f_call_sequence == 4) { - self->private_impl.f_call_sequence = 255; + } else if (self->private_impl.f_call_sequence == 64) { + self->private_impl.f_call_sequence = 96; status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; } else { @@ -36803,7 +36818,7 @@ wuffs_nie__decoder__decode_frame_config( false, 0); } - self->private_impl.f_call_sequence = 4; + self->private_impl.f_call_sequence = 64; ok: self->private_impl.p_decode_frame_config[0] = 0; @@ -36864,13 +36879,13 @@ wuffs_nie__decoder__decode_frame( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence < 4) { + if (self->private_impl.f_call_sequence == 64) { + } else if (self->private_impl.f_call_sequence < 64) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); status = wuffs_nie__decoder__decode_frame_config(self, NULL, a_src); if (status.repr) { goto suspend; } - } else if (self->private_impl.f_call_sequence == 4) { } else { status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; @@ -36911,7 +36926,7 @@ wuffs_nie__decoder__decode_frame( WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(2); } label__0__break:; - self->private_impl.f_call_sequence = 255; + self->private_impl.f_call_sequence = 96; ok: self->private_impl.p_decode_frame[0] = 0; @@ -37061,7 +37076,7 @@ wuffs_nie__decoder__num_decoded_frame_configs( return 0; } - if (self->private_impl.f_call_sequence > 3) { + if (self->private_impl.f_call_sequence > 32) { return 1; } return 0; @@ -37080,7 +37095,7 @@ wuffs_nie__decoder__num_decoded_frames( return 0; } - if (self->private_impl.f_call_sequence > 4) { + if (self->private_impl.f_call_sequence > 64) { return 1; } return 0; @@ -37103,13 +37118,13 @@ wuffs_nie__decoder__restart_frame( : wuffs_base__error__initialize_not_called); } - if (self->private_impl.f_call_sequence < 3) { + if (self->private_impl.f_call_sequence < 32) { return wuffs_base__make_status(wuffs_base__error__bad_call_sequence); } if ((a_index != 0) || (a_io_position != 16)) { return wuffs_base__make_status(wuffs_base__error__bad_argument); } - self->private_impl.f_call_sequence = 3; + self->private_impl.f_call_sequence = 40; return wuffs_base__make_status(NULL); } @@ -39576,16 +39591,10 @@ wuffs_png__decoder__decode_image_config( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence == 2) { - if (self->private_impl.f_metadata_fourcc != 0) { - self->private_impl.f_call_sequence = 1; - status = wuffs_base__make_status(wuffs_base__note__metadata_reported); - goto ok; - } - } else if (self->private_impl.f_call_sequence != 0) { + if (self->private_impl.f_call_sequence != 0) { status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence); goto exit; - } else { + } else if ( ! self->private_impl.f_seen_ihdr) { { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); uint64_t t_0; @@ -39718,6 +39727,11 @@ wuffs_png__decoder__decode_image_config( status = wuffs_base__make_status(wuffs_png__error__bad_checksum); goto exit; } + self->private_impl.f_seen_ihdr = true; + } else if (self->private_impl.f_metadata_fourcc != 0) { + self->private_impl.f_call_sequence = 16; + status = wuffs_base__make_status(wuffs_base__note__metadata_reported); + goto ok; } while (true) { while (((uint64_t)(io2_a_src - iop_a_src)) < 8) { @@ -39775,7 +39789,7 @@ wuffs_png__decoder__decode_image_config( } label__2__break:; if (self->private_impl.f_metadata_fourcc != 0) { - self->private_impl.f_call_sequence = 1; + self->private_impl.f_call_sequence = 16; status = wuffs_base__make_status(wuffs_base__note__metadata_reported); goto ok; } @@ -39840,7 +39854,7 @@ wuffs_png__decoder__decode_image_config( self->private_impl.f_first_disposal = 0; self->private_impl.f_first_overwrite_instead_of_blend = false; } - self->private_impl.f_call_sequence = 3; + self->private_impl.f_call_sequence = 32; ok: self->private_impl.p_decode_image_config[0] = 0; @@ -41801,10 +41815,11 @@ wuffs_png__decoder__decode_frame_config( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence == 255) { - status = wuffs_base__make_status(wuffs_base__note__end_of_data); - goto ok; - } else if (self->private_impl.f_call_sequence < 3) { + if ((self->private_impl.f_call_sequence & 16) != 0) { + status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence); + goto exit; + } else if (self->private_impl.f_call_sequence == 32) { + } else if (self->private_impl.f_call_sequence < 32) { if (a_src) { a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); } @@ -41816,12 +41831,12 @@ wuffs_png__decoder__decode_frame_config( if (status.repr) { goto suspend; } - } else if (self->private_impl.f_call_sequence == 3) { + } else if (self->private_impl.f_call_sequence == 40) { if (self->private_impl.f_frame_config_io_position != wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)))) { status = wuffs_base__make_status(wuffs_base__error__bad_restart); goto exit; } - } else if (self->private_impl.f_call_sequence == 4) { + } else if (self->private_impl.f_call_sequence == 64) { if (a_src) { a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); } @@ -41833,10 +41848,13 @@ wuffs_png__decoder__decode_frame_config( if (status.repr) { goto suspend; } - if (self->private_impl.f_call_sequence == 255) { + if (self->private_impl.f_call_sequence >= 96) { status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; } + } else { + status = wuffs_base__make_status(wuffs_base__note__end_of_data); + goto ok; } if (self->private_impl.f_num_decoded_frame_configs_value == 0) { self->private_impl.f_frame_rect_x0 = self->private_impl.f_first_rect_x0; @@ -41964,7 +41982,7 @@ wuffs_png__decoder__decode_frame_config( 0); } wuffs_base__u32__sat_add_indirect(&self->private_impl.f_num_decoded_frame_configs_value, 1); - self->private_impl.f_call_sequence = 4; + self->private_impl.f_call_sequence = 64; ok: self->private_impl.p_decode_frame_config[0] = 0; @@ -42121,9 +42139,9 @@ wuffs_png__decoder__skip_frame( label__0__break:; wuffs_base__u32__sat_add_indirect(&self->private_impl.f_num_decoded_frames_value, 1); if (self->private_impl.f_num_decoded_frames_value < self->private_impl.f_num_animation_frames_value) { - self->private_impl.f_call_sequence = 5; + self->private_impl.f_call_sequence = 32; } else { - self->private_impl.f_call_sequence = 255; + self->private_impl.f_call_sequence = 96; } ok: @@ -42195,10 +42213,13 @@ wuffs_png__decoder__decode_frame( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence == 255) { + if ((self->private_impl.f_call_sequence & 16) != 0) { + status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence); + goto exit; + } else if (self->private_impl.f_call_sequence >= 96) { status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; - } else if (self->private_impl.f_call_sequence != 4) { + } else if (self->private_impl.f_call_sequence != 64) { if (a_src) { a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); } @@ -42391,9 +42412,9 @@ wuffs_png__decoder__decode_frame( label__2__break:; wuffs_base__u32__sat_add_indirect(&self->private_impl.f_num_decoded_frames_value, 1); if (self->private_impl.f_num_decoded_frames_value < self->private_impl.f_num_animation_frames_value) { - self->private_impl.f_call_sequence = 5; + self->private_impl.f_call_sequence = 32; } else { - self->private_impl.f_call_sequence = 255; + self->private_impl.f_call_sequence = 96; } ok: @@ -42842,12 +42863,12 @@ wuffs_png__decoder__restart_frame( : wuffs_base__error__initialize_not_called); } - if (self->private_impl.f_call_sequence < 3) { + if (self->private_impl.f_call_sequence < 32) { return wuffs_base__make_status(wuffs_base__error__bad_call_sequence); } else if ((a_index >= ((uint64_t)(self->private_impl.f_num_animation_frames_value))) || ((a_index == 0) && (a_io_position != self->private_impl.f_first_config_io_position))) { return wuffs_base__make_status(wuffs_base__error__bad_argument); } - self->private_impl.f_call_sequence = 3; + self->private_impl.f_call_sequence = 40; if (self->private_impl.f_interlace_pass >= 1) { self->private_impl.f_interlace_pass = 1; } @@ -42960,7 +42981,7 @@ wuffs_png__decoder__tell_me_more( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence != 1) { + if ((self->private_impl.f_call_sequence & 16) == 0) { status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence); goto exit; } @@ -43355,7 +43376,7 @@ wuffs_png__decoder__tell_me_more( } self->private_impl.f_metadata_is_zlib_compressed = true; } - self->private_impl.f_call_sequence = 2; + self->private_impl.f_call_sequence &= 239; status = wuffs_base__make_status(NULL); goto ok; } @@ -43380,7 +43401,7 @@ wuffs_png__decoder__tell_me_more( self->private_impl.f_metadata_x = 0; self->private_impl.f_metadata_y = 0; self->private_impl.f_metadata_z = 0; - self->private_impl.f_call_sequence = 2; + self->private_impl.f_call_sequence &= 239; status = wuffs_base__make_status(NULL); goto ok; @@ -44371,7 +44392,7 @@ wuffs_tga__decoder__decode_image_config( self->private_impl.f_frame_config_io_position, self->private_impl.f_opaque); } - self->private_impl.f_call_sequence = 3; + self->private_impl.f_call_sequence = 32; goto ok; ok: @@ -44440,7 +44461,8 @@ wuffs_tga__decoder__decode_frame_config( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence < 3) { + if (self->private_impl.f_call_sequence == 32) { + } else if (self->private_impl.f_call_sequence < 32) { if (a_src) { a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); } @@ -44452,13 +44474,13 @@ wuffs_tga__decoder__decode_frame_config( if (status.repr) { goto suspend; } - } else if (self->private_impl.f_call_sequence == 3) { + } else if (self->private_impl.f_call_sequence == 40) { if (self->private_impl.f_frame_config_io_position != wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)))) { status = wuffs_base__make_status(wuffs_base__error__bad_restart); goto exit; } - } else if (self->private_impl.f_call_sequence == 4) { - self->private_impl.f_call_sequence = 255; + } else if (self->private_impl.f_call_sequence == 64) { + self->private_impl.f_call_sequence = 96; status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; } else { @@ -44481,7 +44503,7 @@ wuffs_tga__decoder__decode_frame_config( false, 4278190080); } - self->private_impl.f_call_sequence = 4; + self->private_impl.f_call_sequence = 64; ok: self->private_impl.p_decode_frame_config[0] = 0; @@ -44582,7 +44604,8 @@ wuffs_tga__decoder__decode_frame( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence < 4) { + if (self->private_impl.f_call_sequence == 64) { + } else if (self->private_impl.f_call_sequence < 64) { if (a_src) { a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); } @@ -44594,7 +44617,6 @@ wuffs_tga__decoder__decode_frame( if (status.repr) { goto suspend; } - } else if (self->private_impl.f_call_sequence == 4) { } else { status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; @@ -44821,7 +44843,7 @@ wuffs_tga__decoder__decode_frame( goto label__resume__break; } label__resume__break:; - self->private_impl.f_call_sequence = 255; + self->private_impl.f_call_sequence = 96; ok: self->private_impl.p_decode_frame[0] = 0; @@ -44902,7 +44924,7 @@ wuffs_tga__decoder__num_decoded_frame_configs( return 0; } - if (self->private_impl.f_call_sequence > 3) { + if (self->private_impl.f_call_sequence > 32) { return 1; } return 0; @@ -44921,7 +44943,7 @@ wuffs_tga__decoder__num_decoded_frames( return 0; } - if (self->private_impl.f_call_sequence > 4) { + if (self->private_impl.f_call_sequence > 64) { return 1; } return 0; @@ -44944,13 +44966,13 @@ wuffs_tga__decoder__restart_frame( : wuffs_base__error__initialize_not_called); } - if (self->private_impl.f_call_sequence < 3) { + if (self->private_impl.f_call_sequence < 32) { return wuffs_base__make_status(wuffs_base__error__bad_call_sequence); } if (a_index != 0) { return wuffs_base__make_status(wuffs_base__error__bad_argument); } - self->private_impl.f_call_sequence = 3; + self->private_impl.f_call_sequence = 40; self->private_impl.f_frame_config_io_position = a_io_position; return wuffs_base__make_status(NULL); } @@ -45270,7 +45292,7 @@ wuffs_wbmp__decoder__decode_image_config( self->private_impl.f_frame_config_io_position, true); } - self->private_impl.f_call_sequence = 3; + self->private_impl.f_call_sequence = 32; goto ok; ok: @@ -45340,7 +45362,8 @@ wuffs_wbmp__decoder__decode_frame_config( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence < 3) { + if (self->private_impl.f_call_sequence == 32) { + } else if (self->private_impl.f_call_sequence < 32) { if (a_src) { a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); } @@ -45352,13 +45375,13 @@ wuffs_wbmp__decoder__decode_frame_config( if (status.repr) { goto suspend; } - } else if (self->private_impl.f_call_sequence == 3) { + } else if (self->private_impl.f_call_sequence == 40) { if (self->private_impl.f_frame_config_io_position != wuffs_base__u64__sat_add((a_src ? a_src->meta.pos : 0), ((uint64_t)(iop_a_src - io0_a_src)))) { status = wuffs_base__make_status(wuffs_base__error__bad_restart); goto exit; } - } else if (self->private_impl.f_call_sequence == 4) { - self->private_impl.f_call_sequence = 255; + } else if (self->private_impl.f_call_sequence == 64) { + self->private_impl.f_call_sequence = 96; status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; } else { @@ -45381,7 +45404,7 @@ wuffs_wbmp__decoder__decode_frame_config( false, 4278190080); } - self->private_impl.f_call_sequence = 4; + self->private_impl.f_call_sequence = 64; ok: self->private_impl.p_decode_frame_config[0] = 0; @@ -45470,7 +45493,8 @@ wuffs_wbmp__decoder__decode_frame( switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; - if (self->private_impl.f_call_sequence < 4) { + if (self->private_impl.f_call_sequence == 64) { + } else if (self->private_impl.f_call_sequence < 64) { if (a_src) { a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); } @@ -45482,7 +45506,6 @@ wuffs_wbmp__decoder__decode_frame( if (status.repr) { goto suspend; } - } else if (self->private_impl.f_call_sequence == 4) { } else { status = wuffs_base__make_status(wuffs_base__note__end_of_data); goto ok; @@ -45545,7 +45568,7 @@ wuffs_wbmp__decoder__decode_frame( v_dst_y += 1; } } - self->private_impl.f_call_sequence = 255; + self->private_impl.f_call_sequence = 96; ok: self->private_impl.p_decode_frame[0] = 0; @@ -45623,7 +45646,7 @@ wuffs_wbmp__decoder__num_decoded_frame_configs( return 0; } - if (self->private_impl.f_call_sequence > 3) { + if (self->private_impl.f_call_sequence > 32) { return 1; } return 0; @@ -45642,7 +45665,7 @@ wuffs_wbmp__decoder__num_decoded_frames( return 0; } - if (self->private_impl.f_call_sequence > 4) { + if (self->private_impl.f_call_sequence > 64) { return 1; } return 0; @@ -45665,13 +45688,13 @@ wuffs_wbmp__decoder__restart_frame( : wuffs_base__error__initialize_not_called); } - if (self->private_impl.f_call_sequence < 3) { + if (self->private_impl.f_call_sequence < 32) { return wuffs_base__make_status(wuffs_base__error__bad_call_sequence); } if (a_index != 0) { return wuffs_base__make_status(wuffs_base__error__bad_argument); } - self->private_impl.f_call_sequence = 3; + self->private_impl.f_call_sequence = 40; self->private_impl.f_frame_config_io_position = a_io_position; return wuffs_base__make_status(NULL); } diff --git a/std/bmp/decode_bmp.wuffs b/std/bmp/decode_bmp.wuffs index bae1eed1..f6bc1608 100644 --- a/std/bmp/decode_bmp.wuffs +++ b/std/bmp/decode_bmp.wuffs @@ -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 } diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index d872de9a..34bc6735 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -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) } diff --git a/std/nie/decode_nie.wuffs b/std/nie/decode_nie.wuffs index 879c92da..7706c6df 100644 --- a/std/nie/decode_nie.wuffs +++ b/std/nie/decode_nie.wuffs @@ -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 } diff --git a/std/png/decode_png.wuffs b/std/png/decode_png.wuffs index 0326ba86..62e0e622 100644 --- a/std/png/decode_png.wuffs +++ b/std/png/decode_png.wuffs @@ -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 } diff --git a/std/tga/decode_tga.wuffs b/std/tga/decode_tga.wuffs index e052e63c..17d2bf11 100644 --- a/std/tga/decode_tga.wuffs +++ b/std/tga/decode_tga.wuffs @@ -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 } diff --git a/std/wbmp/decode_wbmp.wuffs b/std/wbmp/decode_wbmp.wuffs index 5d664532..5f673bd8 100644 --- a/std/wbmp/decode_wbmp.wuffs +++ b/std/wbmp/decode_wbmp.wuffs @@ -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 }