mini-film 10.2.11

Apply Lightroom-style film emulation profiles to RAW files with RawTherapee and HALD workflows.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
use std::{
    fs,
    path::{Path, PathBuf},
    time::Duration,
};

use anyhow::{Context, Result, bail};
use indicatif::ProgressBar;
use mini_film::{GrainSettings, apply_grain, apply_grain_8bit};
use tempfile::Builder;

use crate::app::export::{
    finalize_output_with_retouch, output_ext, validate_export_options, validate_output_format,
};
use crate::app::pp3::{
    write_rawtherapee_color_noise_profile, write_rawtherapee_lens_corrections_profile,
};
use crate::app::profile::{ResolvedProfile, normalize_name, resolve_profile};
use crate::app::progress::{
    ApplyProgress, file_progress_style, progress_length, progress_stage_adaptive, progress_step,
};
use crate::app::raw::{run_raw_develop, run_raw_develop_jpeg};
use crate::app::retouch::{RetouchSettings, write_rawtherapee_retouch_profile};
use crate::app::util::{
    extract_capture_iso, remove_temp_file, sync_output_metadata_from_raw,
    sync_output_timestamps_from_exif, time_of_day_seed,
};
use crate::cli::{ExportOptions, LensCorrections};
use mini_film::rawtherapee_hald_clut_profile_text;

pub(crate) struct ApplyArgs {
    pub(crate) raw: PathBuf,
    pub(crate) output: PathBuf,
    pub(crate) profile: String,
    pub(crate) hald_dir: PathBuf,
    pub(crate) profiles_root: PathBuf,
    pub(crate) hald_level: u32,
    pub(crate) rawtherapee: PathBuf,
    pub(crate) convert: PathBuf,
    pub(crate) keep_intermediate: Option<PathBuf>,
    pub(crate) no_grain: bool,
    pub(crate) color_noise_iso_threshold: u32,
    pub(crate) lens_corrections: LensCorrections,
    pub(crate) grain: Option<String>,
    pub(crate) grain_preset: Option<String>,
    pub(crate) grain_seed: Option<u64>,
    pub(crate) export: ExportOptions,
    pub(crate) retouch: Option<RetouchSettings>,
}

pub(crate) struct ApplyJob<'a> {
    pub(crate) raw: &'a Path,
    pub(crate) output: &'a Path,
    pub(crate) rawtherapee: &'a Path,
    pub(crate) convert: &'a Path,
    pub(crate) keep_intermediate: Option<&'a Path>,
    pub(crate) no_grain: bool,
    pub(crate) color_noise_iso_threshold: u32,
    pub(crate) lens_corrections: LensCorrections,
    pub(crate) export: &'a ExportOptions,
    pub(crate) quiet: bool,
    pub(crate) exif_comment: Option<String>,
    pub(crate) retouch: Option<&'a RetouchSettings>,
}

fn exif_comment_for_command(command: &str, profile: &str) -> String {
    format!(
        "mini-film {} usage={command} profile={profile}",
        env!("CARGO_PKG_VERSION")
    )
}

/// Run the single-file apply command.
///
/// This validates output/export options, creates a temporary workspace, resolves
/// the selected profile into a Hald plus RawTherapee/grain metadata, applies any
/// explicit grain override, chooses a deterministic-or-time-based seed, and then
/// delegates the actual RAW/Hald/grain/export pipeline to `apply_resolved`.
pub(crate) fn run_apply(args: ApplyArgs) -> Result<()> {
    validate_output_format(&args.output)?;
    validate_export_options(&args.export)?;

    let temp_dir = Builder::new().prefix("mini-film-").tempdir()?;
    let mut resolved = resolve_profile(&args, temp_dir.path())?;
    if let Some(grain) =
        resolve_grain_override(args.grain.as_deref(), args.grain_preset.as_deref())?
    {
        resolved.grain = grain;
    }
    let grain_seed = args.grain_seed.unwrap_or_else(time_of_day_seed);

    let file = ProgressBar::new(progress_length());
    file.set_style(file_progress_style());
    file.set_message("starting");
    let started = std::time::Instant::now();
    let progress = ApplyProgress {
        file: &file,
        started,
        estimates: None,
    };

    let result = apply_resolved(
        ApplyJob {
            raw: &args.raw,
            output: &args.output,
            rawtherapee: &args.rawtherapee,
            convert: &args.convert,
            keep_intermediate: args.keep_intermediate.as_deref(),
            no_grain: args.no_grain,
            color_noise_iso_threshold: args.color_noise_iso_threshold,
            lens_corrections: args.lens_corrections,
            export: &args.export,
            quiet: true,
            exif_comment: Some(exif_comment_for_command("apply", &args.profile)),
            retouch: args.retouch.as_ref(),
        },
        &resolved,
        grain_seed,
        temp_dir.path(),
        Some(&progress),
    );
    match &result {
        Ok(()) => file.finish_and_clear(),
        Err(_) => file.abandon_with_message("failed"),
    }
    result?;

    if let Some(hald_path) = &resolved.hald_path {
        eprintln!(
            "wrote {} using {}",
            args.output.display(),
            hald_path.display()
        );
    } else {
        eprintln!("wrote {} using RawTherapee PP3", args.output.display());
    }
    Ok(())
}

/// Compute whether sharpening and color-noise denoising are expected to be active for
/// a specific input before invoking expensive processing.
///
/// Sharpening is derived from resolved emulation metadata. Denoise reflects the
/// same threshold/ISO logic used by `with_optional_color_noise_profile`.
pub(crate) fn resolve_apply_effects(
    raw: &Path,
    resolved: &ResolvedProfile,
    color_noise_iso_threshold: u32,
) -> (bool, bool) {
    let denoise_applied = denoise_profile_applied(raw, color_noise_iso_threshold);
    (resolved.sharpening_applied, denoise_applied)
}

/// Apply an already resolved profile to one RAW input.
///
/// The function owns the processing graph. It develops RAW with RawTherapee
/// while applying generated `.pp3` adjustments and the Hald CLUT via Film
/// Simulation, using 8-bit JPEG intermediates for JPEG-bound outputs and 16-bit
/// TIFF intermediates for TIFF-bound outputs. It eagerly removes temporary
/// files, optionally renders grain in either 8-bit JPEG space or 16-bit TIFF
/// space, and finally exports to the requested output format while updating
/// progress bars for batch callers.
pub(crate) fn apply_resolved(
    job: ApplyJob<'_>,
    resolved: &ResolvedProfile,
    grain_seed: u64,
    temp_dir: &Path,
    progress: Option<&ApplyProgress<'_>>,
) -> Result<()> {
    validate_output_format(job.output)?;

    let grain_enabled = !job.no_grain && resolved.grain.is_enabled();
    let output_ext = output_ext(job.output)?;
    let jpeg_output = output_ext == "jpg" || output_ext == "jpeg";
    let jpeg_intermediate = jpeg_output && job.keep_intermediate.is_none();
    let intermediate = job
        .keep_intermediate
        .map(Path::to_path_buf)
        .unwrap_or_else(|| {
            if jpeg_intermediate {
                temp_dir.join("rawtherapee.jpg")
            } else {
                temp_dir.join("rawtherapee.tif")
            }
        });
    let cleanup_intermediate = job.keep_intermediate.is_none();

    let rawtherapee_profiles = rawtherapee_profiles_for_apply(resolved, temp_dir, job.retouch)?;
    let rawtherapee_profiles = with_optional_color_noise_profile(
        job.raw,
        &rawtherapee_profiles,
        temp_dir,
        job.color_noise_iso_threshold,
    )?;
    let rawtherapee_profiles = with_optional_lens_corrections_profile(
        &rawtherapee_profiles,
        temp_dir,
        job.lens_corrections,
    )?;
    let raw_stage = progress_stage_adaptive(
        progress,
        1,
        3,
        if jpeg_intermediate {
            "rawtherapee-jpeg"
        } else {
            "rawtherapee-tiff"
        },
        "rawtherapee",
        estimate_rawtherapee_duration(job.raw, jpeg_intermediate),
    );
    if jpeg_intermediate {
        run_raw_develop_jpeg(
            job.rawtherapee,
            &rawtherapee_profiles,
            job.raw,
            &intermediate,
            job.export.jpg_quality,
            job.export.jpeg_subsampling,
            job.quiet,
        )?;
    } else {
        run_raw_develop(
            job.rawtherapee,
            &rawtherapee_profiles,
            job.raw,
            &intermediate,
            job.quiet,
        )?;
    }
    raw_stage.finish();

    if grain_enabled && jpeg_output {
        let grain_stage = progress_stage_adaptive(
            progress,
            3,
            4,
            "grain-jpeg",
            "grain",
            estimate_grain_duration(job.raw, true),
        );
        let grained = temp_dir.join("grained-8.ppm");
        apply_grain_8bit(&intermediate, &grained, resolved.grain, grain_seed)?;
        grain_stage.finish();
        if progress.is_none() {
            eprintln!(
                "applied grain amount={} size={} frequency={}",
                resolved.grain.amount, resolved.grain.size, resolved.grain.frequency
            );
        }
        let export_stage = progress_stage_adaptive(
            progress,
            4,
            5,
            "export-jpeg",
            "jpeg export",
            estimate_export_duration(true),
        );
        finalize_output_with_retouch(job.convert, &grained, job.output, job.export, job.retouch)?;
        export_stage.finish();
        remove_temp_file(&grained)?;
    } else if grain_enabled {
        let grain_stage = progress_stage_adaptive(
            progress,
            3,
            4,
            "grain-tiff",
            "grain",
            estimate_grain_duration(job.raw, false),
        );
        let grained = temp_dir.join("grained.tif");
        apply_grain(&intermediate, &grained, resolved.grain, grain_seed)?;
        grain_stage.finish();
        if progress.is_none() {
            eprintln!(
                "applied grain amount={} size={} frequency={}",
                resolved.grain.amount, resolved.grain.size, resolved.grain.frequency
            );
        }
        let export_stage = progress_stage_adaptive(
            progress,
            4,
            5,
            "export-tiff",
            "export",
            estimate_export_duration(false),
        );
        finalize_output_with_retouch(job.convert, &grained, job.output, job.export, job.retouch)?;
        export_stage.finish();
        remove_temp_file(&grained)?;
    } else {
        progress_step(progress, 3, "grain skipped");
        let export_stage = progress_stage_adaptive(
            progress,
            4,
            5,
            if jpeg_output {
                "export-jpeg"
            } else {
                "export-tiff"
            },
            "export",
            estimate_export_duration(jpeg_output),
        );
        finalize_output_with_retouch(
            job.convert,
            &intermediate,
            job.output,
            job.export,
            job.retouch,
        )?;
        export_stage.finish();
    }

    if cleanup_intermediate {
        remove_temp_file(&intermediate)?;
    }

    if !job.export.strip_metadata {
        let exif_stage = progress_stage_adaptive(
            progress,
            5,
            6,
            "exif-metadata",
            "exif",
            estimate_exif_duration(job.raw),
        );
        sync_output_metadata_from_raw(job.raw, job.output, job.exif_comment.as_deref())?;
        sync_output_timestamps_from_exif(job.raw, job.output)?;
        exif_stage.finish();
    } else {
        let timestamp_stage = progress_stage_adaptive(
            progress,
            5,
            6,
            "timestamps",
            "timestamps",
            estimate_timestamp_sync_duration(),
        );
        sync_output_timestamps_from_exif(job.raw, job.output)?;
        timestamp_stage.finish();
    }
    progress_step(progress, 6, "done");
    Ok(())
}

fn rawtherapee_profiles_for_apply(
    resolved: &ResolvedProfile,
    temp_dir: &Path,
    retouch: Option<&RetouchSettings>,
) -> Result<Vec<PathBuf>> {
    let mut profiles = resolved.rawtherapee_profiles.clone();
    if let Some(retouch) = retouch
        && let Some(profile) = write_rawtherapee_retouch_profile(
            &temp_dir.join("retouch.pp3"),
            resolved.retouch_base,
            retouch,
        )?
    {
        profiles.push(profile);
    }
    if let Some(hald_path) = &resolved.hald_path {
        let lut_profile = temp_dir.join("rt-hald-clut.pp3");
        std::fs::write(&lut_profile, rawtherapee_hald_clut_profile_text(hald_path))
            .with_context(|| format!("writing {}", lut_profile.display()))?;
        profiles.push(lut_profile);
    }
    Ok(profiles)
}

fn estimate_rawtherapee_duration(raw: &Path, jpeg_intermediate: bool) -> Duration {
    let mib = file_size_mib(raw).unwrap_or(45.0);
    let seconds = if jpeg_intermediate {
        1.2 + mib * 0.075
    } else {
        2.0 + mib * 0.11
    };
    Duration::from_secs_f64(seconds.clamp(2.0, 18.0))
}

fn with_optional_color_noise_profile(
    raw: &Path,
    base_profiles: &[PathBuf],
    temp_dir: &Path,
    threshold: u32,
) -> Result<Vec<PathBuf>> {
    let mut profiles = Vec::from(base_profiles);
    if threshold == 0 {
        return Ok(profiles);
    }

    let iso = match extract_capture_iso(raw)? {
        Some(iso) => iso,
        None => return Ok(profiles),
    };
    if iso < threshold {
        return Ok(profiles);
    }

    if let Some(path) =
        write_rawtherapee_color_noise_profile(&temp_dir.join("color-noise.pp3"), iso)?
    {
        profiles.push(path);
    }
    Ok(profiles)
}

fn denoise_profile_applied(raw: &Path, threshold: u32) -> bool {
    if threshold == 0 {
        return false;
    }

    let Ok(Some(iso)) = extract_capture_iso(raw) else {
        return false;
    };
    iso >= threshold
}

fn with_optional_lens_corrections_profile(
    base_profiles: &[PathBuf],
    temp_dir: &Path,
    lens_corrections: LensCorrections,
) -> Result<Vec<PathBuf>> {
    let mut profiles = Vec::from(base_profiles);
    if let Some(path) = write_rawtherapee_lens_corrections_profile(
        &temp_dir.join("lens-corrections.pp3"),
        lens_corrections,
    )? {
        profiles.push(path);
    }
    Ok(profiles)
}

fn estimate_grain_duration(raw: &Path, jpeg_output: bool) -> Duration {
    let mib = file_size_mib(raw).unwrap_or(45.0);
    let seconds = if jpeg_output {
        0.35 + mib * 0.018
    } else {
        0.9 + mib * 0.045
    };
    Duration::from_secs_f64(seconds.clamp(0.6, 8.0))
}

fn estimate_export_duration(jpeg_output: bool) -> Duration {
    if jpeg_output {
        Duration::from_millis(900)
    } else {
        Duration::from_secs(2)
    }
}

fn estimate_exif_duration(raw: &Path) -> Duration {
    let mib = file_size_mib(raw).unwrap_or(2.0);
    let seconds = 0.15 + mib * 0.015;
    Duration::from_secs_f64(seconds.clamp(0.2, 2.0))
}

fn estimate_timestamp_sync_duration() -> Duration {
    Duration::from_millis(150)
}

fn file_size_mib(path: &Path) -> Option<f64> {
    Some(fs::metadata(path).ok()?.len() as f64 / 1_048_576.0)
}

/// Resolve command-line grain overrides.
///
/// XMP grain is used by default, but users can override it with either an
/// explicit `amount,size,frequency` tuple or a named preset. The two override
/// forms are mutually exclusive, and `none`/`off` intentionally resolves to
/// disabled grain rather than an error.
pub(crate) fn resolve_grain_override(
    grain: Option<&str>,
    preset: Option<&str>,
) -> Result<Option<GrainSettings>> {
    match (grain, preset) {
        (Some(_), Some(_)) => bail!("use either --grain or --grain-preset, not both"),
        (Some(value), None) => Ok(Some(parse_grain(value)?)),
        (None, Some(value)) => Ok(Some(match normalize_name(value).as_str() {
            "none" | "off" => GrainSettings::default(),
            "light" => GrainSettings {
                amount: 18,
                size: 35,
                frequency: 40,
            },
            "medium" => GrainSettings {
                amount: 30,
                size: 45,
                frequency: 45,
            },
            "heavy" => GrainSettings {
                amount: 45,
                size: 60,
                frequency: 55,
            },
            _ => bail!("unknown grain preset {value:?}; use light, medium, heavy, or none"),
        })),
        (None, None) => Ok(None),
    }
}

fn parse_grain(value: &str) -> Result<GrainSettings> {
    let parts: Vec<_> = value.split(',').map(str::trim).collect();
    if parts.len() != 3 {
        bail!("--grain must be amount,size,frequency, for example --grain 30,45,45");
    }
    Ok(GrainSettings {
        amount: parse_grain_part(parts[0], "amount")?,
        size: parse_grain_part(parts[1], "size")?,
        frequency: parse_grain_part(parts[2], "frequency")?,
    })
}

fn parse_grain_part(value: &str, name: &str) -> Result<u8> {
    let parsed: u16 = value
        .parse()
        .with_context(|| format!("invalid grain {name} value {value:?}"))?;
    if parsed > 100 {
        bail!("grain {name} must be in 0..100");
    }
    Ok(parsed as u8)
}

#[cfg(test)]
mod tests {
    const RAWTHAPE_HELPER_SCRIPT: &str = include_str!("../../scripts/tests/rawtherapee_helper.sh");
    const CONVERT_HELPER_SCRIPT: &str = include_str!("../../scripts/tests/convert_helper.sh");

    use super::*;
    use crate::app::profile::ResolvedProfile;
    use crate::cli::ExportOptions;
    use image::{ImageBuffer, ImageFormat, Rgb};
    use std::{fs, io::Write, os::unix::fs::PermissionsExt};

    #[test]
    fn grain_override_accepts_tuple_presets_and_none() {
        let grain = resolve_grain_override(Some("10, 20,30"), None)
            .unwrap()
            .unwrap();
        assert_eq!(grain.amount, 10);
        assert_eq!(grain.size, 20);
        assert_eq!(grain.frequency, 30);

        let grain = resolve_grain_override(None, Some("heavy"))
            .unwrap()
            .unwrap();
        assert_eq!(grain.amount, 45);
        assert_eq!(grain.size, 60);
        assert_eq!(grain.frequency, 55);

        assert!(
            !resolve_grain_override(None, Some("none"))
                .unwrap()
                .unwrap()
                .is_enabled()
        );
    }

    #[test]
    fn grain_override_rejects_ambiguous_or_out_of_range_values() {
        assert!(resolve_grain_override(Some("1,2,3"), Some("light")).is_err());
        assert!(resolve_grain_override(Some("1,2"), None).is_err());
        assert!(resolve_grain_override(Some("1,2,101"), None).is_err());
        assert!(resolve_grain_override(None, Some("huge")).is_err());
    }

    #[test]
    fn duration_estimates_are_clamped_and_depend_on_output_path() {
        let dir = tempfile::tempdir().unwrap();
        let raw = dir.path().join("raw.dng");
        fs::write(&raw, vec![0u8; 2 * 1024 * 1024]).unwrap();

        let jpeg = estimate_rawtherapee_duration(&raw, true);
        let tiff = estimate_rawtherapee_duration(&raw, false);
        assert!(jpeg >= Duration::from_secs(2));
        assert!(tiff >= jpeg);
        assert_eq!(estimate_export_duration(true), Duration::from_millis(900));
        assert_eq!(estimate_export_duration(false), Duration::from_secs(2));
    }

    #[test]
    fn missing_file_size_returns_none() {
        assert!(file_size_mib(Path::new("/definitely/missing/raw.dng")).is_none());
    }

    struct FakeRawtherapee {
        log: PathBuf,
    }

    fn write_fake_rawtherapee(path: &Path, output_image: &Path) -> Result<FakeRawtherapee> {
        let rendered = RAWTHAPE_HELPER_SCRIPT
            .replace(
                "__LOG_FILE__",
                &path.with_file_name("raw.log").display().to_string(),
            )
            .replace("__OUTPUT_IMAGE__", &output_image.display().to_string())
            .replace("__CREATE_OUTPUT__", "1")
            .replace("__EXIT_CODE__", "0");
        write_executable_script(path, &rendered)?;

        Ok(FakeRawtherapee {
            log: path.with_file_name("raw.log"),
        })
    }

    fn write_fake_convert(path: &Path) -> Result<PathBuf> {
        let rendered = CONVERT_HELPER_SCRIPT
            .replace(
                "__LOG_FILE__",
                &path.with_file_name("convert.log").display().to_string(),
            )
            .replace("__EXIT_CODE__", "0");
        write_executable_script(path, &rendered)?;
        Ok(path.with_file_name("convert.log"))
    }

    fn write_executable_script(path: &Path, rendered: &str) -> Result<()> {
        let temp_path = path.with_extension("tmp");
        let mut file = fs::File::create(&temp_path)?;
        file.write_all(rendered.as_bytes())?;
        file.flush()?;
        file.sync_all()?;
        drop(file);
        let mut permissions = fs::metadata(&temp_path)?.permissions();
        permissions.set_mode(0o755);
        fs::set_permissions(&temp_path, permissions)?;
        fs::rename(&temp_path, path)?;
        Ok(())
    }

    fn make_source_image(path: &Path) {
        let image = ImageBuffer::from_fn(2, 2, |x, y| {
            if (x + y) % 2 == 0 {
                Rgb([32u8, 96, 160])
            } else {
                Rgb([64u8, 128, 192])
            }
        });
        let ext = path
            .extension()
            .and_then(|ext| ext.to_str())
            .unwrap_or("")
            .to_ascii_lowercase();
        match ext.as_str() {
            "jpg" | "jpeg" => image.save_with_format(path, ImageFormat::Jpeg).unwrap(),
            "tif" | "tiff" => image.save_with_format(path, ImageFormat::Tiff).unwrap(),
            _ => image.save(path).unwrap(),
        }
    }

    fn test_export_options() -> ExportOptions {
        ExportOptions {
            jpg_quality: 90,
            resize: None,
            long_edge: None,
            max_width: None,
            max_height: None,
            jpeg_subsampling: crate::cli::JpegSubsampling::S420,
            strip_metadata: false,
            progressive_jpeg: false,
        }
    }

    fn resolved_profile(grain: GrainSettings, hald: Option<PathBuf>) -> ResolvedProfile {
        ResolvedProfile {
            hald_path: hald,
            rawtherapee_profiles: Vec::new(),
            grain,
            sharpening_applied: false,
            resolved_stem: "profile".to_string(),
            retouch_base: Default::default(),
        }
    }

    #[test]
    fn apply_resolved_runs_jpeg_without_grain_and_calls_final_export() {
        let temp = tempfile::tempdir().unwrap();
        let raw = temp.path().join("frame.NEF");
        fs::write(&raw, b"raw").unwrap();
        let source_image = temp.path().join("rawtherapee.jpg");
        make_source_image(&source_image);

        let raw_log = write_fake_rawtherapee(&temp.path().join("rawtherapee"), &source_image)
            .unwrap()
            .log;
        let convert_log = write_fake_convert(&temp.path().join("convert")).unwrap();
        let out = temp.path().join("out.jpg");
        let resolved = resolved_profile(GrainSettings::default(), None);

        apply_resolved(
            ApplyJob {
                raw: &raw,
                output: &out,
                rawtherapee: &temp.path().join("rawtherapee"),
                convert: &temp.path().join("convert"),
                keep_intermediate: None,
                no_grain: true,
                color_noise_iso_threshold: 0,
                lens_corrections: LensCorrections::default(),
                export: &test_export_options(),
                quiet: true,
                exif_comment: Some("mini-film test".to_string()),
                retouch: None,
            },
            &resolved,
            0,
            temp.path(),
            None,
        )
        .unwrap();

        assert!(out.exists());
        let raw_invocation = fs::read_to_string(raw_log).unwrap();
        assert!(raw_invocation.contains("-j90"));
        assert!(raw_invocation.contains("-c"));
        let convert_invocation = fs::read_to_string(convert_log).unwrap();
        assert!(convert_invocation.contains(&out.to_string_lossy().to_string()));
    }

    #[test]
    fn apply_resolved_runs_tiff_with_grain_and_intermediate_grain_step() {
        let temp = tempfile::tempdir().unwrap();
        let raw = temp.path().join("frame.ARW");
        fs::write(&raw, b"raw").unwrap();
        let source_image = temp.path().join("rawtherapee.tif");
        make_source_image(&source_image);

        let raw_log = write_fake_rawtherapee(&temp.path().join("rawtherapee"), &source_image)
            .unwrap()
            .log;
        let convert_log = write_fake_convert(&temp.path().join("convert")).unwrap();
        let out = temp.path().join("out.tif");
        let resolved = resolved_profile(
            GrainSettings {
                amount: 20,
                size: 40,
                frequency: 40,
            },
            None,
        );

        apply_resolved(
            ApplyJob {
                raw: &raw,
                output: &out,
                rawtherapee: &temp.path().join("rawtherapee"),
                convert: &temp.path().join("convert"),
                keep_intermediate: None,
                no_grain: false,
                color_noise_iso_threshold: 0,
                lens_corrections: LensCorrections::default(),
                export: &test_export_options(),
                quiet: true,
                exif_comment: Some("mini-film test".to_string()),
                retouch: None,
            },
            &resolved,
            1,
            temp.path(),
            None,
        )
        .unwrap();

        assert!(out.exists());
        let raw_invocation = fs::read_to_string(raw_log).unwrap();
        assert!(raw_invocation.contains("-t"));
        let convert_invocation = fs::read_to_string(convert_log).unwrap();
        assert!(convert_invocation.contains(&out.to_string_lossy().to_string()));
    }

    #[test]
    fn apply_resolved_runs_grain_jpeg_path() {
        let temp = tempfile::tempdir().unwrap();
        let raw = temp.path().join("frame.CRF");
        fs::write(&raw, b"raw").unwrap();
        let source_image = temp.path().join("rawtherapee.jpg");
        make_source_image(&source_image);

        let raw_log = write_fake_rawtherapee(&temp.path().join("rawtherapee"), &source_image)
            .unwrap()
            .log;
        let convert_log = write_fake_convert(&temp.path().join("convert")).unwrap();
        let out = temp.path().join("out.jpeg");
        let resolved = resolved_profile(
            GrainSettings {
                amount: 25,
                size: 50,
                frequency: 55,
            },
            None,
        );

        let mut export = test_export_options();
        export.jpg_quality = 84;

        apply_resolved(
            ApplyJob {
                raw: &raw,
                output: &out,
                rawtherapee: &temp.path().join("rawtherapee"),
                convert: &temp.path().join("convert"),
                keep_intermediate: None,
                no_grain: false,
                color_noise_iso_threshold: 0,
                lens_corrections: LensCorrections::default(),
                export: &export,
                quiet: true,
                exif_comment: Some("mini-film test".to_string()),
                retouch: None,
            },
            &resolved,
            2,
            temp.path(),
            None,
        )
        .unwrap();

        assert!(out.exists());
        assert!(fs::read_to_string(raw_log).unwrap().contains("-j84"));
        assert!(
            fs::read_to_string(convert_log)
                .unwrap()
                .contains(&out.to_string_lossy().to_string())
        );
    }
}