diff --git a/internal/cgen/base/image-private.h b/internal/cgen/base/image-private.h index ab5d212f..2fa673a7 100644 --- a/internal/cgen/base/image-private.h +++ b/internal/cgen/base/image-private.h @@ -73,4 +73,5 @@ wuffs_base__pixel_swizzler__swizzle_ycck( // ---------------- Images (Utility) +#define wuffs_base__utility__make_pixel_blend wuffs_base__make_pixel_blend #define wuffs_base__utility__make_pixel_format wuffs_base__make_pixel_format diff --git a/internal/cgen/base/image-public.h b/internal/cgen/base/image-public.h index 6483fcf1..17663cd8 100644 --- a/internal/cgen/base/image-public.h +++ b/internal/cgen/base/image-public.h @@ -482,6 +482,11 @@ typedef uint8_t wuffs_base__pixel_blend; #define WUFFS_BASE__PIXEL_BLEND__SRC ((wuffs_base__pixel_blend)0) #define WUFFS_BASE__PIXEL_BLEND__SRC_OVER ((wuffs_base__pixel_blend)1) +static inline wuffs_base__pixel_blend // +wuffs_base__make_pixel_blend(uint8_t repr) { + return repr; +} + // -------- // wuffs_base__pixel_alpha_transparency is a pixel format's alpha channel diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 097deb89..f1d9afaf 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -115,6 +115,11 @@ var Consts = [...]struct { // ---- + {t.IDU8, "0", "PIXEL_BLEND__SRC"}, + {t.IDU8, "1", "PIXEL_BLEND__SRC_OVER"}, + + // ---- + {t.IDU32, "0x02000008", "PIXEL_FORMAT__A"}, {t.IDU32, "0x20000008", "PIXEL_FORMAT__Y"}, @@ -408,6 +413,7 @@ var funcsOther = [...]string{ "utility.i64_divide(a: u64, b: u64[1 ..=]) u64", "utility.make_bitvec256(e00: u64, e01: u64, e02: u64, e03: u64) bitvec256", "utility.make_optional_u63(has_value: bool, value: u64[..= 0x7FFF_FFFF_FFFF_FFFF]) optional_u63", + "utility.make_pixel_blend(repr: u8) pixel_blend", "utility.make_pixel_format(repr: u32) pixel_format", "utility.make_range_ii_u32(min_incl: u32, max_incl: u32) range_ii_u32", "utility.make_range_ie_u32(min_incl: u32, max_excl: u32) range_ie_u32", diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index d2d4a8cf..0f2cb757 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -4826,6 +4826,11 @@ typedef uint8_t wuffs_base__pixel_blend; #define WUFFS_BASE__PIXEL_BLEND__SRC ((wuffs_base__pixel_blend)0) #define WUFFS_BASE__PIXEL_BLEND__SRC_OVER ((wuffs_base__pixel_blend)1) +static inline wuffs_base__pixel_blend // +wuffs_base__make_pixel_blend(uint8_t repr) { + return repr; +} + // -------- // wuffs_base__pixel_alpha_transparency is a pixel format's alpha channel @@ -15467,9 +15472,11 @@ extern const char wuffs_vp8__error__unsupported_vp8_file[]; // ---------------- Public Consts -#define WUFFS_VP8__QUIRK_WIDTH_AND_HEIGHT 1836840960u +#define WUFFS_VP8__QUIRK_HAS_ALPHA_SIDE_CHANNEL 1836840960u -#define WUFFS_VP8__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 553648127u +#define WUFFS_VP8__QUIRK_WIDTH_AND_HEIGHT 1836840961u + +#define WUFFS_VP8__DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE 822050816u // ---------------- Struct Declarations @@ -15640,6 +15647,7 @@ struct wuffs_vp8__decoder__struct { uint32_t f_mbh; uint8_t f_call_sequence; bool f_is_key_frame; + bool f_quirk_has_alpha_side_channel; uint64_t f_frame_config_io_position; uint64_t f_quirk_source_length; uint64_t f_quirk_width_and_height; @@ -16411,9 +16419,11 @@ struct wuffs_webp__decoder__struct { bool f_sub_chunk_has_padding; uint8_t f_variant; uint8_t f_vp8x_header_flags; + uint8_t f_vp8x_alph_flags; uint64_t f_frame_config_io_position; uint32_t f_riff_chunk_length; uint32_t f_sub_chunk_length; + uint32_t f_sub_chunk_type; uint32_t f_bits; uint32_t f_n_bits; bool f_seen_transform[4]; @@ -16432,6 +16442,8 @@ struct wuffs_webp__decoder__struct { uint32_t f_workbuf_offset_for_color_indexing; wuffs_base__pixel_swizzler f_swizzler; + uint32_t p_decode_alph; + uint32_t p_decode_alph_limited; uint32_t p_decode_huffman_groups; uint32_t p_decode_huffman_tree; uint32_t p_decode_huffman_tree_simple; @@ -16525,7 +16537,6 @@ struct wuffs_webp__decoder__struct { uint64_t scratch; } s_do_decode_image_config_limited_vp8x; struct { - uint32_t v_chunk_type; uint64_t scratch; } s_do_decode_frame; } private_data; @@ -19197,6 +19208,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck( // ---------------- Images (Utility) +#define wuffs_base__utility__make_pixel_blend wuffs_base__make_pixel_blend #define wuffs_base__utility__make_pixel_format wuffs_base__make_pixel_format // ---------------- String Conversions @@ -82052,7 +82064,6 @@ WUFFS_BASE__GENERATED_C_CODE static wuffs_base__status wuffs_vp8__decoder__decode_macroblocks( wuffs_vp8__decoder* self, - wuffs_base__pixel_buffer* a_dst, wuffs_base__slice_u8 a_workbuf); WUFFS_BASE__GENERATED_C_CODE @@ -83956,11 +83967,9 @@ WUFFS_BASE__GENERATED_C_CODE static wuffs_base__status wuffs_vp8__decoder__decode_macroblocks( wuffs_vp8__decoder* self, - wuffs_base__pixel_buffer* a_dst, wuffs_base__slice_u8 a_workbuf) { uint32_t v_mby = 0; uint32_t v_mbx = 0; - wuffs_base__status v_status = wuffs_base__make_status(NULL); v_mbx = 0u; while (v_mbx < self->private_impl.f_mbw) { @@ -83992,10 +84001,6 @@ wuffs_vp8__decoder__decode_macroblocks( } else { wuffs_vp8__decoder__filter_normal(self, a_workbuf, (self->private_impl.f_mbh - 1u)); } - v_status = wuffs_vp8__decoder__swizzle(self, a_dst, a_workbuf); - if ( ! wuffs_base__status__is_ok(&v_status)) { - return wuffs_private_impl__status__ensure_not_a_suspension(v_status); - } } return wuffs_base__make_status(NULL); } @@ -85222,6 +85227,10 @@ wuffs_vp8__decoder__get_quirk( if (a_key == 3u) { return self->private_impl.f_quirk_source_length; } else if (a_key == 1836840960u) { + if (self->private_impl.f_quirk_has_alpha_side_channel) { + return 1u; + } + } else if (a_key == 1836840961u) { return self->private_impl.f_quirk_width_and_height; } return 0u; @@ -85265,6 +85274,9 @@ wuffs_vp8__decoder__set_quirk( } return wuffs_base__make_status(NULL); } else if (a_key == 1836840960u) { + self->private_impl.f_quirk_has_alpha_side_channel = (a_value != 0u); + return wuffs_base__make_status(NULL); + } else if (a_key == 1836840961u) { v_w = ((uint32_t)(a_value)); v_h = ((uint32_t)(a_value)); if ((v_w < 1u) || @@ -85727,37 +85739,61 @@ wuffs_vp8__decoder__decode_frame( self->private_impl.active_coroutine = 0; wuffs_base__status status = wuffs_base__make_status(NULL); + uint64_t v_wh = 0; uint64_t v_wb_len = 0; wuffs_base__status v_status = wuffs_base__make_status(NULL); + wuffs_base__slice_u8 v_trimmed = {0}; uint32_t coro_susp_point = self->private_impl.p_decode_frame; switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; while (true) { + v_trimmed = a_workbuf; + if (self->private_impl.f_quirk_has_alpha_side_channel) { + v_wh = ((uint64_t)((self->private_impl.f_width * self->private_impl.f_height))); + if (v_wh > ((uint64_t)(a_workbuf.len))) { + status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length); + goto exit; + } + v_trimmed = wuffs_base__slice_u8__subslice_i(a_workbuf, v_wh); + } v_wb_len = (self->private_impl.f_workbuf_yuv_v_end + ((uint64_t)(self->private_impl.f_partitioned_data_length)) + 8u); - if (v_wb_len > ((uint64_t)(a_workbuf.len))) { + if (v_wb_len > ((uint64_t)(v_trimmed.len))) { status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length); goto exit; } else { - a_workbuf = wuffs_base__slice_u8__subslice_j(a_workbuf, v_wb_len); + v_trimmed = wuffs_base__slice_u8__subslice_j(v_trimmed, v_wb_len); } { wuffs_base__status t_0 = wuffs_vp8__decoder__do_decode_frame(self, a_dst, a_src, a_blend, - a_workbuf, + v_trimmed, a_opts); v_status = t_0; } - if ((v_status.repr == wuffs_base__suspension__short_read) && (a_src && a_src->meta.closed)) { + if (wuffs_base__status__is_ok(&v_status)) { + break; + } else if ((v_status.repr == wuffs_base__suspension__short_read) && (a_src && a_src->meta.closed)) { status = wuffs_base__make_status(wuffs_vp8__error__truncated_input); goto exit; } status = v_status; WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1); } + v_status = wuffs_vp8__decoder__swizzle(self, a_dst, v_trimmed); + if ( ! wuffs_base__status__is_ok(&v_status)) { + status = v_status; + if (wuffs_base__status__is_error(&status)) { + goto exit; + } else if (wuffs_base__status__is_suspension(&status)) { + status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension); + goto exit; + } + goto ok; + } ok: self->private_impl.p_decode_frame = 0; @@ -85859,7 +85895,7 @@ wuffs_vp8__decoder__do_decode_frame( } goto ok; } - v_status = wuffs_vp8__decoder__decode_macroblocks(self, a_dst, a_workbuf); + v_status = wuffs_vp8__decoder__decode_macroblocks(self, a_workbuf); if ( ! wuffs_base__status__is_ok(&v_status)) { status = v_status; if (wuffs_base__status__is_error(&status)) { @@ -86060,10 +86096,14 @@ wuffs_vp8__decoder__workbuf_len( return wuffs_base__utility__empty_range_ii_u64(); } + uint64_t v_wh = 0; uint64_t v_wb_len = 0; + if (self->private_impl.f_quirk_has_alpha_side_channel) { + v_wh = ((uint64_t)((self->private_impl.f_width * self->private_impl.f_height))); + } v_wb_len = (self->private_impl.f_workbuf_yuv_v_end + ((uint64_t)(self->private_impl.f_partitioned_data_length)) + 8u); - return wuffs_base__utility__make_range_ii_u64(v_wb_len, v_wb_len); + return wuffs_base__utility__make_range_ii_u64((v_wh + v_wb_len), (v_wh + v_wb_len)); } #endif // !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__VP8) @@ -87060,6 +87100,44 @@ WUFFS_WEBP__DISTANCE_MAP[120] WUFFS_BASE__POTENTIALLY_UNUSED = { // ---------------- Private Function Prototypes +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_webp__decoder__decode_alph( + wuffs_webp__decoder* self, + wuffs_base__io_buffer* a_src, + wuffs_base__slice_u8 a_workbuf); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_webp__decoder__decode_alph_limited( + wuffs_webp__decoder* self, + wuffs_base__io_buffer* a_src, + wuffs_base__slice_u8 a_workbuf); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_webp__decoder__compact_alph( + wuffs_webp__decoder* self, + wuffs_base__slice_u8 a_s); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_webp__decoder__filter_alph_horizontal( + wuffs_webp__decoder* self, + wuffs_base__slice_u8 a_s); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_webp__decoder__filter_alph_vertical( + wuffs_webp__decoder* self, + wuffs_base__slice_u8 a_s); + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_webp__decoder__filter_alph_gradient( + wuffs_webp__decoder* self, + wuffs_base__slice_u8 a_s); + WUFFS_BASE__GENERATED_C_CODE static wuffs_base__status wuffs_webp__decoder__decode_huffman_groups( @@ -87263,6 +87341,13 @@ wuffs_webp__decoder__do_decode_frame( wuffs_base__slice_u8 a_workbuf, wuffs_base__decode_frame_options* a_opts); +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_webp__decoder__apply_alph_chunk( + wuffs_webp__decoder* self, + wuffs_base__pixel_buffer* a_dst, + wuffs_base__slice_u8 a_workbuf); + // ---------------- VTables const wuffs_base__image_decoder__func_ptrs @@ -87379,6 +87464,375 @@ sizeof__wuffs_webp__decoder(void) { // ---------------- Function Implementations +// -------- func webp.decoder.decode_alph + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_webp__decoder__decode_alph( + wuffs_webp__decoder* self, + wuffs_base__io_buffer* a_src, + wuffs_base__slice_u8 a_workbuf) { + wuffs_base__status status = wuffs_base__make_status(NULL); + + uint64_t v_r_mark = 0; + wuffs_base__status v_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 && a_src->data.ptr) { + 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_alph; + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + while (true) { + { + const bool o_0_closed_a_src = a_src->meta.closed; + const uint8_t* o_0_io2_a_src = io2_a_src; + wuffs_private_impl__io_reader__limit(&io2_a_src, iop_a_src, + ((uint64_t)(self->private_impl.f_sub_chunk_length))); + if (a_src) { + size_t n = ((size_t)(io2_a_src - a_src->data.ptr)); + a_src->meta.closed = a_src->meta.closed && (a_src->meta.wi <= n); + a_src->meta.wi = n; + } + v_r_mark = ((uint64_t)(iop_a_src - io0_a_src)); + { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + wuffs_base__status t_0 = wuffs_webp__decoder__decode_alph_limited(self, a_src, a_workbuf); + v_status = t_0; + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } + } + wuffs_private_impl__u32__sat_sub_indirect(&self->private_impl.f_sub_chunk_length, ((uint32_t)(wuffs_private_impl__io__count_since(v_r_mark, ((uint64_t)(iop_a_src - io0_a_src)))))); + io2_a_src = o_0_io2_a_src; + if (a_src) { + a_src->meta.closed = o_0_closed_a_src; + a_src->meta.wi = ((size_t)(io2_a_src - a_src->data.ptr)); + } + } + if (wuffs_base__status__is_ok(&v_status)) { + break; + } else if ( ! wuffs_base__status__is_suspension(&v_status)) { + status = v_status; + if (wuffs_base__status__is_error(&status)) { + goto exit; + } else if (wuffs_base__status__is_suspension(&status)) { + status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension); + goto exit; + } + goto ok; + } else if ((v_status.repr == wuffs_base__suspension__short_read) && (self->private_impl.f_sub_chunk_length == 0u)) { + status = wuffs_base__make_status(wuffs_webp__error__short_chunk); + goto exit; + } + status = v_status; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(1); + } + + ok: + self->private_impl.p_decode_alph = 0; + goto exit; + } + + goto suspend; + suspend: + self->private_impl.p_decode_alph = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; + + goto exit; + exit: + if (a_src && a_src->data.ptr) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + + return status; +} + +// -------- func webp.decoder.decode_alph_limited + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__status +wuffs_webp__decoder__decode_alph_limited( + wuffs_webp__decoder* self, + wuffs_base__io_buffer* a_src, + wuffs_base__slice_u8 a_workbuf) { + wuffs_base__status status = wuffs_base__make_status(NULL); + + uint64_t v_n = 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 && a_src->data.ptr) { + 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_alph_limited; + switch (coro_susp_point) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; + + { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(1); + 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_0 = *iop_a_src++; + self->private_impl.f_vp8x_alph_flags = t_0; + } + if (((uint8_t)(self->private_impl.f_vp8x_alph_flags & 3u)) == 0u) { + wuffs_private_impl__io_reader__limited_copy_u32_to_slice( + &iop_a_src, io2_a_src,(self->private_impl.f_width * self->private_impl.f_height), a_workbuf); + } else if (((uint8_t)(self->private_impl.f_vp8x_alph_flags & 3u)) == 1u) { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(2); + status = wuffs_webp__decoder__do_decode_frame_vp8l(self, + NULL, + a_src, + wuffs_base__utility__make_pixel_blend(0u), + a_workbuf, + NULL); + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } + if (status.repr) { + goto suspend; + } + v_n = ((uint64_t)((self->private_impl.f_width * self->private_impl.f_height * 4u))); + if (v_n <= ((uint64_t)(a_workbuf.len))) { + wuffs_webp__decoder__compact_alph(self, wuffs_base__slice_u8__subslice_j(a_workbuf, v_n)); + } + } else { + status = wuffs_base__make_status(wuffs_webp__error__bad_vp8x_chunk); + goto exit; + } + self->private_impl.f_vp8x_alph_flags |= 2u; + v_n = ((uint64_t)((self->private_impl.f_width * self->private_impl.f_height))); + if (v_n <= ((uint64_t)(a_workbuf.len))) { + if (((uint8_t)(self->private_impl.f_vp8x_alph_flags & 12u)) == 4u) { + wuffs_webp__decoder__filter_alph_horizontal(self, wuffs_base__slice_u8__subslice_j(a_workbuf, v_n)); + } else if (((uint8_t)(self->private_impl.f_vp8x_alph_flags & 12u)) == 8u) { + wuffs_webp__decoder__filter_alph_vertical(self, wuffs_base__slice_u8__subslice_j(a_workbuf, v_n)); + } else if (((uint8_t)(self->private_impl.f_vp8x_alph_flags & 12u)) == 12u) { + wuffs_webp__decoder__filter_alph_gradient(self, wuffs_base__slice_u8__subslice_j(a_workbuf, v_n)); + } + } + + goto ok; + ok: + self->private_impl.p_decode_alph_limited = 0; + goto exit; + } + + goto suspend; + suspend: + self->private_impl.p_decode_alph_limited = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; + + goto exit; + exit: + if (a_src && a_src->data.ptr) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + + return status; +} + +// -------- func webp.decoder.compact_alph + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_webp__decoder__compact_alph( + wuffs_webp__decoder* self, + wuffs_base__slice_u8 a_s) { + uint64_t v_i = 0; + uint64_t v_j = 0; + + if (((uint64_t)(a_s.len)) > 1073741824u) { + return wuffs_base__make_empty_struct(); + } + v_i = 0u; + v_j = 1u; + while ((v_i < ((uint64_t)(a_s.len))) && (v_j < ((uint64_t)(a_s.len)))) { + a_s.ptr[v_i] = a_s.ptr[v_j]; + v_i += 1u; + v_j += 4u; + } + return wuffs_base__make_empty_struct(); +} + +// -------- func webp.decoder.filter_alph_horizontal + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_webp__decoder__filter_alph_horizontal( + wuffs_webp__decoder* self, + wuffs_base__slice_u8 a_s) { + uint32_t v_y = 0; + uint32_t v_x = 0; + uint64_t v_i = 0; + uint8_t v_pred = 0; + + v_y = 0u; + while (v_y < self->private_impl.f_height) { + v_pred = 0u; + if (v_y > 0u) { + v_i = ((uint64_t)(((v_y - 1u) * self->private_impl.f_width))); + if (v_i >= ((uint64_t)(a_s.len))) { + break; + } + v_pred = a_s.ptr[v_i]; + } + v_x = 0u; + while (v_x < self->private_impl.f_width) { + v_i = ((uint64_t)(((v_y * self->private_impl.f_width) + v_x))); + if (v_i >= ((uint64_t)(a_s.len))) { + break; + } +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" +#endif + a_s.ptr[v_i] += v_pred; +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + v_pred = a_s.ptr[v_i]; + v_x += 1u; + } + v_y += 1u; + } + return wuffs_base__make_empty_struct(); +} + +// -------- func webp.decoder.filter_alph_vertical + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_webp__decoder__filter_alph_vertical( + wuffs_webp__decoder* self, + wuffs_base__slice_u8 a_s) { + uint32_t v_y = 0; + uint32_t v_x = 0; + uint64_t v_i = 0; + uint8_t v_pred = 0; + + v_x = 0u; + while (v_x < self->private_impl.f_width) { + v_pred = 0u; + if (v_x > 0u) { + v_i = ((uint64_t)((v_x - 1u))); + if (v_i >= ((uint64_t)(a_s.len))) { + break; + } + v_pred = a_s.ptr[v_i]; + } + v_y = 0u; + while (v_y < self->private_impl.f_height) { + v_i = ((uint64_t)(((v_y * self->private_impl.f_width) + v_x))); + if (v_i >= ((uint64_t)(a_s.len))) { + break; + } +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" +#endif + a_s.ptr[v_i] += v_pred; +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + v_pred = a_s.ptr[v_i]; + v_y += 1u; + } + v_x += 1u; + } + return wuffs_base__make_empty_struct(); +} + +// -------- func webp.decoder.filter_alph_gradient + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_webp__decoder__filter_alph_gradient( + wuffs_webp__decoder* self, + wuffs_base__slice_u8 a_s) { + uint32_t v_y = 0; + uint32_t v_x = 0; + uint64_t v_i = 0; + uint8_t v_pred = 0; + uint32_t v_grad = 0; + uint8_t v_l = 0; + uint8_t v_t = 0; + uint8_t v_tl = 0; + + v_x = 0u; + while (v_x < self->private_impl.f_width) { + v_i = ((uint64_t)(v_x)); + if (v_i >= ((uint64_t)(a_s.len))) { + break; + } +#if defined(__GNUC__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" +#endif + a_s.ptr[v_i] += v_pred; +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#endif + v_pred = a_s.ptr[v_i]; + v_x += 1u; + } + v_y = 1u; + while (v_y < self->private_impl.f_height) { + v_i = ((uint64_t)(((v_y - 1u) * self->private_impl.f_width))); + if (v_i >= ((uint64_t)(a_s.len))) { + break; + } + v_t = a_s.ptr[v_i]; + v_tl = v_t; + v_l = v_t; + v_x = 0u; + while (v_x < self->private_impl.f_width) { + v_i = ((uint64_t)((((v_y - 1u) * self->private_impl.f_width) + v_x))); + if (v_i >= ((uint64_t)(a_s.len))) { + break; + } + v_t = a_s.ptr[v_i]; + v_i = ((uint64_t)(((v_y * self->private_impl.f_width) + v_x))); + if (v_i >= ((uint64_t)(a_s.len))) { + break; + } + v_grad = ((uint32_t)(((uint32_t)(((uint32_t)(v_l)) + ((uint32_t)(v_t)))) - ((uint32_t)(v_tl)))); + if (v_grad > 2147483647u) { + v_grad = 0u; + } else if (v_grad > 255u) { + v_grad = 255u; + } + v_l = ((uint8_t)(a_s.ptr[v_i] + ((uint8_t)(v_grad)))); + a_s.ptr[v_i] = v_l; + v_tl = v_t; + v_x += 1u; + } + v_y += 1u; + } + return wuffs_base__make_empty_struct(); +} + // -------- func webp.decoder.decode_huffman_groups WUFFS_BASE__GENERATED_C_CODE @@ -89957,6 +90411,9 @@ wuffs_webp__decoder__swizzle( wuffs_base__slice_u8 v_dst = {0}; uint32_t v_y = 0; + if (a_dst == NULL) { + return wuffs_base__make_status(NULL); + } v_status = wuffs_base__pixel_swizzler__prepare(&self->private_impl.f_swizzler, wuffs_base__pixel_buffer__pixel_format(a_dst), wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_palette, 1024)), @@ -90400,10 +90857,6 @@ wuffs_webp__decoder__do_decode_image_config_limited( } self->private_impl.f_sub_chunk_length = t_2; } - if (self->private_impl.f_sub_chunk_length < 4u) { - status = wuffs_base__make_status(wuffs_webp__error__bad_header); - goto exit; - } self->private_impl.f_sub_chunk_has_padding = ((self->private_impl.f_sub_chunk_length & 1u) != 0u); if (self->private_impl.f_variant == 1u) { wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 3u, ((((uint64_t)(self->private_impl.f_sub_chunk_length)) << 1u) | 1u)); @@ -90742,7 +91195,7 @@ wuffs_webp__decoder__do_decode_image_config_limited_vp8x( self->private_impl.f_workbuf_offset_for_transform[1u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + v_transform_size); self->private_impl.f_workbuf_offset_for_transform[2u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + (2u * v_transform_size)); self->private_impl.f_workbuf_offset_for_transform[3u] = ((4u * self->private_impl.f_width * self->private_impl.f_height) + (3u * v_transform_size)); - v_status = wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 1836840960u, (((uint64_t)(self->private_impl.f_width)) | (((uint64_t)(self->private_impl.f_height)) << 32u))); + v_status = wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 1836840961u, (((uint64_t)(self->private_impl.f_width)) | (((uint64_t)(self->private_impl.f_height)) << 32u))); if ( ! wuffs_base__status__is_ok(&v_status)) { status = wuffs_base__make_status(wuffs_webp__error__unsupported_webp_file); goto exit; @@ -91014,8 +91467,8 @@ wuffs_webp__decoder__do_decode_frame( wuffs_base__decode_frame_options* a_opts) { wuffs_base__status status = wuffs_base__make_status(NULL); - uint32_t v_chunk_type = 0; - uint32_t v_chunk_length = 0; + uint64_t v_r_mark = 0; + wuffs_base__status v_status = wuffs_base__make_status(NULL); const uint8_t* iop_a_src = NULL; const uint8_t* io0_a_src WUFFS_BASE__POTENTIALLY_UNUSED = NULL; @@ -91029,9 +91482,6 @@ wuffs_webp__decoder__do_decode_frame( } uint32_t coro_susp_point = self->private_impl.p_do_decode_frame; - if (coro_susp_point) { - v_chunk_type = self->private_data.s_do_decode_frame.v_chunk_type; - } switch (coro_susp_point) { WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0; @@ -91080,7 +91530,7 @@ wuffs_webp__decoder__do_decode_frame( *scratch |= ((uint64_t)(num_bits_0)) << 56; } } - v_chunk_type = t_0; + self->private_impl.f_sub_chunk_type = t_0; } { WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4); @@ -91109,9 +91559,14 @@ wuffs_webp__decoder__do_decode_frame( *scratch |= ((uint64_t)(num_bits_1)) << 56; } } - v_chunk_length = t_1; + self->private_impl.f_sub_chunk_length = t_1; } - if (v_chunk_type == 1346585417u) { + self->private_impl.f_sub_chunk_has_padding = ((self->private_impl.f_sub_chunk_length & 1u) != 0u); + if (self->private_impl.f_sub_chunk_length > 2147483647u) { + status = wuffs_base__make_status(wuffs_webp__error__unsupported_webp_file); + goto exit; + } + if (self->private_impl.f_sub_chunk_type == 1346585417u) { if (self->private_impl.f_variant > 4u) { status = wuffs_base__make_status(wuffs_webp__error__bad_chunk_order); goto exit; @@ -91120,7 +91575,7 @@ wuffs_webp__decoder__do_decode_frame( goto exit; } self->private_impl.f_variant = 8u; - } else if (v_chunk_type == 1296649793u) { + } else if (self->private_impl.f_sub_chunk_type == 1296649793u) { if (self->private_impl.f_variant > 8u) { status = wuffs_base__make_status(wuffs_webp__error__bad_chunk_order); goto exit; @@ -91129,7 +91584,7 @@ wuffs_webp__decoder__do_decode_frame( goto exit; } self->private_impl.f_variant = 16u; - } else if (v_chunk_type == 1179471425u) { + } else if (self->private_impl.f_sub_chunk_type == 1179471425u) { if (self->private_impl.f_variant > 16u) { status = wuffs_base__make_status(wuffs_webp__error__bad_chunk_order); goto exit; @@ -91138,7 +91593,7 @@ wuffs_webp__decoder__do_decode_frame( goto exit; } self->private_impl.f_variant = 32u; - } else if (v_chunk_type == 1213221953u) { + } else if (self->private_impl.f_sub_chunk_type == 1213221953u) { if (self->private_impl.f_variant > 32u) { status = wuffs_base__make_status(wuffs_webp__error__bad_chunk_order); goto exit; @@ -91147,40 +91602,46 @@ wuffs_webp__decoder__do_decode_frame( goto exit; } self->private_impl.f_variant = 64u; - } else if (v_chunk_type == 540561494u) { - if (((uint64_t)(v_chunk_length)) > 8388608u) { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6); + status = wuffs_webp__decoder__decode_alph(self, a_src, a_workbuf); + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } + if (status.repr) { + goto suspend; + } + wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 1836840960u, 1u); + } else if (self->private_impl.f_sub_chunk_type == 540561494u) { + if (((uint64_t)(self->private_impl.f_sub_chunk_length)) > 8388608u) { status = wuffs_base__make_status(wuffs_webp__error__unsupported_vp8x_vp8_chunk_length); goto exit; } - wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 3u, ((((uint64_t)(v_chunk_length)) << 1u) | 1u)); + wuffs_vp8__decoder__set_quirk(&self->private_data.f_vp8, 3u, ((((uint64_t)(self->private_impl.f_sub_chunk_length)) << 1u) | 1u)); self->private_impl.f_variant = 129u; break; - } else if (v_chunk_type == 1278758998u) { + } else if (self->private_impl.f_sub_chunk_type == 1278758998u) { if (((uint8_t)(self->private_impl.f_vp8x_header_flags & 16u)) != 0u) { status = wuffs_base__make_status(wuffs_webp__error__bad_alph_vp8l_combination); goto exit; } self->private_impl.f_variant = 130u; break; - } else if (v_chunk_type == 542133592u) { + } else if (self->private_impl.f_sub_chunk_type == 542133592u) { if (((uint8_t)(self->private_impl.f_vp8x_header_flags & 4u)) == 0u) { status = wuffs_base__make_status(wuffs_webp__error__bad_vp8x_chunk); goto exit; } - } else if (v_chunk_type == 1179211845u) { + } else if (self->private_impl.f_sub_chunk_type == 1179211845u) { if (((uint8_t)(self->private_impl.f_vp8x_header_flags & 8u)) == 0u) { status = wuffs_base__make_status(wuffs_webp__error__bad_vp8x_chunk); goto exit; } } - if (v_chunk_length > 2147483647u) { - status = wuffs_base__make_status(wuffs_webp__error__unsupported_webp_file); - goto exit; - } else if ((v_chunk_length & 1u) != 0u) { - v_chunk_length += 1u; - } - self->private_data.s_do_decode_frame.scratch = v_chunk_length; - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(6); + self->private_data.s_do_decode_frame.scratch = self->private_impl.f_sub_chunk_length; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7); if (self->private_data.s_do_decode_frame.scratch > ((uint64_t)(io2_a_src - iop_a_src))) { self->private_data.s_do_decode_frame.scratch -= ((uint64_t)(io2_a_src - iop_a_src)); iop_a_src = io2_a_src; @@ -91188,41 +91649,87 @@ wuffs_webp__decoder__do_decode_frame( goto suspend; } iop_a_src += self->private_data.s_do_decode_frame.scratch; + if (self->private_impl.f_sub_chunk_has_padding) { + WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8); + if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) { + status = wuffs_base__make_status(wuffs_base__suspension__short_read); + goto suspend; + } + iop_a_src++; + } } - if (((uint8_t)(self->private_impl.f_variant & 1u)) != 0u) { - if (a_src) { - a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + while (true) { + { + const bool o_0_closed_a_src = a_src->meta.closed; + const uint8_t* o_0_io2_a_src = io2_a_src; + wuffs_private_impl__io_reader__limit(&io2_a_src, iop_a_src, + ((uint64_t)(self->private_impl.f_sub_chunk_length))); + if (a_src) { + size_t n = ((size_t)(io2_a_src - a_src->data.ptr)); + a_src->meta.closed = a_src->meta.closed && (a_src->meta.wi <= n); + a_src->meta.wi = n; + } + v_r_mark = ((uint64_t)(iop_a_src - io0_a_src)); + if (((uint8_t)(self->private_impl.f_variant & 1u)) != 0u) { + { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + wuffs_base__status t_2 = wuffs_vp8__decoder__decode_frame(&self->private_data.f_vp8, + a_dst, + a_src, + a_blend, + a_workbuf, + a_opts); + v_status = t_2; + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } + } + } else { + { + if (a_src) { + a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); + } + wuffs_base__status t_3 = wuffs_webp__decoder__do_decode_frame_vp8l(self, + a_dst, + a_src, + a_blend, + a_workbuf, + a_opts); + v_status = t_3; + if (a_src) { + iop_a_src = a_src->data.ptr + a_src->meta.ri; + } + } + } + wuffs_private_impl__u32__sat_sub_indirect(&self->private_impl.f_sub_chunk_length, ((uint32_t)(wuffs_private_impl__io__count_since(v_r_mark, ((uint64_t)(iop_a_src - io0_a_src)))))); + io2_a_src = o_0_io2_a_src; + if (a_src) { + a_src->meta.closed = o_0_closed_a_src; + a_src->meta.wi = ((size_t)(io2_a_src - a_src->data.ptr)); + } } - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(7); - status = wuffs_vp8__decoder__decode_frame(&self->private_data.f_vp8, - a_dst, - a_src, - a_blend, - a_workbuf, - a_opts); - if (a_src) { - iop_a_src = a_src->data.ptr + a_src->meta.ri; - } - if (status.repr) { - goto suspend; - } - } else { - if (a_src) { - a_src->meta.ri = ((size_t)(iop_a_src - a_src->data.ptr)); - } - WUFFS_BASE__COROUTINE_SUSPENSION_POINT(8); - status = wuffs_webp__decoder__do_decode_frame_vp8l(self, - a_dst, - a_src, - a_blend, - a_workbuf, - a_opts); - if (a_src) { - iop_a_src = a_src->data.ptr + a_src->meta.ri; - } - if (status.repr) { - goto suspend; + if (wuffs_base__status__is_ok(&v_status)) { + break; + } else if ( ! wuffs_base__status__is_suspension(&v_status)) { + status = v_status; + if (wuffs_base__status__is_error(&status)) { + goto exit; + } else if (wuffs_base__status__is_suspension(&status)) { + status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension); + goto exit; + } + goto ok; + } else if ((v_status.repr == wuffs_base__suspension__short_read) && (self->private_impl.f_sub_chunk_length == 0u)) { + status = wuffs_base__make_status(wuffs_webp__error__short_chunk); + goto exit; } + status = v_status; + WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(9); + } + if (self->private_impl.f_vp8x_alph_flags != 0u) { + wuffs_webp__decoder__apply_alph_chunk(self, a_dst, a_workbuf); } ok: @@ -91233,7 +91740,6 @@ wuffs_webp__decoder__do_decode_frame( goto suspend; suspend: self->private_impl.p_do_decode_frame = wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0; - self->private_data.s_do_decode_frame.v_chunk_type = v_chunk_type; goto exit; exit: @@ -91244,6 +91750,40 @@ wuffs_webp__decoder__do_decode_frame( return status; } +// -------- func webp.decoder.apply_alph_chunk + +WUFFS_BASE__GENERATED_C_CODE +static wuffs_base__empty_struct +wuffs_webp__decoder__apply_alph_chunk( + wuffs_webp__decoder* self, + wuffs_base__pixel_buffer* a_dst, + wuffs_base__slice_u8 a_workbuf) { + uint32_t v_y = 0; + uint32_t v_x = 0; + wuffs_base__table_u8 v_tab = {0}; + wuffs_base__slice_u8 v_row = {0}; + uint64_t v_d = 0; + uint64_t v_s = 0; + + v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0u); + v_y = 0u; + while (v_y < self->private_impl.f_height) { + v_row = wuffs_private_impl__table_u8__row_u32(v_tab, v_y); + v_x = 0u; + while (v_x < self->private_impl.f_width) { + v_d = ((uint64_t)(((v_x * 4u) + 3u))); + v_s = ((uint64_t)(((v_y * self->private_impl.f_width) + v_x))); + if ((v_d >= ((uint64_t)(v_row.len))) || (v_s >= ((uint64_t)(a_workbuf.len)))) { + break; + } + v_row.ptr[v_d] = a_workbuf.ptr[v_s]; + v_x += 1u; + } + v_y += 1u; + } + return wuffs_base__make_empty_struct(); +} + // -------- func webp.decoder.frame_dirty_rect WUFFS_BASE__GENERATED_C_CODE diff --git a/std/vp8/decode_macroblocks.wuffs b/std/vp8/decode_macroblocks.wuffs index 3b959b39..533debc7 100644 --- a/std/vp8/decode_macroblocks.wuffs +++ b/std/vp8/decode_macroblocks.wuffs @@ -8,10 +8,9 @@ // // SPDX-License-Identifier: Apache-2.0 OR MIT -pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) base.status { - var mby : base.u32 - var mbx : base.u32 - var status : base.status +pri func decoder.decode_macroblocks!(workbuf: slice base.u8) base.status { + var mby : base.u32 + var mbx : base.u32 mbx = 0 while mbx < this.mbw { @@ -55,7 +54,7 @@ pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice mby += 1 } - // Filter the final row, already in args.workbuf, and swizzle to args.dst. + // Filter the final row, already in args.workbuf. if this.mbh > 0 { if this.filt_level <= 0 { // No-op. @@ -65,10 +64,6 @@ pri func decoder.decode_macroblocks!(dst: ptr base.pixel_buffer, workbuf: slice this.filter_normal!(workbuf: args.workbuf, mby: this.mbh - 1) } - status = this.swizzle!(dst: args.dst, workbuf: args.workbuf) - if not status.is_ok() { - return status - } } return ok diff --git a/std/vp8/decode_quirks.wuffs b/std/vp8/decode_quirks.wuffs index ed13f79d..99761bcb 100644 --- a/std/vp8/decode_quirks.wuffs +++ b/std/vp8/decode_quirks.wuffs @@ -18,6 +18,11 @@ pri const QUIRKS_BASE : base.u32 = 0x6D7B_F800 // -------- +// When this quirk is enabled, the workbuf starts with (width × height) alpha +// samples, decoded out-of-band (from the VP8 decoder's perspective, they are +// typically decoded in-band from the surrounding WebP decoder's perspective). +pub const QUIRK_HAS_ALPHA_SIDE_CHANNEL : base.u32 = 0x6D7B_F800 | 0x00 + // Gives the image width and height out-of-band, in the u64 quirk value. The // low 32 bits are the width and the high 32 bits are the height. Both // dimensions should range within 0x0001 ..= 0x3FFF. @@ -35,4 +40,4 @@ pri const QUIRKS_BASE : base.u32 = 0x6D7B_F800 // // This quirk mechanism lets the outer webp decoder tell the inner vp8 decoder // those dimensions, after parsing the VP8X chunk but before ALPH or VP8. -pub const QUIRK_WIDTH_AND_HEIGHT : base.u32 = 0x6D7B_F800 | 0x00 +pub const QUIRK_WIDTH_AND_HEIGHT : base.u32 = 0x6D7B_F800 | 0x01 diff --git a/std/vp8/decode_vp8.wuffs b/std/vp8/decode_vp8.wuffs index cdc0d1d4..c5fde4eb 100644 --- a/std/vp8/decode_vp8.wuffs +++ b/std/vp8/decode_vp8.wuffs @@ -17,7 +17,7 @@ pub status "#truncated input" pub status "#unsupported QUIRK_SOURCE_LENGTH" pub status "#unsupported VP8 file" -pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0x20FF_FFFF +pub const DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE : base.u64 = 0x30FF_8000 pub struct decoder? implements base.image_decoder( width : base.u32[..= 0x3FFF], @@ -33,6 +33,8 @@ pub struct decoder? implements base.image_decoder( is_key_frame : base.bool, + quirk_has_alpha_side_channel : base.bool, + frame_config_io_position : base.u64, // This uses the same representation as an wuffs_base__optional_u63: @@ -46,6 +48,9 @@ pub struct decoder? implements base.image_decoder( // args.workbuf strides and offsets. // + // The foobar_end offsets are measured after any "prefix trimmed work + // buffer" adjustment (if QUIRK_HAS_ALPHA_SIDE_CHANNEL is enabled). + // // The workbuf holds the YUV buffers (after decoding from the source // bytes but before converting YUV to RGBX) and then also a copy of // most of the source bytes themselves - the "at least 2, up to 9" @@ -209,6 +214,10 @@ pub struct decoder? implements base.image_decoder( pub func decoder.get_quirk(key: base.u32) base.u64 { if args.key == base.QUIRK_SOURCE_LENGTH { return this.quirk_source_length + } else if args.key == QUIRK_HAS_ALPHA_SIDE_CHANNEL { + if this.quirk_has_alpha_side_channel { + return 1 + } } else if args.key == QUIRK_WIDTH_AND_HEIGHT { return this.quirk_width_and_height } @@ -236,6 +245,10 @@ pub func decoder.set_quirk!(key: base.u32, value: base.u64) base.status { } return ok + } else if args.key == QUIRK_HAS_ALPHA_SIDE_CHANNEL { + this.quirk_has_alpha_side_channel = args.value <> 0 + return ok + } else if args.key == QUIRK_WIDTH_AND_HEIGHT { w = (args.value & 0xFFFF_FFFF) as base.u32 h = (args.value & 0xFFFF_FFFF) as base.u32 @@ -381,23 +394,50 @@ pri func decoder.do_decode_frame_config?(dst: nptr base.frame_config, src: base. } 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 wh : base.u64[..= 0xFFF_8001] var wb_len : base.u64[..= DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE] var status : base.status + // The args.workbuf, with a "prefix trimmed work buffer" adjustment, if + // QUIRK_HAS_ALPHA_SIDE_CHANNEL is enabled, in which case the first (width + // × height) bytes of args.workbuf hold alpha samples that are e.g. part of + // WebP, not part of VP8 per se. + // + // The prefix isn't used when decoding VP8 to YCbCr, but the final swizzle + // to the destination pixel buffer should combine YCbCr and Alpha. + var trimmed : slice base.u8 + while true { - wb_len = this.workbuf_yuv_v_end + (this.partitioned_data_length as base.u64) + 8 - if wb_len > args.workbuf.length() { - return base."#bad workbuf length" - } else { - args.workbuf = args.workbuf[.. wb_len] + trimmed = args.workbuf + if this.quirk_has_alpha_side_channel { + wh = (this.width * this.height) as base.u64 + if wh > args.workbuf.length() { + return base."#bad workbuf length" + } + trimmed = args.workbuf[wh ..] } - status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts) - if (status == base."$short read") and args.src.is_closed() { + wb_len = this.workbuf_yuv_v_end + (this.partitioned_data_length as base.u64) + 8 + if wb_len > trimmed.length() { + return base."#bad workbuf length" + } else { + trimmed = trimmed[.. wb_len] + } + + status =? this.do_decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: trimmed, opts: args.opts) + if status.is_ok() { + break + } else if (status == base."$short read") and args.src.is_closed() { return "#truncated input" } yield? status } + + // TODO: trimmed should be args.workbuf. + status = this.swizzle!(dst: args.dst, workbuf: trimmed) + if not status.is_ok() { + return status + } } pri func decoder.do_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) { @@ -444,7 +484,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade return status } - status = this.decode_macroblocks!(dst: args.dst, workbuf: args.workbuf) + status = this.decode_macroblocks!(workbuf: args.workbuf) if not status.is_ok() { return status } @@ -490,7 +530,7 @@ pub func decoder.restart_frame!(index: base.u64, io_position: base.u64) base.sta } pub func decoder.set_report_metadata!(fourcc: base.u32, report: base.bool) { - // No-op. VP8 doesn't support metadata (but WEBP does). + // No-op. VP8 doesn't support metadata (but WebP does). } pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_information, src: base.io_reader) { @@ -498,10 +538,15 @@ pub func decoder.tell_me_more?(dst: base.io_writer, minfo: nptr base.more_inform } pub func decoder.workbuf_len() base.range_ii_u64 { - var wb_len : base.u64[..= DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE] + var wh : base.u64[..= 0xFFF_8001] + var wb_len : base.u64[..= DECODER_WORKBUF_LEN_MAX_INCL_WORST_CASE - 0xFFF_8001] + + if this.quirk_has_alpha_side_channel { + wh = (this.width * this.height) as base.u64 + } wb_len = this.workbuf_yuv_v_end + (this.partitioned_data_length as base.u64) + 8 return this.util.make_range_ii_u64( - min_incl: wb_len, - max_incl: wb_len) + min_incl: wh + wb_len, + max_incl: wh + wb_len) } diff --git a/std/webp/decode_alph_default.wuffs b/std/webp/decode_alph_default.wuffs new file mode 100644 index 00000000..2287c52b --- /dev/null +++ b/std/webp/decode_alph_default.wuffs @@ -0,0 +1,242 @@ +// Copyright 2026 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +// +// SPDX-License-Identifier: Apache-2.0 OR MIT + +pri func decoder.decode_alph?(src: base.io_reader, workbuf: slice base.u8) { + var r_mark : base.u64 + var status : base.status + + while true { + io_limit (io: args.src, limit: this.sub_chunk_length as base.u64) { + r_mark = args.src.mark() + status =? this.decode_alph_limited?(src: args.src, workbuf: args.workbuf) + this.sub_chunk_length ~sat-= + (args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32 + } + + if status.is_ok() { + break + } else if not status.is_suspension() { + return status + } else if (status == base."$short read") and (this.sub_chunk_length == 0) { + return "#short chunk" + } + yield? status + } +} + +pri func decoder.decode_alph_limited?(src: base.io_reader, workbuf: slice base.u8) { + var n : base.u64 + + this.vp8x_alph_flags = args.src.read_u8?() + + if (this.vp8x_alph_flags & 3) == 0 { + args.src.limited_copy_u32_to_slice!(up_to: this.width * this.height, s: args.workbuf) + } else if (this.vp8x_alph_flags & 3) == 1 { + this.do_decode_frame_vp8l?( + dst: nullptr, + src: args.src, + blend: this.util.make_pixel_blend(repr: base.PIXEL_BLEND__SRC), + workbuf: args.workbuf, + opts: nullptr) + + n = (this.width * this.height * 4) as base.u64 + if n <= args.workbuf.length() { + this.compact_alph!(s: args.workbuf[.. n]) + } + } else { + return "#bad VP8X chunk" + } + this.vp8x_alph_flags |= 2 + + n = (this.width * this.height) as base.u64 + if n <= args.workbuf.length() { + if (this.vp8x_alph_flags & 0x0C) == 0x04 { + this.filter_alph_horizontal!(s: args.workbuf[.. n]) + } else if (this.vp8x_alph_flags & 0x0C) == 0x08 { + this.filter_alph_vertical!(s: args.workbuf[.. n]) + } else if (this.vp8x_alph_flags & 0x0C) == 0x0C { + this.filter_alph_gradient!(s: args.workbuf[.. n]) + } + } +} + +pri func decoder.compact_alph!(s: slice base.u8) { + var i : base.u64[..= 0x4000_0004] + var j : base.u64[..= 0x4000_0004] + + if args.s.length() > 0x4000_0000 { + return nothing + } + + i = 0 + j = 1 + while (i < args.s.length()) and (j < args.s.length()), + inv args.s.length() <= 0x4000_0000, + { + assert i <= 0x4000_0000 via "a <= b: a <= c; c <= b"(c: args.s.length()) + assert j <= 0x4000_0000 via "a <= b: a <= c; c <= b"(c: args.s.length()) + args.s[i] = args.s[j] + i += 1 + j += 4 + } +} + +// libwebp calls this HorizontalUnfilter_C. +pri func decoder.filter_alph_horizontal!(s: slice base.u8) { + var y : base.u32 + var x : base.u32 + var i : base.u64 + var pred : base.u8 + + y = 0 + while y < this.height { + assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height) + + pred = 0 + if y > 0 { + i = ((y - 1) * this.width) as base.u64 + if i >= args.s.length() { + break + } + pred = args.s[i] + } + + x = 0 + while x < this.width, + inv y < 0x4000, + { + assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width) + + i = ((y * this.width) + x) as base.u64 + if i >= args.s.length() { + break + } + args.s[i] ~mod+= pred + pred = args.s[i] + + x += 1 + } + y += 1 + } +} + +// libwebp calls this VerticalUnfilter_C. +pri func decoder.filter_alph_vertical!(s: slice base.u8) { + var y : base.u32 + var x : base.u32 + var i : base.u64 + var pred : base.u8 + + x = 0 + while x < this.width { + assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width) + + pred = 0 + if x > 0 { + i = (x - 1) as base.u64 + if i >= args.s.length() { + break + } + pred = args.s[i] + } + + y = 0 + while y < this.height, + inv x < 0x4000, + { + assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height) + + i = ((y * this.width) + x) as base.u64 + if i >= args.s.length() { + break + } + args.s[i] ~mod+= pred + pred = args.s[i] + + y += 1 + } + x += 1 + } +} + +// libwebp calls this GradientUnfilter_C. +pri func decoder.filter_alph_gradient!(s: slice base.u8) { + var y : base.u32 + var x : base.u32 + var i : base.u64 + var pred : base.u8 + var grad : base.u32 + var l : base.u8 // Left sample. + var t : base.u8 // Top sample. + var tl : base.u8 // Top-Left sample. + + // First row. + x = 0 + while x < this.width { + assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width) + + i = x as base.u64 + if i >= args.s.length() { + break + } + args.s[i] ~mod+= pred + pred = args.s[i] + + x += 1 + } + + // Other rows. + y = 1 + while y < this.height, + inv y >= 1, + { + assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height) + + i = (((y - 1) * this.width) + 0) as base.u64 + if i >= args.s.length() { + break + } + t = args.s[i] + tl = t + l = t + + x = 0 + while x < this.width, + inv y >= 1, + inv y < 0x4000, + { + assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width) + + i = (((y - 1) * this.width) + x) as base.u64 + if i >= args.s.length() { + break + } + t = args.s[i] + + i = ((y * this.width) + x) as base.u64 + if i >= args.s.length() { + break + } + grad = ((l as base.u32) ~mod+ (t as base.u32)) ~mod- (tl as base.u32) + if grad > 0x7FFF_FFFF { + grad = 0x00 + } else if grad > 0xFF { + grad = 0xFF + } + l = args.s[i] ~mod+ ((grad & 0xFF) as base.u8) + args.s[i] = l + tl = t + + x += 1 + } + + y += 1 + } +} diff --git a/std/webp/decode_vp8l.wuffs b/std/webp/decode_vp8l.wuffs index c0b9f605..4a72e924 100644 --- a/std/webp/decode_vp8l.wuffs +++ b/std/webp/decode_vp8l.wuffs @@ -8,7 +8,7 @@ // // SPDX-License-Identifier: Apache-2.0 OR MIT -pri func decoder.do_decode_frame_vp8l?(dst: ptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) { +pri func decoder.do_decode_frame_vp8l?(dst: nptr base.pixel_buffer, src: base.io_reader, blend: base.pixel_blend, workbuf: slice base.u8, opts: nptr base.decode_frame_options) { var c8 : base.u8 var has_more : base.u32[..= 1] var width : base.u32[..= 0x4000] @@ -426,7 +426,7 @@ pri func decoder.decode_pixels?(dst: slice base.u8, src: base.io_reader, width: tile_size_log2: args.tile_size_log2) } -pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: roslice base.u8, blend: base.pixel_blend) base.status { +pri func decoder.swizzle!(dst: nptr base.pixel_buffer, src: roslice base.u8, blend: base.pixel_blend) base.status { var status : base.status var dst_pixfmt : base.pixel_format var dst_bits_per_pixel : base.u32[..= 256] @@ -438,6 +438,10 @@ pri func decoder.swizzle!(dst: ptr base.pixel_buffer, src: roslice base.u8, blen var dst : slice base.u8 var y : base.u32 + if args.dst == nullptr { + return ok + } + status = this.swizzler.prepare!( dst_pixfmt: args.dst.pixel_format(), dst_palette: args.dst.palette_or_else(fallback: this.palette[..]), diff --git a/std/webp/decode_webp.wuffs b/std/webp/decode_webp.wuffs index 6e5a38e7..77bbc63b 100644 --- a/std/webp/decode_webp.wuffs +++ b/std/webp/decode_webp.wuffs @@ -133,10 +133,18 @@ pub struct decoder? implements base.image_decoder( // ICCP_FLAG = 0x00000020, vp8x_header_flags : base.u8, + // The "Rsv| P | F | C" bits from + // https://developers.google.com/speed/webp/docs/riff_container#alpha + // + // We also re-purpose the otherwise-unused 0x02 bit to mean that we + // have seen the ALPH chunk. + vp8x_alph_flags : base.u8, + frame_config_io_position : base.u64, riff_chunk_length : base.u32, sub_chunk_length : base.u32, + sub_chunk_type : base.u32, bits : base.u32, n_bits : base.u32[..= 31], @@ -323,9 +331,6 @@ pri func decoder.do_decode_image_config_limited?(dst: nptr base.image_config, sr } this.sub_chunk_length = args.src.read_u32le?() - if this.sub_chunk_length < 4 { - return "#bad header" - } this.sub_chunk_has_padding = (this.sub_chunk_length & 1) <> 0 if this.variant == 0x01 { @@ -513,8 +518,8 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, } pri func decoder.do_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 chunk_type : base.u32 - var chunk_length : base.u32 + var r_mark : base.u64 + var status : base.status if this.call_sequence == 0x40 { // No-op. @@ -525,10 +530,14 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade } while (this.variant & 0x03) == 0 { - chunk_type = args.src.read_u32le?() - chunk_length = args.src.read_u32le?() + this.sub_chunk_type = args.src.read_u32le?() + this.sub_chunk_length = args.src.read_u32le?() + this.sub_chunk_has_padding = (this.sub_chunk_length & 1) <> 0 + if this.sub_chunk_length > 0x7FFF_FFFF { + return "#unsupported WebP file" + } - if chunk_type == 'ICCP'le { + if this.sub_chunk_type == 'ICCP'le { if this.variant > 0x04 { return "#bad chunk order" } else if (this.vp8x_header_flags & 0x20) == 0 { @@ -536,7 +545,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade } this.variant = 0x08 - } else if chunk_type == 'ANIM'le { + } else if this.sub_chunk_type == 'ANIM'le { if this.variant > 0x08 { return "#bad chunk order" } else if (this.vp8x_header_flags & 0x02) == 0 { @@ -544,7 +553,7 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade } this.variant = 0x10 - } else if chunk_type == 'ANMF'le { + } else if this.sub_chunk_type == 'ANMF'le { if this.variant > 0x10 { return "#bad chunk order" } else if (this.vp8x_header_flags & 0x02) == 0 { @@ -552,54 +561,111 @@ pri func decoder.do_decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reade } this.variant = 0x20 - } else if chunk_type == 'ALPH'le { + } else if this.sub_chunk_type == 'ALPH'le { if this.variant > 0x20 { return "#bad chunk order" } else if (this.vp8x_header_flags & 0x10) == 0 { return "#bad VP8X chunk" } this.variant = 0x40 + this.decode_alph?(src: args.src, workbuf: args.workbuf) + this.vp8.set_quirk!( + key: vp8.QUIRK_HAS_ALPHA_SIDE_CHANNEL, + value: 1) - } else if chunk_type == 'VP8 'le { - if (chunk_length as base.u64) > VP8X_MAX_INCL_VP8_CHUNK_LENGTH_DEFAULT { + } else if this.sub_chunk_type == 'VP8 'le { + if (this.sub_chunk_length as base.u64) > VP8X_MAX_INCL_VP8_CHUNK_LENGTH_DEFAULT { return "#unsupported VP8X/VP8 chunk length" } this.vp8.set_quirk!( key: base.QUIRK_SOURCE_LENGTH, - value: ((chunk_length as base.u64) << 1) | 1) + value: ((this.sub_chunk_length as base.u64) << 1) | 1) this.variant = 0x81 break - } else if chunk_type == 'VP8L'le { + } else if this.sub_chunk_type == 'VP8L'le { if (this.vp8x_header_flags & 0x10) <> 0 { return "#bad ALPH+VP8L combination" } this.variant = 0x82 break - } else if chunk_type == 'XMP 'le { + } else if this.sub_chunk_type == 'XMP 'le { if (this.vp8x_header_flags & 0x04) == 0 { return "#bad VP8X chunk" } - } else if chunk_type == 'EXIF'le { + } else if this.sub_chunk_type == 'EXIF'le { if (this.vp8x_header_flags & 0x08) == 0 { return "#bad VP8X chunk" } } - if chunk_length > 0x7FFF_FFFF { - return "#unsupported WebP file" - } else if (chunk_length & 1) <> 0 { - chunk_length += 1 + args.src.skip_u32?(n: this.sub_chunk_length) + if this.sub_chunk_has_padding { + args.src.skip_u32?(n: 1) } - args.src.skip_u32?(n: chunk_length) } - if (this.variant & 0x01) <> 0 { - this.vp8.decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts) - } else { - this.do_decode_frame_vp8l?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts) + while true { + io_limit (io: args.src, limit: this.sub_chunk_length as base.u64) { + r_mark = args.src.mark() + if (this.variant & 0x01) <> 0 { + status =? this.vp8.decode_frame?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts) + } else { + status =? this.do_decode_frame_vp8l?(dst: args.dst, src: args.src, blend: args.blend, workbuf: args.workbuf, opts: args.opts) + } + this.sub_chunk_length ~sat-= + (args.src.count_since(mark: r_mark) & 0xFFFF_FFFF) as base.u32 + } + + if status.is_ok() { + break + } else if not status.is_suspension() { + return status + } else if (status == base."$short read") and (this.sub_chunk_length == 0) { + return "#short chunk" + } + yield? status + } + + if this.vp8x_alph_flags <> 0 { + this.apply_alph_chunk!(dst: args.dst, workbuf: args.workbuf) + } +} + +// TODO: don't assume PIXEL_BLEND__SRC and PIXEL_FORMAT__BGRA_NONPREMUL. +pri func decoder.apply_alph_chunk!(dst: ptr base.pixel_buffer, workbuf: slice base.u8) { + var y : base.u32 + var x : base.u32 + var tab : table base.u8 + var row : slice base.u8 + var d : base.u64 + var s : base.u64 + + tab = args.dst.plane(p: 0) + y = 0 + while y < this.height { + assert y < 0x4000 via "a < b: a < c; c <= b"(c: this.height) + + row = tab.row_u32(y: y) + + x = 0 + while x < this.width, + inv y < 0x4000, + { + assert x < 0x4000 via "a < b: a < c; c <= b"(c: this.width) + + d = ((x * 4) + 3) as base.u64 + s = ((y * this.width) + x) as base.u64 + if (d >= row.length()) or (s >= args.workbuf.length()) { + break + } + row[d] = args.workbuf[s] + + x += 1 + } + y += 1 } } diff --git a/test/3pdata/nia-checksums-of-blinksuite.txt b/test/3pdata/nia-checksums-of-blinksuite.txt index a69a1c5e..f16c0f0f 100644 --- a/test/3pdata/nia-checksums-of-blinksuite.txt +++ b/test/3pdata/nia-checksums-of-blinksuite.txt @@ -44,7 +44,7 @@ OK. b3389858 test/3pdata/blinksuite/color-checker-srgb-color-profile.png OK. 10721531 test/3pdata/blinksuite/count-down-color-test.gif OK. 1abc4dc6 test/3pdata/blinksuite/count-down-color-test.png BAD d663b0e6 test/3pdata/blinksuite/count-down-color-test.webp -OK. 83ea9bce test/3pdata/blinksuite/crbug.364830.webp +OK. 18fbe8ca test/3pdata/blinksuite/crbug.364830.webp OK. 24ab1efd test/3pdata/blinksuite/crbug779261.gif OK. ae034abe test/3pdata/blinksuite/crbug807324.png OK. 4f50fdb0 test/3pdata/blinksuite/crbug827754.png @@ -180,9 +180,9 @@ OK. 1b1f5f96 test/3pdata/blinksuite/stripes-large.png OK. 6b85cdb9 test/3pdata/blinksuite/stripes-small.png OK. 16c2c0fe test/3pdata/blinksuite/test-load.jpg OK. 6fbda804 test/3pdata/blinksuite/test.webp -OK. b7690880 test/3pdata/blinksuite/test2.webp +OK. eb16e76c test/3pdata/blinksuite/test2.webp OK. 19d3a207 test/3pdata/blinksuite/test3.webp -OK. 17bda738 test/3pdata/blinksuite/truncated.webp +OK. 22cc757c test/3pdata/blinksuite/truncated.webp BAD 1dfa35c6 test/3pdata/blinksuite/truncated2.webp BAD b16eb1a2 test/3pdata/blinksuite/webp-animated-icc-xmp.webp BAD 45030792 test/3pdata/blinksuite/webp-animated-large.webp @@ -196,7 +196,7 @@ BAD ae7f1fcf test/3pdata/blinksuite/webp-animated.webp OK. eb191e45 test/3pdata/blinksuite/webp-color-no-profile-lossy.webp OK. 60116c2c test/3pdata/blinksuite/webp-color-profile-crash.webp BAD f234aa8a test/3pdata/blinksuite/webp-color-profile-lossless.webp -OK. b861d3c2 test/3pdata/blinksuite/webp-color-profile-lossy-alpha.webp +OK. db8c197f test/3pdata/blinksuite/webp-color-profile-lossy-alpha.webp OK. 684e3635 test/3pdata/blinksuite/webp-color-profile-lossy.webp BAD ed67a9a5 test/3pdata/blinksuite/wrong-block-length.gif OK. 577c5c4c test/3pdata/blinksuite/ycbcr-420-fast-int-progressive.jpg diff --git a/test/3pdata/nia-checksums-of-webpsuite.txt b/test/3pdata/nia-checksums-of-webpsuite.txt index 5257a891..2c7af0b9 100644 --- a/test/3pdata/nia-checksums-of-webpsuite.txt +++ b/test/3pdata/nia-checksums-of-webpsuite.txt @@ -1,19 +1,19 @@ # Generated by script/print-nia-checksums.sh -OK. 651f0caa test/3pdata/webpsuite/alpha_color_cache.webp -OK. 935f9fe1 test/3pdata/webpsuite/alpha_filter_0_method_0.webp -OK. 935f9fe1 test/3pdata/webpsuite/alpha_filter_0_method_1.webp -OK. 28779e98 test/3pdata/webpsuite/alpha_filter_1.webp -OK. 935f9fe1 test/3pdata/webpsuite/alpha_filter_1_method_0.webp -OK. 935f9fe1 test/3pdata/webpsuite/alpha_filter_1_method_1.webp -OK. 28779e98 test/3pdata/webpsuite/alpha_filter_2.webp -OK. 935f9fe1 test/3pdata/webpsuite/alpha_filter_2_method_0.webp -OK. 935f9fe1 test/3pdata/webpsuite/alpha_filter_2_method_1.webp -OK. 28779e98 test/3pdata/webpsuite/alpha_filter_3.webp -OK. 935f9fe1 test/3pdata/webpsuite/alpha_filter_3_method_0.webp -OK. 935f9fe1 test/3pdata/webpsuite/alpha_filter_3_method_1.webp -OK. 28779e98 test/3pdata/webpsuite/alpha_no_compression.webp +OK. a92322d0 test/3pdata/webpsuite/alpha_color_cache.webp +OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_0_method_0.webp +OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_0_method_1.webp +OK. d955c04f test/3pdata/webpsuite/alpha_filter_1.webp +OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_1_method_0.webp +OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_1_method_1.webp +OK. d955c04f test/3pdata/webpsuite/alpha_filter_2.webp +OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_2_method_0.webp +OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_2_method_1.webp +OK. d955c04f test/3pdata/webpsuite/alpha_filter_3.webp +OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_3_method_0.webp +OK. 1e7a17bc test/3pdata/webpsuite/alpha_filter_3_method_1.webp +OK. d955c04f test/3pdata/webpsuite/alpha_no_compression.webp OK. 0ecfd841 test/3pdata/webpsuite/bad_palette_index.webp -OK. 4cb35952 test/3pdata/webpsuite/big_endian_bug_393.webp +OK. 79f9ca4a test/3pdata/webpsuite/big_endian_bug_393.webp OK. 3dd065ab test/3pdata/webpsuite/bryce.webp OK. 7794c8b2 test/3pdata/webpsuite/bug3.webp OK. 6fbda804 test/3pdata/webpsuite/color_cache_bits_11.webp @@ -63,10 +63,10 @@ OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_6.webp OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_7.webp OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_8.webp OK. 6706e719 test/3pdata/webpsuite/lossless_vec_2_9.webp -OK. f553cdb9 test/3pdata/webpsuite/lossy_alpha1.webp -OK. 2707a535 test/3pdata/webpsuite/lossy_alpha2.webp -OK. be4ce0da test/3pdata/webpsuite/lossy_alpha3.webp -OK. 5dce2606 test/3pdata/webpsuite/lossy_alpha4.webp +OK. a62056d3 test/3pdata/webpsuite/lossy_alpha1.webp +OK. 7d50b493 test/3pdata/webpsuite/lossy_alpha2.webp +OK. 3a84e899 test/3pdata/webpsuite/lossy_alpha3.webp +OK. a9d10d15 test/3pdata/webpsuite/lossy_alpha4.webp OK. ac62b1e6 test/3pdata/webpsuite/lossy_extreme_probabilities.webp OK. 84ff1fbf test/3pdata/webpsuite/lossy_q0_f100.webp OK. 6957defa test/3pdata/webpsuite/near_lossless_75.webp diff --git a/test/c/std/webp.c b/test/c/std/webp.c index 6889c238..18fe18d0 100644 --- a/test/c/std/webp.c +++ b/test/c/std/webp.c @@ -125,7 +125,7 @@ test_wuffs_webp_decode_interface_vp8x_alpha_lossy() { return do_test__wuffs_base__image_decoder( wuffs_webp__decoder__upcast_as__wuffs_base__image_decoder(dec), "test/data/hippopotamus.masked-with-muybridge.lossy.webp", 0, SIZE_MAX, - 36, 28, 0xFFF5F5F5); // TODO: apply the alpha channel. + 36, 28, 0x7DF5F5F5); // TODO: should be 0x7D787878, because PREMUL. } const char* // @@ -221,6 +221,188 @@ test_wuffs_webp_decode_many_small_reads() { return NULL; } +const char* // +test_wuffs_webp_decode_filter_alph() { + CHECK_FOCUS(__func__); + + const int width = 41; + const int height = 6; + + // src is arbitrary bytes, a subset of a noise texture. + const uint8_t src[1][246] = { + { + 0x3C, 0xD4, 0xB6, 0x77, 0x64, 0xAB, 0xEF, 0x03, 0x7D, 0x2F, 0xC0, + 0x24, 0x79, 0x3E, 0x87, 0x9A, 0x36, 0x7F, 0xBC, 0xF0, 0x42, 0xAF, + 0x2D, 0x63, 0x22, 0xC3, 0x4E, 0x33, 0xE9, 0x67, 0x9C, 0x01, 0xA9, + 0x23, 0x50, 0xDD, 0x36, 0x26, 0x4B, 0x94, 0xDC, + + 0x14, 0xFB, 0x47, 0x5F, 0xF2, 0x13, 0x53, 0xE7, 0xD7, 0x28, 0x8A, + 0x07, 0xC6, 0x54, 0xE5, 0xB5, 0x0E, 0x78, 0xAE, 0x14, 0x84, 0xC0, + 0x20, 0x7D, 0x95, 0xEA, 0x06, 0x8A, 0xFA, 0xC3, 0x70, 0x5C, 0xA6, + 0x88, 0xCC, 0x09, 0xBA, 0xA6, 0x68, 0x1E, 0xB3, + + 0x5A, 0xA5, 0x68, 0xFD, 0x18, 0x90, 0x70, 0x3A, 0xF0, 0x5F, 0xCA, + 0x44, 0x2B, 0x58, 0x12, 0xBD, 0x43, 0x61, 0xB0, 0x9D, 0x0E, 0x3F, + 0xEC, 0xB6, 0x36, 0x6E, 0x94, 0xDE, 0x2F, 0xCE, 0x77, 0x92, 0x11, + 0xE2, 0x37, 0x81, 0xA0, 0x46, 0xCD, 0xDF, 0xA3, + + 0x05, 0x97, 0xDC, 0x73, 0xE4, 0x35, 0x6D, 0xF2, 0xC7, 0x1D, 0x56, + 0x7B, 0xCE, 0x2A, 0x17, 0x54, 0xE9, 0x21, 0x7E, 0x4E, 0xEB, 0x3B, + 0xF9, 0x47, 0xCA, 0x75, 0xB9, 0xD9, 0x0B, 0x5A, 0x27, 0x8A, 0x4D, + 0xF5, 0x1F, 0xB7, 0x8E, 0xD0, 0xA4, 0x28, 0x81, + + 0xD9, 0x32, 0xE4, 0xA3, 0x8F, 0x65, 0xD8, 0x9B, 0x42, 0xB2, 0x04, + 0x8B, 0x19, 0xBE, 0x9B, 0x00, 0x51, 0x22, 0x31, 0xF6, 0x7C, 0xBF, + 0x1A, 0x6C, 0xB0, 0x3C, 0x63, 0x0A, 0xF9, 0x53, 0x15, 0x93, 0x67, + 0xB8, 0x4B, 0x01, 0xF4, 0x82, 0xBE, 0x11, 0xFD, + + 0x61, 0xC4, 0xA4, 0x57, 0x6D, 0x2C, 0xDD, 0xED, 0xB1, 0x92, 0x63, + 0xA8, 0xE2, 0x35, 0xC8, 0x7F, 0xD5, 0x9D, 0x49, 0x85, 0xAE, 0xE2, + 0x41, 0x0C, 0xFE, 0x76, 0xC5, 0x20, 0x45, 0xAC, 0x30, 0xCB, 0x73, + 0x24, 0xEF, 0xDA, 0x80, 0xAB, 0x5E, 0x89, 0x6E, + }, + }; + + const uint8_t wants[3][246] = { + { + 0x3C, 0x10, 0xC6, 0x3D, 0xA1, 0x4C, 0x3B, 0x3E, 0xBB, 0xEA, 0xAA, + 0xCE, 0x47, 0x85, 0x0C, 0xA6, 0xDC, 0x5B, 0x17, 0x07, 0x49, 0xF8, + 0x25, 0x88, 0xAA, 0x6D, 0xBB, 0xEE, 0xD7, 0x3E, 0xDA, 0xDB, 0x84, + 0xA7, 0xF7, 0xD4, 0x0A, 0x30, 0x7B, 0x0F, 0xEB, + + 0x50, 0x4B, 0x92, 0xF1, 0xE3, 0xF6, 0x49, 0x30, 0x07, 0x2F, 0xB9, + 0xC0, 0x86, 0xDA, 0xBF, 0x74, 0x82, 0xFA, 0xA8, 0xBC, 0x40, 0x00, + 0x20, 0x9D, 0x32, 0x1C, 0x22, 0xAC, 0xA6, 0x69, 0xD9, 0x35, 0xDB, + 0x63, 0x2F, 0x38, 0xF2, 0x98, 0x00, 0x1E, 0xD1, + + 0xAA, 0x4F, 0xB7, 0xB4, 0xCC, 0x5C, 0xCC, 0x06, 0xF6, 0x55, 0x1F, + 0x63, 0x8E, 0xE6, 0xF8, 0xB5, 0xF8, 0x59, 0x09, 0xA6, 0xB4, 0xF3, + 0xDF, 0x95, 0xCB, 0x39, 0xCD, 0xAB, 0xDA, 0xA8, 0x1F, 0xB1, 0xC2, + 0xA4, 0xDB, 0x5C, 0xFC, 0x42, 0x0F, 0xEE, 0x91, + + 0xAF, 0x46, 0x22, 0x95, 0x79, 0xAE, 0x1B, 0x0D, 0xD4, 0xF1, 0x47, + 0xC2, 0x90, 0xBA, 0xD1, 0x25, 0x0E, 0x2F, 0xAD, 0xFB, 0xE6, 0x21, + 0x1A, 0x61, 0x2B, 0xA0, 0x59, 0x32, 0x3D, 0x97, 0xBE, 0x48, 0x95, + 0x8A, 0xA9, 0x60, 0xEE, 0xBE, 0x62, 0x8A, 0x0B, + + 0x88, 0xBA, 0x9E, 0x41, 0xD0, 0x35, 0x0D, 0xA8, 0xEA, 0x9C, 0xA0, + 0x2B, 0x44, 0x02, 0x9D, 0x9D, 0xEE, 0x10, 0x41, 0x37, 0xB3, 0x72, + 0x8C, 0xF8, 0xA8, 0xE4, 0x47, 0x51, 0x4A, 0x9D, 0xB2, 0x45, 0xAC, + 0x64, 0xAF, 0xB0, 0xA4, 0x26, 0xE4, 0xF5, 0xF2, + + 0xE9, 0xAD, 0x51, 0xA8, 0x15, 0x41, 0x1E, 0x0B, 0xBC, 0x4E, 0xB1, + 0x59, 0x3B, 0x70, 0x38, 0xB7, 0x8C, 0x29, 0x72, 0xF7, 0xA5, 0x87, + 0xC8, 0xD4, 0xD2, 0x48, 0x0D, 0x2D, 0x72, 0x1E, 0x4E, 0x19, 0x8C, + 0xB0, 0x9F, 0x79, 0xF9, 0xA4, 0x02, 0x8B, 0xF9, + }, + + { + 0x3C, 0x10, 0xC6, 0x3D, 0xA1, 0x4C, 0x3B, 0x3E, 0xBB, 0xEA, 0xAA, + 0xCE, 0x47, 0x85, 0x0C, 0xA6, 0xDC, 0x5B, 0x17, 0x07, 0x49, 0xF8, + 0x25, 0x88, 0xAA, 0x6D, 0xBB, 0xEE, 0xD7, 0x3E, 0xDA, 0xDB, 0x84, + 0xA7, 0xF7, 0xD4, 0x0A, 0x30, 0x7B, 0x0F, 0xEB, + + 0x50, 0x0B, 0x0D, 0x9C, 0x93, 0x5F, 0x8E, 0x25, 0x92, 0x12, 0x34, + 0xD5, 0x0D, 0xD9, 0xF1, 0x5B, 0xEA, 0xD3, 0xC5, 0x1B, 0xCD, 0xB8, + 0x45, 0x05, 0x3F, 0x57, 0xC1, 0x78, 0xD1, 0x01, 0x4A, 0x37, 0x2A, + 0x2F, 0xC3, 0xDD, 0xC4, 0xD6, 0xE3, 0x2D, 0x9E, + + 0xAA, 0xB0, 0x75, 0x99, 0xAB, 0xEF, 0xFE, 0x5F, 0x82, 0x71, 0xFE, + 0x19, 0x38, 0x31, 0x03, 0x18, 0x2D, 0x34, 0x75, 0xB8, 0xDB, 0xF7, + 0x31, 0xBB, 0x75, 0xC5, 0x55, 0x56, 0x00, 0xCF, 0xC1, 0xC9, 0x3B, + 0x11, 0xFA, 0x5E, 0x64, 0x1C, 0xB0, 0x0C, 0x41, + + 0xAF, 0x47, 0x51, 0x0C, 0x8F, 0x24, 0x6B, 0x51, 0x49, 0x8E, 0x54, + 0x94, 0x06, 0x5B, 0x1A, 0x6C, 0x16, 0x55, 0xF3, 0x06, 0xC6, 0x32, + 0x2A, 0x02, 0x3F, 0x3A, 0x0E, 0x2F, 0x0B, 0x29, 0xE8, 0x53, 0x88, + 0x06, 0x19, 0x15, 0xF2, 0xEC, 0x54, 0x34, 0xC2, + + 0x88, 0x79, 0x35, 0xAF, 0x1E, 0x89, 0x43, 0xEC, 0x8B, 0x40, 0x58, + 0x1F, 0x1F, 0x19, 0xB5, 0x6C, 0x67, 0x77, 0x24, 0xFC, 0x42, 0xF1, + 0x44, 0x6E, 0xEF, 0x76, 0x71, 0x39, 0x04, 0x7C, 0xFD, 0xE6, 0xEF, + 0xBE, 0x64, 0x16, 0xE6, 0x6E, 0x12, 0x45, 0xBF, + + 0xE9, 0x3D, 0xD9, 0x06, 0x8B, 0xB5, 0x20, 0xD9, 0x3C, 0xD2, 0xBB, + 0xC7, 0x01, 0x4E, 0x7D, 0xEB, 0x3C, 0x14, 0x6D, 0x81, 0xF0, 0xD3, + 0x85, 0x7A, 0xED, 0xEC, 0x36, 0x59, 0x49, 0x28, 0x2D, 0xB1, 0x62, + 0xE2, 0x53, 0xF0, 0x66, 0x19, 0x70, 0xCE, 0x2D, + }, + + { + 0x3C, 0x10, 0xC6, 0x3D, 0xA1, 0x4C, 0x3B, 0x3E, 0xBB, 0xEA, 0xAA, + 0xCE, 0x47, 0x85, 0x0C, 0xA6, 0xDC, 0x5B, 0x17, 0x07, 0x49, 0xF8, + 0x25, 0x88, 0xAA, 0x6D, 0xBB, 0xEE, 0xD7, 0x3E, 0xDA, 0xDB, 0x84, + 0xA7, 0xF7, 0xD4, 0x0A, 0x30, 0x7B, 0x0F, 0xEB, + + 0x50, 0x1F, 0x1C, 0x5F, 0xB5, 0x73, 0xB5, 0x9F, 0xD6, 0x27, 0x8A, + 0xB5, 0xF4, 0x53, 0xE5, 0xB4, 0xF8, 0xEF, 0x59, 0x5D, 0x23, 0x92, + 0x20, 0x00, 0xB7, 0x64, 0xB8, 0x75, 0x58, 0xC3, 0x6F, 0xCC, 0x1B, + 0xC6, 0xCB, 0xB1, 0xBA, 0x86, 0x39, 0x1E, 0xAD, + + 0xAA, 0x1E, 0x83, 0xC3, 0x17, 0x90, 0x42, 0x66, 0x8D, 0x5F, 0x8C, + 0xFB, 0x2A, 0x58, 0xFC, 0x88, 0x0F, 0x67, 0xB0, 0x51, 0x25, 0xD3, + 0x4D, 0xE3, 0x35, 0x6E, 0x56, 0xF1, 0x03, 0x3C, 0x77, 0x66, 0x11, + 0x9E, 0xDA, 0x41, 0xEA, 0xFC, 0x7C, 0x40, 0x72, + + 0xAF, 0xBA, 0xDB, 0x72, 0xE4, 0x34, 0x6D, 0x83, 0x71, 0x60, 0xE3, + 0x7A, 0xCE, 0x26, 0xE1, 0xC1, 0x31, 0xAA, 0x71, 0x60, 0x1F, 0x08, + 0xF9, 0x46, 0xCA, 0x74, 0x15, 0x89, 0x0B, 0x9E, 0x00, 0x8A, 0x82, + 0xF4, 0x1E, 0xB7, 0x8D, 0x6F, 0xA4, 0x90, 0x43, + + 0x88, 0xC5, 0xCA, 0x04, 0x05, 0x65, 0x76, 0x27, 0x57, 0xF8, 0x03, + 0x8B, 0xF8, 0x0E, 0x64, 0x44, 0x51, 0xEC, 0xE4, 0xC9, 0x04, 0xBF, + 0x19, 0x6C, 0xA0, 0x86, 0x8A, 0x08, 0xF9, 0x52, 0x15, 0x32, 0x91, + 0xB7, 0x4B, 0xE5, 0xAF, 0x13, 0x06, 0x11, 0xFD, + + 0xE9, 0xC3, 0x6C, 0x57, 0xC5, 0x2B, 0x19, 0xED, 0xB0, 0x91, 0x63, + 0x93, 0xE1, 0x35, 0x53, 0xB2, 0x94, 0x9C, 0xDD, 0x47, 0xAE, 0xE1, + 0x7C, 0xDB, 0xFD, 0x59, 0x22, 0x20, 0x44, 0xAC, 0x9F, 0x87, 0x59, + 0xA3, 0x26, 0x9A, 0xE4, 0xF3, 0x44, 0xD8, 0x6D, + }, + }; + + wuffs_webp__decoder dec = {0}; + dec.private_impl.f_width = width; + dec.private_impl.f_height = height; + + for (int filter = 0; filter < 3; filter++) { + uint8_t buf[246]; + memcpy(&buf[0], &src[0][0], 246); + wuffs_base__slice_u8 s = wuffs_base__make_slice_u8(&buf[0], 246); + + switch (filter) { + case 0: + wuffs_webp__decoder__filter_alph_horizontal(&dec, s); + break; + case 1: + wuffs_webp__decoder__filter_alph_vertical(&dec, s); + break; + case 2: + wuffs_webp__decoder__filter_alph_gradient(&dec, s); + break; + } + + if (0 == memcmp(&buf[0], &wants[filter][0], 246)) { + continue; + } + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + int i = (y * width) + x; + int have = buf[i]; + int want = wants[filter][i]; + if (have != want) { + RETURN_FAIL("filter=%d, y=%d, x=%d: have 0x%02X, want 0x%02X", filter, + y, x, have, want); + } + } + } + + return "unreachable"; + } + return NULL; +} + // ---------------- Mimic Tests #ifdef WUFFS_MIMIC @@ -532,6 +714,7 @@ bench_mimic_webp_lossy_decode_image_4002k_24bpp() { proc g_tests[] = { + test_wuffs_webp_decode_filter_alph, test_wuffs_webp_decode_interface_lossless, test_wuffs_webp_decode_interface_lossy, test_wuffs_webp_decode_interface_vp8x_alpha_lossy, diff --git a/test/nia-checksums-of-data.txt b/test/nia-checksums-of-data.txt index 8da570ef..5caeadc5 100644 --- a/test/nia-checksums-of-data.txt +++ b/test/nia-checksums-of-data.txt @@ -134,7 +134,7 @@ OK. 56cf6ae1 test/data/hippopotamus.lossy.webp OK. 2535637e test/data/hippopotamus.masked-with-muybridge.etc2.bgra-binary.pkm OK. 720180ee test/data/hippopotamus.masked-with-muybridge.etc2.bgra-nonpremul.pkm OK. d3bbed27 test/data/hippopotamus.masked-with-muybridge.gif -OK. c41410be test/data/hippopotamus.masked-with-muybridge.lossy.webp +OK. 1d8cd8b2 test/data/hippopotamus.masked-with-muybridge.lossy.webp OK. 7e6acf01 test/data/hippopotamus.masked-with-muybridge.png OK. dcbb225a test/data/hippopotamus.nie OK. 57d9a4dd test/data/hippopotamus.pgm