Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Transparency lost on conversion to .bmp, how to make images transparent?

This thread is locked; no one can reply to it. rss feed Print
Transparency lost on conversion to .bmp, how to make images transparent?
newn01
Member #12,120
July 2010

Hi everyone. My ball's lost his transparency on the image conversion to .bmp. Used paint though, but as far, as my Photoshop knowledge goes, you would lose the transparency anyway. How can i make the flying thing transparent?

And sorry for creating so many threads, but i'm trying to lear as much as i can, as good as i can and as fast as i can. Also i'm trying to enjoy it. The problem is, that i'm working on many things at once. Well, one game, but many things not finished, like collision is still not working perfectly on the ball and the paddles and ball cannot pass the walls yet. Also paddles can go up and down under the screen, so yea, my problem is, that i don't focus on one thing... Anyway, Half game made anyway, hehe.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Transparency can be done in several ways:

1. Use alpha channel in 32 bit pixels. This requires something other than BMP, for instance TGA files.

2. Use "magic pink". Software rendering will check if a texel is 0xFF00FF, and if it is, won't draw that pixel.

3. IIRC, in 8 bit mode (old and pretty much obsolete) index 0 won't be drawn.

They all watch too much MSNBC... they get ideas.

Tobias Dammers
Member #2,604
August 2002
avatar

First of all, there's transparency (a.k.a. transluciency), and there's masking. Those are two different things.

Transluciency means that the source and destination pixels are interpolated based on an opacity factor; the formula for blending a pixel is relatively simple:
c = a + (b - a) * f
where c is the blended result, a is the original color on the destination bitmap, b is the source bitmap's color, and f is the opacity factor. You can use a constant blending factor for the entire blit operation, but it's more likely that you want a different one per pixel. This can be achieved by storing the opacity (typically called 'alpha' in this context) in the bitmap, along with the red, green, and blue components. This is what 32 bit RGBA sprites are all about. However, the BMP and PCX formats do not support alpha channels. TGA does, however. It isn't the most efficient format, so once you get comfortable with it, you might consider using PNG instead. Allegro 4.x doesn't support it natively, but there's an add-on available.

Masking is much simpler. It works by defining one special ('magic') color as the mask color; when blitting, pixels in the source bitmap that are exactly equal to the mask color are skipped, leaving the destination bitmap intact. In allegro, the magic color is always 'magic pink' (full red and blue, zero green) for 15, 16, 24, and 32 bpp, and index 0 for 8 bpp. Masking is much faster in software-rendered graphics, because it requires no multiplication and no reading from the destination bitmap. However, there are only two levels of opacity - zero and full. For masking, all you need to do is set all pixels that are supposed to be transparent to magic pink, and use a blitting function that uses masking, e.g. draw_sprite() or mask_blit(). Since you don't rely on alpha channels in the source bitmap, you can use any bitmap format you like, including BMP.

A final note: MS Paint is pure crap. Not only is it severly limited in its features, it is also hilariously wrong in so many ways, and full of odd bugs and inconvenient design decisions. It's only slightly more useful than typing BMP image data into a hex editor. If you want something free that has at least the basic set of features, without being as frighteningly complex as photoshop, try Paint.NET.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

newn01
Member #12,120
July 2010

Nice! Thanks! The Paint.NET does the job alright and doesn't weight a few gigs like Photoshop does. Also the image works perfectly, the ball ain't in a white square anymore. :) Okay, now let me get back to the collision stuff.

Thanks for help!

Go to: