pic_scale/scaler_f16.rs
1/*
2 * Copyright (c) Radzivon Bartoshyk. 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 */
29#![forbid(unsafe_code)]
30use crate::image_store::ImageStoreMut;
31use crate::pic_scale_error::PicScaleError;
32use crate::scaler::ScalingOptions;
33use crate::{
34 CbCrF16ImageStore, ImageStore, ImageStoreScaling, PlanarF16ImageStore, RgbF16ImageStore,
35 RgbaF16ImageStore, Scaler, Scaling,
36};
37use core::f16;
38
39/// Implements `f16` type support
40#[cfg_attr(docsrs, doc(cfg(feature = "nightly_f16")))]
41impl Scaler {
42 /// Performs rescaling for RGBA f16
43 ///
44 /// Scales RGBA high bit-depth interleaved image in `f16` type.
45 /// Channel order does not matter.
46 /// To handle alpha pre-multiplication alpha channel expected to be at last position.
47 ///
48 /// # Arguments
49 /// `store` - original image store
50 /// `into` - target image store
51 /// `premultiply_alpha` - flag if it should handle alpha or not
52 ///
53 /// # Example
54 ///
55 /// #[no_build]
56 /// ```rust
57 /// use pic_scale::{ImageStore, ImageStoreMut, ResamplingFunction, Scaler};
58 /// let mut scaler = Scaler::new(ResamplingFunction::Bilinear);
59 /// let src_store = ImageStore::alloc(100, 100);
60 /// let mut dst_store = ImageStoreMut::<f16, 4>::alloc_with_depth(50, 50, 10);
61 /// scaler.resize_rgba_f16(&src_store, &mut dst_store, false).unwrap();
62 /// ```
63 pub fn resize_rgba_f16<'a>(
64 &'a self,
65 store: &ImageStore<'a, f16, 4>,
66 into: &mut ImageStoreMut<'a, f16, 4>,
67 premultiply_alpha: bool,
68 ) -> Result<(), PicScaleError> {
69 self.generic_resize_with_alpha(store, into, premultiply_alpha)
70 }
71
72 /// Performs rescaling for RGB f16
73 ///
74 /// Scales RGB high bit-depth interleaved image in `f16` type.
75 /// Channel order does not matter.
76 ///
77 /// # Arguments
78 /// `store` - original image store
79 /// `into` - target image store
80 ///
81 /// # Example
82 ///
83 /// #[no_build]
84 /// ```rust
85 /// use pic_scale::{ImageStore, ImageStoreMut, ResamplingFunction, Scaler};
86 /// let mut scaler = Scaler::new(ResamplingFunction::Bilinear);
87 /// let src_store = ImageStore::alloc(100, 100);
88 /// let mut dst_store = ImageStoreMut::<f16, 3>::alloc_with_depth(50, 50, 10);
89 /// scaler.resize_rgb_f16(&src_store, &mut dst_store).unwrap();
90 /// ```
91 pub fn resize_rgb_f16<'a>(
92 &'a self,
93 store: &ImageStore<'a, f16, 3>,
94 into: &mut ImageStoreMut<'a, f16, 3>,
95 ) -> Result<(), PicScaleError> {
96 self.generic_resize(store, into)
97 }
98
99 /// Performs rescaling for CbCr f16
100 ///
101 /// Scales CbCr high bit-depth interleaved image in `f16` type, optionally it could handle LumaAlpha images also
102 /// Channel order does not matter.
103 ///
104 /// # Arguments
105 /// `store` - original image store
106 /// `into` - target image store
107 ///
108 /// # Example
109 ///
110 /// #[no_build]
111 /// ```rust
112 /// use pic_scale::{ImageStore, ImageStoreMut, ResamplingFunction, Scaler};
113 /// let mut scaler = Scaler::new(ResamplingFunction::Bilinear);
114 /// let src_store = ImageStore::alloc(100, 100);
115 /// let mut dst_store = ImageStoreMut::<f16, 2>::alloc_with_depth(50, 50, 10);
116 /// scaler.resize_cbcr_f16(&src_store, &mut dst_store).unwrap();
117 /// ```
118 pub fn resize_cbcr_f16<'a>(
119 &'a self,
120 store: &ImageStore<'a, f16, 2>,
121 into: &mut ImageStoreMut<'a, f16, 2>,
122 ) -> Result<(), PicScaleError> {
123 self.generic_resize(store, into)
124 }
125
126 /// Performs rescaling for Planar image f16
127 ///
128 /// Scales planar high bit-depth image in `f16` type, optionally it could handle LumaAlpha images also
129 /// Channel order does not matter.
130 ///
131 /// # Arguments
132 /// `store` - original image store
133 /// `into` - target image store
134 ///
135 /// # Example
136 ///
137 /// #[no_build]
138 /// ```rust
139 /// use pic_scale::{ImageStore, ImageStoreMut, ResamplingFunction, Scaler};
140 /// let mut scaler = Scaler::new(ResamplingFunction::Bilinear);
141 /// let src_store = ImageStore::alloc(100, 100);
142 /// let mut dst_store = ImageStoreMut::<f16, 1>::alloc_with_depth(50, 50, 10);
143 /// scaler.resize_plane_f16(&src_store, &mut dst_store).unwrap();
144 /// ```
145 ///
146 pub fn resize_plane_f16<'a>(
147 &'a self,
148 store: &ImageStore<'a, f16, 1>,
149 into: &mut ImageStoreMut<'a, f16, 1>,
150 ) -> Result<(), PicScaleError> {
151 self.generic_resize(store, into)
152 }
153}
154
155#[cfg_attr(docsrs, doc(cfg(feature = "nightly_f16")))]
156impl<'b> ImageStoreScaling<'b, f16, 1> for PlanarF16ImageStore<'b> {
157 fn scale(
158 &self,
159 store: &mut ImageStoreMut<'b, f16, 1>,
160 options: ScalingOptions,
161 ) -> Result<(), PicScaleError> {
162 let mut scaler = Scaler::new(options.resampling_function);
163 scaler.set_threading_policy(options.threading_policy);
164 scaler.generic_resize(self, store)
165 }
166}
167
168#[cfg_attr(docsrs, doc(cfg(feature = "nightly_f16")))]
169impl<'b> ImageStoreScaling<'b, f16, 2> for CbCrF16ImageStore<'b> {
170 fn scale(
171 &self,
172 store: &mut ImageStoreMut<'b, f16, 2>,
173 options: ScalingOptions,
174 ) -> Result<(), PicScaleError> {
175 let mut scaler = Scaler::new(options.resampling_function);
176 scaler.set_threading_policy(options.threading_policy);
177 scaler.generic_resize(self, store)
178 }
179}
180
181#[cfg_attr(docsrs, doc(cfg(feature = "nightly_f16")))]
182impl<'b> ImageStoreScaling<'b, f16, 3> for RgbF16ImageStore<'b> {
183 fn scale(
184 &self,
185 store: &mut ImageStoreMut<'b, f16, 3>,
186 options: ScalingOptions,
187 ) -> Result<(), PicScaleError> {
188 let mut scaler = Scaler::new(options.resampling_function);
189 scaler.set_threading_policy(options.threading_policy);
190 scaler.generic_resize(self, store)
191 }
192}
193
194#[cfg_attr(docsrs, doc(cfg(feature = "nightly_f16")))]
195impl<'b> ImageStoreScaling<'b, f16, 4> for RgbaF16ImageStore<'b> {
196 fn scale(
197 &self,
198 store: &mut ImageStoreMut<'b, f16, 4>,
199 options: ScalingOptions,
200 ) -> Result<(), PicScaleError> {
201 let mut scaler = Scaler::new(options.resampling_function);
202 scaler.set_threading_policy(options.threading_policy);
203 scaler.generic_resize_with_alpha(self, store, options.premultiply_alpha)
204 }
205}