@@ -1953,12 +1953,8 @@ fn partial_look_brightens_a_dull_scene_without_learning_colour() {
19531953 assert ! ( lightness( & neutral) < target * 0.6 , "the neutral fallback is far darker" ) ;
19541954}
19551955
1956- /// Issue #631's bird: a frame that is mostly bright overcast sky (here 90%, camera JPEG about
1957- /// 0.93, above the fits' 0.85 cut) and dark, nearly colourless branches that the camera renders
1958- /// like the neutral fallback. On the midtones alone no look is clearly better than the fallback;
1959- /// with the bright, unclipped pairs the partial look takes the sky from the camera.
1960- #[ test]
1961- fn a_mostly_bright_frame_gets_its_sky_from_the_bright_pairs ( ) {
1956+ /// Paired sensor and camera JPEG proxies for a bright overcast sky with dark branches.
1957+ fn bright_sky_scene ( ) -> ( Rgb32f , Rgb32f ) {
19621958 let ( w, h) = ( 96usize , 64usize ) ;
19631959 let neutral = ToneMap :: new ( 0.0 , 0.0 , 0.0 ) ;
19641960 let ( mut sensor, mut reference) = ( Rgb32f :: new ( w, h) , Rgb32f :: new ( w, h) ) ;
@@ -1976,16 +1972,61 @@ fn a_mostly_bright_frame_gets_its_sky_from_the_bright_pairs() {
19761972 * r = displayed ( s. map ( f64:: from) , & neutral) . map ( |v| v as f32 ) ;
19771973 }
19781974 }
1979- let none = vec ! [ false ; w * h] ;
1975+ ( sensor, reference)
1976+ }
1977+
1978+ /// Per-channel root mean square error of the rendered held-out pairs against the JPEG.
1979+ fn held_out_render_error ( pairs : & [ Pair ] , map : & ToneMap , matrix : Mat3 , encode : impl Fn ( f64 ) -> f64 ) -> f64 {
1980+ let error = pairs
1981+ . iter ( )
1982+ . step_by ( 3 )
1983+ . map ( |( x, y) | {
1984+ let render = displayed ( matrix. apply ( * x) , map) ;
1985+ render. into_iter ( ) . zip ( y) . map ( |( a, b) | ( encode ( a) - encode ( * b) ) . powi ( 2 ) ) . sum :: < f64 > ( )
1986+ } )
1987+ . sum :: < f64 > ( ) ;
1988+ ( error / ( 3 * pairs. len ( ) . div_ceil ( 3 ) ) as f64 ) . sqrt ( )
1989+ }
1990+
1991+ /// For the bright-overcast case from #631, the midtones already match the neutral fallback.
1992+ /// The bright retry should bring the sky closer to the JPEG while keeping those midtones.
1993+ #[ test]
1994+ fn a_mostly_bright_frame_gets_its_sky_from_the_bright_pairs ( ) {
1995+ let ( sensor, reference) = bright_sky_scene ( ) ;
1996+ let none = vec ! [ false ; sensor. data. len( ) ] ;
19801997 let check = HighlightCheck { jpeg_space : None , pairs : highlights ( & sensor, & reference) , clipped : Vec :: new ( ) , dro : false , grey : false } ;
19811998 let midtones_only = [ None , Some ( EDGE_CONTRAST ) ] . into_iter ( ) . find_map ( |e| fit_partial_on ( & sensor, & reference, e, & check, false ) ) ;
19821999 assert ! ( midtones_only. is_none( ) , "on the midtones alone the look is no clear gain" ) ;
19832000 let look = fit_look ( & sensor, & reference, & none, None ) . expect ( "a partial look from the bright pairs" ) ;
19842001 let tone = ToneMap :: camera ( & look. tone , 0.0 , 0.0 , 0.0 ) ;
1985- let sky = luma ( displayed ( look. matrix . apply ( [ 0.42 ; 3 ] ) , & tone) ) ;
1986- let fallback = luma ( displayed ( [ 0.42 ; 3 ] , & neutral) ) ;
1987- assert ! ( ( sky - 0.92 ) . abs( ) < 0.04 , "the sky as the camera renders it: {sky} (fallback {fallback})" ) ;
1988- assert ! ( fallback < 0.85 , "the fallback is visibly darker: {fallback}" ) ;
2002+ let neutral = ToneMap :: new ( 0.0 , 0.0 , 0.0 ) ;
2003+ let ( midtones, bright) = collect_pairs ( & sensor, & reference, 0.005 , None , false ) . unwrap ( ) ;
2004+ let ( sky, _) = super :: pairs:: bright_tone_pairs ( & bright, None ) ;
2005+ let before = held_out_render_error ( & sky, & neutral, Mat3 :: IDENTITY , |v| v) ;
2006+ let after = held_out_render_error ( & sky, & tone, look. matrix , |v| v) ;
2007+ assert ! ( after. powi( 2 ) < before. powi( 2 ) * super :: pairs:: MIN_IMPROVEMENT , "sky RMS {before} to {after}" ) ;
2008+ let encode = |v : f64 | v. max ( 0.0 ) . powf ( 1.0 / 2.2 ) ;
2009+ let before = held_out_render_error ( & midtones, & neutral, Mat3 :: IDENTITY , encode) ;
2010+ let after = held_out_render_error ( & midtones, & tone, look. matrix , encode) ;
2011+ assert ! ( after <= before + MAX_MIDTONE_LOSS , "midtone RMS {before} to {after}" ) ;
2012+ }
2013+
2014+ #[ test]
2015+ fn a_uniform_bright_sky_keeps_the_neutral_fallback ( ) {
2016+ let ( mut sensor, mut reference) = bright_sky_scene ( ) ;
2017+ let ( sensor_sky, reference_sky) = ( sensor. data [ 1 ] , reference. data [ 1 ] ) ;
2018+ sensor. data . fill ( sensor_sky) ;
2019+ reference. data . fill ( reference_sky) ;
2020+ let none = vec ! [ false ; sensor. data. len( ) ] ;
2021+ assert ! ( fit_look( & sensor, & reference, & none, None ) . is_none( ) , "a uniform sky cannot constrain a look" ) ;
2022+ }
2023+
2024+ #[ test]
2025+ fn a_sky_with_all_bright_sensor_samples_clipped_keeps_the_neutral_fallback ( ) {
2026+ let ( sensor, reference) = bright_sky_scene ( ) ;
2027+ // A proxy sample is marked clipped when any of the sensor sites it averages clipped.
2028+ let clipped: Vec < _ > = reference. data . iter ( ) . map ( |p| luminance_2020 ( * p) >= 0.85 ) . collect ( ) ;
2029+ assert ! ( fit_look( & sensor, & reference, & clipped, None ) . is_none( ) , "a clipped sky cannot train the retry" ) ;
19892030}
19902031
19912032/// Codex review of #631's bird: the second attempt (with bright pairs) must not buy its sky with
@@ -1996,7 +2037,6 @@ fn a_mostly_bright_frame_gets_its_sky_from_the_bright_pairs() {
19962037fn the_bright_pairs_attempt_keeps_midtones_clipped_sky_and_clipping ( ) {
19972038 let ( w, h) = ( 96usize , 64usize ) ;
19982039 let neutral = ToneMap :: new ( 0.0 , 0.0 , 0.0 ) ;
1999- let encoded = |v : f64 | v. max ( 0.0 ) . powf ( 1.0 / 2.2 ) ;
20002040 // (sky sensor range, share of the sky that is clipped)
20012041 for ( case, sky_lo, sky_hi, clipped_share) in [
20022042 ( "dim sky" , 0.10 , 0.20 , 0usize ) ,
@@ -2024,26 +2064,7 @@ fn the_bright_pairs_attempt_keeps_midtones_clipped_sky_and_clipping() {
20242064 * r = displayed ( s. map ( f64:: from) , & neutral) . map ( |v| v as f32 ) ;
20252065 }
20262066 }
2027- let look = fit_look ( & sensor, & reference, & clipped, None ) ;
2028- assert ! ( look. is_none( ) , "{case}: the guards must reject the fit and keep the fallback" ) ;
2029- let render = |x : [ f64 ; 3 ] | match & look {
2030- Some ( look) => displayed ( look. matrix . apply ( x) , & ToneMap :: camera ( & look. tone , 0.0 , 0.0 , 0.0 ) ) ,
2031- None => displayed ( x, & neutral) ,
2032- } ;
2033- // midtones: no further from the camera than the fallback, beyond the allowance
2034- let ( mut fallback, mut kept, mut n) = ( 0.0 , 0.0 , 0.0 ) ;
2035- for ( x, y) in sensor. data . iter ( ) . zip ( & reference. data ) . step_by ( 10 ) {
2036- let ( x, y) = ( x. map ( f64:: from) , y. map ( f64:: from) ) ;
2037- for c in 0 ..3 {
2038- fallback += ( encoded ( displayed ( x, & neutral) [ c] ) - encoded ( y[ c] ) ) . powi ( 2 ) ;
2039- kept += ( encoded ( render ( x) [ c] ) - encoded ( y[ c] ) ) . powi ( 2 ) ;
2040- n += 1.0 ;
2041- }
2042- }
2043- assert ! ( ( kept / n) . sqrt( ) <= ( fallback / n) . sqrt( ) + MAX_MIDTONE_LOSS + 0.005 , "{case}: midtones paid for the sky" ) ;
2044- // a clipped sky no darker than the fallback renders it
2045- let sky = luma ( render ( [ 1.0 ; 3 ] ) ) ;
2046- assert ! ( sky >= luma( displayed( [ 1.0 ; 3 ] , & neutral) ) - 0.01 , "{case}: clipped sky darkened to {sky}" ) ;
2067+ assert ! ( fit_look( & sensor, & reference, & clipped, None ) . is_none( ) , "{case}: the guards must reject the fit and keep the fallback" ) ;
20472068 }
20482069 // clipped in the JPEG's sRGB, though no Rec.2020 channel reaches 0.98
20492070 let to_2020 = REC2020 . from_xyz ( ) . mul ( & SRGB . to_xyz ( ) ) ;
@@ -2087,9 +2108,10 @@ fn the_bright_pairs_attempt_follows_the_camera_toe() {
20872108 let with_toe = fit_partial_on ( & sensor, & reference, None , & check, true ) . expect ( "a partial look from the bright pairs" ) ;
20882109 let straight = fit_partial_on ( & without_dark, & reference, None , & check, true ) . expect ( "the same look without the dark pairs" ) ;
20892110 let scene = |look : & CameraLook , x : [ f32 ; 3 ] | look. tone . apply ( luma ( look. matrix . apply ( x. map ( f64:: from) ) ) as f32 ) ;
2111+ let before = held_out_render_error ( & check. pairs , & ToneMap :: new ( 0.0 , 0.0 , 0.0 ) , Mat3 :: IDENTITY , |v| v) ;
20902112 for look in [ & with_toe, & straight] {
2091- let sky = luma ( displayed ( look . matrix . apply ( [ 0.42 ; 3 ] ) , & ToneMap :: camera ( & look. tone , 0.0 , 0.0 , 0.0 ) ) ) ;
2092- assert ! ( ( sky - 0.92 ) . abs ( ) < 0.04 , "the sky as the camera renders it: {sky }" ) ;
2113+ let after = held_out_render_error ( & check . pairs , & ToneMap :: camera ( & look. tone , 0.0 , 0.0 , 0.0 ) , look . matrix , |v| v ) ;
2114+ assert ! ( after < before , "sky RMS {before} to {after }" ) ;
20932115 }
20942116 let error = |look : & CameraLook | {
20952117 dark. iter ( ) . map ( |& i| ( scene ( look, sensor. data [ i] ) - luminance_2020 ( reference. data [ i] ) ) . abs ( ) ) . sum :: < f32 > ( ) / dark. len ( ) as f32
0 commit comments