Coverage Report

Created: 2024-07-27 06:31

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