picture_rescale.c 9.6 KB

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