@@ -1023,18 +1023,76 @@ impl Session {
10231023 /// The op that sets a photo's develop settings and appends a History entry (for batches).
10241024 pub fn develop_op ( & self , id : PhotoId , settings : DevelopSettings , label : & str ) -> Option < Op > {
10251025 self . catalog . photo ( id) ?;
1026+ let mut settings = settings;
1027+ // old masks (on the corrected frame) go onto the photo, from the frame they were drawn on:
1028+ // masks this edit kept, under the photo's settings before it (a geometry edit then moves
1029+ // the photo, not them); masks it brought in (paste, sync, a preset, a history step, a
1030+ // sidecar), under the settings they came with, where they render now. Kept masks also
1031+ // turn with an orientation change the edit makes (a sync or paste of the Crop group).
1032+ let before = match & self . interaction {
1033+ Some ( i) if i. photo == id => Some ( i. original . clone ( ) ) ,
1034+ _ => self . develop_of ( id) ,
1035+ } ;
1036+ let turn = before. as_ref ( ) . and_then ( |b| orientation_change ( b. orientation , settings. orientation ) ) ;
1037+ if settings. masks . iter ( ) . any ( |m| !m. on_photo ) || turn. is_some ( ) {
1038+ let kept_frame = before. as_ref ( ) . and_then ( |b| self . mask_frame ( id, b) ) ;
1039+ let new_frame = self . mask_frame ( id, & settings) ;
1040+ for m in & mut settings. masks {
1041+ let kept = before. as_ref ( ) . is_some_and ( |b| b. masks . contains ( m) ) ;
1042+ if let Some ( f) = if kept { kept_frame. as_ref ( ) } else { new_frame. as_ref ( ) } {
1043+ lightcraft_pipeline:: mask_space:: attach ( m, f) ;
1044+ }
1045+ if let ( true , Some ( t) ) = ( kept, turn) {
1046+ lightcraft_pipeline:: mask_space:: turn ( m, t) ;
1047+ }
1048+ }
1049+ }
10261050 let settings = Arc :: new ( settings) ;
10271051 let step = lightcraft_catalog:: HistoryStep { label : label. into ( ) , settings : settings. clone ( ) } ;
10281052 Some ( Op :: Batch {
10291053 ops : vec ! [ Op :: SetDevelop { id, settings, label: label. into( ) , edited: Some ( ( self . clock) ( ) ) } , Op :: PushHistory { id, step } ] ,
10301054 } )
10311055 }
10321056
1057+ /// Put the old masks of `d` (on the corrected frame, `Mask::on_photo` false) on photo `id`
1058+ /// before the warp, from the frame of `drawn` (the settings they were drawn under): they
1059+ /// render where they did and from then on stay on their content when lens corrections,
1060+ /// Upright or Transform change.
1061+ pub fn attach_masks ( & self , id : PhotoId , drawn : & DevelopSettings , d : & mut DevelopSettings ) {
1062+ if d. masks . iter ( ) . all ( |m| m. on_photo ) {
1063+ return ;
1064+ }
1065+ let Some ( frame) = self . mask_frame ( id, drawn) else { return } ;
1066+ for m in & mut d. masks {
1067+ lightcraft_pipeline:: mask_space:: attach ( m, & frame) ;
1068+ }
1069+ }
1070+
1071+ /// The uncropped frame of photo `id` under settings `d`, with its lens: where mask
1072+ /// coordinates go through its warp ([`lightcraft_pipeline::geometry::Frame::for_mask`]).
1073+ pub ( crate ) fn mask_frame ( & self , id : PhotoId , d : & DevelopSettings ) -> Option < lightcraft_pipeline:: geometry:: Frame > {
1074+ let p = self . catalog . photo ( id) ?;
1075+ Some ( lightcraft_pipeline:: geometry:: Frame :: with_lens (
1076+ p. width . max ( 1 ) as usize ,
1077+ p. height . max ( 1 ) as usize ,
1078+ & d. effective ( ) ,
1079+ false ,
1080+ p. embedded_lens . as_ref ( ) ,
1081+ ) )
1082+ }
1083+
10331084 pub fn begin_interaction ( & mut self , label : & str ) -> Result < ( ) > {
10341085 if self . interaction . is_some ( ) {
10351086 self . end_interaction ( ) ?;
10361087 }
10371088 let id = self . active ( ) . ok_or_else ( || EngineError :: Other ( "no active photo" . into ( ) ) ) ?;
1089+ // old masks go onto the photo before a drag moves the frame under them (they then stay on
1090+ // their content while it does, not only once it ends)
1091+ if let Some ( d) = self . develop_of ( id) . filter ( |d| d. masks . iter ( ) . any ( |m| !m. on_photo ) ) {
1092+ let mut on = ( * d) . clone ( ) ;
1093+ self . attach_masks ( id, & d, & mut on) ;
1094+ self . apply_silent ( Op :: SetDevelop { id, settings : Arc :: new ( on) , label : label. into ( ) , edited : None } ) ?;
1095+ }
10381096 let original = self . develop_of ( id) . unwrap_or_default ( ) ;
10391097 self . interaction = Some ( Interaction { label : label. into ( ) , photo : id, original, crop_edited : false } ) ;
10401098 Ok ( ( ) )
@@ -1373,3 +1431,22 @@ mod tests;
13731431use tests:: face_recognize as tests_face_recognize;
13741432#[ cfg( test) ]
13751433use tests:: xmp as tests_xmp;
1434+
1435+ /// The turn or flip that takes orientation `from` to `to` (`None` when they are the same): what the
1436+ /// masks, on the oriented photo, must follow ([`lightcraft_pipeline::mask_space::turn`]).
1437+ pub ( crate ) fn orientation_change ( from : lightcraft_geom:: Orientation , to : lightcraft_geom:: Orientation ) -> Option < lightcraft_geom:: Orientation > {
1438+ use lightcraft_geom:: Orientation ;
1439+ if from == to {
1440+ return None ;
1441+ }
1442+ let inv = from. inverse ( ) ;
1443+ let probes = [ ( 0.2 , 0.3 ) , ( 0.7 , 0.1 ) , ( 0.4 , 0.9 ) ] ;
1444+ let close = |a : ( f64 , f64 ) , b : ( f64 , f64 ) | ( a. 0 - b. 0 ) . abs ( ) < 1e-9 && ( a. 1 - b. 1 ) . abs ( ) < 1e-9 ;
1445+ ( 0 ..8u8 ) . map ( |k| Orientation :: from_parts ( k >= 4 , k % 4 ) ) . find ( |t| {
1446+ probes. iter ( ) . all ( |& ( x, y) | {
1447+ let ( rx, ry) = inv. map ( x, y, 1.0 , 1.0 ) ;
1448+ let ( tx, ty) = from. map ( rx, ry, 1.0 , 1.0 ) ;
1449+ close ( t. map ( tx, ty, 1.0 , 1.0 ) , to. map ( rx, ry, 1.0 , 1.0 ) )
1450+ } )
1451+ } )
1452+ }
0 commit comments