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_column<T, F>(
    arena: Arena,
    arena_src: &[&[T]],
    dst: &mut [T],
    image_size: ImageSize,
    scanned_kernel: &[F],
) where
    T: Copy + PrimitiveCast<F>,
    F: ToStorage<T> + Mul<F, Output = F> + MulAdd<F, Output = F> + Add<F, Output = F>,
{
    unsafe {
        let full_width = image_size.width * arena.components;

        let length = scanned_kernel.len();

        let mut cx = 0usize;

        let coeff = *scanned_kernel.get_unchecked(0);

        while cx + 4 <= full_width {
            let v_src = arena_src.get_unchecked(0).get_unchecked(cx..);

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

            for i in 1..length {
                let coeff = *scanned_kernel.get_unchecked(i);
                k0 = mlaf(
                    k0,
                    arena_src.get_unchecked(i).get_unchecked(cx).cast_(),
                    coeff,
                );
                k1 = mlaf(
                    k1,
                    arena_src.get_unchecked(i).get_unchecked(cx + 1).cast_(),
                    coeff,
                );
                k2 = mlaf(
                    k2,
                    arena_src.get_unchecked(i).get_unchecked(cx + 2).cast_(),
                    coeff,
                );
                k3 = mlaf(
                    k3,
                    arena_src.get_unchecked(i).get_unchecked(cx + 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..full_width {
            let v_src = arena_src.get_unchecked(0).get_unchecked(x..);

            let mut k0 = v_src.get_unchecked(0).cast_().mul(coeff);

            for i in 1..length {
                let coeff = *scanned_kernel.get_unchecked(i);
                k0 = mlaf(
                    k0,
                    arena_src.get_unchecked(i).get_unchecked(x).cast_(),
                    coeff,
                );
            }

            *dst.get_unchecked_mut(x) = k0.to_();
        }
    }
}