2012-01-06 14:49:06 -08:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
2012-01-05 13:04:30 +05:30
|
|
|
//
|
2013-06-06 23:05:58 -07:00
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
2012-01-05 13:04:30 +05:30
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
2015-01-28 08:02:41 +01:00
|
|
|
// filter estimation
|
2012-01-05 13:04:30 +05:30
|
|
|
//
|
|
|
|
// Author: Urvang (urvang@google.com)
|
|
|
|
|
|
|
|
#include "./filters.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-28 08:02:41 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Quick estimate of a potentially interesting filter mode to try.
|
2012-01-05 13:04:30 +05:30
|
|
|
|
2015-01-28 08:02:41 +01:00
|
|
|
#define SMAX 16
|
|
|
|
#define SDIFF(a, b) (abs((a) - (b)) >> 4) // Scoring diff, in [0..SMAX)
|
2012-01-05 13:04:30 +05:30
|
|
|
|
2012-01-10 03:18:41 -08:00
|
|
|
static WEBP_INLINE int GradientPredictor(uint8_t a, uint8_t b, uint8_t c) {
|
2012-01-08 19:27:21 -08:00
|
|
|
const int g = a + b - c;
|
2013-03-14 20:26:39 +01:00
|
|
|
return ((g & ~0xff) == 0) ? g : (g < 0) ? 0 : 255; // clip to 8bit
|
2012-01-08 19:27:21 -08:00
|
|
|
}
|
|
|
|
|
2015-01-28 08:02:41 +01:00
|
|
|
WEBP_FILTER_TYPE WebPEstimateBestFilter(const uint8_t* data,
|
|
|
|
int width, int height, int stride) {
|
2012-01-08 19:27:21 -08:00
|
|
|
int i, j;
|
|
|
|
int bins[WEBP_FILTER_LAST][SMAX];
|
|
|
|
memset(bins, 0, sizeof(bins));
|
2013-05-08 17:19:04 -07:00
|
|
|
|
2012-01-08 19:27:21 -08:00
|
|
|
// We only sample every other pixels. That's enough.
|
|
|
|
for (j = 2; j < height - 1; j += 2) {
|
|
|
|
const uint8_t* const p = data + j * stride;
|
|
|
|
int mean = p[0];
|
|
|
|
for (i = 2; i < width - 1; i += 2) {
|
|
|
|
const int diff0 = SDIFF(p[i], mean);
|
|
|
|
const int diff1 = SDIFF(p[i], p[i - 1]);
|
|
|
|
const int diff2 = SDIFF(p[i], p[i - width]);
|
|
|
|
const int grad_pred =
|
|
|
|
GradientPredictor(p[i - 1], p[i - width], p[i - width - 1]);
|
|
|
|
const int diff3 = SDIFF(p[i], grad_pred);
|
|
|
|
bins[WEBP_FILTER_NONE][diff0] = 1;
|
|
|
|
bins[WEBP_FILTER_HORIZONTAL][diff1] = 1;
|
|
|
|
bins[WEBP_FILTER_VERTICAL][diff2] = 1;
|
|
|
|
bins[WEBP_FILTER_GRADIENT][diff3] = 1;
|
|
|
|
mean = (3 * mean + p[i] + 2) >> 2;
|
2012-01-05 13:04:30 +05:30
|
|
|
}
|
|
|
|
}
|
2012-01-08 19:27:21 -08:00
|
|
|
{
|
2013-11-27 20:10:45 -08:00
|
|
|
int filter;
|
|
|
|
WEBP_FILTER_TYPE best_filter = WEBP_FILTER_NONE;
|
2012-01-08 19:27:21 -08:00
|
|
|
int best_score = 0x7fffffff;
|
|
|
|
for (filter = WEBP_FILTER_NONE; filter < WEBP_FILTER_LAST; ++filter) {
|
|
|
|
int score = 0;
|
|
|
|
for (i = 0; i < SMAX; ++i) {
|
|
|
|
if (bins[filter][i] > 0) {
|
|
|
|
score += i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (score < best_score) {
|
|
|
|
best_score = score;
|
2013-11-27 20:10:45 -08:00
|
|
|
best_filter = (WEBP_FILTER_TYPE)filter;
|
2012-01-08 19:27:21 -08:00
|
|
|
}
|
2012-01-05 13:04:30 +05:30
|
|
|
}
|
2012-01-08 19:27:21 -08:00
|
|
|
return best_filter;
|
2012-01-05 13:04:30 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-08 19:27:21 -08:00
|
|
|
#undef SMAX
|
|
|
|
#undef SDIFF
|
2012-01-05 13:04:30 +05:30
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|