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