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