Skip to content

Make images smaller using best-in-class codecs, right in the browser.

JavaScript

Fix HDR image support in AVIF decoder

Surma409df481db8f
diff --git a/codecs/avif_dec/README.md b/codecs/avif_dec/README.md
index 4ec8ef7d..ebebeeaf 100644
--- a/codecs/avif_dec/README.md
+++ b/codecs/avif_dec/README.md
@@ -1,7 +1,7 @@
-# WebP decoder
+# AVIF decoder
-- Source: <https://github.com/webmproject/libwebp>
-- Version: v1.0.2
+- Source: <https://github.com/AOMediaCodec/libavif>
+- Version: v0.5.4
## Example
@@ -9,13 +9,9 @@ See `example.html`
## API
-### `int version()`
-
-Returns the version of libwebp as a number. va.b.c is encoded as 0x0a0b0c
-
### `RawImage decode(std::string buffer)`
-Decodes the given webp buffer into raw RGBA. `RawImage` is a class with 3 fields: `buffer`, `width`, and `height`.
+Decodes the given avif buffer into raw RGBA. `RawImage` is a class with 3 fields: `buffer`, `width`, and `height`.
### `void free_result()`
diff --git a/codecs/avif_dec/avif_dec.cpp b/codecs/avif_dec/avif_dec.cpp
index 24ddbae6..05d9a349 100644
--- a/codecs/avif_dec/avif_dec.cpp
+++ b/codecs/avif_dec/avif_dec.cpp
@@ -56,16 +56,40 @@ RawImage decode(std::string avifimage) {
// ... image->rgbPlanes;
// ... image->rgbRowBytes;
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int pixelOffset = y * image->width + x;
- result[pixelOffset * 4 + 0] = image->rgbPlanes[0][pixelOffset];
- result[pixelOffset * 4 + 1] = image->rgbPlanes[1][pixelOffset];
- result[pixelOffset * 4 + 2] = image->rgbPlanes[2][pixelOffset];
- if (image->alphaPlane) {
- result[pixelOffset * 4 + 3] = image->alphaPlane[pixelOffset];
- } else {
- result[pixelOffset * 4 + 3] = 255;
+ if (image->depth > 8) {
+ uint16_t depthDivistor = 1 << (image->depth - 8);
+ // Plane ptrs are uint16_t*
+ for (int y = 0; y < height; y++) {
+ for (int x = 0; x < width; x++) {
+ int pixelOffset = y * image->width + x;
+ result[pixelOffset * 4 + 0] = (uint8_t)(
+ ((uint16_t *)(image->rgbPlanes[0]))[pixelOffset] / depthDivistor);
+ result[pixelOffset * 4 + 1] = (uint8_t)(
+ ((uint16_t *)(image->rgbPlanes[1]))[pixelOffset] / depthDivistor);
+ result[pixelOffset * 4 + 2] = (uint8_t)(
+ ((uint16_t *)(image->rgbPlanes[2]))[pixelOffset] / depthDivistor);
+ if (image->alphaPlane) {
+ result[pixelOffset * 4 + 3] = (uint8_t)(
+ ((uint16_t *)(image->alphaPlane))[pixelOffset] / depthDivistor);
+ } else {
+ result[pixelOffset * 4 + 3] = 255;
+ }
+ }
+ }
+
+ } else {
+ // Plane ptrs are uint8_t*
+ for (int y = 0; y < height; y++) {
+ for (int x = 0; x < width; x++) {
+ int pixelOffset = y * image->width + x;
+ result[pixelOffset * 4 + 0] = image->rgbPlanes[0][pixelOffset];
+ result[pixelOffset * 4 + 1] = image->rgbPlanes[1][pixelOffset];
+ result[pixelOffset * 4 + 2] = image->rgbPlanes[2][pixelOffset];
+ if (image->alphaPlane) {
+ result[pixelOffset * 4 + 3] = image->alphaPlane[pixelOffset];
+ } else {
+ result[pixelOffset * 4 + 3] = 255;
+ }
}
}
}
diff --git a/codecs/avif_dec/avif_dec.wasm b/codecs/avif_dec/avif_dec.wasm
index 6f027311..e3787338 100644
Binary files a/codecs/avif_dec/avif_dec.wasm and b/codecs/avif_dec/avif_dec.wasm differ
diff --git a/codecs/avif_dec/build.sh b/codecs/avif_dec/build.sh
index e1894d6a..58a774bb 100755
--- a/codecs/avif_dec/build.sh
+++ b/codecs/avif_dec/build.sh
@@ -10,7 +10,7 @@ export CPPFLAGS="${OPTIMIZE}"
echo "============================================="
echo "Compiling libaom"
echo "============================================="
-(
+test -n "$SKIP_LIBAOM" || (
cd node_modules/libavif/ext
test -d aom || git clone -b v1.0.0-errata1-avif --depth 1 https://aomedia.googlesource.com/aom aom
@@ -40,7 +40,7 @@ echo "============================================="
echo "============================================="
echo "Compiling libavif"
echo "============================================="
-(
+test -n "$SKIP_LIBAVIF" || (
cd node_modules/libavif
mkdir -p build
cd build