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
use std::{
    collections::HashMap,
    fs,
    path::{Path, PathBuf},
    process::Command,
    sync::{Mutex, OnceLock},
};

use anyhow::{Context, Result, bail};

use crate::app::retouch::{RetouchSettings, normalize_rotation};
use crate::app::util::cpu_thread_count;
use crate::cli::ExportOptions;

/// Run the final convert invocation that writes the user-facing output.
///
/// Earlier stages may produce a TIFF, PPM, or grained temporary file. This
/// function creates the destination directory, applies thread limits, appends the
/// shared final export options, and checks convert's exit status so failed
/// encodes do not look like successful pipeline completion.
pub(crate) fn finalize_output(
    convert: &Path,
    input: &Path,
    output: &Path,
    export: &ExportOptions,
) -> Result<()> {
    finalize_output_with_retouch(convert, input, output, export, None)
}

pub(crate) fn finalize_output_with_retouch(
    convert: &Path,
    input: &Path,
    output: &Path,
    export: &ExportOptions,
    retouch: Option<&RetouchSettings>,
) -> Result<()> {
    if let Some(parent) = output.parent() {
        fs::create_dir_all(parent).with_context(|| format!("creating {}", parent.display()))?;
    }

    let mut command = Command::new(convert);
    add_convert_thread_limit(&mut command, convert);
    command.arg(input);
    add_retouch_geometry_args(&mut command, input, retouch)?;
    add_final_convert_args(&mut command, output, export)?;

    let status = command
        .status()
        .with_context(|| format!("running {}", convert.display()))?;

    if !status.success() {
        bail!("final export failed with status {status}");
    }

    Ok(())
}

fn add_retouch_geometry_args(
    command: &mut Command,
    input: &Path,
    retouch: Option<&RetouchSettings>,
) -> Result<()> {
    let Some(retouch) = retouch else {
        return Ok(());
    };
    let retouch = retouch.clone().normalized();
    let rotation = normalize_rotation(retouch.rotation_degrees);
    let (mut width, mut height) =
        image::image_dimensions(input).with_context(|| format!("reading {}", input.display()))?;
    if rotation != 0.0 {
        command.arg("-background").arg("black");
        command.arg("-rotate").arg(format_geometry_float(rotation));
        if is_quarter_turn(rotation) {
            std::mem::swap(&mut width, &mut height);
        } else if !is_half_turn(rotation) {
            let (auto_width, auto_height) = rotated_auto_crop_dimensions(width, height, rotation);
            command
                .arg("-gravity")
                .arg("Center")
                .arg("-crop")
                .arg(format!("{auto_width}x{auto_height}+0+0"))
                .arg("+repage")
                .arg("-gravity")
                .arg("NorthWest");
            width = auto_width;
            height = auto_height;
        }
    }
    if let Some(crop) = retouch.crop {
        let crop_width = ((crop.width * width as f32).round() as u32).clamp(1, width.max(1));
        let crop_height = ((crop.height * height as f32).round() as u32).clamp(1, height.max(1));
        let max_x = width.saturating_sub(crop_width);
        let max_y = height.saturating_sub(crop_height);
        let x = ((crop.x * width as f32).round() as u32).min(max_x);
        let y = ((crop.y * height as f32).round() as u32).min(max_y);
        command
            .arg("-crop")
            .arg(format!("{crop_width}x{crop_height}+{x}+{y}"))
            .arg("+repage");
    }
    Ok(())
}

fn is_quarter_turn(rotation: f32) -> bool {
    let normalized = normalize_rotation(rotation).abs();
    (normalized - 90.0).abs() < 0.001
}

fn is_half_turn(rotation: f32) -> bool {
    (normalize_rotation(rotation).abs() - 180.0).abs() < 0.001
}

fn rotated_auto_crop_dimensions(width: u32, height: u32, rotation: f32) -> (u32, u32) {
    let width = width.max(1) as f64;
    let height = height.max(1) as f64;
    let radians = (normalize_rotation(rotation).abs() as f64).to_radians();
    let sin_a = radians.sin().abs();
    let cos_a = radians.cos().abs();
    if sin_a <= f64::EPSILON || cos_a <= f64::EPSILON {
        return (width.round() as u32, height.round() as u32);
    }

    let (long_side, short_side) = if width >= height {
        (width, height)
    } else {
        (height, width)
    };
    let (auto_width, auto_height) =
        if short_side <= 2.0 * sin_a * cos_a * long_side || (sin_a - cos_a).abs() < f64::EPSILON {
            let side = 0.5 * short_side;
            if width >= height {
                (side / sin_a, side / cos_a)
            } else {
                (side / cos_a, side / sin_a)
            }
        } else {
            let cos_2a = cos_a * cos_a - sin_a * sin_a;
            (
                (width * cos_a - height * sin_a) / cos_2a,
                (height * cos_a - width * sin_a) / cos_2a,
            )
        };

    (
        auto_width.floor().max(1.0).min(width).round() as u32,
        auto_height.floor().max(1.0).min(height).round() as u32,
    )
}

fn format_geometry_float(value: f32) -> String {
    format!("{value:.3}")
        .trim_end_matches('0')
        .trim_end_matches('.')
        .to_string()
}

/// Append final output arguments for JPEG or TIFF export.
///
/// Resize options are added first so the encoder sees the final image geometry.
/// JPEG outputs are forced to 8-bit, then receive metadata stripping,
/// progressive/interlace mode, chroma subsampling, and quality settings. TIFF
/// outputs keep the upstream 16-bit depth, use TIFF Zip/Deflate compression,
/// and only strip metadata when explicitly requested.
pub(crate) fn add_final_convert_args(
    command: &mut Command,
    output: &Path,
    export: &ExportOptions,
) -> Result<()> {
    let ext = output_ext(output)?;
    add_resize_args(command, export);

    if ext == "jpg" || ext == "jpeg" {
        if export.strip_metadata {
            command.arg("-strip");
        }
        if export.progressive_jpeg {
            command.arg("-interlace").arg("Line");
        }
        command
            .arg("-depth")
            .arg("8")
            .arg("-sampling-factor")
            .arg(export.jpeg_subsampling.graphicsmagick_sampling_factor())
            .arg("-quality")
            .arg(export.jpg_quality.clamp(1, 100).to_string());
    } else {
        if export.strip_metadata {
            command.arg("-strip");
        }
        command.arg("-compress").arg("Zip");
    }

    command.arg(output);
    Ok(())
}

/// Append one convert resize geometry derived from export options.
///
/// The CLI accepts raw GraphicsMagick geometry, longest-edge limiting, or
/// separate max width/height controls. This helper chooses exactly one geometry
/// string and uses the `>` suffix for bounding modes so convert only downsizes
/// images that exceed the requested limit.
fn add_resize_args(command: &mut Command, export: &ExportOptions) {
    let geometry = if let Some(resize) = &export.resize {
        Some(resize.clone())
    } else if let Some(long_edge) = export.long_edge {
        Some(format!("{long_edge}x{long_edge}>"))
    } else {
        match (export.max_width, export.max_height) {
            (Some(width), Some(height)) => Some(format!("{width}x{height}>")),
            (Some(width), None) => Some(format!("{width}x>")),
            (None, Some(height)) => Some(format!("x{height}>")),
            (None, None) => None,
        }
    };

    if let Some(geometry) = geometry {
        command.arg("-resize").arg(geometry);
    }
}

/// Limit convert's worker threads to the available CPU count.
///
/// ImageMagick/GraphicsMagick can use multiple threads internally. Passing the
/// explicit limit prevents accidental oversubscription when Rayon or batch
/// progress work is also active, while still letting convert use all logical
/// CPUs available on the machine.
pub(crate) fn add_convert_thread_limit(command: &mut Command, convert: &Path) {
    if convert_supports_threads_limit(convert) {
        command
            .arg("-limit")
            .arg("Threads")
            .arg(cpu_thread_count().to_string());
    }
}

fn thread_support_cache() -> &'static Mutex<HashMap<PathBuf, bool>> {
    static CACHE: OnceLock<Mutex<HashMap<PathBuf, bool>>> = OnceLock::new();
    CACHE.get_or_init(|| Mutex::new(HashMap::new()))
}

fn convert_supports_threads_limit(convert: &Path) -> bool {
    let cache = thread_support_cache();

    if let Ok(map) = cache.lock()
        && let Some(supported) = map.get(convert).copied()
    {
        return supported;
    }

    let supported = detect_convert_threads_limit(convert);
    if let Ok(mut map) = cache.lock() {
        map.insert(convert.to_path_buf(), supported);
    }
    supported
}

fn detect_convert_threads_limit(convert: &Path) -> bool {
    let output = Command::new(convert)
        .arg("-list")
        .arg("resource")
        .output()
        .ok();

    let Some(output) = output else {
        return false;
    };

    let listing = String::from_utf8_lossy(&output.stdout);
    let errors = String::from_utf8_lossy(&output.stderr);
    let combined = format!("{listing}\n{errors}");

    is_threads_present_in_resource_output(&combined)
}

fn is_threads_present_in_resource_output(text: &str) -> bool {
    text.lines().any(|line| {
        let trimmed = line.trim_start();
        let lower = trimmed.to_ascii_lowercase();
        lower.starts_with("threads")
            || trimmed
                .split_once(':')
                .is_some_and(|(key, _)| key.trim().eq_ignore_ascii_case("threads"))
    })
}

/// Validate mutually exclusive export sizing options.
///
/// Raw `--resize` geometry and structured max-size flags express the same
/// operation in different forms, so mixing them would create surprising convert
/// argument order. This check rejects ambiguous combinations, zero dimensions,
/// and empty geometry before any RAW work starts.
pub(crate) fn validate_export_options(export: &ExportOptions) -> Result<()> {
    if export.resize.is_some()
        && (export.long_edge.is_some() || export.max_width.is_some() || export.max_height.is_some())
    {
        bail!("use either --resize or --long-edge/--max-width/--max-height");
    }
    if export.long_edge.is_some() && (export.max_width.is_some() || export.max_height.is_some()) {
        bail!("use either --long-edge or --max-width/--max-height");
    }
    for (name, value) in [
        ("--long-edge", export.long_edge),
        ("--max-width", export.max_width),
        ("--max-height", export.max_height),
    ] {
        if value == Some(0) {
            bail!("{name} must be greater than zero");
        }
    }
    if export.resize.as_deref().is_some_and(str::is_empty) {
        bail!("--resize must not be empty");
    }
    Ok(())
}

pub(crate) fn validate_output_format(output: &Path) -> Result<()> {
    match output_ext(output)?.as_str() {
        "tif" | "tiff" | "jpg" | "jpeg" => Ok(()),
        ext => bail!("unsupported output extension .{ext}; use .tif/.tiff or .jpg/.jpeg"),
    }
}

pub(crate) fn output_ext(output: &Path) -> Result<String> {
    output
        .extension()
        .and_then(|s| s.to_str())
        .map(|s| s.to_ascii_lowercase())
        .ok_or_else(|| anyhow::anyhow!("output path must have .tif/.tiff or .jpg/.jpeg extension"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::app::retouch::RetouchSettings;
    use crate::cli::JpegSubsampling;

    fn export_options() -> ExportOptions {
        ExportOptions {
            jpg_quality: 91,
            resize: None,
            long_edge: None,
            max_width: None,
            max_height: None,
            jpeg_subsampling: JpegSubsampling::S422,
            strip_metadata: false,
            progressive_jpeg: false,
        }
    }

    fn command_args(command: &Command) -> Vec<String> {
        command
            .get_args()
            .map(|arg| arg.to_string_lossy().to_string())
            .collect()
    }

    #[test]
    fn arbitrary_rotation_auto_crops_before_user_crop() {
        let temp = tempfile::tempdir().unwrap();
        let input = temp.path().join("input.png");
        image::RgbImage::new(400, 300).save(&input).unwrap();
        let mut command = Command::new("convert");
        let retouch = RetouchSettings {
            rotation_degrees: 5.0,
            ..Default::default()
        };

        add_retouch_geometry_args(&mut command, &input, Some(&retouch)).unwrap();

        let args = command_args(&command);
        assert_eq!(
            &args[..8],
            [
                "-background",
                "black",
                "-rotate",
                "5",
                "-gravity",
                "Center",
                "-crop",
                "378x268+0+0",
            ]
        );
        assert_eq!(&args[8..], ["+repage", "-gravity", "NorthWest"]);
    }

    #[test]
    fn jpeg_export_args_force_8bit_quality_and_sampling() {
        let mut command = Command::new("convert");
        let mut export = export_options();
        export.long_edge = Some(2048);
        export.strip_metadata = true;
        export.progressive_jpeg = true;

        add_final_convert_args(&mut command, Path::new("out.jpg"), &export).unwrap();

        let args = command_args(&command);
        assert_eq!(
            args,
            [
                "-resize",
                "2048x2048>",
                "-strip",
                "-interlace",
                "Line",
                "-depth",
                "8",
                "-sampling-factor",
                "2x1,1x1,1x1",
                "-quality",
                "91",
                "out.jpg",
            ]
        );
    }

    #[test]
    fn tiff_export_keeps_depth_compresses_and_accepts_structured_resize() {
        let mut command = Command::new("convert");
        let mut export = export_options();
        export.max_width = Some(3000);
        export.max_height = Some(2000);
        export.strip_metadata = true;

        add_final_convert_args(&mut command, Path::new("out.tif"), &export).unwrap();

        assert_eq!(
            command_args(&command),
            [
                "-resize",
                "3000x2000>",
                "-strip",
                "-compress",
                "Zip",
                "out.tif"
            ]
        );
    }

    #[test]
    fn detects_threads_resource_from_convert_listing_output() {
        let listing = r#"
Resource limits:
  Width: 4096
  Height: 4096
  Area: 512M
  Threads: 16
  Time: 2.0
"#;
        assert!(is_threads_present_in_resource_output(listing));
    }

    #[test]
    fn ignores_threads_resource_if_not_present() {
        let listing = r#"
Resource limits:
  Width: 4096
  Height: 4096
  Area: 512M
"#;
        assert!(!is_threads_present_in_resource_output(listing));
    }

    #[test]
    fn validate_export_options_rejects_ambiguous_or_zero_resize() {
        let mut export = export_options();
        export.resize = Some("3000x3000>".to_string());
        export.long_edge = Some(3000);
        assert!(validate_export_options(&export).is_err());

        let mut export = export_options();
        export.max_width = Some(0);
        assert!(validate_export_options(&export).is_err());

        let mut export = export_options();
        export.resize = Some(String::new());
        assert!(validate_export_options(&export).is_err());
    }

    #[test]
    fn output_extension_validation_is_case_insensitive() {
        assert_eq!(output_ext(Path::new("x.JPG")).unwrap(), "jpg");
        assert!(validate_output_format(Path::new("x.TIFF")).is_ok());
        assert!(validate_output_format(Path::new("x.png")).is_err());
        assert!(output_ext(Path::new("no-extension")).is_err());
    }
}