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

source code of qtimageformats/src/3rdparty/libwebp/src/utils/quant_levels_dec_utils.c