[BUG][FIX] Read 24-bit BMP files have screwy alpha channel

Martin McClure martin at hand2mouse.com
Fri Jul 20 22:37:20 UTC 2001


The things you discover through ignorance! I wanted to render images 
in Strata 3D and then import the rendered images, with alpha channel, 
to Squeak.
I tried using BMPs to do this. The problem with that was that BMPs 
don't support an alpha channel, but I didn't know that, and Strata 
let me render to BMP with the alpha channel box checked, so I thought 
I had a BMP with an alpha channel.

When I read the BMP into Squeak and drew it to the display with the 
blend combination rule, I got a curious result. Only the black pixels 
were displayed, as all the other pixels were completely transparent.

I dug in, figured out what was going on, and produced the attached 
changeset to fix the problem.
The code in Form class>>bmp24BitPixelDataFrom:width:height: always 
produced a 32-bit Form from a 24-bit BMP, but it didn't explicitly 
set the alpha channel values, so they remained zero, fully 
transparent.
The black pixels were opaque through a side-effect of special 
handling for them that changed their value from pure black (r=0, g=0, 
b=0) to very dark blue (r=0, g=0, b=1) to avoid the Squeak convention 
that pixels whose overall value is zero are considered transparent. 
Besides setting blue to 1, the alpha value was set to 255, fully 
opaque.
My fix simply sets the alpha value of all pixels to 255 so that the 
entire BMP is opaque. I also removed the special handling for pure 
black, since a pixel of (a=255, r=0, g=0, b=0) does not have an 
overall value of zero and so is not considered transparent in the 
situations I've tried/seen.

PLEASE NOTE that I'm not sure this is correct for all situations. I'd 
appreciate it if someone with more knowledge of Squeak's transparency 
handling were to verify or refute my thinking.

My fix only affects the reading of 24-bit BMPs I haven't checked 
other bit-depths to see if they might have a similar problem.


My procedure for reproducing the bug:
* Start with a 3.1-4173 image
* Set display depth to 32 (is the blend combination rule valid for a 
destination Form of lower depth?)
* In a File List, select earth.bmp (attached to this message, or any 
24-bit BMP file with some pure-black pixels would work).
* From the file list pane's yellow-button menu, select 'read graphic 
into ImageImports'.
* Draw the image to the screen by executing something like the following:
(ImageImports at: 'Earth') displayOn: Display at: 10 @ 10 
clippingBox: (10 at 10 corner: 160 at 160) rule: Form blend fillColor: nil

Note that only the black pixels are drawn, since the others are transparent.
After filing in the attached changeset repeating this procedure will 
result in an image of Earth on a black background, with the entire 
square opaque.

Next I'm writing a TiffReadWriter...

-Martin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Earth.bmp
Type: image/x-bmp
Size: 67854 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20010720/bf2060b5/Earth.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BmpTransparencyFix.cs
Type: application/octet-stream
Size: 903 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20010720/bf2060b5/BmpTransparencyFix.obj


More information about the Squeak-dev mailing list