Fix HDR image support in AVIF decoder

This commit is contained in:
Surma
2020-02-05 17:12:03 -08:00
committed by Ingvar Stepanyan
parent 02807aab32
commit 409df481db
4 changed files with 40 additions and 20 deletions

View File

@ -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()`

View File

@ -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;
}
}
}
}

Binary file not shown.

View File

@ -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