Converting a wx.Bitmap to a wx.Image, and back again - and repeating this process - yields different results on different platforms when alpha channels are involved. Here's a small sample program that demonstrates the problem (screenshots below):
<http://www.saunalahti.fi/znark/alpha_bitmaps/alpha_bitmaps.py>
What the program does is as follows:
1) Prepare a blue background bitmap of 16x16 pixels. This
bitmap also features a white "diagonal lines" hatch
pattern.
2) Iterate through a loop of 16 steps while advancing from
left to right. Blit a row of 16 (16x16 pixel) bitmaps.
The algorithm for blitting goes as follows:
- Blit the background bitmap first. (This bitmaps
stays the same throughout the loop.)
- Then prepare a foreground bitmap of the same size -
with an alpha channel - and blit it on the top of
the background bitmap. All foreground bitmaps are
the same shade of red but their alpha channel
grows more opaque in each step.
- Advance to the next position on the right.
In order to manipulate the alpha channel, the red wx.Bitmap is converted to a wx.Image. The values for the alpha channel are set and the image is then converted back to a wx.Bitmap, for blitting. Lather, rinse, repeat.
Essentially, the "same" wx.Bitmap - or at least its RGBA buffer data - is converted back and forth a number of times.
* * *
For some reason, this does not work on GTK. Something weird seems to be going on with the colors and the transparency values (alpha channel data):
<http://www.saunalahti.fi/znark/alpha_bitmaps/wxgtk-screenshot.png>
python-wxgtk2.8 (2.8.7.1-0ubuntu3 - again, with Python 2.5)
On MS Windows, however, everything works fine:
<http://www.saunalahti.fi/znark/alpha_bitmaps/wxmsw-screenshot.png>
wxPython 2.8.6.0 (unicode) for Python 2.5
For comparison, the sample program also blits another stripe of blocks below the first one. This time, the only difference is that a brand new wx.Bitmap is created for each "foreground" blit - by the force of wx.EmptyBitmap - instead of reusing the existing bitmap. This approach works on both platforms.
* * *
Could anyone offer any insight on...
1) Why is this happening in the first place?
2) Is it a bug or a "feature"?
3) If it's "by design", how do I know when I'm allowed to convert
a wx.Bitmap to a wx.Image (and back) without getting these
kind of surprises... and when not?
4) If it is a bug, is it a bug in the actual wxWidgets tookit
or the wxPython wrapper?
···
--
znark