1

I've added a black and white filter onto an image using the following code:

var n:Number = 1/3;
var matrix:Array = [n,n,n,0,0,
                    n,n,n,0,0,
                    n,n,n,0,0, 
                    0,0,0,1,0];
var cmf:ColorMatrixFilter = new ColorMatrixFilter(matrix);

_bitmap.bitmapData.applyFilter(_buffer, _sourceRect, new Point(), cmf);

I now want to be able to remove this filter but I can't seem to figure out how. I've read that if I clear the _bitmap.filters array it should be removed, but when I check, this array is empty.

Does anyone have any suggestion about how I might do this?

Edit I'm using the FlashPunk game engine and I'm manipulating the bitmapData from the Image.as class. All my code is written and compiled using FlashDevelop.

EDIT

I was not able to apply a filter directly onto the bitmap due to the fact that the image class in the flashpunk flashpunk engine was drawing the bitmap using the bitmapData.CopyPixels() function. The filter was not applied to the bitmapData and was therefore not being drawn.

I've changed the render method to use the bitmapData.draw() function which uses the actual bitmap to draw the image.

I can now add a filter to my bitmap by doing the following:

_bitmap.filters = [ColorMatrixFilter];

I can then remove my filters by doing the following:

_bitmap.filter = [];
John Lilley
  • 159
  • 1
  • 3
  • 13

1 Answers1

5

When you apply Filter directly to a bitmapdata, you cannot remove it because filter modify pixel color definitively, however, you can try to revert it by doing reverse operation :

var matrix:Array = [1/n,1/n,1/n,0,0,
                    1/n,1/n,1/n,0,0,
                    1/n,1/n,1/n,0,0, 
                    0,0,0,1,0];

If you want to be able to remove dynamicly filters, you have to apply them on Bitmap Object and not BitmapData Object. Then you can do _bitmap.filters = []; to remove all filters

Here an example showing how to remove filter after 4 seconds :

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.filters.ColorMatrixFilter;
    import flash.utils.setTimeout;

    public class TestTextfield extends Sprite
    {
        public function TestTextfield()
        {
            var bd1 : BitmapData = new BitmapData(300,300);
            var randomNum:Number = Math.floor(Math.random() * 10);
            bd1.perlinNoise(100, 80, 6, randomNum, false, true, 1, true, null);

            var n:Number = 1/3;
            var matrix:Array = [n,n,n,0,0,
                                n,1,n,0,0,
                                n,1,n,0,0, 
                                0,0,0,1,0];
            var cmf:ColorMatrixFilter = new ColorMatrixFilter(matrix);

            var bitmap : Bitmap = new Bitmap(bd1);

            bitmap.filters = [cmf];

            addChild(bitmap);

            setTimeout(function():void{
                bitmap.filters = [];
            }, 4000);
        }

    }
}
Simon Eyraud
  • 2,445
  • 1
  • 20
  • 22
  • If I add the reverse filter onto the Image it just turns it all white. When i try add to _bitmap.filters nothing seems to happen. What is the correct syntax for doing this? – John Lilley May 27 '11 at 12:03
  • For the first method yes, i don't know the right matrix, I eddited my answer to add an exemple to how remove filters. – Simon Eyraud May 27 '11 at 12:25
  • Hmm, thats the way I assumed it was done. This however doesn't work for me. I think it's due to the way the flashpunk engine creates it's images. If i add the bitmap as a child directly after applying the filter, a new image is drawn on the top left of the screen with the filter but my original image is still there in its correct place without the filter. – John Lilley May 27 '11 at 13:57
  • The way I believe that flashpunk draws its images is that it copies the pixels from the bitmapData onto a rectangle on the screen, therefore not using the bitmap itself. This is why the filter worked when i applied it to the bitmap data but not the actual bitmap. – John Lilley May 27 '11 at 14:04
  • Therefore I don't believe what I want to do is possible without actually editing the engine. Thanks for your help though. – John Lilley May 27 '11 at 14:05