picture_tools.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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: alpha handling, etc.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./vp8enci.h"
  14. #include "../dsp/yuv.h"
  15. static WEBP_INLINE uint32_t MakeARGB32(int r, int g, int b) {
  16. return (0xff000000u | (r << 16) | (g << 8) | b);
  17. }
  18. //------------------------------------------------------------------------------
  19. // Helper: clean up fully transparent area to help compressibility.
  20. #define SIZE 8
  21. #define SIZE2 (SIZE / 2)
  22. static int is_transparent_area(const uint8_t* ptr, int stride, int size) {
  23. int y, x;
  24. for (y = 0; y < size; ++y) {
  25. for (x = 0; x < size; ++x) {
  26. if (ptr[x]) {
  27. return 0;
  28. }
  29. }
  30. ptr += stride;
  31. }
  32. return 1;
  33. }
  34. static int is_transparent_argb_area(const uint32_t* ptr, int stride, int size) {
  35. int y, x;
  36. for (y = 0; y < size; ++y) {
  37. for (x = 0; x < size; ++x) {
  38. if (ptr[x] & 0xff000000u) {
  39. return 0;
  40. }
  41. }
  42. ptr += stride;
  43. }
  44. return 1;
  45. }
  46. static void flatten(uint8_t* ptr, int v, int stride, int size) {
  47. int y;
  48. for (y = 0; y < size; ++y) {
  49. memset(ptr, v, size);
  50. ptr += stride;
  51. }
  52. }
  53. static void flatten_argb(uint32_t* ptr, uint32_t v, int stride, int size) {
  54. int x, y;
  55. for (y = 0; y < size; ++y) {
  56. for (x = 0; x < size; ++x) ptr[x] = v;
  57. ptr += stride;
  58. }
  59. }
  60. void WebPCleanupTransparentArea(WebPPicture* pic) {
  61. int x, y, w, h;
  62. if (pic == NULL) return;
  63. w = pic->width / SIZE;
  64. h = pic->height / SIZE;
  65. // note: we ignore the left-overs on right/bottom
  66. if (pic->use_argb) {
  67. uint32_t argb_value = 0;
  68. for (y = 0; y < h; ++y) {
  69. int need_reset = 1;
  70. for (x = 0; x < w; ++x) {
  71. const int off = (y * pic->argb_stride + x) * SIZE;
  72. if (is_transparent_argb_area(pic->argb + off, pic->argb_stride, SIZE)) {
  73. if (need_reset) {
  74. argb_value = pic->argb[off];
  75. need_reset = 0;
  76. }
  77. flatten_argb(pic->argb + off, argb_value, pic->argb_stride, SIZE);
  78. } else {
  79. need_reset = 1;
  80. }
  81. }
  82. }
  83. } else {
  84. const uint8_t* const a_ptr = pic->a;
  85. int values[3] = { 0 };
  86. if (a_ptr == NULL) return; // nothing to do
  87. for (y = 0; y < h; ++y) {
  88. int need_reset = 1;
  89. for (x = 0; x < w; ++x) {
  90. const int off_a = (y * pic->a_stride + x) * SIZE;
  91. const int off_y = (y * pic->y_stride + x) * SIZE;
  92. const int off_uv = (y * pic->uv_stride + x) * SIZE2;
  93. if (is_transparent_area(a_ptr + off_a, pic->a_stride, SIZE)) {
  94. if (need_reset) {
  95. values[0] = pic->y[off_y];
  96. values[1] = pic->u[off_uv];
  97. values[2] = pic->v[off_uv];
  98. need_reset = 0;
  99. }
  100. flatten(pic->y + off_y, values[0], pic->y_stride, SIZE);
  101. flatten(pic->u + off_uv, values[1], pic->uv_stride, SIZE2);
  102. flatten(pic->v + off_uv, values[2], pic->uv_stride, SIZE2);
  103. } else {
  104. need_reset = 1;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. #undef SIZE
  111. #undef SIZE2
  112. //------------------------------------------------------------------------------
  113. // Blend color and remove transparency info
  114. #define BLEND(V0, V1, ALPHA) \
  115. ((((V0) * (255 - (ALPHA)) + (V1) * (ALPHA)) * 0x101) >> 16)
  116. #define BLEND_10BIT(V0, V1, ALPHA) \
  117. ((((V0) * (1020 - (ALPHA)) + (V1) * (ALPHA)) * 0x101) >> 18)
  118. void WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb) {
  119. const int red = (background_rgb >> 16) & 0xff;
  120. const int green = (background_rgb >> 8) & 0xff;
  121. const int blue = (background_rgb >> 0) & 0xff;
  122. int x, y;
  123. if (pic == NULL) return;
  124. if (!pic->use_argb) {
  125. const int uv_width = (pic->width >> 1); // omit last pixel during u/v loop
  126. const int Y0 = VP8RGBToY(red, green, blue, YUV_HALF);
  127. // VP8RGBToU/V expects the u/v values summed over four pixels
  128. const int U0 = VP8RGBToU(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);
  129. const int V0 = VP8RGBToV(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);
  130. const int has_alpha = pic->colorspace & WEBP_CSP_ALPHA_BIT;
  131. if (!has_alpha || pic->a == NULL) return; // nothing to do
  132. for (y = 0; y < pic->height; ++y) {
  133. // Luma blending
  134. uint8_t* const y_ptr = pic->y + y * pic->y_stride;
  135. uint8_t* const a_ptr = pic->a + y * pic->a_stride;
  136. for (x = 0; x < pic->width; ++x) {
  137. const int alpha = a_ptr[x];
  138. if (alpha < 0xff) {
  139. y_ptr[x] = BLEND(Y0, y_ptr[x], a_ptr[x]);
  140. }
  141. }
  142. // Chroma blending every even line
  143. if ((y & 1) == 0) {
  144. uint8_t* const u = pic->u + (y >> 1) * pic->uv_stride;
  145. uint8_t* const v = pic->v + (y >> 1) * pic->uv_stride;
  146. uint8_t* const a_ptr2 =
  147. (y + 1 == pic->height) ? a_ptr : a_ptr + pic->a_stride;
  148. for (x = 0; x < uv_width; ++x) {
  149. // Average four alpha values into a single blending weight.
  150. // TODO(skal): might lead to visible contouring. Can we do better?
  151. const int alpha =
  152. a_ptr[2 * x + 0] + a_ptr[2 * x + 1] +
  153. a_ptr2[2 * x + 0] + a_ptr2[2 * x + 1];
  154. u[x] = BLEND_10BIT(U0, u[x], alpha);
  155. v[x] = BLEND_10BIT(V0, v[x], alpha);
  156. }
  157. if (pic->width & 1) { // rightmost pixel
  158. const int alpha = 2 * (a_ptr[2 * x + 0] + a_ptr2[2 * x + 0]);
  159. u[x] = BLEND_10BIT(U0, u[x], alpha);
  160. v[x] = BLEND_10BIT(V0, v[x], alpha);
  161. }
  162. }
  163. memset(a_ptr, 0xff, pic->width);
  164. }
  165. } else {
  166. uint32_t* argb = pic->argb;
  167. const uint32_t background = MakeARGB32(red, green, blue);
  168. for (y = 0; y < pic->height; ++y) {
  169. for (x = 0; x < pic->width; ++x) {
  170. const int alpha = (argb[x] >> 24) & 0xff;
  171. if (alpha != 0xff) {
  172. if (alpha > 0) {
  173. int r = (argb[x] >> 16) & 0xff;
  174. int g = (argb[x] >> 8) & 0xff;
  175. int b = (argb[x] >> 0) & 0xff;
  176. r = BLEND(red, r, alpha);
  177. g = BLEND(green, g, alpha);
  178. b = BLEND(blue, b, alpha);
  179. argb[x] = MakeARGB32(r, g, b);
  180. } else {
  181. argb[x] = background;
  182. }
  183. }
  184. }
  185. argb += pic->argb_stride;
  186. }
  187. }
  188. }
  189. #undef BLEND
  190. #undef BLEND_10BIT
  191. //------------------------------------------------------------------------------