# ConvolveMatrix

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/api/ferroui-base/media/struct-convolvematrix/

**struct** · [`ferroui-base`](/docs/0.5.0/api/ferroui-base/) · v0.5.0

```rust
struct ConvolveMatrix
```

A matrix convolution of the pixels of a layer (Win2D
`ConvolveMatrixEffect`, the SVG `feConvolveMatrix` filter primitive):
each output pixel is the sum of the pixels around it weighted by the
kernel, divided by the divisor, plus the bias.

The kernel is applied as SVG and Direct2D apply it, turned by half a
turn: with `w` and `h` the width and height of the kernel and
`(cx, cy) = (w / 2, h / 2)` (rounded down) its target element, the output
at `(x, y)` is

```text
sum over row i < h, column j < w of
    source(x - cx + j, y - cy + i) * kernel[(h - 1 - i) * w + (w - 1 - j)]
```

divided by `divisor` and plus `bias`. The
kernel steps over device pixels (Skia's matrix convolution does too).

[View source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L23)

## Members

- [`kernel_width`](#struct_field-kernel_width)
- [`kernel_height`](#struct_field-kernel_height)
- [`kernel`](#struct_field-kernel)
- [`divisor`](#struct_field-divisor)
- [`bias`](#struct_field-bias)
- [`clamp_edges`](#struct_field-clamp_edges)
- [`preserve_alpha`](#struct_field-preserve_alpha)
- [`MAX_SIDE`](#assoc_const-max_side)
- [`MAX_ELEMENTS`](#assoc_const-max_elements)
- [`new`](#function-new)
- [`element_count`](#function-element_count)
- [`elements`](#function-elements)
- [`target`](#function-target)
- [`gain`](#function-gain)
- [`weight`](#function-weight)
- [`correlation_kernel`](#function-correlation_kernel)
- [`apply_to_premultiplied_rgba8`](#function-apply_to_premultiplied_rgba8)

<a id="struct_field-kernel_width"></a>

### kernel_width

```rust
kernel_width: u32
```

The number of columns of the kernel, 1 to `MAX_SIDE`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L25)

<a id="struct_field-kernel_height"></a>

### kernel_height

```rust
kernel_height: u32
```

The number of rows of the kernel, 1 to `MAX_SIDE`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L27)

<a id="struct_field-kernel"></a>

### kernel

```rust
kernel: [f32; 25]
```

The elements of the kernel row by row; the first
`kernel_width * kernel_height` are used, the rest are 0.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L30)

<a id="struct_field-divisor"></a>

### divisor

```rust
divisor: f32
```

What the weighted sum is divided by (default 1; 0 is taken as 1).

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L32)

<a id="struct_field-bias"></a>

### bias

```rust
bias: f32
```

What is added to the result, on the scale of a channel from 0 to 1
(default 0).

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L35)

<a id="struct_field-clamp_edges"></a>

### clamp_edges

```rust
clamp_edges: bool
```

Whether the pixels beyond the edges of the layer are its edge pixels
repeated (Win2D's `Hard` border mode) rather than transparent (the
`Soft` mode, the default).

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L39)

<a id="struct_field-preserve_alpha"></a>

### preserve_alpha

```rust
preserve_alpha: bool
```

Whether the alpha is kept and the colours are convolved
un-premultiplied (Win2D's `PreserveAlpha`); otherwise the four
premultiplied channels are convolved (the default).

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L43)

<a id="assoc_const-max_side"></a>

### MAX_SIDE

```rust
const MAX_SIDE: u32
```

The largest number of rows or columns of a kernel.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L57)

<a id="assoc_const-max_elements"></a>

### MAX_ELEMENTS

```rust
const MAX_ELEMENTS: usize
```

The most elements of a kernel.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L59)

<a id="function-new"></a>

### new

```rust
fn new(kernel_width: u32, kernel_height: u32, elements: &[f32]) -> Option<ConvolveMatrix>
```

A kernel of `kernel_width` by `kernel_height` elements, `elements`
row by row, with a divisor of 1, no bias, soft edges and the alpha
convolved; `None` when a side is not 1 to `MAX_SIDE`
or `elements` has another length.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L65)

<a id="function-element_count"></a>

### element_count

```rust
fn element_count(self: &Self) -> usize
```

The number of elements of the kernel that are used.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L78)

<a id="function-elements"></a>

### elements

```rust
fn elements(self: &Self) -> &[f32]
```

The elements of the kernel that are used, row by row.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L83)

<a id="function-target"></a>

### target

```rust
fn target(self: &Self) -> (u32, u32)
```

The target element of the kernel, `(w / 2, h / 2)`: the column and
row of the kernel over the output pixel.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L89)

<a id="function-gain"></a>

### gain

```rust
fn gain(self: &Self) -> f32
```

The factor of the weighted sum: one over the divisor (one for a
divisor of 0 or not a number).

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L95)

<a id="function-weight"></a>

### weight

```rust
fn weight(self: &Self, row: usize, column: usize) -> f32
```

The weight of the source pixel `column - cx` pixels to the right and
`row - cy` pixels below the output pixel: the element turned by half
a turn (see the type).

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L106)

<a id="function-correlation_kernel"></a>

### correlation_kernel

```rust
fn correlation_kernel(self: &Self) -> Vec<f32>
```

The elements in the order of a correlation (Skia's matrix
convolution): the weight of row `i`, column `j` at `i * w + j`.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L114)

<a id="function-apply_to_premultiplied_rgba8"></a>

### apply_to_premultiplied_rgba8

```rust
fn apply_to_premultiplied_rgba8(self: &Self, pixels: &mut [u8], width: usize, height: usize)
```

Convolves premultiplied RGBA pixels of eight bits a channel (`width`
by `height`, row by row), in place: the CPU reference of the drawing
contexts. The edges of the pixels are the edges of the layer.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.Base/media/convolve_matrix.rs#L121)

## Trait implementations

- `Send`
- `Sync`
- `Freeze`
- `UnsafeUnpin`
- `Unpin`
- `UnwindSafe`
- `RefUnwindSafe`
- [`PropertyValue`](/docs/0.5.0/api/ferroui-base/trait-propertyvalue/)
- [`AnyValue`](/docs/0.5.0/api/ferroui-base/trait-anyvalue/)
- [`ClassDefaults`](/docs/0.5.0/api/ferroui-base/trait-classdefaults/)
- `Borrow<T>`
- `BorrowMut<T>`
- `CloneToUninit`
- `Into<U>`
- `From<T>`
- `TryInto<U>`
- `TryFrom<U>`
- `Any`
- `ToOwned`
- `ErasedDestructor`
- `IntoEither`
- `Clone`
- `Copy`
- `Debug`
- `StructuralPartialEq`
- `PartialEq`
- `Default`

## Availability

Extracted for `aarch64-apple-darwin`. Cargo features are listed in the [crate index](/docs/0.5.0/api/).
