mini-film 4.1.2

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
use std::{
    borrow::Cow,
    fs::{self, File},
    io::BufWriter,
    path::Path,
};

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

use crate::model::{BatchSummary, ConvertedProfile, HaldOptions, RgbTable, XmpRgbTable};

/// Convert one XMP file or a directory tree of XMP files to Hald PNGs.
///
/// The function validates the requested Hald level once, creates the output
/// directory for batch conversion when needed, and dispatches to either the
/// directory walker or the single-file converter. It is the public library
/// entrypoint used by the CLI `hald` command.
pub fn convert_path(
    input: &Path,
    output: &Path,
    options: HaldOptions,
) -> Result<Vec<ConvertedProfile>> {
    validate_hald_level(options.hald_level)?;

    if input.is_dir() {
        if !options.info_only {
            fs::create_dir_all(output).with_context(|| format!("creating {}", output.display()))?;
        }
        convert_dir(input, output, options)
    } else {
        Ok(vec![convert_xmp_to_hald(input, output, options)?])
    }
}

/// Convert every `.xmp` file under a directory and fail on the first error.
///
/// Each input path is mapped to a matching relative output path, with the file
/// stem sanitized and `.hald.png` appended. This mode is useful when callers
/// want strict conversion semantics and prefer a single error over a partial
/// success report.
pub fn convert_dir(
    input_dir: &Path,
    output_dir: &Path,
    options: HaldOptions,
) -> Result<Vec<ConvertedProfile>> {
    validate_hald_level(options.hald_level)?;

    let mut converted = Vec::new();
    for entry in WalkDir::new(input_dir).into_iter().filter_map(Result::ok) {
        if !entry.file_type().is_file() {
            continue;
        }
        if entry.path().extension().and_then(|s| s.to_str()) != Some("xmp") {
            continue;
        }

        let rel = entry.path().strip_prefix(input_dir).unwrap_or(entry.path());
        let stem = rel
            .file_stem()
            .and_then(|s| s.to_str())
            .map(sanitize_filename::sanitize)
            .unwrap_or(Cow::Borrowed("profile"));
        let parent = rel.parent().unwrap_or_else(|| Path::new(""));
        let out = output_dir.join(parent).join(format!("{stem}.hald.png"));

        converted.push(convert_xmp_to_hald(entry.path(), &out, options)?);
    }

    Ok(converted)
}

/// Convert every `.xmp` file under a directory while collecting failures.
///
/// This mirrors `convert_dir` but records skipped files and continues after
/// individual conversion errors. The CLI uses it for directory conversion so one
/// malformed preset does not prevent the usable RGBTable profiles from being
/// generated.
pub fn try_convert_dir(
    input_dir: &Path,
    output_dir: &Path,
    options: HaldOptions,
) -> Result<(Vec<ConvertedProfile>, BatchSummary)> {
    validate_hald_level(options.hald_level)?;

    let mut converted = Vec::new();
    let mut summary = BatchSummary::default();
    for entry in WalkDir::new(input_dir).into_iter().filter_map(Result::ok) {
        if !entry.file_type().is_file() {
            continue;
        }
        if entry.path().extension().and_then(|s| s.to_str()) != Some("xmp") {
            continue;
        }

        let rel = entry.path().strip_prefix(input_dir).unwrap_or(entry.path());
        let stem = rel
            .file_stem()
            .and_then(|s| s.to_str())
            .map(sanitize_filename::sanitize)
            .unwrap_or(Cow::Borrowed("profile"));
        let parent = rel.parent().unwrap_or_else(|| Path::new(""));
        let out = output_dir.join(parent).join(format!("{stem}.hald.png"));

        match convert_xmp_to_hald(entry.path(), &out, options) {
            Ok(profile) => {
                summary.converted += 1;
                converted.push(profile);
            }
            Err(err) => {
                summary.skipped += 1;
                eprintln!("skip {}: {err:#}", entry.path().display());
            }
        }
    }

    Ok((converted, summary))
}

/// Convert one RGBTable-bearing XMP profile into a generated Hald PNG.
///
/// The converter parses the XMP recipe, extracts the embedded RGBTable, decodes
/// the Adobe base85/zlib payload, parses the binary table, and optionally writes
/// a Hald image. The generated Hald contains only the RGBTable lookup; parsed
/// tone/color/sharpening metadata is returned so callers can hand it to
/// RawTherapee through a generated `.pp3` profile.
pub fn convert_xmp_to_hald(
    input: &Path,
    output: &Path,
    options: HaldOptions,
) -> Result<ConvertedProfile> {
    validate_hald_level(options.hald_level)?;

    let recipe = crate::xmp::extract_film_recipe(input)
        .with_context(|| format!("reading RGBTable from {}", input.display()))?;
    let profile = recipe
        .rgb_table
        .clone()
        .ok_or_else(|| anyhow!("missing crs:RGBTable"))?;
    let decoded = crate::rgb_table::decode_rgb_table(&profile.encoded)
        .with_context(|| format!("decoding table {}", profile.table_id))?;
    let table = crate::rgb_table::parse_rgb_table(&decoded)?;

    if !options.info_only {
        if output.exists() && !options.overwrite {
            bail!("output exists, pass --overwrite: {}", output.display());
        }

        if let Some(parent) = output.parent() {
            fs::create_dir_all(parent).with_context(|| format!("creating {}", parent.display()))?;
        }

        write_hald_png(&table, options.hald_level, output)
            .with_context(|| format!("writing {}", output.display()))?;
    }

    Ok(ConvertedProfile {
        input: input.to_path_buf(),
        output: (!options.info_only).then(|| output.to_path_buf()),
        profile,
        table,
        adjustments: recipe.adjustments,
        sharpening: recipe.sharpening,
    })
}

pub fn profile_display_name(input: &Path, profile: &XmpRgbTable) -> String {
    profile.name.clone().unwrap_or_else(|| {
        input
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("unknown profile")
            .to_string()
    })
}

/// Format a detailed metadata line for a converted profile.
///
/// The output includes human-readable profile identity plus table dimensions,
/// primaries/gamma/gamut, amount range, flags, and markers for adjustments or
/// sharpening that should be forwarded to RawTherapee through generated `.pp3`
/// files. Keeping this centralized makes `hald --info-only` and conversion logs
/// report the same facts.
pub fn profile_info_line(converted: &ConvertedProfile) -> String {
    let display_name = profile_display_name(&converted.input, &converted.profile);
    format!(
        "{}{}{}: dims={} divisions={} primaries={} gamma={} gamut={} amount=[{:.2},{:.2}] flags={:?}{}{}",
        display_name,
        converted
            .profile
            .group
            .as_deref()
            .map(|group| format!(" [{group}]"))
            .unwrap_or_default(),
        converted
            .profile
            .uuid
            .as_deref()
            .map(|uuid| format!(" uuid={uuid}"))
            .unwrap_or_default(),
        converted.table.dimensions,
        converted.table.divisions,
        converted.table.primaries,
        converted.table.gamma,
        converted.table.gamut,
        converted.table.min_amount,
        converted.table.max_amount,
        converted.table.flags,
        if converted.adjustments.is_default() {
            ""
        } else {
            " adjustments=pp3"
        },
        if converted.sharpening.is_enabled() {
            " sharpening=pp3"
        } else {
            ""
        }
    )
}

pub fn write_hald_png(table: &RgbTable, level: u32, path: &Path) -> Result<()> {
    write_hald_png_with_adjustments(
        table,
        level,
        path,
        &crate::model::ProfileAdjustments::default(),
    )
}

/// Write a 16-bit RGB Hald CLUT PNG from an RGBTable.
///
/// A Hald level defines `axis = level * level` samples per channel and an image
/// side of `level * axis`. The nested b/g/r loops emit pixels in Hald order,
/// sample the RGBTable at each coordinate, and append big-endian 16-bit RGB
/// channels for the PNG encoder. Overflow checks keep impossible levels from
/// allocating invalid buffers. The adjustment argument is retained for API
/// compatibility but intentionally ignored: tone/color settings now belong in
/// generated RawTherapee `.pp3` profiles, not in the Hald.
pub fn write_hald_png_with_adjustments(
    table: &RgbTable,
    level: u32,
    path: &Path,
    _adjustments: &crate::model::ProfileAdjustments,
) -> Result<()> {
    validate_hald_level(level)?;

    let axis = level
        .checked_mul(level)
        .ok_or_else(|| anyhow!("hald level overflow"))?;
    let side = level
        .checked_mul(axis)
        .ok_or_else(|| anyhow!("hald side overflow"))?;
    let pixel_count = (side as usize)
        .checked_mul(side as usize)
        .ok_or_else(|| anyhow!("hald image too large"))?;

    let mut data = Vec::with_capacity(pixel_count * 6);

    for b in 0..axis {
        for g in 0..axis {
            for r in 0..axis {
                let rgb = crate::rgb_table::sample_table(table, r, g, b, axis);
                for channel in rgb {
                    data.extend_from_slice(&channel.to_be_bytes());
                }
            }
        }
    }

    let file = File::create(path)?;
    let writer = BufWriter::new(file);
    let mut encoder = png::Encoder::new(writer, side, side);
    encoder.set_color(png::ColorType::Rgb);
    encoder.set_depth(png::BitDepth::Sixteen);
    encoder.set_compression(png::Compression::High);
    encoder.set_filter(png::Filter::Adaptive);
    let mut png_writer = encoder.write_header()?;
    png_writer.write_image_data(&data)?;
    Ok(())
}

pub(crate) fn validate_hald_level(level: u32) -> Result<()> {
    if level < 2 {
        bail!("--hald-level must be at least 2");
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{ProfileAdjustments, SharpeningSettings};

    fn identity_table(divisions: u32) -> RgbTable {
        let mut samples = Vec::new();
        for r in 0..divisions {
            for g in 0..divisions {
                for b in 0..divisions {
                    let scale =
                        |value: u32| ((value * 65535 + (divisions >> 1)) / (divisions - 1)) as u16;
                    samples.push([scale(r), scale(g), scale(b)]);
                }
            }
        }
        RgbTable {
            dimensions: 3,
            divisions,
            samples,
            primaries: 1,
            gamma: 2,
            gamut: 3,
            min_amount: 0.0,
            max_amount: 2.0,
            flags: Some(7),
        }
    }

    #[test]
    fn validate_hald_level_rejects_degenerate_levels() {
        assert!(validate_hald_level(1).is_err());
        assert!(validate_hald_level(2).is_ok());
    }

    #[test]
    fn profile_display_name_prefers_xmp_name_and_falls_back_to_path() {
        let named = XmpRgbTable {
            name: Some("Named Profile".to_string()),
            group: None,
            uuid: None,
            table_id: "table".to_string(),
            encoded: String::new(),
        };
        assert_eq!(
            profile_display_name(Path::new("/tmp/fallback.xmp"), &named),
            "Named Profile"
        );

        let unnamed = XmpRgbTable {
            name: None,
            group: None,
            uuid: None,
            table_id: "table".to_string(),
            encoded: String::new(),
        };
        assert_eq!(
            profile_display_name(Path::new("/tmp/fallback profile.xmp"), &unnamed),
            "fallback profile"
        );
    }

    #[test]
    fn profile_info_line_marks_adjustments_and_sharpening() {
        let converted = ConvertedProfile {
            input: Path::new("/tmp/profile.xmp").to_path_buf(),
            output: None,
            profile: XmpRgbTable {
                name: Some("Test".to_string()),
                group: Some("Group".to_string()),
                uuid: Some("uuid".to_string()),
                table_id: "table".to_string(),
                encoded: String::new(),
            },
            table: identity_table(2),
            adjustments: ProfileAdjustments {
                exposure: 0.5,
                ..ProfileAdjustments::default()
            },
            sharpening: SharpeningSettings {
                present: true,
                amount: 40.0,
                radius: 1.0,
                detail: 25.0,
                masking: 0.0,
            },
        };

        let line = profile_info_line(&converted);
        assert!(line.contains("Test [Group] uuid=uuid"));
        assert!(line.contains("dims=3 divisions=2"));
        assert!(line.contains("adjustments=pp3"));
        assert!(line.contains("sharpening=pp3"));
    }

    #[test]
    fn write_hald_png_creates_expected_16bit_rgb_dimensions() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("hald.png");
        write_hald_png(&identity_table(2), 2, &path).unwrap();

        let decoder = png::Decoder::new(std::io::BufReader::new(File::open(path).unwrap()));
        let reader = decoder.read_info().unwrap();
        let info = reader.info();
        assert_eq!(info.width, 8);
        assert_eq!(info.height, 8);
        assert_eq!(info.color_type, png::ColorType::Rgb);
        assert_eq!(info.bit_depth, png::BitDepth::Sixteen);
    }

    #[test]
    fn write_hald_png_uses_best_deflate_compression() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("hald.png");
        write_hald_png(&identity_table(2), 2, &path).unwrap();

        let bytes = fs::read(path).unwrap();
        let first_idat = first_png_chunk_data(&bytes, b"IDAT").unwrap();
        let zlib_header_flags = first_idat[1];
        let compression_level = zlib_header_flags >> 6;

        assert_eq!(compression_level, 3);
    }

    fn first_png_chunk_data<'a>(png: &'a [u8], chunk_type: &[u8; 4]) -> Option<&'a [u8]> {
        let mut offset = 8usize;
        while offset.checked_add(12)? <= png.len() {
            let length = u32::from_be_bytes(png[offset..offset + 4].try_into().ok()?) as usize;
            let kind = &png[offset + 4..offset + 8];
            let data_start = offset + 8;
            let data_end = data_start.checked_add(length)?;
            if data_end.checked_add(4)? > png.len() {
                return None;
            }
            if kind == chunk_type {
                return Some(&png[data_start..data_end]);
            }
            offset = data_end + 4;
        }
        None
    }
}