2626 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2727 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828 */
29- use crate :: mappers:: Rgb ;
3029use crate :: mlaf:: mlaf;
30+ use moxcms:: { f_exp2, f_exp2f, f_log2f, f_powf, Rgb } ;
3131use quick_xml:: Reader ;
3232
3333#[ repr( C ) ]
@@ -709,8 +709,7 @@ impl IsoGainMap {
709709 return Err ( UhdrErrorInfo {
710710 error_code : UhdrErrorCode :: UnsupportedFeature ,
711711 detail : Some ( format ! (
712- "Unexpected minimum version {}, expected 0" ,
713- min_version
712+ "Unexpected minimum version {min_version}, expected 0" ,
714713 ) ) ,
715714 } ) ;
716715 }
@@ -729,8 +728,7 @@ impl IsoGainMap {
729728 return Err ( UhdrErrorInfo {
730729 error_code : UhdrErrorCode :: UnsupportedFeature ,
731730 detail : Some ( format ! (
732- "Unexpected channel count {}, expected 1 or 3" ,
733- channel_count
731+ "Unexpected channel count {channel_count}, expected 1 or 3" ,
734732 ) ) ,
735733 } ) ;
736734 }
@@ -849,9 +847,9 @@ impl IsoGainMap {
849847 let mut to = GainMap :: default ( ) ;
850848 for i in 0 ..3 {
851849 to. max_content_boost [ i] =
852- ( self . gain_map_max_n [ i] as f64 / self . gain_map_max_d [ i] as f64 ) . exp2 ( ) as f32 ;
850+ f_exp2 ( self . gain_map_max_n [ i] as f64 / self . gain_map_max_d [ i] as f64 ) as f32 ;
853851 to. min_content_boost [ i] =
854- ( self . gain_map_min_n [ i] as f64 / self . gain_map_min_d [ i] as f64 ) . exp2 ( ) as f32 ;
852+ f_exp2 ( self . gain_map_min_n [ i] as f64 / self . gain_map_min_d [ i] as f64 ) as f32 ;
855853
856854 to. gamma [ i] =
857855 ( self . gain_map_gamma_n [ i] as f64 / self . gain_map_gamma_d [ i] as f64 ) as f32 ;
@@ -861,11 +859,11 @@ impl IsoGainMap {
861859 to. offset_hdr [ i] =
862860 ( self . alternate_offset_n [ i] as f64 / self . alternate_offset_d [ i] as f64 ) as f32 ;
863861 }
864- to. hdr_capacity_max = ( self . alternate_hdr_headroom_n as f64
865- / self . alternate_hdr_headroom_d as f64 )
866- . exp2 ( ) as f32 ;
862+ to. hdr_capacity_max =
863+ f_exp2 ( self . alternate_hdr_headroom_n as f64 / self . alternate_hdr_headroom_d as f64 )
864+ as f32 ;
867865 to. hdr_capacity_min =
868- ( self . base_hdr_headroom_n as f64 / self . base_hdr_headroom_d as f64 ) . exp2 ( ) as f32 ;
866+ f_exp2 ( self . base_hdr_headroom_n as f64 / self . base_hdr_headroom_d as f64 ) as f32 ;
869867 to. use_base_cg = self . use_base_color_space ;
870868 to
871869 }
@@ -885,7 +883,7 @@ pub struct GainMap {
885883
886884impl GainMap {
887885 #[ allow( dead_code) ]
888- pub ( crate ) fn all_channels_are_identical ( & self ) -> bool {
886+ pub ( crate ) fn are_all_channels_identical ( & self ) -> bool {
889887 self . max_content_boost [ 0 ] == self . max_content_boost [ 1 ]
890888 && self . max_content_boost [ 0 ] == self . max_content_boost [ 2 ]
891889 && self . min_content_boost [ 0 ] == self . min_content_boost [ 1 ]
@@ -910,18 +908,18 @@ pub struct GainLUT<const N: usize> {
910908impl < const N : usize > GainLUT < N > {
911909 fn gen_table ( idx : usize , metadata : GainMap , gainmap_weight : f32 ) -> Box < [ f32 ; N ] > {
912910 let mut set = Box :: new ( [ 0f32 ; N ] ) ;
913- let min_cb = metadata. min_content_boost [ idx] . log2 ( ) ;
914- let max_cb = metadata. max_content_boost [ idx] . log2 ( ) ;
911+ let min_cb = f_log2f ( metadata. min_content_boost [ idx] ) ;
912+ let max_cb = f_log2f ( metadata. max_content_boost [ idx] ) ;
915913 for ( i, gain_value) in set. iter_mut ( ) . enumerate ( ) {
916914 let value = i as f32 / ( N - 1 ) as f32 ;
917915 let log_boost = min_cb * ( 1.0f32 - value) + max_cb * value;
918- * gain_value = ( log_boost * gainmap_weight) . exp2 ( ) ;
916+ * gain_value = f_exp2f ( log_boost * gainmap_weight) ;
919917 }
920918 set
921919 }
922920
923921 pub fn new ( metadata : GainMap , gainmap_weight : f32 ) -> Self {
924- assert ! ( N >= 255 , "Received N" ) ;
922+ assert ! ( N > 255 , "Received N {N} but it should be > 255 " ) ;
925923 let mut gamma_inv = [ 0f32 ; 3 ] ;
926924 gamma_inv[ 0 ] = ( 1f64 / metadata. gamma [ 0 ] as f64 ) as f32 ;
927925 gamma_inv[ 1 ] = ( 1f64 / metadata. gamma [ 1 ] as f64 ) as f32 ;
@@ -941,7 +939,7 @@ impl<const N: usize> GainLUT<N> {
941939 let gamma_inv = self . gamma_inv [ CN ] ;
942940 let mut gain = gain;
943941 if gamma_inv != 1.0f32 {
944- gain = gain . powf ( gamma_inv) ;
942+ gain = f_powf ( gain , gamma_inv) ;
945943 }
946944 let idx = ( mlaf ( 0.5f32 , gain, ( N - 1 ) as f32 ) as i32 )
947945 . min ( 0 )
@@ -971,7 +969,7 @@ impl<const N: usize> GainLUT<N> {
971969 }
972970
973971 #[ inline]
974- pub fn apply_gain ( & self , color : Rgb , gain : Rgb ) -> Rgb {
972+ pub fn apply_gain ( & self , color : Rgb < f32 > , gain : Rgb < f32 > ) -> Rgb < f32 > {
975973 let gain_factor_r = self . get_gain_r_factor ( gain. r ) ;
976974 let gain_factor_g = self . get_gain_g_factor ( gain. g ) ;
977975 let gain_factor_b = self . get_gain_b_factor ( gain. b ) ;
@@ -993,7 +991,7 @@ impl<const N: usize> GainLUT<N> {
993991/// Computes gain map weight for given display boost
994992pub fn make_gainmap_weight ( gain_map : GainMap , display_boost : f32 ) -> f32 {
995993 let input_boost = display_boost. max ( 1f32 ) ;
996- let gainmap_weight = ( input_boost . log2 ( ) - gain_map. hdr_capacity_min . log2 ( ) )
997- / ( gain_map. hdr_capacity_max . log2 ( ) - gain_map. hdr_capacity_min . log2 ( ) ) ;
994+ let gainmap_weight = ( f_log2f ( input_boost ) - f_log2f ( gain_map. hdr_capacity_min ) )
995+ / ( f_log2f ( gain_map. hdr_capacity_max ) - f_log2f ( gain_map. hdr_capacity_min ) ) ;
998996 gainmap_weight. max ( 0.0f32 ) . min ( 1.0f32 )
999997}
0 commit comments