picture_rescale_enc.c 9.0 KB

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