Skip to main content

pic_scale/plan/
planner.rs

1/*
2 * Copyright (c) Radzivon Bartoshyk 3/2026. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1.  Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2.  Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * 3.  Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29use crate::{ImageSize, ImageStore, ImageStoreMut, PicScaleError};
30
31/// A precomputed resampling plan for scaling images of pixel type `T` with `N` channels.
32///
33/// A plan is created once via methods like [`plan_rgba_resampling`] or [`plan_rgb_resampling`]
34/// on a scaler, and can then be executed repeatedly against different image buffers of the
35/// same dimensions without recomputing filter weights.
36pub trait ResamplingPlan<T: Copy, const N: usize> {
37    /// Resamples `store` into `into`, allocating any necessary scratch memory internally.
38    ///
39    /// This is the simplest way to execute a plan. If you are resampling many images in a
40    /// tight loop and want to avoid repeated allocations, prefer [`resample_with_scratch`]
41    /// with a buffer obtained from [`alloc_scratch`].
42    fn resample(
43        &self,
44        store: &ImageStore<'_, T, N>,
45        into: &mut ImageStoreMut<'_, T, N>,
46    ) -> Result<(), PicScaleError>;
47    /// Resamples `store` into `into` using the caller-supplied `scratch` buffer.
48    ///
49    /// Avoids internal allocation on every call, which is useful when resampling many
50    /// images of the same size. The scratch buffer must be at least [`scratch_size`] elements
51    /// long; obtain a correctly sized buffer with [`alloc_scratch`].
52    /// # Example
53    ///
54    /// ```rust,no_run,ignore
55    /// let plan = scaler.plan_rgb_resampling(source_size, target_size)?;
56    /// let mut scratch = plan.alloc_scratch();
57    ///
58    /// for frame in frames {
59    ///     plan.resample_with_scratch(&frame, &mut output, &mut scratch)?;
60    /// }
61    fn resample_with_scratch(
62        &self,
63        store: &ImageStore<'_, T, N>,
64        into: &mut ImageStoreMut<'_, T, N>,
65        scratch: &mut [T],
66    ) -> Result<(), PicScaleError>;
67    /// Allocates a scratch buffer of the correct size for use with [`resample_with_scratch`].
68    ///
69    /// The returned `Vec` is zero initialized and exactly [`scratch_size`] elements long.
70    /// Reuse it across calls to avoid repeated allocation.
71    fn alloc_scratch(&self) -> Vec<T>;
72    /// Returns the number of `T` elements required in the scratch buffer.
73    ///
74    /// Pass a slice of at least this length to [`resample_with_scratch`].
75    fn scratch_size(&self) -> usize;
76    /// Returns the target (output) image dimensions this plan was built for.
77    fn target_size(&self) -> ImageSize;
78    /// Returns the source (input) image dimensions this plan was built for.
79    fn source_size(&self) -> ImageSize;
80}
81
82/// Type alias for a thread-safe, dynamically dispatched [`ResamplingPlan`].
83///
84/// Returned by scaler planning methods (e.g. `plan_rgba_resampling`) and intended
85/// to be stored as `Arc<Resampling<T, N>>` so the plan can be shared across threads
86/// or held for the lifetime of a processing pipeline.
87///
88/// # Example
89///
90/// ```rust,no_run,ignore
91/// let plan: Arc<Resampling<u8, 4>> =
92///     scaler.plan_rgba_resampling(source_size, target_size, true)?;
93/// ```
94pub type Resampling<T, const N: usize> = dyn ResamplingPlan<T, N> + Send + Sync;