Coverage Report

Created: 2025-07-23 08:18

/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
78.9M
#define SIZE 8
28
6.72M
#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.59M
static void Flatten(uint8_t* ptr, int v, int stride, int size) {
43
9.59M
  int y;
44
60.7M
  for (y = 0; y < size; ++y) {
45
51.1M
    memset(ptr, v, size);
46
51.1M
    ptr += stride;
47
51.1M
  }
48
9.59M
}
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.1M
                         int y_stride, int width, int height) {
62
17.1M
  int sum = 0, count = 0;
63
17.1M
  int x, y;
64
17.1M
  const uint8_t* alpha_ptr = a_ptr;
65
17.1M
  uint8_t* luma_ptr = y_ptr;
66
154M
  for (y = 0; y < height; ++y) {
67
1.22G
    for (x = 0; x < width; ++x) {
68
1.09G
      if (alpha_ptr[x] != 0) {
69
876M
        ++count;
70
876M
        sum += luma_ptr[x];
71
876M
      }
72
1.09G
    }
73
136M
    alpha_ptr += a_stride;
74
136M
    luma_ptr += y_stride;
75
136M
  }
76
17.1M
  if (count > 0 && count < width * height) {
77
1.86M
    const uint8_t avg_u8 = (uint8_t)(sum / count);
78
1.86M
    alpha_ptr = a_ptr;
79
1.86M
    luma_ptr = y_ptr;
80
16.7M
    for (y = 0; y < height; ++y) {
81
133M
      for (x = 0; x < width; ++x) {
82
118M
        if (alpha_ptr[x] == 0) luma_ptr[x] = avg_u8;
83
118M
      }
84
14.8M
      alpha_ptr += a_stride;
85
14.8M
      luma_ptr += y_stride;
86
14.8M
    }
87
1.86M
  }
88
17.1M
  return (count == 0);
89
17.1M
}
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.90k
void WebPCleanupTransparentArea(WebPPicture* pic) {
105
5.90k
  int x, y, w, h;
106
5.90k
  if (pic == NULL) return;
107
5.90k
  w = pic->width / SIZE;
108
5.90k
  h = pic->height / SIZE;
109
110
  // note: we ignore the left-overs on right/bottom, except for SmoothenBlock().
111
5.90k
  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.90k
  } else {
129
5.90k
    const int width = pic->width;
130
5.90k
    const int height = pic->height;
131
5.90k
    const int y_stride = pic->y_stride;
132
5.90k
    const int uv_stride = pic->uv_stride;
133
5.90k
    const int a_stride = pic->a_stride;
134
5.90k
    uint8_t* y_ptr = pic->y;
135
5.90k
    uint8_t* u_ptr = pic->u;
136
5.90k
    uint8_t* v_ptr = pic->v;
137
5.90k
    const uint8_t* a_ptr = pic->a;
138
5.90k
    int values[3] = { 0 };
139
5.90k
    if (a_ptr == NULL || y_ptr == NULL || u_ptr == NULL || v_ptr == NULL) {
140
1.64k
      return;
141
1.64k
    }
142
165k
    for (y = 0; y + SIZE <= height; y += SIZE) {
143
161k
      int need_reset = 1;
144
17.0M
      for (x = 0; x + SIZE <= width; x += SIZE) {
145
16.9M
        if (SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,
146
16.9M
                          SIZE, SIZE)) {
147
3.19M
          if (need_reset) {
148
27.3k
            values[0] = y_ptr[x];
149
27.3k
            values[1] = u_ptr[x >> 1];
150
27.3k
            values[2] = v_ptr[x >> 1];
151
27.3k
            need_reset = 0;
152
27.3k
          }
153
3.19M
          Flatten(y_ptr + x,        values[0], y_stride,  SIZE);
154
3.19M
          Flatten(u_ptr + (x >> 1), values[1], uv_stride, SIZE2);
155
3.19M
          Flatten(v_ptr + (x >> 1), values[2], uv_stride, SIZE2);
156
13.7M
        } else {
157
13.7M
          need_reset = 1;
158
13.7M
        }
159
16.9M
      }
160
161k
      if (x < width) {
161
141k
        SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,
162
141k
                      width - x, SIZE);
163
141k
      }
164
161k
      a_ptr += SIZE * a_stride;
165
161k
      y_ptr += SIZE * y_stride;
166
161k
      u_ptr += SIZE2 * uv_stride;
167
161k
      v_ptr += SIZE2 * uv_stride;
168
161k
    }
169
4.26k
    if (y < height) {
170
4.03k
      const int sub_height = height - y;
171
116k
      for (x = 0; x + SIZE <= width; x += SIZE) {
172
112k
        SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,
173
112k
                      SIZE, sub_height);
174
112k
      }
175
4.03k
      if (x < width) {
176
3.85k
        SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride,
177
3.85k
                      width - x, sub_height);
178
3.85k
      }
179
4.03k
    }
180
4.26k
  }
181
5.90k
}
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 =
233
0
              a_ptr[2 * x + 0] + a_ptr[2 * x + 1] +
234
0
              a_ptr2[2 * x + 0] + a_ptr2[2 * x + 1];
235
0
          u_ptr[x] = BLEND_10BIT(U0, u_ptr[x], alpha);
236
0
          v_ptr[x] = BLEND_10BIT(V0, v_ptr[x], alpha);
237
0
        }
238
0
        if (picture->width & 1) {  // rightmost pixel
239
0
          const uint32_t alpha = 2 * (a_ptr[2 * x + 0] + a_ptr2[2 * x + 0]);
240
0
          u_ptr[x] = BLEND_10BIT(U0, u_ptr[x], alpha);
241
0
          v_ptr[x] = BLEND_10BIT(V0, v_ptr[x], alpha);
242
0
        }
243
0
      } else {
244
0
        u_ptr += picture->uv_stride;
245
0
        v_ptr += picture->uv_stride;
246
0
      }
247
0
      memset(a_ptr, 0xff, picture->width);  // reset alpha value to opaque
248
0
      a_ptr += picture->a_stride;
249
0
      y_ptr += picture->y_stride;
250
0
    }
251
0
  } else {
252
0
    uint32_t* argb = picture->argb;
253
0
    const uint32_t background = MakeARGB32(red, green, blue);
254
0
    for (y = 0; y < picture->height; ++y) {
255
0
      for (x = 0; x < picture->width; ++x) {
256
0
        const int alpha = (argb[x] >> 24) & 0xff;
257
0
        if (alpha != 0xff) {
258
0
          if (alpha > 0) {
259
0
            int r = (argb[x] >> 16) & 0xff;
260
0
            int g = (argb[x] >>  8) & 0xff;
261
0
            int b = (argb[x] >>  0) & 0xff;
262
0
            r = BLEND(red, r, alpha);
263
0
            g = BLEND(green, g, alpha);
264
0
            b = BLEND(blue, b, alpha);
265
0
            argb[x] = MakeARGB32(r, g, b);
266
0
          } else {
267
0
            argb[x] = background;
268
0
          }
269
0
        }
270
0
      }
271
0
      argb += picture->argb_stride;
272
0
    }
273
0
  }
274
0
}
275
276
#undef BLEND
277
#undef BLEND_10BIT
278
279
//------------------------------------------------------------------------------