Use magenta as transparent color
Diego Barboza

Hello everyone!

Is there a way to specify color to use as a transparency mask for a bitmap in Allegro 5?

I'm creating some bitmaps at run-time and based on a previously acquired image with no alpha channel (these images are also acquired at run-time). So, I use magenta (255, 0, 255) as a key color and I'm trying to replace this for alpha == 0.

This gives me the expected effect, but it's very slow. Is it possible by any means to tell Allegro to always use magenta as transparency?

al_convert_mask_to_alpha(buffer, al_map_rgb(255, 0, 255));

Thanks in advance,

Diego Barboza

jmasterx, thanks for the link, but as I said on my initial post, I've already tested this function and it's too slow to use every time I create a new bitmap.


You should probably create your bitmaps as ALLEGRO_MEMORY bitmaps, apply the mask conversion, then convert to video. If they are created as video, then to alter it, you have to do a lot of extra work.

If none of that works, the more modern solution might be to apply a shader when you render these bitmaps. The shader logic is:

if(pixelcolor == magic pink)
return color(0,0,0,0)
return pixelcolor

The other option is, if you can, before creating the allegro bitmaps, remove the pink from whatever memory buffer you're creating them from.

Peter Hull

How slow is too slow? I did some quick checking and it takes about 4 milliseconds on my laptop to call al_convert_mask_to_alpha(buffer, al_map_rgb(255, 0, 255)) on a 256x256 bitmap.

You may be able to get quicker if you use al_lock_bitmap and roll your own conversion.


l j

At "run-time" is quite vague. Are you generating them in real-time or something?
How often are you calling al_convert_mask_to_alpha?

David Couzelis

This gives me the expected effect, but it's very slow.

That's why I use a set of helper functions I wrote (in C):

The code IMG("filename.png"); will return a pointer to an ALLEGRO_BITMAP. The first time you use that filename it is loaded from the hard drive into memory. After that, every time you load the same filename it just reuses the bitmap that's already been loaded into memory.

Peter Hull

The first time you use that filename it is loaded from the hard drive

good, apart from...

I'm creating some bitmaps at run-time

DB could you give us some more detail on how, and how often, these bitmaps are created?

David Couzelis

Oh wow, I totally misunderstood the situation.

Please consider disregarding me! ;D

Diego Barboza

Hi everyone,

I'm making a video streaming application, so these bitmaps are generated every frame. I followed jmasterx's advice and used a shader. It's pretty easy to implement and it's very fast! Thank you all!

Thread #616212. Printed from