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