/rust/registry/src/index.crates.io-1949cf8c6b5b557f/moxcms-0.8.1/src/conversions/reduction.rs
Line | Count | Source |
1 | | /* |
2 | | * // Copyright (c) Radzivon Bartoshyk 12/2025. 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 | | |
30 | | pub(crate) trait LutBarycentricReduction<T, U> { |
31 | | #![allow(unused)] |
32 | | fn reduce<const SRC_BP: usize, const BINS: usize>(v: T) -> U; |
33 | | } |
34 | | |
35 | | impl LutBarycentricReduction<u8, u8> for () { |
36 | | #[inline(always)] |
37 | 0 | fn reduce<const SRC_BP: usize, const BINS: usize>(v: u8) -> u8 { |
38 | 0 | v |
39 | 0 | } |
40 | | } |
41 | | |
42 | | impl LutBarycentricReduction<u8, u16> for () { |
43 | | #[inline(always)] |
44 | 0 | fn reduce<const SRC_BP: usize, const BINS: usize>(v: u8) -> u16 { |
45 | 0 | if BINS == 65536 { |
46 | 0 | return u16::from_ne_bytes([v, v]); |
47 | 0 | } |
48 | 0 | if BINS == 16384 { |
49 | 0 | return u16::from_ne_bytes([v, v]) >> 2; |
50 | 0 | } |
51 | 0 | unimplemented!() |
52 | 0 | } |
53 | | } |
54 | | |
55 | | impl LutBarycentricReduction<f32, u8> for () { |
56 | | #[inline(always)] |
57 | 0 | fn reduce<const SRC_BP: usize, const BINS: usize>(v: f32) -> u8 { |
58 | 0 | (v * 255.).round().min(255.).max(0.) as u8 |
59 | 0 | } |
60 | | } |
61 | | |
62 | | impl LutBarycentricReduction<f32, u16> for () { |
63 | | #[inline(always)] |
64 | 0 | fn reduce<const SRC_BP: usize, const BINS: usize>(v: f32) -> u16 { |
65 | 0 | let scale = (BINS - 1) as f32; |
66 | 0 | (v * scale).round().min(scale).max(0.) as u16 |
67 | 0 | } |
68 | | } |
69 | | |
70 | | impl LutBarycentricReduction<f64, u8> for () { |
71 | | #[inline(always)] |
72 | 0 | fn reduce<const SRC_BP: usize, const BINS: usize>(v: f64) -> u8 { |
73 | 0 | (v * 255.).round().min(255.).max(0.) as u8 |
74 | 0 | } |
75 | | } |
76 | | |
77 | | impl LutBarycentricReduction<f64, u16> for () { |
78 | | #[inline(always)] |
79 | 0 | fn reduce<const SRC_BP: usize, const BINS: usize>(v: f64) -> u16 { |
80 | 0 | let scale = (BINS - 1) as f64; |
81 | 0 | (v * scale).round().min(scale).max(0.) as u16 |
82 | 0 | } |
83 | | } |
84 | | |
85 | | impl LutBarycentricReduction<u16, u16> for () { |
86 | | #[inline(always)] |
87 | 0 | fn reduce<const SRC_BP: usize, const BINS: usize>(v: u16) -> u16 { |
88 | 0 | let src_scale = 1. / ((1 << SRC_BP) - 1) as f32; |
89 | 0 | let scale = src_scale * (BINS - 1) as f32; |
90 | 0 | (v as f32 * scale).round().min(scale).max(0.) as u16 |
91 | 0 | } |
92 | | } |
93 | | |
94 | | impl LutBarycentricReduction<u16, u8> for () { |
95 | | #[inline(always)] |
96 | 0 | fn reduce<const SRC_BP: usize, const BINS: usize>(v: u16) -> u8 { |
97 | 0 | let shift = SRC_BP as u16 - 8; |
98 | 0 | if SRC_BP == 16 { |
99 | 0 | (v >> 8) as u8 |
100 | | } else { |
101 | 0 | (v >> shift).min(255) as u8 |
102 | | } |
103 | 0 | } |
104 | | } |