Packageflash.filters
Classpublic class ConvolutionFilter
InheritanceConvolutionFilter Inheritance BitmapFilter Inheritance Object

Player version: Flash Player 8

The ConvolutionFilter class applies a matrix convolution filter effect. A convolution combines pixels in the input image with neighboring pixels to produce an image. A wide variety of imaging operations can be achieved through convolutions, including blurring, edge detection, sharpening, embossing, and beveling. You can apply this effect on bitmaps and MovieClip instances.

The use of filters depends on the object to which you apply the filter:

You can also apply filter effects to images and video at authoring time. For more information, see your authoring documentation.

If you apply a filter to a movie clip or button, the cacheAsBitmap property of the movie clip or button is set to true. If you clear all filters, the original value of cacheAsBitmap is restored.

A matrix convolution is based on an n x m matrix, which describes how a given pixel value in the input image is combined with its neighboring pixel values to produce a resulting pixel value. Each result pixel is determined by applying the matrix to the corresponding source pixel and its neighboring pixels.

For a 3 x 3 matrix convolution, the following formula is used for each independent color channel:

  dst (x, y) = ((src (x-1, y-1) * a0 + src(x, y-1) * a1....
                    src(x, y+1) * a7 + src (x+1,y+1) * a8) / divisor) + bias
  

When run by a processor that offers SSE (Streaming SIMD Extensions), certain filter specifications perform faster.

A filter is not applied if the resulting image would exceed 2880 pixels in width or height. For example, if you zoom in on a large movie clip with a filter applied, the filter is turned off if the resulting image reaches the 2880-pixel limit.

See also

flash.display.BitmapData.applyFilter()
MovieClip.filters
MovieClip.cacheAsBitmap


Public Properties
 Property
  alpha : Number
The alpha transparency value of the substitute color.
  bias : Number
Bias to add to the result of the matrix transformation.
  clamp : Boolean
Indicates whether the image should be clamped.
  color : Number
The hexadecimal color to substitute for pixels that are off the source image.
  divisor : Number
The divisor used during matrix transformation.
  matrix : Array
An array of values used for matrix transformation; returns a copy.
  matrixX : Number
The x dimension of the matrix (the number of columns in the matrix).
  matrixY : Number
The y dimension of the matrix (the number of rows in the matrix).
  preserveAlpha : Boolean
Indicates what the convolution applies to.
 Properties inherited from class Object
 __proto__, __resolve, constructor, prototype
Public Methods
 Method
  
ConvolutionFilter(matrixX:Number, matrixY:Number, matrix:Array, [divisor:Number], [bias:Number], [preserveAlpha:Boolean], [clamp:Boolean], [color:Number], [alpha:Number])
Initializes a ConvolutionFilter instance with the specified parameters.
  
Returns a copy of this filter object.
 Methods inherited from class BitmapFilter
 clone
 Methods inherited from class Object
 addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch
Property detail
alphaproperty
public var alpha:Number

Player version: Flash Player 8

The alpha transparency value of the substitute color. Valid values are 0 to 1.0. The default is 0. For example, 0.25 sets a transparency value of 25 percent.


Example
The following example changes the alpha property of filter from its default value of 1 to 0.35.
import flash.filters.ConvolutionFilter;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;

var alpha:Number = 0.35;
var filter:ConvolutionFilter = new ConvolutionFilter(3, 3, [1, 1, 1, 1, 1, 1, 1, 1, 1], 9, 0, true, false, 0x0000FF, alpha);

var myBitmapData:BitmapData = new BitmapData(100, 80, true, 0xCCFF0000);
    
var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc.attachBitmap(myBitmapData, this.getNextHighestDepth());
myBitmapData.noise(128, 0, 255, 1 | 2 | 4 | 8, false);
    
mc.onPress = function() {
    myBitmapData.applyFilter(myBitmapData, new Rectangle(0, 0, 98, 78), new Point(2, 2), filter);
}

biasproperty 
public var bias:Number

Player version: Flash Player 8

Bias to add to the result of the matrix transformation. The default is 0.


Example
The following example changes the bias property of filter from its default value of 0 to 50.
import flash.filters.ConvolutionFilter;
import flash.display.BitmapData;
    
var bias:Number = 50;
var filter:ConvolutionFilter = new ConvolutionFilter(3, 3, [1, 1, 1, 1, 1, 1, 1, 1, 1], 9, bias);

var myBitmapData:BitmapData = new BitmapData(100, 80, false, 0x00FF0000);

var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc.attachBitmap(myBitmapData, this.getNextHighestDepth());
myBitmapData.noise(128);

mc.onPress = function() {
    myBitmapData.applyFilter(myBitmapData, myBitmapData.rectangle, new Point(0, 0), filter);
}

clampproperty 
public var clamp:Boolean

Player version: Flash Player 8

Indicates whether the image should be clamped. For pixels that are off the source image, a value of true indicates that the input image is extended along each of its borders as necessary by duplicating the color values at the given edge of the input image. A value of false indicates that another color should be used, as specified in the color and alpha properties. The default is true.


Example
The following example creates two boxes using the BitmapData class, one of which is half the size of the other. When the example first loads, the larger box is drawn inside mc using the attachBitmap(). When mc is clicked and the applyFilter() method is called, the largeBox instance of BitmapData is redrawn with smallBox as a source bitmap. Since applyFilter() draws smallBox over a Rectangle whose width and height is specified as those of largeBox, the source bitmap is smaller than the drawing area. The clamp property of ConvolutionFilter in this case is set to false and the area which is not covered by the source bitmap, smallBox, is a solid red as determined by the clampColor and clampAlpha variables.
import flash.filters.ConvolutionFilter;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;

// Variables that affect clamping:
var clamp:Boolean = false;
var clampColor:Number = 0xFF0000;
var clampAlpha:Number = 1;

// For illustration, keep other ConvolutionFilter variables neutral:
var bias:Number = 0;
var preserveAlpha:Boolean = true;
// Construct a neutral matrix
var matrixCols:Number = 3;
var matrixRows:Number = 3;
var matrix:Array = [ 1,1,1,
                        1,1,1,
                        1,1,1 ];

var filter:ConvolutionFilter = new ConvolutionFilter(matrixCols, matrixRows, matrix, matrix.length, bias, preserveAlpha, clamp, clampColor, clampAlpha);

var largeBoxWidth:Number = 100;
var largeBoxHeight:Number = 100;
var largeBox:BitmapData = new BitmapData(largeBoxWidth, largeBoxWidth, true, 0xCC00FF00);
var smallBoxWidth:Number = largeBoxWidth / 2;
var smallBoxHeight:Number = largeBoxHeight / 2;
var smallBox:BitmapData = new BitmapData(smallBoxWidth, smallBoxWidth, true, 0xCC0000FF);
    
var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc.attachBitmap(largeBox, this.getNextHighestDepth());

mc.onPress = function() {
    largeBox.applyFilter(smallBox, new Rectangle(0,0, largeBoxWidth, largeBoxHeight), new Point(0,0), filter);
}

colorproperty 
public var color:Number

Player version: Flash Player 8

The hexadecimal color to substitute for pixels that are off the source image. This is an RGB value with no alpha component. The default is 0.


Example
The following example changes the color property of filter from its default value of 0 to 0xFF0000.
import flash.filters.ConvolutionFilter;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;

var color:Number = 0x0000FF;
var filter:ConvolutionFilter = new ConvolutionFilter(3, 3, [1, 1, 1, 1, 1, 1, 1, 1, 1], 9, 0, true, false, color, 1);

var myBitmapData:BitmapData = new BitmapData(100, 80, true, 0xCCFF0000);
    
var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc.attachBitmap(myBitmapData, this.getNextHighestDepth());
myBitmapData.noise(128, 0, 255, 1 | 2 | 4 | 8, false);
        
var height:Number = 100;
var width:Number = 80;
mc.onPress = function() {
    height -= 2;
    width -= 2;
    myBitmapData.applyFilter(myBitmapData, new Rectangle(0, 0, height, width), new Point(2, 2), filter);
}

divisorproperty 
public var divisor:Number

Player version: Flash Player 8

The divisor used during matrix transformation. The default value is 1. A divisor that is the sum of all the matrix values evens out the overall color intensity of the result. A value of 0 is ignored and the default is used instead.


Example
The following example changes the divisor property of filter to 6.
import flash.filters.ConvolutionFilter;
import flash.display.BitmapData;
    
var filter:ConvolutionFilter = new ConvolutionFilter(3, 3, [1, 1, 1, 1, 1, 1, 1, 1, 1], 9);

var myBitmapData:BitmapData = new BitmapData(100, 80, false, 0x00FF0000);

var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc.attachBitmap(myBitmapData, this.getNextHighestDepth());
myBitmapData.noise(128);
        
mc.onPress = function() {
    var newDivisor:Number = 6;
    filter.divisor = newDivisor;
        myBitmapData.applyFilter(myBitmapData, myBitmapData.rectangle, new Point(0, 0), filter);
}

matrixproperty 
public var matrix:Array

Player version: Flash Player 8

An array of values used for matrix transformation; returns a copy. The number of items in the array must equal matrixX*matrixY.

The matrix property cannot be changed by directly modifying the values (for example, myFilter.matrix[2] = 1;). Instead, as shown in the following example, you must get a reference to the array, make the change to the reference, and reset the value using filter.matrix = newMatrix;.


Example
The following example changes the matrix property of filter from one that blurs a bitmap to one that sharpens it.
import flash.filters.ConvolutionFilter;
import flash.display.BitmapData;
    
var filter:ConvolutionFilter = new ConvolutionFilter(3, 3, [1, 1, 1, 1, 1, 1, 1, 1, 1], 9);

var myBitmapData:BitmapData = new BitmapData(100, 80, false, 0x00FF0000);
    
var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc.attachBitmap(myBitmapData, this.getNextHighestDepth());
myBitmapData.noise(128);
        
mc.onPress = function() {
    var newMatrix:Array = [0, -1, 0, -1, 8, -1, 0, -1, 0];
    filter.matrix = newMatrix;
    myBitmapData.applyFilter(myBitmapData, myBitmapData.rectangle, new Point(0, 0), filter);
}

matrixXproperty 
public var matrixX:Number

Player version: Flash Player 8

The x dimension of the matrix (the number of columns in the matrix). The default value is 0.


Example
The following example displays the matrixX property of filter.
import flash.filters.ConvolutionFilter;

var filter:ConvolutionFilter = new ConvolutionFilter(2, 3, [1, 0, 0, 1, 0, 0], 6);
trace(filter.matrixX);    // 2

matrixYproperty 
public var matrixY:Number

Player version: Flash Player 8

The y dimension of the matrix (the number of rows in the matrix). The default value is 0.


Example
The following example displays the matrixY property of filter.
import flash.filters.ConvolutionFilter;
    
var filter:ConvolutionFilter = new ConvolutionFilter(2, 3, [1, 0, 0, 1, 0, 0], 6);
trace(filter.matrixY);    // 3

preserveAlphaproperty 
public var preserveAlpha:Boolean

Player version: Flash Player 8

Indicates what the convolution applies to. A value of false indicates that the convolution applies to all channels, including the alpha channel. A value of true indicates that the convolution applies only to the color channels. The default value is true.


Example
The following example changes the preserveAlpha property of filter from its default value of true to false.
import flash.filters.ConvolutionFilter;
import flash.display.BitmapData;
    
var preserveAlpha:Boolean = false;
var filter:ConvolutionFilter = new ConvolutionFilter(3, 3, [1, 1, 1, 1, 1, 1, 1, 1, 1], 9, 0, preserveAlpha);

var myBitmapData:BitmapData = new BitmapData(100, 80, true, 0xCCFF0000);

var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc.attachBitmap(myBitmapData, this.getNextHighestDepth());
myBitmapData.noise(128, 0, 255, 1 | 2 | 4 | 8, false);

mc.onPress = function() {
    myBitmapData.applyFilter(myBitmapData, myBitmapData.rectangle, new Point(0, 0), filter);
}

Constructor detail
ConvolutionFilter()constructor
public function ConvolutionFilter(matrixX:Number, matrixY:Number, matrix:Array, [divisor:Number], [bias:Number], [preserveAlpha:Boolean], [clamp:Boolean], [color:Number], [alpha:Number])

Player version: Flash Player 8

Initializes a ConvolutionFilter instance with the specified parameters.

Parameters
matrixX:Number — The x dimension of the matrix (the number of columns in the matrix). The default value is 0.
 
matrixY:Number — The y dimension of the matrix (the number of rows in the matrix). The default value is 0.
 
matrix:Array — The array of values used for matrix transformation; returns a copy. The number of items in the array must equal matrixX*matrixY.
 
divisor:Number [optional] — The divisor used during matrix transformation. The default value is 1. A divisor that is the sum of all the matrix values evens out the overall color intensity of the result. A value of 0 is ignored and the default is used instead.
 
bias:Number [optional] — The bias to add to the result of the matrix transformation. The default value is 0.
 
preserveAlpha:Boolean [optional] — A value of false indicates that the convolution applies to all channels, including the alpha channel. A value of true indicates that the convolution applies only to the color channels. The default value is true.
 
clamp:Boolean [optional] — For pixels that are off the source image, a value of true indicates that the input image is extended along each of its borders as necessary by duplicating the color values at the given edge of the input image. A value of false indicates that another color should be used, as specified in the color and alpha properties. The default is true.
 
color:Number [optional] — The hexadecimal color to substitute for pixels that are off the source image.
 
alpha:Number [optional] — The alpha of the substitute color.

Example
The following code creates a 3 x 3 convolution filter with a divisor of 9. The filter would make an image appear blurred:
    var myArray:Array = [1, 1, 1, 1, 1, 1, 1, 1, 1];
    var myFilter:ConvolutionFilter = new flash.filters.ConvolutionFilter (3, 3, myArray, 9);
    

The following example creates a ConvolutionFilter object with the four required parameters matrixX, matrixY, matrix, and divisor.
import flash.filters.ConvolutionFilter;
import flash.display.BitmapData;
    
var matrixX:Number = 3;
var matrixY:Number = 3;
var matrix:Array = [1, 1, 1, 1, 1, 1, 1, 1, 1];
var divisor:Number = 9;
    
var filter:ConvolutionFilter = new ConvolutionFilter(matrixX, matrixY, matrix, divisor);

var myBitmapData:BitmapData = new BitmapData(100, 80, false, 0x00FF0000);

var mc:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());
mc.attachBitmap(myBitmapData, this.getNextHighestDepth());
myBitmapData.noise(128);
    
mc.onPress = function() {
    myBitmapData.applyFilter(myBitmapData, myBitmapData.rectangle, new Point(0, 0), filter);
}

Method detail
clone()method
public function clone():ConvolutionFilter

Player version: Flash Player 8

Returns a copy of this filter object.

Returns
ConvolutionFilter — A new ConvolutionFilter instance with all the same properties as the original one.

Example
The following example creates three ConvolutionFilter objects and compares them: filter_1 is created by using the ConvolutionFilter constructor; filter_2 is created by setting it equal to filter_1; and clonedFilter is created by cloning filter_1. Notice that although filter_2 evaluates as being equal to filter_1, clonedFilter, even though it contains the same values as filter_1, does not.
import flash.filters.ConvolutionFilter;

var filter_1:ConvolutionFilter = new ConvolutionFilter(3, 3, [1, 1, 1, 1, 1, 1, 1, 1, 1], 9);
var filter_2:ConvolutionFilter = filter_1;
var clonedFilter:ConvolutionFilter = filter_1.clone();

trace(filter_1 == filter_2);        // true
trace(filter_1 == clonedFilter);    // false

for(var i in filter_1) {
    trace(">> " + i + ": " + filter_1[i]);
    // >> clone: [type Function]
    // >> alpha: 0
    // >> color: 0
    // >> clamp: true
    // >> preserveAlpha: true
    // >> bias: 0
    // >> divisor: 9
    // >> matrix: 1,1,1,1,1,1,1,1,1
    // >> matrixY: 3
    // >> matrixX: 3
}

for(var i in clonedFilter) {
    trace(">> " + i + ": " + clonedFilter[i]);
    // >> clone: [type Function]
    // >> alpha: 0
    // >> color: 0
    // >> clamp: true
    // >> preserveAlpha: true
    // >> bias: 0
    // >> divisor: 9
    // >> matrix: 1,1,1,1,1,1,1,1,1
    // >> matrixY: 3
    // >> matrixX: 3
}        
To further demonstrate the relationships between filter_1, filter_2, and clonedFilter the following example modifies the bias property of filter_1. Modifying bias demonstrates that the clone() method creates a new instance based on values of filter_1 instead of pointing to them in reference.
import flash.filters.ConvolutionFilter;

var filter_1:ConvolutionFilter = new ConvolutionFilter(3, 3, [1, 1, 1, 1, 1, 1, 1, 1, 1], 9);
var filter_2:ConvolutionFilter = filter_1;
var clonedFilter:ConvolutionFilter = filter_1.clone();
trace(filter_1.bias);            // 0
trace(filter_2.bias);            // 0
trace(clonedFilter.bias);        // 0

filter_1.bias = 20;

trace(filter_1.bias);            // 20
trace(filter_2.bias);            // 20
trace(clonedFilter.bias);        // 0