class ConvolutionFilter
package openfl.filters
extends BitmapFilter
Available on all platforms
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 image effects can be achieved through
convolutions, including blurring, edge detection, sharpening, embossing,
and beveling. You can apply the filter to any display object (that is,
objects that inherit from the DisplayObject class), such as MovieClip,
SimpleButton, TextField, and Video objects, as well as to BitmapData
objects.
To create a convolution filter, use the syntax new ConvolutionFilter()
.
The use of filters depends on the object to which you apply the filter:
- To apply filters to movie clips, text fields, buttons, and video, use
the
filters
property (inherited from DisplayObject). Setting thefilters
property of an object does not modify the object, and you can remove the filter by clearing thefilters
property. - To apply filters to BitmapData objects, use the
BitmapData.applyFilter()
method. CallingapplyFilter()
on a BitmapData object takes the source BitmapData object and the filter object and generates a filtered image as a result.
If you apply a filter to a display object, the value of the
cacheAsBitmap
property of the object is set to true
. If you clear all
filters, the original value of cacheAsBitmap
is restored.
A filter is not applied if the resulting image exceeds the maximum dimensions. In AIR 1.5 and Flash Player 10, the maximum is 8,191 pixels in width or height, and the total number of pixels cannot exceed 16,777,215 pixels. (So, if an image is 8,191 pixels wide, it can only be 2,048 pixels high.) In Flash Player 9 and earlier and AIR 1.1 and earlier, the limitation is 2,880 pixels in height and 2,880 pixels in width. For example, if you zoom in on a large movie clip with a filter applied, the filter is turned off if the resulting image exceeds maximum dimensions.
Constructor
new(matrixX:Int = 0, matrixY:Int = 0, ?matrix:Array<Float>, divisor:Float = 1.0, bias:Float = 0.0, preserveAlpha:Bool = true, clamp:Bool = true, color:Int = 0, alpha:Float = 0.0)
Initializes a ConvolutionFilter instance with the specified parameters.
Parameters:
matrixX | The x dimension of the matrix (the number of columns in the matrix). The default value is 0. |
---|---|
matrixY | The y dimension of the matrix (the number of rows in the matrix). The default value is 0. |
divisor | 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 | The bias to add to the result of the matrix transformation. The default value is 0. |
preserveAlpha | A value of |
clamp | For pixels that are off the source image, a value
of |
color | The hexadecimal color to substitute for pixels that are off the source image. |
alpha | The alpha of the substitute color. |
Variables
alpha:Float
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%.
bias:Float
The amount of bias to add to the result of the matrix transformation. The bias increases the color value of each channel, so that dark colors appear brighter. The default value is 0.
clamp:Bool
Indicates whether the image should be clamped. For pixels 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 each respective 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:Int
The hexadecimal color to substitute for pixels that are off the source image. It is an RGB value with no alpha component. The default is 0.
divisor:Float
The divisor used during matrix transformation. The default value is 1. A divisor that is the sum of all the matrix values smooths out the overall color intensity of the result. A value of 0 is ignored and the default is used instead.
matrix:Array<Float>
An array of values used for matrix transformation. The number of items
in the array must equal matrixX * matrixY
.
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
Certain filter specifications perform faster when run by a processor that offers SSE (Streaming SIMD Extensions). The following are criteria for faster convolution operations:
- The filter must be a 3x3 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.
Note: If you create a ConvolutionFilter instance using the
constructor without parameters, the order you assign values to matrix
properties affects the behavior of the filter. In the following case,
the matrix array is assigned while the matrixX
and matrixY
properties are still set to 0
(the default value):
public var myfilter = new ConvolutionFilter();
myfilter.matrix = [0, 0, 0, 0, 1, 0, 0, 0, 0];
myfilter.matrixX = 3;
myfilter.matrixY = 3;
In the following case, the matrix array is assigned while the
matrixX
and matrixY
properties are set to 3
:
public var myfilter = new ConvolutionFilter();
myfilter.matrixX = 3;
myfilter.matrixY = 3;
myfilter.matrix = [0, 0, 0, 0, 1, 0, 0, 0, 0];
Throws:
TypeError | The Array is null when being set |
---|
matrixX:Int
The x dimension of the matrix (the number of columns in the matrix). The default value is 0.
matrixY:Int
The y dimension of the matrix (the number of rows in the matrix). The default value is 0.
preserveAlpha:Bool
Indicates if the alpha channel is preserved without the filter effect
or if the convolution filter is applied to the alpha channel as well
as the color channels. 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
.