, Sr. Member
Artemis - 23 February 2012 11:41 PM
alphaThreshold is just checking out if given pixel has a transparency above/below a certaing value and makes the pixel either fully opaque or fully transparent?
Yes, something like that.
Artemis - 23 February 2012 11:41 PM
is there any way to combine those methods so for every pixel below the threshold it would be fully transparent and for every pixel above the threshold it would stay with unchanged transparency value?
I don’t think so.
The only way, I can think of, to do exactly what you need is to use BitmapData functions getPixel32 and setPixel32.
Loop thorough all pixels, get their alpha and if its below the threshold set it to 0, if its above leave it. If you want it done at a GPU level you’ll have to write a shader which is much more complex.
getPixel32 and setPixels32 use the color as uint value in this order (alpha, red, green, blue) each with value 0-255.
Here are some functions to turn the uint to understandable values and put those back in uint so you can setPuxel32 them:
private function extractRed(c:uint):uint {
return (( c >> 16 ) & 0xFF);
}
private function extractGreen(c:uint):uint {
return ( (c >> 8) & 0xFF );
}
private function extractBlue(c:uint):uint {
return ( c & 0xFF );
}
function extractAlpha(c:uint):uint {
return (( c >> 24 ) & 0xFF);
}
private function combineARGB(a:uint,r:uint,g:uint,b:uint):uint {
return ( (a << 24) | ( r << 16 ) | ( g << 8 ) | b );
}
...oh what the hell I’ll write down the main function too ;)
public function AlphaCut(bitmap_base:BitmapData, threshHold:uint):BitmapData {
var tempPixel:uint;
var tempAlpha:uint;
if(threshHold < 1 || threshHold > 254) throw new Error("threshHold should be between 1 and 254!");
for (var i:uint = 0; i < bitmap_base.height; i++){
for (var l:uint = 0; l < bitmap_base.width; l++){
tempPixel = bitmap_base.getPixel32(l, i);
tempAlpha = extractAlpha(tempPixel);
if(tempAlpha < threshHold){
tempAlpha = 0;
tempPixel = combineARGB(tempAlpha, extractRed(tempPixel), extractGreen(tempPixel), extractBlue(tempPixel))
bitmap_base.setPixel32(l, i, tempPixel)
}
}
}
return bitmap_base;
}
PS.use this with alpha only, not alphaThreshold