Coverage Report

Created: 2026-03-31 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/enc/picture_tools_enc.c
Line
Count
Source
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
59.6M
#define SIZE 8
28
11.0M
#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
16.3M
static void Flatten(uint8_t* ptr, int v, int stride, int size) {
43
16.3M
  int y;
44
103M
  for (y = 0; y < size; ++y) {
45
86.9M
    memset(ptr, v, size);
46
86.9M
    ptr += stride;
47
86.9M
  }
48
16.3M
}
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
10.7M
                         int y_stride, int width, int height) {
62
10.7M
  int sum = 0, count = 0;
63
10.7M
  int x, y;
64
10.7M
  const uint8_t* alpha_ptr = a_ptr;
65
10.7M
  uint8_t* luma_ptr = y_ptr;
66
96.3M
  for (y = 0; y < height; ++y) {
67
767M
    for (x = 0; x < width; ++x) {
68
681M
      if (alpha_ptr[x] != 0) {
69
323M
        ++count;
70
323M
        sum += luma_ptr[x];
71
323M
      }
72
681M
    }
73
85.6M
    alpha_ptr += a_stride;
74
85.6M
    luma_ptr += y_stride;
75
85.6M
  }
76
10.7M
  if (count > 0 && count < width * height) {
77
653k
    const uint8_t avg_u8 = (uint8_t)(sum / count);
78
653k
    alpha_ptr = a_ptr;
79
653k
    luma_ptr = y_ptr;
80
5.85M
    for (y = 0; y < height; ++y) {
81
46.5M
      for (x = 0; x < width; ++x) {
82
41.3M
        if (alpha_ptr[x] == 0) luma_ptr[x] = avg_u8;
83
41.3M
      }
84
5.20M
      alpha_ptr += a_stride;
85
5.20M
      luma_ptr += y_stride;
86
5.20M
    }
87
653k
  }
88
10.7M
  return (count == 0);
89
10.7M
}
90
91
894
void WebPReplaceTransparentPixels(WebPPicture* const pic, uint32_t color) {
92
894
  if (pic != NULL && pic->use_argb) {
93
894
    int y = pic->height;
94
894
    uint32_t* argb = pic->argb;
95
894
    color &= 0xffffffu;  // force alpha=0
96
894
    WebPInitAlphaProcessing();
97
489k
    while (y-- > 0) {
98
489k
      WebPAlphaReplace(argb, pic->width, color);
99
489k
      argb += pic->argb_stride;
100
489k
    }
101
894
  }
102
894
}
103
104
1.72k
void WebPCleanupTransparentArea(WebPPicture* pic) {
105
1.72k
  int x, y, w, h;
106
1.72k
  if (pic == NULL) return;
107
1.72k
  w = pic->width / SIZE;
108
1.72k
  h = pic->height / SIZE;
109
110
  // note: we ignore the left-overs on right/bottom, except for SmoothenBlock().
111
1.72k
  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
1.72k
  } else {
129
1.72k
    const int width = pic->width;
130
1.72k
    const int height = pic->height;
131
1.72k
    const int y_stride = pic->y_stride;
132
1.72k
    const int uv_stride = pic->uv_stride;
133
1.72k
    const int a_stride = pic->a_stride;
134
1.72k
    uint8_t* y_ptr = pic->y;
135
1.72k
    uint8_t* u_ptr = pic->u;
136
1.72k
    uint8_t* v_ptr = pic->v;
137
1.72k
    const uint8_t* a_ptr = pic->a;
138
1.72k
    int values[3] = {0};
139
1.72k
    if (a_ptr == NULL || y_ptr == NULL || u_ptr == NULL || v_ptr == NULL) {
140
446
      return;
141
446
    }
142
108k
    for (y = 0; y + SIZE <= height; y += SIZE) {
143
107k
      int need_reset = 1;
144
10.6M
      for (x = 0; x + SIZE <= width; x += SIZE) {
145
10.5M
        if (SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride, SIZE,
146
10.5M
                          SIZE)) {
147
5.43M
          if (need_reset) {
148
47.7k
            values[0] = y_ptr[x];
149
47.7k
            values[1] = u_ptr[x >> 1];
150
47.7k
            values[2] = v_ptr[x >> 1];
151
47.7k
            need_reset = 0;
152
47.7k
          }
153
5.43M
          Flatten(y_ptr + x, values[0], y_stride, SIZE);
154
5.43M
          Flatten(u_ptr + (x >> 1), values[1], uv_stride, SIZE2);
155
5.43M
          Flatten(v_ptr + (x >> 1), values[2], uv_stride, SIZE2);
156
5.43M
        } else {
157
5.13M
          need_reset = 1;
158
5.13M
        }
159
10.5M
      }
160
107k
      if (x < width) {
161
91.2k
        SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride, width - x,
162
91.2k
                      SIZE);
163
91.2k
      }
164
107k
      a_ptr += SIZE * a_stride;
165
107k
      y_ptr += SIZE * y_stride;
166
107k
      u_ptr += SIZE2 * uv_stride;
167
107k
      v_ptr += SIZE2 * uv_stride;
168
107k
    }
169
1.27k
    if (y < height) {
170
1.17k
      const int sub_height = height - y;
171
84.7k
      for (x = 0; x + SIZE <= width; x += SIZE) {
172
83.5k
        SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride, SIZE,
173
83.5k
                      sub_height);
174
83.5k
      }
175
1.17k
      if (x < width) {
176
1.01k
        SmoothenBlock(a_ptr + x, a_stride, y_ptr + x, y_stride, width - x,
177
1.01k
                      sub_height);
178
1.01k
      }
179
1.17k
    }
180
1.27k
  }
181
1.72k
}
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
//------------------------------------------------------------------------------