ConvolutionFilter (flash.filters.ConvolutionFilter)


Object   |   +-flash.filters.BitmapFilter     |     +-flash.filters.ConvolutionFilter public class ConvolutionFilter extends BitmapFilter

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:

  • To apply filters to movie clips at runtime, use the filters property. Setting the filters property of an object does not modify the object and can be undone by clearing the filters property.

  • To apply filters to BitmapData instances, use the BitmapData.applyFilter() method. Calling applyFilter() on a BitmapData object takes the source BitmapData object and the filter object and generates a filtered image as a result.

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.

  • The filter must be a 3 x 3 filter.

  • All the filter terms must be integers between -127 and +127.

  • The sum of all the filter terms must not have an absolute value greater than 127.

  • If any filter term is negative, the divisor must be between 2.00001 and 256.

  • If all filter terms are positive, the divisor must be between 1.1 and 256.

  • The bias must be an integer.

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.

Availability: ActionScript 1.0; Flash Player 8

See also

applyFilter (BitmapData.applyFilter method), filters (MovieClip.filters property), cacheAsBitmap (MovieClip.cacheAsBitmap property)

Property summary

Modifiers

Property

Description

 

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

constructor (Object.constructor property), __proto__ (Object.__proto__ property), prototype (Object.prototype property), __resolve (Object.__resolve property)


Constructor summary

Signature

Description

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.


Method summary

Modifiers

Signature

Description

 

clone() : ConvolutionFilter

Returns a copy of this filter object.


Methods inherited from class BitmapFilter

clone (BitmapFilter.clone method)


Methods inherited from class Object

addProperty (Object.addProperty method), hasOwnProperty (Object.hasOwnProperty method), isPropertyEnumerable (Object.isPropertyEnumerable method), isPrototypeOf (Object.isPrototypeOf method), registerClass (Object.registerClass method), toString (Object.toString method), unwatch (Object.unwatch method), valueOf (Object.valueOf method), watch (Object.watch method)


alpha (ConvolutionFilter.alpha property)

public alpha : Number

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

Availability: ActionScript 1.0; Flash Player 8

Example

The following example changes the alpha property of filter from its default value of 1 to .35.

import flash.filters.ConvolutionFilter; import flash.display.BitmapData; import flash.geom.Rectangle; import flash.geom.Point; var alpha:Number = .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); }

bias (ConvolutionFilter.bias property)

public bias : Number

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

Availability: ActionScript 1.0; Flash Player 8

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); }

clamp (ConvolutionFilter.clamp property)

public clamp : Boolean

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.

Availability: ActionScript 1.0; Flash Player 8

Example

The following example changes the clamp property of filter from its default value of TRue to false.

import flash.filters.ConvolutionFilter; import flash.display.BitmapData; import flash.geom.Rectangle; import flash.geom.Point; var clamp:Boolean = false; var filter:ConvolutionFilter = new ConvolutionFilter(3, 3, [1, 1, 1, 1, 1,   1, 1, 1, 1], 9, 0, true, clamp, 0x00FF00, 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); mc.onPress = function() {   myBitmapData.applyFilter(myBitmapData, new Rectangle(0, 0, 98, 78), new   Point(2, -2), filter); }

clone (ConvolutionFilter.clone method)

public clone() : ConvolutionFilter

Returns a copy of this filter object.

Availability: ActionScript 1.0; Flash Player 8

Returns

flash.filters.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

color (ConvolutionFilter.color property)

public color : Number

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.

Availability: ActionScript 1.0; Flash Player 8

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); }

ConvolutionFilter constructor

public 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.

Availability: ActionScript 1.0; Flash Player 8

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); }

divisor (ConvolutionFilter.divisor property)

public divisor : Number

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.

Availability: ActionScript 1.0; Flash Player 8

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); }

matrix (ConvolutionFilter.matrix property)

public matrix : Array

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;.

Availability: ActionScript 1.0; Flash Player 8

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); }

matrixX (ConvolutionFilter.matrixX property)

public matrixX : Number

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

Availability: ActionScript 1.0; Flash Player 8

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

matrixY (ConvolutionFilter.matrixY property)

public matrixY : Number

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

Availability: ActionScript 1.0; Flash Player 8

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

preserveAlpha (ConvolutionFilter.preserveAlpha property)

public preserveAlpha : Boolean

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.

Availability: ActionScript 1.0; Flash Player 8

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); }



ActionScript 2.0 Language Reference for Macromedia Flash 8
ActionScript 2.0 Language Reference for Macromedia Flash 8
ISBN: 0321384040
EAN: 2147483647
Year: 2004
Pages: 113

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net