Path: blob/master/3rdparty/libwebp/src/enc/picture_tools_enc.c
16349 views
// Copyright 2014 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// WebPPicture tools: alpha handling, etc.10//11// Author: Skal ([email protected])1213#include <assert.h>1415#include "src/enc/vp8i_enc.h"16#include "src/dsp/yuv.h"1718static WEBP_INLINE uint32_t MakeARGB32(int r, int g, int b) {19return (0xff000000u | (r << 16) | (g << 8) | b);20}2122//------------------------------------------------------------------------------23// Helper: clean up fully transparent area to help compressibility.2425#define SIZE 826#define SIZE2 (SIZE / 2)27static int IsTransparentARGBArea(const uint32_t* ptr, int stride, int size) {28int y, x;29for (y = 0; y < size; ++y) {30for (x = 0; x < size; ++x) {31if (ptr[x] & 0xff000000u) {32return 0;33}34}35ptr += stride;36}37return 1;38}3940static void Flatten(uint8_t* ptr, int v, int stride, int size) {41int y;42for (y = 0; y < size; ++y) {43memset(ptr, v, size);44ptr += stride;45}46}4748static void FlattenARGB(uint32_t* ptr, uint32_t v, int stride, int size) {49int x, y;50for (y = 0; y < size; ++y) {51for (x = 0; x < size; ++x) ptr[x] = v;52ptr += stride;53}54}5556// Smoothen the luma components of transparent pixels. Return true if the whole57// block is transparent.58static int SmoothenBlock(const uint8_t* a_ptr, int a_stride, uint8_t* y_ptr,59int y_stride, int width, int height) {60int sum = 0, count = 0;61int x, y;62const uint8_t* alpha_ptr = a_ptr;63uint8_t* luma_ptr = y_ptr;64for (y = 0; y < height; ++y) {65for (x = 0; x < width; ++x) {66if (alpha_ptr[x] != 0) {67++count;68sum += luma_ptr[x];69}70}71alpha_ptr += a_stride;72luma_ptr += y_stride;73}74if (count > 0 && count < width * height) {75const uint8_t avg_u8 = (uint8_t)(sum / count);76alpha_ptr = a_ptr;77luma_ptr = y_ptr;78for (y = 0; y < height; ++y) {79for (x = 0; x < width; ++x) {80if (alpha_ptr[x] == 0) luma_ptr[x] = avg_u8;81}82alpha_ptr += a_stride;83luma_ptr += y_stride;84}85}86return (count == 0);87}8889void WebPCleanupTransparentArea(WebPPicture* pic) {90int x, y, w, h;91if (pic == NULL) return;92w = pic->width / SIZE;93h = pic->height / SIZE;9495// note: we ignore the left-overs on right/bottom, except for SmoothenBlock().96if (pic->use_argb) {97uint32_t argb_value = 0;98for (y = 0; y < h; ++y) {99int need_reset = 1;100for (x = 0; x < w; ++x) {101const int off = (y * pic->argb_stride + x) * SIZE;102if (IsTransparentARGBArea(pic->argb + off, pic->argb_stride, SIZE)) {103if (need_reset) {104argb_value = pic->argb[off];105need_reset = 0;106}107FlattenARGB(pic->argb + off, argb_value, pic->argb_stride, SIZE);108} else {109need_reset = 1;110}111}112}113} else {114const int width = pic->width;115const int height = pic->height;116const int y_stride = pic->y_stride;117const int uv_stride = pic->uv_stride;118const int a_stride = pic->a_stride;119uint8_t* y_ptr = pic->y;120uint8_t* u_ptr = pic->u;121uint8_t* v_ptr = pic->v;122const uint8_t* a_ptr = pic->a;123int values[3] = { 0 };124if (a_ptr == NULL || y_ptr == NULL || u_ptr == NULL || v_ptr == NULL) {125return;126}127for (y = 0; y + SIZE <= height; y += SIZE) {128int need_reset = 1;129for (x = 0; x + SIZE <= width; x += SIZE) {130if (SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,131SIZE, SIZE)) {132if (need_reset) {133values[0] = y_ptr[x];134values[1] = u_ptr[x >> 1];135values[2] = v_ptr[x >> 1];136need_reset = 0;137}138Flatten(y_ptr + x, values[0], y_stride, SIZE);139Flatten(u_ptr + (x >> 1), values[1], uv_stride, SIZE2);140Flatten(v_ptr + (x >> 1), values[2], uv_stride, SIZE2);141} else {142need_reset = 1;143}144}145if (x < width) {146SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,147width - x, SIZE);148}149a_ptr += SIZE * a_stride;150y_ptr += SIZE * y_stride;151u_ptr += SIZE2 * uv_stride;152v_ptr += SIZE2 * uv_stride;153}154if (y < height) {155const int sub_height = height - y;156for (x = 0; x + SIZE <= width; x += SIZE) {157SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,158SIZE, sub_height);159}160if (x < width) {161SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,162width - x, sub_height);163}164}165}166}167168#undef SIZE169#undef SIZE2170171void WebPCleanupTransparentAreaLossless(WebPPicture* const pic) {172int x, y, w, h;173uint32_t* argb;174assert(pic != NULL && pic->use_argb);175w = pic->width;176h = pic->height;177argb = pic->argb;178179for (y = 0; y < h; ++y) {180for (x = 0; x < w; ++x) {181if ((argb[x] & 0xff000000) == 0) {182argb[x] = 0x00000000;183}184}185argb += pic->argb_stride;186}187}188189//------------------------------------------------------------------------------190// Blend color and remove transparency info191192#define BLEND(V0, V1, ALPHA) \193((((V0) * (255 - (ALPHA)) + (V1) * (ALPHA)) * 0x101 + 256) >> 16)194#define BLEND_10BIT(V0, V1, ALPHA) \195((((V0) * (1020 - (ALPHA)) + (V1) * (ALPHA)) * 0x101 + 1024) >> 18)196197void WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb) {198const int red = (background_rgb >> 16) & 0xff;199const int green = (background_rgb >> 8) & 0xff;200const int blue = (background_rgb >> 0) & 0xff;201int x, y;202if (pic == NULL) return;203if (!pic->use_argb) {204const int uv_width = (pic->width >> 1); // omit last pixel during u/v loop205const int Y0 = VP8RGBToY(red, green, blue, YUV_HALF);206// VP8RGBToU/V expects the u/v values summed over four pixels207const int U0 = VP8RGBToU(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);208const int V0 = VP8RGBToV(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);209const int has_alpha = pic->colorspace & WEBP_CSP_ALPHA_BIT;210if (!has_alpha || pic->a == NULL) return; // nothing to do211for (y = 0; y < pic->height; ++y) {212// Luma blending213uint8_t* const y_ptr = pic->y + y * pic->y_stride;214uint8_t* const a_ptr = pic->a + y * pic->a_stride;215for (x = 0; x < pic->width; ++x) {216const int alpha = a_ptr[x];217if (alpha < 0xff) {218y_ptr[x] = BLEND(Y0, y_ptr[x], a_ptr[x]);219}220}221// Chroma blending every even line222if ((y & 1) == 0) {223uint8_t* const u = pic->u + (y >> 1) * pic->uv_stride;224uint8_t* const v = pic->v + (y >> 1) * pic->uv_stride;225uint8_t* const a_ptr2 =226(y + 1 == pic->height) ? a_ptr : a_ptr + pic->a_stride;227for (x = 0; x < uv_width; ++x) {228// Average four alpha values into a single blending weight.229// TODO(skal): might lead to visible contouring. Can we do better?230const int alpha =231a_ptr[2 * x + 0] + a_ptr[2 * x + 1] +232a_ptr2[2 * x + 0] + a_ptr2[2 * x + 1];233u[x] = BLEND_10BIT(U0, u[x], alpha);234v[x] = BLEND_10BIT(V0, v[x], alpha);235}236if (pic->width & 1) { // rightmost pixel237const int alpha = 2 * (a_ptr[2 * x + 0] + a_ptr2[2 * x + 0]);238u[x] = BLEND_10BIT(U0, u[x], alpha);239v[x] = BLEND_10BIT(V0, v[x], alpha);240}241}242memset(a_ptr, 0xff, pic->width);243}244} else {245uint32_t* argb = pic->argb;246const uint32_t background = MakeARGB32(red, green, blue);247for (y = 0; y < pic->height; ++y) {248for (x = 0; x < pic->width; ++x) {249const int alpha = (argb[x] >> 24) & 0xff;250if (alpha != 0xff) {251if (alpha > 0) {252int r = (argb[x] >> 16) & 0xff;253int g = (argb[x] >> 8) & 0xff;254int b = (argb[x] >> 0) & 0xff;255r = BLEND(red, r, alpha);256g = BLEND(green, g, alpha);257b = BLEND(blue, b, alpha);258argb[x] = MakeARGB32(r, g, b);259} else {260argb[x] = background;261}262}263}264argb += pic->argb_stride;265}266}267}268269#undef BLEND270#undef BLEND_10BIT271272//------------------------------------------------------------------------------273274275