pxdct 0.2.4

Discrete cosine transform
Documentation
/*
 * // Copyright (c) Radzivon Bartoshyk 2/2026. 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::transpose::Transposition;
use crate::util::{DctSample, try_vec};
use crate::{PxdctError, PxdctExecutor};
use std::sync::Arc;

pub trait MultidimensionalDctExecutor<T> {
    /// Executes a 2D DCT on source and writes the result into output.
    ///
    /// Both source and output must have length equal to width() * height().
    ///
    /// # Errors
    /// Returns a [PxdctError] if the execution fails.
    fn execute(&self, data: &mut [T]) -> Result<(), PxdctError>;
    /// Executes a 2D DCT using a pre-allocated `scratch` buffer for temporary storage.
    /// This can reduce allocations and improve performance for repeated calls.
    /// Both `source`, `output`, and `scratch` must have sufficient length.
    ///
    /// # Errors
    /// Returns a [`PxdctError`] if the execution fails.
    fn execute_with_scratch(&self, data: &mut [T], scratch: &mut [T]) -> Result<(), PxdctError>;
    /// Returns the **width** (number of columns, X-dimension) of the 2D input data grid.
    fn width(&self) -> usize;
    /// Returns the **height** (number of rows, Y-dimension) of the 2D input data grid.
    fn height(&self) -> usize;
    /// Required scratch size
    fn required_scratch_size(&self) -> usize;
}

pub(crate) struct TwoDimensionalDct<T> {
    pub(crate) width_executor: Arc<dyn PxdctExecutor<T> + Send + Sync>,
    pub(crate) height_executor: Arc<dyn PxdctExecutor<T> + Send + Sync>,
    pub(crate) transpose_width_to_height: Arc<dyn Transposition<T> + Send + Sync>,
    pub(crate) width: usize,
    pub(crate) height: usize,
}

impl<T: DctSample> MultidimensionalDctExecutor<T> for TwoDimensionalDct<T> {
    fn execute(&self, data: &mut [T]) -> Result<(), PxdctError> {
        let mut scratch = try_vec![T::default(); self.required_scratch_size()];
        self.execute_with_scratch(data, &mut scratch)
    }

    fn execute_with_scratch(&self, data: &mut [T], scratch: &mut [T]) -> Result<(), PxdctError> {
        if scratch.len() < self.required_scratch_size() {
            return Err(PxdctError::ScratchBufferIsTooSmall(
                scratch.len(),
                self.required_scratch_size(),
            ));
        }
        let full_size = self.width * self.height;
        if !data.len().is_multiple_of(full_size) {
            return Err(PxdctError::InvalidSizeMultiplier(data.len(), full_size));
        }
        let (scratch, _) = scratch.split_at_mut(self.required_scratch_size());

        for chunk in data.chunks_exact_mut(full_size) {
            scratch.copy_from_slice(chunk);

            scratch.chunks_exact_mut(self.width).for_each(|row| {
                _ = self.width_executor.execute(row);
            });

            self.transpose_width_to_height.transpose(scratch, chunk);

            chunk.chunks_exact_mut(self.height).for_each(|column| {
                _ = self.height_executor.execute(column);
            });
        }
        Ok(())
    }

    #[inline]
    fn width(&self) -> usize {
        self.width
    }

    #[inline]
    fn height(&self) -> usize {
        self.height
    }

    #[inline]
    fn required_scratch_size(&self) -> usize {
        self.width * self.height
    }
}