Path: blob/master/thirdparty/libwebp/src/utils/quant_levels_dec_utils.c
9912 views
// Copyright 2013 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Implement gradient smoothing: we replace a current alpha value by its10// surrounding average if it's close enough (that is: the change will be less11// than the minimum distance between two quantized level).12// We use sliding window for computing the 2d moving average.13//14// Author: Skal ([email protected])1516#include "src/utils/quant_levels_dec_utils.h"1718#include <string.h> // for memset1920#include "src/utils/utils.h"2122// #define USE_DITHERING // uncomment to enable ordered dithering (not vital)2324#define FIX 16 // fix-point precision for averaging25#define LFIX 2 // extra precision for look-up table26#define LUT_SIZE ((1 << (8 + LFIX)) - 1) // look-up table size2728#if defined(USE_DITHERING)2930#define DFIX 4 // extra precision for ordered dithering31#define DSIZE 4 // dithering size (must be a power of two)32// cf. https://en.wikipedia.org/wiki/Ordered_dithering33static const uint8_t kOrderedDither[DSIZE][DSIZE] = {34{ 0, 8, 2, 10 }, // coefficients are in DFIX fixed-point precision35{ 12, 4, 14, 6 },36{ 3, 11, 1, 9 },37{ 15, 7, 13, 5 }38};3940#else41#define DFIX 042#endif4344typedef struct {45int width_, height_; // dimension46int stride_; // stride in bytes47int row_; // current input row being processed48uint8_t* src_; // input pointer49uint8_t* dst_; // output pointer5051int radius_; // filter radius (=delay)52int scale_; // normalization factor, in FIX bits precision5354void* mem_; // all memory5556// various scratch buffers57uint16_t* start_;58uint16_t* cur_;59uint16_t* end_;60uint16_t* top_;61uint16_t* average_;6263// input levels distribution64int num_levels_; // number of quantized levels65int min_, max_; // min and max level values66int min_level_dist_; // smallest distance between two consecutive levels6768int16_t* correction_; // size = 1 + 2*LUT_SIZE -> ~4k memory69} SmoothParams;7071//------------------------------------------------------------------------------7273#define CLIP_8b_MASK (int)(~0U << (8 + DFIX))74static WEBP_INLINE uint8_t clip_8b(int v) {75return (!(v & CLIP_8b_MASK)) ? (uint8_t)(v >> DFIX) : (v < 0) ? 0u : 255u;76}77#undef CLIP_8b_MASK7879// vertical accumulation80static void VFilter(SmoothParams* const p) {81const uint8_t* src = p->src_;82const int w = p->width_;83uint16_t* const cur = p->cur_;84const uint16_t* const top = p->top_;85uint16_t* const out = p->end_;86uint16_t sum = 0; // all arithmetic is modulo 16bit87int x;8889for (x = 0; x < w; ++x) {90uint16_t new_value;91sum += src[x];92new_value = top[x] + sum;93out[x] = new_value - cur[x]; // vertical sum of 'r' pixels.94cur[x] = new_value;95}96// move input pointers one row down97p->top_ = p->cur_;98p->cur_ += w;99if (p->cur_ == p->end_) p->cur_ = p->start_; // roll-over100// We replicate edges, as it's somewhat easier as a boundary condition.101// That's why we don't update the 'src' pointer on top/bottom area:102if (p->row_ >= 0 && p->row_ < p->height_ - 1) {103p->src_ += p->stride_;104}105}106107// horizontal accumulation. We use mirror replication of missing pixels, as it's108// a little easier to implement (surprisingly).109static void HFilter(SmoothParams* const p) {110const uint16_t* const in = p->end_;111uint16_t* const out = p->average_;112const uint32_t scale = p->scale_;113const int w = p->width_;114const int r = p->radius_;115116int x;117for (x = 0; x <= r; ++x) { // left mirroring118const uint16_t delta = in[x + r - 1] + in[r - x];119out[x] = (delta * scale) >> FIX;120}121for (; x < w - r; ++x) { // bulk middle run122const uint16_t delta = in[x + r] - in[x - r - 1];123out[x] = (delta * scale) >> FIX;124}125for (; x < w; ++x) { // right mirroring126const uint16_t delta =1272 * in[w - 1] - in[2 * w - 2 - r - x] - in[x - r - 1];128out[x] = (delta * scale) >> FIX;129}130}131132// emit one filtered output row133static void ApplyFilter(SmoothParams* const p) {134const uint16_t* const average = p->average_;135const int w = p->width_;136const int16_t* const correction = p->correction_;137#if defined(USE_DITHERING)138const uint8_t* const dither = kOrderedDither[p->row_ % DSIZE];139#endif140uint8_t* const dst = p->dst_;141int x;142for (x = 0; x < w; ++x) {143const int v = dst[x];144if (v < p->max_ && v > p->min_) {145const int c = (v << DFIX) + correction[average[x] - (v << LFIX)];146#if defined(USE_DITHERING)147dst[x] = clip_8b(c + dither[x % DSIZE]);148#else149dst[x] = clip_8b(c);150#endif151}152}153p->dst_ += p->stride_; // advance output pointer154}155156//------------------------------------------------------------------------------157// Initialize correction table158159static void InitCorrectionLUT(int16_t* const lut, int min_dist) {160// The correction curve is:161// f(x) = x for x <= threshold2162// f(x) = 0 for x >= threshold1163// and a linear interpolation for range x=[threshold2, threshold1]164// (along with f(-x) = -f(x) symmetry).165// Note that: threshold2 = 3/4 * threshold1166const int threshold1 = min_dist << LFIX;167const int threshold2 = (3 * threshold1) >> 2;168const int max_threshold = threshold2 << DFIX;169const int delta = threshold1 - threshold2;170int i;171for (i = 1; i <= LUT_SIZE; ++i) {172int c = (i <= threshold2) ? (i << DFIX)173: (i < threshold1) ? max_threshold * (threshold1 - i) / delta174: 0;175c >>= LFIX;176lut[+i] = +c;177lut[-i] = -c;178}179lut[0] = 0;180}181182static void CountLevels(SmoothParams* const p) {183int i, j, last_level;184uint8_t used_levels[256] = { 0 };185const uint8_t* data = p->src_;186p->min_ = 255;187p->max_ = 0;188for (j = 0; j < p->height_; ++j) {189for (i = 0; i < p->width_; ++i) {190const int v = data[i];191if (v < p->min_) p->min_ = v;192if (v > p->max_) p->max_ = v;193used_levels[v] = 1;194}195data += p->stride_;196}197// Compute the mininum distance between two non-zero levels.198p->min_level_dist_ = p->max_ - p->min_;199last_level = -1;200for (i = 0; i < 256; ++i) {201if (used_levels[i]) {202++p->num_levels_;203if (last_level >= 0) {204const int level_dist = i - last_level;205if (level_dist < p->min_level_dist_) {206p->min_level_dist_ = level_dist;207}208}209last_level = i;210}211}212}213214// Initialize all params.215static int InitParams(uint8_t* const data, int width, int height, int stride,216int radius, SmoothParams* const p) {217const int R = 2 * radius + 1; // total size of the kernel218219const size_t size_scratch_m = (R + 1) * width * sizeof(*p->start_);220const size_t size_m = width * sizeof(*p->average_);221const size_t size_lut = (1 + 2 * LUT_SIZE) * sizeof(*p->correction_);222const size_t total_size = size_scratch_m + size_m + size_lut;223uint8_t* mem = (uint8_t*)WebPSafeMalloc(1U, total_size);224225if (mem == NULL) return 0;226p->mem_ = (void*)mem;227228p->start_ = (uint16_t*)mem;229p->cur_ = p->start_;230p->end_ = p->start_ + R * width;231p->top_ = p->end_ - width;232memset(p->top_, 0, width * sizeof(*p->top_));233mem += size_scratch_m;234235p->average_ = (uint16_t*)mem;236mem += size_m;237238p->width_ = width;239p->height_ = height;240p->stride_ = stride;241p->src_ = data;242p->dst_ = data;243p->radius_ = radius;244p->scale_ = (1 << (FIX + LFIX)) / (R * R); // normalization constant245p->row_ = -radius;246247// analyze the input distribution so we can best-fit the threshold248CountLevels(p);249250// correction table251p->correction_ = ((int16_t*)mem) + LUT_SIZE;252InitCorrectionLUT(p->correction_, p->min_level_dist_);253254return 1;255}256257static void CleanupParams(SmoothParams* const p) {258WebPSafeFree(p->mem_);259}260261int WebPDequantizeLevels(uint8_t* const data, int width, int height, int stride,262int strength) {263int radius = 4 * strength / 100;264265if (strength < 0 || strength > 100) return 0;266if (data == NULL || width <= 0 || height <= 0) return 0; // bad params267268// limit the filter size to not exceed the image dimensions269if (2 * radius + 1 > width) radius = (width - 1) >> 1;270if (2 * radius + 1 > height) radius = (height - 1) >> 1;271272if (radius > 0) {273SmoothParams p;274memset(&p, 0, sizeof(p));275if (!InitParams(data, width, height, stride, radius, &p)) return 0;276if (p.num_levels_ > 2) {277for (; p.row_ < p.height_; ++p.row_) {278VFilter(&p); // accumulate average of input279// Need to wait few rows in order to prime the filter,280// before emitting some output.281if (p.row_ >= p.radius_) {282HFilter(&p);283ApplyFilter(&p);284}285}286}287CleanupParams(&p);288}289return 1;290}291292293