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