From 58c30e9d6d306fcb772db24aa42c4817f36b4497 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 23 Dec 2020 22:11:04 +1100 Subject: [PATCH] Add std/png --- doc/changelog.md | 1 + example/imageviewer/imageviewer.c | 19 + release/c/wuffs-unsupported-snapshot.c | 1211 ++++++++++++++++++++++++ std/png/decode_png.wuffs | 287 ++++++ test/c/std/png.c | 168 ++++ 5 files changed, 1686 insertions(+) create mode 100644 std/png/decode_png.wuffs create mode 100644 test/c/std/png.c diff --git a/doc/changelog.md b/doc/changelog.md index 07289558..d1cbe40b 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -22,6 +22,7 @@ - Added `std/gif.config_decoder`. - Added `std/json`. - Added `std/nie`. +- Added `std/png`. - Added `std/wbmp`. - Added `tell_me_more?` mechanism. - Added alloc functions. diff --git a/example/imageviewer/imageviewer.c b/example/imageviewer/imageviewer.c index 116cb1cb..bac76294 100644 --- a/example/imageviewer/imageviewer.c +++ b/example/imageviewer/imageviewer.c @@ -52,12 +52,17 @@ The Escape key quits. // modules we use makes that process explicit. Preprocessing means that such // code simply isn't compiled. #define WUFFS_CONFIG__MODULES +#define WUFFS_CONFIG__MODULE__ADLER32 #define WUFFS_CONFIG__MODULE__BASE #define WUFFS_CONFIG__MODULE__BMP +#define WUFFS_CONFIG__MODULE__CRC32 +#define WUFFS_CONFIG__MODULE__DEFLATE #define WUFFS_CONFIG__MODULE__GIF #define WUFFS_CONFIG__MODULE__LZW #define WUFFS_CONFIG__MODULE__NIE +#define WUFFS_CONFIG__MODULE__PNG #define WUFFS_CONFIG__MODULE__WBMP +#define WUFFS_CONFIG__MODULE__ZLIB // If building this program in an environment that doesn't easily accommodate // relative includes, you can use the script/inline-c-relative-includes.go @@ -94,6 +99,7 @@ union { wuffs_bmp__decoder bmp; wuffs_gif__decoder gif; wuffs_nie__decoder nie; + wuffs_png__decoder png; wuffs_wbmp__decoder wbmp; } g_potential_decoders; @@ -177,6 +183,19 @@ load_image_type() { &g_potential_decoders.nie); break; + case '\x89': + status = wuffs_png__decoder__initialize( + &g_potential_decoders.png, sizeof g_potential_decoders.png, + WUFFS_VERSION, WUFFS_INITIALIZE__DEFAULT_OPTIONS); + if (!wuffs_base__status__is_ok(&status)) { + printf("%s: %s\n", g_filename, wuffs_base__status__message(&status)); + return false; + } + g_image_decoder = + wuffs_png__decoder__upcast_as__wuffs_base__image_decoder( + &g_potential_decoders.png); + break; + default: printf("%s: unrecognized file format\n", g_filename); return false; diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 5aadca9d..b7ac21cf 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -7946,6 +7946,327 @@ extern "C" { // ---------------- Status Codes +extern const char wuffs_png__error__bad_header[]; +extern const char wuffs_png__error__unsupported_png_file[]; + +// ---------------- Public Consts + +#define WUFFS_PNG__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 0 + +// ---------------- Struct Declarations + +typedef struct wuffs_png__decoder__struct wuffs_png__decoder; + +// ---------------- Public Initializer Prototypes + +// For any given "wuffs_foo__bar* self", "wuffs_foo__bar__initialize(self, +// etc)" should be called before any other "wuffs_foo__bar__xxx(self, etc)". +// +// Pass sizeof(*self) and WUFFS_VERSION for sizeof_star_self and wuffs_version. +// Pass 0 (or some combination of WUFFS_INITIALIZE__XXX) for options. + +wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT +wuffs_png__decoder__initialize( + wuffs_png__decoder* self, + size_t sizeof_star_self, + uint64_t wuffs_version, + uint32_t options); + +size_t +sizeof__wuffs_png__decoder(); + +// ---------------- Allocs + +// These functions allocate and initialize Wuffs structs. They return NULL if +// memory allocation fails. If they return non-NULL, there is no need to call +// wuffs_foo__bar__initialize, but the caller is responsible for eventually +// calling free on the returned pointer. That pointer is effectively a C++ +// std::unique_ptr. + +wuffs_png__decoder* +wuffs_png__decoder__alloc(); + +static inline wuffs_base__image_decoder* +wuffs_png__decoder__alloc_as__wuffs_base__image_decoder() { + return (wuffs_base__image_decoder*)(wuffs_png__decoder__alloc()); +} + +// ---------------- Upcasts + +static inline wuffs_base__image_decoder* +wuffs_png__decoder__upcast_as__wuffs_base__image_decoder( + wuffs_png__decoder* p) { + return (wuffs_base__image_decoder*)p; +} + +// ---------------- Public Function Prototypes + +WUFFS_BASE__MAYBE_STATIC wuffs_base__empty_struct +wuffs_png__decoder__set_quirk_enabled( + wuffs_png__decoder* self, + uint32_t a_quirk, + bool a_enabled); + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_png__decoder__decode_image_config( + wuffs_png__decoder* self, + wuffs_base__image_config* a_dst, + wuffs_base__io_buffer* a_src); + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_png__decoder__decode_frame_config( + wuffs_png__decoder* self, + wuffs_base__frame_config* a_dst, + wuffs_base__io_buffer* a_src); + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_png__decoder__decode_frame( + wuffs_png__decoder* self, + wuffs_base__pixel_buffer* a_dst, + wuffs_base__io_buffer* a_src, + wuffs_base__pixel_blend a_blend, + wuffs_base__slice_u8 a_workbuf, + wuffs_base__decode_frame_options* a_opts); + +WUFFS_BASE__MAYBE_STATIC wuffs_base__rect_ie_u32 +wuffs_png__decoder__frame_dirty_rect( + const wuffs_png__decoder* self); + +WUFFS_BASE__MAYBE_STATIC uint32_t +wuffs_png__decoder__num_animation_loops( + const wuffs_png__decoder* self); + +WUFFS_BASE__MAYBE_STATIC uint64_t +wuffs_png__decoder__num_decoded_frame_configs( + const wuffs_png__decoder* self); + +WUFFS_BASE__MAYBE_STATIC uint64_t +wuffs_png__decoder__num_decoded_frames( + const wuffs_png__decoder* self); + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_png__decoder__restart_frame( + wuffs_png__decoder* self, + uint64_t a_index, + uint64_t a_io_position); + +WUFFS_BASE__MAYBE_STATIC wuffs_base__empty_struct +wuffs_png__decoder__set_report_metadata( + wuffs_png__decoder* self, + uint32_t a_fourcc, + bool a_report); + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_png__decoder__tell_me_more( + wuffs_png__decoder* self, + wuffs_base__io_buffer* a_dst, + wuffs_base__more_information* a_minfo, + wuffs_base__io_buffer* a_src); + +WUFFS_BASE__MAYBE_STATIC wuffs_base__range_ii_u64 +wuffs_png__decoder__workbuf_len( + const wuffs_png__decoder* self); + +// ---------------- Struct Definitions + +// These structs' fields, and the sizeof them, are private implementation +// details that aren't guaranteed to be stable across Wuffs versions. +// +// See https://en.wikipedia.org/wiki/Opaque_pointer#C + +#if defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION) + +struct wuffs_png__decoder__struct { + // Do not access the private_impl's or private_data's fields directly. There + // is no API/ABI compatibility or safety guarantee if you do so. Instead, use + // the wuffs_foo__bar__baz functions. + // + // It is a struct, not a struct*, so that the outermost wuffs_foo__bar struct + // can be stack allocated when WUFFS_IMPLEMENTATION is defined. + + struct { + uint32_t magic; + uint32_t active_coroutine; + wuffs_base__vtable vtable_for__wuffs_base__image_decoder; + wuffs_base__vtable null_vtable; + + uint32_t f_width; + uint32_t f_height; + uint64_t f_bytes_per_row; + uint64_t f_workbuf_length; + uint8_t f_call_sequence; + uint8_t f_depth; + uint32_t f_src_pixfmt; + uint32_t f_chunk_length; + uint32_t f_chunk_type; + uint64_t f_frame_config_io_position; + wuffs_base__pixel_swizzler f_swizzler; + + uint32_t p_decode_image_config[1]; + uint32_t p_decode_frame_config[1]; + uint32_t p_decode_frame[1]; + } private_impl; + + struct { + struct { + uint32_t v_dst_pixfmt; + uint64_t scratch; + } s_decode_image_config[1]; + } private_data; + +#ifdef __cplusplus +#if (__cplusplus >= 201103L) + using unique_ptr = std::unique_ptr; + + // On failure, the alloc_etc functions return nullptr. They don't throw. + + static inline unique_ptr + alloc() { + return unique_ptr(wuffs_png__decoder__alloc(), &free); + } + + static inline wuffs_base__image_decoder::unique_ptr + alloc_as__wuffs_base__image_decoder() { + return wuffs_base__image_decoder::unique_ptr( + wuffs_png__decoder__alloc_as__wuffs_base__image_decoder(), &free); + } +#endif // (__cplusplus >= 201103L) + +#if (__cplusplus >= 201103L) && !defined(WUFFS_IMPLEMENTATION) + // Disallow constructing or copying an object via standard C++ mechanisms, + // e.g. the "new" operator, as this struct is intentionally opaque. Its total + // size and field layout is not part of the public, stable, memory-safe API. + // Use malloc or memcpy and the sizeof__wuffs_foo__bar function instead, and + // call wuffs_foo__bar__baz methods (which all take a "this"-like pointer as + // their first argument) rather than tweaking bar.private_impl.qux fields. + // + // In C, we can just leave wuffs_foo__bar as an incomplete type (unless + // WUFFS_IMPLEMENTATION is #define'd). In C++, we define a complete type in + // order to provide convenience methods. These forward on "this", so that you + // can write "bar->baz(etc)" instead of "wuffs_foo__bar__baz(bar, etc)". + wuffs_png__decoder__struct() = delete; + wuffs_png__decoder__struct(const wuffs_png__decoder__struct&) = delete; + wuffs_png__decoder__struct& operator=( + const wuffs_png__decoder__struct&) = delete; + + // As above, the size of the struct is not part of the public API, and unless + // WUFFS_IMPLEMENTATION is #define'd, this struct type T should be heap + // allocated, not stack allocated. Its size is not intended to be known at + // compile time, but it is unfortunately divulged as a side effect of + // defining C++ convenience methods. Use "sizeof__T()", calling the function, + // instead of "sizeof T", invoking the operator. To make the two values + // different, so that passing the latter will be rejected by the initialize + // function, we add an arbitrary amount of dead weight. + uint8_t dead_weight[123000000]; // 123 MB. +#endif // (__cplusplus >= 201103L) && !defined(WUFFS_IMPLEMENTATION) + + inline wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT + initialize( + size_t sizeof_star_self, + uint64_t wuffs_version, + uint32_t options) { + return wuffs_png__decoder__initialize( + this, sizeof_star_self, wuffs_version, options); + } + + inline wuffs_base__image_decoder* + upcast_as__wuffs_base__image_decoder() { + return (wuffs_base__image_decoder*)this; + } + + inline wuffs_base__empty_struct + set_quirk_enabled( + uint32_t a_quirk, + bool a_enabled) { + return wuffs_png__decoder__set_quirk_enabled(this, a_quirk, a_enabled); + } + + inline wuffs_base__status + decode_image_config( + wuffs_base__image_config* a_dst, + wuffs_base__io_buffer* a_src) { + return wuffs_png__decoder__decode_image_config(this, a_dst, a_src); + } + + inline wuffs_base__status + decode_frame_config( + wuffs_base__frame_config* a_dst, + wuffs_base__io_buffer* a_src) { + return wuffs_png__decoder__decode_frame_config(this, a_dst, a_src); + } + + inline wuffs_base__status + decode_frame( + wuffs_base__pixel_buffer* a_dst, + wuffs_base__io_buffer* a_src, + wuffs_base__pixel_blend a_blend, + wuffs_base__slice_u8 a_workbuf, + wuffs_base__decode_frame_options* a_opts) { + return wuffs_png__decoder__decode_frame(this, a_dst, a_src, a_blend, a_workbuf, a_opts); + } + + inline wuffs_base__rect_ie_u32 + frame_dirty_rect() const { + return wuffs_png__decoder__frame_dirty_rect(this); + } + + inline uint32_t + num_animation_loops() const { + return wuffs_png__decoder__num_animation_loops(this); + } + + inline uint64_t + num_decoded_frame_configs() const { + return wuffs_png__decoder__num_decoded_frame_configs(this); + } + + inline uint64_t + num_decoded_frames() const { + return wuffs_png__decoder__num_decoded_frames(this); + } + + inline wuffs_base__status + restart_frame( + uint64_t a_index, + uint64_t a_io_position) { + return wuffs_png__decoder__restart_frame(this, a_index, a_io_position); + } + + inline wuffs_base__empty_struct + set_report_metadata( + uint32_t a_fourcc, + bool a_report) { + return wuffs_png__decoder__set_report_metadata(this, a_fourcc, a_report); + } + + inline wuffs_base__status + tell_me_more( + wuffs_base__io_buffer* a_dst, + wuffs_base__more_information* a_minfo, + wuffs_base__io_buffer* a_src) { + return wuffs_png__decoder__tell_me_more(this, a_dst, a_minfo, a_src); + } + + inline wuffs_base__range_ii_u64 + workbuf_len() const { + return wuffs_png__decoder__workbuf_len(this); + } + +#endif // __cplusplus +}; // struct wuffs_png__decoder__struct + +#endif // defined(__cplusplus) || defined(WUFFS_IMPLEMENTATION) + +#ifdef __cplusplus +} // extern "C" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +// ---------------- Status Codes + extern const char wuffs_wbmp__error__bad_header[]; // ---------------- Public Consts @@ -28990,6 +29311,896 @@ wuffs_nie__decoder__workbuf_len( #endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__NIE) +#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__PNG) + +// ---------------- Status Codes Implementations + +const char wuffs_png__error__bad_header[] = "#png: bad header"; +const char wuffs_png__error__unsupported_png_file[] = "#png: unsupported PNG file"; + +// ---------------- Private Consts + +// ---------------- Private Initializer Prototypes + +// ---------------- Private Function Prototypes + +// ---------------- VTables + +const wuffs_base__image_decoder__func_ptrs +wuffs_png__decoder__func_ptrs_for__wuffs_base__image_decoder = { + (wuffs_base__status(*)(void*, + wuffs_base__pixel_buffer*, + wuffs_base__io_buffer*, + wuffs_base__pixel_blend, + wuffs_base__slice_u8, + wuffs_base__decode_frame_options*))(&wuffs_png__decoder__decode_frame), + (wuffs_base__status(*)(void*, + wuffs_base__frame_config*, + wuffs_base__io_buffer*))(&wuffs_png__decoder__decode_frame_config), + (wuffs_base__status(*)(void*, + wuffs_base__image_config*, + wuffs_base__io_buffer*))(&wuffs_png__decoder__decode_image_config), + (wuffs_base__rect_ie_u32(*)(const void*))(&wuffs_png__decoder__frame_dirty_rect), + (uint32_t(*)(const void*))(&wuffs_png__decoder__num_animation_loops), + (uint64_t(*)(const void*))(&wuffs_png__decoder__num_decoded_frame_configs), + (uint64_t(*)(const void*))(&wuffs_png__decoder__num_decoded_frames), + (wuffs_base__status(*)(void*, + uint64_t, + uint64_t))(&wuffs_png__decoder__restart_frame), + (wuffs_base__empty_struct(*)(void*, + uint32_t, + bool))(&wuffs_png__decoder__set_quirk_enabled), + (wuffs_base__empty_struct(*)(void*, + uint32_t, + bool))(&wuffs_png__decoder__set_report_metadata), + (wuffs_base__status(*)(void*, + wuffs_base__io_buffer*, + wuffs_base__more_information*, + wuffs_base__io_buffer*))(&wuffs_png__decoder__tell_me_more), + (wuffs_base__range_ii_u64(*)(const void*))(&wuffs_png__decoder__workbuf_len), +}; + +// ---------------- Initializer Implementations + +wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT +wuffs_png__decoder__initialize( + wuffs_png__decoder* self, + size_t sizeof_star_self, + uint64_t wuffs_version, + uint32_t options){ + if (!self) { + return wuffs_base__make_status(wuffs_base__error__bad_receiver); + } + if (sizeof(*self) != sizeof_star_self) { + return wuffs_base__make_status(wuffs_base__error__bad_sizeof_receiver); + } + if (((wuffs_version >> 32) != WUFFS_VERSION_MAJOR) || + (((wuffs_version >> 16) & 0xFFFF) > WUFFS_VERSION_MINOR)) { + return wuffs_base__make_status(wuffs_base__error__bad_wuffs_version); + } + + if ((options & WUFFS_INITIALIZE__ALREADY_ZEROED) != 0) { + // The whole point of this if-check is to detect an uninitialized *self. + // We disable the warning on GCC. Clang-5.0 does not have this warning. +#if !defined(__clang__) && defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +#endif + if (self->private_impl.magic != 0) { + return wuffs_base__make_status(wuffs_base__error__initialize_falsely_claimed_already_zeroed); + } +#if !defined(__clang__) && defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + } else { + if ((options & WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED) == 0) { + memset(self, 0, sizeof(*self)); + options |= WUFFS_INITIALIZE__ALREADY_ZEROED; + } else { + memset(&(self->private_impl), 0, sizeof(self->private_impl)); + } + } + + self->private_impl.magic = WUFFS_BASE__MAGIC; + self->private_impl.vtable_for__wuffs_base__image_decoder.vtable_name = + wuffs_base__image_decoder__vtable_name; + self->private_impl.vtable_for__wuffs_base__image_decoder.function_pointers = + (const void*)(&wuffs_png__decoder__func_ptrs_for__wuffs_base__image_decoder); + return wuffs_base__make_status(NULL); +} + +wuffs_png__decoder* +wuffs_png__decoder__alloc() { + wuffs_png__decoder* x = + (wuffs_png__decoder*)(calloc(sizeof(wuffs_png__decoder), 1)); + if (!x) { + return NULL; + } + if (wuffs_png__decoder__initialize( + x, sizeof(wuffs_png__decoder), WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED).repr) { + free(x); + return NULL; + } + return x; +} + +size_t +sizeof__wuffs_png__decoder() { + return sizeof(wuffs_png__decoder); +} + +// ---------------- Function Implementations + +// -------- func png.decoder.set_quirk_enabled + +WUFFS_BASE__MAYBE_STATIC wuffs_base__empty_struct +wuffs_png__decoder__set_quirk_enabled( + wuffs_png__decoder* self, + uint32_t a_quirk, + bool a_enabled) { + return wuffs_base__make_empty_struct(); +} + +// -------- func png.decoder.decode_image_config + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_png__decoder__decode_image_config( + wuffs_png__decoder* self, + wuffs_base__image_config* a_dst, + wuffs_base__io_buffer* a_src) { + if (!self) { + return wuffs_base__make_status(wuffs_base__error__bad_receiver); + } + if (self->private_impl.magic != WUFFS_BASE__MAGIC) { + return wuffs_base__make_status( + (self->private_impl.magic == WUFFS_BASE__DISABLED) + ? wuffs_base__error__disabled_by_previous_error + : wuffs_base__error__initialize_not_called); + } + if (!a_src) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + return wuffs_base__make_status(wuffs_base__error__bad_argument); + } + if ((self->private_impl.active_coroutine != 0) && + (self->private_impl.active_coroutine != 1)) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls); + } + self->private_impl.active_coroutine = 0; + wuffs_base__status status = wuffs_base__make_status(NULL); + + uint64_t v_magic = 0; + uint32_t v_a32 = 0; + uint8_t v_a8 = 0; + uint32_t v_dst_pixfmt = 0; + + const uint8_t* iop_a_src = NULL; + const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + if (a_src) { + io0_a_src = a_src->data.ptr; + io1_a_src = io0_a_src + a_src->meta.ri; + iop_a_src = io1_a_src; + io2_a_src = io0_a_src + a_src->meta.wi; + } + + uint32_t coro_susp_point = self->private_impl.p_decode_image_config[0]; + if (coro_susp_point) { + v_dst_pixfmt = self->private_data.s_decode_image_config[0].v_dst_pixfmt; + } + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + if (self->private_impl.f_call_sequence != 0) { + status = wuffs_base__make_status(wuffs_base__error__bad_call_sequence); + goto exit; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + uint64_t t_0; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 8)) { + t_0 = wuffs_base__load_u64le__no_bounds_check(iop_a_src); + iop_a_src += 8; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_0 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_0; + if (num_bits_0 == 56) { + t_0 = ((uint64_t)(*scratch)); + break; + } + num_bits_0 += 8; + *scratch |= ((uint64_t)(num_bits_0)) << 56; + } + } + v_magic = t_0; + } + if (v_magic != 727905341920923785) { + status = wuffs_base__make_status(wuffs_png__error__bad_header); + goto exit; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(3); + uint64_t t_1; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 8)) { + t_1 = wuffs_base__load_u64le__no_bounds_check(iop_a_src); + iop_a_src += 8; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_1 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_1; + if (num_bits_1 == 56) { + t_1 = ((uint64_t)(*scratch)); + break; + } + num_bits_1 += 8; + *scratch |= ((uint64_t)(num_bits_1)) << 56; + } + } + v_magic = t_1; + } + if (v_magic != 5927942488114331648) { + status = wuffs_base__make_status(wuffs_png__error__bad_header); + goto exit; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(5); + uint32_t t_2; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_2 = wuffs_base__load_u32be__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_2 = ((uint32_t)(*scratch & 0xFF)); + *scratch >>= 8; + *scratch <<= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_2); + if (num_bits_2 == 24) { + t_2 = ((uint32_t)(*scratch >> 32)); + break; + } + num_bits_2 += 8; + *scratch |= ((uint64_t)(num_bits_2)); + } + } + v_a32 = t_2; + } + if (v_a32 >= 2147483648) { + status = wuffs_base__make_status(wuffs_png__error__bad_header); + goto exit; + } else if (v_a32 >= 16777216) { + status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); + goto exit; + } + self->private_impl.f_width = v_a32; + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7); + uint32_t t_3; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_3 = wuffs_base__load_u32be__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_3 = ((uint32_t)(*scratch & 0xFF)); + *scratch >>= 8; + *scratch <<= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_3); + if (num_bits_3 == 24) { + t_3 = ((uint32_t)(*scratch >> 32)); + break; + } + num_bits_3 += 8; + *scratch |= ((uint64_t)(num_bits_3)); + } + } + v_a32 = t_3; + } + if (v_a32 >= 2147483648) { + status = wuffs_base__make_status(wuffs_png__error__bad_header); + goto exit; + } else if (v_a32 >= 16777216) { + status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); + goto exit; + } + self->private_impl.f_height = v_a32; + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(9); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_4 = *iop_a_src++; + v_a8 = t_4; + } + if (v_a8 > 16) { + status = wuffs_base__make_status(wuffs_png__error__bad_header); + goto exit; + } + self->private_impl.f_depth = v_a8; + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(10); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_5 = *iop_a_src++; + v_a8 = t_5; + } + if (v_a8 == 0) { + v_dst_pixfmt = 536870920; + self->private_impl.f_src_pixfmt = 536870920; + if (self->private_impl.f_depth == 8) { + self->private_impl.f_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * 1); + } else { + status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); + goto exit; + } + } else if (v_a8 == 2) { + v_dst_pixfmt = 2147485832; + self->private_impl.f_src_pixfmt = 2684356744; + if (self->private_impl.f_depth == 8) { + self->private_impl.f_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * 3); + } else { + status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); + goto exit; + } + } else { + status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); + goto exit; + } + self->private_impl.f_workbuf_length = (((uint64_t)(self->private_impl.f_height)) * (1 + self->private_impl.f_bytes_per_row)); + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(11); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_6 = *iop_a_src++; + v_a8 = t_6; + } + if (v_a8 != 0) { + status = wuffs_base__make_status(wuffs_png__error__bad_header); + goto exit; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(12); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_7 = *iop_a_src++; + v_a8 = t_7; + } + if (v_a8 != 0) { + status = wuffs_base__make_status(wuffs_png__error__bad_header); + goto exit; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(13); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint8_t t_8 = *iop_a_src++; + v_a8 = t_8; + } + if (v_a8 == 0) { + } else if (v_a8 == 1) { + status = wuffs_base__make_status(wuffs_png__error__unsupported_png_file); + goto exit; + } else { + status = wuffs_base__make_status(wuffs_png__error__bad_header); + goto exit; + } + self->private_data.s_decode_image_config[0].scratch = 4; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(14); + if (self->private_data.s_decode_image_config[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) { + self->private_data.s_decode_image_config[0].scratch -= ((uint64_t)(io2_a_src - iop_a_src)); + iop_a_src = io2_a_src; + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + iop_a_src += self->private_data.s_decode_image_config[0].scratch; + while (true) { + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(15); + uint32_t t_9; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_9 = wuffs_base__load_u32be__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(16); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_9 = ((uint32_t)(*scratch & 0xFF)); + *scratch >>= 8; + *scratch <<= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << (56 - num_bits_9); + if (num_bits_9 == 24) { + t_9 = ((uint32_t)(*scratch >> 32)); + break; + } + num_bits_9 += 8; + *scratch |= ((uint64_t)(num_bits_9)); + } + } + self->private_impl.f_chunk_length = t_9; + } + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(17); + uint32_t t_10; + if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= 4)) { + t_10 = wuffs_base__load_u32le__no_bounds_check(iop_a_src); + iop_a_src += 4; + } else { + self->private_data.s_decode_image_config[0].scratch = 0; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(18); + while (true) { + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + uint64_t* scratch = &self->private_data.s_decode_image_config[0].scratch; + uint32_t num_bits_10 = ((uint32_t)(*scratch >> 56)); + *scratch <<= 8; + *scratch >>= 8; + *scratch |= ((uint64_t)(*iop_a_src++)) << num_bits_10; + if (num_bits_10 == 24) { + t_10 = ((uint32_t)(*scratch)); + break; + } + num_bits_10 += 8; + *scratch |= ((uint64_t)(num_bits_10)) << 56; + } + } + self->private_impl.f_chunk_type = t_10; + } + if (self->private_impl.f_chunk_type == 1413563465) { + goto label__0__break; + } + self->private_data.s_decode_image_config[0].scratch = self->private_impl.f_chunk_length; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(19); + if (self->private_data.s_decode_image_config[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) { + self->private_data.s_decode_image_config[0].scratch -= ((uint64_t)(io2_a_src - iop_a_src)); + iop_a_src = io2_a_src; + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + iop_a_src += self->private_data.s_decode_image_config[0].scratch; + self->private_data.s_decode_image_config[0].scratch = 4; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(20); + if (self->private_data.s_decode_image_config[0].scratch > ((uint64_t)(io2_a_src - iop_a_src))) { + self->private_data.s_decode_image_config[0].scratch -= ((uint64_t)(io2_a_src - iop_a_src)); + iop_a_src = io2_a_src; + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + iop_a_src += self->private_data.s_decode_image_config[0].scratch; + } + label__0__break:; + self->private_impl.f_frame_config_io_position = wuffs_base__u64__sat_add(a_src->meta.pos, ((uint64_t)(iop_a_src - io0_a_src))); + if (a_dst != NULL) { + wuffs_base__image_config__set( + a_dst, + v_dst_pixfmt, + 0, + self->private_impl.f_width, + self->private_impl.f_height, + self->private_impl.f_frame_config_io_position, + false); + } + self->private_impl.f_call_sequence = 3; + + goto ok; + ok: + self->private_impl.p_decode_image_config[0] = 0; + goto exit; + } + + goto suspend; + suspend: + self->private_impl.p_decode_image_config[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; + self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 1 : 0; + self->private_data.s_decode_image_config[0].v_dst_pixfmt = v_dst_pixfmt; + + goto exit; + exit: + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + + if (wuffs_base__status__is_error(&status)) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + } + return status; +} + +// -------- func png.decoder.decode_frame_config + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_png__decoder__decode_frame_config( + wuffs_png__decoder* self, + wuffs_base__frame_config* a_dst, + wuffs_base__io_buffer* a_src) { + if (!self) { + return wuffs_base__make_status(wuffs_base__error__bad_receiver); + } + if (self->private_impl.magic != WUFFS_BASE__MAGIC) { + return wuffs_base__make_status( + (self->private_impl.magic == WUFFS_BASE__DISABLED) + ? wuffs_base__error__disabled_by_previous_error + : wuffs_base__error__initialize_not_called); + } + if (!a_src) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + return wuffs_base__make_status(wuffs_base__error__bad_argument); + } + if ((self->private_impl.active_coroutine != 0) && + (self->private_impl.active_coroutine != 2)) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls); + } + self->private_impl.active_coroutine = 0; + wuffs_base__status status = wuffs_base__make_status(NULL); + + const uint8_t* iop_a_src = NULL; + const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + const uint8_t* io1_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + const uint8_t* io2_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; + if (a_src) { + io0_a_src = a_src->data.ptr; + io1_a_src = io0_a_src + a_src->meta.ri; + iop_a_src = io1_a_src; + io2_a_src = io0_a_src + a_src->meta.wi; + } + + uint32_t coro_susp_point = self->private_impl.p_decode_frame_config[0]; + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + if (self->private_impl.f_call_sequence < 3) { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + status = wuffs_png__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_frame_config_io_position != wuffs_base__u64__sat_add(a_src->meta.pos, ((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; + 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 (a_dst != NULL) { + wuffs_base__frame_config__set( + a_dst, + wuffs_base__utility__make_rect_ie_u32( + 0, + 0, + self->private_impl.f_width, + self->private_impl.f_height), + ((wuffs_base__flicks)(0)), + 0, + self->private_impl.f_frame_config_io_position, + 0, + false, + false, + 0); + } + self->private_impl.f_call_sequence = 4; + + goto ok; + ok: + self->private_impl.p_decode_frame_config[0] = 0; + goto exit; + } + + goto suspend; + suspend: + self->private_impl.p_decode_frame_config[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; + self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 2 : 0; + + goto exit; + exit: + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + + if (wuffs_base__status__is_error(&status)) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + } + return status; +} + +// -------- func png.decoder.decode_frame + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_png__decoder__decode_frame( + wuffs_png__decoder* self, + wuffs_base__pixel_buffer* a_dst, + wuffs_base__io_buffer* a_src, + wuffs_base__pixel_blend a_blend, + wuffs_base__slice_u8 a_workbuf, + wuffs_base__decode_frame_options* a_opts) { + if (!self) { + return wuffs_base__make_status(wuffs_base__error__bad_receiver); + } + if (self->private_impl.magic != WUFFS_BASE__MAGIC) { + return wuffs_base__make_status( + (self->private_impl.magic == WUFFS_BASE__DISABLED) + ? wuffs_base__error__disabled_by_previous_error + : wuffs_base__error__initialize_not_called); + } + if (!a_dst || !a_src) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + return wuffs_base__make_status(wuffs_base__error__bad_argument); + } + if ((self->private_impl.active_coroutine != 0) && + (self->private_impl.active_coroutine != 3)) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls); + } + self->private_impl.active_coroutine = 0; + wuffs_base__status status = wuffs_base__make_status(NULL); + + uint32_t coro_susp_point = self->private_impl.p_decode_frame[0]; + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + if (self->private_impl.f_call_sequence < 4) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + status = wuffs_png__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; + } + self->private_impl.f_call_sequence = 255; + + goto ok; + ok: + self->private_impl.p_decode_frame[0] = 0; + goto exit; + } + + goto suspend; + suspend: + self->private_impl.p_decode_frame[0] = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; + self->private_impl.active_coroutine = wuffs_base__status__is_suspension(&status) ? 3 : 0; + + goto exit; + exit: + if (wuffs_base__status__is_error(&status)) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + } + return status; +} + +// -------- func png.decoder.frame_dirty_rect + +WUFFS_BASE__MAYBE_STATIC wuffs_base__rect_ie_u32 +wuffs_png__decoder__frame_dirty_rect( + const wuffs_png__decoder* self) { + if (!self) { + return wuffs_base__utility__empty_rect_ie_u32(); + } + if ((self->private_impl.magic != WUFFS_BASE__MAGIC) && + (self->private_impl.magic != WUFFS_BASE__DISABLED)) { + return wuffs_base__utility__empty_rect_ie_u32(); + } + + return wuffs_base__utility__make_rect_ie_u32( + 0, + 0, + self->private_impl.f_width, + self->private_impl.f_height); +} + +// -------- func png.decoder.num_animation_loops + +WUFFS_BASE__MAYBE_STATIC uint32_t +wuffs_png__decoder__num_animation_loops( + const wuffs_png__decoder* self) { + if (!self) { + return 0; + } + if ((self->private_impl.magic != WUFFS_BASE__MAGIC) && + (self->private_impl.magic != WUFFS_BASE__DISABLED)) { + return 0; + } + + return 0; +} + +// -------- func png.decoder.num_decoded_frame_configs + +WUFFS_BASE__MAYBE_STATIC uint64_t +wuffs_png__decoder__num_decoded_frame_configs( + const wuffs_png__decoder* self) { + if (!self) { + return 0; + } + if ((self->private_impl.magic != WUFFS_BASE__MAGIC) && + (self->private_impl.magic != WUFFS_BASE__DISABLED)) { + return 0; + } + + if (self->private_impl.f_call_sequence > 3) { + return 1; + } + return 0; +} + +// -------- func png.decoder.num_decoded_frames + +WUFFS_BASE__MAYBE_STATIC uint64_t +wuffs_png__decoder__num_decoded_frames( + const wuffs_png__decoder* self) { + if (!self) { + return 0; + } + if ((self->private_impl.magic != WUFFS_BASE__MAGIC) && + (self->private_impl.magic != WUFFS_BASE__DISABLED)) { + return 0; + } + + if (self->private_impl.f_call_sequence > 4) { + return 1; + } + return 0; +} + +// -------- func png.decoder.restart_frame + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_png__decoder__restart_frame( + wuffs_png__decoder* self, + uint64_t a_index, + uint64_t a_io_position) { + if (!self) { + return wuffs_base__make_status(wuffs_base__error__bad_receiver); + } + if (self->private_impl.magic != WUFFS_BASE__MAGIC) { + return wuffs_base__make_status( + (self->private_impl.magic == WUFFS_BASE__DISABLED) + ? wuffs_base__error__disabled_by_previous_error + : wuffs_base__error__initialize_not_called); + } + + if (self->private_impl.f_call_sequence < 3) { + 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_frame_config_io_position = a_io_position; + return wuffs_base__make_status(NULL); +} + +// -------- func png.decoder.set_report_metadata + +WUFFS_BASE__MAYBE_STATIC wuffs_base__empty_struct +wuffs_png__decoder__set_report_metadata( + wuffs_png__decoder* self, + uint32_t a_fourcc, + bool a_report) { + return wuffs_base__make_empty_struct(); +} + +// -------- func png.decoder.tell_me_more + +WUFFS_BASE__MAYBE_STATIC wuffs_base__status +wuffs_png__decoder__tell_me_more( + wuffs_png__decoder* self, + wuffs_base__io_buffer* a_dst, + wuffs_base__more_information* a_minfo, + wuffs_base__io_buffer* a_src) { + if (!self) { + return wuffs_base__make_status(wuffs_base__error__bad_receiver); + } + if (self->private_impl.magic != WUFFS_BASE__MAGIC) { + return wuffs_base__make_status( + (self->private_impl.magic == WUFFS_BASE__DISABLED) + ? wuffs_base__error__disabled_by_previous_error + : wuffs_base__error__initialize_not_called); + } + if (!a_dst || !a_src) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + return wuffs_base__make_status(wuffs_base__error__bad_argument); + } + if ((self->private_impl.active_coroutine != 0) && + (self->private_impl.active_coroutine != 4)) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls); + } + self->private_impl.active_coroutine = 0; + wuffs_base__status status = wuffs_base__make_status(NULL); + + status = wuffs_base__make_status(wuffs_base__error__no_more_information); + goto exit; + + goto ok; + ok: + goto exit; + exit: + if (wuffs_base__status__is_error(&status)) { + self->private_impl.magic = WUFFS_BASE__DISABLED; + } + return status; +} + +// -------- func png.decoder.workbuf_len + +WUFFS_BASE__MAYBE_STATIC wuffs_base__range_ii_u64 +wuffs_png__decoder__workbuf_len( + const wuffs_png__decoder* self) { + if (!self) { + return wuffs_base__utility__empty_range_ii_u64(); + } + if ((self->private_impl.magic != WUFFS_BASE__MAGIC) && + (self->private_impl.magic != WUFFS_BASE__DISABLED)) { + return wuffs_base__utility__empty_range_ii_u64(); + } + + return wuffs_base__utility__make_range_ii_u64(self->private_impl.f_workbuf_length, self->private_impl.f_workbuf_length); +} + +#endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__PNG) + #if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__WBMP) // ---------------- Status Codes Implementations diff --git a/std/png/decode_png.wuffs b/std/png/decode_png.wuffs new file mode 100644 index 00000000..208b878e --- /dev/null +++ b/std/png/decode_png.wuffs @@ -0,0 +1,287 @@ +// Copyright 2020 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub status "#bad header" +pub status "#unsupported PNG file" + +pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0 + +pub struct decoder? implements base.image_decoder( + // The 0x00FF_FFFF limit is arbitrary (the PNG spec says 0x7FFF_FFFF) but + // it means that (width * height * bytes_per_pixel) doesn't overflow. + width : base.u32[..= 0x00FF_FFFF], + height : base.u32[..= 0x00FF_FFFF], + + // bytes_per_row doesn't include the 1 byte for the per-row filter. + bytes_per_row : base.u64[..= 0x07FF_FFF8], + + workbuf_length : base.u64[..= 0x0007_FFFF_F100_0007], + + // 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 + call_sequence : base.u8, + + depth : base.u8[..= 16], + + src_pixfmt : base.u32, + + chunk_length : base.u32, + chunk_type : base.u32, + + frame_config_io_position : base.u64, + + swizzler : base.pixel_swizzler, + util : base.utility, +)( +) + +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 magic : base.u64 + var a32 : base.u32 + var a8 : base.u8 + var dst_pixfmt : base.u32 + + if this.call_sequence <> 0 { + return base."#bad call sequence" + } + + magic = args.src.read_u64le?() + if magic <> '\x89PNG\x0D\x0A\x1A\x0A'le { + return "#bad header" + } + + magic = args.src.read_u64le?() + if magic <> '\x00\x00\x00\x0DIHDR'le { + return "#bad header" + } + + a32 = args.src.read_u32be?() + if a32 >= 0x8000_0000 { + return "#bad header" + } else if a32 >= 0x0100_0000 { + return "#unsupported PNG file" + } + this.width = a32 + + a32 = args.src.read_u32be?() + if a32 >= 0x8000_0000 { + return "#bad header" + } else if a32 >= 0x0100_0000 { + return "#unsupported PNG file" + } + this.height = a32 + + // Depth. + a8 = args.src.read_u8?() + if a8 > 16 { + return "#bad header" + } + this.depth = a8 + + // Color. + a8 = args.src.read_u8?() + if a8 == 0 { + dst_pixfmt = base.PIXEL_FORMAT__Y + this.src_pixfmt = base.PIXEL_FORMAT__Y + if this.depth == 8 { + this.bytes_per_row = (this.width as base.u64) * 1 + } else { + return "#unsupported PNG file" + } + } else if a8 == 2 { + dst_pixfmt = base.PIXEL_FORMAT__BGR + this.src_pixfmt = base.PIXEL_FORMAT__RGB + if this.depth == 8 { + this.bytes_per_row = (this.width as base.u64) * 3 + } else { + return "#unsupported PNG file" + } + } else { + return "#unsupported PNG file" + } + this.workbuf_length = (this.height as base.u64) * (1 + this.bytes_per_row) + + // Compression. + a8 = args.src.read_u8?() + if a8 <> 0 { + return "#bad header" + } + + // Filter. + a8 = args.src.read_u8?() + if a8 <> 0 { + return "#bad header" + } + + // Interlace. + a8 = args.src.read_u8?() + if a8 == 0 { + // No-op. + } else if a8 == 1 { + return "#unsupported PNG file" + } else { + return "#bad header" + } + + // TODO: verify CRC-32 checksum. + args.src.skip_u32?(n: 4) + + // Read up until an IDAT chunk. + while true { + this.chunk_length = args.src.read_u32be?() + this.chunk_type = args.src.read_u32le?() + if this.chunk_type == 'IDAT'le { + break + } + args.src.skip_u32?(n: this.chunk_length) + // TODO: verify CRC-32 checksum. + args.src.skip_u32?(n: 4) + } endwhile + + this.frame_config_io_position = args.src.position() + + if args.dst <> nullptr { + args.dst.set!( + pixfmt: dst_pixfmt, + pixsub: 0, + width: this.width, + height: this.height, + first_frame_io_position: this.frame_config_io_position, + first_frame_is_opaque: false) + } + + this.call_sequence = 3 +} + +pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_reader) { + if this.call_sequence < 3 { + this.decode_image_config?(dst: nullptr, src: args.src) + } else if this.call_sequence == 3 { + if this.frame_config_io_position <> args.src.position() { + return base."#bad restart" + } + } else if this.call_sequence == 4 { + this.call_sequence = 0xFF + return base."@end of data" + } else { + return base."@end of data" + } + + if args.dst <> nullptr { + args.dst.set!(bounds: this.util.make_rect_ie_u32( + min_incl_x: 0, + min_incl_y: 0, + max_excl_x: this.width, + max_excl_y: this.height), + duration: 0, + index: 0, + io_position: this.frame_config_io_position, + disposal: 0, + opaque_within_bounds: false, + overwrite_instead_of_blend: false, + background_color: 0x0000_0000) + } + + this.call_sequence = 4 +} + +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) { + if this.call_sequence < 4 { + this.decode_frame_config?(dst: nullptr, src: args.src) + } else if this.call_sequence == 4 { + // No-op. + } else { + return base."@end of data" + } + + this.call_sequence = 0xFF +} + +pub func decoder.frame_dirty_rect() base.rect_ie_u32 { + return this.util.make_rect_ie_u32( + min_incl_x: 0, + min_incl_y: 0, + max_excl_x: this.width, + max_excl_y: this.height) +} + +pub func decoder.num_animation_loops() base.u32 { + return 0 +} + +pub func decoder.num_decoded_frame_configs() base.u64 { + if this.call_sequence > 3 { + return 1 + } + return 0 +} + +pub func decoder.num_decoded_frames() base.u64 { + if this.call_sequence > 4 { + return 1 + } + return 0 +} + +pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.status { + if this.call_sequence < 3 { + return base."#bad call sequence" + } + if args.index <> 0 { + return base."#bad argument" + } + this.call_sequence = 3 + this.frame_config_io_position = args.io_position + return ok +} + +pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) { + // TODO. +} + +pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) { + return base."#no more information" +} + +pub func decoder.workbuf_len() base.range_ii_u64 { + return this.util.make_range_ii_u64( + min_incl: this.workbuf_length, + max_incl: this.workbuf_length) +} diff --git a/test/c/std/png.c b/test/c/std/png.c new file mode 100644 index 00000000..e46beb7d --- /dev/null +++ b/test/c/std/png.c @@ -0,0 +1,168 @@ +// Copyright 2020 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// ---------------- + +/* +This test program is typically run indirectly, by the "wuffs test" or "wuffs +bench" commands. These commands take an optional "-mimic" flag to check that +Wuffs' output mimics (i.e. exactly matches) other libraries' output, such as +giflib for GIF, libpng for PNG, etc. + +To manually run this test: + +for CC in clang gcc; do + $CC -std=c99 -Wall -Werror png.c && ./a.out + rm -f a.out +done + +Each edition should print "PASS", amongst other information, and exit(0). + +Add the "wuffs mimic cflags" (everything after the colon below) to the C +compiler flags (after the .c file) to run the mimic tests. + +To manually run the benchmarks, replace "-Wall -Werror" with "-O3" and replace +the first "./a.out" with "./a.out -bench". Combine these changes with the +"wuffs mimic cflags" to run the mimic benchmarks. +*/ + +// !! wuffs mimic cflags: -DWUFFS_MIMIC + +// Wuffs ships as a "single file C library" or "header file library" as per +// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt +// +// To use that single file as a "foo.c"-like implementation, instead of a +// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or +// compiling it. +#define WUFFS_IMPLEMENTATION + +// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of +// release/c/etc.c whitelist which parts of Wuffs to build. That file contains +// the entire Wuffs standard library, implementing a variety of codecs and file +// formats. Without this macro definition, an optimizing compiler or linker may +// very well discard Wuffs code for unused codecs, but listing the Wuffs +// modules we use makes that process explicit. Preprocessing means that such +// code simply isn't compiled. +#define WUFFS_CONFIG__MODULES +#define WUFFS_CONFIG__MODULE__ADLER32 +#define WUFFS_CONFIG__MODULE__BASE +#define WUFFS_CONFIG__MODULE__CRC32 +#define WUFFS_CONFIG__MODULE__DEFLATE +#define WUFFS_CONFIG__MODULE__PNG +#define WUFFS_CONFIG__MODULE__ZLIB + +// If building this program in an environment that doesn't easily accommodate +// relative includes, you can use the script/inline-c-relative-includes.go +// program to generate a stand-alone C file. +#include "../../../release/c/wuffs-unsupported-snapshot.c" +#include "../testlib/testlib.c" +#ifdef WUFFS_MIMIC +// No mimic library. +#endif + +// -------- + +const char* // +test_wuffs_png_decode_interface() { + CHECK_FOCUS(__func__); + wuffs_png__decoder dec; + CHECK_STATUS("initialize", + wuffs_png__decoder__initialize( + &dec, sizeof dec, WUFFS_VERSION, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED)); + return do_test__wuffs_base__image_decoder( + wuffs_png__decoder__upcast_as__wuffs_base__image_decoder(&dec), + "test/data/bricks-gray.png", 0, SIZE_MAX, 160, 120, 0); // TODO. +} + +const char* // +test_wuffs_png_decode_frame_config() { + CHECK_FOCUS(__func__); + wuffs_png__decoder dec; + CHECK_STATUS("initialize", + wuffs_png__decoder__initialize( + &dec, sizeof dec, WUFFS_VERSION, + WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED)); + + wuffs_base__frame_config fc = ((wuffs_base__frame_config){}); + wuffs_base__io_buffer src = ((wuffs_base__io_buffer){ + .data = g_src_slice_u8, + }); + CHECK_STRING(read_file(&src, "test/data/hibiscus.regular.png")); + CHECK_STATUS("decode_frame_config #0", + wuffs_png__decoder__decode_frame_config(&dec, &fc, &src)); + + wuffs_base__status status = + wuffs_png__decoder__decode_frame_config(&dec, &fc, &src); + if (status.repr != wuffs_base__note__end_of_data) { + RETURN_FAIL("decode_frame_config #1: have \"%s\", want \"%s\"", status.repr, + wuffs_base__note__end_of_data); + } + return NULL; +} + +// ---------------- Mimic Tests + +#ifdef WUFFS_MIMIC + +// No mimic tests. + +#endif // WUFFS_MIMIC + +// ---------------- PNG Benches + +// No PNG benches. + +// ---------------- Mimic Benches + +#ifdef WUFFS_MIMIC + +// No mimic benches. + +#endif // WUFFS_MIMIC + +// ---------------- Manifest + +proc g_tests[] = { + + test_wuffs_png_decode_frame_config, + test_wuffs_png_decode_interface, + +#ifdef WUFFS_MIMIC + +// No mimic tests. + +#endif // WUFFS_MIMIC + + NULL, +}; + +proc g_benches[] = { + +// No PNG benches. + +#ifdef WUFFS_MIMIC + +// No mimic benches. + +#endif // WUFFS_MIMIC + + NULL, +}; + +int // +main(int argc, char** argv) { + g_proc_package_name = "std/png"; + return test_main(argc, argv, g_tests, g_benches); +}