views:

144

answers:

2

There is a specific bitwise logical op on three bitmaps I would like to achieve in Flex/AS3. It would presumably have to be done with some combination of DisplayObject blendmodes, ColorMatrix filters, and possibly something else I haven't thought of, as FLex/As3 doesn't have bitwise logical ops on pixels (except for BlendMode.INVERT).

So here is the operation:

(B & S) | (F & ~S)

[where B,S,F are three bitmaps]

That's it.

Incidentally, I tried this with Pixel Bender, but incredibly, it doesn't have bitwise logical ops either. (What exactly were you thinking Adobe). So I simulated them without about 50 modulos and divides but it came out way too slow. (Could be because I didn't have a supported graphcs card which brings up another question - if Pixel Bender only works with certain cards, how do you find out at runtime from Flex/AS3 if the browser computer has a supported card.)

But anyway, my main question is how to perform that trivial little bitwise op above in Flex/AS3. (It would have to be as fast as BlendModes).

+1  A: 

I'm not very experienced using bitwise operations, but i threw together a quick test case using the fancy new Flash player 10 vectors which are very nice for this type of data wrangling.

This runs through the 2000x2000 pixels in 115ms using the standalone debug player on my computer, it'll likely be a bit faster in the release player.

I'm not sure if this is fast enough since I don't know how often or on how large images you need to run it, but it might at least be a starting point.

package  {
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.geom.Rectangle;
    import flash.text.TextField;
    import flash.utils.getTimer;
    /**
     * ...
     * @author Martin Jonasson
     */
    public class Test extends Sprite {

     public function Test() {

      var output:TextField = new TextField();
      output.autoSize = "left";
      addChild(output);

      var bmpB:BitmapData = new BitmapData(2000, 2000, false, 0xff00ff);
      var bmpS:BitmapData = new BitmapData(2000, 2000, false, 0xffffff);
      var bmpF:BitmapData = new BitmapData(2000, 2000, false, 0x000000);

      var rect:Rectangle = new Rectangle(0, 0, 2000, 2000)

      var vecB:Vector.<uint> = bmpB.getVector(rect);
      var vecS:Vector.<uint> = bmpS.getVector(rect);
      var vecF:Vector.<uint> = bmpF.getVector(rect);

      var vecFinal:Vector.<uint> = new Vector.<uint>(vecB.length, true);

      var startTime:int = getTimer();
      for (var i:int = vecB.length - 1; i >= 0; --i) {
       vecFinal[i] = (vecB[i] & vecS[i]) | (vecF[i] & ~vecS[i]);
      }
      output.appendText("bitwise stuff done, took: " + (getTimer() - startTime) + "ms \n");

      startTime = getTimer();
      var bmpFinal:BitmapData = new BitmapData(2000, 2000, false);
      bmpFinal.setVector(rect, vecFinal);
      output.appendText("created bitmapdata, took: " + (getTimer() - startTime) + "ms \n");

     }

    }

}
grapefrukt
Just now saw this - I'll try it out and get back to you; thanks.
A: 

To grapefrukt:

With 1000X1000 bitmaps its 47 ms on my system, so this is looking very promising (approaching standard refresh rates with some tweaks one would hope).

I learned Flex 2 first, so didn't know about Vectors, which are evidently just highly efficient one-type only arrays.

I need to start paying it forward at some point, because this forum is amazing (4 for 4 on some fairly obscure questions since I first found out about it 10 days ago.)

Thanks again.

i'm glad it works for you, solving obscure stuff is the most fun really so you've come to the right place!(and oh, do respond as a comment not as an answer as it gets a bit weird chronologically.)
grapefrukt