diff --git a/docs/latex/wx/image.tex b/docs/latex/wx/image.tex index bd9b723e0e..88068a73d4 100644 --- a/docs/latex/wx/image.tex +++ b/docs/latex/wx/image.tex @@ -304,6 +304,16 @@ Deprecated, use equivalent \helpref{wxBitmap constructor}{wxbitmapctor} (which takes wxImage and depth as its arguments) instead. +\membersection{wxImage::ConvertToGreyscale}\label{wximageconverttogreyscale} + +\constfunc{wxImage}{ConvertToGreyscale}{\param{double}{ lr = 0.299}, \param{double}{ lg = 0.587}, \param{double}{ lb = 0.114}} + +Returns a greyscale version of the image. The returned image uses the luminance +component of the original to calculate the greyscale. Defaults to using +ITU-T BT.601 when converting to YUV, where every pixel equals +(R * {\it lr}) + (G * {\it lg}) + (B * {\it lb}). + + \membersection{wxImage::ConvertToMono}\label{wxbitmapconverttomono} \constfunc{wxImage}{ConvertToMono}{\param{unsigned char}{ r}, \param{unsigned char}{ g}, \param{unsigned char}{ b}} diff --git a/include/wx/image.h b/include/wx/image.h index e685022009..b7b9d9a8d5 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -160,7 +160,7 @@ public: public: RGBValue(unsigned char r=0, unsigned char g=0, unsigned char b=0) : red(r), green(g), blue(b) {} - unsigned char red; + unsigned char red; unsigned char green; unsigned char blue; }; @@ -171,7 +171,7 @@ public: public: HSVValue(double h=0.0, double s=0.0, double v=0.0) : hue(h), saturation(s), value(v) {} - double hue; + double hue; double saturation; double value; }; @@ -241,6 +241,10 @@ public: void Replace( unsigned char r1, unsigned char g1, unsigned char b1, unsigned char r2, unsigned char g2, unsigned char b2 ); + // Convert to greyscale image. Uses the luminance component (Y) of the image. + // The luma value (YUV) is calculated using (R * lr) + (G * lg) + (B * lb), defaults to ITU-T BT.601 + wxImage ConvertToGreyscale( double lr = 0.299, double lg = 0.587, double lb = 0.114 ) const; + // convert to monochrome image (<r,g,b> will be replaced by white, // everything else by black) wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const; diff --git a/src/common/image.cpp b/src/common/image.cpp index 8d1794a51d..d261424034 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -769,6 +769,46 @@ void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1, } } +wxImage wxImage::ConvertToGreyscale( double lr, double lg, double lb ) const +{ + wxImage image; + + wxCHECK_MSG( Ok(), image, wxT("invalid image") ); + + image.Create(M_IMGDATA->m_width, M_IMGDATA->m_height, false); + + unsigned char *dest = image.GetData(); + + wxCHECK_MSG( dest, image, wxT("unable to create image") ); + + unsigned char *src = M_IMGDATA->m_data; + bool hasMask = M_IMGDATA->m_hasMask; + unsigned char maskRed = M_IMGDATA->m_maskRed; + unsigned char maskGreen = M_IMGDATA->m_maskGreen; + unsigned char maskBlue = M_IMGDATA->m_maskBlue; + + if ( hasMask ) + image.SetMaskColour(maskRed, maskGreen, maskBlue); + + const long size = M_IMGDATA->m_width * M_IMGDATA->m_height; + for ( long i = 0; i < size; i++, src += 3, dest += 3 ) + { + // don't modify the mask + if ( hasMask && src[0] == maskRed && src[1] == maskGreen && src[2] == maskBlue ) + { + memcpy(dest, src, 3); + } + else + { + // calculate the luma + double luma = (src[0] * lr + src[1] * lg + src[2] * lb) + 0.5; + dest[0] = dest[1] = dest[2] = wx_static_cast(unsigned char, luma); + } + } + + return image; +} + wxImage wxImage::ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const { wxImage image;