(rect.y+rect.height <= GetHeight()),
wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
- wxBitmap ret( rect.width, rect.height );
+ wxBitmap ret( rect.width, rect.height, GetDepth() );
wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
#ifndef __WXMICROWIN__
- // TODO: copy alpha channel data if any
+ // handle alpha channel, if any
+ if (HasAlpha())
+ ret.UseAlpha();
// copy bitmap data
MemoryHDC dcSrc,
data.m_height = h;
// remember that DIBs are stored in top to bottom order!
- const LONG bytesPerRow = ds.dsBm.bmWidthBytes;
+ // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
+ // multiple of 2, as required by the documentation. So we use the official
+ // formula, which we already use elsewhere.)
+ const LONG bytesPerRow =
+ wxDIB::GetLineSize(ds.dsBm.bmWidth, ds.dsBm.bmBitsPixel);
data.m_stride = -bytesPerRow;
char *bits = (char *)ds.dsBm.bmBits;
if ( !Ok() )
return;
- // the cast is ugly but we can't do without it and without making this
- // function template (and hence inline) unfortunately
- typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> PixelData;
- PixelData& data = (PixelData &)dataBase;
-
- if ( !data )
+ if ( !&dataBase )
{
// invalid data, don't crash -- but don't assert neither as we're
// called automatically from wxPixelDataBase dtor and so there is no
return;
}
- if ( GetBitmapData()->m_hasAlpha )
- {
- // AlphaBlend() wants to have premultiplied source alpha but
- // wxRawBitmap API uses normal, not premultiplied, colours, so adjust
- // them here now
- PixelData::Iterator p(data);
-
- const int w = data.GetWidth();
- const int h = data.GetHeight();
-
- for ( int y = 0; y < h; y++ )
- {
- PixelData::Iterator rowStart = p;
-
- for ( int x = 0; x < w; x++ )
- {
- const unsigned alpha = p.Alpha();
-
- p.Red() = (p.Red() * alpha + 127) / 255;
- p.Blue() = (p.Blue() * alpha + 127) / 255;
- p.Green() = (p.Green() * alpha + 127) / 255;
-
- ++p;
- }
-
- p = rowStart;
- p.OffsetY(data, 1);
- }
- }
-
// if we're a DDB we need to convert DIB back to DDB now to make the
// changes made via raw bitmap access effective
if ( !GetBitmapData()->m_isDIB )
{
// copy one DIB line
unsigned char *dst = dstLineStart;
- for ( int x = 0; x < w; x++ )
+ if ( alpha )
{
- // also, the order of RGB is inversed for DIBs
- *dst++ = src[2];
- *dst++ = src[1];
- *dst++ = src[0];
-
- src += 3;
-
- if ( alpha )
- *dst++ = *alpha++;
+ for ( int x = 0; x < w; x++ )
+ {
+ // RGB order is reversed, and we need to premultiply
+ // all channels by alpha value for use with ::AlphaBlend.
+ const unsigned char a = *alpha++;
+ *dst++ = (src[2] * a + 127) / 255;
+ *dst++ = (src[1] * a + 127) / 255;
+ *dst++ = (src[0] * a + 127) / 255;
+ *dst++ = a;
+ src += 3;
+ }
+ }
+ else // no alpha channel
+ {
+ for ( int x = 0; x < w; x++ )
+ {
+ // RGB order is reversed.
+ *dst++ = src[2];
+ *dst++ = src[1];
+ *dst++ = src[0];
+ src += 3;
+ }
}
// pass to the previous line in the image