Coverage Report

Created: 2026-08-13 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/enc/picture_rescale_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: copy, crop, rescaling and view.
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include <assert.h>
15
#include <stddef.h>
16
#include <stdlib.h>
17
18
#include "src/dsp/dsp.h"
19
#include "src/enc/vp8i_enc.h"
20
#include "src/webp/encode.h"
21
#include "src/webp/types.h"
22
23
#if !defined(WEBP_REDUCE_SIZE)
24
#include "src/utils/rescaler_utils.h"
25
#include "src/utils/utils.h"
26
#endif  // !defined(WEBP_REDUCE_SIZE)
27
28
304k
#define HALVE(x) (((x) + 1) >> 1)
29
30
// Grab the 'specs' (writer, *opaque, width, height...) from 'src' and copy them
31
// into 'dst'. Mark 'dst' as not owning any memory.
32
static void PictureGrabSpecs(const WebPPicture* const src,
33
248k
                             WebPPicture* const dst) {
34
248k
  assert(src != NULL && dst != NULL);
35
248k
  *dst = *src;
36
248k
  WebPPictureResetBuffers(dst);
37
248k
}
38
39
//------------------------------------------------------------------------------
40
41
// Adjust top-left corner to chroma sample position.
42
static void SnapTopLeftPosition(const WebPPicture* const pic, int* const left,
43
163k
                                int* const top) {
44
163k
  if (!pic->use_argb) {
45
3.43k
    *left &= ~1;
46
3.43k
    *top &= ~1;
47
3.43k
  }
48
163k
}
49
50
// Adjust top-left corner and verify that the sub-rectangle is valid.
51
static int AdjustAndCheckRectangle(const WebPPicture* const pic,
52
                                   int* const left, int* const top, int width,
53
163k
                                   int height) {
54
163k
  SnapTopLeftPosition(pic, left, top);
55
163k
  if ((*left) < 0 || (*top) < 0) return 0;
56
163k
  if (width <= 0 || height <= 0) return 0;
57
163k
  if (width > pic->width || width > pic->width - (*left)) return 0;
58
163k
  if (height > pic->height || height > pic->height - (*top)) return 0;
59
163k
  return 1;
60
163k
}
61
62
#if !defined(WEBP_REDUCE_SIZE)
63
38.4k
int WebPPictureCopy(const WebPPicture* src, WebPPicture* dst) {
64
38.4k
  if (src == NULL || dst == NULL) return 0;
65
38.4k
  if (src == dst) return 1;
66
67
38.4k
  PictureGrabSpecs(src, dst);
68
38.4k
  if (!WebPPictureAlloc(dst)) return 0;
69
70
38.4k
  if (!src->use_argb) {
71
0
    WebPCopyPlane(src->y, src->y_stride, dst->y, dst->y_stride, dst->width,
72
0
                  dst->height);
73
0
    WebPCopyPlane(src->u, src->uv_stride, dst->u, dst->uv_stride,
74
0
                  HALVE(dst->width), HALVE(dst->height));
75
0
    WebPCopyPlane(src->v, src->uv_stride, dst->v, dst->uv_stride,
76
0
                  HALVE(dst->width), HALVE(dst->height));
77
0
    if (dst->a != NULL) {
78
0
      WebPCopyPlane(src->a, src->a_stride, dst->a, dst->a_stride, dst->width,
79
0
                    dst->height);
80
0
    }
81
38.4k
  } else {
82
38.4k
    WebPCopyPlane((const uint8_t*)src->argb, 4 * src->argb_stride,
83
38.4k
                  (uint8_t*)dst->argb, 4 * dst->argb_stride, 4 * dst->width,
84
38.4k
                  dst->height);
85
38.4k
  }
86
38.4k
  return 1;
87
38.4k
}
88
#endif  // !defined(WEBP_REDUCE_SIZE)
89
90
0
int WebPPictureIsView(const WebPPicture* picture) {
91
0
  if (picture == NULL) return 0;
92
0
  if (picture->use_argb) {
93
0
    return (picture->memory_argb_ == NULL);
94
0
  }
95
0
  return (picture->memory_ == NULL);
96
0
}
97
98
int WebPPictureView(const WebPPicture* src, int left, int top, int width,
99
157k
                    int height, WebPPicture* dst) {
100
157k
  if (src == NULL || dst == NULL) return 0;
101
102
  // verify rectangle position.
103
157k
  if (!AdjustAndCheckRectangle(src, &left, &top, width, height)) return 0;
104
105
157k
  if (src != dst) {  // beware of aliasing! We don't want to leak 'memory_'.
106
157k
    PictureGrabSpecs(src, dst);
107
157k
  }
108
157k
  dst->width = width;
109
157k
  dst->height = height;
110
157k
  if (!src->use_argb) {
111
0
    dst->y = src->y + top * src->y_stride + left;
112
0
    dst->u = src->u + (top >> 1) * src->uv_stride + (left >> 1);
113
0
    dst->v = src->v + (top >> 1) * src->uv_stride + (left >> 1);
114
0
    dst->y_stride = src->y_stride;
115
0
    dst->uv_stride = src->uv_stride;
116
0
    if (src->a != NULL) {
117
0
      dst->a = src->a + top * src->a_stride + left;
118
0
      dst->a_stride = src->a_stride;
119
0
    }
120
157k
  } else {
121
157k
    dst->argb = src->argb + top * src->argb_stride + left;
122
157k
    dst->argb_stride = src->argb_stride;
123
157k
  }
124
157k
  return 1;
125
157k
}
126
127
#if !defined(WEBP_REDUCE_SIZE)
128
//------------------------------------------------------------------------------
129
// Picture cropping
130
131
int WebPPictureCrop(WebPPicture* pic, int left, int top, int width,
132
5.66k
                    int height) {
133
5.66k
  WebPPicture tmp;
134
135
5.66k
  if (pic == NULL) return 0;
136
5.66k
  if (!AdjustAndCheckRectangle(pic, &left, &top, width, height)) return 0;
137
138
5.66k
  PictureGrabSpecs(pic, &tmp);
139
5.66k
  tmp.width = width;
140
5.66k
  tmp.height = height;
141
5.66k
  if (!WebPPictureAlloc(&tmp)) {
142
0
    return WebPEncodingSetError(pic, tmp.error_code);
143
0
  }
144
145
5.66k
  if (!pic->use_argb) {
146
3.43k
    const int y_offset = top * pic->y_stride + left;
147
3.43k
    const int uv_offset = (top / 2) * pic->uv_stride + left / 2;
148
3.43k
    WebPCopyPlane(pic->y + y_offset, pic->y_stride, tmp.y, tmp.y_stride, width,
149
3.43k
                  height);
150
3.43k
    WebPCopyPlane(pic->u + uv_offset, pic->uv_stride, tmp.u, tmp.uv_stride,
151
3.43k
                  HALVE(width), HALVE(height));
152
3.43k
    WebPCopyPlane(pic->v + uv_offset, pic->uv_stride, tmp.v, tmp.uv_stride,
153
3.43k
                  HALVE(width), HALVE(height));
154
155
3.43k
    if (tmp.a != NULL) {
156
1.66k
      const int a_offset = top * pic->a_stride + left;
157
1.66k
      WebPCopyPlane(pic->a + a_offset, pic->a_stride, tmp.a, tmp.a_stride,
158
1.66k
                    width, height);
159
1.66k
    }
160
3.43k
  } else {
161
2.22k
    const uint8_t* const src =
162
2.22k
        (const uint8_t*)(pic->argb + top * pic->argb_stride + left);
163
2.22k
    WebPCopyPlane(src, pic->argb_stride * 4, (uint8_t*)tmp.argb,
164
2.22k
                  tmp.argb_stride * 4, width * 4, height);
165
2.22k
  }
166
5.66k
  WebPPictureFree(pic);
167
5.66k
  *pic = tmp;
168
5.66k
  return 1;
169
5.66k
}
170
171
//------------------------------------------------------------------------------
172
// Simple picture rescaler
173
174
static int RescalePlane(const uint8_t* src, int src_width, int src_height,
175
                        int src_stride, uint8_t* dst, int dst_width,
176
                        int dst_height, int dst_stride, rescaler_t* const work,
177
136k
                        int num_channels) {
178
136k
  WebPRescaler rescaler;
179
136k
  int y = 0;
180
136k
  if (!WebPRescalerInit(&rescaler, src_width, src_height, dst, dst_width,
181
136k
                        dst_height, dst_stride, num_channels, work)) {
182
0
    return 0;
183
0
  }
184
3.25M
  while (y < src_height) {
185
3.12M
    y += WebPRescalerImport(&rescaler, src_height - y,
186
3.12M
                            src + (ptrdiff_t)y * src_stride, src_stride);
187
3.12M
    WebPRescalerExport(&rescaler);
188
3.12M
  }
189
136k
  return 1;
190
136k
}
191
192
21.4k
static void AlphaMultiplyARGB(WebPPicture* const pic, int inverse) {
193
21.4k
  assert(pic->argb != NULL);
194
21.4k
  WebPMultARGBRows((uint8_t*)pic->argb, pic->argb_stride * sizeof(*pic->argb),
195
21.4k
                   pic->width, pic->height, inverse);
196
21.4k
}
197
198
72.5k
static void AlphaMultiplyY(WebPPicture* const pic, int inverse) {
199
72.5k
  if (pic->a != NULL) {
200
34.2k
    WebPMultRows(pic->y, pic->y_stride, pic->a, pic->a_stride, pic->width,
201
34.2k
                 pic->height, inverse);
202
34.2k
  }
203
72.5k
}
204
205
47.0k
int WebPPictureRescale(WebPPicture* picture, int width, int height) {
206
47.0k
  WebPPicture tmp;
207
47.0k
  int prev_width, prev_height;
208
47.0k
  rescaler_t* work;
209
47.0k
  int status = VP8_ENC_OK;
210
211
47.0k
  if (picture == NULL) return 0;
212
47.0k
  prev_width = picture->width;
213
47.0k
  prev_height = picture->height;
214
47.0k
  if (!WebPRescalerGetScaledDimensions(prev_width, prev_height, &width,
215
47.0k
                                       &height)) {
216
0
    return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
217
0
  }
218
219
47.0k
  PictureGrabSpecs(picture, &tmp);
220
47.0k
  tmp.width = width;
221
47.0k
  tmp.height = height;
222
47.0k
  if (!WebPPictureAlloc(&tmp)) {
223
0
    return WebPEncodingSetError(picture, tmp.error_code);
224
0
  }
225
226
47.0k
  if (!picture->use_argb) {
227
36.2k
    work = (rescaler_t*)WebPSafeMalloc(2ULL * width, sizeof(*work));
228
36.2k
    if (work == NULL) {
229
0
      status = VP8_ENC_ERROR_OUT_OF_MEMORY;
230
0
      goto Cleanup;
231
0
    }
232
    // If present, we need to rescale alpha first (for AlphaMultiplyY).
233
36.2k
    if (picture->a != NULL) {
234
17.1k
      WebPInitAlphaProcessing();
235
17.1k
      if (!RescalePlane(picture->a, prev_width, prev_height, picture->a_stride,
236
17.1k
                        tmp.a, width, height, tmp.a_stride, work, 1)) {
237
0
        status = VP8_ENC_ERROR_BAD_DIMENSION;
238
0
        goto Cleanup;
239
0
      }
240
17.1k
    }
241
242
    // We take transparency into account on the luma plane only. That's not
243
    // totally exact blending, but still is a good approximation.
244
36.2k
    AlphaMultiplyY(picture, 0);
245
36.2k
    if (!RescalePlane(picture->y, prev_width, prev_height, picture->y_stride,
246
36.2k
                      tmp.y, width, height, tmp.y_stride, work, 1) ||
247
36.2k
        !RescalePlane(picture->u, HALVE(prev_width), HALVE(prev_height),
248
36.2k
                      picture->uv_stride, tmp.u, HALVE(width), HALVE(height),
249
36.2k
                      tmp.uv_stride, work, 1) ||
250
36.2k
        !RescalePlane(picture->v, HALVE(prev_width), HALVE(prev_height),
251
36.2k
                      picture->uv_stride, tmp.v, HALVE(width), HALVE(height),
252
36.2k
                      tmp.uv_stride, work, 1)) {
253
0
      status = VP8_ENC_ERROR_BAD_DIMENSION;
254
0
      goto Cleanup;
255
0
    }
256
36.2k
    AlphaMultiplyY(&tmp, 1);
257
36.2k
  } else {
258
10.7k
    work = (rescaler_t*)WebPSafeMalloc(2ULL * width * 4, sizeof(*work));
259
10.7k
    if (work == NULL) {
260
0
      status = VP8_ENC_ERROR_BAD_DIMENSION;
261
0
      goto Cleanup;
262
0
    }
263
    // In order to correctly interpolate colors, we need to apply the alpha
264
    // weighting first (black-matting), scale the RGB values, and remove
265
    // the premultiplication afterward (while preserving the alpha channel).
266
10.7k
    WebPInitAlphaProcessing();
267
10.7k
    AlphaMultiplyARGB(picture, 0);
268
10.7k
    if (!RescalePlane((const uint8_t*)picture->argb, prev_width, prev_height,
269
10.7k
                      picture->argb_stride * 4, (uint8_t*)tmp.argb, width,
270
10.7k
                      height, tmp.argb_stride * 4, work, 4)) {
271
0
      status = VP8_ENC_ERROR_BAD_DIMENSION;
272
0
      goto Cleanup;
273
0
    }
274
10.7k
    AlphaMultiplyARGB(&tmp, 1);
275
10.7k
  }
276
277
47.0k
Cleanup:
278
47.0k
  WebPSafeFree(work);
279
47.0k
  if (status != VP8_ENC_OK) {
280
0
    WebPPictureFree(&tmp);
281
0
    return WebPEncodingSetError(picture, status);
282
0
  }
283
284
47.0k
  WebPPictureFree(picture);
285
47.0k
  *picture = tmp;
286
47.0k
  return 1;
287
47.0k
}
288
289
#else   // defined(WEBP_REDUCE_SIZE)
290
291
int WebPPictureCopy(const WebPPicture* src, WebPPicture* dst) {
292
  (void)src;
293
  (void)dst;
294
  return 0;
295
}
296
297
int WebPPictureCrop(WebPPicture* pic, int left, int top, int width,
298
                    int height) {
299
  (void)pic;
300
  (void)left;
301
  (void)top;
302
  (void)width;
303
  (void)height;
304
  return 0;
305
}
306
307
int WebPPictureRescale(WebPPicture* pic, int width, int height) {
308
  (void)pic;
309
  (void)width;
310
  (void)height;
311
  return 0;
312
}
313
#endif  // !defined(WEBP_REDUCE_SIZE)