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