mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Add wuffs_base__pixel_buffer__color_u32_at
This commit is contained in:
1 parent
47ce6ea296
commit
557c2b7998
5 files changed
+263
-21
No files matched your search
@@ -21,6 +21,107 @@ const uint32_t wuffs_base__pixel_format__bits_per_channel[16] = {
|
||||
0x08, 0x0A, 0x0C, 0x10, 0x18, 0x20, 0x30, 0x40,
|
||||
};
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__swap_u32_argb_abgr(uint32_t u) {
|
||||
uint32_t o = u & 0xFF00FF00;
|
||||
uint32_t r = u & 0x00FF0000;
|
||||
uint32_t b = u & 0x000000FF;
|
||||
return o | (r >> 16) | (b << 16);
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__premul_u32_axxx(uint32_t u) {
|
||||
// Multiplying by 0x101 (twice, once for alpha and once for color) converts
|
||||
// from 8-bit to 16-bit color. Shifting right by 8 undoes that.
|
||||
//
|
||||
// Working in the higher bit depth can produce slightly different (and
|
||||
// arguably slightly more accurate) results. For example, given 8-bit blue
|
||||
// and alpha of 0x80 and 0x81:
|
||||
//
|
||||
// - ((0x80 * 0x81 ) / 0xFF ) = 0x40 = 0x40
|
||||
// - ((0x8080 * 0x8181) / 0xFFFF) >> 8 = 0x4101 >> 8 = 0x41
|
||||
uint32_t a = 0xFF & (u >> 24);
|
||||
uint32_t a16 = a * (0x101 * 0x101);
|
||||
uint32_t r = 0xFF & (u >> 16);
|
||||
r = ((r * a16) / 0xFFFF) >> 8;
|
||||
uint32_t g = 0xFF & (u >> 8);
|
||||
g = ((g * a16) / 0xFFFF) >> 8;
|
||||
uint32_t b = 0xFF & (u >> 0);
|
||||
b = ((b * a16) / 0xFFFF) >> 8;
|
||||
return (a << 24) | (r << 16) | (g << 8) | (b << 0);
|
||||
}
|
||||
|
||||
wuffs_base__color_u32_argb_premul //
|
||||
wuffs_base__pixel_buffer__color_u32_at(const wuffs_base__pixel_buffer* b,
|
||||
uint32_t x,
|
||||
uint32_t y) {
|
||||
if (!b || (x >= b->pixcfg.private_impl.width) ||
|
||||
(y >= b->pixcfg.private_impl.height)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (wuffs_base__pixel_format__is_planar(&b->pixcfg.private_impl.pixfmt)) {
|
||||
// TODO: support planar formats.
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t stride = b->private_impl.planes[0].stride;
|
||||
uint8_t* row = b->private_impl.planes[0].ptr + (stride * ((size_t)y));
|
||||
|
||||
switch (b->pixcfg.private_impl.pixfmt.repr) {
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:
|
||||
WUFFS_BASE__FALLTHROUGH;
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY:
|
||||
return wuffs_base__load_u32le(row + (4 * ((size_t)x)));
|
||||
|
||||
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL:
|
||||
WUFFS_BASE__FALLTHROUGH;
|
||||
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY: {
|
||||
uint8_t* palette = b->private_impl.planes[3].ptr;
|
||||
return wuffs_base__load_u32le(palette + (4 * ((size_t)row[x])));
|
||||
}
|
||||
|
||||
// Common formats above. Rarer formats below.
|
||||
|
||||
case WUFFS_BASE__PIXEL_FORMAT__Y:
|
||||
return 0xFF000000 | (0x00010101 * ((uint32_t)(row[x])));
|
||||
|
||||
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL: {
|
||||
uint8_t* palette = b->private_impl.planes[3].ptr;
|
||||
return wuffs_base__premul_u32_axxx(
|
||||
wuffs_base__load_u32le(palette + (4 * ((size_t)row[x]))));
|
||||
}
|
||||
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGR:
|
||||
return 0xFF000000 | wuffs_base__load_u24le(row + (3 * ((size_t)x)));
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGRX:
|
||||
return 0xFF000000 | wuffs_base__load_u32le(row + (4 * ((size_t)x)));
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
|
||||
return wuffs_base__premul_u32_axxx(
|
||||
wuffs_base__load_u32le(row + (4 * ((size_t)x))));
|
||||
|
||||
case WUFFS_BASE__PIXEL_FORMAT__RGB:
|
||||
return wuffs_base__swap_u32_argb_abgr(
|
||||
0xFF000000 | wuffs_base__load_u24le(row + (3 * ((size_t)x))));
|
||||
case WUFFS_BASE__PIXEL_FORMAT__RGBX:
|
||||
return wuffs_base__swap_u32_argb_abgr(
|
||||
0xFF000000 | wuffs_base__load_u32le(row + (4 * ((size_t)x))));
|
||||
case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL:
|
||||
return wuffs_base__swap_u32_argb_abgr(wuffs_base__premul_u32_axxx(
|
||||
wuffs_base__load_u32le(row + (4 * ((size_t)x)))));
|
||||
case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL:
|
||||
WUFFS_BASE__FALLTHROUGH;
|
||||
case WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY:
|
||||
return wuffs_base__swap_u32_argb_abgr(
|
||||
wuffs_base__load_u32le(row + (4 * ((size_t)x))));
|
||||
}
|
||||
|
||||
// TODO: support more formats.
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --------
|
||||
|
||||
static uint64_t //
|
||||
wuffs_base__pixel_swizzler__copy_1_1(wuffs_base__slice_u8 dst,
|
||||
wuffs_base__slice_u8 dst_palette,
|
||||
|
||||
@@ -907,6 +907,8 @@ typedef struct {
|
||||
inline wuffs_base__slice_u8 palette();
|
||||
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,
|
||||
uint32_t y) const;
|
||||
#endif // __cplusplus
|
||||
|
||||
} wuffs_base__pixel_buffer;
|
||||
@@ -1058,6 +1060,11 @@ wuffs_base__pixel_buffer__plane(wuffs_base__pixel_buffer* b, uint32_t p) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
wuffs_base__color_u32_argb_premul //
|
||||
wuffs_base__pixel_buffer__color_u32_at(const wuffs_base__pixel_buffer* b,
|
||||
uint32_t x,
|
||||
uint32_t y);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
inline wuffs_base__status //
|
||||
@@ -1087,6 +1094,11 @@ wuffs_base__pixel_buffer::plane(uint32_t p) {
|
||||
return wuffs_base__pixel_buffer__plane(this, p);
|
||||
}
|
||||
|
||||
inline wuffs_base__color_u32_argb_premul //
|
||||
wuffs_base__pixel_buffer::color_u32_at(uint32_t x, uint32_t y) const {
|
||||
return wuffs_base__pixel_buffer__color_u32_at(this, x, y);
|
||||
}
|
||||
|
||||
#endif // __cplusplus
|
||||
|
||||
// --------
|
||||
|
||||
+21
-16
@@ -26,14 +26,19 @@ const baseAllImplC = "" +
|
||||
""
|
||||
|
||||
const baseImageImplC = "" +
|
||||
"// ---------------- 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__xxx__index__src(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_len3 = dst.len / 3;\n size_t len = dst_len3 < src.len ? dst_len3 : src.len;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = len;\n\n // N is the loop unroll count.\n const int N = 4;\n\n // The comparison in the while condition" +
|
||||
" is \">\", not \">=\", because with \">=\",\n // the last 4-byte store could write past the end of the dst slice.\n //\n // Each 4-byte store writes one too many bytes, but a subsequent store will\n // overwrite that with the correct byte. There is always another store,\n // whether a 4-byte store in this loop or a 1-byte store in the next loop.\n while (n > N) {\n wuffs_base__store_u32le(\n d + (0 * 3),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4)));\n wuffs_base__store_u32le(\n d + (1 * 3),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[1]) * 4)));\n wuffs_base__store_u32le(\n d + (2 * 3),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[2]) * 4)));\n wuffs_base__store_u32le(\n d + (3 * 3),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[3]) * 4)));\n\n s += 1 * N;\n d += 3 * N;\n n -= (size_t)(1 * N);\n }\n\n while (n >= 1) {\n uint32_t s0 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[" +
|
||||
"0]) * 4));\n d[0] = (uint8_t)(s0 >> 0);\n d[1] = (uint8_t)(s0 >> 8);\n d[2] = (uint8_t)(s0 >> 16);\n\n s += 1 * 1;\n d += 3 * 1;\n n -= (size_t)(1 * 1);\n }\n\n return len;\n}\n\nstatic uint64_t //\nwuffs_base__pixel_swizzler__xxx__index_binary_alpha__src_over(\n 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_len3 = dst.len / 3;\n size_t len = dst_len3 < src.len ? dst_len3 : src.len;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = len;\n\n // N is the loop unroll count.\n const int N = 4;\n\n while (n >= N) {\n uint32_t s0 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4));\n if (s0) {\n wuffs_base__store_u24le(d + (0 * 4), s0);\n }\n uint32_t s1 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[1]) * 4));\n if (s1) {\n wuffs_base__store_u24le(d + (1 * 4), s1);\n }\n uint32_t s2 =\n wuffs_base__load_u32le(dst_pal" +
|
||||
"ette.ptr + ((uint32_t)(s[2]) * 4));\n if (s2) {\n wuffs_base__store_u24le(d + (2 * 4), s2);\n }\n uint32_t s3 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[3]) * 4));\n if (s3) {\n wuffs_base__store_u24le(d + (3 * 4), s3);\n }\n\n s += 1 * N;\n d += 3 * N;\n n -= (size_t)(1 * N);\n }\n\n while (n >= 1) {\n uint32_t s0 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4));\n if (s0) {\n wuffs_base__store_u24le(d + (0 * 4), s0);\n }\n\n s += 1 * 1;\n d += 3 * 1;\n n -= (size_t)(1 * 1);\n }\n\n return len;\n}\n\nstatic uint64_t //\nwuffs_base__pixel_swizzler__xxxx__index__src(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 siz" +
|
||||
"e_t n = len;\n\n // N is the loop unroll count.\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 -= (size_t)(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 -= (size_t)(1 * 1);\n }\n\n return len;\n}\n\nstatic uint64_t //\nwuffs_base__pixel_swizzler__xxxx__index_binary_alpha__src_over(\n wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 dst_palett" +
|
||||
"e,\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 size_t n = len;\n\n // N is the loop unroll count.\n const int N = 4;\n\n while (n >= N) {\n uint32_t s0 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4));\n if (s0) {\n wuffs_base__store_u32le(d + (0 * 4), s0);\n }\n uint32_t s1 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[1]) * 4));\n if (s1) {\n wuffs_base__store_u32le(d + (1 * 4), s1);\n }\n uint32_t s2 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[2]) * 4));\n if (s2) {\n wuffs_base__store_u32le(d + (2 * 4), s2);\n }\n uint32_t s3 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[3]) * 4));\n if (s3) {\n wuffs_base__store_u32le(d + (3 * 4), s3);\n }\n\n s += 1 * N;\n d += 4 * N;\n n -= (size_t)(1 * N);\n }\n\n whil" +
|
||||
"e (n >= 1) {\n uint32_t s0 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4));\n if (s0) {\n wuffs_base__store_u32le(d + (0 * 4), s0);\n }\n\n s += 1 * 1;\n d += 4 * 1;\n n -= (size_t)(1 * 1);\n }\n\n return len;\n}\n\nstatic uint64_t //\nwuffs_base__pixel_swizzler__xxxx__y(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 dst_palette,\n wuffs_base__slice_u8 src) {\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 size_t n = len;\n\n // TODO: unroll.\n\n while (n >= 1) {\n wuffs_base__store_u32le(d + (0 * 4),\n 0xFF000000 | (0x010101 * (uint32_t)s[0]));\n\n s += 1 * 1;\n d += 4 * 1;\n n -= (size_t)(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\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 inline uint32_t //\nwuffs_base__swap_u32_argb_abgr(uint32_t u) {\n uint32_t o = u & 0xFF00FF00;\n uint32_t r = u & 0x00FF0000;\n uint32_t b = u & 0x000000FF;\n return o | (r >> 16) | (b << 16);\n}\n\nstatic inline uint32_t //\nwuffs_base__premul_u32_axxx(uint32_t u) {\n // Multiplying by 0x101 (twice, once for alpha and once for color) converts\n // from 8-bit to 16-bit color. Shifting right by 8 undoes that.\n //\n // Working in the higher bit depth can produce slightly different (and\n // arguably slightly more accurate) results. For example, given 8-bit blue\n // and alpha of 0x80 and 0x81:\n //\n // - ((0x80 * 0x81 ) / 0xFF ) = 0x40 = 0x40\n // - ((0x8080 * 0x8181) / 0xFFFF) >> 8 = 0x4101 >> 8 = 0x41\n uint32_t a = 0xFF & (u >> 24);\n uint32_t a16 = a * (0x101 * 0x101);\n uint32_t r = " +
|
||||
"0xFF & (u >> 16);\n r = ((r * a16) / 0xFFFF) >> 8;\n uint32_t g = 0xFF & (u >> 8);\n g = ((g * a16) / 0xFFFF) >> 8;\n uint32_t b = 0xFF & (u >> 0);\n b = ((b * a16) / 0xFFFF) >> 8;\n return (a << 24) | (r << 16) | (g << 8) | (b << 0);\n}\n\nwuffs_base__color_u32_argb_premul //\nwuffs_base__pixel_buffer__color_u32_at(const wuffs_base__pixel_buffer* b,\n uint32_t x,\n uint32_t y) {\n if (!b || (x >= b->pixcfg.private_impl.width) ||\n (y >= b->pixcfg.private_impl.height)) {\n return 0;\n }\n\n if (wuffs_base__pixel_format__is_planar(&b->pixcfg.private_impl.pixfmt)) {\n // TODO: support planar formats.\n return 0;\n }\n\n size_t stride = b->private_impl.planes[0].stride;\n uint8_t* row = b->private_impl.planes[0].ptr + (stride * ((size_t)y));\n\n switch (b->pixcfg.private_impl.pixfmt.repr) {\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:\n WUFFS_BASE__FALLTHROUGH;\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY:\n return wuffs_b" +
|
||||
"ase__load_u32le(row + (4 * ((size_t)x)));\n\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL:\n WUFFS_BASE__FALLTHROUGH;\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY: {\n uint8_t* palette = b->private_impl.planes[3].ptr;\n return wuffs_base__load_u32le(palette + (4 * ((size_t)row[x])));\n }\n\n // Common formats above. Rarer formats below.\n\n case WUFFS_BASE__PIXEL_FORMAT__Y:\n return 0xFF000000 | (0x00010101 * ((uint32_t)(row[x])));\n\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL: {\n uint8_t* palette = b->private_impl.planes[3].ptr;\n return wuffs_base__premul_u32_axxx(\n wuffs_base__load_u32le(palette + (4 * ((size_t)row[x]))));\n }\n\n case WUFFS_BASE__PIXEL_FORMAT__BGR:\n return 0xFF000000 | wuffs_base__load_u24le(row + (3 * ((size_t)x)));\n case WUFFS_BASE__PIXEL_FORMAT__BGRX:\n return 0xFF000000 | wuffs_base__load_u32le(row + (4 * ((size_t)x)));\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:\n return wuffs_base__pr" +
|
||||
"emul_u32_axxx(\n wuffs_base__load_u32le(row + (4 * ((size_t)x))));\n\n case WUFFS_BASE__PIXEL_FORMAT__RGB:\n return wuffs_base__swap_u32_argb_abgr(\n 0xFF000000 | wuffs_base__load_u24le(row + (3 * ((size_t)x))));\n case WUFFS_BASE__PIXEL_FORMAT__RGBX:\n return wuffs_base__swap_u32_argb_abgr(\n 0xFF000000 | wuffs_base__load_u32le(row + (4 * ((size_t)x))));\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL:\n return wuffs_base__swap_u32_argb_abgr(wuffs_base__premul_u32_axxx(\n wuffs_base__load_u32le(row + (4 * ((size_t)x)))));\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL:\n WUFFS_BASE__FALLTHROUGH;\n case WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY:\n return wuffs_base__swap_u32_argb_abgr(\n wuffs_base__load_u32le(row + (4 * ((size_t)x))));\n }\n\n // TODO: support more formats.\n return 0;\n}\n\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__xxx__index__src(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_len3 = dst.len / 3;\n size_t len = dst_len3 < src.len ? dst_len3 : src.len;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = len;\n\n // N is the loop unroll count.\n const int N = 4;\n\n // The comparison in the while condition is \">\", not \">=\", because with \">=\",\n // the last 4-byte store could write past the end of the dst slice.\n //\n // Each 4-byte store writes one too many bytes, but a subsequent store wil" +
|
||||
"l\n // overwrite that with the correct byte. There is always another store,\n // whether a 4-byte store in this loop or a 1-byte store in the next loop.\n while (n > N) {\n wuffs_base__store_u32le(\n d + (0 * 3),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4)));\n wuffs_base__store_u32le(\n d + (1 * 3),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[1]) * 4)));\n wuffs_base__store_u32le(\n d + (2 * 3),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[2]) * 4)));\n wuffs_base__store_u32le(\n d + (3 * 3),\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[3]) * 4)));\n\n s += 1 * N;\n d += 3 * N;\n n -= (size_t)(1 * N);\n }\n\n while (n >= 1) {\n uint32_t s0 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4));\n d[0] = (uint8_t)(s0 >> 0);\n d[1] = (uint8_t)(s0 >> 8);\n d[2] = (uint8_t)(s0 >> 16);\n\n s += 1 * 1;\n d += 3 * 1;\n n -= (size_t)(1 * 1);\n }\n\n return len;\n}\n\nsta" +
|
||||
"tic uint64_t //\nwuffs_base__pixel_swizzler__xxx__index_binary_alpha__src_over(\n 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_len3 = dst.len / 3;\n size_t len = dst_len3 < src.len ? dst_len3 : src.len;\n uint8_t* d = dst.ptr;\n uint8_t* s = src.ptr;\n size_t n = len;\n\n // N is the loop unroll count.\n const int N = 4;\n\n while (n >= N) {\n uint32_t s0 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4));\n if (s0) {\n wuffs_base__store_u24le(d + (0 * 4), s0);\n }\n uint32_t s1 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[1]) * 4));\n if (s1) {\n wuffs_base__store_u24le(d + (1 * 4), s1);\n }\n uint32_t s2 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[2]) * 4));\n if (s2) {\n wuffs_base__store_u24le(d + (2 * 4), s2);\n }\n uint32_t s3 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[3]) " +
|
||||
"* 4));\n if (s3) {\n wuffs_base__store_u24le(d + (3 * 4), s3);\n }\n\n s += 1 * N;\n d += 3 * N;\n n -= (size_t)(1 * N);\n }\n\n while (n >= 1) {\n uint32_t s0 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4));\n if (s0) {\n wuffs_base__store_u24le(d + (0 * 4), s0);\n }\n\n s += 1 * 1;\n d += 3 * 1;\n n -= (size_t)(1 * 1);\n }\n\n return len;\n}\n\nstatic uint64_t //\nwuffs_base__pixel_swizzler__xxxx__index__src(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 size_t n = len;\n\n // N is the loop unroll count.\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 + ((ui" +
|
||||
"nt32_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 -= (size_t)(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 -= (size_t)(1 * 1);\n }\n\n return len;\n}\n\nstatic uint64_t //\nwuffs_base__pixel_swizzler__xxxx__index_binary_alpha__src_over(\n 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 size_t n = len;\n\n // N is the loop unroll count.\n const int N = 4;\n\n while (n >= N) {\n uint32_t s0 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4));\n if (s0) {\n wuffs_base__store_u32le(d + (0 * 4), s0);\n }\n uint32_t s1 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[1]) * 4));\n if (s1) {\n wuffs_base__store_u32le(d + (1 * 4), s1);\n }\n uint32_t s2 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[2]) * 4));\n if (s2) {\n wuffs_base__store_u32le(d + (2 * 4), s2);\n }\n uint32_t s3 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[3]) * 4));\n if (s3) {\n wuffs_base__store_u32le(d + (3 * 4), s3);\n }\n\n s += 1 * N;\n d += 4 * N;\n n -= (size_t)(1 * N);\n }\n\n while (n >= 1) {\n uint32_t s0 =\n wuffs_base__load_u32le(dst_palette.ptr + ((uint32_t)(s[0]) * 4));\n if (s0) {\n wuffs_base__store_u32le(d + (0 * 4), s0);\n }\n\n s += 1 * 1;" +
|
||||
"\n d += 4 * 1;\n n -= (size_t)(1 * 1);\n }\n\n return len;\n}\n\nstatic uint64_t //\nwuffs_base__pixel_swizzler__xxxx__y(wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 dst_palette,\n wuffs_base__slice_u8 src) {\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 size_t n = len;\n\n // TODO: unroll.\n\n while (n >= 1) {\n wuffs_base__store_u32le(d + (0 * 4),\n 0xFF000000 | (0x010101 * (uint32_t)s[0]));\n\n s += 1 * 1;\n d += 4 * 1;\n n -= (size_t)(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\n" +
|
||||
"" +
|
||||
"// --------\n\nwuffs_base__status //\nwuffs_base__pixel_swizzler__prepare(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 wuffs_base__pixel_blend blend) {\n if (!p) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n\n // TODO: support many more formats.\n\n wuffs_base__pixel_swizzler__func func = NULL;\n\n switch (src_format.repr) {\n case WUFFS_BASE__PIXEL_FORMAT__Y:\n switch (dst_format.repr) {\n case WUFFS_BASE__PIXEL_FORMAT__BGR:\n case WUFFS_BASE__PIXEL_FORMAT__RGB:\n // TODO.\n break;\n\n case WUFFS_BASE__PIXEL_FORMAT__BGRX:\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:\n case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:\n case WU" +
|
||||
"FFS_BASE__PIXEL_FORMAT__BGRA_BINARY:\n case WUFFS_BASE__PIXEL_FORMAT__RGBX:\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 func = wuffs_base__pixel_swizzler__xxxx__y;\n break;\n\n default:\n break;\n }\n break;\n\n case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY:\n switch (dst_format.repr) {\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 switch (blend) {\n case WUFFS_BASE__PIXEL_BLEND__SRC:\n func = wuffs_base__pixel_swizzler__copy_1_1;\n break;\n default:\n break;\n }\n break;\n\n case WUFFS_" +
|
||||
@@ -155,14 +160,14 @@ const baseImagePublicH = "" +
|
||||
" 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__update(\n this, bounds, duration, index, io_position, disposal,\n opaque_within_bounds, 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_base__frame_config::index() const {\n return wuffs_b" +
|
||||
"ase__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 {\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#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* b,\n const wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n if (!b) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n memset(b, 0, sizeof(*b));\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 // 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 &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__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 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 wuffs_base__make_status(NULL);\n}\n\nstatic inline wuffs_base__status //\nwuffs_base__pixel_buffer__set_from_table(wuffs_base__pixel_buffer* b,\n const wuffs_base__pixel_config* pixcfg,\n wuffs_base__table_u8 pixbuf_memory) {\n if (" +
|
||||
"!b) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n memset(b, 0, sizeof(*b));\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 b->pixcfg = *pixcfg;\n b->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* 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__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* b) {\n if (b) {\n return b->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* b, uint32_t p) {\n if (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX)) {\n retur" +
|
||||
"n b->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\n#ifdef __cplusplus\n\ninline wuffs_base__status //\nwuffs_base__pixel_buffer::set_from_slice(const 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__status //\nwuffs_base__pixel_buffer::set_from_table(const wuffs_base__pixel_config* pixcfg,\n wuffs_base__table_u8 pixbuf_memory) {\n return wuffs_base__pixel_buffer__set_from_table(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() 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\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(\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#endif // __cplusplus\n\n} wuffs_base__pixel_buffer;\n\nstatic inline wuffs_base__pixel_buffer //\nwuffs_base__null_pixel_buffe" +
|
||||
"r() {\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* b,\n const wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n if (!b) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n memset(b, 0, sizeof(*b));\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__unsu" +
|
||||
"pported_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 &b->private_impl.planes[WUFFS_BASE__PIXEL_FORM" +
|
||||
"AT__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 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 wuffs_base__make_status(NULL);\n}\n\nstatic inline wuffs_base__status //\nwuffs_base__pixel_buffer__set_from_table(wuffs_base__pixel_buffer* b,\n " +
|
||||
" const wuffs_base__pixel_config* pixcfg,\n wuffs_base__table_u8 pixbuf_memory) {\n if (!b) {\n return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n }\n memset(b, 0, sizeof(*b));\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_b" +
|
||||
"ase__error__bad_argument);\n }\n\n b->pixcfg = *pixcfg;\n b->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* 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__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* b) {\n if (b) {\n return b->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 //\nwu" +
|
||||
"ffs_base__pixel_buffer__plane(wuffs_base__pixel_buffer* b, uint32_t p) {\n if (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX)) {\n return b->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__color_u32_argb_premul //\nwuffs_base__pixel_buffer__color_u32_at(const wuffs_base__pixel_buffer* b,\n uint32_t x,\n uint32_t y);\n\n#ifdef __cplusplus\n\ninline wuffs_base__status //\nwuffs_base__pixel_buffer::set_from_slice(const 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__status //\nwuffs_base__pixel_buffer::set_from_table(const wuffs_base__pixel_config* pixcfg,\n wuffs_base__table_u8 pixbuf_memory) {\n return wuffs_base__pixel_buffe" +
|
||||
"r__set_from_table(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() 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\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" +
|
||||
"" +
|
||||
|
||||
@@ -2360,6 +2360,8 @@ typedef struct {
|
||||
inline wuffs_base__slice_u8 palette();
|
||||
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,
|
||||
uint32_t y) const;
|
||||
#endif // __cplusplus
|
||||
|
||||
} wuffs_base__pixel_buffer;
|
||||
@@ -2511,6 +2513,11 @@ wuffs_base__pixel_buffer__plane(wuffs_base__pixel_buffer* b, uint32_t p) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
wuffs_base__color_u32_argb_premul //
|
||||
wuffs_base__pixel_buffer__color_u32_at(const wuffs_base__pixel_buffer* b,
|
||||
uint32_t x,
|
||||
uint32_t y);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
inline wuffs_base__status //
|
||||
@@ -2540,6 +2547,11 @@ wuffs_base__pixel_buffer::plane(uint32_t p) {
|
||||
return wuffs_base__pixel_buffer__plane(this, p);
|
||||
}
|
||||
|
||||
inline wuffs_base__color_u32_argb_premul //
|
||||
wuffs_base__pixel_buffer::color_u32_at(uint32_t x, uint32_t y) const {
|
||||
return wuffs_base__pixel_buffer__color_u32_at(this, x, y);
|
||||
}
|
||||
|
||||
#endif // __cplusplus
|
||||
|
||||
// --------
|
||||
@@ -5817,6 +5829,107 @@ const uint32_t wuffs_base__pixel_format__bits_per_channel[16] = {
|
||||
0x08, 0x0A, 0x0C, 0x10, 0x18, 0x20, 0x30, 0x40,
|
||||
};
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__swap_u32_argb_abgr(uint32_t u) {
|
||||
uint32_t o = u & 0xFF00FF00;
|
||||
uint32_t r = u & 0x00FF0000;
|
||||
uint32_t b = u & 0x000000FF;
|
||||
return o | (r >> 16) | (b << 16);
|
||||
}
|
||||
|
||||
static inline uint32_t //
|
||||
wuffs_base__premul_u32_axxx(uint32_t u) {
|
||||
// Multiplying by 0x101 (twice, once for alpha and once for color) converts
|
||||
// from 8-bit to 16-bit color. Shifting right by 8 undoes that.
|
||||
//
|
||||
// Working in the higher bit depth can produce slightly different (and
|
||||
// arguably slightly more accurate) results. For example, given 8-bit blue
|
||||
// and alpha of 0x80 and 0x81:
|
||||
//
|
||||
// - ((0x80 * 0x81 ) / 0xFF ) = 0x40 = 0x40
|
||||
// - ((0x8080 * 0x8181) / 0xFFFF) >> 8 = 0x4101 >> 8 = 0x41
|
||||
uint32_t a = 0xFF & (u >> 24);
|
||||
uint32_t a16 = a * (0x101 * 0x101);
|
||||
uint32_t r = 0xFF & (u >> 16);
|
||||
r = ((r * a16) / 0xFFFF) >> 8;
|
||||
uint32_t g = 0xFF & (u >> 8);
|
||||
g = ((g * a16) / 0xFFFF) >> 8;
|
||||
uint32_t b = 0xFF & (u >> 0);
|
||||
b = ((b * a16) / 0xFFFF) >> 8;
|
||||
return (a << 24) | (r << 16) | (g << 8) | (b << 0);
|
||||
}
|
||||
|
||||
wuffs_base__color_u32_argb_premul //
|
||||
wuffs_base__pixel_buffer__color_u32_at(const wuffs_base__pixel_buffer* b,
|
||||
uint32_t x,
|
||||
uint32_t y) {
|
||||
if (!b || (x >= b->pixcfg.private_impl.width) ||
|
||||
(y >= b->pixcfg.private_impl.height)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (wuffs_base__pixel_format__is_planar(&b->pixcfg.private_impl.pixfmt)) {
|
||||
// TODO: support planar formats.
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t stride = b->private_impl.planes[0].stride;
|
||||
uint8_t* row = b->private_impl.planes[0].ptr + (stride * ((size_t)y));
|
||||
|
||||
switch (b->pixcfg.private_impl.pixfmt.repr) {
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL:
|
||||
WUFFS_BASE__FALLTHROUGH;
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGRA_BINARY:
|
||||
return wuffs_base__load_u32le(row + (4 * ((size_t)x)));
|
||||
|
||||
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL:
|
||||
WUFFS_BASE__FALLTHROUGH;
|
||||
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY: {
|
||||
uint8_t* palette = b->private_impl.planes[3].ptr;
|
||||
return wuffs_base__load_u32le(palette + (4 * ((size_t)row[x])));
|
||||
}
|
||||
|
||||
// Common formats above. Rarer formats below.
|
||||
|
||||
case WUFFS_BASE__PIXEL_FORMAT__Y:
|
||||
return 0xFF000000 | (0x00010101 * ((uint32_t)(row[x])));
|
||||
|
||||
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL: {
|
||||
uint8_t* palette = b->private_impl.planes[3].ptr;
|
||||
return wuffs_base__premul_u32_axxx(
|
||||
wuffs_base__load_u32le(palette + (4 * ((size_t)row[x]))));
|
||||
}
|
||||
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGR:
|
||||
return 0xFF000000 | wuffs_base__load_u24le(row + (3 * ((size_t)x)));
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGRX:
|
||||
return 0xFF000000 | wuffs_base__load_u32le(row + (4 * ((size_t)x)));
|
||||
case WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL:
|
||||
return wuffs_base__premul_u32_axxx(
|
||||
wuffs_base__load_u32le(row + (4 * ((size_t)x))));
|
||||
|
||||
case WUFFS_BASE__PIXEL_FORMAT__RGB:
|
||||
return wuffs_base__swap_u32_argb_abgr(
|
||||
0xFF000000 | wuffs_base__load_u24le(row + (3 * ((size_t)x))));
|
||||
case WUFFS_BASE__PIXEL_FORMAT__RGBX:
|
||||
return wuffs_base__swap_u32_argb_abgr(
|
||||
0xFF000000 | wuffs_base__load_u32le(row + (4 * ((size_t)x))));
|
||||
case WUFFS_BASE__PIXEL_FORMAT__RGBA_NONPREMUL:
|
||||
return wuffs_base__swap_u32_argb_abgr(wuffs_base__premul_u32_axxx(
|
||||
wuffs_base__load_u32le(row + (4 * ((size_t)x)))));
|
||||
case WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL:
|
||||
WUFFS_BASE__FALLTHROUGH;
|
||||
case WUFFS_BASE__PIXEL_FORMAT__RGBA_BINARY:
|
||||
return wuffs_base__swap_u32_argb_abgr(
|
||||
wuffs_base__load_u32le(row + (4 * ((size_t)x))));
|
||||
}
|
||||
|
||||
// TODO: support more formats.
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --------
|
||||
|
||||
static uint64_t //
|
||||
wuffs_base__pixel_swizzler__copy_1_1(wuffs_base__slice_u8 dst,
|
||||
wuffs_base__slice_u8 dst_palette,
|
||||
|
||||
@@ -830,12 +830,23 @@ const char* do_test__wuffs_base__image_decoder(
|
||||
RETURN_FAIL("pixbuf_len too small");
|
||||
} else if (n > BUFFER_SIZE) {
|
||||
RETURN_FAIL("pixbuf_len too large");
|
||||
} else {
|
||||
wuffs_base__color_u32_argb_premul got_final_pixel =
|
||||
wuffs_base__load_u32le(&global_pixel_array[n - 4]);
|
||||
if (got_final_pixel != want_final_pixel) {
|
||||
RETURN_FAIL("final pixel: got 0x%08" PRIX32 ", want 0x%08" PRIX32,
|
||||
got_final_pixel, want_final_pixel);
|
||||
}
|
||||
}
|
||||
wuffs_base__color_u32_argb_premul got_final_pixel =
|
||||
wuffs_base__load_u32le(&global_pixel_array[n - 4]);
|
||||
if (got_final_pixel != want_final_pixel) {
|
||||
RETURN_FAIL("final pixel: got 0x%08" PRIX32 ", want 0x%08" PRIX32,
|
||||
got_final_pixel, want_final_pixel);
|
||||
|
||||
if ((want_width > 0) && (want_height > 0)) {
|
||||
wuffs_base__color_u32_argb_premul got_final_pixel =
|
||||
wuffs_base__pixel_buffer__color_u32_at(&pb, want_width - 1,
|
||||
want_height - 1);
|
||||
if (got_final_pixel != want_final_pixel) {
|
||||
RETURN_FAIL("final pixel: got 0x%08" PRIX32 ", want 0x%08" PRIX32,
|
||||
got_final_pixel, want_final_pixel);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user