O'Reilly logo

ActionScript 3.0 Cookbook by Joey Lott, Darron Schall, Keith Peters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Applying a Filter to a Bitmap

Problem

You want to apply a filter to a BitmapData.

Solution

Use the applyFilter() method of the BitmapData class, or add a filter to the filters array of the enclosing Bitmap.

Discussion

As mentioned, there are two methods of applying a filter to a bitmap. The first method is to use the applyFilter() method directly on the BitmapData itself. Like some of the other BitmapData methods, this method is applied to one BitmapData, which is the destination bitmap, and it can take another BitmapData as a source bitmap:

destBmp.applyFilter(srcBmp, sourceRect, destPoint, filter);
  • The srcBmp is the bitmap file you want to apply the filter to.

  • The sourceRect is the specific rectangular area from the source bitmap that you want to apply the filter to.

  • The destPoint parameter specifies the upper lefthand point from which the pixels will be affected in the destination bitmap.

  • The filter, of course, is an instance of the BitmapFilter that you want to apply.

Tip

You can, of course, apply a filter using the same bitmap as both source and destination. Doing so is considered “destructive,” not because it’s a bad thing but because it permanently changes the bitmap.

The values of each pixel are calculated based on the original content and the filter parameters, and these values overwrite the original pixel data. This is useful in a couple of instances:

  • If you are sure you will never need to change either the bitmap’s content or the filter itself.

  • When you have multiple or repeated ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required