quant_levels_dec.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2013 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. // Implement gradient smoothing: we replace a current alpha value by its
  11. // surrounding average if it's close enough (that is: the change will be less
  12. // than the minimum distance between two quantized level).
  13. // We use sliding window for computing the 2d moving average.
  14. //
  15. // Author: Skal (pascal.massimino@gmail.com)
  16. #include "./quant_levels_dec.h"
  17. #include <string.h> // for memset
  18. #include "./utils.h"
  19. // #define USE_DITHERING // uncomment to enable ordered dithering (not vital)
  20. #define FIX 16 // fix-point precision for averaging
  21. #define LFIX 2 // extra precision for look-up table
  22. #define LUT_SIZE ((1 << (8 + LFIX)) - 1) // look-up table size
  23. #if defined(USE_DITHERING)
  24. #define DFIX 4 // extra precision for ordered dithering
  25. #define DSIZE 4 // dithering size (must be a power of two)
  26. // cf. http://en.wikipedia.org/wiki/Ordered_dithering
  27. static const uint8_t kOrderedDither[DSIZE][DSIZE] = {
  28. { 0, 8, 2, 10 }, // coefficients are in DFIX fixed-point precision
  29. { 12, 4, 14, 6 },
  30. { 3, 11, 1, 9 },
  31. { 15, 7, 13, 5 }
  32. };
  33. #else
  34. #define DFIX 0
  35. #endif
  36. typedef struct {
  37. int width_, height_; // dimension
  38. int row_; // current input row being processed
  39. uint8_t* src_; // input pointer
  40. uint8_t* dst_; // output pointer
  41. int radius_; // filter radius (=delay)
  42. int scale_; // normalization factor, in FIX bits precision
  43. void* mem_; // all memory
  44. // various scratch buffers
  45. uint16_t* start_;
  46. uint16_t* cur_;
  47. uint16_t* end_;
  48. uint16_t* top_;
  49. uint16_t* average_;
  50. // input levels distribution
  51. int num_levels_; // number of quantized levels
  52. int min_, max_; // min and max level values
  53. int min_level_dist_; // smallest distance between two consecutive levels
  54. int16_t* correction_; // size = 1 + 2*LUT_SIZE -> ~4k memory
  55. } SmoothParams;
  56. //------------------------------------------------------------------------------
  57. #define CLIP_MASK (int)(~0U << (8 + DFIX))
  58. static WEBP_INLINE uint8_t clip_8b(int v) {
  59. return (!(v & CLIP_MASK)) ? (uint8_t)(v >> DFIX) : (v < 0) ? 0u : 255u;
  60. }
  61. // vertical accumulation
  62. static void VFilter(SmoothParams* const p) {
  63. const uint8_t* src = p->src_;
  64. const int w = p->width_;
  65. uint16_t* const cur = p->cur_;
  66. const uint16_t* const top = p->top_;
  67. uint16_t* const out = p->end_;
  68. uint16_t sum = 0; // all arithmetic is modulo 16bit
  69. int x;
  70. for (x = 0; x < w; ++x) {
  71. uint16_t new_value;
  72. sum += src[x];
  73. new_value = top[x] + sum;
  74. out[x] = new_value - cur[x]; // vertical sum of 'r' pixels.
  75. cur[x] = new_value;
  76. }
  77. // move input pointers one row down
  78. p->top_ = p->cur_;
  79. p->cur_ += w;
  80. if (p->cur_ == p->end_) p->cur_ = p->start_; // roll-over
  81. // We replicate edges, as it's somewhat easier as a boundary condition.
  82. // That's why we don't update the 'src' pointer on top/bottom area:
  83. if (p->row_ >= 0 && p->row_ < p->height_ - 1) {
  84. p->src_ += p->width_;
  85. }
  86. }
  87. // horizontal accumulation. We use mirror replication of missing pixels, as it's
  88. // a little easier to implement (surprisingly).
  89. static void HFilter(SmoothParams* const p) {
  90. const uint16_t* const in = p->end_;
  91. uint16_t* const out = p->average_;
  92. const uint32_t scale = p->scale_;
  93. const int w = p->width_;
  94. const int r = p->radius_;
  95. int x;
  96. for (x = 0; x <= r; ++x) { // left mirroring
  97. const uint16_t delta = in[x + r - 1] + in[r - x];
  98. out[x] = (delta * scale) >> FIX;
  99. }
  100. for (; x < w - r; ++x) { // bulk middle run
  101. const uint16_t delta = in[x + r] - in[x - r - 1];
  102. out[x] = (delta * scale) >> FIX;
  103. }
  104. for (; x < w; ++x) { // right mirroring
  105. const uint16_t delta =
  106. 2 * in[w - 1] - in[2 * w - 2 - r - x] - in[x - r - 1];
  107. out[x] = (delta * scale) >> FIX;
  108. }
  109. }
  110. // emit one filtered output row
  111. static void ApplyFilter(SmoothParams* const p) {
  112. const uint16_t* const average = p->average_;
  113. const int w = p->width_;
  114. const int16_t* const correction = p->correction_;
  115. #if defined(USE_DITHERING)
  116. const uint8_t* const dither = kOrderedDither[p->row_ % DSIZE];
  117. #endif
  118. uint8_t* const dst = p->dst_;
  119. int x;
  120. for (x = 0; x < w; ++x) {
  121. const int v = dst[x];
  122. if (v < p->max_ && v > p->min_) {
  123. const int c = (v << DFIX) + correction[average[x] - (v << LFIX)];
  124. #if defined(USE_DITHERING)
  125. dst[x] = clip_8b(c + dither[x % DSIZE]);
  126. #else
  127. dst[x] = clip_8b(c);
  128. #endif
  129. }
  130. }
  131. p->dst_ += w; // advance output pointer
  132. }
  133. //------------------------------------------------------------------------------
  134. // Initialize correction table
  135. static void InitCorrectionLUT(int16_t* const lut, int min_dist) {
  136. // The correction curve is:
  137. // f(x) = x for x <= threshold2
  138. // f(x) = 0 for x >= threshold1
  139. // and a linear interpolation for range x=[threshold2, threshold1]
  140. // (along with f(-x) = -f(x) symmetry).
  141. // Note that: threshold2 = 3/4 * threshold1
  142. const int threshold1 = min_dist << LFIX;
  143. const int threshold2 = (3 * threshold1) >> 2;
  144. const int max_threshold = threshold2 << DFIX;
  145. const int delta = threshold1 - threshold2;
  146. int i;
  147. for (i = 1; i <= LUT_SIZE; ++i) {
  148. int c = (i <= threshold2) ? (i << DFIX)
  149. : (i < threshold1) ? max_threshold * (threshold1 - i) / delta
  150. : 0;
  151. c >>= LFIX;
  152. lut[+i] = +c;
  153. lut[-i] = -c;
  154. }
  155. lut[0] = 0;
  156. }
  157. static void CountLevels(const uint8_t* const data, int size,
  158. SmoothParams* const p) {
  159. int i, last_level;
  160. uint8_t used_levels[256] = { 0 };
  161. p->min_ = 255;
  162. p->max_ = 0;
  163. for (i = 0; i < size; ++i) {
  164. const int v = data[i];
  165. if (v < p->min_) p->min_ = v;
  166. if (v > p->max_) p->max_ = v;
  167. used_levels[v] = 1;
  168. }
  169. // Compute the mininum distance between two non-zero levels.
  170. p->min_level_dist_ = p->max_ - p->min_;
  171. last_level = -1;
  172. for (i = 0; i < 256; ++i) {
  173. if (used_levels[i]) {
  174. ++p->num_levels_;
  175. if (last_level >= 0) {
  176. const int level_dist = i - last_level;
  177. if (level_dist < p->min_level_dist_) {
  178. p->min_level_dist_ = level_dist;
  179. }
  180. }
  181. last_level = i;
  182. }
  183. }
  184. }
  185. // Initialize all params.
  186. static int InitParams(uint8_t* const data, int width, int height,
  187. int radius, SmoothParams* const p) {
  188. const int R = 2 * radius + 1; // total size of the kernel
  189. const size_t size_scratch_m = (R + 1) * width * sizeof(*p->start_);
  190. const size_t size_m = width * sizeof(*p->average_);
  191. const size_t size_lut = (1 + 2 * LUT_SIZE) * sizeof(*p->correction_);
  192. const size_t total_size = size_scratch_m + size_m + size_lut;
  193. uint8_t* mem = (uint8_t*)WebPSafeMalloc(1U, total_size);
  194. if (mem == NULL) return 0;
  195. p->mem_ = (void*)mem;
  196. p->start_ = (uint16_t*)mem;
  197. p->cur_ = p->start_;
  198. p->end_ = p->start_ + R * width;
  199. p->top_ = p->end_ - width;
  200. memset(p->top_, 0, width * sizeof(*p->top_));
  201. mem += size_scratch_m;
  202. p->average_ = (uint16_t*)mem;
  203. mem += size_m;
  204. p->width_ = width;
  205. p->height_ = height;
  206. p->src_ = data;
  207. p->dst_ = data;
  208. p->radius_ = radius;
  209. p->scale_ = (1 << (FIX + LFIX)) / (R * R); // normalization constant
  210. p->row_ = -radius;
  211. // analyze the input distribution so we can best-fit the threshold
  212. CountLevels(data, width * height, p);
  213. // correction table
  214. p->correction_ = ((int16_t*)mem) + LUT_SIZE;
  215. InitCorrectionLUT(p->correction_, p->min_level_dist_);
  216. return 1;
  217. }
  218. static void CleanupParams(SmoothParams* const p) {
  219. WebPSafeFree(p->mem_);
  220. }
  221. int WebPDequantizeLevels(uint8_t* const data, int width, int height,
  222. int strength) {
  223. const int radius = 4 * strength / 100;
  224. if (strength < 0 || strength > 100) return 0;
  225. if (data == NULL || width <= 0 || height <= 0) return 0; // bad params
  226. if (radius > 0) {
  227. SmoothParams p;
  228. memset(&p, 0, sizeof(p));
  229. if (!InitParams(data, width, height, radius, &p)) return 0;
  230. if (p.num_levels_ > 2) {
  231. for (; p.row_ < p.height_; ++p.row_) {
  232. VFilter(&p); // accumulate average of input
  233. // Need to wait few rows in order to prime the filter,
  234. // before emitting some output.
  235. if (p.row_ >= p.radius_) {
  236. HFilter(&p);
  237. ApplyFilter(&p);
  238. }
  239. }
  240. }
  241. CleanupParams(&p);
  242. }
  243. return 1;
  244. }