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