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 <assert.h>
15
16 #include "src/enc/vp8i_enc.h"
17 #include "src/dsp/yuv.h"
18
19 //------------------------------------------------------------------------------
20 // Helper: clean up fully transparent area to help compressibility.
21
22 #define SIZE 8
23 #define SIZE2 (SIZE / 2)
IsTransparentARGBArea(const uint32_t * ptr,int stride,int size)24 static int IsTransparentARGBArea(const uint32_t* ptr, int stride, int size) {
25 int y, x;
26 for (y = 0; y < size; ++y) {
27 for (x = 0; x < size; ++x) {
28 if (ptr[x] & 0xff000000u) {
29 return 0;
30 }
31 }
32 ptr += stride;
33 }
34 return 1;
35 }
36
Flatten(uint8_t * ptr,int v,int stride,int size)37 static void Flatten(uint8_t* ptr, int v, int stride, int size) {
38 int y;
39 for (y = 0; y < size; ++y) {
40 memset(ptr, v, size);
41 ptr += stride;
42 }
43 }
44
FlattenARGB(uint32_t * ptr,uint32_t v,int stride,int size)45 static void FlattenARGB(uint32_t* ptr, uint32_t v, int stride, int size) {
46 int x, y;
47 for (y = 0; y < size; ++y) {
48 for (x = 0; x < size; ++x) ptr[x] = v;
49 ptr += stride;
50 }
51 }
52
53 // Smoothen the luma components of transparent pixels. Return true if the whole
54 // block is transparent.
SmoothenBlock(const uint8_t * a_ptr,int a_stride,uint8_t * y_ptr,int y_stride,int width,int height)55 static int SmoothenBlock(const uint8_t* a_ptr, int a_stride, uint8_t* y_ptr,
56 int y_stride, int width, int height) {
57 int sum = 0, count = 0;
58 int x, y;
59 const uint8_t* alpha_ptr = a_ptr;
60 uint8_t* luma_ptr = y_ptr;
61 for (y = 0; y < height; ++y) {
62 for (x = 0; x < width; ++x) {
63 if (alpha_ptr[x] != 0) {
64 ++count;
65 sum += luma_ptr[x];
66 }
67 }
68 alpha_ptr += a_stride;
69 luma_ptr += y_stride;
70 }
71 if (count > 0 && count < width * height) {
72 const uint8_t avg_u8 = (uint8_t)(sum / count);
73 alpha_ptr = a_ptr;
74 luma_ptr = y_ptr;
75 for (y = 0; y < height; ++y) {
76 for (x = 0; x < width; ++x) {
77 if (alpha_ptr[x] == 0) luma_ptr[x] = avg_u8;
78 }
79 alpha_ptr += a_stride;
80 luma_ptr += y_stride;
81 }
82 }
83 return (count == 0);
84 }
85
WebPReplaceTransparentPixels(WebPPicture * const pic,uint32_t color)86 void WebPReplaceTransparentPixels(WebPPicture* const pic, uint32_t color) {
87 if (pic != NULL && pic->use_argb) {
88 int y = pic->height;
89 uint32_t* argb = pic->argb;
90 color &= 0xffffffu; // force alpha=0
91 WebPInitAlphaProcessing();
92 while (y-- > 0) {
93 WebPAlphaReplace(argb, pic->width, color);
94 argb += pic->argb_stride;
95 }
96 }
97 }
98
WebPCleanupTransparentArea(WebPPicture * pic)99 void WebPCleanupTransparentArea(WebPPicture* pic) {
100 int x, y, w, h;
101 if (pic == NULL) return;
102 w = pic->width / SIZE;
103 h = pic->height / SIZE;
104
105 // note: we ignore the left-overs on right/bottom, except for SmoothenBlock().
106 if (pic->use_argb) {
107 uint32_t argb_value = 0;
108 for (y = 0; y < h; ++y) {
109 int need_reset = 1;
110 for (x = 0; x < w; ++x) {
111 const int off = (y * pic->argb_stride + x) * SIZE;
112 if (IsTransparentARGBArea(pic->argb + off, pic->argb_stride, SIZE)) {
113 if (need_reset) {
114 argb_value = pic->argb[off];
115 need_reset = 0;
116 }
117 FlattenARGB(pic->argb + off, argb_value, pic->argb_stride, SIZE);
118 } else {
119 need_reset = 1;
120 }
121 }
122 }
123 } else {
124 const int width = pic->width;
125 const int height = pic->height;
126 const int y_stride = pic->y_stride;
127 const int uv_stride = pic->uv_stride;
128 const int a_stride = pic->a_stride;
129 uint8_t* y_ptr = pic->y;
130 uint8_t* u_ptr = pic->u;
131 uint8_t* v_ptr = pic->v;
132 const uint8_t* a_ptr = pic->a;
133 int values[3] = { 0 };
134 if (a_ptr == NULL || y_ptr == NULL || u_ptr == NULL || v_ptr == NULL) {
135 return;
136 }
137 for (y = 0; y + SIZE <= height; y += SIZE) {
138 int need_reset = 1;
139 for (x = 0; x + SIZE <= width; x += SIZE) {
140 if (SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,
141 SIZE, SIZE)) {
142 if (need_reset) {
143 values[0] = y_ptr[x];
144 values[1] = u_ptr[x >> 1];
145 values[2] = v_ptr[x >> 1];
146 need_reset = 0;
147 }
148 Flatten(y_ptr + x, values[0], y_stride, SIZE);
149 Flatten(u_ptr + (x >> 1), values[1], uv_stride, SIZE2);
150 Flatten(v_ptr + (x >> 1), values[2], uv_stride, SIZE2);
151 } else {
152 need_reset = 1;
153 }
154 }
155 if (x < width) {
156 SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,
157 width - x, SIZE);
158 }
159 a_ptr += SIZE * a_stride;
160 y_ptr += SIZE * y_stride;
161 u_ptr += SIZE2 * uv_stride;
162 v_ptr += SIZE2 * uv_stride;
163 }
164 if (y < height) {
165 const int sub_height = height - y;
166 for (x = 0; x + SIZE <= width; x += SIZE) {
167 SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,
168 SIZE, sub_height);
169 }
170 if (x < width) {
171 SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,
172 width - x, sub_height);
173 }
174 }
175 }
176 }
177
178 #undef SIZE
179 #undef SIZE2
180
181 //------------------------------------------------------------------------------
182 // Blend color and remove transparency info
183
184 #define BLEND(V0, V1, ALPHA) \
185 ((((V0) * (255 - (ALPHA)) + (V1) * (ALPHA)) * 0x101 + 256) >> 16)
186 #define BLEND_10BIT(V0, V1, ALPHA) \
187 ((((V0) * (1020 - (ALPHA)) + (V1) * (ALPHA)) * 0x101 + 1024) >> 18)
188
MakeARGB32(int r,int g,int b)189 static WEBP_INLINE uint32_t MakeARGB32(int r, int g, int b) {
190 return (0xff000000u | (r << 16) | (g << 8) | b);
191 }
192
WebPBlendAlpha(WebPPicture * pic,uint32_t background_rgb)193 void WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb) {
194 const int red = (background_rgb >> 16) & 0xff;
195 const int green = (background_rgb >> 8) & 0xff;
196 const int blue = (background_rgb >> 0) & 0xff;
197 int x, y;
198 if (pic == NULL) return;
199 if (!pic->use_argb) {
200 const int uv_width = (pic->width >> 1); // omit last pixel during u/v loop
201 const int Y0 = VP8RGBToY(red, green, blue, YUV_HALF);
202 // VP8RGBToU/V expects the u/v values summed over four pixels
203 const int U0 = VP8RGBToU(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);
204 const int V0 = VP8RGBToV(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);
205 const int has_alpha = pic->colorspace & WEBP_CSP_ALPHA_BIT;
206 uint8_t* y_ptr = pic->y;
207 uint8_t* u_ptr = pic->u;
208 uint8_t* v_ptr = pic->v;
209 uint8_t* a_ptr = pic->a;
210 if (!has_alpha || a_ptr == NULL) return; // nothing to do
211 for (y = 0; y < pic->height; ++y) {
212 // Luma blending
213 for (x = 0; x < pic->width; ++x) {
214 const uint8_t alpha = a_ptr[x];
215 if (alpha < 0xff) {
216 y_ptr[x] = BLEND(Y0, y_ptr[x], alpha);
217 }
218 }
219 // Chroma blending every even line
220 if ((y & 1) == 0) {
221 uint8_t* const a_ptr2 =
222 (y + 1 == pic->height) ? a_ptr : a_ptr + pic->a_stride;
223 for (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?
226 const uint32_t alpha =
227 a_ptr[2 * x + 0] + a_ptr[2 * x + 1] +
228 a_ptr2[2 * x + 0] + a_ptr2[2 * x + 1];
229 u_ptr[x] = BLEND_10BIT(U0, u_ptr[x], alpha);
230 v_ptr[x] = BLEND_10BIT(V0, v_ptr[x], alpha);
231 }
232 if (pic->width & 1) { // rightmost pixel
233 const uint32_t alpha = 2 * (a_ptr[2 * x + 0] + a_ptr2[2 * x + 0]);
234 u_ptr[x] = BLEND_10BIT(U0, u_ptr[x], alpha);
235 v_ptr[x] = BLEND_10BIT(V0, v_ptr[x], alpha);
236 }
237 } else {
238 u_ptr += pic->uv_stride;
239 v_ptr += pic->uv_stride;
240 }
241 memset(a_ptr, 0xff, pic->width); // reset alpha value to opaque
242 a_ptr += pic->a_stride;
243 y_ptr += pic->y_stride;
244 }
245 } else {
246 uint32_t* argb = pic->argb;
247 const uint32_t background = MakeARGB32(red, green, blue);
248 for (y = 0; y < pic->height; ++y) {
249 for (x = 0; x < pic->width; ++x) {
250 const int alpha = (argb[x] >> 24) & 0xff;
251 if (alpha != 0xff) {
252 if (alpha > 0) {
253 int r = (argb[x] >> 16) & 0xff;
254 int g = (argb[x] >> 8) & 0xff;
255 int b = (argb[x] >> 0) & 0xff;
256 r = BLEND(red, r, alpha);
257 g = BLEND(green, g, alpha);
258 b = BLEND(blue, b, alpha);
259 argb[x] = MakeARGB32(r, g, b);
260 } else {
261 argb[x] = background;
262 }
263 }
264 }
265 argb += pic->argb_stride;
266 }
267 }
268 }
269
270 #undef BLEND
271 #undef BLEND_10BIT
272
273 //------------------------------------------------------------------------------
274