0

When i use bitmapdata threshold, what happens to the pixels that fail the threshold test? As per my observation they remain as in, so is there any way to remove them?

Netrus
  • 79
  • 10
  • do another threshold with replacement color of 0x00000000 (ie: zero alpha) –  Aug 02 '13 at 12:59
  • @LeeBurrows This would work if a threshold color would be within the threshold, aka calling the same threshold will not alter the bitmap further. But it's possible that the color passed into `BitmapData.threshold()` will be naturally outside the threshold, thus this approach will nullify the entire bitmap. – Vesper Aug 05 '13 at 13:49
  • @Vesper. Netrus could reverse the condition on second threshold to affect the other set of pixels. ie: if first threshold was using ">" then second ones uses "<" –  Aug 05 '13 at 17:55
  • @LeeBurrows Yes, but it's not enough. Say you have a bitmap consisting of two colors, 0x305070ff and 0xaaaaaaaa. You want a threshold of alpha greater than 0x80 to turn into 0x55ff0000. So, you call `threshold()` and have a BitmapData of two colors, 0x305070ff and 0x55ff0000. Now you want to nullify all the other pixels, you call threshold with reversed operation and BAM, all your bitmapdata now consists of zeroes! That's not what you normally want, right? – Vesper Aug 06 '13 at 03:46
  • thnx guys! :D bt making a temporary bitmap for copying the threshold that passed the test worked for me.. – Netrus Aug 07 '13 at 10:26

1 Answers1

1

The best way would be to use a temporary (static, reusable) transparent BitmapData for this operation. You fill it with 0x0, then call threshold() setting source to your BitmapData, and copySource flag set to false, then you copyPixels() back with mergeAlpha set to false.

var tbd:BitmapData=yourBitmapData.clone(); // this makes a new BitmapData, so be warned
var p0:Point=new Point();
tbd.fillRect(tbd.rect,0);
tbd.threshold(yourBitmapData,yourBitmapData.rect,p0,yourOperation,
    yourThreshold,yourColor,yourMask,false);
yourBitmapData.copyPixels(tbd,tbd.rect,p0);
tbd.dispose();
Vesper
  • 18,599
  • 6
  • 39
  • 61