diff --git a/docs/changes.txt b/docs/changes.txt index cefce2aeb3..0b2a1f79e9 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -114,6 +114,7 @@ All (GUI): - Fix calculating point position in wxDataViewCtrl::HitTest(). - Fix position of the rectangle returned by wxDataViewCtrl::GetItemRect(). - Add wxDataViewRenderer::GetAccessibleDescription(). +- Improve wxImage::Scale() handling of pixels with alpha channel (Tim Kosse). wxGTK: diff --git a/src/common/image.cpp b/src/common/image.cpp index b0ca1d8fd0..1520264631 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -659,23 +659,48 @@ wxImage wxImage::ResampleBox(int width, int height) const // Calculate the actual index in our source pixels src_pixel_index = j * M_IMGDATA->m_width + i; - sum_r += src_data[src_pixel_index * 3 + 0]; - sum_g += src_data[src_pixel_index * 3 + 1]; - sum_b += src_data[src_pixel_index * 3 + 2]; - if ( src_alpha ) + if (src_alpha) + { + sum_r += src_data[src_pixel_index * 3 + 0] * src_alpha[src_pixel_index]; + sum_g += src_data[src_pixel_index * 3 + 1] * src_alpha[src_pixel_index]; + sum_b += src_data[src_pixel_index * 3 + 2] * src_alpha[src_pixel_index]; sum_a += src_alpha[src_pixel_index]; + } + else + { + sum_r += src_data[src_pixel_index * 3 + 0]; + sum_g += src_data[src_pixel_index * 3 + 1]; + sum_b += src_data[src_pixel_index * 3 + 2]; + } averaged_pixels++; } } // Calculate the average from the sum and number of averaged pixels - dst_data[0] = (unsigned char)(sum_r / averaged_pixels); - dst_data[1] = (unsigned char)(sum_g / averaged_pixels); - dst_data[2] = (unsigned char)(sum_b / averaged_pixels); - dst_data += 3; - if ( src_alpha ) + if (src_alpha) + { + if (sum_a) + { + dst_data[0] = (unsigned char)(sum_r / sum_a); + dst_data[1] = (unsigned char)(sum_g / sum_a); + dst_data[2] = (unsigned char)(sum_b / sum_a); + } + else + { + dst_data[0] = 0; + dst_data[1] = 0; + dst_data[2] = 0; + } *dst_alpha++ = (unsigned char)(sum_a / averaged_pixels); + } + else + { + dst_data[0] = (unsigned char)(sum_r / averaged_pixels); + dst_data[1] = (unsigned char)(sum_g / averaged_pixels); + dst_data[2] = (unsigned char)(sum_b / averaged_pixels); + } + dst_data += 3; } }