AS3 Bitmap black and white - for compression reasons

210 views Asked by At

I have a field made up of BitmapData, which I use for pixel-precise hit detection.

However, BitmapData naturally stores 2^32 (or 2^24 with no alpha?) possibilities for each pixel. I only need 2 - black or white.

But I still need to use .draw to make other objects being drawn onto that BitmapData. It doesn't need to be visible.

Extracting a pixel for hit-detection does not seem too difficult - but drawing without cycling through each pixel seems hard. Is it possible?

What would the right approach for this problem be?

1

There are 1 answers

0
Vesper On

If you depend on having your bitmap data to be black or white only, you can employ BitmapData.threshold() after drawing a new mask over that bitmap. To turn your existing BitmapData to black and white with a threshold of half red channel do the following:

bd.threshold(bd,bd.rect,new Point(),"<",0x00800000,0x0,0x00ff0000,true);
bd.threshold(bd,bd.rect,new Point(),">=",0x00800000,0x00ffffff,0x00ff0000,true);

The first call with turn all points that have red below 0x80 black, the second will turn all the remaining points white. Change the mask and threshold value to use green or blue channels if you want. Consider applying a properly channeled ColorTransform object to your draw calls to make the mask correctly applied to a newly drawn object.