quant_levels_dec_utils.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_utils.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. https://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 stride_; // stride in bytes
  39. int row_; // current input row being processed
  40. uint8_t* src_; // input pointer
  41. uint8_t* dst_; // output pointer
  42. int radius_; // filter radius (=delay)
  43. int scale_; // normalization factor, in FIX bits precision
  44. void* mem_; // all memory
  45. // various scratch buffers
  46. uint16_t* start_;
  47. uint16_t* cur_;
  48. uint16_t* end_;
  49. uint16_t* top_;
  50. uint16_t* average_;
  51. // input levels distribution
  52. int num_levels_; // number of quantized levels
  53. int min_, max_; // min and max level values
  54. int min_level_dist_; // smallest distance between two consecutive levels
  55. int16_t* correction_; // size = 1 + 2*LUT_SIZE -> ~4k memory
  56. } SmoothParams;
  57. //------------------------------------------------------------------------------
  58. #define CLIP_8b_MASK (int)(~0U << (8 + DFIX))
  59. static WEBP_INLINE uint8_t clip_8b(int v) {
  60. return (!(v & CLIP_8b_MASK)) ? (uint8_t)(v >> DFIX) : (v < 0) ? 0u : 255u;
  61. }
  62. #undef CLIP_8b_MASK
  63. // vertical accumulation
  64. static void VFilter(SmoothParams* const p) {
  65. const uint8_t* src = p->src_;
  66. const int w = p->width_;
  67. uint16_t* const cur = p->cur_;
  68. const uint16_t* const top = p->top_;
  69. uint16_t* const out = p->end_;
  70. uint16_t sum = 0; // all arithmetic is modulo 16bit
  71. int x;
  72. for (x = 0; x < w; ++x) {
  73. uint16_t new_value;
  74. sum += src[x];
  75. new_value = top[x] + sum;
  76. out[x] = new_value - cur[x]; // vertical sum of 'r' pixels.
  77. cur[x] = new_value;
  78. }
  79. // move input pointers one row down
  80. p->top_ = p->cur_;
  81. p->cur_ += w;
  82. if (p->cur_ == p->end_) p->cur_ = p->start_; // roll-over
  83. // We replicate edges, as it's somewhat easier as a boundary condition.
  84. // That's why we don't update the 'src' pointer on top/bottom area:
  85. if (p->row_ >= 0 && p->row_ < p->height_ - 1) {
  86. p->src_ += p->stride_;
  87. }
  88. }
  89. // horizontal accumulation. We use mirror replication of missing pixels, as it's
  90. // a little easier to implement (surprisingly).
  91. static void HFilter(SmoothParams* const p) {
  92. const uint16_t* const in = p->end_;
  93. uint16_t* const out = p->average_;
  94. const uint32_t scale = p->scale_;
  95. const int w = p->width_;
  96. const int r = p->radius_;
  97. int x;
  98. for (x = 0; x <= r; ++x) { // left mirroring
  99. const uint16_t delta = in[x + r - 1] + in[r - x];
  100. out[x] = (delta * scale) >> FIX;
  101. }
  102. for (; x < w - r; ++x) { // bulk middle run
  103. const uint16_t delta = in[x + r] - in[x - r - 1];
  104. out[x] = (delta * scale) >> FIX;
  105. }
  106. for (; x < w; ++x) { // right mirroring
  107. const uint16_t delta =
  108. 2 * in[w - 1] - in[2 * w - 2 - r - x] - in[x - r - 1];
  109. out[x] = (delta * scale) >> FIX;
  110. }
  111. }
  112. // emit one filtered output row
  113. static void ApplyFilter(SmoothParams* const p) {
  114. const uint16_t* const average = p->average_;
  115. const int w = p->width_;
  116. const int16_t* const correction = p->correction_;
  117. #if defined(USE_DITHERING)
  118. const uint8_t* const dither = kOrderedDither[p->row_ % DSIZE];
  119. #endif
  120. uint8_t* const dst = p->dst_;
  121. int x;
  122. for (x = 0; x < w; ++x) {
  123. const int v = dst[x];
  124. if (v < p->max_ && v > p->min_) {
  125. const int c = (v << DFIX) + correction[average[x] - (v << LFIX)];
  126. #if defined(USE_DITHERING)
  127. dst[x] = clip_8b(c + dither[x % DSIZE]);
  128. #else
  129. dst[x] = clip_8b(c);
  130. #endif
  131. }
  132. }
  133. p->dst_ += p->stride_; // advance output pointer
  134. }
  135. //------------------------------------------------------------------------------
  136. // Initialize correction table
  137. static void InitCorrectionLUT(int16_t* const lut, int min_dist) {
  138. // The correction curve is:
  139. // f(x) = x for x <= threshold2
  140. // f(x) = 0 for x >= threshold1
  141. // and a linear interpolation for range x=[threshold2, threshold1]
  142. // (along with f(-x) = -f(x) symmetry).
  143. // Note that: threshold2 = 3/4 * threshold1
  144. const int threshold1 = min_dist << LFIX;
  145. const int threshold2 = (3 * threshold1) >> 2;
  146. const int max_threshold = threshold2 << DFIX;
  147. const int delta = threshold1 - threshold2;
  148. int i;
  149. for (i = 1; i <= LUT_SIZE; ++i) {
  150. int c = (i <= threshold2) ? (i << DFIX)
  151. : (i < threshold1) ? max_threshold * (threshold1 - i) / delta
  152. : 0;
  153. c >>= LFIX;
  154. lut[+i] = +c;
  155. lut[-i] = -c;
  156. }
  157. lut[0] = 0;
  158. }
  159. static void CountLevels(SmoothParams* const p) {
  160. int i, j, last_level;
  161. uint8_t used_levels[256] = { 0 };
  162. const uint8_t* data = p->src_;
  163. p->min_ = 255;
  164. p->max_ = 0;
  165. for (j = 0; j < p->height_; ++j) {
  166. for (i = 0; i < p->width_; ++i) {
  167. const int v = data[i];
  168. if (v < p->min_) p->min_ = v;
  169. if (v > p->max_) p->max_ = v;
  170. used_levels[v] = 1;
  171. }
  172. data += p->stride_;
  173. }
  174. // Compute the mininum distance between two non-zero levels.
  175. p->min_level_dist_ = p->max_ - p->min_;
  176. last_level = -1;
  177. for (i = 0; i < 256; ++i) {
  178. if (used_levels[i]) {
  179. ++p->num_levels_;
  180. if (last_level >= 0) {
  181. const int level_dist = i - last_level;
  182. if (level_dist < p->min_level_dist_) {
  183. p->min_level_dist_ = level_dist;
  184. }
  185. }
  186. last_level = i;
  187. }
  188. }
  189. }
  190. // Initialize all params.
  191. static int InitParams(uint8_t* const data, int width, int height, int stride,
  192. int radius, SmoothParams* const p) {
  193. const int R = 2 * radius + 1; // total size of the kernel
  194. const size_t size_scratch_m = (R + 1) * width * sizeof(*p->start_);
  195. const size_t size_m = width * sizeof(*p->average_);
  196. const size_t size_lut = (1 + 2 * LUT_SIZE) * sizeof(*p->correction_);
  197. const size_t total_size = size_scratch_m + size_m + size_lut;
  198. uint8_t* mem = (uint8_t*)WebPSafeMalloc(1U, total_size);
  199. if (mem == NULL) return 0;
  200. p->mem_ = (void*)mem;
  201. p->start_ = (uint16_t*)mem;
  202. p->cur_ = p->start_;
  203. p->end_ = p->start_ + R * width;
  204. p->top_ = p->end_ - width;
  205. memset(p->top_, 0, width * sizeof(*p->top_));
  206. mem += size_scratch_m;
  207. p->average_ = (uint16_t*)mem;
  208. mem += size_m;
  209. p->width_ = width;
  210. p->height_ = height;
  211. p->stride_ = stride;
  212. p->src_ = data;
  213. p->dst_ = data;
  214. p->radius_ = radius;
  215. p->scale_ = (1 << (FIX + LFIX)) / (R * R); // normalization constant
  216. p->row_ = -radius;
  217. // analyze the input distribution so we can best-fit the threshold
  218. CountLevels(p);
  219. // correction table
  220. p->correction_ = ((int16_t*)mem) + LUT_SIZE;
  221. InitCorrectionLUT(p->correction_, p->min_level_dist_);
  222. return 1;
  223. }
  224. static void CleanupParams(SmoothParams* const p) {
  225. WebPSafeFree(p->mem_);
  226. }
  227. int WebPDequantizeLevels(uint8_t* const data, int width, int height, int stride,
  228. int strength) {
  229. int radius = 4 * strength / 100;
  230. if (strength < 0 || strength > 100) return 0;
  231. if (data == NULL || width <= 0 || height <= 0) return 0; // bad params
  232. // limit the filter size to not exceed the image dimensions
  233. if (2 * radius + 1 > width) radius = (width - 1) >> 1;
  234. if (2 * radius + 1 > height) radius = (height - 1) >> 1;
  235. if (radius > 0) {
  236. SmoothParams p;
  237. memset(&p, 0, sizeof(p));
  238. if (!InitParams(data, width, height, stride, radius, &p)) return 0;
  239. if (p.num_levels_ > 2) {
  240. for (; p.row_ < p.height_; ++p.row_) {
  241. VFilter(&p); // accumulate average of input
  242. // Need to wait few rows in order to prime the filter,
  243. // before emitting some output.
  244. if (p.row_ >= p.radius_) {
  245. HFilter(&p);
  246. ApplyFilter(&p);
  247. }
  248. }
  249. }
  250. CleanupParams(&p);
  251. }
  252. return 1;
  253. }