@@ -833,6 +833,22 @@ fn to_prophoto() -> Mat3 {
833833 PROPHOTO . from_xyz ( ) . mul ( & bradford ( D65 , D50 ) ) . mul ( & REC2020 . to_xyz ( ) )
834834}
835835
836+ /// [`HueSat::apply`] restores the luminance in full only if the table kept at least this share of it. A colour
837+ /// the table darkens to [`NO_LUMINANCE_KEPT`] or less (only near-black noise, whose channels go negative) keeps
838+ /// its colour; in between the two are blended, so noisy shadows show no step.
839+ const MIN_LUMINANCE_KEPT : f32 = 0.25 ;
840+ const NO_LUMINANCE_KEPT : f32 = 0.15 ;
841+
842+ /// How much of the table's colour a pixel gets when the table kept `share` of its luminance: 0 up to
843+ /// [`NO_LUMINANCE_KEPT`], 1 from [`MIN_LUMINANCE_KEPT`], a smoothstep between.
844+ fn restore_weight ( share : f32 ) -> f32 {
845+ if !share. is_finite ( ) {
846+ return 0.0 ;
847+ }
848+ let t = ( ( share - NO_LUMINANCE_KEPT ) / ( MIN_LUMINANCE_KEPT - NO_LUMINANCE_KEPT ) ) . clamp ( 0.0 , 1.0 ) ;
849+ t * t * ( 3.0 - 2.0 * t)
850+ }
851+
836852/// A fitted [`CameraLook::hue_sat`] table, ready to apply to linear Rec.2020 pixels. It changes
837853/// hue and saturation only: luminance is restored, the camera tone curve owns it.
838854pub ( crate ) struct HueSat < ' a > {
@@ -852,7 +868,19 @@ impl<'a> HueSat<'a> {
852868 let mul = |m : & [ [ f32 ; 3 ] ; 3 ] , v : [ f32 ; 3 ] | -> [ f32 ; 3 ] { std:: array:: from_fn ( |i| m[ i] [ 0 ] * v[ 0 ] + m[ i] [ 1 ] * v[ 1 ] + m[ i] [ 2 ] * v[ 2 ] ) } ;
853869 let out = mul ( & self . from , self . table . apply ( mul ( & self . to , rgb) ) ) ;
854870 let ( before, after) = ( luminance_2020 ( rgb) , luminance_2020 ( out) ) ;
855- if before > 0.0 && after > 0.0 && out. iter ( ) . all ( |v| v. is_finite ( ) ) { out. map ( |v| v * before / after) } else { rgb }
871+ // restoring the luminance divides by the table's: near black, where noise makes some channels
872+ // negative, the table can bring it close to 0, and the gain would turn the pixel white (#820)
873+ if before <= 0.0 || after <= 0.0 || !out. iter ( ) . all ( |v| v. is_finite ( ) ) {
874+ return rgb;
875+ }
876+ let w = restore_weight ( after / before) ;
877+ if w >= 1.0 {
878+ return out. map ( |v| v * before / after) ;
879+ }
880+ if w <= 0.0 {
881+ return rgb;
882+ }
883+ std:: array:: from_fn ( |i| rgb[ i] + ( out[ i] * before / after - rgb[ i] ) * w)
856884 }
857885}
858886
@@ -1439,6 +1467,61 @@ mod tests {
14391467 assert ! ( fit_hue_sat( & [ ] , & Mat3 :: IDENTITY ) . is_none( ) , "too few samples" ) ;
14401468 }
14411469
1470+ #[ test]
1471+ fn hue_sat_never_turns_near_black_noise_white ( ) {
1472+ // a saturating table, as fitted to high-ISO Sony files; near-black noise with mixed-sign channels
1473+ let table = HsvTable { hue_divisions : 4 , sat_divisions : 2 , val_divisions : 1 , data : vec ! [ [ 10.0 , 1.5 , 1.0 ] ; 8 ] , srgb_value : false } ;
1474+ let hue_sat = HueSat :: new ( & table) . unwrap ( ) ;
1475+ let mut seed = 0x9e37_79b9_7f4a_7c15u64 ;
1476+ let mut next = || {
1477+ seed ^= seed << 13 ;
1478+ seed ^= seed >> 7 ;
1479+ seed ^= seed << 17 ;
1480+ ( seed >> 11 ) as f32 / ( 1u64 << 53 ) as f32 * 2.0 - 1.0
1481+ } ;
1482+ let mut worst = 0.0f32 ;
1483+ for _ in 0 ..200_000 {
1484+ let rgb = [ 0.002 + 0.004 * next ( ) , 0.002 + 0.004 * next ( ) , 0.002 + 0.004 * next ( ) ] ;
1485+ if luminance_2020 ( rgb) <= 0.0 {
1486+ continue ;
1487+ }
1488+ let out = hue_sat. apply ( rgb) ;
1489+ let scale = rgb. iter ( ) . fold ( 0.0f32 , |m, v| m. max ( v. abs ( ) ) ) ;
1490+ worst = worst. max ( out. iter ( ) . fold ( 0.0f32 , |m, v| m. max ( v. abs ( ) ) ) / scale) ;
1491+ }
1492+ assert ! ( worst < 20.0 , "a near-black pixel came out {worst}× its largest channel" ) ;
1493+ }
1494+
1495+ #[ test]
1496+ fn hue_sat_restore_weight_blends_smoothly_between_the_thresholds ( ) {
1497+ assert_eq ! ( restore_weight( 0.0 ) , 0.0 ) ;
1498+ assert_eq ! ( restore_weight( NO_LUMINANCE_KEPT ) , 0.0 ) ;
1499+ assert_eq ! ( restore_weight( MIN_LUMINANCE_KEPT ) , 1.0 ) ;
1500+ assert_eq ! ( restore_weight( 1.0 ) , 1.0 ) ;
1501+ assert_eq ! ( restore_weight( f32 :: NAN ) , 0.0 ) ;
1502+ // monotonic and without a step: neighbouring shares 0.001 apart differ by little
1503+ let mut last = 0.0f32 ;
1504+ for i in 0 ..=400 {
1505+ let w = restore_weight ( i as f32 * 0.001 ) ;
1506+ assert ! ( w >= last && w - last < 0.02 , "step at {}: {last} → {w}" , i as f32 * 0.001 ) ;
1507+ last = w;
1508+ }
1509+ }
1510+
1511+ #[ test]
1512+ fn hue_sat_keeps_colours_the_table_leaves_bright_exactly_as_before ( ) {
1513+ // the blend only touches colours whose luminance the table cut below MIN_LUMINANCE_KEPT
1514+ let table = HsvTable { hue_divisions : 4 , sat_divisions : 2 , val_divisions : 1 , data : vec ! [ [ 10.0 , 1.5 , 1.0 ] ; 8 ] , srgb_value : false } ;
1515+ let hue_sat = HueSat :: new ( & table) . unwrap ( ) ;
1516+ let mul = |m : & [ [ f32 ; 3 ] ; 3 ] , v : [ f32 ; 3 ] | -> [ f32 ; 3 ] { std:: array:: from_fn ( |i| m[ i] [ 0 ] * v[ 0 ] + m[ i] [ 1 ] * v[ 1 ] + m[ i] [ 2 ] * v[ 2 ] ) } ;
1517+ for rgb in [ [ 0.4 , 0.2 , 0.1 ] , [ 0.05 , 0.3 , 0.2 ] , [ 0.18 , 0.18 , 0.18 ] , [ 0.6 , 0.1 , 0.5 ] ] {
1518+ let out = mul ( & hue_sat. from , table. apply ( mul ( & hue_sat. to , rgb) ) ) ;
1519+ let ( before, after) = ( luminance_2020 ( rgb) , luminance_2020 ( out) ) ;
1520+ assert ! ( after >= MIN_LUMINANCE_KEPT * before) ;
1521+ assert_eq ! ( hue_sat. apply( rgb) , out. map( |v| v * before / after) ) ;
1522+ }
1523+ }
1524+
14421525 fn raw_of ( format : RawFormat , make : & str , model : & str , color : lightcraft_raw:: ColorData ) -> RawImage {
14431526 let area = lightcraft_raw:: Rect :: new ( 0 , 0 , 2 , 2 ) ;
14441527 RawImage {
0 commit comments