Path: blob/master/thirdparty/libwebp/src/enc/picture_tools_enc.c
21733 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>14#include <stddef.h>15#include <string.h>1617#include "src/dsp/dsp.h"18#include "src/dsp/yuv.h"19#include "src/enc/vp8i_enc.h"20#include "src/webp/encode.h"21#include "src/webp/types.h"2223//------------------------------------------------------------------------------24// Helper: clean up fully transparent area to help compressibility.2526#define SIZE 827#define SIZE2 (SIZE / 2)28static int IsTransparentARGBArea(const uint32_t* ptr, int stride, int size) {29int y, x;30for (y = 0; y < size; ++y) {31for (x = 0; x < size; ++x) {32if (ptr[x] & 0xff000000u) {33return 0;34}35}36ptr += stride;37}38return 1;39}4041static void Flatten(uint8_t* ptr, int v, int stride, int size) {42int y;43for (y = 0; y < size; ++y) {44memset(ptr, v, size);45ptr += stride;46}47}4849static void FlattenARGB(uint32_t* ptr, uint32_t v, int stride, int size) {50int x, y;51for (y = 0; y < size; ++y) {52for (x = 0; x < size; ++x) ptr[x] = v;53ptr += stride;54}55}5657// Smoothen the luma components of transparent pixels. Return true if the whole58// block is transparent.59static int SmoothenBlock(const uint8_t* a_ptr, int a_stride, uint8_t* y_ptr,60int y_stride, int width, int height) {61int sum = 0, count = 0;62int x, y;63const uint8_t* alpha_ptr = a_ptr;64uint8_t* luma_ptr = y_ptr;65for (y = 0; y < height; ++y) {66for (x = 0; x < width; ++x) {67if (alpha_ptr[x] != 0) {68++count;69sum += luma_ptr[x];70}71}72alpha_ptr += a_stride;73luma_ptr += y_stride;74}75if (count > 0 && count < width * height) {76const uint8_t avg_u8 = (uint8_t)(sum / count);77alpha_ptr = a_ptr;78luma_ptr = y_ptr;79for (y = 0; y < height; ++y) {80for (x = 0; x < width; ++x) {81if (alpha_ptr[x] == 0) luma_ptr[x] = avg_u8;82}83alpha_ptr += a_stride;84luma_ptr += y_stride;85}86}87return (count == 0);88}8990void WebPReplaceTransparentPixels(WebPPicture* const pic, uint32_t color) {91if (pic != NULL && pic->use_argb) {92int y = pic->height;93uint32_t* argb = pic->argb;94color &= 0xffffffu; // force alpha=095WebPInitAlphaProcessing();96while (y-- > 0) {97WebPAlphaReplace(argb, pic->width, color);98argb += pic->argb_stride;99}100}101}102103void WebPCleanupTransparentArea(WebPPicture* pic) {104int x, y, w, h;105if (pic == NULL) return;106w = pic->width / SIZE;107h = pic->height / SIZE;108109// note: we ignore the left-overs on right/bottom, except for SmoothenBlock().110if (pic->use_argb) {111uint32_t argb_value = 0;112for (y = 0; y < h; ++y) {113int need_reset = 1;114for (x = 0; x < w; ++x) {115const int off = (y * pic->argb_stride + x) * SIZE;116if (IsTransparentARGBArea(pic->argb + off, pic->argb_stride, SIZE)) {117if (need_reset) {118argb_value = pic->argb[off];119need_reset = 0;120}121FlattenARGB(pic->argb + off, argb_value, pic->argb_stride, SIZE);122} else {123need_reset = 1;124}125}126}127} else {128const int width = pic->width;129const int height = pic->height;130const int y_stride = pic->y_stride;131const int uv_stride = pic->uv_stride;132const int a_stride = pic->a_stride;133uint8_t* y_ptr = pic->y;134uint8_t* u_ptr = pic->u;135uint8_t* v_ptr = pic->v;136const uint8_t* a_ptr = pic->a;137int values[3] = { 0 };138if (a_ptr == NULL || y_ptr == NULL || u_ptr == NULL || v_ptr == NULL) {139return;140}141for (y = 0; y + SIZE <= height; y += SIZE) {142int need_reset = 1;143for (x = 0; x + SIZE <= width; x += SIZE) {144if (SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,145SIZE, SIZE)) {146if (need_reset) {147values[0] = y_ptr[x];148values[1] = u_ptr[x >> 1];149values[2] = v_ptr[x >> 1];150need_reset = 0;151}152Flatten(y_ptr + x, values[0], y_stride, SIZE);153Flatten(u_ptr + (x >> 1), values[1], uv_stride, SIZE2);154Flatten(v_ptr + (x >> 1), values[2], uv_stride, SIZE2);155} else {156need_reset = 1;157}158}159if (x < width) {160SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,161width - x, SIZE);162}163a_ptr += SIZE * a_stride;164y_ptr += SIZE * y_stride;165u_ptr += SIZE2 * uv_stride;166v_ptr += SIZE2 * uv_stride;167}168if (y < height) {169const int sub_height = height - y;170for (x = 0; x + SIZE <= width; x += SIZE) {171SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,172SIZE, sub_height);173}174if (x < width) {175SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,176width - x, sub_height);177}178}179}180}181182#undef SIZE183#undef SIZE2184185//------------------------------------------------------------------------------186// Blend color and remove transparency info187188#define BLEND(V0, V1, ALPHA) \189((((V0) * (255 - (ALPHA)) + (V1) * (ALPHA)) * 0x101 + 256) >> 16)190#define BLEND_10BIT(V0, V1, ALPHA) \191((((V0) * (1020 - (ALPHA)) + (V1) * (ALPHA)) * 0x101 + 1024) >> 18)192193static WEBP_INLINE uint32_t MakeARGB32(int r, int g, int b) {194return (0xff000000u | (r << 16) | (g << 8) | b);195}196197void WebPBlendAlpha(WebPPicture* picture, 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 (picture == NULL) return;203if (!picture->use_argb) {204// omit last pixel during u/v loop205const int uv_width = (picture->width >> 1);206const int Y0 = VP8RGBToY(red, green, blue, YUV_HALF);207// VP8RGBToU/V expects the u/v values summed over four pixels208const int U0 = VP8RGBToU(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);209const int V0 = VP8RGBToV(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);210const int has_alpha = picture->colorspace & WEBP_CSP_ALPHA_BIT;211uint8_t* y_ptr = picture->y;212uint8_t* u_ptr = picture->u;213uint8_t* v_ptr = picture->v;214uint8_t* a_ptr = picture->a;215if (!has_alpha || a_ptr == NULL) return; // nothing to do216for (y = 0; y < picture->height; ++y) {217// Luma blending218for (x = 0; x < picture->width; ++x) {219const uint8_t alpha = a_ptr[x];220if (alpha < 0xff) {221y_ptr[x] = BLEND(Y0, y_ptr[x], alpha);222}223}224// Chroma blending every even line225if ((y & 1) == 0) {226uint8_t* const a_ptr2 =227(y + 1 == picture->height) ? a_ptr : a_ptr + picture->a_stride;228for (x = 0; x < uv_width; ++x) {229// Average four alpha values into a single blending weight.230// TODO(skal): might lead to visible contouring. Can we do better?231const uint32_t alpha =232a_ptr[2 * x + 0] + a_ptr[2 * x + 1] +233a_ptr2[2 * x + 0] + a_ptr2[2 * x + 1];234u_ptr[x] = BLEND_10BIT(U0, u_ptr[x], alpha);235v_ptr[x] = BLEND_10BIT(V0, v_ptr[x], alpha);236}237if (picture->width & 1) { // rightmost pixel238const uint32_t alpha = 2 * (a_ptr[2 * x + 0] + a_ptr2[2 * x + 0]);239u_ptr[x] = BLEND_10BIT(U0, u_ptr[x], alpha);240v_ptr[x] = BLEND_10BIT(V0, v_ptr[x], alpha);241}242} else {243u_ptr += picture->uv_stride;244v_ptr += picture->uv_stride;245}246memset(a_ptr, 0xff, picture->width); // reset alpha value to opaque247a_ptr += picture->a_stride;248y_ptr += picture->y_stride;249}250} else {251uint32_t* argb = picture->argb;252const uint32_t background = MakeARGB32(red, green, blue);253for (y = 0; y < picture->height; ++y) {254for (x = 0; x < picture->width; ++x) {255const int alpha = (argb[x] >> 24) & 0xff;256if (alpha != 0xff) {257if (alpha > 0) {258int r = (argb[x] >> 16) & 0xff;259int g = (argb[x] >> 8) & 0xff;260int b = (argb[x] >> 0) & 0xff;261r = BLEND(red, r, alpha);262g = BLEND(green, g, alpha);263b = BLEND(blue, b, alpha);264argb[x] = MakeARGB32(r, g, b);265} else {266argb[x] = background;267}268}269}270argb += picture->argb_stride;271}272}273}274275#undef BLEND276#undef BLEND_10BIT277278//------------------------------------------------------------------------------279280281