aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeonardo Hernández Hernández <leohdz172@proton.me>2024-06-14 14:04:03 -0600
committerLeonardo Hernández Hernández <leohdz172@proton.me>2024-06-15 11:53:41 -0600
commit5fbfd76293e7ce2d189e6742f0a957e538d76036 (patch)
tree05b0d336445c1c45969e5bbd893b09306c832f24
parentd9002dc181c95aa6f3358776420beda226ab2e2a (diff)
downloadwbg-5fbfd76293e7ce2d189e6742f0a957e538d76036.tar.gz
jxl: do alpha pre-multiplication manually
some images does not render correctly if we rely in libjxl doing the pre-multiplication
-rw-r--r--jxl.c25
1 files changed, 22 insertions, 3 deletions
diff --git a/jxl.c b/jxl.c
index 6c1ac4d..57dca49 100644
--- a/jxl.c
+++ b/jxl.c
@@ -68,9 +68,6 @@ jxl_load(FILE *fp, const char *path)
JxlDecoderSetParallelRunner(decoder, JxlResizableParallelRunner, runner);
#endif
- /* pixman expects premultiplied alpha */
- JxlDecoderSetUnpremultiplyAlpha(decoder, JXL_FALSE);
-
JxlDecoderSubscribeEvents(decoder, JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE);
JxlDecoderSetInput(decoder, file_data, file_size);
@@ -133,6 +130,28 @@ jxl_load(FILE *fp, const char *path)
}
}
+ for (uint32_t *abgr = (uint32_t *)image;
+ abgr < (uint32_t *)(image + (size_t)width * (size_t)height * 4);
+ abgr++) {
+ uint8_t alpha = (*abgr >> 24) & 0xff;
+ uint8_t red = (*abgr >> 16) & 0xff;
+ uint8_t green = (*abgr >> 8) & 0xff;
+ uint8_t blue = (*abgr >> 0) & 0xff;
+
+ if (alpha == 0xff)
+ continue;
+
+ if (alpha == 0x00)
+ blue = green = red = 0x00;
+ else {
+ blue = blue * alpha / 0xff;
+ green = green * alpha / 0xff;
+ red = red * alpha / 0xff;
+ }
+
+ *abgr = (uint32_t)alpha << 24 | red << 16 | green << 8 | blue;
+ }
+
pix = pixman_image_create_bits_no_clear(format, width, height,
(uint32_t *)image, stride);
ok = pix != NULL;