Applied patch [ 1339764 ] Add wxImage::ConvertToGreyscale

Jamie Gadd


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@36995 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart 2006-01-19 09:55:15 +00:00
parent f96a9d34e6
commit ec85956ae9
3 changed files with 56 additions and 2 deletions

View File

@ -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}}

View File

@ -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;

View File

@ -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;