Path: blob/master/thirdparty/libwebp/src/enc/picture_tools_enc.c
9913 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"1718//------------------------------------------------------------------------------19// Helper: clean up fully transparent area to help compressibility.2021#define SIZE 822#define SIZE2 (SIZE / 2)23static int IsTransparentARGBArea(const uint32_t* ptr, int stride, int size) {24int y, x;25for (y = 0; y < size; ++y) {26for (x = 0; x < size; ++x) {27if (ptr[x] & 0xff000000u) {28return 0;29}30}31ptr += stride;32}33return 1;34}3536static void Flatten(uint8_t* ptr, int v, int stride, int size) {37int y;38for (y = 0; y < size; ++y) {39memset(ptr, v, size);40ptr += stride;41}42}4344static void FlattenARGB(uint32_t* ptr, uint32_t v, int stride, int size) {45int x, y;46for (y = 0; y < size; ++y) {47for (x = 0; x < size; ++x) ptr[x] = v;48ptr += stride;49}50}5152// Smoothen the luma components of transparent pixels. Return true if the whole53// block is transparent.54static int SmoothenBlock(const uint8_t* a_ptr, int a_stride, uint8_t* y_ptr,55int y_stride, int width, int height) {56int sum = 0, count = 0;57int x, y;58const uint8_t* alpha_ptr = a_ptr;59uint8_t* luma_ptr = y_ptr;60for (y = 0; y < height; ++y) {61for (x = 0; x < width; ++x) {62if (alpha_ptr[x] != 0) {63++count;64sum += luma_ptr[x];65}66}67alpha_ptr += a_stride;68luma_ptr += y_stride;69}70if (count > 0 && count < width * height) {71const uint8_t avg_u8 = (uint8_t)(sum / count);72alpha_ptr = a_ptr;73luma_ptr = y_ptr;74for (y = 0; y < height; ++y) {75for (x = 0; x < width; ++x) {76if (alpha_ptr[x] == 0) luma_ptr[x] = avg_u8;77}78alpha_ptr += a_stride;79luma_ptr += y_stride;80}81}82return (count == 0);83}8485void WebPReplaceTransparentPixels(WebPPicture* const pic, uint32_t color) {86if (pic != NULL && pic->use_argb) {87int y = pic->height;88uint32_t* argb = pic->argb;89color &= 0xffffffu; // force alpha=090WebPInitAlphaProcessing();91while (y-- > 0) {92WebPAlphaReplace(argb, pic->width, color);93argb += pic->argb_stride;94}95}96}9798void WebPCleanupTransparentArea(WebPPicture* pic) {99int x, y, w, h;100if (pic == NULL) return;101w = pic->width / SIZE;102h = pic->height / SIZE;103104// note: we ignore the left-overs on right/bottom, except for SmoothenBlock().105if (pic->use_argb) {106uint32_t argb_value = 0;107for (y = 0; y < h; ++y) {108int need_reset = 1;109for (x = 0; x < w; ++x) {110const int off = (y * pic->argb_stride + x) * SIZE;111if (IsTransparentARGBArea(pic->argb + off, pic->argb_stride, SIZE)) {112if (need_reset) {113argb_value = pic->argb[off];114need_reset = 0;115}116FlattenARGB(pic->argb + off, argb_value, pic->argb_stride, SIZE);117} else {118need_reset = 1;119}120}121}122} else {123const int width = pic->width;124const int height = pic->height;125const int y_stride = pic->y_stride;126const int uv_stride = pic->uv_stride;127const int a_stride = pic->a_stride;128uint8_t* y_ptr = pic->y;129uint8_t* u_ptr = pic->u;130uint8_t* v_ptr = pic->v;131const uint8_t* a_ptr = pic->a;132int values[3] = { 0 };133if (a_ptr == NULL || y_ptr == NULL || u_ptr == NULL || v_ptr == NULL) {134return;135}136for (y = 0; y + SIZE <= height; y += SIZE) {137int need_reset = 1;138for (x = 0; x + SIZE <= width; x += SIZE) {139if (SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,140SIZE, SIZE)) {141if (need_reset) {142values[0] = y_ptr[x];143values[1] = u_ptr[x >> 1];144values[2] = v_ptr[x >> 1];145need_reset = 0;146}147Flatten(y_ptr + x, values[0], y_stride, SIZE);148Flatten(u_ptr + (x >> 1), values[1], uv_stride, SIZE2);149Flatten(v_ptr + (x >> 1), values[2], uv_stride, SIZE2);150} else {151need_reset = 1;152}153}154if (x < width) {155SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,156width - x, SIZE);157}158a_ptr += SIZE * a_stride;159y_ptr += SIZE * y_stride;160u_ptr += SIZE2 * uv_stride;161v_ptr += SIZE2 * uv_stride;162}163if (y < height) {164const int sub_height = height - y;165for (x = 0; x + SIZE <= width; x += SIZE) {166SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,167SIZE, sub_height);168}169if (x < width) {170SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,171width - x, sub_height);172}173}174}175}176177#undef SIZE178#undef SIZE2179180//------------------------------------------------------------------------------181// Blend color and remove transparency info182183#define BLEND(V0, V1, ALPHA) \184((((V0) * (255 - (ALPHA)) + (V1) * (ALPHA)) * 0x101 + 256) >> 16)185#define BLEND_10BIT(V0, V1, ALPHA) \186((((V0) * (1020 - (ALPHA)) + (V1) * (ALPHA)) * 0x101 + 1024) >> 18)187188static WEBP_INLINE uint32_t MakeARGB32(int r, int g, int b) {189return (0xff000000u | (r << 16) | (g << 8) | b);190}191192void WebPBlendAlpha(WebPPicture* picture, uint32_t background_rgb) {193const int red = (background_rgb >> 16) & 0xff;194const int green = (background_rgb >> 8) & 0xff;195const int blue = (background_rgb >> 0) & 0xff;196int x, y;197if (picture == NULL) return;198if (!picture->use_argb) {199// omit last pixel during u/v loop200const int uv_width = (picture->width >> 1);201const int Y0 = VP8RGBToY(red, green, blue, YUV_HALF);202// VP8RGBToU/V expects the u/v values summed over four pixels203const int U0 = VP8RGBToU(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);204const int V0 = VP8RGBToV(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);205const int has_alpha = picture->colorspace & WEBP_CSP_ALPHA_BIT;206uint8_t* y_ptr = picture->y;207uint8_t* u_ptr = picture->u;208uint8_t* v_ptr = picture->v;209uint8_t* a_ptr = picture->a;210if (!has_alpha || a_ptr == NULL) return; // nothing to do211for (y = 0; y < picture->height; ++y) {212// Luma blending213for (x = 0; x < picture->width; ++x) {214const uint8_t alpha = a_ptr[x];215if (alpha < 0xff) {216y_ptr[x] = BLEND(Y0, y_ptr[x], alpha);217}218}219// Chroma blending every even line220if ((y & 1) == 0) {221uint8_t* const a_ptr2 =222(y + 1 == picture->height) ? a_ptr : a_ptr + picture->a_stride;223for (x = 0; x < uv_width; ++x) {224// Average four alpha values into a single blending weight.225// TODO(skal): might lead to visible contouring. Can we do better?226const uint32_t alpha =227a_ptr[2 * x + 0] + a_ptr[2 * x + 1] +228a_ptr2[2 * x + 0] + a_ptr2[2 * x + 1];229u_ptr[x] = BLEND_10BIT(U0, u_ptr[x], alpha);230v_ptr[x] = BLEND_10BIT(V0, v_ptr[x], alpha);231}232if (picture->width & 1) { // rightmost pixel233const uint32_t alpha = 2 * (a_ptr[2 * x + 0] + a_ptr2[2 * x + 0]);234u_ptr[x] = BLEND_10BIT(U0, u_ptr[x], alpha);235v_ptr[x] = BLEND_10BIT(V0, v_ptr[x], alpha);236}237} else {238u_ptr += picture->uv_stride;239v_ptr += picture->uv_stride;240}241memset(a_ptr, 0xff, picture->width); // reset alpha value to opaque242a_ptr += picture->a_stride;243y_ptr += picture->y_stride;244}245} else {246uint32_t* argb = picture->argb;247const uint32_t background = MakeARGB32(red, green, blue);248for (y = 0; y < picture->height; ++y) {249for (x = 0; x < picture->width; ++x) {250const int alpha = (argb[x] >> 24) & 0xff;251if (alpha != 0xff) {252if (alpha > 0) {253int r = (argb[x] >> 16) & 0xff;254int g = (argb[x] >> 8) & 0xff;255int b = (argb[x] >> 0) & 0xff;256r = BLEND(red, r, alpha);257g = BLEND(green, g, alpha);258b = BLEND(blue, b, alpha);259argb[x] = MakeARGB32(r, g, b);260} else {261argb[x] = background;262}263}264}265argb += picture->argb_stride;266}267}268}269270#undef BLEND271#undef BLEND_10BIT272273//------------------------------------------------------------------------------274275276