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
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, ThreadingPolicy,
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(if options.use_multithreading {
164 ThreadingPolicy::Adaptive
165 } else {
166 ThreadingPolicy::Single
167 });
168 scaler.generic_resize(self, store)
169 }
170}
171
172#[cfg_attr(docsrs, doc(cfg(feature = "nightly_f16")))]
173impl<'b> ImageStoreScaling<'b, f16, 2> for CbCrF16ImageStore<'b> {
174 fn scale(
175 &self,
176 store: &mut ImageStoreMut<'b, f16, 2>,
177 options: ScalingOptions,
178 ) -> Result<(), PicScaleError> {
179 let mut scaler = Scaler::new(options.resampling_function);
180 scaler.set_threading_policy(if options.use_multithreading {
181 ThreadingPolicy::Adaptive
182 } else {
183 ThreadingPolicy::Single
184 });
185 scaler.generic_resize(self, store)
186 }
187}
188
189#[cfg_attr(docsrs, doc(cfg(feature = "nightly_f16")))]
190impl<'b> ImageStoreScaling<'b, f16, 3> for RgbF16ImageStore<'b> {
191 fn scale(
192 &self,
193 store: &mut ImageStoreMut<'b, f16, 3>,
194 options: ScalingOptions,
195 ) -> Result<(), PicScaleError> {
196 let mut scaler = Scaler::new(options.resampling_function);
197 scaler.set_threading_policy(if options.use_multithreading {
198 ThreadingPolicy::Adaptive
199 } else {
200 ThreadingPolicy::Single
201 });
202 scaler.generic_resize(self, store)
203 }
204}
205
206#[cfg_attr(docsrs, doc(cfg(feature = "nightly_f16")))]
207impl<'b> ImageStoreScaling<'b, f16, 4> for RgbaF16ImageStore<'b> {
208 fn scale(
209 &self,
210 store: &mut ImageStoreMut<'b, f16, 4>,
211 options: ScalingOptions,
212 ) -> Result<(), PicScaleError> {
213 let mut scaler = Scaler::new(options.resampling_function);
214 scaler.set_threading_policy(if options.use_multithreading {
215 ThreadingPolicy::Adaptive
216 } else {
217 ThreadingPolicy::Single
218 });
219 scaler.generic_resize_with_alpha(self, store, options.premultiply_alpha)
220 }
221}