Merge "Fix VP8ApplyNearLossless to respect const and stride."

This commit is contained in:
Pascal Massimino 2017-04-27 15:56:47 +00:00 committed by Gerrit Code Review
commit 1b92b237ac
3 changed files with 41 additions and 19 deletions

View File

@ -69,22 +69,30 @@ static int IsSmooth(const uint32_t* const prev_row,
} }
// Adjusts pixel values of image with given maximum error. // Adjusts pixel values of image with given maximum error.
static void NearLossless(int xsize, int ysize, uint32_t* argb, static void NearLossless(int xsize, int ysize, const uint32_t* argb_src,
int limit_bits, uint32_t* copy_buffer) { int stride, int limit_bits, uint32_t* copy_buffer,
uint32_t* argb_dst) {
int x, y; int x, y;
const int limit = 1 << limit_bits; const int limit = 1 << limit_bits;
uint32_t* prev_row = copy_buffer; uint32_t* prev_row = copy_buffer;
uint32_t* curr_row = prev_row + xsize; uint32_t* curr_row = prev_row + xsize;
uint32_t* next_row = curr_row + xsize; uint32_t* next_row = curr_row + xsize;
memcpy(copy_buffer, argb, xsize * 2 * sizeof(argb[0])); memcpy(curr_row, argb_src, xsize * sizeof(argb_src[0]));
memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
for (y = 1; y < ysize - 1; ++y) { for (y = 0; y < ysize; ++y, argb_src += stride, argb_dst += xsize) {
uint32_t* const curr_argb_row = argb + y * xsize; if (y == 0 || y == ysize - 1) {
uint32_t* const next_argb_row = curr_argb_row + xsize; memcpy(argb_dst, argb_src, xsize * sizeof(argb_src[0]));
memcpy(next_row, next_argb_row, xsize * sizeof(argb[0])); } else {
memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
argb_dst[0] = argb_src[0];
argb_dst[xsize - 1] = argb_src[xsize - 1];
for (x = 1; x < xsize - 1; ++x) { for (x = 1; x < xsize - 1; ++x) {
if (!IsSmooth(prev_row, curr_row, next_row, x, limit)) { if (IsSmooth(prev_row, curr_row, next_row, x, limit)) {
curr_argb_row[x] = ClosestDiscretizedArgb(curr_row[x], limit_bits); argb_dst[x] = curr_row[x];
} else {
argb_dst[x] = ClosestDiscretizedArgb(curr_row[x], limit_bits);
}
} }
} }
{ {
@ -97,25 +105,37 @@ static void NearLossless(int xsize, int ysize, uint32_t* argb,
} }
} }
int VP8ApplyNearLossless(int xsize, int ysize, uint32_t* argb, int quality) { int VP8ApplyNearLossless(const WebPPicture* const picture, int quality,
uint32_t* const argb_dst) {
int i; int i;
const int xsize = picture->width;
const int ysize = picture->height;
const int stride = picture->argb_stride;
uint32_t* const copy_buffer = uint32_t* const copy_buffer =
(uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer)); (uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer));
const int limit_bits = VP8LNearLosslessBits(quality); const int limit_bits = VP8LNearLosslessBits(quality);
assert(argb != NULL); assert(argb_dst != NULL);
assert(limit_bits >= 0); assert(limit_bits > 0);
assert(limit_bits <= MAX_LIMIT_BITS); assert(limit_bits <= MAX_LIMIT_BITS);
if (copy_buffer == NULL) { if (copy_buffer == NULL) {
return 0; return 0;
} }
// For small icon images, don't attempt to apply near-lossless compression. // For small icon images, don't attempt to apply near-lossless compression.
if (xsize < MIN_DIM_FOR_NEAR_LOSSLESS && ysize < MIN_DIM_FOR_NEAR_LOSSLESS) { if ((xsize < MIN_DIM_FOR_NEAR_LOSSLESS &&
ysize < MIN_DIM_FOR_NEAR_LOSSLESS) ||
ysize < 3) {
for (i = 0; i < ysize; ++i) {
memcpy(argb_dst + i * xsize, picture->argb + i * picture->argb_stride,
xsize * sizeof(*argb_dst));
}
WebPSafeFree(copy_buffer); WebPSafeFree(copy_buffer);
return 1; return 1;
} }
for (i = limit_bits; i != 0; --i) { NearLossless(xsize, ysize, picture->argb, stride, limit_bits, copy_buffer,
NearLossless(xsize, ysize, argb, i, copy_buffer); argb_dst);
for (i = limit_bits - 1; i != 0; --i) {
NearLossless(xsize, ysize, argb_dst, xsize, i, copy_buffer, argb_dst);
} }
WebPSafeFree(copy_buffer); WebPSafeFree(copy_buffer);
return 1; return 1;

View File

@ -504,7 +504,8 @@ void WebPCleanupTransparentAreaLossless(WebPPicture* const pic);
// in near_lossless.c // in near_lossless.c
// Near lossless preprocessing in RGB color-space. // Near lossless preprocessing in RGB color-space.
int VP8ApplyNearLossless(int xsize, int ysize, uint32_t* argb, int quality); int VP8ApplyNearLossless(const WebPPicture* const picture, int quality,
uint32_t* const argb_dst);
// Near lossless adjustment for predictors. // Near lossless adjustment for predictors.
void VP8ApplyNearLosslessPredict(int xsize, int ysize, int pred_bits, void VP8ApplyNearLosslessPredict(int xsize, int ysize, int pred_bits,
const uint32_t* argb_orig, const uint32_t* argb_orig,

View File

@ -1512,8 +1512,9 @@ WebPEncodingError VP8LEncodeStream(const WebPConfig* const config,
use_near_lossless = use_near_lossless =
(config->near_lossless < 100) && !enc->use_palette_ && !enc->use_predict_; (config->near_lossless < 100) && !enc->use_palette_ && !enc->use_predict_;
if (use_near_lossless) { if (use_near_lossless) {
if (!VP8ApplyNearLossless(width, height, picture->argb, err = AllocateTransformBuffer(enc, width, height);
config->near_lossless)) { if (err != VP8_ENC_OK) goto Error;
if (!VP8ApplyNearLossless(picture, config->near_lossless, enc->argb_)) {
err = VP8_ENC_ERROR_OUT_OF_MEMORY; err = VP8_ENC_ERROR_OUT_OF_MEMORY;
goto Error; goto Error;
} }