Skip to content

Commit

Permalink
fix alpha on non-8bit or limited range
Browse files Browse the repository at this point in the history
  • Loading branch information
GC committed Mar 16, 2020
1 parent ff0c9da commit 806207f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 6 deletions.
64 changes: 64 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,70 @@
<string name="app_name">AndroidGlideAvifDecoder</string>
<string name="resource_root">https://raw.githubusercontent.com/link-u/avif-sample-images/master/</string>
<string-array name="resource_names">
<item>plum-blossom-large.profile0.10bpc.yuv420.alpha-full.avif</item>
<item>plum-blossom-large.profile0.10bpc.yuv420.alpha-full.monochrome.avif</item>
<item>plum-blossom-large.profile0.10bpc.yuv420.alpha-limited.avif</item>
<item>plum-blossom-large.profile0.10bpc.yuv420.alpha-limited.monochrome.avif</item>
<item>plum-blossom-large.profile0.8bpc.yuv420.alpha-full.avif</item>
<item>plum-blossom-large.profile0.8bpc.yuv420.alpha-full.monochrome.avif</item>
<item>plum-blossom-large.profile0.8bpc.yuv420.alpha-limited.avif</item>
<item>plum-blossom-large.profile0.8bpc.yuv420.alpha-limited.monochrome.avif</item>
<item>plum-blossom-large.profile1.10bpc.yuv444.alpha-full.avif</item>
<item>plum-blossom-large.profile1.10bpc.yuv444.alpha-limited.avif</item>
<item>plum-blossom-large.profile1.8bpc.yuv444.alpha-full.avif</item>
<item>plum-blossom-large.profile1.8bpc.yuv444.alpha-limited.avif</item>
<item>plum-blossom-large.profile2.10bpc.yuv422.alpha-full.avif</item>
<item>plum-blossom-large.profile2.10bpc.yuv422.alpha-full.monochrome.avif</item>
<item>plum-blossom-large.profile2.10bpc.yuv422.alpha-limited.avif</item>
<item>plum-blossom-large.profile2.10bpc.yuv422.alpha-limited.monochrome.avif</item>
<item>plum-blossom-large.profile2.12bpc.yuv420.alpha-full.avif</item>
<item>plum-blossom-large.profile2.12bpc.yuv420.alpha-full.monochrome.avif</item>
<item>plum-blossom-large.profile2.12bpc.yuv420.alpha-limited.avif</item>
<item>plum-blossom-large.profile2.12bpc.yuv420.alpha-limited.monochrome.avif</item>
<item>plum-blossom-large.profile2.12bpc.yuv422.alpha-full.avif</item>
<item>plum-blossom-large.profile2.12bpc.yuv422.alpha-full.monochrome.avif</item>
<item>plum-blossom-large.profile2.12bpc.yuv422.alpha-limited.avif</item>
<item>plum-blossom-large.profile2.12bpc.yuv422.alpha-limited.monochrome.avif</item>
<item>plum-blossom-large.profile2.12bpc.yuv444.alpha-full.avif</item>
<item>plum-blossom-large.profile2.12bpc.yuv444.alpha-full.monochrome.avif</item>
<item>plum-blossom-large.profile2.12bpc.yuv444.alpha-limited.avif</item>
<item>plum-blossom-large.profile2.12bpc.yuv444.alpha-limited.monochrome.avif</item>
<item>plum-blossom-large.profile2.8bpc.yuv422.alpha-full.avif</item>
<item>plum-blossom-large.profile2.8bpc.yuv422.alpha-full.monochrome.avif</item>
<item>plum-blossom-large.profile2.8bpc.yuv422.alpha-limited.avif</item>
<item>plum-blossom-large.profile2.8bpc.yuv422.alpha-limited.monochrome.avif</item>
<item>plum-blossom-small.profile0.10bpc.yuv420.alpha-full.avif</item>
<item>plum-blossom-small.profile0.10bpc.yuv420.alpha-full.monochrome.avif</item>
<item>plum-blossom-small.profile0.10bpc.yuv420.alpha-limited.avif</item>
<item>plum-blossom-small.profile0.10bpc.yuv420.alpha-limited.monochrome.avif</item>
<item>plum-blossom-small.profile0.8bpc.yuv420.alpha-full.avif</item>
<item>plum-blossom-small.profile0.8bpc.yuv420.alpha-full.monochrome.avif</item>
<item>plum-blossom-small.profile0.8bpc.yuv420.alpha-limited.avif</item>
<item>plum-blossom-small.profile0.8bpc.yuv420.alpha-limited.monochrome.avif</item>
<item>plum-blossom-small.profile1.10bpc.yuv444.alpha-full.avif</item>
<item>plum-blossom-small.profile1.10bpc.yuv444.alpha-limited.avif</item>
<item>plum-blossom-small.profile1.8bpc.yuv444.alpha-full.avif</item>
<item>plum-blossom-small.profile1.8bpc.yuv444.alpha-limited.avif</item>
<item>plum-blossom-small.profile2.10bpc.yuv422.alpha-full.avif</item>
<item>plum-blossom-small.profile2.10bpc.yuv422.alpha-full.monochrome.avif</item>
<item>plum-blossom-small.profile2.10bpc.yuv422.alpha-limited.avif</item>
<item>plum-blossom-small.profile2.10bpc.yuv422.alpha-limited.monochrome.avif</item>
<item>plum-blossom-small.profile2.12bpc.yuv420.alpha-full.avif</item>
<item>plum-blossom-small.profile2.12bpc.yuv420.alpha-full.monochrome.avif</item>
<item>plum-blossom-small.profile2.12bpc.yuv420.alpha-limited.avif</item>
<item>plum-blossom-small.profile2.12bpc.yuv420.alpha-limited.monochrome.avif</item>
<item>plum-blossom-small.profile2.12bpc.yuv422.alpha-full.avif</item>
<item>plum-blossom-small.profile2.12bpc.yuv422.alpha-full.monochrome.avif</item>
<item>plum-blossom-small.profile2.12bpc.yuv422.alpha-limited.avif</item>
<item>plum-blossom-small.profile2.12bpc.yuv422.alpha-limited.monochrome.avif</item>
<item>plum-blossom-small.profile2.12bpc.yuv444.alpha-full.avif</item>
<item>plum-blossom-small.profile2.12bpc.yuv444.alpha-full.monochrome.avif</item>
<item>plum-blossom-small.profile2.12bpc.yuv444.alpha-limited.avif</item>
<item>plum-blossom-small.profile2.12bpc.yuv444.alpha-limited.monochrome.avif</item>
<item>plum-blossom-small.profile2.8bpc.yuv422.alpha-full.avif</item>
<item>plum-blossom-small.profile2.8bpc.yuv422.alpha-full.monochrome.avif</item>
<item>plum-blossom-small.profile2.8bpc.yuv422.alpha-limited.avif</item>
<item>plum-blossom-small.profile2.8bpc.yuv422.alpha-limited.monochrome.avif</item>
<item>fox.png</item>
<item>fox.profile0.10bpc.yuv420.avif</item>
<item>fox.profile0.10bpc.yuv420.monochrome.avif</item>
Expand Down
46 changes: 41 additions & 5 deletions glideavif/src/main/cpp/avif_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <avif/avif.h>
#include <jni_util.hpp>
#include <libyuv/convert_argb.h>
#include <algorithm>

#include "common.hpp"
#include "my_bitmap.hpp"
Expand All @@ -25,6 +26,26 @@ void copyGrayscalePixels(JNIEnv *env, avifImage *im, std::vector<uint8_t> &rgbaL
}
}

template<typename T>
void
copyAlphaPixels(JNIEnv *env, avifImage *im, bool limitedRange, std::vector<uint8_t> &rgbaList) {
uint32_t w = im->width * sizeof(T) / sizeof(uint8_t);
if (im->alphaRowBytes < w) {
throwException(env, "invalid alpha bytes");
}

for (int i = 0; i < im->height; ++i) {
auto p = (T *) (im->alphaPlane + (i * im->alphaRowBytes));
for (int j = 0; j < im->width; ++j) {
uint32_t c0 = p[j] >> (im->depth - 8);
if (limitedRange) {
c0 = std::max<uint32_t>(std::min<uint32_t>((c0 - 16) * 255 / (235 - 16), 255), 0);
}
rgbaList.at(4 * (i * im->width + j) + 3) = (uint8_t) c0;
}
}
}

bool isGrayscale(avifImage *im) {
return !(im->yuvRowBytes[1] && im->yuvRowBytes[2]);
}
Expand Down Expand Up @@ -156,6 +177,7 @@ jobject decodeAvif(JNIEnv *env, const uint8_t *sourceData, int sourceDataLength)
}
memcpy(rgbaList.data(), rgb->pixels, rgb->rowBytes * rgb->height);

// On non-alpha image, A of RGBA is decoded 0x00
if (!im->alphaPlane) {
for (int i = 0; i < im->height; ++i) {
for (int j = 0; j < im->width; ++j) {
Expand All @@ -167,11 +189,25 @@ jobject decodeAvif(JNIEnv *env, const uint8_t *sourceData, int sourceDataLength)
}();

if (im->alphaPlane) {
for (int i = 0; i < im->height; ++i) {
for (int j = 0; j < im->width; ++j) {
uint8_t c = im->alphaPlane[i * im->alphaRowBytes + j];
rgbaList.at(4 * (i * im->width + j) + 3) = c;
}
bool limitedRange = false;
switch (im->alphaRange) {
case AVIF_RANGE_LIMITED:
limitedRange = true;
break;
case AVIF_RANGE_FULL:
limitedRange = false;
break;
}
switch (im->depth) {
case 8:
copyAlphaPixels<uint8_t>(env, im, limitedRange, rgbaList);
break;
case 10:
case 12:
copyAlphaPixels<uint16_t>(env, im, limitedRange, rgbaList);
break;
default:
throwException(env, "unknown color depth");
}
}

Expand Down
2 changes: 1 addition & 1 deletion glideavif/src/main/cpp/libavif

0 comments on commit 806207f

Please sign in to comment.