Skip to main content

moxcms/conversions/
reduction.rs

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