Harmonize the alpha-filter predictions at boundary

often reduces compressed size by ~10's of bytes
+ refactored / sped-up the prediction code (gradient: ~30% faster)

Change-Id: I26bd983655dad4f85d5c5ddc20a1980f384c4dd6
This commit is contained in:
Pascal Massimino 2012-01-10 03:18:41 -08:00
parent 3a9895340f
commit 57cab7b891

View File

@ -30,140 +30,127 @@ extern "C" {
assert(bpp > 0); \ assert(bpp > 0); \
assert(stride >= width * bpp); assert(stride >= width * bpp);
static WEBP_INLINE void PredictLine(const uint8_t* src, const uint8_t* pred,
uint8_t* dst, int length, int inverse) {
int i;
if (inverse) {
for (i = 0; i < length; ++i) dst[i] = src[i] + pred[i];
} else {
for (i = 0; i < length; ++i) dst[i] = src[i] - pred[i];
}
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Horizontal filter. // Horizontal filter.
static void HorizontalFilter(const uint8_t* data, int width, int height, static WEBP_INLINE void DoHorizontalFilter(const uint8_t* in,
int bpp, int stride, uint8_t* filtered_data) { int width, int height, int bpp, int stride, int inverse, uint8_t* out) {
int h; int h;
SANITY_CHECK(data, filtered_data); const uint8_t* preds = (inverse ? out : in);
SANITY_CHECK(in, out);
// Filter line-by-line. // Filter line-by-line.
for (h = 0; h < height; ++h) { for (h = 0; h < height; ++h) {
int w; // Leftmost pixel is predicted from above (except for topmost scanline).
const uint8_t* const scan_line = data + h * stride; if (h == 0) {
uint8_t* const out = filtered_data + h * stride; memcpy((void*)out, (const void*)in, bpp);
} else {
memcpy((void*)out, (const void*)scan_line, bpp); PredictLine(in, preds - stride, out, bpp, inverse);
for (w = bpp; w < width * bpp; ++w) {
out[w] = scan_line[w] - scan_line[w - bpp];
} }
PredictLine(in + bpp, preds, out + bpp, bpp * (width - 1), inverse);
preds += stride;
in += stride;
out += stride;
} }
} }
static void HorizontalFilter(const uint8_t* data, int width, int height,
int bpp, int stride, uint8_t* filtered_data) {
DoHorizontalFilter(data, width, height, bpp, stride, 0, filtered_data);
}
static void HorizontalUnfilter(const uint8_t* data, int width, int height, static void HorizontalUnfilter(const uint8_t* data, int width, int height,
int bpp, int stride, uint8_t* recon_data) { int bpp, int stride, uint8_t* recon_data) {
int h; DoHorizontalFilter(data, width, height, bpp, stride, 1, recon_data);
SANITY_CHECK(data, recon_data);
// Unfilter line-by-line.
for (h = 0; h < height; ++h) {
int w;
const uint8_t* const scan_line = data + h * stride;
uint8_t* const out = recon_data + h * stride;
memcpy((void*)out, (const void*)scan_line, bpp);
for (w = bpp; w < width * bpp; ++w) {
out[w] = scan_line[w] + out[w - bpp];
}
}
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Vertical filter. // Vertical filter.
static void VerticalFilter(const uint8_t* data, int width, int height, static WEBP_INLINE void DoVerticalFilter(const uint8_t* in,
int bpp, int stride, uint8_t* filtered_data) { int width, int height, int bpp, int stride, int inverse, uint8_t* out) {
int h; int h;
SANITY_CHECK(data, filtered_data); const uint8_t* preds = (inverse ? out : in);
SANITY_CHECK(in, out);
// Copy top scan-line as it is. // Very first top-left pixel is copied.
memcpy((void*)filtered_data, (const void*)data, width * bpp); memcpy((void*)out, (const void*)in, bpp);
// Rest of top scan-line is left-predicted.
PredictLine(in + bpp, preds, out + bpp, bpp * (width - 1), inverse);
// Filter line-by-line. // Filter line-by-line.
for (h = 1; h < height; ++h) { for (h = 1; h < height; ++h) {
int w; in += stride;
const uint8_t* const scan_line = data + h * stride; out += stride;
uint8_t* const out = filtered_data + h * stride; PredictLine(in, preds, out, bpp * width, inverse);
const uint8_t* const prev_line = scan_line - stride; preds += stride;
for (w = 0; w < width * bpp; ++w) {
out[w] = scan_line[w] - prev_line[w];
}
} }
} }
static void VerticalFilter(const uint8_t* data, int width, int height,
int bpp, int stride, uint8_t* filtered_data) {
DoVerticalFilter(data, width, height, bpp, stride, 0, filtered_data);
}
static void VerticalUnfilter(const uint8_t* data, int width, int height, static void VerticalUnfilter(const uint8_t* data, int width, int height,
int bpp, int stride, uint8_t* recon_data) { int bpp, int stride, uint8_t* recon_data) {
int h; DoVerticalFilter(data, width, height, bpp, stride, 1, recon_data);
SANITY_CHECK(data, recon_data);
// Copy top scan-line as it is.
memcpy((void*)recon_data, (const void*)data, width * 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_line = out - stride;
for (w = 0; w < width * bpp; ++w) {
out[w] = scan_line[w] + out_prev_line[w];
}
}
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Gradient filter. // Gradient filter.
static WEBP_INLINE uint8_t GradientPredictor(uint8_t a, uint8_t b, uint8_t c) { static WEBP_INLINE int GradientPredictor(uint8_t a, uint8_t b, uint8_t c) {
const int g = a + b - c; const int g = a + b - c;
return (g < 0) ? 0 : (g > 255) ? 255 : (uint8_t)g; return (g < 0) ? 0 : (g > 255) ? 255 : g;
} }
static void GradientFilter(const uint8_t* data, int width, int height, static WEBP_INLINE
int bpp, int stride, uint8_t* filtered_data) { void DoGradientFilter(const uint8_t* in, int width, int height,
int bpp, int stride, int inverse, uint8_t* out) {
const uint8_t* preds = (inverse ? out : in);
int h; int h;
SANITY_CHECK(data, filtered_data); SANITY_CHECK(in, out);
// Copy top scan-line as it is. // left prediction for top scan-line
memcpy((void*)filtered_data, (const void*)data, width * bpp); memcpy((void*)out, (const void*)in, bpp);
PredictLine(in + bpp, preds, out + bpp, bpp * (width - 1), inverse);
// Filter line-by-line. // Filter line-by-line.
for (h = 1; h < height; ++h) { for (h = 1; h < height; ++h) {
int w; int w;
const uint8_t* const scan_line = data + h * stride; preds += stride;
uint8_t* const out = filtered_data + h * stride; in += stride;
const uint8_t* const prev_line = scan_line - stride; out += stride;
memcpy((void*)out, (const void*)scan_line, bpp); // leftmost pixel: predict from above.
PredictLine(in, preds - stride, out, bpp, inverse);
for (w = bpp; w < width * bpp; ++w) { for (w = bpp; w < width * bpp; ++w) {
out[w] = scan_line[w] - GradientPredictor(scan_line[w - bpp], const int pred = GradientPredictor(preds[w - bpp],
prev_line[w], preds[w - stride],
prev_line[w - bpp]); preds[w - stride - bpp]);
out[w] = in[w] + (inverse ? pred : -pred);
} }
} }
} }
static void GradientFilter(const uint8_t* data, int width, int height,
int bpp, int stride, uint8_t* filtered_data) {
DoGradientFilter(data, width, height, bpp, stride, 0, filtered_data);
}
static void GradientUnfilter(const uint8_t* data, int width, int height, static void GradientUnfilter(const uint8_t* data, int width, int height,
int bpp, int stride, uint8_t* recon_data) { int bpp, int stride, uint8_t* recon_data) {
int h; DoGradientFilter(data, width, height, bpp, stride, 1, recon_data);
SANITY_CHECK(data, recon_data);
// Copy top scan-line as it is.
memcpy((void*)recon_data, (const void*)data, width * 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_line = out - stride;
memcpy((void*)out, (const void*)scan_line, bpp);
for (w = bpp; w < width * bpp; ++w) {
out[w] = scan_line[w] + GradientPredictor(out[w - bpp],
out_prev_line[w],
out_prev_line[w - bpp]);
}
}
} }
#undef SANITY_CHECK #undef SANITY_CHECK