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