From 45ab79b451deda28a0898c51987bb03b37c1076f Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 8 Dec 2018 16:08:02 +1100 Subject: [PATCH] Allow GIF decoding to non-indexed pixel formats --- cmd/wuffs-c/internal/cgen/base/core-private.h | 12 +- cmd/wuffs-c/internal/cgen/base/image-impl.c | 44 ++++- cmd/wuffs-c/internal/cgen/base/image-public.h | 71 ++++++-- cmd/wuffs-c/internal/cgen/data.go | 24 +-- example/gifplayer/gifplayer.c | 21 +-- release/c/wuffs-unsupported-snapshot.h | 154 ++++++++++++++---- std/gif/decode_gif.wuffs | 20 ++- test/c/std/gif.c | 86 +++++++--- test/c/testlib/testlib.c | 38 ++++- 9 files changed, 355 insertions(+), 115 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/base/core-private.h b/cmd/wuffs-c/internal/cgen/base/core-private.h index 585984bb..3b5e0683 100644 --- a/cmd/wuffs-c/internal/cgen/base/core-private.h +++ b/cmd/wuffs-c/internal/cgen/base/core-private.h @@ -397,19 +397,19 @@ wuffs_base__slice_u8__suffix(wuffs_base__slice_u8 s, uint64_t up_to) { return s; } -// wuffs_base__slice_u8__copy_from_slice calls memmove(dst.ptr, src.ptr, -// length) where length is the minimum of dst.len and src.len. +// wuffs_base__slice_u8__copy_from_slice calls memmove(dst.ptr, src.ptr, len) +// where len is the minimum of dst.len and src.len. // // Passing a wuffs_base__slice_u8 with all fields NULL or zero (a valid, empty // slice) is valid and results in a no-op. static inline uint64_t // wuffs_base__slice_u8__copy_from_slice(wuffs_base__slice_u8 dst, wuffs_base__slice_u8 src) { - size_t length = dst.len < src.len ? dst.len : src.len; - if (length > 0) { - memmove(dst.ptr, src.ptr, length); + size_t len = dst.len < src.len ? dst.len : src.len; + if (len > 0) { + memmove(dst.ptr, src.ptr, len); } - return length; + return len; } // -------- diff --git a/cmd/wuffs-c/internal/cgen/base/image-impl.c b/cmd/wuffs-c/internal/cgen/base/image-impl.c index aa72e66d..53bf4cb8 100644 --- a/cmd/wuffs-c/internal/cgen/base/image-impl.c +++ b/cmd/wuffs-c/internal/cgen/base/image-impl.c @@ -24,12 +24,42 @@ wuffs_base__pixel_swizzler__copy_1_1(wuffs_base__slice_u8 dst, } static uint64_t // -wuffs_base__pixel_swizzler__swap_rgbx_bgrx(wuffs_base__slice_u8 dst, - wuffs_base__slice_u8 src) { - size_t length4 = (dst.len < src.len ? dst.len : src.len) / 4; +wuffs_base__pixel_swizzler__copy_4_1(wuffs_base__slice_u8 dst, + wuffs_base__slice_u8 dst_palette, + wuffs_base__slice_u8 src) { + if (dst_palette.len != 1024) { + return 0; + } + size_t dst_len4 = dst.len / 4; + size_t len = dst_len4 < src.len ? dst_len4 : src.len; uint8_t* d = dst.ptr; uint8_t* s = src.ptr; - size_t n = length4; + + size_t n = len; + while (n--) { + uint8_t* p = dst_palette.ptr + ((size_t)(*s) * 4); + uint8_t b0 = p[0]; + uint8_t b1 = p[1]; + uint8_t b2 = p[2]; + uint8_t b3 = p[3]; + d[0] = b0; + d[1] = b1; + d[2] = b2; + d[3] = b3; + s += 1; + d += 4; + } + return len; +} + +static uint64_t // +wuffs_base__pixel_swizzler__swap_rgbx_bgrx(wuffs_base__slice_u8 dst, + wuffs_base__slice_u8 src) { + size_t len4 = (dst.len < src.len ? dst.len : src.len) / 4; + uint8_t* d = dst.ptr; + uint8_t* s = src.ptr; + + size_t n = len4; while (n--) { uint8_t b0 = s[0]; uint8_t b1 = s[1]; @@ -42,7 +72,7 @@ wuffs_base__pixel_swizzler__swap_rgbx_bgrx(wuffs_base__slice_u8 dst, s += 4; d += 4; } - return length4 * 4; + return len4 * 4; } void // @@ -75,14 +105,14 @@ wuffs_base__pixel_swizzler__initialize(wuffs_base__pixel_swizzler* p, 1024) { break; } - // TODO: implement. + func = wuffs_base__pixel_swizzler__copy_4_1; break; case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: if (wuffs_base__pixel_swizzler__swap_rgbx_bgrx(dst_palette, src_palette) != 1024) { break; } - // TODO: implement. + func = wuffs_base__pixel_swizzler__copy_4_1; break; default: break; diff --git a/cmd/wuffs-c/internal/cgen/base/image-public.h b/cmd/wuffs-c/internal/cgen/base/image-public.h index 00bfbd37..e45c52ea 100644 --- a/cmd/wuffs-c/internal/cgen/base/image-public.h +++ b/cmd/wuffs-c/internal/cgen/base/image-public.h @@ -380,17 +380,40 @@ wuffs_base__pixel_config__height(wuffs_base__pixel_config* c) { // example, decoding a JPEG image straight to RGBA instead of to YCbCr? static inline uint64_t // wuffs_base__pixel_config__pixbuf_len(wuffs_base__pixel_config* c) { - if (c) { - uint64_t n = - ((uint64_t)c->private_impl.width) * ((uint64_t)c->private_impl.height); - // TODO: handle things other than 1 byte per pixel. When doing so, consider - // that the +1024 below could overflow. - if (wuffs_base__pixel_format__is_indexed(c->private_impl.pixfmt)) { - n += 1024; - } - return n; + // TODO: support more pixel formats. + uint64_t bytes_per_pixel = 0; + switch (wuffs_base__pixel_config__pixel_format(c)) { + case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL: + bytes_per_pixel = 1; + break; + case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL: + case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: + bytes_per_pixel = 4; + break; + default: + return 0; } - return 0; + + if (!c) { + return 0; + } + + uint64_t n = + ((uint64_t)c->private_impl.width) * ((uint64_t)c->private_impl.height); + + if (n > (UINT64_MAX / bytes_per_pixel)) { + return 0; + } + n *= bytes_per_pixel; + + if (wuffs_base__pixel_format__is_indexed(c->private_impl.pixfmt)) { + if (n > (UINT64_MAX - 1024)) { + return 0; + } + n += 1024; + } + + return n; } #ifdef __cplusplus @@ -824,18 +847,40 @@ wuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* b, len -= 1024; } - // TODO: don't assume 1 byte per pixel. Don't assume packed. + // TODO: support more pixel formats. + uint64_t bytes_per_pixel = 0; + switch (wuffs_base__pixel_config__pixel_format(pixcfg)) { + case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL: + bytes_per_pixel = 1; + break; + case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL: + case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: + bytes_per_pixel = 4; + break; + default: + return wuffs_base__error__bad_argument; + } + + // TODO: don't assume packed. uint64_t wh = ((uint64_t)pixcfg->private_impl.width) * ((uint64_t)pixcfg->private_impl.height); + size_t width = (size_t)(pixcfg->private_impl.width); + if ((wh > (UINT64_MAX / bytes_per_pixel)) || + (width > (SIZE_MAX / bytes_per_pixel))) { + return wuffs_base__error__bad_argument; + } + wh *= bytes_per_pixel; + width *= bytes_per_pixel; if (wh > len) { return wuffs_base__error__bad_argument_length_too_short; } + b->pixcfg = *pixcfg; wuffs_base__table_u8* tab = &b->private_impl.planes[0]; tab->ptr = ptr; - tab->width = pixcfg->private_impl.width; + tab->width = width; tab->height = pixcfg->private_impl.height; - tab->stride = pixcfg->private_impl.width; + tab->stride = width; return NULL; } diff --git a/cmd/wuffs-c/internal/cgen/data.go b/cmd/wuffs-c/internal/cgen/data.go index 779ef4d0..8b2a11b3 100644 --- a/cmd/wuffs-c/internal/cgen/data.go +++ b/cmd/wuffs-c/internal/cgen/data.go @@ -24,9 +24,10 @@ const baseAllImplC = "" + "" const baseImageImplC = "" + - "// ---------------- Images\n\nstatic uint64_t //\nwuffs_base__pixel_swizzler__copy_1_1(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 dst_palette,\n wuffs_base__slice_u8 src) {\n return wuffs_base__slice_u8__copy_from_slice(dst, src);\n}\n\nstatic uint64_t //\nwuffs_base__pixel_swizzler__swap_rgbx_bgrx(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t length4 = (dst.len < src.len ? dst.len : src.len) / 4;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = length4;\n while (n--) {\n uint8_t b0 = s[0];\n uint8_t b1 = s[1];\n uint8_t b2 = s[2];\n uint8_t b3 = s[3];\n d[0] = b2;\n d[1] = b1;\n d[2] = b0;\n d[3] = b3;\n s += 4;\n d += 4;\n }\n return length4 * 4;\n}\n\nvoid //\nwuffs_base__pixel_swizzler__initialize(wuffs_base__pixel_swizzler* p,\n wuffs_base__pixel_format dst_format,\n wuffs_b" + - "ase__slice_u8 dst_palette,\n wuffs_base__pixel_format src_format,\n wuffs_base__slice_u8 src_palette) {\n if (!p) {\n return;\n }\n\n // TODO: support many more formats.\n\n uint64_t (*func)(wuffs_base__slice_u8 dst, wuffs_base__slice_u8 dst_palette,\n wuffs_base__slice_u8 src) = NULL;\n\n switch (src_format) {\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL:\n switch (dst_format) {\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL:\n if (wuffs_base__slice_u8__copy_from_slice(dst_palette, src_palette) !=\n 1024) {\n break;\n }\n func = wuffs_base__pixel_swizzler__copy_1_1;\n break;\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:\n if (wuffs_base__slice_u8__copy_from_slice(dst_palette, src_palette) !=\n 1024) {\n break;\n }\n // TODO: implement.\n break;\n case WUFFS_BASE__PI" + - "XEL_FORMAT__RGBA_NONPREMUL:\n if (wuffs_base__pixel_swizzler__swap_rgbx_bgrx(dst_palette,\n src_palette) != 1024) {\n break;\n }\n // TODO: implement.\n break;\n default:\n break;\n }\n break;\n\n default:\n break;\n }\n\n p->private_impl.func = func;\n}\n\nuint64_t //\nwuffs_base__pixel_swizzler__swizzle_packed(wuffs_base__pixel_swizzler* p,\n wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 dst_palette,\n wuffs_base__slice_u8 src) {\n if (p && p->private_impl.func) {\n return (*(p->private_impl.func))(dst, dst_palette, src);\n }\n return 0;\n}\n" + + "// ---------------- Images\n\nstatic uint64_t //\nwuffs_base__pixel_swizzler__copy_1_1(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 dst_palette,\n wuffs_base__slice_u8 src) {\n return wuffs_base__slice_u8__copy_from_slice(dst, src);\n}\n\nstatic uint64_t //\nwuffs_base__pixel_swizzler__copy_4_1(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 dst_palette,\n wuffs_base__slice_u8 src) {\n if (dst_palette.len != 1024) {\n return 0;\n }\n size_t dst_len4 = dst.len / 4;\n size_t len = dst_len4 < src.len ? dst_len4 : src.len;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n\n size_t n = len;\n while (n--) {\n uint8_t* p = dst_palette.ptr + ((size_t)(*s) * 4);\n uint8_t b0 = p[0];\n uint8_t b1 = p[1];\n uint8_t b2 = p[2];\n uint8_t b3 = p[3];\n d[0] = b0;\n d[1] = b1;\n d[2] = b2;\n d[3] = b3;\n s += 1;\n d += 4;\n }\n return len;\n}\n\nstatic uint64_t //\nwu" + + "ffs_base__pixel_swizzler__swap_rgbx_bgrx(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t len4 = (dst.len < src.len ? dst.len : src.len) / 4;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n\n size_t n = len4;\n while (n--) {\n uint8_t b0 = s[0];\n uint8_t b1 = s[1];\n uint8_t b2 = s[2];\n uint8_t b3 = s[3];\n d[0] = b2;\n d[1] = b1;\n d[2] = b0;\n d[3] = b3;\n s += 4;\n d += 4;\n }\n return len4 * 4;\n}\n\nvoid //\nwuffs_base__pixel_swizzler__initialize(wuffs_base__pixel_swizzler* p,\n wuffs_base__pixel_format dst_format,\n wuffs_base__slice_u8 dst_palette,\n wuffs_base__pixel_format src_format,\n wuffs_base__slice_u8 src_palette) {\n if (!p) {\n return;\n }\n\n // TODO: support many more formats.\n\n uint64_t (*func)(wuffs_base__slice_u8 dst, wuffs_base__slice_u8 dst_palette,\n " + + " wuffs_base__slice_u8 src) = NULL;\n\n switch (src_format) {\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL:\n switch (dst_format) {\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL:\n if (wuffs_base__slice_u8__copy_from_slice(dst_palette, src_palette) !=\n 1024) {\n break;\n }\n func = wuffs_base__pixel_swizzler__copy_1_1;\n break;\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:\n if (wuffs_base__slice_u8__copy_from_slice(dst_palette, src_palette) !=\n 1024) {\n break;\n }\n func = wuffs_base__pixel_swizzler__copy_4_1;\n break;\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL:\n if (wuffs_base__pixel_swizzler__swap_rgbx_bgrx(dst_palette,\n src_palette) != 1024) {\n break;\n }\n func = wuffs_base__pixel_swizzler__copy_4_1;\n break;\n default:\n brea" + + "k;\n }\n break;\n\n default:\n break;\n }\n\n p->private_impl.func = func;\n}\n\nuint64_t //\nwuffs_base__pixel_swizzler__swizzle_packed(wuffs_base__pixel_swizzler* p,\n wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 dst_palette,\n wuffs_base__slice_u8 src) {\n if (p && p->private_impl.func) {\n return (*(p->private_impl.func))(dst, dst_palette, src);\n }\n return 0;\n}\n" + "" const baseCorePrivateH = "" + @@ -48,8 +49,8 @@ const baseCorePrivateH = "" + "" + "// --------\n\nstatic inline void //\nwuffs_base__u8__sat_add_indirect(uint8_t* x, uint8_t y) {\n *x = wuffs_base__u8__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u8__sat_sub_indirect(uint8_t* x, uint8_t y) {\n *x = wuffs_base__u8__sat_sub(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u16__sat_add_indirect(uint16_t* x, uint16_t y) {\n *x = wuffs_base__u16__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u16__sat_sub_indirect(uint16_t* x, uint16_t y) {\n *x = wuffs_base__u16__sat_sub(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u32__sat_add_indirect(uint32_t* x, uint32_t y) {\n *x = wuffs_base__u32__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u32__sat_sub_indirect(uint32_t* x, uint32_t y) {\n *x = wuffs_base__u32__sat_sub(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u64__sat_add_indirect(uint64_t* x, uint64_t y) {\n *x = wuffs_base__u64__sat_add(*x, y);\n}\n\nstatic inline void //\nwuffs_base__u64__sat_sub_indirect(uint64_t* x, uint64_t y) {\n *x = wuffs_base__u64__sat_sub(*x, y);\n}\n\n" + "" + - "// ---------------- Slices and Tables\n\n// wuffs_base__slice_u8__prefix returns up to the first up_to bytes of s.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__prefix(wuffs_base__slice_u8 s, uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.len = up_to;\n }\n return s;\n}\n\n// wuffs_base__slice_u8__suffix returns up to the last up_to bytes of s.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__suffix(wuffs_base__slice_u8 s, uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.ptr += (uint64_t)(s.len) - up_to;\n s.len = up_to;\n }\n return s;\n}\n\n// wuffs_base__slice_u8__copy_from_slice calls memmove(dst.ptr, src.ptr,\n// length) where length is the minimum of dst.len and src.len.\n//\n// Passing a wuffs_base__slice_u8 with all fields NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline uint64_t //\nwuffs_base__slice_u8__copy_from_slice(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t lengt" + - "h = dst.len < src.len ? dst.len : src.len;\n if (length > 0) {\n memmove(dst.ptr, src.ptr, length);\n }\n return length;\n}\n\n" + + "// ---------------- Slices and Tables\n\n// wuffs_base__slice_u8__prefix returns up to the first up_to bytes of s.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__prefix(wuffs_base__slice_u8 s, uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.len = up_to;\n }\n return s;\n}\n\n// wuffs_base__slice_u8__suffix returns up to the last up_to bytes of s.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__slice_u8__suffix(wuffs_base__slice_u8 s, uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.ptr += (uint64_t)(s.len) - up_to;\n s.len = up_to;\n }\n return s;\n}\n\n// wuffs_base__slice_u8__copy_from_slice calls memmove(dst.ptr, src.ptr, len)\n// where len is the minimum of dst.len and src.len.\n//\n// Passing a wuffs_base__slice_u8 with all fields NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline uint64_t //\nwuffs_base__slice_u8__copy_from_slice(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t len = dst.l" + + "en < src.len ? dst.len : src.len;\n if (len > 0) {\n memmove(dst.ptr, src.ptr, len);\n }\n return len;\n}\n\n" + "" + "// --------\n\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__table_u8__row(wuffs_base__table_u8 t, uint32_t y) {\n if (y < t.height) {\n return ((wuffs_base__slice_u8){\n .ptr = t.ptr + (t.stride * y),\n .len = t.width,\n });\n }\n return ((wuffs_base__slice_u8){});\n}\n" + "" @@ -110,8 +111,9 @@ const baseImagePublicH = "" + "// --------\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__pixel_format pixfmt;\n wuffs_base__pixel_subsampling pixsub;\n uint32_t width;\n uint32_t height;\n } private_impl;\n\n#ifdef __cplusplus\n inline void initialize(wuffs_base__pixel_format pixfmt,\n wuffs_base__pixel_subsampling pixsub,\n uint32_t width,\n uint32_t height);\n inline void invalidate();\n inline bool is_valid();\n inline wuffs_base__pixel_format pixel_format();\n inline wuffs_base__pixel_subsampling pixel_subsampling();\n inline wuffs_base__rect_ie_u32 bounds();\n inline uint32_t width();\n inline uint32_t height();\n inline uint64_t pixbuf_len();\n#endif // __cplusplus\n\n} wuffs_base__pixel_config;\n\n// TODO: Should this function return bool? An error type?\nstatic inline void //\nwuffs_base__pixel_config__initialize(wuffs_base__pixel_config* c" + ",\n wuffs_base__pixel_format pixfmt,\n wuffs_base__pixel_subsampling pixsub,\n uint32_t width,\n uint32_t height) {\n if (!c) {\n return;\n }\n if (pixfmt) {\n uint64_t wh = ((uint64_t)width) * ((uint64_t)height);\n // TODO: handle things other than 1 byte per pixel.\n if (wh <= ((uint64_t)SIZE_MAX)) {\n c->private_impl.pixfmt = pixfmt;\n c->private_impl.pixsub = pixsub;\n c->private_impl.width = width;\n c->private_impl.height = height;\n return;\n }\n }\n *c = ((wuffs_base__pixel_config){});\n}\n\nstatic inline void //\nwuffs_base__pixel_config__invalidate(wuffs_base__pixel_config* c) {\n if (c) {\n *c = ((wuffs_base__pixel_config){});\n }\n}\n\nstatic inline bool //\nwuffs_base__pixel_config__is_valid(wuffs_base__pixel_config* c) {\n return c && c->private_impl.pixfmt;\n}\n\nstatic inline wuffs_base__pixel_format //\nwuffs_base__pixel_config__pix" + "el_format(wuffs_base__pixel_config* c) {\n return c ? c->private_impl.pixfmt : 0;\n}\n\nstatic inline wuffs_base__pixel_subsampling //\nwuffs_base__pixel_config__pixel_subsampling(wuffs_base__pixel_config* c) {\n return c ? c->private_impl.pixsub : 0;\n}\n\nstatic inline wuffs_base__rect_ie_u32 //\nwuffs_base__pixel_config__bounds(wuffs_base__pixel_config* c) {\n return c ? ((wuffs_base__rect_ie_u32){\n .min_incl_x = 0,\n .min_incl_y = 0,\n .max_excl_x = c->private_impl.width,\n .max_excl_y = c->private_impl.height,\n })\n : ((wuffs_base__rect_ie_u32){});\n}\n\nstatic inline uint32_t //\nwuffs_base__pixel_config__width(wuffs_base__pixel_config* c) {\n return c ? c->private_impl.width : 0;\n}\n\nstatic inline uint32_t //\nwuffs_base__pixel_config__height(wuffs_base__pixel_config* c) {\n return c ? c->private_impl.height : 0;\n}\n\n// TODO: this is the right API for planar (not packed) pixbufs? Should it allow\n// decoding into a color model differ" + - "ent from the format's intrinsic one? For\n// example, decoding a JPEG image straight to RGBA instead of to YCbCr?\nstatic inline uint64_t //\nwuffs_base__pixel_config__pixbuf_len(wuffs_base__pixel_config* c) {\n if (c) {\n uint64_t n =\n ((uint64_t)c->private_impl.width) * ((uint64_t)c->private_impl.height);\n // TODO: handle things other than 1 byte per pixel. When doing so, consider\n // that the +1024 below could overflow.\n if (wuffs_base__pixel_format__is_indexed(c->private_impl.pixfmt)) {\n n += 1024;\n }\n return n;\n }\n return 0;\n}\n\n#ifdef __cplusplus\n\ninline void //\nwuffs_base__pixel_config::initialize(wuffs_base__pixel_format pixfmt,\n wuffs_base__pixel_subsampling pixsub,\n uint32_t width,\n uint32_t height) {\n wuffs_base__pixel_config__initialize(this, pixfmt, pixsub, width, height);\n}\n\ninline void //\nwuffs_base__pixel_config::invalidate() {\n wuffs_base__pixel_config__i" + - "nvalidate(this);\n}\n\ninline bool //\nwuffs_base__pixel_config::is_valid() {\n return wuffs_base__pixel_config__is_valid(this);\n}\n\ninline wuffs_base__pixel_format //\nwuffs_base__pixel_config::pixel_format() {\n return wuffs_base__pixel_config__pixel_format(this);\n}\n\ninline wuffs_base__pixel_subsampling //\nwuffs_base__pixel_config::pixel_subsampling() {\n return wuffs_base__pixel_config__pixel_subsampling(this);\n}\n\ninline wuffs_base__rect_ie_u32 //\nwuffs_base__pixel_config::bounds() {\n return wuffs_base__pixel_config__bounds(this);\n}\n\ninline uint32_t //\nwuffs_base__pixel_config::width() {\n return wuffs_base__pixel_config__width(this);\n}\n\ninline uint32_t //\nwuffs_base__pixel_config::height() {\n return wuffs_base__pixel_config__height(this);\n}\n\ninline uint64_t //\nwuffs_base__pixel_config::pixbuf_len() {\n return wuffs_base__pixel_config__pixbuf_len(this);\n}\n\n#endif // __cplusplus\n\n" + + "ent from the format's intrinsic one? For\n// example, decoding a JPEG image straight to RGBA instead of to YCbCr?\nstatic inline uint64_t //\nwuffs_base__pixel_config__pixbuf_len(wuffs_base__pixel_config* c) {\n // TODO: support more pixel formats.\n uint64_t bytes_per_pixel = 0;\n switch (wuffs_base__pixel_config__pixel_format(c)) {\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL:\n bytes_per_pixel = 1;\n break;\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL:\n bytes_per_pixel = 4;\n break;\n default:\n return 0;\n }\n\n if (!c) {\n return 0;\n }\n\n uint64_t n =\n ((uint64_t)c->private_impl.width) * ((uint64_t)c->private_impl.height);\n\n if (n > (UINT64_MAX / bytes_per_pixel)) {\n return 0;\n }\n n *= bytes_per_pixel;\n\n if (wuffs_base__pixel_format__is_indexed(c->private_impl.pixfmt)) {\n if (n > (UINT64_MAX - 1024)) {\n return 0;\n }\n n += 1024;\n }\n\n return n;\n}\n\n#ifdef __cplusplus\n\ninline void //\nwuff" + + "s_base__pixel_config::initialize(wuffs_base__pixel_format pixfmt,\n wuffs_base__pixel_subsampling pixsub,\n uint32_t width,\n uint32_t height) {\n wuffs_base__pixel_config__initialize(this, pixfmt, pixsub, width, height);\n}\n\ninline void //\nwuffs_base__pixel_config::invalidate() {\n wuffs_base__pixel_config__invalidate(this);\n}\n\ninline bool //\nwuffs_base__pixel_config::is_valid() {\n return wuffs_base__pixel_config__is_valid(this);\n}\n\ninline wuffs_base__pixel_format //\nwuffs_base__pixel_config::pixel_format() {\n return wuffs_base__pixel_config__pixel_format(this);\n}\n\ninline wuffs_base__pixel_subsampling //\nwuffs_base__pixel_config::pixel_subsampling() {\n return wuffs_base__pixel_config__pixel_subsampling(this);\n}\n\ninline wuffs_base__rect_ie_u32 //\nwuffs_base__pixel_config::bounds() {\n return wuffs_base__pixel_config__bounds(this);\n}\n\ninline uint32_t //\nwuffs_base__pixel_config::width() {\n return w" + + "uffs_base__pixel_config__width(this);\n}\n\ninline uint32_t //\nwuffs_base__pixel_config::height() {\n return wuffs_base__pixel_config__height(this);\n}\n\ninline uint64_t //\nwuffs_base__pixel_config::pixbuf_len() {\n return wuffs_base__pixel_config__pixbuf_len(this);\n}\n\n#endif // __cplusplus\n\n" + "" + "// --------\n\ntypedef struct {\n wuffs_base__pixel_config pixcfg;\n\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__range_ii_u64 workbuf_len;\n uint64_t first_frame_io_position;\n bool first_frame_is_opaque;\n } private_impl;\n\n#ifdef __cplusplus\n inline void initialize(wuffs_base__pixel_format pixfmt,\n wuffs_base__pixel_subsampling pixsub,\n uint32_t width,\n uint32_t height,\n uint64_t workbuf_len0,\n uint64_t workbuf_len1,\n uint64_t first_frame_io_position,\n bool first_frame_is_opaque);\n inline void invalidate();\n inline bool is_valid();\n inline wuffs_base__range_ii_u64 workbuf_len();\n inline uint64_t first_frame_io_position();\n inline bool first_frame_is_opaque();\n#endif // __cplusplus\n\n} wuffs_base__image_config;\n\n// TODO: Should " + "this function return bool? An error type?\nstatic inline void //\nwuffs_base__image_config__initialize(wuffs_base__image_config* c,\n wuffs_base__pixel_format pixfmt,\n wuffs_base__pixel_subsampling pixsub,\n uint32_t width,\n uint32_t height,\n uint64_t workbuf_len0,\n uint64_t workbuf_len1,\n uint64_t first_frame_io_position,\n bool first_frame_is_opaque) {\n if (!c) {\n return;\n }\n if (wuffs_base__pixel_format__is_valid(pixfmt)) {\n c->pixcfg.private_impl.pixfmt = pixfmt;\n c->pixcfg.private_impl.pixsub = pixsub;\n c->pixcfg.private_impl.width = width;\n c->pixcfg.private_impl.height = height;\n c->private_impl.workbuf_len.min_incl = workbuf_len0;\n c->private_impl.workbuf_len.max_incl = workbuf_len1;\n c->private_" + @@ -131,10 +133,10 @@ const baseImagePublicH = "" + "ame_config::disposal() {\n return wuffs_base__frame_config__disposal(this);\n}\n\n#endif // __cplusplus\n\n" + "" + "// --------\n\ntypedef struct {\n wuffs_base__pixel_config pixcfg;\n\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__table_u8 planes[WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX];\n // TODO: color spaces.\n } private_impl;\n\n#ifdef __cplusplus\n inline wuffs_base__status set_from_slice(wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory);\n inline wuffs_base__slice_u8 palette();\n inline wuffs_base__pixel_format pixel_format();\n inline wuffs_base__table_u8 plane(uint32_t p);\n#endif // __cplusplus\n\n} wuffs_base__pixel_buffer;\n\nstatic inline wuffs_base__status //\nwuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* b,\n wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n if (!b) {\n return wuffs_base__error__bad_receiver;\n }\n *b = (" + - "(wuffs_base__pixel_buffer){});\n if (!pixcfg) {\n return wuffs_base__error__bad_argument;\n }\n\n uint8_t* ptr = pixbuf_memory.ptr;\n uint64_t len = pixbuf_memory.len;\n if (wuffs_base__pixel_format__is_indexed(pixcfg->private_impl.pixfmt)) {\n // Split a 1024 byte chunk (256 palette entries × 4 bytes per entry) from\n // the start of pixbuf_memory. We split from the start, not the end, so\n // that the both chunks' pointers have the same alignment as the original\n // pointer, up to an alignment of 1024.\n if (len < 1024) {\n return wuffs_base__error__bad_argument_length_too_short;\n }\n wuffs_base__table_u8* tab =\n &b->private_impl.planes[WUFFS_BASE__PIXEL_FORMAT__INDEXED__COLOR_PLANE];\n tab->ptr = ptr;\n tab->width = 1024;\n tab->height = 1;\n tab->stride = 1024;\n ptr += 1024;\n len -= 1024;\n }\n\n // TODO: don't assume 1 byte per pixel. Don't assume packed.\n uint64_t wh = ((uint64_t)pixcfg->private_impl.width) *\n ((uint64_t)pixcfg->private_impl.heig" + - "ht);\n if (wh > len) {\n return wuffs_base__error__bad_argument_length_too_short;\n }\n b->pixcfg = *pixcfg;\n wuffs_base__table_u8* tab = &b->private_impl.planes[0];\n tab->ptr = ptr;\n tab->width = pixcfg->private_impl.width;\n tab->height = pixcfg->private_impl.height;\n tab->stride = pixcfg->private_impl.width;\n return NULL;\n}\n\n// wuffs_base__pixel_buffer__palette returns the palette color data. If\n// non-empty, it will have length 1024.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__pixel_buffer__palette(wuffs_base__pixel_buffer* b) {\n if (b &&\n wuffs_base__pixel_format__is_indexed(b->pixcfg.private_impl.pixfmt)) {\n wuffs_base__table_u8* tab =\n &b->private_impl.planes[WUFFS_BASE__PIXEL_FORMAT__INDEXED__COLOR_PLANE];\n if ((tab->width == 1024) && (tab->height == 1)) {\n return ((wuffs_base__slice_u8){\n .ptr = tab->ptr,\n .len = 1024,\n });\n }\n }\n return ((wuffs_base__slice_u8){});\n}\n\nstatic inline wuffs_base__pixel_format //\nwuffs_base__pixel_buffer_" + - "_pixel_format(wuffs_base__pixel_buffer* b) {\n if (b) {\n return b->pixcfg.private_impl.pixfmt;\n }\n return WUFFS_BASE__PIXEL_FORMAT__INVALID;\n}\n\nstatic inline wuffs_base__table_u8 //\nwuffs_base__pixel_buffer__plane(wuffs_base__pixel_buffer* b, uint32_t p) {\n return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))\n ? b->private_impl.planes[p]\n : ((wuffs_base__table_u8){});\n}\n\n#ifdef __cplusplus\n\ninline wuffs_base__status //\nwuffs_base__pixel_buffer::set_from_slice(wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n return wuffs_base__pixel_buffer__set_from_slice(this, pixcfg, pixbuf_memory);\n}\n\ninline wuffs_base__slice_u8 //\nwuffs_base__pixel_buffer::palette() {\n return wuffs_base__pixel_buffer__palette(this);\n}\n\ninline wuffs_base__pixel_format //\nwuffs_base__pixel_buffer::pixel_format() {\n return wuffs_base__pixel_buffer__pixel_format(this);\n}\n\ninline wuffs_base__table_u8 //\nwuffs_base__pixel_buffer::plane(" + - "uint32_t p) {\n return wuffs_base__pixel_buffer__plane(this, p);\n}\n\n#endif // __cplusplus\n\n" + + "(wuffs_base__pixel_buffer){});\n if (!pixcfg) {\n return wuffs_base__error__bad_argument;\n }\n\n uint8_t* ptr = pixbuf_memory.ptr;\n uint64_t len = pixbuf_memory.len;\n if (wuffs_base__pixel_format__is_indexed(pixcfg->private_impl.pixfmt)) {\n // Split a 1024 byte chunk (256 palette entries × 4 bytes per entry) from\n // the start of pixbuf_memory. We split from the start, not the end, so\n // that the both chunks' pointers have the same alignment as the original\n // pointer, up to an alignment of 1024.\n if (len < 1024) {\n return wuffs_base__error__bad_argument_length_too_short;\n }\n wuffs_base__table_u8* tab =\n &b->private_impl.planes[WUFFS_BASE__PIXEL_FORMAT__INDEXED__COLOR_PLANE];\n tab->ptr = ptr;\n tab->width = 1024;\n tab->height = 1;\n tab->stride = 1024;\n ptr += 1024;\n len -= 1024;\n }\n\n // TODO: support more pixel formats.\n uint64_t bytes_per_pixel = 0;\n switch (wuffs_base__pixel_config__pixel_format(pixcfg)) {\n case WUFFS_BASE__PIXEL_FORMAT__INDEXE" + + "D__BGRA_NONPREMUL:\n bytes_per_pixel = 1;\n break;\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL:\n bytes_per_pixel = 4;\n break;\n default:\n return wuffs_base__error__bad_argument;\n }\n\n // TODO: don't assume packed.\n uint64_t wh = ((uint64_t)pixcfg->private_impl.width) *\n ((uint64_t)pixcfg->private_impl.height);\n size_t width = (size_t)(pixcfg->private_impl.width);\n if ((wh > (UINT64_MAX / bytes_per_pixel)) ||\n (width > (SIZE_MAX / bytes_per_pixel))) {\n return wuffs_base__error__bad_argument;\n }\n wh *= bytes_per_pixel;\n width *= bytes_per_pixel;\n if (wh > len) {\n return wuffs_base__error__bad_argument_length_too_short;\n }\n\n b->pixcfg = *pixcfg;\n wuffs_base__table_u8* tab = &b->private_impl.planes[0];\n tab->ptr = ptr;\n tab->width = width;\n tab->height = pixcfg->private_impl.height;\n tab->stride = width;\n return NULL;\n}\n\n// wuffs_base__pixel_buffer__palette returns the palette color data. If" + + "\n// non-empty, it will have length 1024.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__pixel_buffer__palette(wuffs_base__pixel_buffer* b) {\n if (b &&\n wuffs_base__pixel_format__is_indexed(b->pixcfg.private_impl.pixfmt)) {\n wuffs_base__table_u8* tab =\n &b->private_impl.planes[WUFFS_BASE__PIXEL_FORMAT__INDEXED__COLOR_PLANE];\n if ((tab->width == 1024) && (tab->height == 1)) {\n return ((wuffs_base__slice_u8){\n .ptr = tab->ptr,\n .len = 1024,\n });\n }\n }\n return ((wuffs_base__slice_u8){});\n}\n\nstatic inline wuffs_base__pixel_format //\nwuffs_base__pixel_buffer__pixel_format(wuffs_base__pixel_buffer* b) {\n if (b) {\n return b->pixcfg.private_impl.pixfmt;\n }\n return WUFFS_BASE__PIXEL_FORMAT__INVALID;\n}\n\nstatic inline wuffs_base__table_u8 //\nwuffs_base__pixel_buffer__plane(wuffs_base__pixel_buffer* b, uint32_t p) {\n return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))\n ? b->private_impl.planes[p]\n : ((wuffs_base__table_u8){})" + + ";\n}\n\n#ifdef __cplusplus\n\ninline wuffs_base__status //\nwuffs_base__pixel_buffer::set_from_slice(wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n return wuffs_base__pixel_buffer__set_from_slice(this, pixcfg, pixbuf_memory);\n}\n\ninline wuffs_base__slice_u8 //\nwuffs_base__pixel_buffer::palette() {\n return wuffs_base__pixel_buffer__palette(this);\n}\n\ninline wuffs_base__pixel_format //\nwuffs_base__pixel_buffer::pixel_format() {\n return wuffs_base__pixel_buffer__pixel_format(this);\n}\n\ninline wuffs_base__table_u8 //\nwuffs_base__pixel_buffer::plane(uint32_t p) {\n return wuffs_base__pixel_buffer__plane(this, p);\n}\n\n#endif // __cplusplus\n\n" + "" + "// --------\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n uint8_t TODO;\n } private_impl;\n\n#ifdef __cplusplus\n#endif // __cplusplus\n\n} wuffs_base__decode_frame_options;\n\n#ifdef __cplusplus\n\n#endif // __cplusplus\n\n" + "" + diff --git a/example/gifplayer/gifplayer.c b/example/gifplayer/gifplayer.c index a100a44e..ed16b458 100644 --- a/example/gifplayer/gifplayer.c +++ b/example/gifplayer/gifplayer.c @@ -145,26 +145,21 @@ void restore_background(wuffs_base__pixel_buffer* pb, } void compose(wuffs_base__pixel_buffer* pb, wuffs_base__rect_ie_u32 bounds) { - wuffs_base__slice_u8 palette = wuffs_base__pixel_buffer__palette(pb); - if (palette.len != 1024) { - return; - } wuffs_base__table_u8 tab = wuffs_base__pixel_buffer__plane(pb, 0); size_t width = wuffs_base__pixel_config__width(&pb->pixcfg); size_t y; for (y = bounds.min_incl_y; y < bounds.max_excl_y; y++) { size_t x; wuffs_base__color_u32_argb_premul* d = - curr_dst_buffer + (y * width) + bounds.min_incl_x; - uint8_t* s = tab.ptr + (y * tab.stride) + bounds.min_incl_x; + curr_dst_buffer + (y * width) + (bounds.min_incl_x * 1); + uint8_t* s = tab.ptr + (y * tab.stride) + (bounds.min_incl_x * 4); for (x = bounds.min_incl_x; x < bounds.max_excl_x; x++) { - uint32_t index = *s++; - wuffs_base__color_u32_argb_premul c = - load_u32le(palette.ptr + (4 * index)); + wuffs_base__color_u32_argb_premul c = load_u32le(s); if (c) { *d = c; } - d++; + d += 1; + s += 4; } } } @@ -322,6 +317,12 @@ const char* play() { if ((width > MAX_DIMENSION) || (height > MAX_DIMENSION)) { return "image dimensions are too large"; } + + // Override the source's indexed pixel format to be non-indexed. + wuffs_base__pixel_config__initialize( + &ic.pixcfg, WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL, + WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, width, height); + const char* msg = allocate(&dec, &ic); if (msg) { return msg; diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index 317e2d71..f757c791 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -1577,17 +1577,40 @@ wuffs_base__pixel_config__height(wuffs_base__pixel_config* c) { // example, decoding a JPEG image straight to RGBA instead of to YCbCr? static inline uint64_t // wuffs_base__pixel_config__pixbuf_len(wuffs_base__pixel_config* c) { - if (c) { - uint64_t n = - ((uint64_t)c->private_impl.width) * ((uint64_t)c->private_impl.height); - // TODO: handle things other than 1 byte per pixel. When doing so, consider - // that the +1024 below could overflow. - if (wuffs_base__pixel_format__is_indexed(c->private_impl.pixfmt)) { - n += 1024; - } - return n; + // TODO: support more pixel formats. + uint64_t bytes_per_pixel = 0; + switch (wuffs_base__pixel_config__pixel_format(c)) { + case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL: + bytes_per_pixel = 1; + break; + case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL: + case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: + bytes_per_pixel = 4; + break; + default: + return 0; } - return 0; + + if (!c) { + return 0; + } + + uint64_t n = + ((uint64_t)c->private_impl.width) * ((uint64_t)c->private_impl.height); + + if (n > (UINT64_MAX / bytes_per_pixel)) { + return 0; + } + n *= bytes_per_pixel; + + if (wuffs_base__pixel_format__is_indexed(c->private_impl.pixfmt)) { + if (n > (UINT64_MAX - 1024)) { + return 0; + } + n += 1024; + } + + return n; } #ifdef __cplusplus @@ -2021,18 +2044,40 @@ wuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* b, len -= 1024; } - // TODO: don't assume 1 byte per pixel. Don't assume packed. + // TODO: support more pixel formats. + uint64_t bytes_per_pixel = 0; + switch (wuffs_base__pixel_config__pixel_format(pixcfg)) { + case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL: + bytes_per_pixel = 1; + break; + case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL: + case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: + bytes_per_pixel = 4; + break; + default: + return wuffs_base__error__bad_argument; + } + + // TODO: don't assume packed. uint64_t wh = ((uint64_t)pixcfg->private_impl.width) * ((uint64_t)pixcfg->private_impl.height); + size_t width = (size_t)(pixcfg->private_impl.width); + if ((wh > (UINT64_MAX / bytes_per_pixel)) || + (width > (SIZE_MAX / bytes_per_pixel))) { + return wuffs_base__error__bad_argument; + } + wh *= bytes_per_pixel; + width *= bytes_per_pixel; if (wh > len) { return wuffs_base__error__bad_argument_length_too_short; } + b->pixcfg = *pixcfg; wuffs_base__table_u8* tab = &b->private_impl.planes[0]; tab->ptr = ptr; - tab->width = pixcfg->private_impl.width; + tab->width = width; tab->height = pixcfg->private_impl.height; - tab->stride = pixcfg->private_impl.width; + tab->stride = width; return NULL; } @@ -3444,19 +3489,19 @@ wuffs_base__slice_u8__suffix(wuffs_base__slice_u8 s, uint64_t up_to) { return s; } -// wuffs_base__slice_u8__copy_from_slice calls memmove(dst.ptr, src.ptr, -// length) where length is the minimum of dst.len and src.len. +// wuffs_base__slice_u8__copy_from_slice calls memmove(dst.ptr, src.ptr, len) +// where len is the minimum of dst.len and src.len. // // Passing a wuffs_base__slice_u8 with all fields NULL or zero (a valid, empty // slice) is valid and results in a no-op. static inline uint64_t // wuffs_base__slice_u8__copy_from_slice(wuffs_base__slice_u8 dst, wuffs_base__slice_u8 src) { - size_t length = dst.len < src.len ? dst.len : src.len; - if (length > 0) { - memmove(dst.ptr, src.ptr, length); + size_t len = dst.len < src.len ? dst.len : src.len; + if (len > 0) { + memmove(dst.ptr, src.ptr, len); } - return length; + return len; } // -------- @@ -3882,12 +3927,42 @@ wuffs_base__pixel_swizzler__copy_1_1(wuffs_base__slice_u8 dst, } static uint64_t // -wuffs_base__pixel_swizzler__swap_rgbx_bgrx(wuffs_base__slice_u8 dst, - wuffs_base__slice_u8 src) { - size_t length4 = (dst.len < src.len ? dst.len : src.len) / 4; +wuffs_base__pixel_swizzler__copy_4_1(wuffs_base__slice_u8 dst, + wuffs_base__slice_u8 dst_palette, + wuffs_base__slice_u8 src) { + if (dst_palette.len != 1024) { + return 0; + } + size_t dst_len4 = dst.len / 4; + size_t len = dst_len4 < src.len ? dst_len4 : src.len; uint8_t* d = dst.ptr; uint8_t* s = src.ptr; - size_t n = length4; + + size_t n = len; + while (n--) { + uint8_t* p = dst_palette.ptr + ((size_t)(*s) * 4); + uint8_t b0 = p[0]; + uint8_t b1 = p[1]; + uint8_t b2 = p[2]; + uint8_t b3 = p[3]; + d[0] = b0; + d[1] = b1; + d[2] = b2; + d[3] = b3; + s += 1; + d += 4; + } + return len; +} + +static uint64_t // +wuffs_base__pixel_swizzler__swap_rgbx_bgrx(wuffs_base__slice_u8 dst, + wuffs_base__slice_u8 src) { + size_t len4 = (dst.len < src.len ? dst.len : src.len) / 4; + uint8_t* d = dst.ptr; + uint8_t* s = src.ptr; + + size_t n = len4; while (n--) { uint8_t b0 = s[0]; uint8_t b1 = s[1]; @@ -3900,7 +3975,7 @@ wuffs_base__pixel_swizzler__swap_rgbx_bgrx(wuffs_base__slice_u8 dst, s += 4; d += 4; } - return length4 * 4; + return len4 * 4; } void // @@ -3933,14 +4008,14 @@ wuffs_base__pixel_swizzler__initialize(wuffs_base__pixel_swizzler* p, 1024) { break; } - // TODO: implement. + func = wuffs_base__pixel_swizzler__copy_4_1; break; case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: if (wuffs_base__pixel_swizzler__swap_rgbx_bgrx(dst_palette, src_palette) != 1024) { break; } - // TODO: implement. + func = wuffs_base__pixel_swizzler__copy_4_1; break; default: break; @@ -9427,13 +9502,22 @@ wuffs_gif__decoder__copy_to_image_buffer(wuffs_gif__decoder* self, wuffs_base__slice_u8 v_src; uint32_t v_n; uint32_t v_new_ri; + uint32_t v_bytes_per_pixel; + uint32_t v_pixfmt; wuffs_base__table_u8 v_tab; + uint64_t v_i; + uint64_t v_j; uint64_t v_n64; v_dst = ((wuffs_base__slice_u8){}); v_src = ((wuffs_base__slice_u8){}); v_n = 0; v_new_ri = 0; + v_bytes_per_pixel = 1; + v_pixfmt = wuffs_base__pixel_buffer__pixel_format(a_pb); + if ((v_pixfmt == 1107331208) || (v_pixfmt == 1375766664)) { + v_bytes_per_pixel = 4; + } v_tab = wuffs_base__pixel_buffer__plane(a_pb, 0); label_0_continue:; while (self->private_impl.f_uncompressed_wi > @@ -9450,17 +9534,15 @@ label_0_continue:; goto exit; } v_dst = wuffs_base__table_u8__row(v_tab, self->private_impl.f_dst_y); - if (((uint64_t)(self->private_impl.f_dst_x)) < ((uint64_t)(v_dst.len))) { - if ((((uint64_t)(self->private_impl.f_dst_x)) <= - ((uint64_t)(self->private_impl.f_frame_rect_x1))) && - (((uint64_t)(self->private_impl.f_frame_rect_x1)) <= - ((uint64_t)(v_dst.len)))) { - v_dst = wuffs_base__slice_u8__subslice_ij( - v_dst, ((uint64_t)(self->private_impl.f_dst_x)), - ((uint64_t)(self->private_impl.f_frame_rect_x1))); + v_i = (((uint64_t)(self->private_impl.f_dst_x)) * + ((uint64_t)(v_bytes_per_pixel))); + if (v_i < ((uint64_t)(v_dst.len))) { + v_j = (((uint64_t)(self->private_impl.f_frame_rect_x1)) * + ((uint64_t)(v_bytes_per_pixel))); + if ((v_i <= v_j) && (v_j <= ((uint64_t)(v_dst.len)))) { + v_dst = wuffs_base__slice_u8__subslice_ij(v_dst, v_i, v_j); } else { - v_dst = wuffs_base__slice_u8__subslice_i( - v_dst, ((uint64_t)(self->private_impl.f_dst_x))); + v_dst = wuffs_base__slice_u8__subslice_i(v_dst, v_i); } v_n64 = wuffs_base__pixel_swizzler__swizzle_packed( &self->private_impl.f_swizzler, v_dst, diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index 49287ffe..4744502f 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -688,6 +688,14 @@ pri func decoder.copy_to_image_buffer!?(pb ptr base.pixel_buffer) { // TODO: we shouldn't need this temporary variable. var new_ri base.u32 + // TODO: a Wuffs (not just C) name for the WUFFS_BASE__PIXEL_FORMAT__ETC + // magic pixfmt constants. Also, support more formats. + var bytes_per_pixel base.u32[1..64] = 1 + var pixfmt base.u32 = args.pb.pixel_format() + if (pixfmt == 0x42008888) or (pixfmt == 0x52008888) { + bytes_per_pixel = 4 + } + var tab table base.u8 = args.pb.plane(p:0) while this.uncompressed_wi > this.uncompressed_ri { @@ -702,13 +710,13 @@ pri func decoder.copy_to_image_buffer!?(pb ptr base.pixel_buffer) { // args.pb's bounds. dst = tab.row(y:this.dst_y) - if (this.dst_x as base.u64) < dst.length() { - if ((this.dst_x as base.u64) <= (this.frame_rect_x1 as base.u64)) and - ((this.frame_rect_x1 as base.u64) <= dst.length()) { - // TODO: drop the "as base.u64"s on the next line? - dst = dst[this.dst_x as base.u64:this.frame_rect_x1 as base.u64] + var i base.u64 = (this.dst_x as base.u64) * (bytes_per_pixel as base.u64) + if i < dst.length() { + var j base.u64 = (this.frame_rect_x1 as base.u64) * (bytes_per_pixel as base.u64) + if (i <= j) and (j <= dst.length()) { + dst = dst[i:j] } else { - dst = dst[this.dst_x as base.u64:] + dst = dst[i:] } var n64 base.u64 = this.swizzler.swizzle_packed!(dst:dst, dst_palette:this.dst_palette[:], src:src) n = (n64 & 0xFFFFFFFF) as base.u32 diff --git a/test/c/std/gif.c b/test/c/std/gif.c index a543cd56..221614da 100644 --- a/test/c/std/gif.c +++ b/test/c/std/gif.c @@ -237,7 +237,8 @@ const char* wuffs_gif_decode(wuffs_base__io_buffer* dst, const char* do_test_wuffs_gif_decode(const char* filename, const char* palette_filename, const char* indexes_filename, - uint64_t rlimit) { + uint64_t rlimit, + wuffs_base__pixel_format dst_pixfmt) { wuffs_base__io_buffer got = ((wuffs_base__io_buffer){ .data = global_got_slice, }); @@ -285,6 +286,10 @@ const char* do_test_wuffs_gif_decode(const char* filename, if (!wuffs_base__image_config__first_frame_is_opaque(&ic)) { RETURN_FAIL("first_frame_is_opaque: got false, want true"); } + + wuffs_base__pixel_config__initialize( + &ic.pixcfg, dst_pixfmt, WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, 160, 120); + z = wuffs_base__pixel_buffer__set_from_slice(&pb, &ic.pixcfg, global_pixel_slice); if (z) { @@ -372,10 +377,6 @@ const char* do_test_wuffs_gif_decode(const char* filename, } } - wuffs_base__io_buffer pal_got = ((wuffs_base__io_buffer){ - .data = wuffs_base__pixel_buffer__palette(&pb), - }); - pal_got.meta.wi = pal_got.data.len; uint8_t pal_want_array[1024]; wuffs_base__io_buffer pal_want = ((wuffs_base__io_buffer){ .data = ((wuffs_base__slice_u8){ @@ -387,9 +388,15 @@ const char* do_test_wuffs_gif_decode(const char* filename, if (z) { return z; } - z = check_io_buffers_equal("palette ", &pal_got, &pal_want); - if (z) { - return z; + if (dst_pixfmt == WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL) { + wuffs_base__io_buffer pal_got = ((wuffs_base__io_buffer){ + .data = wuffs_base__pixel_buffer__palette(&pb), + }); + pal_got.meta.wi = pal_got.data.len; + z = check_io_buffers_equal("palette ", &pal_got, &pal_want); + if (z) { + return z; + } } wuffs_base__io_buffer ind_want = ((wuffs_base__io_buffer){ @@ -399,9 +406,31 @@ const char* do_test_wuffs_gif_decode(const char* filename, if (z) { return z; } - z = check_io_buffers_equal("indexes ", &got, &ind_want); - if (z) { - return z; + if (dst_pixfmt == WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL) { + z = check_io_buffers_equal("indexes ", &got, &ind_want); + if (z) { + return z; + } + } else { + wuffs_base__io_buffer expanded_want = ((wuffs_base__io_buffer){ + .data = global_work_slice, + }); + if (ind_want.meta.wi > (expanded_want.data.len / 4)) { + RETURN_FAIL("indexes are too long to expand into the work buffer"); + } + size_t i; + for (i = 0; i < ind_want.meta.wi; i++) { + uint8_t index = ind_want.data.ptr[i]; + expanded_want.data.ptr[4 * i + 0] = pal_want_array[4 * index + 0]; + expanded_want.data.ptr[4 * i + 1] = pal_want_array[4 * index + 1]; + expanded_want.data.ptr[4 * i + 2] = pal_want_array[4 * index + 2]; + expanded_want.data.ptr[4 * i + 3] = pal_want_array[4 * index + 3]; + } + expanded_want.meta.wi = 4 * ind_want.meta.wi; + z = check_io_buffers_equal("pixels ", &got, &expanded_want); + if (z) { + return z; + } } { @@ -701,25 +730,36 @@ const char* test_wuffs_gif_decode_frame_out_of_bounds() { return NULL; } -const char* test_wuffs_gif_decode_input_is_a_gif() { +const char* test_wuffs_gif_decode_bgra_nonpremul() { CHECK_FOCUS(__func__); return do_test_wuffs_gif_decode("test/data/bricks-dither.gif", "test/data/bricks-dither.palette", - "test/data/bricks-dither.indexes", 0); + "test/data/bricks-dither.indexes", 0, + WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL); +} + +const char* test_wuffs_gif_decode_input_is_a_gif() { + CHECK_FOCUS(__func__); + return do_test_wuffs_gif_decode( + "test/data/bricks-dither.gif", "test/data/bricks-dither.palette", + "test/data/bricks-dither.indexes", 0, + WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL); } const char* test_wuffs_gif_decode_input_is_a_gif_many_big_reads() { CHECK_FOCUS(__func__); - return do_test_wuffs_gif_decode("test/data/bricks-dither.gif", - "test/data/bricks-dither.palette", - "test/data/bricks-dither.indexes", 4096); + return do_test_wuffs_gif_decode( + "test/data/bricks-dither.gif", "test/data/bricks-dither.palette", + "test/data/bricks-dither.indexes", 4096, + WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL); } const char* test_wuffs_gif_decode_input_is_a_gif_many_medium_reads() { CHECK_FOCUS(__func__); - return do_test_wuffs_gif_decode("test/data/bricks-dither.gif", - "test/data/bricks-dither.palette", - "test/data/bricks-dither.indexes", 787); + return do_test_wuffs_gif_decode( + "test/data/bricks-dither.gif", "test/data/bricks-dither.palette", + "test/data/bricks-dither.indexes", 787, + WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL); // The magic 787 tickles being in the middle of a decode_extension skip // call. // @@ -728,9 +768,10 @@ const char* test_wuffs_gif_decode_input_is_a_gif_many_medium_reads() { const char* test_wuffs_gif_decode_input_is_a_gif_many_small_reads() { CHECK_FOCUS(__func__); - return do_test_wuffs_gif_decode("test/data/bricks-dither.gif", - "test/data/bricks-dither.palette", - "test/data/bricks-dither.indexes", 13); + return do_test_wuffs_gif_decode( + "test/data/bricks-dither.gif", "test/data/bricks-dither.palette", + "test/data/bricks-dither.indexes", 13, + WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL); } const char* test_wuffs_gif_decode_input_is_a_png() { @@ -1384,6 +1425,7 @@ proc tests[] = { test_wuffs_gif_decode_animated_big, // test_wuffs_gif_decode_animated_medium, // test_wuffs_gif_decode_animated_small, // + test_wuffs_gif_decode_bgra_nonpremul, // test_wuffs_gif_decode_frame_out_of_bounds, // test_wuffs_gif_decode_input_is_a_gif, // test_wuffs_gif_decode_input_is_a_gif_many_big_reads, // diff --git a/test/c/testlib/testlib.c b/test/c/testlib/testlib.c index 38ba8ad1..db7c0fc6 100644 --- a/test/c/testlib/testlib.c +++ b/test/c/testlib/testlib.c @@ -231,7 +231,8 @@ const char* chdir_to_the_wuffs_root_directory() { return "could not chdir(\"..\")"; } } - return "could not find Wuffs root directory; chdir there before running this program"; + return "could not find Wuffs root directory; chdir there before running this " + "program"; } typedef const char* (*proc)(); @@ -393,17 +394,46 @@ void set_writer_limit(wuffs_base__io_writer* o, uint64_t limit) { const char* copy_to_io_buffer_from_pixel_buffer(wuffs_base__io_buffer* dst, wuffs_base__pixel_buffer* src, wuffs_base__rect_ie_u32 r) { - // TODO: don't assume 1 plane or WUFFS_BASE__PIXEL_SUBSAMPLING__NONE. + if (!src) { + return "copy_to_io_buffer_from_pixel_buffer: NULL src"; + } + + // TODO: don't assume 1 plane or WUFFS_BASE__PIXEL_SUBSAMPLING__NONE. Also, + // support more pixel formats. + size_t bytes_per_pixel = 0; + wuffs_base__pixel_format pixfmt = + wuffs_base__pixel_config__pixel_format(&src->pixcfg); + switch (pixfmt) { + case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL: + bytes_per_pixel = 1; + break; + case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL: + case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: + bytes_per_pixel = 4; + break; + default: + RETURN_FAIL( + "copy_to_io_buffer_from_pixel_buffer: unsupported src pixfmt: 0x%08x", + (int)(pixfmt)); + } + uint32_t p; for (p = 0; p < 1; p++) { wuffs_base__table_u8 tab = wuffs_base__pixel_buffer__plane(src, p); uint32_t y; for (y = r.min_incl_y; y < r.max_excl_y; y++) { wuffs_base__slice_u8 row = wuffs_base__table_u8__row(tab, y); - if ((r.min_incl_x >= r.max_excl_x) || (r.max_excl_x > row.len)) { + if ((r.min_incl_x >= r.max_excl_x) || + (r.max_excl_x > (row.len / bytes_per_pixel))) { break; } - uint32_t n = r.max_excl_x - r.min_incl_x; + + size_t n = r.max_excl_x - r.min_incl_x; + if (n > (SIZE_MAX / bytes_per_pixel)) { + return "copy_to_io_buffer_from_pixel_buffer: n is too large"; + } + n *= bytes_per_pixel; + if (n > (dst->data.len - dst->meta.wi)) { return "copy_to_io_buffer_from_pixel_buffer: dst buffer is too small"; }