LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Rescaling bitmap with transparency

Hello,
 
I am looking for a way to rescale a bitmap with transparency. Now I am using the following method:
- The user can select a picture file on his disk. All file formats that CVI supports can be choosen (bmp, png, ico).
- This picture is put on a picture control, using the DisplayImageFile function.
- Now, I need to store this picture, so I can put it back on the picture control the next time the user launches my program. But I only need to store a picture of 32 by 32 pixels. Since the user can select a huge file, I first want to rescale this picture to the 32x32 format. Appearantly, the GetCtrlBitmap function returns a bitmap which is the same size of the selected picture. So my idea was to draw this bitmap on a 32x32 invisible canvas, using the CanvasDrawBitmap function, and then getting the picture back from the canvas, using the GetCtrlBitmap function on this canvas.
 - OK, now I got a 32x32 bitmap, which I could save to a png file, using the  SaveBitmapToPNGFile function. Problem is: the transparency of my picture is lost! Suppose the user selected a png file with a transparent area. Putting this file to the picture control, and getting the bitmap from this control, I get a bitmap with an alpha channel, indicating the transparency of the pixels. After I draw this bitmap to the canvas (canvas has a transparent background!) and then getting the bitmap from the canvas. The alpha channel of the bitmap is gone.
 
So now I am looking for some way to rescale the original bitmap (with transparency) into a 32x32 bitmap. I am pretty sure that some really good algorithms already exist, so I'd rather not write them myself Smiley Wink
 
All hints are appreciated.
 
Wim
Gemidis n.v. - Innovating Display Technology
Ghent, Belgium
0 Kudos
Message 1 of 2
(3,135 Views)

OK, I found a workaround for my problem.

When the bitmap uses an Alpha channel, I convert this alpha channel to a mask before drawing the bitmap on the canvas. Unfortunately this conversion is not that easy. You should note that an alpha channel contains a byte for each pixel, while a mask has one bit per pixel. Also, a mask has to contain an even set of bytes for each pixel row.  So when the width of the picture is not a multiple of 16, dummy bits should be added to the end of each row.

Appearantly the alpha channel is thrown away by the canvas, but the mask remains. Is this some bug in CVI? In the conversion I set the mask bit to '1' when alpha is higher then 127, and to '0' for the other alpha values. Problem solved, but with quite some effort...

0 Kudos
Message 2 of 2
(3,127 Views)