mirror of
https://github.com/webmproject/libwebp.git
synced 2025-01-15 17:18:23 +01:00
make WebPCleanupTransparentArea work with argb picture
the -alpha_cleanup flag was ineffective since we switched cwebp to using ARGB input always. Original idea by David Eckel (dvdckl at gmail dot com) Change-Id: I0917a8b91ce15a43199728ff4ee2a163be443bab
This commit is contained in:
parent
5da185522b
commit
c741183c10
@ -992,7 +992,20 @@ static int is_transparent_area(const uint8_t* ptr, int stride, int size) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static WEBP_INLINE void flatten(uint8_t* ptr, int v, int stride, int size) {
|
static int is_transparent_argb_area(const uint32_t* ptr, int stride, int size) {
|
||||||
|
int y, x;
|
||||||
|
for (y = 0; y < size; ++y) {
|
||||||
|
for (x = 0; x < size; ++x) {
|
||||||
|
if (ptr[x] & 0xff000000u) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ptr += stride;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void flatten(uint8_t* ptr, int v, int stride, int size) {
|
||||||
int y;
|
int y;
|
||||||
for (y = 0; y < size; ++y) {
|
for (y = 0; y < size; ++y) {
|
||||||
memset(ptr, v, size);
|
memset(ptr, v, size);
|
||||||
@ -1000,39 +1013,63 @@ static WEBP_INLINE void flatten(uint8_t* ptr, int v, int stride, int size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void flatten_argb(uint32_t* ptr, uint32_t v, int stride, int size) {
|
||||||
|
int x, y;
|
||||||
|
for (y = 0; y < size; ++y) {
|
||||||
|
for (x = 0; x < size; ++x) ptr[x] = v;
|
||||||
|
ptr += stride;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void WebPCleanupTransparentArea(WebPPicture* pic) {
|
void WebPCleanupTransparentArea(WebPPicture* pic) {
|
||||||
int x, y, w, h;
|
int x, y, w, h;
|
||||||
const uint8_t* a_ptr;
|
|
||||||
int values[3] = { 0 };
|
|
||||||
|
|
||||||
if (pic == NULL) return;
|
if (pic == NULL) return;
|
||||||
|
|
||||||
a_ptr = pic->a;
|
|
||||||
if (a_ptr == NULL) return; // nothing to do
|
|
||||||
|
|
||||||
w = pic->width / SIZE;
|
w = pic->width / SIZE;
|
||||||
h = pic->height / SIZE;
|
h = pic->height / SIZE;
|
||||||
for (y = 0; y < h; ++y) {
|
|
||||||
int need_reset = 1;
|
// note: we ignore the left-overs on right/bottom
|
||||||
for (x = 0; x < w; ++x) {
|
if (pic->use_argb) {
|
||||||
const int off_a = (y * pic->a_stride + x) * SIZE;
|
uint32_t argb_value = 0;
|
||||||
const int off_y = (y * pic->y_stride + x) * SIZE;
|
for (y = 0; y < h; ++y) {
|
||||||
const int off_uv = (y * pic->uv_stride + x) * SIZE2;
|
int need_reset = 1;
|
||||||
if (is_transparent_area(a_ptr + off_a, pic->a_stride, SIZE)) {
|
for (x = 0; x < w; ++x) {
|
||||||
if (need_reset) {
|
const int off = (y * pic->argb_stride + x) * SIZE;
|
||||||
values[0] = pic->y[off_y];
|
if (is_transparent_argb_area(pic->argb + off, pic->argb_stride, SIZE)) {
|
||||||
values[1] = pic->u[off_uv];
|
if (need_reset) {
|
||||||
values[2] = pic->v[off_uv];
|
argb_value = pic->argb[off];
|
||||||
need_reset = 0;
|
need_reset = 0;
|
||||||
|
}
|
||||||
|
flatten_argb(pic->argb + off, argb_value, pic->argb_stride, SIZE);
|
||||||
|
} else {
|
||||||
|
need_reset = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const uint8_t* const a_ptr = pic->a;
|
||||||
|
int values[3] = { 0 };
|
||||||
|
if (a_ptr == NULL) return; // nothing to do
|
||||||
|
for (y = 0; y < h; ++y) {
|
||||||
|
int need_reset = 1;
|
||||||
|
for (x = 0; x < w; ++x) {
|
||||||
|
const int off_a = (y * pic->a_stride + x) * SIZE;
|
||||||
|
const int off_y = (y * pic->y_stride + x) * SIZE;
|
||||||
|
const int off_uv = (y * pic->uv_stride + x) * SIZE2;
|
||||||
|
if (is_transparent_area(a_ptr + off_a, pic->a_stride, SIZE)) {
|
||||||
|
if (need_reset) {
|
||||||
|
values[0] = pic->y[off_y];
|
||||||
|
values[1] = pic->u[off_uv];
|
||||||
|
values[2] = pic->v[off_uv];
|
||||||
|
need_reset = 0;
|
||||||
|
}
|
||||||
|
flatten(pic->y + off_y, values[0], pic->y_stride, SIZE);
|
||||||
|
flatten(pic->u + off_uv, values[1], pic->uv_stride, SIZE2);
|
||||||
|
flatten(pic->v + off_uv, values[2], pic->uv_stride, SIZE2);
|
||||||
|
} else {
|
||||||
|
need_reset = 1;
|
||||||
}
|
}
|
||||||
flatten(pic->y + off_y, values[0], pic->y_stride, SIZE);
|
|
||||||
flatten(pic->u + off_uv, values[1], pic->uv_stride, SIZE2);
|
|
||||||
flatten(pic->v + off_uv, values[2], pic->uv_stride, SIZE2);
|
|
||||||
} else {
|
|
||||||
need_reset = 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ignore the left-overs on right/bottom
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -459,9 +459,9 @@ WEBP_EXTERN(int) WebPPictureARGBToYUVADithered(
|
|||||||
// Returns false in case of error.
|
// Returns false in case of error.
|
||||||
WEBP_EXTERN(int) WebPPictureYUVAToARGB(WebPPicture* picture);
|
WEBP_EXTERN(int) WebPPictureYUVAToARGB(WebPPicture* picture);
|
||||||
|
|
||||||
// Helper function: given a width x height plane of YUV(A) samples
|
// Helper function: given a width x height plane of RGBA or YUV(A) samples
|
||||||
// (with stride 'stride'), clean-up the YUV samples under fully transparent
|
// clean-up the YUV or RGB samples under fully transparent area, to help
|
||||||
// area, to help compressibility (no guarantee, though).
|
// compressibility (no guarantee, though).
|
||||||
WEBP_EXTERN(void) WebPCleanupTransparentArea(WebPPicture* picture);
|
WEBP_EXTERN(void) WebPCleanupTransparentArea(WebPPicture* picture);
|
||||||
|
|
||||||
// Scan the picture 'picture' for the presence of non fully opaque alpha values.
|
// Scan the picture 'picture' for the presence of non fully opaque alpha values.
|
||||||
|
Loading…
Reference in New Issue
Block a user