From 5dc97cc2ca85faa5fb6c49ff408b7ad554cb14e5 Mon Sep 17 00:00:00 2001 From: Tim Kosse Date: Mon, 12 Dec 2016 10:48:23 +0100 Subject: [PATCH] Take alpha into account in wxImage::ResampleBicubic() Just like in ResampleBox() (see b99ad85b6fd1f7bc2fbdae7a470387d42bf82d0c), ResampleBicubic() needs to also weigh pixels by their respective alpha value. Closes #17748. --- src/common/image.cpp | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/src/common/image.cpp b/src/common/image.cpp index 1520264631..8b1325b509 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -1005,23 +1005,48 @@ wxImage wxImage::ResampleBicubic(int width, int height) const // Create a sum of all velues for each color channel // adjusted for the pixel's calculated weight - sum_r += src_data[src_pixel_index * 3 + 0] * pixel_weight; - sum_g += src_data[src_pixel_index * 3 + 1] * pixel_weight; - sum_b += src_data[src_pixel_index * 3 + 2] * pixel_weight; if ( src_alpha ) - sum_a += src_alpha[src_pixel_index] * pixel_weight; + { + const unsigned char a = src_alpha[src_pixel_index]; + sum_r += src_data[src_pixel_index * 3 + 0] * pixel_weight * a; + sum_g += src_data[src_pixel_index * 3 + 1] * pixel_weight * a; + sum_b += src_data[src_pixel_index * 3 + 2] * pixel_weight * a; + sum_a += a * pixel_weight; + } + else + { + sum_r += src_data[src_pixel_index * 3 + 0] * pixel_weight; + sum_g += src_data[src_pixel_index * 3 + 1] * pixel_weight; + sum_b += src_data[src_pixel_index * 3 + 2] * pixel_weight; + } } } // Put the data into the destination image. The summed values are // of double data type and are rounded here for accuracy - dst_data[0] = (unsigned char)(sum_r + 0.5); - dst_data[1] = (unsigned char)(sum_g + 0.5); - dst_data[2] = (unsigned char)(sum_b + 0.5); - dst_data += 3; - if ( src_alpha ) + { + if ( sum_a ) + { + dst_data[0] = (unsigned char)(sum_r / sum_a + 0.5); + dst_data[1] = (unsigned char)(sum_g / sum_a + 0.5); + dst_data[2] = (unsigned char)(sum_b / sum_a + 0.5); + } + else + { + dst_data[0] = 0; + dst_data[1] = 0; + dst_data[2] = 0; + } *dst_alpha++ = (unsigned char)sum_a; + } + else + { + dst_data[0] = (unsigned char)(sum_r + 0.5); + dst_data[1] = (unsigned char)(sum_g + 0.5); + dst_data[2] = (unsigned char)(sum_b + 0.5); + } + dst_data += 3; } }