|
| 1 | +/* |
| 2 | + * // Copyright (c) Radzivon Bartoshyk 8/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 | +use crate::mlaf::mlaf; |
| 30 | +use crate::{m_clamp, ForgeError, RgbToneMapperParameters, ToneMapper}; |
| 31 | +use moxcms::{filmlike_clip, CmsError, InPlaceStage, Matrix3f, Rgb}; |
| 32 | +use num_traits::AsPrimitive; |
| 33 | +use std::fmt::Debug; |
| 34 | + |
| 35 | +pub(crate) struct ToneMapperImpl<T: Copy, const N: usize, const CN: usize, const GAMMA_SIZE: usize> |
| 36 | +{ |
| 37 | + pub(crate) linear_map_r: Box<[f32; N]>, |
| 38 | + pub(crate) linear_map_g: Box<[f32; N]>, |
| 39 | + pub(crate) linear_map_b: Box<[f32; N]>, |
| 40 | + pub(crate) gamma_map_r: Box<[T; 65536]>, |
| 41 | + pub(crate) gamma_map_g: Box<[T; 65536]>, |
| 42 | + pub(crate) gamma_map_b: Box<[T; 65536]>, |
| 43 | + pub(crate) im_stage: Option<Box<dyn InPlaceStage + Sync + Send>>, |
| 44 | + pub(crate) tone_map: Box<crate::tonemapper::SyncToneMap>, |
| 45 | + pub(crate) params: RgbToneMapperParameters, |
| 46 | +} |
| 47 | + |
| 48 | +pub(crate) struct MatrixStage<const CN: usize> { |
| 49 | + pub(crate) gamut_color_conversion: Matrix3f, |
| 50 | +} |
| 51 | + |
| 52 | +impl<const CN: usize> InPlaceStage for MatrixStage<CN> { |
| 53 | + fn transform(&self, dst: &mut [f32]) -> Result<(), CmsError> { |
| 54 | + let c = self.gamut_color_conversion; |
| 55 | + for chunk in dst.chunks_exact_mut(CN) { |
| 56 | + let r = mlaf( |
| 57 | + mlaf(chunk[0] * c.v[0][0], chunk[1], c.v[0][1]), |
| 58 | + chunk[2], |
| 59 | + c.v[0][2], |
| 60 | + ); |
| 61 | + let g = mlaf( |
| 62 | + mlaf(chunk[0] * c.v[1][0], chunk[1], c.v[1][1]), |
| 63 | + chunk[2], |
| 64 | + c.v[1][2], |
| 65 | + ); |
| 66 | + let b = mlaf( |
| 67 | + mlaf(chunk[0] * c.v[2][0], chunk[1], c.v[2][1]), |
| 68 | + chunk[2], |
| 69 | + c.v[2][2], |
| 70 | + ); |
| 71 | + |
| 72 | + chunk[0] = m_clamp(r, 0.0, 1.0); |
| 73 | + chunk[1] = m_clamp(g, 0.0, 1.0); |
| 74 | + chunk[2] = m_clamp(b, 0.0, 1.0); |
| 75 | + } |
| 76 | + Ok(()) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +pub(crate) struct MatrixGamutClipping<const CN: usize> { |
| 81 | + pub(crate) gamut_color_conversion: Matrix3f, |
| 82 | +} |
| 83 | + |
| 84 | +impl<const CN: usize> InPlaceStage for MatrixGamutClipping<CN> { |
| 85 | + fn transform(&self, dst: &mut [f32]) -> Result<(), CmsError> { |
| 86 | + let c = self.gamut_color_conversion; |
| 87 | + for chunk in dst.chunks_exact_mut(CN) { |
| 88 | + let r = mlaf( |
| 89 | + mlaf(chunk[0] * c.v[0][0], chunk[1], c.v[0][1]), |
| 90 | + chunk[2], |
| 91 | + c.v[0][2], |
| 92 | + ); |
| 93 | + let g = mlaf( |
| 94 | + mlaf(chunk[0] * c.v[1][0], chunk[1], c.v[1][1]), |
| 95 | + chunk[2], |
| 96 | + c.v[1][2], |
| 97 | + ); |
| 98 | + let b = mlaf( |
| 99 | + mlaf(chunk[0] * c.v[2][0], chunk[1], c.v[2][1]), |
| 100 | + chunk[2], |
| 101 | + c.v[2][2], |
| 102 | + ); |
| 103 | + |
| 104 | + let mut rgb = Rgb::new(r, g, b); |
| 105 | + if rgb.is_out_of_gamut() { |
| 106 | + rgb = filmlike_clip(rgb); |
| 107 | + chunk[0] = m_clamp(rgb.r, 0.0, 1.0); |
| 108 | + chunk[1] = m_clamp(rgb.g, 0.0, 1.0); |
| 109 | + chunk[2] = m_clamp(rgb.b, 0.0, 1.0); |
| 110 | + } else { |
| 111 | + chunk[0] = m_clamp(r, 0.0, 1.0); |
| 112 | + chunk[1] = m_clamp(g, 0.0, 1.0); |
| 113 | + chunk[2] = m_clamp(b, 0.0, 1.0); |
| 114 | + } |
| 115 | + } |
| 116 | + Ok(()) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl< |
| 121 | + T: Copy + AsPrimitive<usize> + Clone + Default + Debug, |
| 122 | + const N: usize, |
| 123 | + const CN: usize, |
| 124 | + const GAMMA_SIZE: usize, |
| 125 | + > ToneMapper<T> for ToneMapperImpl<T, N, CN, GAMMA_SIZE> |
| 126 | +where |
| 127 | + u32: AsPrimitive<T>, |
| 128 | +{ |
| 129 | + fn tonemap_lane(&self, src: &[T], dst: &mut [T]) -> Result<(), ForgeError> { |
| 130 | + assert!(CN == 3 || CN == 4); |
| 131 | + if src.len() != dst.len() { |
| 132 | + return Err(ForgeError::LaneSizeMismatch); |
| 133 | + } |
| 134 | + if src.len() % CN != 0 { |
| 135 | + return Err(ForgeError::LaneMultipleOfChannels); |
| 136 | + } |
| 137 | + assert_eq!(src.len(), dst.len()); |
| 138 | + let mut linearized_content = vec![0f32; src.len()]; |
| 139 | + for (src, dst) in src |
| 140 | + .chunks_exact(CN) |
| 141 | + .zip(linearized_content.chunks_exact_mut(CN)) |
| 142 | + { |
| 143 | + dst[0] = self.linear_map_r[src[0].as_()] * self.params.exposure; |
| 144 | + dst[1] = self.linear_map_g[src[1].as_()] * self.params.exposure; |
| 145 | + dst[2] = self.linear_map_b[src[2].as_()] * self.params.exposure; |
| 146 | + if CN == 4 { |
| 147 | + dst[3] = f32::from_bits(src[3].as_() as u32); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + self.tonemap_linearized_lane(&mut linearized_content)?; |
| 152 | + |
| 153 | + if let Some(c) = &self.im_stage { |
| 154 | + c.transform(&mut linearized_content) |
| 155 | + .map_err(|_| ForgeError::UnknownError)?; |
| 156 | + } else { |
| 157 | + for chunk in linearized_content.chunks_exact_mut(CN) { |
| 158 | + let rgb = Rgb::new(chunk[0], chunk[1], chunk[2]); |
| 159 | + chunk[0] = rgb.r.max(0.); |
| 160 | + chunk[1] = rgb.g.max(0.); |
| 161 | + chunk[2] = rgb.b.max(0.); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + let scale_value = (GAMMA_SIZE - 1) as f32; |
| 166 | + |
| 167 | + for (dst, src) in dst |
| 168 | + .chunks_exact_mut(CN) |
| 169 | + .zip(linearized_content.chunks_exact(CN)) |
| 170 | + { |
| 171 | + let r = mlaf(0.5, src[0], scale_value).min(u16::MAX as f32) as u16; |
| 172 | + let g = mlaf(0.5, src[1], scale_value).min(u16::MAX as f32) as u16; |
| 173 | + let b = mlaf(0.5, src[2], scale_value).min(u16::MAX as f32) as u16; |
| 174 | + dst[0] = self.gamma_map_r[r as usize]; |
| 175 | + dst[1] = self.gamma_map_g[g as usize]; |
| 176 | + dst[2] = self.gamma_map_b[b as usize]; |
| 177 | + if CN == 4 { |
| 178 | + dst[3] = src[3].to_bits().as_(); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + Ok(()) |
| 183 | + } |
| 184 | + |
| 185 | + fn tonemap_linearized_lane(&self, in_place: &mut [f32]) -> Result<(), ForgeError> { |
| 186 | + assert!(CN == 3 || CN == 4); |
| 187 | + if in_place.len() % CN != 0 { |
| 188 | + return Err(ForgeError::LaneMultipleOfChannels); |
| 189 | + } |
| 190 | + self.tone_map.process_lane(in_place); |
| 191 | + Ok(()) |
| 192 | + } |
| 193 | +} |
0 commit comments