heifer-cli 0.3.27

Convert HEIC/HEIF photos to and from PNG, JPEG and WebP, in pure Rust
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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
//! `heifer`: converts HEIC/HEIF photos to and from PNG, JPEG, WebP and other formats.

use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::time::Instant;

use heifer::image_crate::HeifEncoder;
use heifer::{EncodeFormat, EncodeOptions};
use image::codecs::jpeg::JpegEncoder;
use image::codecs::png::PngEncoder;
use image::codecs::webp::WebPEncoder;
use image::metadata::Orientation;
use image::{
    DynamicImage, ImageBuffer, ImageDecoder, ImageEncoder, ImageFormat, ImageReader, Luma, Rgba,
};

const USAGE: &str = "\
heifer: convert HEIC/HEIF photos, in pure Rust

Usage:
  heifer <input> <output> [-q <quality>]        convert one file
  heifer <inputs>... --to <format> [-o <dir>] [-q <quality>]
                                                 convert many files
  heifer info <files>...                         show size and metadata

The formats come from the file extensions: heic (or heif), png, jpg, webp, tiff, bmp, gif.
EXIF and ICC profiles are kept, and photos are written upright.

Options:
  -q, --quality <0-100>  quality of HEIC and JPEG output (default: 80 for HEIC, 90 for JPEG)
      --format <mode>    HEIC coding: 420 (default), 444 or lossless
      --lossless         same as --format lossless
      --aux              also write every auxiliary image as PNG
      --depth            also write the depth map as PNG
      --gain-map         also write the HDR gain map as PNG
  -t, --to <format>      output format when converting several files
  -o, --out-dir <dir>    output folder (default: next to each input)
  -f, --force            overwrite existing files
  -h, --help             show this help
  -V, --version          show the version

Auxiliary images are written next to the output, for example photo.depth.png.

Examples:
  heifer IMG_1234.heic photo.jpg
  heifer IMG_1234.heic photo.jpg --depth
  heifer portrait.heic portrait.png --aux
  heifer *.HEIC --to jpg -o converted --gain-map
  heifer screenshot.png screenshot.heic -q 70
  heifer screenshot.png screenshot.heic --lossless
  heifer info IMG_1234.heic";

/// What the command line asks for.
#[derive(Debug, Default, PartialEq)]
struct Args {
    info: bool,
    inputs: Vec<PathBuf>,
    output: Option<PathBuf>,
    to: Option<String>,
    out_dir: Option<PathBuf>,
    quality: Option<u8>,
    format: Option<EncodeFormat>,
    aux: bool,
    depth: bool,
    gain_map: bool,
    force: bool,
}

impl Args {
    /// Whether an auxiliary image should be written next to the output.
    fn wants_aux(&self) -> bool {
        self.aux || self.depth || self.gain_map
    }
}

/// HEIC coding mode named on the command line.
fn parse_format(name: &str) -> Result<EncodeFormat, String> {
    match name.to_lowercase().as_str() {
        "420" | "420p" | "yuv420" | "4:2:0" => Ok(EncodeFormat::Ycbcr420),
        "444" | "yuv444" | "4:4:4" => Ok(EncodeFormat::Ycbcr444),
        "lossless" | "rgb" => Ok(EncodeFormat::Lossless),
        _ => Err(format!(
            "unknown HEIC format `{name}`: use 420, 444 or lossless"
        )),
    }
}

/// Records the coding mode, rejecting a `--format` and a `--lossless` that disagree.
fn set_format(args: &mut Args, format: EncodeFormat) -> Result<(), String> {
    match args.format {
        Some(current) if current != format => {
            Err("--format and --lossless ask for different codings".into())
        }
        _ => {
            args.format = Some(format);
            Ok(())
        }
    }
}

fn parse_args(raw: impl IntoIterator<Item = String>) -> Result<Option<Args>, String> {
    let mut args = Args::default();
    let mut positional = Vec::new();
    let mut raw = raw.into_iter();
    while let Some(arg) = raw.next() {
        let mut value = |name: &str| raw.next().ok_or(format!("{name} needs a value"));
        match arg.as_str() {
            "-h" | "--help" => return Ok(None),
            "-V" | "--version" => {
                println!("heifer {}", env!("CARGO_PKG_VERSION"));
                std::process::exit(0);
            }
            "-q" | "--quality" => {
                let q = value(&arg)?;
                args.quality = Some(
                    q.parse::<u8>()
                        .ok()
                        .filter(|&q| q <= 100)
                        .ok_or(format!("quality must be 0 to 100, not `{q}`"))?,
                );
            }
            "-t" | "--to" => args.to = Some(value(&arg)?.trim_start_matches('.').to_lowercase()),
            "--format" => {
                let format = parse_format(&value(&arg)?)?;
                set_format(&mut args, format)?;
            }
            "--lossless" => set_format(&mut args, EncodeFormat::Lossless)?,
            "--aux" => args.aux = true,
            "--depth" => args.depth = true,
            "--gain-map" => args.gain_map = true,
            "-o" | "--out-dir" => args.out_dir = Some(PathBuf::from(value(&arg)?)),
            "-f" | "--force" => args.force = true,
            s if s.starts_with('-') && s.len() > 1 => return Err(format!("unknown option `{s}`")),
            _ => positional.push(PathBuf::from(arg)),
        }
    }
    if positional.first().is_some_and(|p| p.as_os_str() == "info") {
        positional.remove(0);
        args.info = true;
    } else if args.to.is_none() && args.out_dir.is_none() && positional.len() == 2 {
        args.output = positional.pop();
    } else if args.to.is_none() && !positional.is_empty() {
        return Err("give an output file, or `--to <format>` for several files".into());
    }
    if positional.is_empty() {
        return Ok(None);
    }
    args.inputs = positional;
    Ok(Some(args))
}

fn main() -> ExitCode {
    let args = match parse_args(std::env::args().skip(1)) {
        Ok(Some(args)) => args,
        Ok(None) => {
            println!("{USAGE}");
            return ExitCode::SUCCESS;
        }
        Err(e) => {
            eprintln!("error: {e}\n\nRun `heifer --help` for usage.");
            return ExitCode::from(2);
        }
    };
    heifer::image_crate::register_image_decoder_hooks();

    let mut failed = 0;
    for input in &args.inputs {
        let result = if args.info {
            info(input)
        } else {
            output_path(input, &args).and_then(|out| convert(input, &out, &args))
        };
        if let Err(e) = result {
            eprintln!("{}: {e}", input.display());
            failed += 1;
        }
    }
    if failed > 0 {
        if args.inputs.len() > 1 {
            eprintln!("{failed} of {} files failed", args.inputs.len());
        }
        return ExitCode::FAILURE;
    }
    ExitCode::SUCCESS
}

fn output_path(input: &Path, args: &Args) -> Result<PathBuf, String> {
    if let Some(out) = &args.output {
        return Ok(out.clone());
    }
    let to = args.to.as_deref().ok_or("no output format")?;
    let stem = input.file_stem().ok_or("not a file name")?;
    let dir = match &args.out_dir {
        Some(dir) => {
            std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
            dir.clone()
        }
        None => input.parent().map(Path::to_path_buf).unwrap_or_default(),
    };
    Ok(dir.join(stem).with_extension(to))
}

fn is_heif(path: &Path) -> bool {
    path.extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| matches!(e.to_lowercase().as_str(), "heic" | "heif" | "hif"))
}

/// A decoded image, upright, with its ICC profile and EXIF data.
type Decoded = (DynamicImage, Option<Vec<u8>>, Option<Vec<u8>>);

/// Decodes any supported file.
fn read(input: &Path) -> Result<Decoded, String> {
    let reader = ImageReader::open(input)
        .and_then(ImageReader::with_guessed_format)
        .map_err(|e| e.to_string())?;
    let mut decoder = reader.into_decoder().map_err(|e| e.to_string())?;
    let icc = decoder.icc_profile().ok().flatten();
    let mut exif = decoder.exif_metadata().ok().flatten();
    let orientation = decoder.orientation().unwrap_or(Orientation::NoTransforms);
    let mut image = DynamicImage::from_decoder(decoder).map_err(|e| e.to_string())?;
    image.apply_orientation(orientation);
    // The pixels are upright now: no viewer must rotate them again.
    if let Some(exif) = &mut exif {
        let _ = Orientation::remove_from_exif_chunk(exif);
    }
    Ok((image, icc, exif))
}

/// Rejects a HEIC coding mode when the output is not a HEIC file.
fn check_heic_only(args: &Args, output: &Path) -> Result<(), String> {
    if !is_heif(output) && args.format.is_some() {
        return Err("the HEIC coding mode only applies to HEIC output".into());
    }
    Ok(())
}

fn convert(input: &Path, output: &Path, args: &Args) -> Result<(), String> {
    check_heic_only(args, output)?;
    if args.wants_aux() && !is_heif(input) {
        return Err("auxiliary images can only be extracted from HEIC/HEIF input".into());
    }
    if output.exists() && !args.force {
        return Err(format!(
            "{} exists (use --force to overwrite)",
            output.display()
        ));
    }
    let start = Instant::now();
    let aux_bytes = if args.wants_aux() {
        Some(std::fs::read(input).map_err(|e| e.to_string())?)
    } else {
        None
    };
    let (image, icc, exif) = read(input)?;
    let file = std::fs::File::create(output).map_err(|e| format!("{}: {e}", output.display()))?;
    let mut writer = std::io::BufWriter::new(file);

    // Encoders that keep the metadata, for the formats that store it; others through `image`.
    let format = if is_heif(output) {
        None
    } else {
        Some(
            ImageFormat::from_path(output)
                .map_err(|_| format!("unknown output format for {}", output.display()))?,
        )
    };
    let result = match format {
        None => {
            let options = EncodeOptions {
                quality: args.quality.unwrap_or(80),
                ..Default::default()
            };
            write(
                HeifEncoder::with_format(&mut writer, options, args.format.unwrap_or_default()),
                &image,
                icc,
                exif,
            )
        }
        Some(ImageFormat::Jpeg) => {
            // JPEG has no alpha channel.
            let rgb = DynamicImage::ImageRgb8(image.to_rgb8());
            let encoder = JpegEncoder::new_with_quality(&mut writer, args.quality.unwrap_or(90));
            write(encoder, &rgb, icc, exif)
        }
        Some(ImageFormat::Png) => write(PngEncoder::new(&mut writer), &image, icc, exif),
        Some(ImageFormat::WebP) => {
            let image = if image.color().has_alpha() {
                DynamicImage::ImageRgba8(image.to_rgba8())
            } else {
                DynamicImage::ImageRgb8(image.to_rgb8())
            };
            write(WebPEncoder::new_lossless(&mut writer), &image, icc, exif)
        }
        Some(format) => image
            .write_to(&mut writer, format)
            .map_err(|e| e.to_string()),
    };
    let result =
        result.and_then(|()| std::io::Write::flush(&mut writer).map_err(|e| e.to_string()));
    if let Err(e) = result {
        drop(writer);
        let _ = std::fs::remove_file(output); // no truncated file left behind
        return Err(e);
    }
    let size = std::fs::metadata(output).map(|m| m.len()).unwrap_or(0);
    println!(
        "{} → {} ({}×{}, {}, {:.2?})",
        input.display(),
        output.display(),
        image.width(),
        image.height(),
        human(size),
        start.elapsed()
    );
    if let Some(bytes) = &aux_bytes {
        write_auxiliary(output, bytes, args)?;
    }
    Ok(())
}

/// Writes the auxiliary images selected by `--aux`, `--depth` and `--gain-map`.
fn write_auxiliary(output: &Path, bytes: &[u8], args: &Args) -> Result<(), String> {
    let auxiliary = heifer::auxiliary_images(bytes).map_err(|e| e.to_string())?;
    // An explicit selector must find the type it asks for; `--aux` alone is happy with none.
    if args.depth && !auxiliary.iter().any(heifer::AuxiliaryImage::is_depth) {
        return Err("no depth map in this file".into());
    }
    if args.gain_map && !auxiliary.iter().any(heifer::AuxiliaryImage::is_gain_map) {
        return Err("no HDR gain map in this file".into());
    }
    let mut seen = Vec::new();
    let selected: Vec<_> = auxiliary
        .into_iter()
        .filter(|aux| {
            args.aux || (args.depth && aux.is_depth()) || (args.gain_map && aux.is_gain_map())
        })
        .map(|aux| {
            let path = auxiliary_path(output, &aux, &mut seen);
            (path, aux)
        })
        .collect();
    // Check every target before writing any, so that a conflict leaves no partial output.
    for (path, _) in &selected {
        if path.exists() && !args.force {
            return Err(format!(
                "{} exists (use --force to overwrite)",
                path.display()
            ));
        }
    }
    for (path, aux) in selected {
        let image = heifer::decode_auxiliary(bytes, aux.id).map_err(|e| e.to_string())?;
        write_auxiliary_png(&path, &aux, &image)?;
        println!(
            "  {} ({} {}×{}, {}-bit)",
            path.display(),
            auxiliary_kind(&aux),
            aux.width,
            aux.height,
            aux.bit_depth
        );
    }
    Ok(())
}

/// Path of an auxiliary image next to the main output: `photo.depth.png`, `photo.gain-map.png`,
/// then `photo.depth-<id>.png` for a second image of the same kind.
fn auxiliary_path(
    output: &Path,
    aux: &heifer::AuxiliaryImage,
    seen: &mut Vec<(&'static str, usize)>,
) -> PathBuf {
    let kind = auxiliary_slug(aux);
    let number = match seen.iter_mut().find(|(name, _)| *name == kind) {
        Some((_, number)) => {
            *number += 1;
            *number
        }
        None => {
            seen.push((kind, 1));
            1
        }
    };
    let base = output.with_extension("");
    let stem = base
        .file_name()
        .map(|name| name.to_string_lossy())
        .unwrap_or_else(|| "image".into());
    let name = if number == 1 {
        format!("{stem}.{kind}.png")
    } else {
        format!("{stem}.{kind}-{}.png", aux.id)
    };
    base.with_file_name(name)
}

fn auxiliary_slug(aux: &heifer::AuxiliaryImage) -> &'static str {
    if aux.is_depth() {
        "depth"
    } else if aux.is_gain_map() {
        "gain-map"
    } else if aux.is_alpha() {
        "alpha"
    } else {
        "aux"
    }
}

fn auxiliary_kind(aux: &heifer::AuxiliaryImage) -> &'static str {
    if aux.is_depth() {
        "depth map"
    } else if aux.is_gain_map() {
        "HDR gain map"
    } else if aux.is_alpha() {
        "alpha plane"
    } else {
        "auxiliary image"
    }
}

/// Writes a decoded auxiliary image as PNG: grayscale for depth, alpha and gain maps, RGBA for
/// other refinements, at the coded bit depth. Samples below 8 bits are widened to 8, above to 16,
/// so that the PNG viewer sees the full range.
fn write_auxiliary_png(
    path: &Path,
    aux: &heifer::AuxiliaryImage,
    image: &heifer::Image,
) -> Result<(), String> {
    let file = std::fs::File::create(path).map_err(|e| format!("{}: {e}", path.display()))?;
    let mut writer = std::io::BufWriter::new(file);
    let grayscale = aux.is_depth() || aux.is_gain_map() || aux.is_alpha();
    let width = image.width;
    let height = image.height;
    let max = u32::from(image.max_value());
    let to_8 = |value: u16| ((u32::from(value) * 255 + max / 2) / max) as u8;
    let to_16 = |value: u16| ((u32::from(value) * 65535 + max / 2) / max) as u16;
    let invalid = || format!("{}: invalid image size", path.display());
    let png = match (grayscale, image.bit_depth) {
        (true, 0..=8) => {
            let data = image
                .data
                .chunks_exact(4)
                .map(|pixel| to_8(pixel[0]))
                .collect();
            DynamicImage::ImageLuma8(
                ImageBuffer::<Luma<u8>, _>::from_raw(width, height, data).ok_or_else(invalid)?,
            )
        }
        (true, _) => {
            let data = image
                .data
                .chunks_exact(4)
                .map(|pixel| to_16(pixel[0]))
                .collect();
            DynamicImage::ImageLuma16(
                ImageBuffer::<Luma<u16>, _>::from_raw(width, height, data).ok_or_else(invalid)?,
            )
        }
        (false, 0..=8) => {
            let data = image
                .data
                .chunks_exact(4)
                .flat_map(|pixel| {
                    [
                        to_8(pixel[0]),
                        to_8(pixel[1]),
                        to_8(pixel[2]),
                        to_8(pixel[3]),
                    ]
                })
                .collect();
            DynamicImage::ImageRgba8(
                ImageBuffer::<Rgba<u8>, _>::from_raw(width, height, data).ok_or_else(invalid)?,
            )
        }
        (false, _) => {
            let data = image
                .data
                .chunks_exact(4)
                .flat_map(|pixel| {
                    [
                        to_16(pixel[0]),
                        to_16(pixel[1]),
                        to_16(pixel[2]),
                        to_16(pixel[3]),
                    ]
                })
                .collect();
            DynamicImage::ImageRgba16(
                ImageBuffer::<Rgba<u16>, _>::from_raw(width, height, data).ok_or_else(invalid)?,
            )
        }
    };
    let result = png
        .write_to(&mut writer, ImageFormat::Png)
        .map_err(|error| error.to_string())
        .and_then(|()| std::io::Write::flush(&mut writer).map_err(|error| error.to_string()));
    if let Err(error) = result {
        drop(writer);
        let _ = std::fs::remove_file(path); // no truncated file left behind
        return Err(format!("{}: {error}", path.display()));
    }
    Ok(())
}

fn write(
    mut encoder: impl ImageEncoder,
    image: &DynamicImage,
    icc: Option<Vec<u8>>,
    exif: Option<Vec<u8>>,
) -> Result<(), String> {
    // Formats without metadata support simply refuse it.
    if let Some(icc) = icc {
        let _ = encoder.set_icc_profile(icc);
    }
    if let Some(exif) = exif {
        let _ = encoder.set_exif_metadata(exif);
    }
    image.write_with_encoder(encoder).map_err(|e| e.to_string())
}

fn info(input: &Path) -> Result<(), String> {
    for line in info_lines(input)? {
        println!("{line}");
    }
    Ok(())
}

/// The lines printed by `heifer info`, kept separate so that tests can read them.
fn info_lines(input: &Path) -> Result<Vec<String>, String> {
    let bytes = std::fs::read(input).map_err(|e| e.to_string())?;
    let mut lines = vec![input.display().to_string()];
    if !bytes.get(4..8).is_some_and(|b| b == b"ftyp") {
        let (image, _, _) = read(input)?;
        lines.push(format!(
            "  {}×{}, {:?}",
            image.width(),
            image.height(),
            image.color()
        ));
        return Ok(lines);
    }
    let file = heifer::heifer_isobmff::HeifFile::parse(&bytes).map_err(|e| e.to_string())?;
    let image = heifer::decode(&bytes).map_err(|e| e.to_string())?;
    let primary = file.item(file.primary_id).map_err(|e| e.to_string())?;
    let layout = match file.grid(file.primary_id) {
        Ok(g) => format!("grid of {}×{} tiles", g.columns, g.rows),
        Err(_) => format!("single `{}` image", primary.item_type),
    };
    lines.push(format!(
        "  {}×{}, {}-bit{}, {layout}, {}",
        image.width,
        image.height,
        image.bit_depth,
        if image.has_alpha { ", alpha" } else { "" },
        human(bytes.len() as u64)
    ));
    for thumb in file.referencing_items(file.primary_id, b"thmb") {
        if let Ok(Some((w, h))) = file.image_size(thumb) {
            lines.push(format!("  thumbnail {w}×{h}"));
        }
    }
    let meta = heifer::read_metadata(&bytes).map_err(|e| e.to_string())?;
    let exif = meta.exif_fields();
    let headroom = exif.as_ref().and_then(|exif| exif.apple_hdr_headroom());
    for aux in heifer::auxiliary_images(&bytes).unwrap_or_default() {
        let size = if aux.width == 0 || aux.height == 0 {
            "unknown size".to_string()
        } else {
            format!("{}×{}", aux.width, aux.height)
        };
        lines.push(format!(
            "  {} {}, {}-bit, {}",
            auxiliary_kind(&aux),
            size,
            aux.bit_depth,
            aux.urn
        ));
        if aux.is_depth()
            && let Ok(Some(info)) = heifer::DepthInfo::read(&bytes, aux.id)
        {
            let mut parts = Vec::new();
            if let Some(value) = info.z_near {
                parts.push(format!("z-near {value:.6}"));
            }
            if let Some(value) = info.z_far {
                parts.push(format!("z-far {value:.6}"));
            }
            if let Some(value) = info.d_min {
                parts.push(format!("d-min {value:.6}"));
            }
            if let Some(value) = info.d_max {
                parts.push(format!("d-max {value:.6}"));
            }
            parts.push(info.representation.to_string());
            if let Some(view) = info.disparity_reference_view {
                parts.push(format!("reference view {view}"));
            }
            lines.push(format!("    depth metadata: {}", parts.join(", ")));
        }
        if aux.is_gain_map()
            && let Some(headroom) = headroom
        {
            lines.push(format!("    gain map metadata: headroom {headroom:.1}×"));
        }
    }
    if let Some(exif) = exif {
        let mut parts = Vec::new();
        if let (Some(make), Some(model)) = (exif.make(), exif.model()) {
            parts.push(format!("{make} {model}"));
        }
        if let Some(date) = exif.date_time() {
            parts.push(date.to_string());
        }
        if exif.has_gps() {
            parts.push("GPS position".into());
        }
        if !parts.is_empty() {
            lines.push(format!("  {}", parts.join(" · ")));
        }
    }
    let has_iptc = file.items.iter().any(|item| item.item_type.0 == *b"iptc");
    let present: Vec<&str> = [
        (meta.exif.is_some(), "EXIF"),
        (meta.xmp.is_some(), "XMP"),
        (meta.icc_profile.is_some(), "ICC profile"),
        (has_iptc, "IPTC"),
    ]
    .into_iter()
    .filter_map(|(has, name)| has.then_some(name))
    .collect();
    if !present.is_empty() {
        lines.push(format!("  metadata: {}", present.join(", ")));
    }
    Ok(lines)
}

fn human(bytes: u64) -> String {
    match bytes {
        0..1_000 => format!("{bytes} B"),
        1_000..1_000_000 => format!("{:.0} KB", bytes as f64 / 1e3),
        _ => format!("{:.1} MB", bytes as f64 / 1e6),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU32, Ordering};

    fn parse(s: &str) -> Result<Option<Args>, String> {
        parse_args(s.split_whitespace().map(String::from))
    }

    fn fixture(name: &str) -> PathBuf {
        Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../../tests/fixtures")
            .join(name)
    }

    /// A unique folder under the system temporary directory, removed by the caller.
    fn temp_dir(tag: &str) -> PathBuf {
        static NEXT: AtomicU32 = AtomicU32::new(0);
        let number = NEXT.fetch_add(1, Ordering::Relaxed);
        let dir =
            std::env::temp_dir().join(format!("heifer-cli-{}-{tag}-{number}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn single_conversion() {
        let a = parse("in.heic out.jpg -q 70").unwrap().unwrap();
        assert_eq!(a.inputs, [PathBuf::from("in.heic")]);
        assert_eq!(a.output, Some(PathBuf::from("out.jpg")));
        assert_eq!(a.quality, Some(70));
    }

    #[test]
    fn batch_conversion() {
        let a = parse("a.heic b.heic c.heic --to .JPG -o out")
            .unwrap()
            .unwrap();
        assert_eq!(a.inputs.len(), 3);
        assert_eq!(a.to.as_deref(), Some("jpg"));
        assert_eq!(
            output_path(Path::new("x/a.heic"), &a).ok(),
            Some(PathBuf::from("out/a.jpg"))
        );
        let a = parse("dir/a.heic --to png").unwrap().unwrap();
        assert_eq!(
            output_path(&a.inputs[0], &a).ok(),
            Some(PathBuf::from("dir/a.png"))
        );
    }

    #[test]
    fn heic_coding_modes() {
        let format = |s: &str| parse(s).unwrap().unwrap().format;
        assert_eq!(
            format("a.png b.heic --format 444"),
            Some(EncodeFormat::Ycbcr444)
        );
        assert_eq!(
            format("a.png b.heic --format 4:4:4"),
            Some(EncodeFormat::Ycbcr444)
        );
        assert_eq!(
            format("a.png b.heic --format lossless"),
            Some(EncodeFormat::Lossless)
        );
        assert_eq!(
            format("a.png b.heic --lossless"),
            Some(EncodeFormat::Lossless)
        );
        assert_eq!(format("a.png b.heic"), None, "the default stays 4:2:0");
        assert!(parse("a.png b.heic --format 422").is_err());
        assert!(parse("a.png b.heic --format 444 --lossless").is_err());

        let args = parse("a.png b.heic --lossless").unwrap().unwrap();
        assert!(check_heic_only(&args, Path::new("b.heic")).is_ok());
        assert!(check_heic_only(&args, Path::new("b.png")).is_err());
    }

    #[test]
    fn info_and_errors() {
        assert!(parse("info a.heic b.heic").unwrap().unwrap().info);
        assert_eq!(parse("").unwrap(), None);
        assert_eq!(parse("--help").unwrap(), None);
        assert!(parse("a.heic").is_err());
        assert!(parse("a b c").is_err());
        assert!(parse("a b -q 101").is_err());
        assert!(parse("a b --bogus").is_err());
    }

    #[test]
    fn auxiliary_flags_are_parsed() {
        let all = parse("in.heic out.png --aux").unwrap().unwrap();
        assert!(all.aux && !all.depth && !all.gain_map);
        assert!(all.wants_aux());
        let depth = parse("in.heic out.png --depth").unwrap().unwrap();
        assert!(depth.depth && !depth.aux && depth.wants_aux());
        let gain = parse("in.heic out.png --gain-map").unwrap().unwrap();
        assert!(gain.gain_map && !gain.aux && gain.wants_aux());
        assert!(!parse("in.heic out.png").unwrap().unwrap().wants_aux());
    }

    #[test]
    fn info_describes_a_portrait() {
        let path = fixture("real/heif_other__pug.heic");
        if !path.exists() {
            eprintln!("skipping: {} not found", path.display());
            return;
        }
        let lines = info_lines(&path).unwrap();
        let joined = lines.join("\n");
        assert!(
            joined.contains("depth map 768×576, 8-bit, urn:mpeg:hevc:2015:auxid:2"),
            "{joined}"
        );
        assert!(
            joined.contains("d-min 1.498047") && joined.contains("d-max 4.382812"),
            "{joined}"
        );
        assert!(joined.contains("uniform disparity"), "{joined}");
        assert!(joined.contains("reference view 0"), "{joined}");
        assert!(
            joined
                .contains("HDR gain map 2016×1512, 8-bit, urn:com:apple:photo:2020:aux:hdrgainmap"),
            "{joined}"
        );
    }

    #[test]
    fn info_describes_an_unci_depth_map() {
        let path = fixture("real/heif_special__unsupported_depth_image.heic");
        if !path.exists() {
            eprintln!("skipping: {} not found", path.display());
            return;
        }
        let lines = info_lines(&path).unwrap();
        let joined = lines.join("\n");
        assert!(
            joined.contains("depth map 64×32, 8-bit, urn:mpeg:hevc:2015:auxid:2"),
            "{joined}"
        );
        assert!(!joined.contains("depth metadata"), "{joined}");
    }

    #[test]
    fn info_reports_iptc() {
        let path = fixture("real/heif_other__L_exif_xmp_iptc.heic");
        if !path.exists() {
            eprintln!("skipping: {} not found", path.display());
            return;
        }
        let lines = info_lines(&path).unwrap();
        let joined = lines.join("\n");
        assert!(joined.contains("metadata: EXIF, XMP, IPTC"), "{joined}");
    }

    #[test]
    fn extracts_every_auxiliary_image() {
        let path = fixture("real/heif_other__pug.heic");
        if !path.exists() {
            eprintln!("skipping: {} not found", path.display());
            return;
        }
        let bytes = std::fs::read(&path).unwrap();
        let dir = temp_dir("aux");
        let output = dir.join("photo.png");
        let mut args = parse("in.heic out.png --aux").unwrap().unwrap();
        write_auxiliary(&output, &bytes, &args).unwrap();

        let depth = dir.join("photo.depth.png");
        let gain = dir.join("photo.gain-map.png");
        assert!(depth.exists() && gain.exists());
        let depth_image = image::open(&depth).unwrap();
        assert_eq!((depth_image.width(), depth_image.height()), (768, 576));
        assert_eq!(depth_image.color(), image::ColorType::L8);
        let gain_image = image::open(&gain).unwrap();
        assert_eq!((gain_image.width(), gain_image.height()), (2016, 1512));
        assert_eq!(gain_image.color(), image::ColorType::L8);

        // Existing files are kept unless --force is given.
        let error = write_auxiliary(&output, &bytes, &args).unwrap_err();
        assert!(error.contains("exists"), "{error}");
        args.force = true;
        write_auxiliary(&output, &bytes, &args).unwrap();

        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn missing_auxiliary_types_are_errors() {
        let unci = fixture("real/heif_special__unsupported_depth_image.heic");
        let alpha = fixture("alpha.heic");
        let plain = fixture("single_image.heic");
        if !unci.exists() || !alpha.exists() || !plain.exists() {
            eprintln!("skipping: fixtures not found");
            return;
        }
        let dir = temp_dir("missing");
        let output = dir.join("photo.png");

        let bytes = std::fs::read(&unci).unwrap();
        let args = parse("in.heic out.png --gain-map").unwrap().unwrap();
        let error = write_auxiliary(&output, &bytes, &args).unwrap_err();
        assert!(error.contains("no HDR gain map"), "{error}");

        let bytes = std::fs::read(&alpha).unwrap();
        let args = parse("in.heic out.png --depth").unwrap().unwrap();
        let error = write_auxiliary(&output, &bytes, &args).unwrap_err();
        assert!(error.contains("no depth map"), "{error}");

        // --aux alone is fine with a file that has no auxiliary image at all.
        let bytes = std::fs::read(&plain).unwrap();
        let args = parse("in.heic out.png --aux").unwrap().unwrap();
        assert!(write_auxiliary(&output, &bytes, &args).is_ok());

        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn widens_10_bit_auxiliary_images_to_16_bit_png() {
        let path = fixture("real/heif__RGBA_10__128x128.heif");
        if !path.exists() {
            eprintln!("skipping: {} not found", path.display());
            return;
        }
        let bytes = std::fs::read(&path).unwrap();
        let dir = temp_dir("alpha10");
        let output = dir.join("photo.png");
        let args = parse("in.heif out.png --aux").unwrap().unwrap();
        write_auxiliary(&output, &bytes, &args).unwrap();

        let alpha = image::open(dir.join("photo.alpha.png")).unwrap();
        assert_eq!(alpha.color(), image::ColorType::L16);
        assert_eq!((alpha.width(), alpha.height()), (128, 128));
        assert!(
            alpha.to_luma16().iter().any(|&value| value > 1023),
            "10-bit samples are widened to the 16-bit PNG range"
        );

        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn auxiliary_output_requires_a_heif_input() {
        let dir = temp_dir("non-heif");
        let args = parse("in.png out.png --depth").unwrap().unwrap();
        let error = convert(&dir.join("in.png"), &dir.join("out.png"), &args).unwrap_err();
        assert!(
            error.contains("only be extracted from HEIC/HEIF"),
            "{error}"
        );
        std::fs::remove_dir_all(&dir).unwrap();
    }
}