Add more pixel formats to set_color_u32_at

This commit is contained in:
Nigel Tao committed 2020-05-16 11:23:44 +10:00
1 parent 9a1b8def46
commit d585f9b902
5 files changed
+231 -26

No files matched your search

+108
View File
@@ -2507,6 +2507,23 @@ wuffs_base__malloc_slice_u64(void* (*malloc_func)(size_t), uint64_t num_u64) {
// 0xAARRGGBB (Alpha most significant, Blue least), regardless of endianness.
typedef uint32_t wuffs_base__color_u32_argb_premul;
static inline uint8_t //
wuffs_base__color_u32_argb_premul__as_gray(
wuffs_base__color_u32_argb_premul c) {
// Work in 16-bit color.
uint32_t cr = 0x101 * (0xFF & (c >> 16));
uint32_t cg = 0x101 * (0xFF & (c >> 8));
uint32_t cb = 0x101 * (0xFF & (c >> 0));
// These coefficients (the fractions 0.299, 0.587 and 0.114) are the same
// as those given by the JFIF specification.
//
// Note that 19595 + 38470 + 7471 equals 65536, also known as (1 << 16). We
// shift by 24, not just by 16, because the return value is 8-bit color, not
// 16-bit color.
return ((19595 * cr) + (38470 * cg) + (7471 * cb) + 32768) >> 24;
}
// wuffs_base__premul_u32_axxx converts from non-premultiplied alpha to
// premultiplied alpha. The "axxx" means either "argb" or "abgr".
static inline uint32_t //
@@ -3715,6 +3732,23 @@ typedef struct {
// --------
// wuffs_base__pixel_palette__closest_element returns the index of the palette
// element that minimizes the sum of squared differences of the four ARGB
// channels, working in premultiplied alpha. Ties favor the smaller index.
//
// The palette_slice.len may equal (N*4), for N less than 256, which means that
// only the first N palette elements are considered. It returns 0 when N is 0.
//
// Applying this function on a per-pixel basis will not produce whole-of-image
// dithering.
uint8_t //
wuffs_base__pixel_palette__closest_element(
wuffs_base__slice_u8 palette_slice,
wuffs_base__pixel_format palette_format,
wuffs_base__color_u32_argb_premul c);
// --------
// TODO: should the func type take restrict pointers?
typedef uint64_t (*wuffs_base__pixel_swizzler__func)(
wuffs_base__slice_u8 dst,
@@ -8626,6 +8660,18 @@ wuffs_base__pixel_buffer__set_color_u32_at(
// Common formats above. Rarer formats below.
case WUFFS_BASE__PIXEL_FORMAT__Y:
wuffs_base__store_u8__no_bounds_check(
row + ((size_t)x), wuffs_base__color_u32_argb_premul__as_gray(color));
break;
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY:
wuffs_base__store_u8__no_bounds_check(
row + ((size_t)x), wuffs_base__pixel_palette__closest_element(
wuffs_base__pixel_buffer__palette(pb),
pb->pixcfg.private_impl.pixfmt, color));
break;
case WUFFS_BASE__PIXEL_FORMAT__BGR_565: {
uint32_t b5 = 0x1F & (color >> (8 - 5));
uint32_t g6 = 0x3F & (color >> (16 - 6));
@@ -8669,6 +8715,68 @@ wuffs_base__pixel_buffer__set_color_u32_at(
// --------
uint8_t //
wuffs_base__pixel_palette__closest_element(
wuffs_base__slice_u8 palette_slice,
wuffs_base__pixel_format palette_format,
wuffs_base__color_u32_argb_premul c) {
size_t n = palette_slice.len / 4;
if (n > 256) {
n = 256;
}
size_t best_index = 0;
uint64_t best_score = 0xFFFFFFFFFFFFFFFF;
// Work in 16-bit color.
uint32_t ca = 0x101 * (0xFF & (c >> 24));
uint32_t cr = 0x101 * (0xFF & (c >> 16));
uint32_t cg = 0x101 * (0xFF & (c >> 8));
uint32_t cb = 0x101 * (0xFF & (c >> 0));
switch (palette_format.repr) {
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL:
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_PREMUL:
case WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_BINARY: {
bool nonpremul = palette_format.repr ==
WUFFS_BASE__PIXEL_FORMAT__INDEXED__BGRA_NONPREMUL;
size_t i;
for (i = 0; i < n; i++) {
// Work in 16-bit color.
uint32_t pb = 0x101 * ((uint32_t)(palette_slice.ptr[(4 * i) + 0]));
uint32_t pg = 0x101 * ((uint32_t)(palette_slice.ptr[(4 * i) + 1]));
uint32_t pr = 0x101 * ((uint32_t)(palette_slice.ptr[(4 * i) + 2]));
uint32_t pa = 0x101 * ((uint32_t)(palette_slice.ptr[(4 * i) + 3]));
// Convert to premultiplied alpha.
if (nonpremul && (pa != 0xFFFF)) {
pb = (pb * pa) / 0xFFFF;
pg = (pg * pa) / 0xFFFF;
pr = (pr * pa) / 0xFFFF;
}
// These deltas are conceptually int32_t (signed) but after squaring,
// it's equivalent to work in uint32_t (unsigned).
pb -= cb;
pg -= cg;
pr -= cr;
pa -= ca;
uint64_t score = ((uint64_t)(pb * pb)) + ((uint64_t)(pg * pg)) +
((uint64_t)(pr * pr)) + ((uint64_t)(pa * pa));
if (best_score > score) {
best_score = score;
best_index = i;
}
}
break;
}
}
return best_index;
}
// --------
static uint64_t //
wuffs_base__pixel_swizzler__bgra_premul__bgra_nonpremul__src(
wuffs_base__slice_u8 dst,