From 8482161c918991a846909a4371a7b403330c809c Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 2 Dec 2020 22:13:35 +1100 Subject: [PATCH] Add base.pixel_buffer.palette_or_else --- internal/cgen/base/image-public.h | 21 ++++++++++++ internal/cgen/data/data.go | 19 +++++------ lang/builtin/builtin.go | 1 + release/c/wuffs-unsupported-snapshot.c | 45 ++++++++++++++------------ std/bmp/decode_bmp.wuffs | 20 +++--------- std/gif/decode_gif.wuffs | 8 +---- 6 files changed, 62 insertions(+), 52 deletions(-) diff --git a/internal/cgen/base/image-public.h b/internal/cgen/base/image-public.h index b28ae84e..f945bee8 100644 --- a/internal/cgen/base/image-public.h +++ b/internal/cgen/base/image-public.h @@ -1072,6 +1072,7 @@ typedef struct wuffs_base__pixel_buffer__struct { const wuffs_base__pixel_config* pixcfg, wuffs_base__table_u8 pixbuf_memory); inline wuffs_base__slice_u8 palette(); + inline wuffs_base__slice_u8 palette_or_else(wuffs_base__slice_u8 fallback); inline wuffs_base__pixel_format pixel_format() const; inline wuffs_base__table_u8 plane(uint32_t p); inline wuffs_base__color_u32_argb_premul color_u32_at(uint32_t x, @@ -1211,6 +1212,21 @@ wuffs_base__pixel_buffer__palette(wuffs_base__pixel_buffer* pb) { return wuffs_base__make_slice_u8(NULL, 0); } +static inline wuffs_base__slice_u8 // +wuffs_base__pixel_buffer__palette_or_else(wuffs_base__pixel_buffer* pb, + wuffs_base__slice_u8 fallback) { + if (pb && + wuffs_base__pixel_format__is_indexed(&pb->pixcfg.private_impl.pixfmt)) { + wuffs_base__table_u8* tab = + &pb->private_impl + .planes[WUFFS_BASE__PIXEL_FORMAT__INDEXED__COLOR_PLANE]; + if ((tab->width == 1024) && (tab->height == 1)) { + return wuffs_base__make_slice_u8(tab->ptr, 1024); + } + } + return fallback; +} + static inline wuffs_base__pixel_format // wuffs_base__pixel_buffer__pixel_format(const wuffs_base__pixel_buffer* pb) { if (pb) { @@ -1268,6 +1284,11 @@ wuffs_base__pixel_buffer::palette() { return wuffs_base__pixel_buffer__palette(this); } +inline wuffs_base__slice_u8 // +wuffs_base__pixel_buffer::palette_or_else(wuffs_base__slice_u8 fallback) { + return wuffs_base__pixel_buffer__palette_or_else(this, fallback); +} + inline wuffs_base__pixel_format // wuffs_base__pixel_buffer::pixel_format() const { return wuffs_base__pixel_buffer__pixel_format(this); diff --git a/internal/cgen/data/data.go b/internal/cgen/data/data.go index 126d54d6..8279ce5a 100644 --- a/internal/cgen/data/data.go +++ b/internal/cgen/data/data.go @@ -161,15 +161,16 @@ const BaseImagePublicH = "" + "(\n wuffs_base__rect_ie_u32 bounds,\n wuffs_base__flicks duration,\n uint64_t index,\n uint64_t io_position,\n wuffs_base__animation_disposal disposal,\n bool opaque_within_bounds,\n bool overwrite_instead_of_blend,\n wuffs_base__color_u32_argb_premul background_color) {\n wuffs_base__frame_config__set(this, bounds, duration, index, io_position,\n disposal, opaque_within_bounds,\n overwrite_instead_of_blend, background_color);\n}\n\ninline wuffs_base__rect_ie_u32 //\nwuffs_base__frame_config::bounds() const {\n return wuffs_base__frame_config__bounds(this);\n}\n\ninline uint32_t //\nwuffs_base__frame_config::width() const {\n return wuffs_base__frame_config__width(this);\n}\n\ninline uint32_t //\nwuffs_base__frame_config::height() const {\n return wuffs_base__frame_config__height(this);\n}\n\ninline wuffs_base__flicks //\nwuffs_base__frame_config::duration() const {\n return wuffs_base__frame_config__duration(this);\n}\n\ninline uint64_t //\nwuffs_b" + "ase__frame_config::index() const {\n return wuffs_base__frame_config__index(this);\n}\n\ninline uint64_t //\nwuffs_base__frame_config::io_position() const {\n return wuffs_base__frame_config__io_position(this);\n}\n\ninline wuffs_base__animation_disposal //\nwuffs_base__frame_config::disposal() const {\n return wuffs_base__frame_config__disposal(this);\n}\n\ninline bool //\nwuffs_base__frame_config::opaque_within_bounds() const {\n return wuffs_base__frame_config__opaque_within_bounds(this);\n}\n\ninline bool //\nwuffs_base__frame_config::overwrite_instead_of_blend() const {\n return wuffs_base__frame_config__overwrite_instead_of_blend(this);\n}\n\ninline wuffs_base__color_u32_argb_premul //\nwuffs_base__frame_config::background_color() const {\n return wuffs_base__frame_config__background_color(this);\n}\n\n#endif // __cplusplus\n\n" + "" + - "// --------\n\ntypedef struct wuffs_base__pixel_buffer__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(\n const wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory);\n inline wuffs_base__status set_from_table(\n const wuffs_base__pixel_config* pixcfg,\n wuffs_base__table_u8 pixbuf_memory);\n inline wuffs_base__slice_u8 palette();\n inline wuffs_base__pixel_format pixel_format() const;\n inline wuffs_base__table_u8 plane(uint32_t p);\n inline wuffs_base__color_u32_argb_premul color_u32_at(uint32_t x,\n uint32_t y) const;\n inline wuffs_base__status set_color_u32_at(\n uint32_t x,\n uint32_t y,\n wuf" + - "fs_base__color_u32_argb_premul color);\n#endif // __cplusplus\n\n} wuffs_base__pixel_buffer;\n\nstatic inline wuffs_base__pixel_buffer //\nwuffs_base__null_pixel_buffer() {\n wuffs_base__pixel_buffer ret;\n ret.pixcfg = wuffs_base__null_pixel_config();\n ret.private_impl.planes[0] = wuffs_base__empty_table_u8();\n ret.private_impl.planes[1] = wuffs_base__empty_table_u8();\n ret.private_impl.planes[2] = wuffs_base__empty_table_u8();\n ret.private_impl.planes[3] = wuffs_base__empty_table_u8();\n return ret;\n}\n\nstatic inline wuffs_base__status //\nwuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* pb,\n const wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n if (!pb) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n memset(pb, 0, sizeof(*pb));\n if (!pixcfg) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n if (wuffs_base__pixel_format__is_plana" + - "r(&pixcfg->private_impl.pixfmt)) {\n // TODO: support planar pixel formats, concious of pixel subsampling.\n return wuffs_base__make_status(wuffs_base__error__unsupported_option);\n }\n uint32_t bits_per_pixel =\n wuffs_base__pixel_format__bits_per_pixel(&pixcfg->private_impl.pixfmt);\n if ((bits_per_pixel == 0) || ((bits_per_pixel % 8) != 0)) {\n // TODO: support fraction-of-byte pixels, e.g. 1 bit per pixel?\n return wuffs_base__make_status(wuffs_base__error__unsupported_option);\n }\n uint64_t bytes_per_pixel = bits_per_pixel / 8;\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_" + - "_make_status(\n wuffs_base__error__bad_argument_length_too_short);\n }\n wuffs_base__table_u8* tab =\n &pb->private_impl\n .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 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__make_status(wuffs_base__error__bad_argument);\n }\n wh *= bytes_per_pixel;\n width *= bytes_per_pixel;\n if (wh > len) {\n return wuffs_base__make_status(\n wuffs_base__error__bad_argument_length_too_short);\n }\n\n pb->pixcfg = *pixcfg;\n wuffs_base__table_u8* tab = &pb->private_impl.planes[0];\n tab->ptr = ptr;\n tab->width = width;\n tab->height = pixcfg->private_impl.height;\n tab->strid" + - "e = width;\n return wuffs_base__make_status(NULL);\n}\n\nstatic inline wuffs_base__status //\nwuffs_base__pixel_buffer__set_from_table(wuffs_base__pixel_buffer* pb,\n const wuffs_base__pixel_config* pixcfg,\n wuffs_base__table_u8 pixbuf_memory) {\n if (!pb) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n memset(pb, 0, sizeof(*pb));\n if (!pixcfg ||\n wuffs_base__pixel_format__is_planar(&pixcfg->private_impl.pixfmt)) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n uint32_t bits_per_pixel =\n wuffs_base__pixel_format__bits_per_pixel(&pixcfg->private_impl.pixfmt);\n if ((bits_per_pixel == 0) || ((bits_per_pixel % 8) != 0)) {\n // TODO: support fraction-of-byte pixels, e.g. 1 bit per pixel?\n return wuffs_base__make_status(wuffs_base__error__unsupported_option);\n }\n uint64_t bytes_per_pixel = bits_per_pixel / 8;\n\n uint64_t width_in_bytes =\n ((uint64_t)pixcfg->pri" + - "vate_impl.width) * bytes_per_pixel;\n if ((width_in_bytes > pixbuf_memory.width) ||\n (pixcfg->private_impl.height > pixbuf_memory.height)) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n\n pb->pixcfg = *pixcfg;\n pb->private_impl.planes[0] = pixbuf_memory;\n return wuffs_base__make_status(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* pb) {\n if (pb &&\n wuffs_base__pixel_format__is_indexed(&pb->pixcfg.private_impl.pixfmt)) {\n wuffs_base__table_u8* tab =\n &pb->private_impl\n .planes[WUFFS_BASE__PIXEL_FORMAT__INDEXED__COLOR_PLANE];\n if ((tab->width == 1024) && (tab->height == 1)) {\n return wuffs_base__make_slice_u8(tab->ptr, 1024);\n }\n }\n return wuffs_base__make_slice_u8(NULL, 0);\n}\n\nstatic inline wuffs_base__pixel_format //\nwuffs_base__pixel_buffer__pixel_format(const" + - " wuffs_base__pixel_buffer* pb) {\n if (pb) {\n return pb->pixcfg.private_impl.pixfmt;\n }\n return wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__INVALID);\n}\n\nstatic inline wuffs_base__table_u8 //\nwuffs_base__pixel_buffer__plane(wuffs_base__pixel_buffer* pb, uint32_t p) {\n if (pb && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX)) {\n return pb->private_impl.planes[p];\n }\n\n wuffs_base__table_u8 ret;\n ret.ptr = NULL;\n ret.width = 0;\n ret.height = 0;\n ret.stride = 0;\n return ret;\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__color_u32_argb_premul //\nwuffs_base__pixel_buffer__color_u32_at(const wuffs_base__pixel_buffer* pb,\n uint32_t x,\n uint32_t y);\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__status //\nwuffs_base__pixel_buffer__set_color_u32_at(\n wuffs_base__pixel_buffer* pb,\n uint32_t x,\n uint32_t y,\n wuffs_base__color_u32_argb_premul color);\n\n#ifdef __cplusplus\n\ninline wuffs_base__status //\nwuffs_base__pixel_bu" + - "ffer::set_from_slice(\n const wuffs_base__pixel_config* pixcfg_arg,\n wuffs_base__slice_u8 pixbuf_memory) {\n return wuffs_base__pixel_buffer__set_from_slice(this, pixcfg_arg,\n pixbuf_memory);\n}\n\ninline wuffs_base__status //\nwuffs_base__pixel_buffer::set_from_table(\n const wuffs_base__pixel_config* pixcfg_arg,\n wuffs_base__table_u8 pixbuf_memory) {\n return wuffs_base__pixel_buffer__set_from_table(this, pixcfg_arg,\n 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() const {\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\ninline wuffs_base__color_u32_argb_premul //\nwuffs_base__pixel_buffer::color_u" + - "32_at(uint32_t x, uint32_t y) const {\n return wuffs_base__pixel_buffer__color_u32_at(this, x, y);\n}\n\ninline wuffs_base__status //\nwuffs_base__pixel_buffer::set_color_u32_at(\n uint32_t x,\n uint32_t y,\n wuffs_base__color_u32_argb_premul color) {\n return wuffs_base__pixel_buffer__set_color_u32_at(this, x, y, color);\n}\n\n#endif // __cplusplus\n\n" + + "// --------\n\ntypedef struct wuffs_base__pixel_buffer__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(\n const wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory);\n inline wuffs_base__status set_from_table(\n const wuffs_base__pixel_config* pixcfg,\n wuffs_base__table_u8 pixbuf_memory);\n inline wuffs_base__slice_u8 palette();\n inline wuffs_base__slice_u8 palette_or_else(wuffs_base__slice_u8 fallback);\n inline wuffs_base__pixel_format pixel_format() const;\n inline wuffs_base__table_u8 plane(uint32_t p);\n inline wuffs_base__color_u32_argb_premul color_u32_at(uint32_t x,\n uint32_t y) const;\n inline wuff" + + "s_base__status set_color_u32_at(\n uint32_t x,\n uint32_t y,\n wuffs_base__color_u32_argb_premul color);\n#endif // __cplusplus\n\n} wuffs_base__pixel_buffer;\n\nstatic inline wuffs_base__pixel_buffer //\nwuffs_base__null_pixel_buffer() {\n wuffs_base__pixel_buffer ret;\n ret.pixcfg = wuffs_base__null_pixel_config();\n ret.private_impl.planes[0] = wuffs_base__empty_table_u8();\n ret.private_impl.planes[1] = wuffs_base__empty_table_u8();\n ret.private_impl.planes[2] = wuffs_base__empty_table_u8();\n ret.private_impl.planes[3] = wuffs_base__empty_table_u8();\n return ret;\n}\n\nstatic inline wuffs_base__status //\nwuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* pb,\n const wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n if (!pb) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n memset(pb, 0, sizeof(*pb));\n if (!pixcfg) {\n return wuffs_base__make_status(" + + "wuffs_base__error__bad_argument);\n }\n if (wuffs_base__pixel_format__is_planar(&pixcfg->private_impl.pixfmt)) {\n // TODO: support planar pixel formats, concious of pixel subsampling.\n return wuffs_base__make_status(wuffs_base__error__unsupported_option);\n }\n uint32_t bits_per_pixel =\n wuffs_base__pixel_format__bits_per_pixel(&pixcfg->private_impl.pixfmt);\n if ((bits_per_pixel == 0) || ((bits_per_pixel % 8) != 0)) {\n // TODO: support fraction-of-byte pixels, e.g. 1 bit per pixel?\n return wuffs_base__make_status(wuffs_base__error__unsupported_option);\n }\n uint64_t bytes_per_pixel = bits_per_pixel / 8;\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 // point" + + "er, up to an alignment of 1024.\n if (len < 1024) {\n return wuffs_base__make_status(\n wuffs_base__error__bad_argument_length_too_short);\n }\n wuffs_base__table_u8* tab =\n &pb->private_impl\n .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 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__make_status(wuffs_base__error__bad_argument);\n }\n wh *= bytes_per_pixel;\n width *= bytes_per_pixel;\n if (wh > len) {\n return wuffs_base__make_status(\n wuffs_base__error__bad_argument_length_too_short);\n }\n\n pb->pixcfg = *pixcfg;\n wuffs_base__table_u8* tab = &pb->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 wuffs_base__make_status(NULL);\n}\n\nstatic inline wuffs_base__status //\nwuffs_base__pixel_buffer__set_from_table(wuffs_base__pixel_buffer* pb,\n const wuffs_base__pixel_config* pixcfg,\n wuffs_base__table_u8 pixbuf_memory) {\n if (!pb) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n memset(pb, 0, sizeof(*pb));\n if (!pixcfg ||\n wuffs_base__pixel_format__is_planar(&pixcfg->private_impl.pixfmt)) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n uint32_t bits_per_pixel =\n wuffs_base__pixel_format__bits_per_pixel(&pixcfg->private_impl.pixfmt);\n if ((bits_per_pixel == 0) || ((bits_per_pixel % 8) != 0)) {\n // TODO: support fraction-of-byte pixels, e.g. 1 bit per pixel?\n return wuffs_base__make_status(wuffs_base__error__unsupported_option);\n }\n uint64_t bytes_per_pixel =" + + " bits_per_pixel / 8;\n\n uint64_t width_in_bytes =\n ((uint64_t)pixcfg->private_impl.width) * bytes_per_pixel;\n if ((width_in_bytes > pixbuf_memory.width) ||\n (pixcfg->private_impl.height > pixbuf_memory.height)) {\n return wuffs_base__make_status(wuffs_base__error__bad_argument);\n }\n\n pb->pixcfg = *pixcfg;\n pb->private_impl.planes[0] = pixbuf_memory;\n return wuffs_base__make_status(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* pb) {\n if (pb &&\n wuffs_base__pixel_format__is_indexed(&pb->pixcfg.private_impl.pixfmt)) {\n wuffs_base__table_u8* tab =\n &pb->private_impl\n .planes[WUFFS_BASE__PIXEL_FORMAT__INDEXED__COLOR_PLANE];\n if ((tab->width == 1024) && (tab->height == 1)) {\n return wuffs_base__make_slice_u8(tab->ptr, 1024);\n }\n }\n return wuffs_base__make_slice_u8(NULL, 0);\n}\n\nstatic in" + + "line wuffs_base__slice_u8 //\nwuffs_base__pixel_buffer__palette_or_else(wuffs_base__pixel_buffer* pb,\n wuffs_base__slice_u8 fallback) {\n if (pb &&\n wuffs_base__pixel_format__is_indexed(&pb->pixcfg.private_impl.pixfmt)) {\n wuffs_base__table_u8* tab =\n &pb->private_impl\n .planes[WUFFS_BASE__PIXEL_FORMAT__INDEXED__COLOR_PLANE];\n if ((tab->width == 1024) && (tab->height == 1)) {\n return wuffs_base__make_slice_u8(tab->ptr, 1024);\n }\n }\n return fallback;\n}\n\nstatic inline wuffs_base__pixel_format //\nwuffs_base__pixel_buffer__pixel_format(const wuffs_base__pixel_buffer* pb) {\n if (pb) {\n return pb->pixcfg.private_impl.pixfmt;\n }\n return wuffs_base__make_pixel_format(WUFFS_BASE__PIXEL_FORMAT__INVALID);\n}\n\nstatic inline wuffs_base__table_u8 //\nwuffs_base__pixel_buffer__plane(wuffs_base__pixel_buffer* pb, uint32_t p) {\n if (pb && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX)) {\n return pb->private_impl.planes[p];\n }\n\n wuffs_" + + "base__table_u8 ret;\n ret.ptr = NULL;\n ret.width = 0;\n ret.height = 0;\n ret.stride = 0;\n return ret;\n}\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__color_u32_argb_premul //\nwuffs_base__pixel_buffer__color_u32_at(const wuffs_base__pixel_buffer* pb,\n uint32_t x,\n uint32_t y);\n\nWUFFS_BASE__MAYBE_STATIC wuffs_base__status //\nwuffs_base__pixel_buffer__set_color_u32_at(\n wuffs_base__pixel_buffer* pb,\n uint32_t x,\n uint32_t y,\n wuffs_base__color_u32_argb_premul color);\n\n#ifdef __cplusplus\n\ninline wuffs_base__status //\nwuffs_base__pixel_buffer::set_from_slice(\n const wuffs_base__pixel_config* pixcfg_arg,\n wuffs_base__slice_u8 pixbuf_memory) {\n return wuffs_base__pixel_buffer__set_from_slice(this, pixcfg_arg,\n pixbuf_memory);\n}\n\ninline wuffs_base__status //\nwuffs_base__pixel_buffer::set_from_table(\n const wuffs_base__pixel_config* pixcfg_arg,\n wuffs_base__table_u8 pixbu" + + "f_memory) {\n return wuffs_base__pixel_buffer__set_from_table(this, pixcfg_arg,\n 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__slice_u8 //\nwuffs_base__pixel_buffer::palette_or_else(wuffs_base__slice_u8 fallback) {\n return wuffs_base__pixel_buffer__palette_or_else(this, fallback);\n}\n\ninline wuffs_base__pixel_format //\nwuffs_base__pixel_buffer::pixel_format() const {\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\ninline wuffs_base__color_u32_argb_premul //\nwuffs_base__pixel_buffer::color_u32_at(uint32_t x, uint32_t y) const {\n return wuffs_base__pixel_buffer__color_u32_at(this, x, y);\n}\n\ninline wuffs_base__status //\nwuffs_base__pixel_buffer::set_color_u32_at(\n uint32_t x,\n uint32_t y,\n wuffs_bas" + + "e__color_u32_argb_premul color) {\n return wuffs_base__pixel_buffer__set_color_u32_at(this, x, y, color);\n}\n\n#endif // __cplusplus\n\n" + "" + "// --------\n\ntypedef struct wuffs_base__decode_frame_options__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/lang/builtin/builtin.go b/lang/builtin/builtin.go index b4145776..c63f0b32 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -501,6 +501,7 @@ var Funcs = []string{ // ---- pixel_buffer "pixel_buffer.palette() slice u8", + "pixel_buffer.palette_or_else(fallback: slice u8) slice u8", "pixel_buffer.pixel_format() pixel_format", "pixel_buffer.plane(p: u32[..= 3]) table u8", diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index de884966..2482506b 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -3829,6 +3829,7 @@ typedef struct wuffs_base__pixel_buffer__struct { const wuffs_base__pixel_config* pixcfg, wuffs_base__table_u8 pixbuf_memory); inline wuffs_base__slice_u8 palette(); + inline wuffs_base__slice_u8 palette_or_else(wuffs_base__slice_u8 fallback); inline wuffs_base__pixel_format pixel_format() const; inline wuffs_base__table_u8 plane(uint32_t p); inline wuffs_base__color_u32_argb_premul color_u32_at(uint32_t x, @@ -3968,6 +3969,21 @@ wuffs_base__pixel_buffer__palette(wuffs_base__pixel_buffer* pb) { return wuffs_base__make_slice_u8(NULL, 0); } +static inline wuffs_base__slice_u8 // +wuffs_base__pixel_buffer__palette_or_else(wuffs_base__pixel_buffer* pb, + wuffs_base__slice_u8 fallback) { + if (pb && + wuffs_base__pixel_format__is_indexed(&pb->pixcfg.private_impl.pixfmt)) { + wuffs_base__table_u8* tab = + &pb->private_impl + .planes[WUFFS_BASE__PIXEL_FORMAT__INDEXED__COLOR_PLANE]; + if ((tab->width == 1024) && (tab->height == 1)) { + return wuffs_base__make_slice_u8(tab->ptr, 1024); + } + } + return fallback; +} + static inline wuffs_base__pixel_format // wuffs_base__pixel_buffer__pixel_format(const wuffs_base__pixel_buffer* pb) { if (pb) { @@ -4025,6 +4041,11 @@ wuffs_base__pixel_buffer::palette() { return wuffs_base__pixel_buffer__palette(this); } +inline wuffs_base__slice_u8 // +wuffs_base__pixel_buffer::palette_or_else(wuffs_base__slice_u8 fallback) { + return wuffs_base__pixel_buffer__palette_or_else(this, fallback); +} + inline wuffs_base__pixel_format // wuffs_base__pixel_buffer::pixel_format() const { return wuffs_base__pixel_buffer__pixel_format(this); @@ -16901,7 +16922,6 @@ wuffs_bmp__decoder__decode_frame( self->private_impl.active_coroutine = 0; wuffs_base__status status = wuffs_base__make_status(NULL); - wuffs_base__slice_u8 v_dst_palette = {0}; wuffs_base__status v_status = wuffs_base__make_status(NULL); const uint8_t* iop_a_src = NULL; @@ -16956,13 +16976,9 @@ wuffs_bmp__decoder__decode_frame( self->private_impl.f_dst_y_end = 4294967295; self->private_impl.f_dst_y_inc = 4294967295; } - v_dst_palette = wuffs_base__pixel_buffer__palette(a_dst); - if (((uint64_t)(v_dst_palette.len)) == 0) { - v_dst_palette = wuffs_base__slice_u8__subslice_i(wuffs_base__make_slice_u8(self->private_data.f_scratch, 2048), 1024); - } v_status = wuffs_base__pixel_swizzler__prepare(&self->private_impl.f_swizzler, wuffs_base__pixel_buffer__pixel_format(a_dst), - v_dst_palette, + wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__slice_u8__subslice_i(wuffs_base__make_slice_u8(self->private_data.f_scratch, 2048), 1024)), wuffs_base__utility__make_pixel_format(self->private_impl.f_src_pixfmt), wuffs_base__make_slice_u8(self->private_data.f_src_palette, 1024), a_blend); @@ -17074,10 +17090,7 @@ wuffs_bmp__decoder__swizzle_compress0( } v_dst_bytes_per_pixel = ((uint64_t)((v_dst_bits_per_pixel / 8))); v_dst_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * v_dst_bytes_per_pixel); - v_dst_palette = wuffs_base__pixel_buffer__palette(a_dst); - if (((uint64_t)(v_dst_palette.len)) == 0) { - v_dst_palette = wuffs_base__slice_u8__subslice_i(wuffs_base__make_slice_u8(self->private_data.f_scratch, 2048), 1024); - } + v_dst_palette = wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__slice_u8__subslice_i(wuffs_base__make_slice_u8(self->private_data.f_scratch, 2048), 1024)); v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0); label__outer__continue:; while (true) { @@ -17182,10 +17195,7 @@ wuffs_bmp__decoder__swizzle_compress3( } v_dst_bytes_per_pixel = ((uint64_t)((v_dst_bits_per_pixel / 8))); v_dst_bytes_per_row = (((uint64_t)(self->private_impl.f_width)) * v_dst_bytes_per_pixel); - v_dst_palette = wuffs_base__pixel_buffer__palette(a_dst); - if (((uint64_t)(v_dst_palette.len)) == 0) { - v_dst_palette = wuffs_base__slice_u8__subslice_i(wuffs_base__make_slice_u8(self->private_data.f_scratch, 2048), 1024); - } + v_dst_palette = wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__slice_u8__subslice_i(wuffs_base__make_slice_u8(self->private_data.f_scratch, 2048), 1024)); v_tab = wuffs_base__pixel_buffer__plane(a_dst, 0); label__outer__continue:; while (true) { @@ -25526,7 +25536,6 @@ wuffs_gif__decoder__decode_id_part1( uint32_t v_num_palette_entries = 0; uint32_t v_i = 0; uint32_t v_argb = 0; - wuffs_base__slice_u8 v_dst_palette = {0}; wuffs_base__status v_status = wuffs_base__make_status(NULL); uint8_t v_lw = 0; @@ -25626,13 +25635,9 @@ wuffs_gif__decoder__decode_id_part1( self->private_data.f_palettes[1][((4 * ((uint32_t)(self->private_impl.f_gc_transparent_index))) + 2)] = 0; self->private_data.f_palettes[1][((4 * ((uint32_t)(self->private_impl.f_gc_transparent_index))) + 3)] = 0; } - v_dst_palette = wuffs_base__pixel_buffer__palette(a_dst); - if (((uint64_t)(v_dst_palette.len)) == 0) { - v_dst_palette = wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024); - } v_status = wuffs_base__pixel_swizzler__prepare(&self->private_impl.f_swizzler, wuffs_base__pixel_buffer__pixel_format(a_dst), - v_dst_palette, + wuffs_base__pixel_buffer__palette_or_else(a_dst, wuffs_base__make_slice_u8(self->private_data.f_dst_palette, 1024)), wuffs_base__utility__make_pixel_format(2198077448), wuffs_base__make_slice_u8(self->private_data.f_palettes[v_which_palette], 1024), a_blend); diff --git a/std/bmp/decode_bmp.wuffs b/std/bmp/decode_bmp.wuffs index 8a671fb5..5c374839 100644 --- a/std/bmp/decode_bmp.wuffs +++ b/std/bmp/decode_bmp.wuffs @@ -313,8 +313,7 @@ pub func decoder.decode_frame_config?(dst: nptr base.frame_config, src: base.io_ } 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 dst_palette : slice base.u8 - var status : base.status + var status : base.status if this.call_sequence < 2 { this.decode_frame_config?(dst: nullptr, src: args.src) @@ -338,14 +337,9 @@ pub func decoder.decode_frame?(dst: ptr base.pixel_buffer, src: base.io_reader, this.dst_y_inc = 0xFFFF_FFFF // -1 as a base.u32. } - dst_palette = args.dst.palette() - if dst_palette.length() == 0 { - dst_palette = this.scratch[1024 ..] - } - status = this.swizzler.prepare!( dst_pixfmt: args.dst.pixel_format(), - dst_palette: dst_palette, + dst_palette: args.dst.palette_or_else(fallback: this.scratch[1024 ..]), src_pixfmt: this.util.make_pixel_format(repr: this.src_pixfmt), src_palette: this.src_palette[..], blend: args.blend) @@ -392,10 +386,7 @@ pri func decoder.swizzle_compress0!(dst: ptr base.pixel_buffer, src: base.io_rea } dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64 dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel - dst_palette = args.dst.palette() - if dst_palette.length() == 0 { - dst_palette = this.scratch[1024 ..] - } + dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..]) tab = args.dst.plane(p: 0) while.outer true { @@ -472,10 +463,7 @@ pri func decoder.swizzle_compress3!(dst: ptr base.pixel_buffer, src: base.io_rea } dst_bytes_per_pixel = (dst_bits_per_pixel / 8) as base.u64 dst_bytes_per_row = (this.width as base.u64) * dst_bytes_per_pixel - dst_palette = args.dst.palette() - if dst_palette.length() == 0 { - dst_palette = this.scratch[1024 ..] - } + dst_palette = args.dst.palette_or_else(fallback: this.scratch[1024 ..]) tab = args.dst.plane(p: 0) while.outer true { diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index fc024d6c..f0e5c47c 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -819,7 +819,6 @@ pri func decoder.decode_id_part1?(dst: ptr base.pixel_buffer, src: base.io_reade var num_palette_entries : base.u32[..= 256] var i : base.u32 var argb : base.u32 - var dst_palette : slice base.u8 var status : base.status var lw : base.u8 @@ -871,14 +870,9 @@ pri func decoder.decode_id_part1?(dst: ptr base.pixel_buffer, src: base.io_reade this.palettes[1][(4 * (this.gc_transparent_index as base.u32)) + 3] = 0x00 } - dst_palette = args.dst.palette() - if dst_palette.length() == 0 { - dst_palette = this.dst_palette[..] - } - status = this.swizzler.prepare!( dst_pixfmt: args.dst.pixel_format(), - dst_palette: dst_palette, + dst_palette: args.dst.palette_or_else(fallback: this.dst_palette[..]), src_pixfmt: this.util.make_pixel_format(repr: base.PIXEL_FORMAT__INDEXED__BGRA_BINARY), src_palette: this.palettes[which_palette][..], blend: args.blend)