Merge changes Ia01bd397,Ibf3771af into main

* changes:
  Add an internal WebPValidatePicture.
  Some renamings for consistency.
This commit is contained in:
James Zern 2022-05-16 17:49:04 +00:00 committed by Gerrit Code Review
commit ea967098a4
7 changed files with 100 additions and 91 deletions

View File

@ -496,7 +496,7 @@ static int ImportYUVAFromRGBA(const uint8_t* r_ptr,
use_iterative_conversion = 0; use_iterative_conversion = 0;
} }
if (!WebPPictureAllocYUVA(picture, width, height)) { if (!WebPPictureAllocYUVA(picture)) {
return 0; return 0;
} }
if (has_alpha) { if (has_alpha) {
@ -677,7 +677,7 @@ int WebPPictureYUVAToARGB(WebPPicture* picture) {
return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION); return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
} }
// Allocate a new argb buffer (discarding the previous one). // Allocate a new argb buffer (discarding the previous one).
if (!WebPPictureAllocARGB(picture, picture->width, picture->height)) return 0; if (!WebPPictureAllocARGB(picture)) return 0;
picture->use_argb = 1; picture->use_argb = 1;
// Convert // Convert
@ -798,24 +798,24 @@ static int Import(WebPPicture* const picture,
#if !defined(WEBP_REDUCE_CSP) #if !defined(WEBP_REDUCE_CSP)
int WebPPictureImportBGR(WebPPicture* picture, int WebPPictureImportBGR(WebPPicture* picture,
const uint8_t* rgb, int rgb_stride) { const uint8_t* bgr, int bgr_stride) {
return (picture != NULL && rgb != NULL) return (picture != NULL && bgr != NULL)
? Import(picture, rgb, rgb_stride, 3, 1, 0) ? Import(picture, bgr, bgr_stride, 3, 1, 0)
: 0; : 0;
} }
int WebPPictureImportBGRA(WebPPicture* picture, int WebPPictureImportBGRA(WebPPicture* picture,
const uint8_t* rgba, int rgba_stride) { const uint8_t* bgra, int bgra_stride) {
return (picture != NULL && rgba != NULL) return (picture != NULL && bgra != NULL)
? Import(picture, rgba, rgba_stride, 4, 1, 1) ? Import(picture, bgra, bgra_stride, 4, 1, 1)
: 0; : 0;
} }
int WebPPictureImportBGRX(WebPPicture* picture, int WebPPictureImportBGRX(WebPPicture* picture,
const uint8_t* rgba, int rgba_stride) { const uint8_t* bgrx, int bgrx_stride) {
return (picture != NULL && rgba != NULL) return (picture != NULL && bgrx != NULL)
? Import(picture, rgba, rgba_stride, 4, 1, 0) ? Import(picture, bgrx, bgrx_stride, 4, 1, 0)
: 0; : 0;
} }
@ -836,9 +836,9 @@ int WebPPictureImportRGBA(WebPPicture* picture,
} }
int WebPPictureImportRGBX(WebPPicture* picture, int WebPPictureImportRGBX(WebPPicture* picture,
const uint8_t* rgba, int rgba_stride) { const uint8_t* rgbx, int rgbx_stride) {
return (picture != NULL && rgba != NULL) return (picture != NULL && rgbx != NULL)
? Import(picture, rgba, rgba_stride, 4, 0, 0) ? Import(picture, rgbx, rgbx_stride, 4, 0, 0)
: 0; : 0;
} }

View File

@ -45,6 +45,22 @@ int WebPPictureInitInternal(WebPPicture* picture, int version) {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
int WebPValidatePicture(const WebPPicture* const picture) {
if (picture == NULL) return 0;
if (picture->width <= 0 || picture->height <= 0) {
return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
}
if (picture->width <= 0 || picture->width / 4 > INT_MAX / 4 ||
picture->height <= 0 || picture->height / 4 > INT_MAX / 4) {
return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
}
if (picture->colorspace != WEBP_YUV420 &&
picture->colorspace != WEBP_YUV420A) {
return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
}
return 1;
}
static void WebPPictureResetBufferARGB(WebPPicture* const picture) { static void WebPPictureResetBufferARGB(WebPPicture* const picture) {
picture->memory_argb_ = NULL; picture->memory_argb_ = NULL;
picture->argb = NULL; picture->argb = NULL;
@ -63,18 +79,17 @@ void WebPPictureResetBuffers(WebPPicture* const picture) {
WebPPictureResetBufferYUVA(picture); WebPPictureResetBufferYUVA(picture);
} }
int WebPPictureAllocARGB(WebPPicture* const picture, int width, int height) { int WebPPictureAllocARGB(WebPPicture* const picture) {
void* memory; void* memory;
const int width = picture->width;
const int height = picture->height;
const uint64_t argb_size = (uint64_t)width * height; const uint64_t argb_size = (uint64_t)width * height;
assert(picture != NULL); if (!WebPValidatePicture(picture)) return 0;
WebPSafeFree(picture->memory_argb_); WebPSafeFree(picture->memory_argb_);
WebPPictureResetBufferARGB(picture); WebPPictureResetBufferARGB(picture);
if (width <= 0 || height <= 0) {
return WebPEncodingSetError(picture, VP8_ENC_ERROR_BAD_DIMENSION);
}
// allocate a new buffer. // allocate a new buffer.
memory = WebPSafeMalloc(argb_size + WEBP_ALIGN_CST, sizeof(*picture->argb)); memory = WebPSafeMalloc(argb_size + WEBP_ALIGN_CST, sizeof(*picture->argb));
if (memory == NULL) { if (memory == NULL) {
@ -86,10 +101,10 @@ int WebPPictureAllocARGB(WebPPicture* const picture, int width, int height) {
return 1; return 1;
} }
int WebPPictureAllocYUVA(WebPPicture* const picture, int width, int height) { int WebPPictureAllocYUVA(WebPPicture* const picture) {
const WebPEncCSP uv_csp =
(WebPEncCSP)((int)picture->colorspace & WEBP_CSP_UV_MASK);
const int has_alpha = (int)picture->colorspace & WEBP_CSP_ALPHA_BIT; const int has_alpha = (int)picture->colorspace & WEBP_CSP_ALPHA_BIT;
const int width = picture->width;
const int height = picture->height;
const int y_stride = width; const int y_stride = width;
const int uv_width = (int)(((int64_t)width + 1) >> 1); const int uv_width = (int)(((int64_t)width + 1) >> 1);
const int uv_height = (int)(((int64_t)height + 1) >> 1); const int uv_height = (int)(((int64_t)height + 1) >> 1);
@ -98,15 +113,11 @@ int WebPPictureAllocYUVA(WebPPicture* const picture, int width, int height) {
uint64_t y_size, uv_size, a_size, total_size; uint64_t y_size, uv_size, a_size, total_size;
uint8_t* mem; uint8_t* mem;
assert(picture != NULL); if (!WebPValidatePicture(picture)) return 0;
WebPSafeFree(picture->memory_); WebPSafeFree(picture->memory_);
WebPPictureResetBufferYUVA(picture); WebPPictureResetBufferYUVA(picture);
if (uv_csp != WEBP_YUV420) {
return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
}
// alpha // alpha
a_width = has_alpha ? width : 0; a_width = has_alpha ? width : 0;
a_stride = a_width; a_stride = a_width;
@ -152,15 +163,12 @@ int WebPPictureAllocYUVA(WebPPicture* const picture, int width, int height) {
int WebPPictureAlloc(WebPPicture* picture) { int WebPPictureAlloc(WebPPicture* picture) {
if (picture != NULL) { if (picture != NULL) {
const int width = picture->width;
const int height = picture->height;
WebPPictureFree(picture); // erase previous buffer WebPPictureFree(picture); // erase previous buffer
if (!picture->use_argb) { if (!picture->use_argb) {
return WebPPictureAllocYUVA(picture, width, height); return WebPPictureAllocYUVA(picture);
} else { } else {
return WebPPictureAllocARGB(picture, width, height); return WebPPictureAllocARGB(picture);
} }
} }
return 1; return 1;

View File

@ -198,34 +198,34 @@ static void AlphaMultiplyY(WebPPicture* const pic, int inverse) {
} }
} }
int WebPPictureRescale(WebPPicture* pic, int width, int height) { int WebPPictureRescale(WebPPicture* picture, int width, int height) {
WebPPicture tmp; WebPPicture tmp;
int prev_width, prev_height; int prev_width, prev_height;
rescaler_t* work; rescaler_t* work;
if (pic == NULL) return 0; if (picture == NULL) return 0;
prev_width = pic->width; prev_width = picture->width;
prev_height = pic->height; prev_height = picture->height;
if (!WebPRescalerGetScaledDimensions( if (!WebPRescalerGetScaledDimensions(
prev_width, prev_height, &width, &height)) { prev_width, prev_height, &width, &height)) {
return 0; return 0;
} }
PictureGrabSpecs(pic, &tmp); PictureGrabSpecs(picture, &tmp);
tmp.width = width; tmp.width = width;
tmp.height = height; tmp.height = height;
if (!WebPPictureAlloc(&tmp)) return 0; if (!WebPPictureAlloc(&tmp)) return 0;
if (!pic->use_argb) { if (!picture->use_argb) {
work = (rescaler_t*)WebPSafeMalloc(2ULL * width, sizeof(*work)); work = (rescaler_t*)WebPSafeMalloc(2ULL * width, sizeof(*work));
if (work == NULL) { if (work == NULL) {
WebPPictureFree(&tmp); WebPPictureFree(&tmp);
return 0; return 0;
} }
// If present, we need to rescale alpha first (for AlphaMultiplyY). // If present, we need to rescale alpha first (for AlphaMultiplyY).
if (pic->a != NULL) { if (picture->a != NULL) {
WebPInitAlphaProcessing(); WebPInitAlphaProcessing();
if (!RescalePlane(pic->a, prev_width, prev_height, pic->a_stride, if (!RescalePlane(picture->a, prev_width, prev_height, picture->a_stride,
tmp.a, width, height, tmp.a_stride, work, 1)) { tmp.a, width, height, tmp.a_stride, work, 1)) {
return 0; return 0;
} }
@ -233,17 +233,15 @@ int WebPPictureRescale(WebPPicture* pic, int width, int height) {
// We take transparency into account on the luma plane only. That's not // We take transparency into account on the luma plane only. That's not
// totally exact blending, but still is a good approximation. // totally exact blending, but still is a good approximation.
AlphaMultiplyY(pic, 0); AlphaMultiplyY(picture, 0);
if (!RescalePlane(pic->y, prev_width, prev_height, pic->y_stride, if (!RescalePlane(picture->y, prev_width, prev_height, picture->y_stride,
tmp.y, width, height, tmp.y_stride, work, 1) || tmp.y, width, height, tmp.y_stride, work, 1) ||
!RescalePlane(pic->u, !RescalePlane(picture->u, HALVE(prev_width), HALVE(prev_height),
HALVE(prev_width), HALVE(prev_height), pic->uv_stride, picture->uv_stride, tmp.u, HALVE(width), HALVE(height),
tmp.u, tmp.uv_stride, work, 1) ||
HALVE(width), HALVE(height), tmp.uv_stride, work, 1) || !RescalePlane(picture->v, HALVE(prev_width), HALVE(prev_height),
!RescalePlane(pic->v, picture->uv_stride, tmp.v, HALVE(width), HALVE(height),
HALVE(prev_width), HALVE(prev_height), pic->uv_stride, tmp.uv_stride, work, 1)) {
tmp.v,
HALVE(width), HALVE(height), tmp.uv_stride, work, 1)) {
return 0; return 0;
} }
AlphaMultiplyY(&tmp, 1); AlphaMultiplyY(&tmp, 1);
@ -257,18 +255,17 @@ int WebPPictureRescale(WebPPicture* pic, int width, int height) {
// weighting first (black-matting), scale the RGB values, and remove // weighting first (black-matting), scale the RGB values, and remove
// the premultiplication afterward (while preserving the alpha channel). // the premultiplication afterward (while preserving the alpha channel).
WebPInitAlphaProcessing(); WebPInitAlphaProcessing();
AlphaMultiplyARGB(pic, 0); AlphaMultiplyARGB(picture, 0);
if (!RescalePlane((const uint8_t*)pic->argb, prev_width, prev_height, if (!RescalePlane((const uint8_t*)picture->argb, prev_width, prev_height,
pic->argb_stride * 4, picture->argb_stride * 4, (uint8_t*)tmp.argb, width,
(uint8_t*)tmp.argb, width, height, height, tmp.argb_stride * 4, work, 4)) {
tmp.argb_stride * 4, work, 4)) {
return 0; return 0;
} }
AlphaMultiplyARGB(&tmp, 1); AlphaMultiplyARGB(&tmp, 1);
} }
WebPPictureFree(pic); WebPPictureFree(picture);
WebPSafeFree(work); WebPSafeFree(work);
*pic = tmp; *picture = tmp;
return 1; return 1;
} }

View File

@ -190,27 +190,28 @@ static WEBP_INLINE uint32_t MakeARGB32(int r, int g, int b) {
return (0xff000000u | (r << 16) | (g << 8) | b); return (0xff000000u | (r << 16) | (g << 8) | b);
} }
void WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb) { void WebPBlendAlpha(WebPPicture* picture, uint32_t background_rgb) {
const int red = (background_rgb >> 16) & 0xff; const int red = (background_rgb >> 16) & 0xff;
const int green = (background_rgb >> 8) & 0xff; const int green = (background_rgb >> 8) & 0xff;
const int blue = (background_rgb >> 0) & 0xff; const int blue = (background_rgb >> 0) & 0xff;
int x, y; int x, y;
if (pic == NULL) return; if (picture == NULL) return;
if (!pic->use_argb) { if (!picture->use_argb) {
const int uv_width = (pic->width >> 1); // omit last pixel during u/v loop // omit last pixel during u/v loop
const int uv_width = (picture->width >> 1);
const int Y0 = VP8RGBToY(red, green, blue, YUV_HALF); const int Y0 = VP8RGBToY(red, green, blue, YUV_HALF);
// VP8RGBToU/V expects the u/v values summed over four pixels // VP8RGBToU/V expects the u/v values summed over four pixels
const int U0 = VP8RGBToU(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF); const int U0 = VP8RGBToU(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);
const int V0 = VP8RGBToV(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF); const int V0 = VP8RGBToV(4 * red, 4 * green, 4 * blue, 4 * YUV_HALF);
const int has_alpha = pic->colorspace & WEBP_CSP_ALPHA_BIT; const int has_alpha = picture->colorspace & WEBP_CSP_ALPHA_BIT;
uint8_t* y_ptr = pic->y; uint8_t* y_ptr = picture->y;
uint8_t* u_ptr = pic->u; uint8_t* u_ptr = picture->u;
uint8_t* v_ptr = pic->v; uint8_t* v_ptr = picture->v;
uint8_t* a_ptr = pic->a; uint8_t* a_ptr = picture->a;
if (!has_alpha || a_ptr == NULL) return; // nothing to do if (!has_alpha || a_ptr == NULL) return; // nothing to do
for (y = 0; y < pic->height; ++y) { for (y = 0; y < picture->height; ++y) {
// Luma blending // Luma blending
for (x = 0; x < pic->width; ++x) { for (x = 0; x < picture->width; ++x) {
const uint8_t alpha = a_ptr[x]; const uint8_t alpha = a_ptr[x];
if (alpha < 0xff) { if (alpha < 0xff) {
y_ptr[x] = BLEND(Y0, y_ptr[x], alpha); y_ptr[x] = BLEND(Y0, y_ptr[x], alpha);
@ -219,7 +220,7 @@ void WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb) {
// Chroma blending every even line // Chroma blending every even line
if ((y & 1) == 0) { if ((y & 1) == 0) {
uint8_t* const a_ptr2 = uint8_t* const a_ptr2 =
(y + 1 == pic->height) ? a_ptr : a_ptr + pic->a_stride; (y + 1 == picture->height) ? a_ptr : a_ptr + picture->a_stride;
for (x = 0; x < uv_width; ++x) { for (x = 0; x < uv_width; ++x) {
// Average four alpha values into a single blending weight. // Average four alpha values into a single blending weight.
// TODO(skal): might lead to visible contouring. Can we do better? // TODO(skal): might lead to visible contouring. Can we do better?
@ -229,24 +230,24 @@ void WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb) {
u_ptr[x] = BLEND_10BIT(U0, u_ptr[x], alpha); u_ptr[x] = BLEND_10BIT(U0, u_ptr[x], alpha);
v_ptr[x] = BLEND_10BIT(V0, v_ptr[x], alpha); v_ptr[x] = BLEND_10BIT(V0, v_ptr[x], alpha);
} }
if (pic->width & 1) { // rightmost pixel if (picture->width & 1) { // rightmost pixel
const uint32_t alpha = 2 * (a_ptr[2 * x + 0] + a_ptr2[2 * x + 0]); const uint32_t alpha = 2 * (a_ptr[2 * x + 0] + a_ptr2[2 * x + 0]);
u_ptr[x] = BLEND_10BIT(U0, u_ptr[x], alpha); u_ptr[x] = BLEND_10BIT(U0, u_ptr[x], alpha);
v_ptr[x] = BLEND_10BIT(V0, v_ptr[x], alpha); v_ptr[x] = BLEND_10BIT(V0, v_ptr[x], alpha);
} }
} else { } else {
u_ptr += pic->uv_stride; u_ptr += picture->uv_stride;
v_ptr += pic->uv_stride; v_ptr += picture->uv_stride;
} }
memset(a_ptr, 0xff, pic->width); // reset alpha value to opaque memset(a_ptr, 0xff, picture->width); // reset alpha value to opaque
a_ptr += pic->a_stride; a_ptr += picture->a_stride;
y_ptr += pic->y_stride; y_ptr += picture->y_stride;
} }
} else { } else {
uint32_t* argb = pic->argb; uint32_t* argb = picture->argb;
const uint32_t background = MakeARGB32(red, green, blue); const uint32_t background = MakeARGB32(red, green, blue);
for (y = 0; y < pic->height; ++y) { for (y = 0; y < picture->height; ++y) {
for (x = 0; x < pic->width; ++x) { for (x = 0; x < picture->width; ++x) {
const int alpha = (argb[x] >> 24) & 0xff; const int alpha = (argb[x] >> 24) & 0xff;
if (alpha != 0xff) { if (alpha != 0xff) {
if (alpha > 0) { if (alpha > 0) {
@ -262,7 +263,7 @@ void WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb) {
} }
} }
} }
argb += pic->argb_stride; argb += picture->argb_stride;
} }
} }
} }

View File

@ -491,19 +491,24 @@ int VP8FilterStrengthFromDelta(int sharpness, int delta);
// misc utils for picture_*.c: // misc utils for picture_*.c:
// Returns true if 'picture' is non-NULL and dimensions/colorspace are within
// their valid ranges. If returning false, the 'error_code' in 'picture' is
// updated.
int WebPValidatePicture(const WebPPicture* picture);
// Remove reference to the ARGB/YUVA buffer (doesn't free anything). // Remove reference to the ARGB/YUVA buffer (doesn't free anything).
void WebPPictureResetBuffers(WebPPicture* const picture); void WebPPictureResetBuffers(WebPPicture* const picture);
// Allocates ARGB buffer of given dimension (previous one is always free'd). // Allocates ARGB buffer according to set width/height (previous one is
// Preserves the YUV(A) buffer. Returns false in case of error (invalid param, // always free'd). Preserves the YUV(A) buffer. Returns false in case of error
// out-of-memory). // (invalid param, out-of-memory).
int WebPPictureAllocARGB(WebPPicture* const picture, int width, int height); int WebPPictureAllocARGB(WebPPicture* const picture);
// Allocates YUVA buffer of given dimension (previous one is always free'd). // Allocates YUVA buffer according to set width/height (previous one is always
// Uses picture->csp to determine whether an alpha buffer is needed. // free'd). Uses picture->csp to determine whether an alpha buffer is needed.
// Preserves the ARGB buffer. // Preserves the ARGB buffer.
// Returns false in case of error (invalid param, out-of-memory). // Returns false in case of error (invalid param, out-of-memory).
int WebPPictureAllocYUVA(WebPPicture* const picture, int width, int height); int WebPPictureAllocYUVA(WebPPicture* const picture);
// Replace samples that are fully transparent by 'color' to help compressibility // Replace samples that are fully transparent by 'color' to help compressibility
// (no guarantee, though). Assumes pic->use_argb is true. // (no guarantee, though). Assumes pic->use_argb is true.

View File

@ -336,9 +336,7 @@ int WebPEncode(const WebPConfig* config, WebPPicture* pic) {
if (!WebPValidateConfig(config)) { if (!WebPValidateConfig(config)) {
return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION); return WebPEncodingSetError(pic, VP8_ENC_ERROR_INVALID_CONFIGURATION);
} }
if (pic->width <= 0 || pic->height <= 0) { if (!WebPValidatePicture(pic)) return 0;
return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_DIMENSION);
}
if (pic->width > WEBP_MAX_DIMENSION || pic->height > WEBP_MAX_DIMENSION) { if (pic->width > WEBP_MAX_DIMENSION || pic->height > WEBP_MAX_DIMENSION) {
return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_DIMENSION); return WebPEncodingSetError(pic, VP8_ENC_ERROR_BAD_DIMENSION);
} }

View File

@ -455,7 +455,7 @@ WEBP_EXTERN int WebPPictureIsView(const WebPPicture* picture);
// dimension will be calculated preserving the aspect ratio. // dimension will be calculated preserving the aspect ratio.
// No gamma correction is applied. // No gamma correction is applied.
// Returns false in case of error (invalid parameter or insufficient memory). // Returns false in case of error (invalid parameter or insufficient memory).
WEBP_EXTERN int WebPPictureRescale(WebPPicture* pic, int width, int height); WEBP_EXTERN int WebPPictureRescale(WebPPicture* picture, int width, int height);
// Colorspace conversion function to import RGB samples. // Colorspace conversion function to import RGB samples.
// Previous buffer will be free'd, if any. // Previous buffer will be free'd, if any.
@ -526,7 +526,7 @@ WEBP_EXTERN int WebPPictureHasTransparency(const WebPPicture* picture);
// Remove the transparency information (if present) by blending the color with // Remove the transparency information (if present) by blending the color with
// the background color 'background_rgb' (specified as 24bit RGB triplet). // the background color 'background_rgb' (specified as 24bit RGB triplet).
// After this call, all alpha values are reset to 0xff. // After this call, all alpha values are reset to 0xff.
WEBP_EXTERN void WebPBlendAlpha(WebPPicture* pic, uint32_t background_rgb); WEBP_EXTERN void WebPBlendAlpha(WebPPicture* picture, uint32_t background_rgb);
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Main call // Main call