libblur 0.24.0

Fast image blurring in pure Rust
Documentation
/*
 * // Copyright (c) Radzivon Bartoshyk. All rights reserved.
 * //
 * // Redistribution and use in source and binary forms, with or without modification,
 * // are permitted provided that the following conditions are met:
 * //
 * // 1.  Redistributions of source code must retain the above copyright notice, this
 * // list of conditions and the following disclaimer.
 * //
 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
 * // this list of conditions and the following disclaimer in the documentation
 * // and/or other materials provided with the distribution.
 * //
 * // 3.  Neither the name of the copyright holder nor the names of its
 * // contributors may be used to endorse or promote products derived from
 * // this software without specific prior written permission.
 * //
 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
use crate::filter1d::arena::Arena;
use crate::img_size::ImageSize;
use crate::mlaf::mlaf;
use crate::primitives::PrimitiveCast;
use crate::to_storage::ToStorage;
use num_traits::MulAdd;
use std::ops::{Add, Mul};

pub(crate) fn filter_row<T, F, const N: usize>(
    _: Arena,
    arena_src: &[T],
    dst: &mut [T],
    image_size: ImageSize,
    scanned_kernel: &[F],
) where
    T: Copy + PrimitiveCast<F>,
    F: ToStorage<T> + Mul<Output = F> + MulAdd<F, Output = F> + Add<F, Output = F>,
{
    unsafe {
        let width = image_size.width;

        let src = arena_src;

        let local_src = src;

        let length = scanned_kernel.len();

        let mut cx = 0usize;

        let max_width = width * N;

        while cx + 4 <= max_width {
            let coeff = *scanned_kernel.get_unchecked(0);

            let shifted_src = local_src.get_unchecked(cx..);

            let mut k0 = (*shifted_src.get_unchecked(0)).cast_().mul(coeff);
            let mut k1 = (*shifted_src.get_unchecked(1)).cast_().mul(coeff);
            let mut k2 = (*shifted_src.get_unchecked(2)).cast_().mul(coeff);
            let mut k3 = (*shifted_src.get_unchecked(3)).cast_().mul(coeff);

            for i in 1..length {
                let coeff = *scanned_kernel.get_unchecked(i);
                k0 = mlaf(k0, (*shifted_src.get_unchecked(i * N)).cast_(), coeff);
                k1 = mlaf(k1, (*shifted_src.get_unchecked(i * N + 1)).cast_(), coeff);
                k2 = mlaf(k2, (*shifted_src.get_unchecked(i * N + 2)).cast_(), coeff);
                k3 = mlaf(k3, (*shifted_src.get_unchecked(i * N + 3)).cast_(), coeff);
            }

            *dst.get_unchecked_mut(cx) = k0.to_();
            *dst.get_unchecked_mut(cx + 1) = k1.to_();
            *dst.get_unchecked_mut(cx + 2) = k2.to_();
            *dst.get_unchecked_mut(cx + 3) = k3.to_();
            cx += 4;
        }

        for x in cx..max_width {
            let coeff = *scanned_kernel.get_unchecked(0);
            let shifted_src = local_src.get_unchecked(x..);
            let mut k0 = (*shifted_src.get_unchecked(0)).cast_().mul(coeff);

            for i in 1..length {
                let coeff = *scanned_kernel.get_unchecked(i);
                k0 = mlaf(k0, (*shifted_src.get_unchecked(i * N)).cast_(), coeff);
            }
            *dst.get_unchecked_mut(x) = k0.to_();
        }
    }
}