aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2021-12-15 03:24:05 +0100
committerPřemysl Eric Janouch <p@janouch.name>2021-12-15 03:43:57 +0100
commit7297c40f93763597af43e25e04650159e7a13597 (patch)
treeae8c41f3ccf30b14ae34a446806a2fcd822de1b6
parentea2d159773c55575c6b1ea2fa76f6dd4c4883499 (diff)
downloadfiv-7297c40f93763597af43e25e04650159e7a13597.tar.gz
fiv-7297c40f93763597af43e25e04650159e7a13597.tar.xz
fiv-7297c40f93763597af43e25e04650159e7a13597.zip
Employ libwebp's alpha premultiplication
It seems to perform roughly equally in optimized builds.
-rw-r--r--fastiv-io.c18
1 files changed, 10 insertions, 8 deletions
diff --git a/fastiv-io.c b/fastiv-io.c
index 67041d3..33ff9b8 100644
--- a/fastiv-io.c
+++ b/fastiv-io.c
@@ -162,6 +162,9 @@ try_append_page(cairo_surface_t *surface, cairo_surface_t **result,
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+// From libwebp, verified to exactly match [x * a / 255].
+#define PREMULTIPLY8(a, x) (((uint32_t) (x) * (uint32_t) (a) * 32897U) >> 23)
+
static bool
pull_passthrough(const wuffs_base__more_information *minfo,
wuffs_base__io_buffer *src, wuffs_base__io_buffer *dst, GError **error)
@@ -1375,12 +1378,11 @@ load_libheif_image(struct heif_image_handle *handle, GError **error)
for (int y = 0; y < h; y++) {
uint32_t *dstp = (uint32_t *) (dst + dst_stride * y);
for (int x = 0; x < w; x++) {
- uint32_t pixel = dstp[x], a = pixel >> 24;
- uint8_t r = pixel >> 16;
- uint8_t g = pixel >> 8;
- uint8_t b = pixel;
+ uint32_t argb = dstp[x], a = argb >> 24;
dstp[x] = a << 24 |
- (r * a / 255) << 16 | (g * a / 255) << 8 | (b * a / 255);
+ PREMULTIPLY8(a, 0xFF & (argb >> 16)) << 16 |
+ PREMULTIPLY8(a, 0xFF & (argb >> 8)) << 8 |
+ PREMULTIPLY8(a, 0xFF & argb);
}
}
}
@@ -2361,9 +2363,9 @@ read_spng_thumbnail(
for (size_t i = size / sizeof *data; i--; ) {
const uint8_t *unit = (const uint8_t *) &data[i];
uint32_t a = unit[3],
- b = unit[2] * a / 255,
- g = unit[1] * a / 255,
- r = unit[0] * a / 255;
+ b = PREMULTIPLY8(a, unit[2]),
+ g = PREMULTIPLY8(a, unit[1]),
+ r = PREMULTIPLY8(a, unit[0]);
data[i] = a << 24 | r << 16 | g << 8 | b;
}
} else {