ConvolveMatrixPrimitive Class

A primitive that computes each pixel from the pixels around it with a matrix of weights: feConvolveMatrix of SVG.

public sealed class ConvolveMatrixPrimitive : FilterPrimitive

Remarks

With a matrix of orderX columns and orderY rows and a target (tx, ty), a pixel of the result is sum(I(x - tx + j, y - ty + i) K(orderX - j - 1, orderY - i - 1)) / divisor + bias: the matrix is turned by a half turn, as in SVG. The weights are one pixel of the canvas apart, whatever the transformation. What is read beyond the subregion of the primitive is given by ConvolveMatrixPrimitive.EdgeMode (the nearest pixel by default, as in SVG); inside it, an input is transparent outside its own subregion, so that a bias colors the pixels around a smaller input, as browsers do.

The bias is added to every component. Without ConvolveMatrixPrimitive.PreserveAlpha, the four components, the colors multiplied by their alpha, are computed, the alpha is clamped to the range from 0 to 1 and each color to the range from 0 to the alpha. With it, the straight colors are computed and clamped to the range from 0 to 1, and the pixel keeps its alpha.

Inheritance
FilterPrimitive ConvolveMatrixPrimitive

Constructors

NameDescription
ConvolveMatrixPrimitive(int, int, ReadOnlySpan<double>)Initializes a new instance of the ConvolveMatrixPrimitive class.

ConvolveMatrixPrimitive(int, int, ReadOnlySpan<double>)

ConvolveMatrixPrimitive.cs

Initializes a new instance of the ConvolveMatrixPrimitive class.

public ConvolveMatrixPrimitive(int orderX, int orderY, ReadOnlySpan<double> kernel)

Parameters

orderX int
The number of columns of the matrix: order. Must be between 1 and 255.
orderY int
The number of rows of the matrix. Must be between 1 and 255.
kernel ReadOnlySpan<double>
The weights, row by row: kernelMatrix. They are copied.

Exceptions

ArgumentOutOfRangeException
An order is not between 1 and 255.
ArgumentException
The number of weights is not orderX orderY, or a weight is not a finite number.

Properties

NameDescription
BiasGets what is added to every component of the result: bias.
DivisorGets what the sum is divided by: divisor. Defaults to null: the sum of the weights, or 1 when it is zero.
EdgeModeGets what is read beyond the subregion of the primitive: edgeMode. Defaults to FilterEdgeMode.Duplicate.
InputGets the input: in.
KernelGets the weights, row by row.
OrderXGets the number of columns of the matrix.
OrderYGets the number of rows of the matrix.
PreserveAlphaGets a value indicating whether the colors are computed without their alpha, which each pixel keeps: preserveAlpha.
TargetXGets the column of the matrix that is on the pixel: targetX. Defaults to floor(orderX / 2).
TargetYGets the row of the matrix that is on the pixel: targetY. Defaults to floor(orderY / 2).

Gets what is added to every component of the result: bias.

public double Bias { get; init; }

Property value

double

Exceptions

ArgumentException
The value is not a finite number.

Gets what the sum is divided by: divisor. Defaults to null: the sum of the weights, or 1 when it is zero.

public double? Divisor { get; init; }

Property value

double?

Exceptions

ArgumentException
The value is zero or not a finite number.

Gets what is read beyond the subregion of the primitive: edgeMode. Defaults to FilterEdgeMode.Duplicate.

public FilterEdgeMode EdgeMode { get; init; }

Property value

FilterEdgeMode

Exceptions

ArgumentOutOfRangeException
The value is not a defined FilterEdgeMode.

Gets the input: in.

public FilterInput Input { get; init; }

Property value

FilterInput

Gets the weights, row by row.

public IReadOnlyList<double> Kernel { get; }

Property value

IReadOnlyList<double>

Gets the number of columns of the matrix.

public int OrderX { get; }

Property value

int

Gets the number of rows of the matrix.

public int OrderY { get; }

Property value

int

Gets a value indicating whether the colors are computed without their alpha, which each pixel keeps: preserveAlpha.

public bool PreserveAlpha { get; init; }

Property value

bool

Gets the column of the matrix that is on the pixel: targetX. Defaults to floor(orderX / 2).

public int TargetX { get; init; }

Property value

int

Exceptions

ArgumentOutOfRangeException
The value is not a column of the matrix.

Gets the row of the matrix that is on the pixel: targetY. Defaults to floor(orderY / 2).

public int TargetY { get; init; }

Property value

int

Exceptions

ArgumentOutOfRangeException
The value is not a row of the matrix.