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