From 41c2a8d2f5fe198d6ca04bea34eb5298ac48c26b Mon Sep 17 00:00:00 2001 From: Andrzej Hunt Date: Fri, 7 Nov 2025 16:10:28 +0000 Subject: [PATCH] VP8ApplyNearLossless: skip alloc for small images Change-Id: I193513c1fbbc5fab571014d3cfd3319d671d8949 --- src/enc/near_lossless_enc.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/enc/near_lossless_enc.c b/src/enc/near_lossless_enc.c index 6a336c77..d7b14751 100644 --- a/src/enc/near_lossless_enc.c +++ b/src/enc/near_lossless_enc.c @@ -111,18 +111,15 @@ static void NearLossless(int xsize, int ysize, const uint32_t* argb_src, int VP8ApplyNearLossless(const WebPPicture* const picture, int quality, uint32_t* const argb_dst) { int i; + uint32_t* copy_buffer; const int xsize = picture->width; const int ysize = picture->height; const int stride = picture->argb_stride; - uint32_t* const copy_buffer = - (uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer)); const int limit_bits = VP8LNearLosslessBits(quality); assert(argb_dst != NULL); assert(limit_bits > 0); assert(limit_bits <= MAX_LIMIT_BITS); - if (copy_buffer == NULL) { - return 0; - } + // 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) || @@ -131,10 +128,14 @@ int VP8ApplyNearLossless(const WebPPicture* const picture, int quality, memcpy(argb_dst + i * xsize, picture->argb + i * picture->argb_stride, xsize * sizeof(*argb_dst)); } - WebPSafeFree(copy_buffer); return 1; } + copy_buffer = (uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer)); + if (copy_buffer == NULL) { + return 0; + } + NearLossless(xsize, ysize, picture->argb, stride, limit_bits, copy_buffer, argb_dst); for (i = limit_bits - 1; i != 0; --i) {