Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Use magenta as transparent color

Credits go to jmasterx for helping out!
This thread is locked; no one can reply to it. rss feed Print
Use magenta as transparent color
Diego Barboza
Member #16,292
April 2016

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

jmasterx
Member #11,410
October 2009

Diego Barboza
Member #16,292
April 2016

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.

jmasterx
Member #11,410
October 2009

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)
else
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
Member #1,136
March 2001

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.

Pete

l j
Member #10,584
January 2009
avatar

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
Member #10,079
August 2008
avatar

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):

https://github.com/drcouzelis/colorwandcastle/blob/master/src/resources.h

https://github.com/drcouzelis/colorwandcastle/blob/master/src/resources.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
Member #1,136
March 2001

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
Member #10,079
August 2008
avatar

Oh wow, I totally misunderstood the situation.

Please consider disregarding me! ;D

Diego Barboza
Member #16,292
April 2016

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!

Go to: