picture_rescale_enc.c 9.9 KB

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