Coverage Report

Created: 2025-08-11 08:01

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