/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 | 0 | #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 | 0 | WebPPicture* const dst) { |
34 | 0 | assert(src != NULL && dst != NULL); |
35 | 0 | *dst = *src; |
36 | 0 | WebPPictureResetBuffers(dst); |
37 | 0 | } |
38 | | |
39 | | //------------------------------------------------------------------------------ |
40 | | |
41 | | // Adjust top-left corner to chroma sample position. |
42 | | static void SnapTopLeftPosition(const WebPPicture* const pic, int* const left, |
43 | 0 | int* const top) { |
44 | 0 | if (!pic->use_argb) { |
45 | 0 | *left &= ~1; |
46 | 0 | *top &= ~1; |
47 | 0 | } |
48 | 0 | } |
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 | 0 | int height) { |
54 | 0 | SnapTopLeftPosition(pic, left, top); |
55 | 0 | if ((*left) < 0 || (*top) < 0) return 0; |
56 | 0 | if (width <= 0 || height <= 0) return 0; |
57 | 0 | if (width > pic->width || width > pic->width - (*left)) return 0; |
58 | 0 | if (height > pic->height || height > pic->height - (*top)) return 0; |
59 | 0 | return 1; |
60 | 0 | } |
61 | | |
62 | | #if !defined(WEBP_REDUCE_SIZE) |
63 | 0 | int WebPPictureCopy(const WebPPicture* src, WebPPicture* dst) { |
64 | 0 | if (src == NULL || dst == NULL) return 0; |
65 | 0 | if (src == dst) return 1; |
66 | | |
67 | 0 | PictureGrabSpecs(src, dst); |
68 | 0 | if (!WebPPictureAlloc(dst)) return 0; |
69 | | |
70 | 0 | 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 | 0 | } else { |
82 | 0 | WebPCopyPlane((const uint8_t*)src->argb, 4 * src->argb_stride, |
83 | 0 | (uint8_t*)dst->argb, 4 * dst->argb_stride, 4 * dst->width, |
84 | 0 | dst->height); |
85 | 0 | } |
86 | 0 | return 1; |
87 | 0 | } |
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 | 0 | int height, WebPPicture* dst) { |
100 | 0 | if (src == NULL || dst == NULL) return 0; |
101 | | |
102 | | // verify rectangle position. |
103 | 0 | if (!AdjustAndCheckRectangle(src, &left, &top, width, height)) return 0; |
104 | | |
105 | 0 | if (src != dst) { // beware of aliasing! We don't want to leak 'memory_'. |
106 | 0 | PictureGrabSpecs(src, dst); |
107 | 0 | } |
108 | 0 | dst->width = width; |
109 | 0 | dst->height = height; |
110 | 0 | 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 | 0 | } else { |
121 | 0 | dst->argb = src->argb + top * src->argb_stride + left; |
122 | 0 | dst->argb_stride = src->argb_stride; |
123 | 0 | } |
124 | 0 | return 1; |
125 | 0 | } |
126 | | |
127 | | #if !defined(WEBP_REDUCE_SIZE) |
128 | | //------------------------------------------------------------------------------ |
129 | | // Picture cropping |
130 | | |
131 | | int WebPPictureCrop(WebPPicture* pic, int left, int top, int width, |
132 | 0 | int height) { |
133 | 0 | WebPPicture tmp; |
134 | |
|
135 | 0 | if (pic == NULL) return 0; |
136 | 0 | if (!AdjustAndCheckRectangle(pic, &left, &top, width, height)) return 0; |
137 | | |
138 | 0 | PictureGrabSpecs(pic, &tmp); |
139 | 0 | tmp.width = width; |
140 | 0 | tmp.height = height; |
141 | 0 | if (!WebPPictureAlloc(&tmp)) { |
142 | 0 | return WebPEncodingSetError(pic, tmp.error_code); |
143 | 0 | } |
144 | | |
145 | 0 | if (!pic->use_argb) { |
146 | 0 | const int y_offset = top * pic->y_stride + left; |
147 | 0 | const int uv_offset = (top / 2) * pic->uv_stride + left / 2; |
148 | 0 | WebPCopyPlane(pic->y + y_offset, pic->y_stride, tmp.y, tmp.y_stride, width, |
149 | 0 | height); |
150 | 0 | WebPCopyPlane(pic->u + uv_offset, pic->uv_stride, tmp.u, tmp.uv_stride, |
151 | 0 | HALVE(width), HALVE(height)); |
152 | 0 | WebPCopyPlane(pic->v + uv_offset, pic->uv_stride, tmp.v, tmp.uv_stride, |
153 | 0 | HALVE(width), HALVE(height)); |
154 | |
|
155 | 0 | if (tmp.a != NULL) { |
156 | 0 | const int a_offset = top * pic->a_stride + left; |
157 | 0 | WebPCopyPlane(pic->a + a_offset, pic->a_stride, tmp.a, tmp.a_stride, |
158 | 0 | width, height); |
159 | 0 | } |
160 | 0 | } else { |
161 | 0 | const uint8_t* const src = |
162 | 0 | (const uint8_t*)(pic->argb + top * pic->argb_stride + left); |
163 | 0 | WebPCopyPlane(src, pic->argb_stride * 4, (uint8_t*)tmp.argb, |
164 | 0 | tmp.argb_stride * 4, width * 4, height); |
165 | 0 | } |
166 | 0 | WebPPictureFree(pic); |
167 | 0 | *pic = tmp; |
168 | 0 | return 1; |
169 | 0 | } |
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 | 0 | int num_channels) { |
178 | 0 | WebPRescaler rescaler; |
179 | 0 | int y = 0; |
180 | 0 | if (!WebPRescalerInit(&rescaler, src_width, src_height, dst, dst_width, |
181 | 0 | dst_height, dst_stride, num_channels, work)) { |
182 | 0 | return 0; |
183 | 0 | } |
184 | 0 | while (y < src_height) { |
185 | 0 | y += WebPRescalerImport(&rescaler, src_height - y, |
186 | 0 | src + (ptrdiff_t)y * src_stride, src_stride); |
187 | 0 | WebPRescalerExport(&rescaler); |
188 | 0 | } |
189 | 0 | return 1; |
190 | 0 | } |
191 | | |
192 | 0 | static void AlphaMultiplyARGB(WebPPicture* const pic, int inverse) { |
193 | 0 | assert(pic->argb != NULL); |
194 | 0 | WebPMultARGBRows((uint8_t*)pic->argb, pic->argb_stride * sizeof(*pic->argb), |
195 | 0 | pic->width, pic->height, inverse); |
196 | 0 | } |
197 | | |
198 | 0 | static void AlphaMultiplyY(WebPPicture* const pic, int inverse) { |
199 | 0 | if (pic->a != NULL) { |
200 | 0 | WebPMultRows(pic->y, pic->y_stride, pic->a, pic->a_stride, pic->width, |
201 | 0 | pic->height, inverse); |
202 | 0 | } |
203 | 0 | } |
204 | | |
205 | 0 | int WebPPictureRescale(WebPPicture* picture, int width, int height) { |
206 | 0 | WebPPicture tmp; |
207 | 0 | int prev_width, prev_height; |
208 | 0 | rescaler_t* work; |
209 | 0 | int status = VP8_ENC_OK; |
210 | |
|
211 | 0 | if (picture == NULL) return 0; |
212 | 0 | prev_width = picture->width; |
213 | 0 | prev_height = picture->height; |
214 | 0 | if (!WebPRescalerGetScaledDimensions(prev_width, prev_height, &width, |
215 | 0 | &height)) { |
216 | 0 | return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION); |
217 | 0 | } |
218 | | |
219 | 0 | PictureGrabSpecs(picture, &tmp); |
220 | 0 | tmp.width = width; |
221 | 0 | tmp.height = height; |
222 | 0 | if (!WebPPictureAlloc(&tmp)) { |
223 | 0 | return WebPEncodingSetError(picture, tmp.error_code); |
224 | 0 | } |
225 | | |
226 | 0 | if (!picture->use_argb) { |
227 | 0 | work = (rescaler_t*)WebPSafeMalloc(2ULL * width, sizeof(*work)); |
228 | 0 | 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 | 0 | if (picture->a != NULL) { |
234 | 0 | WebPInitAlphaProcessing(); |
235 | 0 | if (!RescalePlane(picture->a, prev_width, prev_height, picture->a_stride, |
236 | 0 | tmp.a, width, height, tmp.a_stride, work, 1)) { |
237 | 0 | status = VP8_ENC_ERROR_BAD_DIMENSION; |
238 | 0 | goto Cleanup; |
239 | 0 | } |
240 | 0 | } |
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 | 0 | AlphaMultiplyY(picture, 0); |
245 | 0 | if (!RescalePlane(picture->y, prev_width, prev_height, picture->y_stride, |
246 | 0 | tmp.y, width, height, tmp.y_stride, work, 1) || |
247 | 0 | !RescalePlane(picture->u, HALVE(prev_width), HALVE(prev_height), |
248 | 0 | picture->uv_stride, tmp.u, HALVE(width), HALVE(height), |
249 | 0 | tmp.uv_stride, work, 1) || |
250 | 0 | !RescalePlane(picture->v, HALVE(prev_width), HALVE(prev_height), |
251 | 0 | picture->uv_stride, tmp.v, HALVE(width), HALVE(height), |
252 | 0 | tmp.uv_stride, work, 1)) { |
253 | 0 | status = VP8_ENC_ERROR_BAD_DIMENSION; |
254 | 0 | goto Cleanup; |
255 | 0 | } |
256 | 0 | AlphaMultiplyY(&tmp, 1); |
257 | 0 | } else { |
258 | 0 | work = (rescaler_t*)WebPSafeMalloc(2ULL * width * 4, sizeof(*work)); |
259 | 0 | 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 | 0 | WebPInitAlphaProcessing(); |
267 | 0 | AlphaMultiplyARGB(picture, 0); |
268 | 0 | if (!RescalePlane((const uint8_t*)picture->argb, prev_width, prev_height, |
269 | 0 | picture->argb_stride * 4, (uint8_t*)tmp.argb, width, |
270 | 0 | height, tmp.argb_stride * 4, work, 4)) { |
271 | 0 | status = VP8_ENC_ERROR_BAD_DIMENSION; |
272 | 0 | goto Cleanup; |
273 | 0 | } |
274 | 0 | AlphaMultiplyARGB(&tmp, 1); |
275 | 0 | } |
276 | | |
277 | 0 | Cleanup: |
278 | 0 | WebPSafeFree(work); |
279 | 0 | if (status != VP8_ENC_OK) { |
280 | 0 | WebPPictureFree(&tmp); |
281 | 0 | return WebPEncodingSetError(picture, status); |
282 | 0 | } |
283 | | |
284 | 0 | WebPPictureFree(picture); |
285 | 0 | *picture = tmp; |
286 | 0 | return 1; |
287 | 0 | } |
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) |