go-libwebp

Experimental translation from libwebp to Go source.
Log | Files | Refs | README | LICENSE

filters_utils.c (2588B)


      1 // Copyright 2011 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 // filter estimation
     11 //
     12 // Author: Urvang (urvang@google.com)
     13 
     14 #include <stdlib.h>
     15 #include <string.h>
     16 
     17 #include "src/dsp/dsp.h"
     18 #include "src/webp/types.h"
     19 #include "src/utils/filters_utils.h"
     20 
     21 // -----------------------------------------------------------------------------
     22 // Quick estimate of a potentially interesting filter mode to try.
     23 
     24 #define SMAX 16
     25 #define SDIFF(a, b) (abs((a) - (b)) >> 4)   // Scoring diff, in [0..SMAX)
     26 
     27 static WEBP_INLINE int GradientPredictor(uint8_t a, uint8_t b, uint8_t c) {
     28   const int g = a + b - c;
     29   return ((g & ~0xff) == 0) ? g : (g < 0) ? 0 : 255;  // clip to 8bit
     30 }
     31 
     32 WEBP_FILTER_TYPE WebPEstimateBestFilter(const uint8_t* data,
     33                                         int width, int height, int stride) {
     34   int i, j;
     35   int bins[WEBP_FILTER_LAST][SMAX];
     36   memset(bins, 0, sizeof(bins));
     37 
     38   // We only sample every other pixels. That's enough.
     39   for (j = 2; j < height - 1; j += 2) {
     40     const uint8_t* const p = data + j * stride;
     41     int mean = p[0];
     42     for (i = 2; i < width - 1; i += 2) {
     43       const int diff0 = SDIFF(p[i], mean);
     44       const int diff1 = SDIFF(p[i], p[i - 1]);
     45       const int diff2 = SDIFF(p[i], p[i - width]);
     46       const int grad_pred =
     47           GradientPredictor(p[i - 1], p[i - width], p[i - width - 1]);
     48       const int diff3 = SDIFF(p[i], grad_pred);
     49       bins[WEBP_FILTER_NONE][diff0] = 1;
     50       bins[WEBP_FILTER_HORIZONTAL][diff1] = 1;
     51       bins[WEBP_FILTER_VERTICAL][diff2] = 1;
     52       bins[WEBP_FILTER_GRADIENT][diff3] = 1;
     53       mean = (3 * mean + p[i] + 2) >> 2;
     54     }
     55   }
     56   {
     57     int filter;
     58     WEBP_FILTER_TYPE best_filter = WEBP_FILTER_NONE;
     59     int best_score = 0x7fffffff;
     60     for (filter = WEBP_FILTER_NONE; filter < WEBP_FILTER_LAST; ++filter) {
     61       int score = 0;
     62       for (i = 0; i < SMAX; ++i) {
     63         if (bins[filter][i] > 0) {
     64           score += i;
     65         }
     66       }
     67       if (score < best_score) {
     68         best_score = score;
     69         best_filter = (WEBP_FILTER_TYPE)filter;
     70       }
     71     }
     72     return best_filter;
     73   }
     74 }
     75 
     76 #undef SMAX
     77 #undef SDIFF
     78 
     79 //------------------------------------------------------------------------------