mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-19 07:20:02 +02:00
Introduce a 'fast' alpha mode
.. where only 2 filtering modes are potentially tried, instead of all of them. This is fast than the exhaustive 'best' mode, and not much worse. Options for cwebp are: -alpha_filter none -alpha_filter fast (<- default) -alpha_filter best (<- slow) Change-Id: I8cb90ee11b8f981811e013ea4ad5bf72ba3ea7d4
This commit is contained in:
@ -115,6 +115,11 @@ static void VerticalUnfilter(const uint8_t* data, int width, int height,
|
||||
//------------------------------------------------------------------------------
|
||||
// Gradient filter.
|
||||
|
||||
static WEBP_INLINE uint8_t GradientPredictor(uint8_t a, uint8_t b, uint8_t c) {
|
||||
const int g = a + b - c;
|
||||
return (g < 0) ? 0 : (g > 255) ? 255 : (uint8_t)g;
|
||||
}
|
||||
|
||||
static void GradientFilter(const uint8_t* data, int width, int height,
|
||||
int bpp, int stride, uint8_t* filtered_data) {
|
||||
int h;
|
||||
@ -131,9 +136,9 @@ static void GradientFilter(const uint8_t* data, int width, int height,
|
||||
const uint8_t* const prev_line = scan_line - stride;
|
||||
memcpy((void*)out, (const void*)scan_line, bpp);
|
||||
for (w = bpp; w < width * bpp; ++w) {
|
||||
const uint8_t predictor = scan_line[w - bpp] + prev_line[w] -
|
||||
prev_line[w - bpp];
|
||||
out[w] = scan_line[w] - predictor;
|
||||
out[w] = scan_line[w] - GradientPredictor(scan_line[w - bpp],
|
||||
prev_line[w],
|
||||
prev_line[w - bpp]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -154,110 +159,81 @@ static void GradientUnfilter(const uint8_t* data, int width, int height,
|
||||
const uint8_t* const out_prev_line = out - stride;
|
||||
memcpy((void*)out, (const void*)scan_line, bpp);
|
||||
for (w = bpp; w < width * bpp; ++w) {
|
||||
const uint8_t predictor = out[w - bpp] + out_prev_line[w] -
|
||||
out_prev_line[w - bpp];
|
||||
out[w] = scan_line[w] + predictor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Paeth filter.
|
||||
|
||||
static WEBP_INLINE int AbsDiff(int a, int b) {
|
||||
return (a > b) ? a - b : b - a;
|
||||
}
|
||||
|
||||
static WEBP_INLINE uint8_t PaethPredictor(uint8_t a, uint8_t b, uint8_t c) {
|
||||
const int p = a + b - c; // Base.
|
||||
const int pa = AbsDiff(p, a);
|
||||
const int pb = AbsDiff(p, b);
|
||||
const int pc = AbsDiff(p, c);
|
||||
|
||||
// Return nearest to base of a, b, c.
|
||||
return (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
|
||||
}
|
||||
|
||||
static void PaethFilter(const uint8_t* data, int width, int height,
|
||||
int bpp, int stride, uint8_t* filtered_data) {
|
||||
int w;
|
||||
int h;
|
||||
SANITY_CHECK(data, filtered_data);
|
||||
|
||||
// Top scan line (special case).
|
||||
memcpy((void*)filtered_data, (const void*)data, bpp);
|
||||
for (w = bpp; w < width * bpp; ++w) {
|
||||
// Note: PaethPredictor(scan_line[w - bpp], 0, 0) == scan_line[w - bpp].
|
||||
filtered_data[w] = data[w] - data[w - bpp];
|
||||
}
|
||||
|
||||
// Filter line-by-line.
|
||||
for (h = 1; h < height; ++h) {
|
||||
int w;
|
||||
const uint8_t* const scan_line = data + h * stride;
|
||||
uint8_t* const out = filtered_data + h * stride;
|
||||
const uint8_t* const prev_line = scan_line - stride;
|
||||
for (w = 0; w < bpp; ++w) {
|
||||
// Note: PaethPredictor(0, prev_line[w], 0) == prev_line[w].
|
||||
out[w] = scan_line[w] - prev_line[w];
|
||||
}
|
||||
for (w = bpp; w < width * bpp; ++w) {
|
||||
out[w] = scan_line[w] - PaethPredictor(scan_line[w - bpp], prev_line[w],
|
||||
prev_line[w - bpp]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void PaethUnfilter(const uint8_t* data, int width, int height,
|
||||
int bpp, int stride, uint8_t* recon_data) {
|
||||
int w;
|
||||
int h;
|
||||
SANITY_CHECK(data, recon_data);
|
||||
|
||||
// Top scan line (special case).
|
||||
memcpy((void*)recon_data, (const void*)data, bpp);
|
||||
for (w = bpp; w < width * bpp; ++w) {
|
||||
// Note: PaethPredictor(out[w - bpp], 0, 0) == out[w - bpp].
|
||||
recon_data[w] = data[w] + recon_data[w - bpp];
|
||||
}
|
||||
|
||||
// Unfilter line-by-line.
|
||||
for (h = 1; h < height; ++h) {
|
||||
int w;
|
||||
const uint8_t* const scan_line = data + h * stride;
|
||||
uint8_t* const out = recon_data + h * stride;
|
||||
const uint8_t* const out_prev = out - stride;
|
||||
for (w = 0; w < bpp; ++w) {
|
||||
// Note: PaethPredictor(0, out_prev[w], 0) == out_prev[w].
|
||||
out[w] = scan_line[w] + out_prev[w];
|
||||
}
|
||||
for (w = bpp; w < width * bpp; ++w) {
|
||||
out[w] = scan_line[w] + PaethPredictor(out[w - bpp], out_prev[w],
|
||||
out_prev[w - bpp]);
|
||||
out[w] = scan_line[w] + GradientPredictor(out[w - bpp],
|
||||
out_prev_line[w],
|
||||
out_prev_line[w - bpp]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#undef SANITY_CHECK
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Quick estimate of a potentially interesting filter mode to try, in addition
|
||||
// to the default NONE.
|
||||
|
||||
#define SMAX 16
|
||||
#define SDIFF(a, b) (abs((a) - (b)) >> 4) // Scoring diff, in [0..SMAX)
|
||||
|
||||
WEBP_FILTER_TYPE EstimateBestFilter(const uint8_t* data,
|
||||
int width, int height, int stride) {
|
||||
int i, j;
|
||||
int bins[WEBP_FILTER_LAST][SMAX];
|
||||
memset(bins, 0, sizeof(bins));
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
{
|
||||
WEBP_FILTER_TYPE filter, best_filter = WEBP_FILTER_NONE;
|
||||
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;
|
||||
best_filter = filter;
|
||||
}
|
||||
}
|
||||
return best_filter;
|
||||
}
|
||||
}
|
||||
|
||||
#undef SMAX
|
||||
#undef SDIFF
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const WebPFilterFunc WebPFilters[WEBP_FILTER_LAST] = {
|
||||
NULL, // WEBP_FILTER_NONE
|
||||
HorizontalFilter, // WEBP_FILTER_HORIZONTAL
|
||||
VerticalFilter, // WEBP_FILTER_VERTICAL
|
||||
GradientFilter, // WEBP_FILTER_GRADIENT
|
||||
PaethFilter, // WEBP_FILTER_PAETH
|
||||
NULL // WEBP_FILTER_BEST
|
||||
GradientFilter // WEBP_FILTER_GRADIENT
|
||||
};
|
||||
|
||||
const WebPFilterFunc WebPUnfilters[WEBP_FILTER_LAST] = {
|
||||
NULL, // WEBP_FILTER_NONE
|
||||
HorizontalUnfilter, // WEBP_FILTER_HORIZONTAL
|
||||
VerticalUnfilter, // WEBP_FILTER_VERTICAL
|
||||
GradientUnfilter, // WEBP_FILTER_GRADIENT
|
||||
PaethUnfilter, // WEBP_FILTER_PAETH
|
||||
NULL // WEBP_FILTER_BEST
|
||||
GradientUnfilter // WEBP_FILTER_GRADIENT
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user