Best way to modify Alpha Channel

Hi guys

Which is the best way you consider to modify the alpha channel of a Bitmap. I want to add some “rects” to make the bitmap transparent / opaque in some regions. And related with this question, are there any way to “extract” the transparency of an sprite and apply it to another sprite ?

You can try bitmapData.fillRect or bitmapData.getPixels and bitmapData.setPixels to change whole sections, you could also use bitmapData.copyChannel to copy the alpha channel from one to another

I’ve found that BitmapData::copyChannel() doesn’t work as expected on native targets…the bitmap is not rendered to screen for both Windows and OSX targets…It works fine on the HTML5 target though…

Would you mind whipping up a little sample app that helps illustrate the problem you’re seeing?

I am slightly confused about the best way to achieve this.

A and B are bitmaps with alpha.
all the white zones of bitmaps are Transparent.
And I want A + B as a final result… I only want in a few words which operations I should do to achive the result.

In other language I would make an “ExtractAlpha” of B image, I would make 2 Fill rects in that new alpha “temp”, then I would apply that alpha to image A and finally I would copypixel A over B using the created Alpha. But I am not sure which will be the best way to achieve it in Ofl

Thanks in advance

If they are both transparent, you could try doing something like…

var bitmapA = new Bitmap (Assets.getBitmapData ("A.png"));
var bitmapB = new Bitmap (Assets.getBitmapData ("B.png"));
var container = new Sprite ();
container.addChild (bitmapA);
container.addChild (bitmapB);

Then if you don’t need to modify anything further, you could just leave it like that, and never modify the BitmapData. If you do, however, you could then “draw” this to a new BitmapData:

var bitmapData = new BitmapData (container.width, container.height, true);
bitmapData.draw (container);

yes they are both transparent so If I do what you write I would get this

Which is not the result required.

As I told you I usually create an independent 8 bits bitmap with the alpha channel…work with it as needed and then set it back as an alpha in the original image. Wondering if something similar is possible in openFL

Ah, gotcha, yeah, this is what “copyChannel” is good at, I used to use that in Flash in order to reduce file sizes (compress the alpha channel as a low-quality PNG and use a black-background JPEG for the color data, 90% size savings), I’m not aware of an issue with it but if there are hiccups (like @dimumurray said) then I definitely want to see that fixed

Finally solved.

What I did is to create an auxiliary BitmapData object. Then I made a copyChannel from the alpha I want to modify into the green channel. I made all the modifications over the green channel of the auxiliary bitmap and then I made the inverse copy ( from to original.alpha).

However In the testing I found something “strange” with copyChannel. I couldn’t reproduce it, but it seems like copyChannel affects somehow the source channel.

Maybe is a clue and a kickstart to solve the problem mentioned by @dimumurray

Regards and once again. Thanks!!