mini-film 2.4.1

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
use std::path::PathBuf;

use clap::{Parser, Subcommand, ValueEnum};

const DEFAULT_HALD_LEVEL: u32 = 16;

#[derive(Parser, Debug)]
#[command(
    version,
    about = "Develop RAW files with Lightroom-style film profile Hald CLUTs"
)]
pub(crate) struct Cli {
    #[command(subcommand)]
    pub(crate) command: CommandKind,
}

#[derive(Subcommand, Debug)]
pub(crate) enum CommandKind {
    /// Convert Adobe Camera Raw crs:RGBTable XMP profiles to Hald CLUT PNGs.
    Hald {
        /// XMP profile file or directory to convert.
        input: PathBuf,

        /// Output PNG path for a single file, or output directory for a directory input. Defaults to $HOME/.cache/mini-film/hald.
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Hald level. Level 16 produces a 256x256x256 CLUT stored as a 4096x4096 PNG.
        #[arg(short = 'l', long, default_value_t = DEFAULT_HALD_LEVEL)]
        hald_level: u32,

        /// Overwrite existing output files.
        #[arg(long)]
        overwrite: bool,

        /// Print table metadata without writing PNGs.
        #[arg(long)]
        info_only: bool,
    },

    /// Print parsed details for an emulation or internal RGBTable profile.
    Info {
        /// Profile selector: emulation XMP path/name, internal RGBTable XMP path/name, Hald PNG path/name, or PP3 path.
        profile: String,

        /// Film library root. Emulation XMPs are selected from emulations/ and RGBTable profiles from profiles/.
        #[arg(long)]
        profiles_root: Option<PathBuf>,

        /// Directory containing generated cached Hald PNGs. Defaults to $HOME/.cache/mini-film/hald.
        #[arg(long)]
        hald_dir: Option<PathBuf>,

        /// Hald level used when reporting the cached Hald path for XMP profiles.
        #[arg(short = 'l', long, default_value_t = DEFAULT_HALD_LEVEL)]
        hald_level: u32,
    },

    /// Print the RawTherapee PP3 generated for an emulation or RGBTable profile.
    Pp3 {
        /// Profile selector: emulation XMP path/name, internal RGBTable XMP path/name, Hald PNG path/name, or PP3 path.
        profile: String,

        /// Output PP3 path.
        #[arg(short, long, default_value = "/dev/stdout")]
        output: PathBuf,

        /// Film library root. Emulation XMPs are selected from emulations/ and RGBTable profiles from profiles/.
        #[arg(long)]
        profiles_root: Option<PathBuf>,

        /// Directory containing generated cached Hald PNGs. Defaults to $HOME/.cache/mini-film/hald.
        #[arg(long)]
        hald_dir: Option<PathBuf>,

        /// Hald level used when reporting the cached Hald path for XMP profiles.
        #[arg(short = 'l', long, default_value_t = DEFAULT_HALD_LEVEL)]
        hald_level: u32,
    },

    /// Fit an XMP/RGBTable/Hald look into a Nikon classic .NCP Picture Control.
    Nikon {
        /// Profile selector: emulation XMP path/name, internal RGBTable XMP path/name, or Hald PNG path/name.
        profile: String,

        /// Output .NCP path.
        #[arg(short, long)]
        output: PathBuf,

        /// Optional report path describing approximation error and fitted controls.
        #[arg(long)]
        report: Option<PathBuf>,

        /// Picture Control display name. NCP names are ASCII and truncated to 19 bytes.
        #[arg(long)]
        name: Option<String>,

        /// Film library root. Emulation XMPs are selected from emulations/ and RGBTable profiles from profiles/.
        #[arg(long)]
        profiles_root: Option<PathBuf>,

        /// Directory containing generated cached Hald PNGs. Defaults to $HOME/.cache/mini-film/hald.
        #[arg(long)]
        hald_dir: Option<PathBuf>,

        /// Hald level used when resolving cached Hald paths for XMP profiles.
        #[arg(short = 'l', long, default_value_t = DEFAULT_HALD_LEVEL)]
        hald_level: u32,
    },

    /// Run RawTherapee, then apply a Hald CLUT with GraphicsMagick/ImageMagick convert.
    Apply {
        /// RAW file to develop (supports common camera RAW formats such as `.dng`,
        /// `.nef`, `.cr2`, `.cr3`, `.arw`, `.raf`, `.orf`, `.rw2`).
        raw: PathBuf,

        /// Output image path.
        #[arg(short, long)]
        output: PathBuf,

        /// Profile selector: Hald PNG path/name, emulation XMP path/name, or RawTherapee PP3 path.
        #[arg(short, long)]
        profile: String,

        /// Directory containing generated cached Hald PNGs. Defaults to $HOME/.cache/mini-film/hald.
        #[arg(long)]
        hald_dir: Option<PathBuf>,

        /// Film library root. Emulation XMPs are selected from emulations/ and RGBTable profiles from profiles/.
        #[arg(long)]
        profiles_root: Option<PathBuf>,

        /// Hald level to use when --profile points to an XMP or resolves to an XMP.
        #[arg(short = 'l', long, default_value_t = DEFAULT_HALD_LEVEL)]
        hald_level: u32,

        /// Path to rawtherapee-cli binary.
        #[arg(long, default_value = "rawtherapee-cli")]
        rawtherapee: PathBuf,

        /// Path to convert binary.
        #[arg(long, default_value = "convert")]
        convert: PathBuf,

        /// Keep the intermediate TIFF generated by RawTherapee.
        #[arg(long)]
        keep_intermediate: Option<PathBuf>,

        /// Disable Lightroom XMP grain emulation.
        #[arg(long)]
        no_grain: bool,

        /// Override grain as amount,size,frequency, each 0..100. Example: --grain 30,45,45
        #[arg(long)]
        grain: Option<String>,

        /// Built-in grain override: light, medium, or heavy.
        #[arg(long)]
        grain_preset: Option<String>,

        /// Seed for deterministic generated grain. Defaults to current time of day.
        #[arg(long)]
        grain_seed: Option<u64>,

        /// JPEG quality when output path ends in .jpg or .jpeg.
        #[arg(long, default_value_t = 95)]
        jpg_quality: u8,

        /// Resize final output with GraphicsMagick geometry, for example 3000x3000 or 3000x3000>.
        #[arg(long)]
        resize: Option<String>,

        /// Resize final output so the longest edge is at most this many pixels.
        #[arg(long)]
        long_edge: Option<u32>,

        /// Resize final output so width is at most this many pixels.
        #[arg(long)]
        max_width: Option<u32>,

        /// Resize final output so height is at most this many pixels.
        #[arg(long)]
        max_height: Option<u32>,

        /// JPEG chroma subsampling.
        #[arg(long, value_enum, default_value_t = JpegSubsampling::S444)]
        jpeg_subsampling: JpegSubsampling,

        /// Strip profiles and text metadata from final output.
        #[arg(long)]
        strip_metadata: bool,

        /// Write progressive/interlaced JPEG output.
        #[arg(long)]
        progressive_jpeg: bool,
    },

    /// Apply a profile to every supported RAW file in an input folder.
    Batch {
        /// Input folder scanned recursively for supported RAW files (case-insensitive), e.g.
        /// `.dng`, `.nef`, `.cr2`, `.cr3`, `.arw`, `.raf`, `.orf`, `.rw2`, ...
        input: PathBuf,

        /// Output folder. It is created if it does not exist.
        output: PathBuf,

        /// Profile selector: Hald PNG path/name, emulation XMP path/name, or RawTherapee PP3 path.
        #[arg(short, long)]
        profile: String,

        /// Directory containing generated cached Hald PNGs. Defaults to $HOME/.cache/mini-film/hald.
        #[arg(long)]
        hald_dir: Option<PathBuf>,

        /// Film library root. Emulation XMPs are selected from emulations/ and RGBTable profiles from profiles/.
        #[arg(long)]
        profiles_root: Option<PathBuf>,

        /// Hald level to use when --profile points to an XMP or resolves to an XMP.
        #[arg(short = 'l', long, default_value_t = DEFAULT_HALD_LEVEL)]
        hald_level: u32,

        /// Path to rawtherapee-cli binary.
        #[arg(long, default_value = "rawtherapee-cli")]
        rawtherapee: PathBuf,

        /// Path to convert binary.
        #[arg(long, default_value = "convert")]
        convert: PathBuf,

        /// Disable Lightroom XMP grain emulation.
        #[arg(long)]
        no_grain: bool,

        /// Override grain as amount,size,frequency, each 0..100. Example: --grain 30,45,45
        #[arg(long)]
        grain: Option<String>,

        /// Built-in grain override: light, medium, or heavy.
        #[arg(long)]
        grain_preset: Option<String>,

        /// Base seed for deterministic generated grain. Defaults to current time of day.
        #[arg(long)]
        grain_seed: Option<u64>,

        /// Number of RAW files to process in parallel. Defaults to half of CPU threads.
        #[arg(long)]
        jobs: Option<usize>,

        /// Output format for generated batch files.
        #[arg(long, value_enum, default_value_t = BatchOutputFormat::Jpg)]
        output_format: BatchOutputFormat,

        /// JPEG quality for JPG batch outputs.
        #[arg(long, default_value_t = 95)]
        jpg_quality: u8,

        /// Resize final outputs with GraphicsMagick geometry, for example 3000x3000 or 3000x3000>.
        #[arg(long)]
        resize: Option<String>,

        /// Resize final outputs so the longest edge is at most this many pixels.
        #[arg(long)]
        long_edge: Option<u32>,

        /// Resize final outputs so width is at most this many pixels.
        #[arg(long)]
        max_width: Option<u32>,

        /// Resize final outputs so height is at most this many pixels.
        #[arg(long)]
        max_height: Option<u32>,

        /// JPEG chroma subsampling.
        #[arg(long, value_enum, default_value_t = JpegSubsampling::S444)]
        jpeg_subsampling: JpegSubsampling,

        /// Strip profiles and text metadata from final outputs.
        #[arg(long)]
        strip_metadata: bool,

        /// Write progressive/interlaced JPEGs.
        #[arg(long)]
        progressive_jpeg: bool,
    },

    /// Render every resolvable XMP profile as a structured contact-sheet thumbnail.
    Sampler {
        /// RAW file to use as the sampler source (supports common camera RAW formats
        /// like `.dng`, `.nef`, `.cr2`, `.cr3`, `.arw`, `.raf`, `.orf`, `.rw2`).
        raw: PathBuf,

        /// Output contact sheet path (.jpg/.jpeg or .html).
        #[arg(short, long)]
        output: PathBuf,

        /// Film library root. Sampler reads emulation XMPs from emulations/ and resolves RGBTables from profiles/.
        #[arg(long)]
        profiles_root: Option<PathBuf>,

        /// Directory containing generated cached Hald PNGs. Defaults to $HOME/.cache/mini-film/hald.
        #[arg(long)]
        hald_dir: Option<PathBuf>,

        /// Hald level to use for temporary XMP profile conversion.
        #[arg(short = 'l', long, default_value_t = DEFAULT_HALD_LEVEL)]
        hald_level: u32,

        /// Path to rawtherapee-cli binary.
        #[arg(long, default_value = "rawtherapee-cli")]
        rawtherapee: PathBuf,

        /// Path to convert binary.
        #[arg(long, default_value = "convert")]
        convert: PathBuf,

        /// Legacy compatibility option; sampler sheet assembly now uses convert.
        #[arg(long, default_value = "montage", hide = true)]
        montage: PathBuf,

        /// Disable Lightroom XMP grain emulation.
        #[arg(long)]
        no_grain: bool,

        /// Base seed for deterministic generated grain. Defaults to current time of day.
        #[arg(long)]
        grain_seed: Option<u64>,

        /// Disable /tmp sampler thumbnail cache and regenerate every profile thumbnail.
        #[arg(long)]
        no_cache: bool,

        /// Number of profiles to render in parallel. Defaults to half of CPU threads.
        #[arg(long)]
        jobs: Option<usize>,

        /// Thumbnail longest edge in pixels.
        #[arg(long, default_value_t = 512)]
        thumbnail_long_edge: u32,

        /// Maximum thumbnails per sampler row.
        #[arg(long, default_value_t = 8)]
        columns: u32,

        /// JPEG quality for thumbnails and JPEG contact sheets.
        #[arg(long, default_value_t = 95)]
        jpg_quality: u8,

        /// JPEG chroma subsampling for thumbnails and the final contact sheet.
        #[arg(long, value_enum, default_value_t = JpegSubsampling::S444)]
        jpeg_subsampling: JpegSubsampling,

        /// Strip profiles and text metadata from generated JPEGs.
        #[arg(long)]
        strip_metadata: bool,

        /// Write progressive/interlaced sampler JPEGs.
        #[arg(long = "progressive", alias = "progressive-jpeg")]
        progressive_jpeg: bool,
    },

    /// Watch an input inbox folder and apply one or more profiles as files arrive.
    #[command(name = "daemon")]
    BatchDaemon {
        /// Input folder to watch recursively for new RAW files.
        input: PathBuf,

        /// Output root folder. It is created if it does not exist.
        output: PathBuf,

        /// Profile selectors to apply to each incoming RAW. Repeat this option for each profile.
        /// Profiles are rendered to output files using their profile stems.
        #[arg(short = 'p', long = "profile", required = true)]
        profile: Vec<String>,

        /// Directory containing generated cached Hald PNGs. Defaults to $HOME/.cache/mini-film/hald.
        #[arg(long)]
        hald_dir: Option<PathBuf>,

        /// Film library root. Emulation XMPs are selected from emulations/ and RGBTables from profiles/.
        #[arg(long)]
        profiles_root: Option<PathBuf>,

        /// Hald level used when --profile resolves to emulation XMPs.
        #[arg(short = 'l', long, default_value_t = DEFAULT_HALD_LEVEL)]
        hald_level: u32,

        /// Path to rawtherapee-cli binary.
        #[arg(long, default_value = "rawtherapee-cli")]
        rawtherapee: PathBuf,

        /// Path to convert binary.
        #[arg(long, default_value = "convert")]
        convert: PathBuf,

        /// Disable Lightroom XMP grain emulation.
        #[arg(long)]
        no_grain: bool,

        /// Override grain as amount,size,frequency, for example 30,45,45.
        #[arg(long)]
        grain: Option<String>,

        /// Built-in grain preset: light, medium, or heavy.
        #[arg(long)]
        grain_preset: Option<String>,

        /// Base seed for deterministic generated grain. Defaults to current time of day.
        #[arg(long)]
        grain_seed: Option<u64>,

        /// Number of files to process in parallel. Defaults to half of CPU threads.
        #[arg(long)]
        jobs: Option<usize>,

        /// Debounce time in seconds for newly-created files when no inotify-style
        /// close/move completion notification is available.
        #[arg(long, default_value_t = 0)]
        debounce_seconds: u64,

        /// Output format for generated files.
        #[arg(long, value_enum, default_value_t = BatchOutputFormat::Jpg)]
        output_format: BatchOutputFormat,

        /// JPEG quality for JPG outputs.
        #[arg(long, default_value_t = 95)]
        jpg_quality: u8,

        /// Resize final outputs with GraphicsMagick geometry, for example 3000x3000 or 3000x3000>.
        #[arg(long)]
        resize: Option<String>,

        /// Resize final outputs so the longest edge is at most this many pixels.
        #[arg(long)]
        long_edge: Option<u32>,

        /// Resize final outputs so width is at most this many pixels.
        #[arg(long)]
        max_width: Option<u32>,

        /// Resize final outputs so height is at most this many pixels.
        #[arg(long)]
        max_height: Option<u32>,

        /// JPEG chroma subsampling.
        #[arg(long, value_enum, default_value_t = JpegSubsampling::S444)]
        jpeg_subsampling: JpegSubsampling,

        /// Strip profiles and text metadata from final outputs.
        #[arg(long)]
        strip_metadata: bool,

        /// Write progressive/interlaced JPEG output.
        #[arg(long = "progressive", alias = "progressive-jpeg")]
        progressive_jpeg: bool,
    },
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
pub(crate) enum JpegSubsampling {
    S444,
    S422,
    S420,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
pub(crate) enum BatchOutputFormat {
    Jpg,
    Tiff,
}

impl BatchOutputFormat {
    pub(crate) fn extension(self) -> &'static str {
        match self {
            BatchOutputFormat::Jpg => "jpg",
            BatchOutputFormat::Tiff => "tif",
        }
    }
}

impl JpegSubsampling {
    pub(crate) fn graphicsmagick_sampling_factor(self) -> &'static str {
        match self {
            JpegSubsampling::S444 => "1x1,1x1,1x1",
            JpegSubsampling::S422 => "2x1,1x1,1x1",
            JpegSubsampling::S420 => "2x2,1x1,1x1",
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) struct ExportOptions {
    pub(crate) jpg_quality: u8,
    pub(crate) resize: Option<String>,
    pub(crate) long_edge: Option<u32>,
    pub(crate) max_width: Option<u32>,
    pub(crate) max_height: Option<u32>,
    pub(crate) jpeg_subsampling: JpegSubsampling,
    pub(crate) strip_metadata: bool,
    pub(crate) progressive_jpeg: bool,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn batch_output_format_extensions_match_generated_files() {
        assert_eq!(BatchOutputFormat::Jpg.extension(), "jpg");
        assert_eq!(BatchOutputFormat::Tiff.extension(), "tif");
    }

    #[test]
    fn jpeg_subsampling_maps_to_graphicsmagick_sampling_factors() {
        assert_eq!(
            JpegSubsampling::S444.graphicsmagick_sampling_factor(),
            "1x1,1x1,1x1"
        );
        assert_eq!(
            JpegSubsampling::S422.graphicsmagick_sampling_factor(),
            "2x1,1x1,1x1"
        );
        assert_eq!(
            JpegSubsampling::S420.graphicsmagick_sampling_factor(),
            "2x2,1x1,1x1"
        );
    }

    #[test]
    fn cli_parses_level_16_as_the_hald_default_for_all_profile_commands() {
        let cli = Cli::parse_from(["mini-film", "hald", "profiles"]);
        assert!(matches!(
            cli.command,
            CommandKind::Hald { hald_level: 16, .. }
        ));

        let cli = Cli::parse_from(["mini-film", "info", "profile"]);
        assert!(matches!(
            cli.command,
            CommandKind::Info { hald_level: 16, .. }
        ));

        let cli = Cli::parse_from(["mini-film", "pp3", "profile"]);
        assert!(matches!(
            cli.command,
            CommandKind::Pp3 { hald_level: 16, .. }
        ));

        let cli = Cli::parse_from(["mini-film", "nikon", "profile", "--output", "out.ncp"]);
        assert!(matches!(
            cli.command,
            CommandKind::Nikon { hald_level: 16, .. }
        ));

        let cli = Cli::parse_from([
            "mini-film",
            "apply",
            "--output",
            "out.jpg",
            "--profile",
            "profile",
            "input.dng",
        ]);
        assert!(matches!(
            cli.command,
            CommandKind::Apply { hald_level: 16, .. }
        ));

        let cli = Cli::parse_from([
            "mini-film",
            "batch",
            "input-dir",
            "output-dir",
            "--profile",
            "profile",
        ]);
        assert!(matches!(
            cli.command,
            CommandKind::Batch { hald_level: 16, .. }
        ));

        let cli = Cli::parse_from(["mini-film", "sampler", "input.dng", "--output", "out.jpg"]);
        assert!(matches!(
            cli.command,
            CommandKind::Sampler { hald_level: 16, .. }
        ));

        let cli = Cli::parse_from([
            "mini-film",
            "sampler",
            "input.dng",
            "--output",
            "out.jpg",
            "--jobs",
            "8",
            "--columns",
            "4",
            "--no-cache",
        ]);
        assert!(matches!(
            cli.command,
            CommandKind::Sampler {
                jobs: Some(8),
                columns: 4,
                no_cache: true,
                ..
            }
        ));

        let cli = Cli::parse_from([
            "mini-film",
            "daemon",
            "input-dir",
            "output-dir",
            "--profile",
            "portra 400 grainy",
            "--profile",
            "portra 400",
            "--jobs",
            "12",
            "--debounce-seconds",
            "15",
            "--output-format",
            "tiff",
        ]);
        assert!(matches!(
            cli.command,
            CommandKind::BatchDaemon {
                profile,
                jobs: Some(12),
                debounce_seconds: 15,
                output_format: crate::cli::BatchOutputFormat::Tiff,
                ..
            } if profile == vec!["portra 400 grainy", "portra 400"]
        ));
    }
}