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