gif2webp: Improved compression for lossy animated WebP

We reduce transparency by turning some transparent pixels into
corresponding RGB values from previous canvas.

This improves compression by about 23%.

Change-Id: I02d70a43a1d0906ac09a7e2dc510be3b2d38f593
This commit is contained in:
Urvang Joshi 2013-09-30 16:48:39 -07:00
parent fb887f7fe6
commit 606c4304c4
3 changed files with 29 additions and 0 deletions

View File

@ -166,6 +166,11 @@ static int OptimizeAndEncodeFrame(
// Update prev_canvas by simply copying from 'curr'. // Update prev_canvas by simply copying from 'curr'.
WebPUtilCopyPixels(curr, prev_canvas); WebPUtilCopyPixels(curr, prev_canvas);
} else { } else {
if (!config->lossless) {
// For lossy compression, it's better to replace transparent pixels of
// 'curr' with actual RGB values, whenever possible.
WebPUtilReduceTransparency(prev_canvas, &rect, curr);
}
if (!WebPFrameCacheShouldTryKeyFrame(cache)) { if (!WebPFrameCacheShouldTryKeyFrame(cache)) {
// Add this as a frame rectangle. // Add this as a frame rectangle.
if (!WebPFrameCacheAddFrame(cache, config, info, sub_image, NULL, NULL)) { if (!WebPFrameCacheAddFrame(cache, config, info, sub_image, NULL, NULL)) {

View File

@ -287,6 +287,25 @@ void WebPUtilBlendPixels(const WebPPicture* const src,
} }
} }
void WebPUtilReduceTransparency(const WebPPicture* const src,
const WebPFrameRect* const rect,
WebPPicture* const dst) {
int j;
assert(src->width == dst->width && src->height == dst->height);
for (j = rect->y_offset; j < rect->y_offset + rect->height; ++j) {
int i;
for (i = rect->x_offset; i < rect->x_offset + rect->width; ++i) {
const uint32_t src_pixel = src->argb[j * src->argb_stride + i];
const int src_alpha = src_pixel >> 24;
const uint32_t dst_pixel = dst->argb[j * dst->argb_stride + i];
const int dst_alpha = dst_pixel >> 24;
if (dst_alpha == 0 && src_alpha == 0xff) {
dst->argb[j * dst->argb_stride + i] = src_pixel;
}
}
}
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Key frame related utilities. // Key frame related utilities.

View File

@ -86,6 +86,11 @@ void WebPUtilBlendPixels(const struct WebPPicture* const src,
const WebPFrameRect* const src_rect, const WebPFrameRect* const src_rect,
struct WebPPicture* const dst); struct WebPPicture* const dst);
// Replace transparent pixels within 'dst_rect' of 'dst' by those in the 'src'.
void WebPUtilReduceTransparency(const struct WebPPicture* const src,
const WebPFrameRect* const dst_rect,
struct WebPPicture* const dst);
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Key frame related. // Key frame related.