1// Copyright 2014 Google Inc. All Rights Reserved.
2//
3// Use of this source code is governed by a BSD-style license
4// that can be found in the COPYING file in the root of the source
5// tree. An additional intellectual property rights grant can be found
6// in the file PATENTS. All contributing project authors may
7// be found in the AUTHORS file in the root of the source tree.
8// -----------------------------------------------------------------------------
9//
10// WebPPicture tools: alpha handling, etc.
11//
12// Author: Skal (pascal.massimino@gmail.com)
13
14#include "./vp8enci.h"
15#include "../dsp/yuv.h"
16
17static WEBP_INLINE uint32_t MakeARGB32(int r, int g, int b) {
18  return (0xff000000u | (r << 16) | (g << 8) | b);
19}
20
21//------------------------------------------------------------------------------
22// Helper: clean up fully transparent area to help compressibility.
23
24#define SIZE 8
25#define SIZE2 (SIZE / 2)
26static int is_transparent_area(const uint8_t* ptr, int stride, int size) {
27  int y, x;
28  for (y = 0; y < size; ++y) {
29    for (x = 0; x < size; ++x) {
30      if (ptr[x]) {
31        return 0;
32      }
33    }
34    ptr += stride;
35  }
36  return 1;
37}
38
39static int is_transparent_argb_area(const uint32_t* ptr, int stride, int size) {
40  int y, x;
41  for (y = 0; y < size; ++y) {
42    for (x = 0; x < size; ++x) {
43      if (ptr[x] & 0xff000000u) {
44        return 0;
45      }
46    }
47    ptr += stride;
48  }
49  return 1;
50}
51
52static void flatten(uint8_t* ptr, int v, int stride, int size) {
53  int y;
54  for (y = 0; y < size; ++y) {
55    memset(ptr, v, size);
56    ptr += stride;
57  }
58}
59
60static void flatten_argb(uint32_t* ptr, uint32_t v, int stride, int size) {
61  int x, y;
62  for (y = 0; y < size; ++y) {
63    for (x = 0; x < size; ++x) ptr[x] = v;
64    ptr += stride;
65  }
66}
67
68void WebPCleanupTransparentArea(WebPPicture* pic) {
69  int x, y, w, h;
70  if (pic == NULL) return;
71  w = pic->width / SIZE;
72  h = pic->height / SIZE;
73
74  // note: we ignore the left-overs on right/bottom
75  if (pic->use_argb) {
76    uint32_t argb_value = 0;
77    for (y = 0; y < h; ++y) {
78      int need_reset = 1;
79      for (x = 0; x < w; ++x) {
80        const int off = (y * pic->argb_stride + x) * SIZE;
81        if (is_transparent_argb_area(pic->argb + off, pic->argb_stride, SIZE)) {
82          if (need_reset) {
83            argb_value = pic->argb[off];
84            need_reset = 0;
85          }
86          flatten_argb(pic->argb + off, argb_value, pic->argb_stride, SIZE);
87        } else {
88          need_reset = 1;
89        }
90      }
91    }
92  } else {
93    const uint8_t* const a_ptr = pic->a;
94    int values[3] = { 0 };
95    if (a_ptr == NULL) return;    // nothing to do
96    for (y = 0; y < h; ++y) {
97      int need_reset = 1;
98      for (x = 0; x < w; ++x) {
99        const int off_a = (y * pic->a_stride + x) * SIZE;
100        const int off_y = (y * pic->y_stride + x) * SIZE;
101        const int off_uv = (y * pic->uv_stride + x) * SIZE2;
102        if (is_transparent_area(a_ptr + off_a, pic->a_stride, SIZE)) {
103          if (need_reset) {
104            values[0] = pic->y[off_y];
105            values[1] = pic->u[off_uv];
106            values[2] = pic->v[off_uv];
107            need_reset = 0;
108          }
109          flatten(pic->y + off_y, values[0], pic->y_stride, SIZE);
110          flatten(pic->u + off_uv, values[1], pic->uv_stride, SIZE2);
111          flatten(pic->v + off_uv, values[2], pic->uv_stride, SIZE2);
112        } else {
113          need_reset = 1;
114        }
115      }
116    }
117  }
118}
119
120#undef SIZE
121#undef SIZE2
122
123//------------------------------------------------------------------------------
124// Blend color and remove transparency info
125
126#define BLEND(V0, V1, ALPHA) \
127    ((((V0) * (255 - (ALPHA)) + (V1) * (ALPHA)) * 0x101) >> 16)
128#define BLEND_10BIT(V0, V1, ALPHA) \
129    ((((V0) * (1020 - (ALPHA)) + (V1) * (ALPHA)) * 0x101) >> 18)
130
131void WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb) {
132  const int red = (background_rgb >> 16) & 0xff;
133  const int green = (background_rgb >> 8) & 0xff;
134  const int blue = (background_rgb >> 0) & 0xff;
135  int x, y;
136  if (pic == NULL) return;
137  if (!pic->use_argb) {
138    const int uv_width = (pic->width >> 1);  // omit last pixel during u/v loop
139    const int Y0 = VP8RGBToY(red, green, blue, YUV_HALF);
140    // VP8RGBToU/V expects the u/v values summed over four pixels
141    const int U0 = VP8RGBToU(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);
142    const int V0 = VP8RGBToV(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);
143    const int has_alpha = pic->colorspace & WEBP_CSP_ALPHA_BIT;
144    if (!has_alpha || pic->a == NULL) return;    // nothing to do
145    for (y = 0; y < pic->height; ++y) {
146      // Luma blending
147      uint8_t* const y_ptr = pic->y + y * pic->y_stride;
148      uint8_t* const a_ptr = pic->a + y * pic->a_stride;
149      for (x = 0; x < pic->width; ++x) {
150        const int alpha = a_ptr[x];
151        if (alpha < 0xff) {
152          y_ptr[x] = BLEND(Y0, y_ptr[x], a_ptr[x]);
153        }
154      }
155      // Chroma blending every even line
156      if ((y & 1) == 0) {
157        uint8_t* const u = pic->u + (y >> 1) * pic->uv_stride;
158        uint8_t* const v = pic->v + (y >> 1) * pic->uv_stride;
159        uint8_t* const a_ptr2 =
160            (y + 1 == pic->height) ? a_ptr : a_ptr + pic->a_stride;
161        for (x = 0; x < uv_width; ++x) {
162          // Average four alpha values into a single blending weight.
163          // TODO(skal): might lead to visible contouring. Can we do better?
164          const int alpha =
165              a_ptr[2 * x + 0] + a_ptr[2 * x + 1] +
166              a_ptr2[2 * x + 0] + a_ptr2[2 * x + 1];
167          u[x] = BLEND_10BIT(U0, u[x], alpha);
168          v[x] = BLEND_10BIT(V0, v[x], alpha);
169        }
170        if (pic->width & 1) {   // rightmost pixel
171          const int alpha = 2 * (a_ptr[2 * x + 0] + a_ptr2[2 * x + 0]);
172          u[x] = BLEND_10BIT(U0, u[x], alpha);
173          v[x] = BLEND_10BIT(V0, v[x], alpha);
174        }
175      }
176      memset(a_ptr, 0xff, pic->width);
177    }
178  } else {
179    uint32_t* argb = pic->argb;
180    const uint32_t background = MakeARGB32(red, green, blue);
181    for (y = 0; y < pic->height; ++y) {
182      for (x = 0; x < pic->width; ++x) {
183        const int alpha = (argb[x] >> 24) & 0xff;
184        if (alpha != 0xff) {
185          if (alpha > 0) {
186            int r = (argb[x] >> 16) & 0xff;
187            int g = (argb[x] >>  8) & 0xff;
188            int b = (argb[x] >>  0) & 0xff;
189            r = BLEND(red, r, alpha);
190            g = BLEND(green, g, alpha);
191            b = BLEND(blue, b, alpha);
192            argb[x] = MakeARGB32(r, g, b);
193          } else {
194            argb[x] = background;
195          }
196        }
197      }
198      argb += pic->argb_stride;
199    }
200  }
201}
202
203#undef BLEND
204#undef BLEND_10BIT
205
206//------------------------------------------------------------------------------
207