near_lossless_enc.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "./vp8i_enc.h"
  21. #define MIN_DIM_FOR_NEAR_LOSSLESS 64
  22. #define MAX_LIMIT_BITS 5
  23. // Quantizes the value up or down to a multiple of 1<<bits (or to 255),
  24. // choosing the closer one, resolving ties using bankers' rounding.
  25. static int FindClosestDiscretized(int a, int bits) {
  26. const int mask = (1 << bits) - 1;
  27. const int biased = a + (mask >> 1) + ((a >> bits) & 1);
  28. assert(bits > 0);
  29. if (biased > 0xff) return 0xff;
  30. return biased & ~mask;
  31. }
  32. // Applies FindClosestDiscretized to all channels of pixel.
  33. static uint32_t ClosestDiscretizedArgb(uint32_t a, int bits) {
  34. return
  35. (FindClosestDiscretized(a >> 24, bits) << 24) |
  36. (FindClosestDiscretized((a >> 16) & 0xff, bits) << 16) |
  37. (FindClosestDiscretized((a >> 8) & 0xff, bits) << 8) |
  38. (FindClosestDiscretized(a & 0xff, bits));
  39. }
  40. // Checks if distance between corresponding channel values of pixels a and b
  41. // is within the given limit.
  42. static int IsNear(uint32_t a, uint32_t b, int limit) {
  43. int k;
  44. for (k = 0; k < 4; ++k) {
  45. const int delta =
  46. (int)((a >> (k * 8)) & 0xff) - (int)((b >> (k * 8)) & 0xff);
  47. if (delta >= limit || delta <= -limit) {
  48. return 0;
  49. }
  50. }
  51. return 1;
  52. }
  53. static int IsSmooth(const uint32_t* const prev_row,
  54. const uint32_t* const curr_row,
  55. const uint32_t* const next_row,
  56. int ix, int limit) {
  57. // Check that all pixels in 4-connected neighborhood are smooth.
  58. return (IsNear(curr_row[ix], curr_row[ix - 1], limit) &&
  59. IsNear(curr_row[ix], curr_row[ix + 1], limit) &&
  60. IsNear(curr_row[ix], prev_row[ix], limit) &&
  61. IsNear(curr_row[ix], next_row[ix], limit));
  62. }
  63. // Adjusts pixel values of image with given maximum error.
  64. static void NearLossless(int xsize, int ysize, uint32_t* argb,
  65. int limit_bits, uint32_t* copy_buffer) {
  66. int x, y;
  67. const int limit = 1 << limit_bits;
  68. uint32_t* prev_row = copy_buffer;
  69. uint32_t* curr_row = prev_row + xsize;
  70. uint32_t* next_row = curr_row + xsize;
  71. memcpy(copy_buffer, argb, xsize * 2 * sizeof(argb[0]));
  72. for (y = 1; y < ysize - 1; ++y) {
  73. uint32_t* const curr_argb_row = argb + y * xsize;
  74. uint32_t* const next_argb_row = curr_argb_row + xsize;
  75. memcpy(next_row, next_argb_row, xsize * sizeof(argb[0]));
  76. for (x = 1; x < xsize - 1; ++x) {
  77. if (!IsSmooth(prev_row, curr_row, next_row, x, limit)) {
  78. curr_argb_row[x] = ClosestDiscretizedArgb(curr_row[x], limit_bits);
  79. }
  80. }
  81. {
  82. // Three-way swap.
  83. uint32_t* const temp = prev_row;
  84. prev_row = curr_row;
  85. curr_row = next_row;
  86. next_row = temp;
  87. }
  88. }
  89. }
  90. int VP8ApplyNearLossless(int xsize, int ysize, uint32_t* argb, int quality) {
  91. int i;
  92. uint32_t* const copy_buffer =
  93. (uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer));
  94. const int limit_bits = VP8LNearLosslessBits(quality);
  95. assert(argb != NULL);
  96. assert(limit_bits >= 0);
  97. assert(limit_bits <= MAX_LIMIT_BITS);
  98. if (copy_buffer == NULL) {
  99. return 0;
  100. }
  101. // For small icon images, don't attempt to apply near-lossless compression.
  102. if (xsize < MIN_DIM_FOR_NEAR_LOSSLESS && ysize < MIN_DIM_FOR_NEAR_LOSSLESS) {
  103. WebPSafeFree(copy_buffer);
  104. return 1;
  105. }
  106. for (i = limit_bits; i != 0; --i) {
  107. NearLossless(xsize, ysize, argb, i, copy_buffer);
  108. }
  109. WebPSafeFree(copy_buffer);
  110. return 1;
  111. }