diff --git a/cmd/wuffs-c/internal/cgen/base/image-impl.c b/cmd/wuffs-c/internal/cgen/base/image-impl.c index c6286c89..da9d8c30 100644 --- a/cmd/wuffs-c/internal/cgen/base/image-impl.c +++ b/cmd/wuffs-c/internal/cgen/base/image-impl.c @@ -16,6 +16,11 @@ // ---------------- Images +const uint32_t wuffs_base__pixel_format__bits_per_channel[16] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x0A, 0x0C, 0x10, 0x18, 0x20, 0x30, 0x40, +}; + static uint64_t // wuffs_base__pixel_swizzler__copy_1_1(wuffs_base__slice_u8 dst, wuffs_base__slice_u8 dst_palette, diff --git a/cmd/wuffs-c/internal/cgen/base/image-public.h b/cmd/wuffs-c/internal/cgen/base/image-public.h index 78f651cf..ffdd8fec 100644 --- a/cmd/wuffs-c/internal/cgen/base/image-public.h +++ b/cmd/wuffs-c/internal/cgen/base/image-public.h @@ -193,19 +193,44 @@ typedef uint32_t wuffs_base__pixel_format; #define WUFFS_BASE__PIXEL_FORMAT__CMY ((wuffs_base__pixel_format)0x60020888) #define WUFFS_BASE__PIXEL_FORMAT__CMYK ((wuffs_base__pixel_format)0x61038888) +extern const uint32_t wuffs_base__pixel_format__bits_per_channel[16]; + static inline bool // wuffs_base__pixel_format__is_valid(wuffs_base__pixel_format f) { return f != 0; } +// wuffs_base__pixel_format__bits_per_pixel returns, for packed pixel formats, +// the number of bits per pixel. It returns 0 for planar pixel formats. +static inline uint32_t // +wuffs_base__pixel_format__bits_per_pixel(wuffs_base__pixel_format f) { + if (((f >> 16) & 0x03) != 0) { + return 0; + } + return wuffs_base__pixel_format__bits_per_channel[0x0F & (f >> 0)] + + wuffs_base__pixel_format__bits_per_channel[0x0F & (f >> 4)] + + wuffs_base__pixel_format__bits_per_channel[0x0F & (f >> 8)] + + wuffs_base__pixel_format__bits_per_channel[0x0F & (f >> 12)]; +} + static inline bool // wuffs_base__pixel_format__is_indexed(wuffs_base__pixel_format f) { return (f >> 18) & 0x01; } +static inline bool // +wuffs_base__pixel_format__is_packed(wuffs_base__pixel_format f) { + return ((f >> 16) & 0x03) == 0; +} + +static inline bool // +wuffs_base__pixel_format__is_planar(wuffs_base__pixel_format f) { + return ((f >> 16) & 0x03) != 0; +} + static inline uint32_t // wuffs_base__pixel_format__num_planes(wuffs_base__pixel_format f) { - return f ? (((f >> 16) & 0x03) + 1) : 0; + return ((f >> 16) & 0x03) + 1; } #define WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX 4 @@ -394,33 +419,23 @@ 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) { - // 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: - case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY: - bytes_per_pixel = 1; - break; - case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL: - case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY: - bytes_per_pixel = 4; - break; - default: - return 0; - } - if (!c) { return 0; } + if (wuffs_base__pixel_format__is_planar(c->private_impl.pixfmt)) { + // TODO: support planar pixel formats, concious of pixel subsampling. + return 0; + } + uint32_t bits_per_pixel = + wuffs_base__pixel_format__bits_per_pixel(c->private_impl.pixfmt); + if ((bits_per_pixel == 0) || ((bits_per_pixel % 8) != 0)) { + // TODO: support fraction-of-byte pixels, e.g. 1 bit per pixel? + return 0; + } + uint64_t bytes_per_pixel = bits_per_pixel / 8; uint64_t n = ((uint64_t)c->private_impl.width) * ((uint64_t)c->private_impl.height); - if (n > (UINT64_MAX / bytes_per_pixel)) { return 0; } @@ -846,6 +861,16 @@ wuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* b, if (!pixcfg) { return wuffs_base__error__bad_argument; } + if (wuffs_base__pixel_format__is_planar(pixcfg->private_impl.pixfmt)) { + // TODO: support planar pixel formats, concious of pixel subsampling. + return wuffs_base__error__bad_argument; + } + uint32_t bits_per_pixel = + wuffs_base__pixel_format__bits_per_pixel(pixcfg->private_impl.pixfmt); + if ((bits_per_pixel == 0) || ((bits_per_pixel % 8) != 0)) { + return wuffs_base__error__bad_argument; + } + uint64_t bytes_per_pixel = bits_per_pixel / 8; uint8_t* ptr = pixbuf_memory.ptr; uint64_t len = pixbuf_memory.len; @@ -867,27 +892,6 @@ wuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* b, len -= 1024; } - // 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: - case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY: - bytes_per_pixel = 1; - break; - case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL: - case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY: - 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); diff --git a/cmd/wuffs-c/internal/cgen/data.go b/cmd/wuffs-c/internal/cgen/data.go index 96887632..1cc5267c 100644 --- a/cmd/wuffs-c/internal/cgen/data.go +++ b/cmd/wuffs-c/internal/cgen/data.go @@ -24,11 +24,11 @@ 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__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 const int N = 4;\n\n while (n >= N) {\n wuffs_base__store_u32le(\n d + (0 * 4),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4)));\n wuffs_base__store_u32le(\n d + (1 * 4),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[1]) * 4)));\n " + - " wuffs_base__store_u32le(\n d + (2 * 4),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[2]) * 4)));\n wuffs_base__store_u32le(\n d + (3 * 4),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[3]) * 4)));\n\n s += 1 * N;\n d += 4 * N;\n n -= 1 * N;\n }\n\n while (n >= 1) {\n wuffs_base__store_u32le(\n d + (0 * 4),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4)));\n\n s += 1 * 1;\n d += 4 * 1;\n n -= 1 * 1;\n }\n\n return len;\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 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_BINARY:\n switch (dst_format) {\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY:\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 case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY:\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 case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY:\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 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\nconst uint32_t wuffs_base__pixel_format__bits_per_channel[16] = {\n 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,\n 0x08, 0x0A, 0x0C, 0x10, 0x18, 0x20, 0x30, 0x40,\n};\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 const int N = 4;\n\n while (n >= N) {\n wuffs_base__store_u32le(\n d + (0 * 4),\n wuffs_base__load_u3" + + "2le(dst_palette.ptr + ((uint32_t)(s[0]) * 4)));\n wuffs_base__store_u32le(\n d + (1 * 4),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[1]) * 4)));\n wuffs_base__store_u32le(\n d + (2 * 4),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[2]) * 4)));\n wuffs_base__store_u32le(\n d + (3 * 4),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[3]) * 4)));\n\n s += 1 * N;\n d += 4 * N;\n n -= 1 * N;\n }\n\n while (n >= 1) {\n wuffs_base__store_u32le(\n d + (0 * 4),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4)));\n\n s += 1 * 1;\n d += 4 * 1;\n n -= 1 * 1;\n }\n\n return len;\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 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_BINARY:\n switch (dst_format) {\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL:\n case WUFFS_BASE__PIXEL_FORM" + + "AT__INDEXED__BGRA_BINARY:\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 case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY:\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 case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY:\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 defaul" + + "t:\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" + "" const baseCorePrivateH = "" + @@ -102,8 +102,8 @@ const baseImagePublicH = "" + " LSB\n// G₂G₁G₀B₄B₃B₂B₁B₀ R₄R₃R₂R₁R₀G₅G₄G₃\n//\n// On little-endian systems (but not big-endian), this Wuffs pixel format value\n// (0x40000565) corresponds to the Cairo library's CAIRO_FORMAT_RGB16_565, the\n// SDL2 (Simple DirectMedia Layer 2) library's SDL_PIXELFORMAT_RGB565 and the\n// Skia library's kRGB_565_SkColorType. Note BGR in Wuffs versus RGB in the\n// other libraries.\n//\n// Regardless of endianness, this Wuffs pixel format value (0x40000565)\n// corresponds to the V4L2 (Video For Linux 2) library's V4L2_PIX_FMT_RGB565\n// and the Wayland-DRM library's WL_DRM_FORMAT_RGB565.\n//\n// Different software libraries name their pixel formats (and especially their\n// channel order) either according to memory layout or as bits of a native\n// integer type like uint32_t. The two conventions differ because of a system's\n// endianness. As mentioned earlier, Wuffs pixel formats are always in memory\n// order. More detail of other software libraries' naming conventions is in the\n// Pi" + "xel Format Guide at https://afrantzis.github.io/pixel-format-guide/\n//\n// Do not manipulate these bits directly; they are private implementation\n// details. Use methods such as wuffs_base__pixel_format__num_planes instead.\ntypedef uint32_t wuffs_base__pixel_format;\n\n// Common 8-bit-depth pixel formats. This list is not exhaustive; not all valid\n// wuffs_base__pixel_format values are present.\n\n#define WUFFS_BASE__PIXEL_FORMAT__INVALID ((wuffs_base__pixel_format)0x00000000)\n\n#define WUFFS_BASE__PIXEL_FORMAT__A ((wuffs_base__pixel_format)0x02000008)\n\n#define WUFFS_BASE__PIXEL_FORMAT__Y ((wuffs_base__pixel_format)0x10000008)\n#define WUFFS_BASE__PIXEL_FORMAT__YA_NONPREMUL \\\n ((wuffs_base__pixel_format)0x15000008)\n#define WUFFS_BASE__PIXEL_FORMAT__YA_PREMUL \\\n ((wuffs_base__pixel_format)0x16000008)\n\n#define WUFFS_BASE__PIXEL_FORMAT__YCBCR ((wuffs_base__pixel_format)0x20020888)\n#define WUFFS_BASE__PIXEL_FORMAT__YCBCRK ((wuffs_base__pixel_format)0x21038888)\n#define WUFFS_BASE__PIXEL_FORMAT__YCBCRA_NONPREMUL \\\n ((w" + "uffs_base__pixel_format)0x25038888)\n\n#define WUFFS_BASE__PIXEL_FORMAT__YCOCG ((wuffs_base__pixel_format)0x30020888)\n#define WUFFS_BASE__PIXEL_FORMAT__YCOCGK ((wuffs_base__pixel_format)0x31038888)\n#define WUFFS_BASE__PIXEL_FORMAT__YCOCGA_NONPREMUL \\\n ((wuffs_base__pixel_format)0x35038888)\n\n#define WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL \\\n ((wuffs_base__pixel_format)0x45040008)\n#define WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL \\\n ((wuffs_base__pixel_format)0x46040008)\n#define WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY \\\n ((wuffs_base__pixel_format)0x47040008)\n\n#define WUFFS_BASE__PIXEL_FORMAT__BGR ((wuffs_base__pixel_format)0x40000888)\n#define WUFFS_BASE__PIXEL_FORMAT__BGRX ((wuffs_base__pixel_format)0x41008888)\n#define WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL \\\n ((wuffs_base__pixel_format)0x45008888)\n#define WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL \\\n ((wuffs_base__pixel_format)0x46008888)\n#define WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY \\\n ((wuffs_base__pixel_format)0x47008888)\n\n#define W" + - "UFFS_BASE__PIXEL_FORMAT__RGB ((wuffs_base__pixel_format)0x50000888)\n#define WUFFS_BASE__PIXEL_FORMAT__RGBX ((wuffs_base__pixel_format)0x51008888)\n#define WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL \\\n ((wuffs_base__pixel_format)0x55008888)\n#define WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL \\\n ((wuffs_base__pixel_format)0x56008888)\n#define WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY \\\n ((wuffs_base__pixel_format)0x57008888)\n\n#define WUFFS_BASE__PIXEL_FORMAT__CMY ((wuffs_base__pixel_format)0x60020888)\n#define WUFFS_BASE__PIXEL_FORMAT__CMYK ((wuffs_base__pixel_format)0x61038888)\n\nstatic inline bool //\nwuffs_base__pixel_format__is_valid(wuffs_base__pixel_format f) {\n return f != 0;\n}\n\nstatic inline bool //\nwuffs_base__pixel_format__is_indexed(wuffs_base__pixel_format f) {\n return (f >> 18) & 0x01;\n}\n\nstatic inline uint32_t //\nwuffs_base__pixel_format__num_planes(wuffs_base__pixel_format f) {\n return f ? (((f >> 16) & 0x03) + 1) : 0;\n}\n\n#define WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX 4\n\n#define WUFFS_BASE__PIXEL_FORMA" + - "T__INDEXED__INDEX_PLANE 0\n#define WUFFS_BASE__PIXEL_FORMAT__INDEXED__COLOR_PLANE 3\n\n" + + "UFFS_BASE__PIXEL_FORMAT__RGB ((wuffs_base__pixel_format)0x50000888)\n#define WUFFS_BASE__PIXEL_FORMAT__RGBX ((wuffs_base__pixel_format)0x51008888)\n#define WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL \\\n ((wuffs_base__pixel_format)0x55008888)\n#define WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL \\\n ((wuffs_base__pixel_format)0x56008888)\n#define WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY \\\n ((wuffs_base__pixel_format)0x57008888)\n\n#define WUFFS_BASE__PIXEL_FORMAT__CMY ((wuffs_base__pixel_format)0x60020888)\n#define WUFFS_BASE__PIXEL_FORMAT__CMYK ((wuffs_base__pixel_format)0x61038888)\n\nextern const uint32_t wuffs_base__pixel_format__bits_per_channel[16];\n\nstatic inline bool //\nwuffs_base__pixel_format__is_valid(wuffs_base__pixel_format f) {\n return f != 0;\n}\n\n// wuffs_base__pixel_format__bits_per_pixel returns, for packed pixel formats,\n// the number of bits per pixel. It returns 0 for planar pixel formats.\nstatic inline uint32_t //\nwuffs_base__pixel_format__bits_per_pixel(wuffs_base__pixel_format f) {\n if (((f >> 16) & 0x03" + + ") != 0) {\n return 0;\n }\n return wuffs_base__pixel_format__bits_per_channel[0x0F & (f >> 0)] +\n wuffs_base__pixel_format__bits_per_channel[0x0F & (f >> 4)] +\n wuffs_base__pixel_format__bits_per_channel[0x0F & (f >> 8)] +\n wuffs_base__pixel_format__bits_per_channel[0x0F & (f >> 12)];\n}\n\nstatic inline bool //\nwuffs_base__pixel_format__is_indexed(wuffs_base__pixel_format f) {\n return (f >> 18) & 0x01;\n}\n\nstatic inline bool //\nwuffs_base__pixel_format__is_packed(wuffs_base__pixel_format f) {\n return ((f >> 16) & 0x03) == 0;\n}\n\nstatic inline bool //\nwuffs_base__pixel_format__is_planar(wuffs_base__pixel_format f) {\n return ((f >> 16) & 0x03) != 0;\n}\n\nstatic inline uint32_t //\nwuffs_base__pixel_format__num_planes(wuffs_base__pixel_format f) {\n return ((f >> 16) & 0x03) + 1;\n}\n\n#define WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX 4\n\n#define WUFFS_BASE__PIXEL_FORMAT__INDEXED__INDEX_PLANE 0\n#define WUFFS_BASE__PIXEL_FORMAT__INDEXED__COLOR_PLANE 3\n\n" + "" + "// --------\n\n// wuffs_base__pixel_subsampling encodes the mapping of pixel space coordinates\n// (x, y) to pixel buffer indices (i, j). That mapping can differ for each\n// plane p. For a depth of 8 bits (1 byte), the p'th plane's sample starts at\n// (planes[p].ptr + (j * planes[p].stride) + i).\n//\n// For packed pixel formats, the mapping is trivial: i = x and j = y. For\n// planar pixel formats, the mapping can differ due to chroma subsampling. For\n// example, consider a three plane YCbCr pixel format with 4:2:2 subsampling.\n// For the luma (Y) channel, there is one sample for every pixel, but for the\n// chroma (Cb, Cr) channels, there is one sample for every two pixels: pairs of\n// horizontally adjacent pixels form one macropixel, i = x / 2 and j == y. In\n// general, for a given p:\n// - i = (x + bias_x) >> shift_x.\n// - j = (y + bias_y) >> shift_y.\n// where biases and shifts are in the range 0..3 and 0..2 respectively.\n//\n// In general, the biases will be zero after decoding an image. However, making\n// a su" + "b-image may change the bias, since the (x, y) coordinates are relative\n// to the sub-image's top-left origin, but the backing pixel buffers were\n// created relative to the original image's origin.\n//\n// For each plane p, each of those four numbers (biases and shifts) are encoded\n// in two bits, which combine to form an 8 bit unsigned integer:\n//\n// e_p = (bias_x << 6) | (shift_x << 4) | (bias_y << 2) | (shift_y << 0)\n//\n// Those e_p values (e_0 for the first plane, e_1 for the second plane, etc)\n// combine to form a wuffs_base__pixel_subsampling value:\n//\n// pixsub = (e_3 << 24) | (e_2 << 16) | (e_1 << 8) | (e_0 << 0)\n//\n// Do not manipulate these bits directly; they are private implementation\n// details. Use methods such as wuffs_base__pixel_subsampling__bias_x instead.\ntypedef uint32_t wuffs_base__pixel_subsampling;\n\n#define WUFFS_BASE__PIXEL_SUBSAMPLING__NONE ((wuffs_base__pixel_subsampling)0)\n\n#define WUFFS_BASE__PIXEL_SUBSAMPLING__444 \\\n ((wuffs_base__pixel_subsampling)0x000000)\n#define WUFFS_BASE__PI" + @@ -113,9 +113,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 // 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 case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY:\n bytes_per_pixel = 1;\n break;\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY:\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY:\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->privat" + - "e_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 //\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__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 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 if (!c) {\n return 0;\n }\n if (wuffs_base__pixel_format__is_planar(c->private_impl.pixfmt)) {\n // TODO: support planar pixel formats, concious of pixel subsampling.\n return 0;\n }\n uint32_t bits_per_pixel =\n wuffs_base__pixel_format__bits_per_pixel(c->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 0;\n }\n uint64_t bytes_per_pixel = bits_per_pixel / 8;\n\n uint64_t n =\n ((uint64_t)c->private_impl.width) * ((uint64_t)c->private_impl.height);\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 //\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__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 uint" + + "32_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" + "" + "// --------\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_" + @@ -135,10 +135,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: 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 case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY:\n bytes_per_pixel = 1;\n break;\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY:\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY:\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_argu" + - "ment_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" + + "(wuffs_base__pixel_buffer){});\n if (!pixcfg) {\n return 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__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 return wuffs_base__error__bad_argument;\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_ba" + + "se__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 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-e" + + "mpty, 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#ifd" + + "ef __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/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index ebe6ac8b..c570e346 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -1396,19 +1396,44 @@ typedef uint32_t wuffs_base__pixel_format; #define WUFFS_BASE__PIXEL_FORMAT__CMY ((wuffs_base__pixel_format)0x60020888) #define WUFFS_BASE__PIXEL_FORMAT__CMYK ((wuffs_base__pixel_format)0x61038888) +extern const uint32_t wuffs_base__pixel_format__bits_per_channel[16]; + static inline bool // wuffs_base__pixel_format__is_valid(wuffs_base__pixel_format f) { return f != 0; } +// wuffs_base__pixel_format__bits_per_pixel returns, for packed pixel formats, +// the number of bits per pixel. It returns 0 for planar pixel formats. +static inline uint32_t // +wuffs_base__pixel_format__bits_per_pixel(wuffs_base__pixel_format f) { + if (((f >> 16) & 0x03) != 0) { + return 0; + } + return wuffs_base__pixel_format__bits_per_channel[0x0F & (f >> 0)] + + wuffs_base__pixel_format__bits_per_channel[0x0F & (f >> 4)] + + wuffs_base__pixel_format__bits_per_channel[0x0F & (f >> 8)] + + wuffs_base__pixel_format__bits_per_channel[0x0F & (f >> 12)]; +} + static inline bool // wuffs_base__pixel_format__is_indexed(wuffs_base__pixel_format f) { return (f >> 18) & 0x01; } +static inline bool // +wuffs_base__pixel_format__is_packed(wuffs_base__pixel_format f) { + return ((f >> 16) & 0x03) == 0; +} + +static inline bool // +wuffs_base__pixel_format__is_planar(wuffs_base__pixel_format f) { + return ((f >> 16) & 0x03) != 0; +} + static inline uint32_t // wuffs_base__pixel_format__num_planes(wuffs_base__pixel_format f) { - return f ? (((f >> 16) & 0x03) + 1) : 0; + return ((f >> 16) & 0x03) + 1; } #define WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX 4 @@ -1597,33 +1622,23 @@ 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) { - // 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: - case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY: - bytes_per_pixel = 1; - break; - case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL: - case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY: - bytes_per_pixel = 4; - break; - default: - return 0; - } - if (!c) { return 0; } + if (wuffs_base__pixel_format__is_planar(c->private_impl.pixfmt)) { + // TODO: support planar pixel formats, concious of pixel subsampling. + return 0; + } + uint32_t bits_per_pixel = + wuffs_base__pixel_format__bits_per_pixel(c->private_impl.pixfmt); + if ((bits_per_pixel == 0) || ((bits_per_pixel % 8) != 0)) { + // TODO: support fraction-of-byte pixels, e.g. 1 bit per pixel? + return 0; + } + uint64_t bytes_per_pixel = bits_per_pixel / 8; uint64_t n = ((uint64_t)c->private_impl.width) * ((uint64_t)c->private_impl.height); - if (n > (UINT64_MAX / bytes_per_pixel)) { return 0; } @@ -2049,6 +2064,16 @@ wuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* b, if (!pixcfg) { return wuffs_base__error__bad_argument; } + if (wuffs_base__pixel_format__is_planar(pixcfg->private_impl.pixfmt)) { + // TODO: support planar pixel formats, concious of pixel subsampling. + return wuffs_base__error__bad_argument; + } + uint32_t bits_per_pixel = + wuffs_base__pixel_format__bits_per_pixel(pixcfg->private_impl.pixfmt); + if ((bits_per_pixel == 0) || ((bits_per_pixel % 8) != 0)) { + return wuffs_base__error__bad_argument; + } + uint64_t bytes_per_pixel = bits_per_pixel / 8; uint8_t* ptr = pixbuf_memory.ptr; uint64_t len = pixbuf_memory.len; @@ -2070,27 +2095,6 @@ wuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* b, len -= 1024; } - // 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: - case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY: - bytes_per_pixel = 1; - break; - case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL: - case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY: - 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); @@ -4008,6 +4012,11 @@ const char* wuffs_base__error__disabled_by_previous_error = // ---------------- Images +const uint32_t wuffs_base__pixel_format__bits_per_channel[16] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x0A, 0x0C, 0x10, 0x18, 0x20, 0x30, 0x40, +}; + static uint64_t // wuffs_base__pixel_swizzler__copy_1_1(wuffs_base__slice_u8 dst, wuffs_base__slice_u8 dst_palette, diff --git a/test/c/testlib/testlib.c b/test/c/testlib/testlib.c index c371781c..16509cc4 100644 --- a/test/c/testlib/testlib.c +++ b/test/c/testlib/testlib.c @@ -398,30 +398,20 @@ const char* copy_to_io_buffer_from_pixel_buffer(wuffs_base__io_buffer* dst, 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: - case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY: - bytes_per_pixel = 1; - break; - case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL: - case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL: - case WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY: - bytes_per_pixel = 4; - break; - default: - RETURN_FAIL( - "copy_to_io_buffer_from_pixel_buffer: unsupported src pixfmt: 0x%08x", - (int)(pixfmt)); + if (wuffs_base__pixel_format__is_planar(pixfmt)) { + // If we want to support planar pixel buffers, in the future, be concious + // of pixel subsampling. + return "copy_to_io_buffer_from_pixel_buffer: cannot copy from planar src"; } + uint32_t bits_per_pixel = wuffs_base__pixel_format__bits_per_pixel(pixfmt); + if (bits_per_pixel == 0) { + return "copy_to_io_buffer_from_pixel_buffer: invalid bits_per_pixel"; + } else if ((bits_per_pixel % 8) != 0) { + return "copy_to_io_buffer_from_pixel_buffer: cannot copy fractional bytes"; + } + size_t bytes_per_pixel = bits_per_pixel / 8; uint32_t p; for (p = 0; p < 1; p++) {