5 Replies Latest reply on May 4, 2012 3:45 PM by Gil1

    How to create a filter to do a bitmapdata compare and another to do a threshold?

    Gil1 Level 1

      I am new to PixelBender. I have looked at it many times in the last few years, but I can understand how it works.

       

      I want to compare a large number of images and I would like to try PixelBender to create a filter that does the same thing as the

      I think PB should be faster.

       

      Also, I want to create a filter equivalent to the AS3 BitmapData threshold() method.

       

      Any suggestions on how I can do those 2 filters?

        • 1. Re: How to create a filter to do a bitmapdata compare and another to do a threshold?
          Gil1 Level 1

          I experimented a little trying to do the equivalent of a BitmapData.compare() in Pixel Bender by writing it as:

           

          <languageVersion : 1.0;>

           

           

          kernel compare1

          <   namespace : "Your Namespace";

              vendor : "Your Vendor";

              version : 1;

          >

          {

              input image4 src;

              input image4 src2;

              output pixel4 dst;

           

           

              void

              evaluatePixel()

              {

                  pixel4  color = sampleNearest(src, outCoord());

                  pixel4  color2 = sampleNearest(src2, outCoord());

                 

           

           

                 if (color == color2) {

                      dst = float4(0.0, 0.0, 0.0, 0.0);

                 } else{

                      dst = float4(1.0,1.0,1.0,1.0);

                  }

                 

              }

          }

           

          It kind of work when selecting GPU, but when selecting Flash as the way to run it, it produced a different result (and not at all what I was expecting). Only it seems to work OK when the pixels on both images are either black or white.

           

          Any ideas why this is not working? And off course any suggestions on how to create a filter that does something similar to BitmapData threshold are welcome.

          • 2. Re: How to create a filter to do a bitmapdata compare and another to do a threshold?
            PS::Chuck Adobe Employee

            I tried a few different formulations with the pixel bender toolkit and you're right that something is wonky.  I think the scaling in the toolkit might be the issue rather than the flash code itself.  It appears to treat the first and second images differently, which is a bug.

             

            Have you tried running your filter in a flash program, rather than via the toolkit?

             

            Thanks,

            Chuck.

            • 3. Re: How to create a filter to do a bitmapdata compare and another to do a threshold?
              Gil1 Level 1

              Chuck,

               

              I run the the filter ina a flash program getting a different result that the one I expected.

              • 4. Re: How to create a filter to do a bitmapdata compare and another to do a threshold?
                PS::Chuck Adobe Employee

                Can you post the actionscript you are using?  The Flash pathway in the PixelBender toolkit seems to be broken for this scenario, but I may be able to debug via your program.

                 

                Thanks,

                Chuck.

                • 5. Re: How to create a filter to do a bitmapdata compare and another to do a threshold?
                  Gil1 Level 1

                  Chuck here is the code. The images are created dynamically for the test.

                   

                  <?xml version="1.0" encoding="utf-8"?>

                  <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                                                   xmlns:s="library://ns.adobe.com/flex/spark"

                                                   xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%"

                                                   creationComplete="init()">

                            <fx:Declarations>

                                      <!-- Place non-visual elements (e.g., services, value objects) here -->

                            </fx:Declarations>

                   

                            <fx:Script>

                                      <![CDATA[

                                                import flash.display.Bitmap;

                                                import flash.display.BitmapData;

                                                import flash.display.Shader;

                                                import flash.display.ShaderJob;

                                                import flash.geom.Rectangle;

                                                import flash.utils.getTimer;

                   

                                                import utils.BitmapSaver;

                                                import utils.CompareShader;

                   

                                                private var initialTime : int;

                                                private var loadComplete : int;

                                                private var scale:Number = 10;

                                                private var outputBitmapData:BitmapData;

                                                [Bindable] private var img:Bitmap;

                   

                                                [Embed(source = 'compare.pbj', mimeType = 'application/octet-stream')]

                                                private var TestShader:Class;

                   

                                                private function init():void

                                                {

                                                          var myBitmapData:BitmapData = new BitmapData(400*scale, 400*scale, false, 0x00FF00);

                                                          outputBitmapData = new BitmapData(myBitmapData.width, myBitmapData.height);

                   

                                                          var rect:Rectangle = new Rectangle(100*scale, 100*scale, 120*scale, 120*scale);

                                                          myBitmapData.fillRect(rect, 0xff0000);

                   

                                                          var bm:Bitmap = new Bitmap(myBitmapData);

                                                          a.source =bm;

                   

                                                          var bd2:BitmapData = new BitmapData(400*scale, 400*scale, false, 0x00FF00);

                                                          rect = new Rectangle(180*scale, 180*scale, 80*scale, 80*scale);

                                                          bd2.fillRect(rect, 0xff0000);

                   

                                                          var bm2:Bitmap = new Bitmap(bd2);

                                                          b.source=bm2;

                   

                                                          initialTime = getTimer();

                                                          var diffBmpData:BitmapData = myBitmapData.compare(bd2) as BitmapData;

                                                          var r:Bitmap = new Bitmap(diffBmpData);

                                                          c.source=r;

                   

                   

                                                          loadComplete = getTimer();

                                                          msg.text = "Process time: " + (loadComplete - initialTime) + "ms - - No. Pixels processed: " + String((400*scale)*(400*scale));

                   

                                                          initialTime = getTimer();

                   

                   

                                                          var myShader:Shader = new Shader(new TestShader() as ByteArray);

                                                          myShader.data.src.input = myBitmapData;

                                                          myShader.data.src2.input = bd2;

                   

                                                          //create the ShaderJob

                                                          var myJob:ShaderJob = new ShaderJob(myShader, outputBitmapData);

                                                          myJob.addEventListener(ShaderEvent.COMPLETE, onComplete); //Needed if passing the true parameter on myJob.start();

                                                          //and run it!

                                                          myJob.start();

                                                          img = new Bitmap(outputBitmapData);

                   

                                                          loadComplete = getTimer();

                                                          msg.text += "\nProcess time with shader: " + (loadComplete - initialTime) + "ms";

                   

                                                }

                   

                                                private function onComplete(e:ShaderEvent):void

                                                {

                                                          img = new Bitmap(outputBitmapData);

                                                          loadComplete = getTimer();

                                                          msg.text += "\nProcess time with shader: " + (loadComplete - initialTime) + "ms";

                                                }

                   

                                                private function saveImg(img:Image):void

                                                {

                                                          var tBS:BitmapSaver = new BitmapSaver();

                                                          tBS.savePNG(img);

                                                }

                   

                                      ]]>

                            </fx:Script>

                            <s:HGroup width="100%" height="100%" >

                                      <s:Image id="a" width="400" height="400" click="saveImg(a)"/>

                                      <s:Image id="b" width="400" height="400" click="saveImg(b)"/>

                                      <s:Image id="c" width="400" height="400" />

                                      <s:Image id="d" width="400" height="400" source="{img}" click="saveImg(d)"/>

                            </s:HGroup>

                            <s:Label id="msg" width="100%" y="450" />

                   

                  </s:Application>

                   

                  Gilbert