From: Paul Cornett Date: Tue, 6 Aug 2013 06:05:38 +0000 (+0000) Subject: Rewrite ConvertToGreyscale() and ConvertToDisabled() X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/632b58cdce7a1b62f7aa1a26acc61e5ced04494b?ds=sidebyside Rewrite ConvertToGreyscale() and ConvertToDisabled() They do almost the same thing, so use almost the same code. Fixes ConvertToDisabled() modifying its own data. Closes #15389 git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@74625 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/common/image.cpp b/src/common/image.cpp index 3b026dae77..40b0ab0325 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -1625,35 +1625,39 @@ wxImage wxImage::ConvertToGreyscale(void) const wxImage wxImage::ConvertToGreyscale(double weight_r, double weight_g, double weight_b) const { - wxImage image(MakeEmptyClone()); - - wxCHECK( image.IsOk(), image ); - - const unsigned char *src = M_IMGDATA->m_data; - unsigned char *dest = image.GetData(); - - const bool hasMask = M_IMGDATA->m_hasMask; - const unsigned char maskRed = M_IMGDATA->m_maskRed; - const unsigned char maskGreen = M_IMGDATA->m_maskGreen; - const unsigned char maskBlue = M_IMGDATA->m_maskBlue; + wxImage image; + wxCHECK_MSG(IsOk(), image, "invalid image"); - const long size = M_IMGDATA->m_width * M_IMGDATA->m_height; - for ( long i = 0; i < size; i++, src += 3, dest += 3 ) + const int w = M_IMGDATA->m_width; + const int h = M_IMGDATA->m_height; + size_t size = size_t(w) * h; + image.Create(w, h, false); + const unsigned char* alpha = M_IMGDATA->m_alpha; + if (alpha) { - memcpy(dest, src, 3); - // only modify non-masked pixels - if ( !hasMask || src[0] != maskRed || src[1] != maskGreen || src[2] != maskBlue ) - { - wxColour::MakeGrey(dest + 0, dest + 1, dest + 2, weight_r, weight_g, weight_b); - } + image.SetAlpha(); + memcpy(image.GetAlpha(), alpha, size); } - - // copy the alpha channel, if any - if ( image.HasAlpha() ) - { - memcpy( image.GetAlpha(), GetAlpha(), GetWidth() * GetHeight() ); + const unsigned char mask_r = M_IMGDATA->m_maskRed; + const unsigned char mask_g = M_IMGDATA->m_maskGreen; + const unsigned char mask_b = M_IMGDATA->m_maskBlue; + const bool hasMask = M_IMGDATA->m_hasMask; + if (hasMask) + image.SetMaskColour(mask_r, mask_g, mask_b); + + const unsigned char* src = M_IMGDATA->m_data; + unsigned char* dst = image.GetData(); + while (size--) + { + unsigned char r = *src++; + unsigned char g = *src++; + unsigned char b = *src++; + if (!hasMask || r != mask_r || g != mask_g || b != mask_b) + wxColour::MakeGrey(&r, &g, &b, weight_r, weight_g, weight_b); + *dst++ = r; + *dst++ = g; + *dst++ = b; } - return image; } @@ -1694,30 +1698,38 @@ wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char wxImage wxImage::ConvertToDisabled(unsigned char brightness) const { - wxImage image = *this; - - unsigned char mr = image.GetMaskRed(); - unsigned char mg = image.GetMaskGreen(); - unsigned char mb = image.GetMaskBlue(); - - int width = image.GetWidth(); - int height = image.GetHeight(); - bool has_mask = image.HasMask(); + wxImage image; + wxCHECK_MSG(IsOk(), image, "invalid image"); - for (int y = height-1; y >= 0; --y) + const int w = M_IMGDATA->m_width; + const int h = M_IMGDATA->m_height; + size_t size = size_t(w) * h; + image.Create(w, h, false); + const unsigned char* alpha = M_IMGDATA->m_alpha; + if (alpha) { - for (int x = width-1; x >= 0; --x) - { - unsigned char* data = image.GetData() + (y*(width*3))+(x*3); - unsigned char* r = data; - unsigned char* g = data+1; - unsigned char* b = data+2; - - if (has_mask && (*r == mr) && (*g == mg) && (*b == mb)) - continue; - - wxColour::MakeDisabled(r, g, b, brightness); - } + image.SetAlpha(); + memcpy(image.GetAlpha(), alpha, size); + } + const unsigned char mask_r = M_IMGDATA->m_maskRed; + const unsigned char mask_g = M_IMGDATA->m_maskGreen; + const unsigned char mask_b = M_IMGDATA->m_maskBlue; + const bool hasMask = M_IMGDATA->m_hasMask; + if (hasMask) + image.SetMaskColour(mask_r, mask_g, mask_b); + + const unsigned char* src = M_IMGDATA->m_data; + unsigned char* dst = image.GetData(); + while (size--) + { + unsigned char r = *src++; + unsigned char g = *src++; + unsigned char b = *src++; + if (!hasMask || r != mask_r || g != mask_g || b != mask_b) + wxColour::MakeDisabled(&r, &g, &b, brightness); + *dst++ = r; + *dst++ = g; + *dst++ = b; } return image; }