near_lossless_enc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // Near-lossless image preprocessing adjusts pixel values to help
  11. // compressibility with a guarantee of maximum deviation between original and
  12. // resulting pixel values.
  13. //
  14. // Author: Jyrki Alakuijala (jyrki@google.com)
  15. // Converted to C by Aleksander Kramarz (akramarz@google.com)
  16. #include <assert.h>
  17. #include <stdlib.h>
  18. #include "../dsp/lossless_common.h"
  19. #include "../utils/utils.h"
  20. #include "./vp8li_enc.h"
  21. #if (WEBP_NEAR_LOSSLESS == 1)
  22. #define MIN_DIM_FOR_NEAR_LOSSLESS 64
  23. #define MAX_LIMIT_BITS 5
  24. // Quantizes the value up or down to a multiple of 1<<bits (or to 255),
  25. // choosing the closer one, resolving ties using bankers' rounding.
  26. static uint32_t FindClosestDiscretized(uint32_t a, int bits) {
  27. const uint32_t mask = (1u << bits) - 1;
  28. const uint32_t biased = a + (mask >> 1) + ((a >> bits) & 1);
  29. assert(bits > 0);
  30. if (biased > 0xff) return 0xff;
  31. return biased & ~mask;
  32. }
  33. // Applies FindClosestDiscretized to all channels of pixel.
  34. static uint32_t ClosestDiscretizedArgb(uint32_t a, int bits) {
  35. return
  36. (FindClosestDiscretized(a >> 24, bits) << 24) |
  37. (FindClosestDiscretized((a >> 16) & 0xff, bits) << 16) |
  38. (FindClosestDiscretized((a >> 8) & 0xff, bits) << 8) |
  39. (FindClosestDiscretized(a & 0xff, bits));
  40. }
  41. // Checks if distance between corresponding channel values of pixels a and b
  42. // is within the given limit.
  43. static int IsNear(uint32_t a, uint32_t b, int limit) {
  44. int k;
  45. for (k = 0; k < 4; ++k) {
  46. const int delta =
  47. (int)((a >> (k * 8)) & 0xff) - (int)((b >> (k * 8)) & 0xff);
  48. if (delta >= limit || delta <= -limit) {
  49. return 0;
  50. }
  51. }
  52. return 1;
  53. }
  54. static int IsSmooth(const uint32_t* const prev_row,
  55. const uint32_t* const curr_row,
  56. const uint32_t* const next_row,
  57. int ix, int limit) {
  58. // Check that all pixels in 4-connected neighborhood are smooth.
  59. return (IsNear(curr_row[ix], curr_row[ix - 1], limit) &&
  60. IsNear(curr_row[ix], curr_row[ix + 1], limit) &&
  61. IsNear(curr_row[ix], prev_row[ix], limit) &&
  62. IsNear(curr_row[ix], next_row[ix], limit));
  63. }
  64. // Adjusts pixel values of image with given maximum error.
  65. static void NearLossless(int xsize, int ysize, const uint32_t* argb_src,
  66. int stride, int limit_bits, uint32_t* copy_buffer,
  67. uint32_t* argb_dst) {
  68. int x, y;
  69. const int limit = 1 << limit_bits;
  70. uint32_t* prev_row = copy_buffer;
  71. uint32_t* curr_row = prev_row + xsize;
  72. uint32_t* next_row = curr_row + xsize;
  73. memcpy(curr_row, argb_src, xsize * sizeof(argb_src[0]));
  74. memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
  75. for (y = 0; y < ysize; ++y, argb_src += stride, argb_dst += xsize) {
  76. if (y == 0 || y == ysize - 1) {
  77. memcpy(argb_dst, argb_src, xsize * sizeof(argb_src[0]));
  78. } else {
  79. memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
  80. argb_dst[0] = argb_src[0];
  81. argb_dst[xsize - 1] = argb_src[xsize - 1];
  82. for (x = 1; x < xsize - 1; ++x) {
  83. if (IsSmooth(prev_row, curr_row, next_row, x, limit)) {
  84. argb_dst[x] = curr_row[x];
  85. } else {
  86. argb_dst[x] = ClosestDiscretizedArgb(curr_row[x], limit_bits);
  87. }
  88. }
  89. }
  90. {
  91. // Three-way swap.
  92. uint32_t* const temp = prev_row;
  93. prev_row = curr_row;
  94. curr_row = next_row;
  95. next_row = temp;
  96. }
  97. }
  98. }
  99. int VP8ApplyNearLossless(const WebPPicture* const picture, int quality,
  100. uint32_t* const argb_dst) {
  101. int i;
  102. const int xsize = picture->width;
  103. const int ysize = picture->height;
  104. const int stride = picture->argb_stride;
  105. uint32_t* const copy_buffer =
  106. (uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer));
  107. const int limit_bits = VP8LNearLosslessBits(quality);
  108. assert(argb_dst != NULL);
  109. assert(limit_bits > 0);
  110. assert(limit_bits <= MAX_LIMIT_BITS);
  111. if (copy_buffer == NULL) {
  112. return 0;
  113. }
  114. // For small icon images, don't attempt to apply near-lossless compression.
  115. if ((xsize < MIN_DIM_FOR_NEAR_LOSSLESS &&
  116. ysize < MIN_DIM_FOR_NEAR_LOSSLESS) ||
  117. ysize < 3) {
  118. for (i = 0; i < ysize; ++i) {
  119. memcpy(argb_dst + i * xsize, picture->argb + i * picture->argb_stride,
  120. xsize * sizeof(*argb_dst));
  121. }
  122. WebPSafeFree(copy_buffer);
  123. return 1;
  124. }
  125. NearLossless(xsize, ysize, picture->argb, stride, limit_bits, copy_buffer,
  126. argb_dst);
  127. for (i = limit_bits - 1; i != 0; --i) {
  128. NearLossless(xsize, ysize, argb_dst, xsize, i, copy_buffer, argb_dst);
  129. }
  130. WebPSafeFree(copy_buffer);
  131. return 1;
  132. }
  133. #else // (WEBP_NEAR_LOSSLESS == 1)
  134. // Define a stub to suppress compiler warnings.
  135. extern void VP8LNearLosslessStub(void);
  136. void VP8LNearLosslessStub(void) {}
  137. #endif // (WEBP_NEAR_LOSSLESS == 1)