gamut-cli 0.3.1

Sandbox CLI for the gamut codecs: decode PNG/JPEG/PPM/WebP/JXL and encode AVIF/WebP/TIFF/PNG/JXL/JPEG, plus inspection of the shared color/dsp/bitstream primitives (e.g. `gamut convert input.png output.avif`).
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
//! `gamut isobmff` — read and write ISOBMFF/HEIF still-image containers (gamut-isobmff).
//!
//! The container layer is codec-agnostic: the coded bitstream is carried as opaque bytes, so these
//! subcommands exercise the whole `gamut-isobmff` read/write surface without a working AV1/HEVC
//! codec. `inspect` parses a real `.avif`/`.heic` and prints its box structure; `remux` re-serialises
//! a container (proving the read→write round-trip on real files); `build` constructs a synthetic
//! container that exercises every modelled box, property, reference, and group.

use std::path::{Path, PathBuf};

use clap::Subcommand;
use gamut::isobmff::{
    self, ColourInformation, EntityGroup, ImageGrid, IsoBmffImage, Item, ItemReference, NclxColr,
    Property, PropertyKind,
};

use crate::error::CliError;

/// `gamut isobmff` subcommands.
#[derive(Subcommand)]
pub(crate) enum IsobmffCommand {
    /// Parse a still-image container (.avif/.heic) and print its box structure.
    Inspect {
        /// Input `.avif` or `.heic` file.
        input: PathBuf,
    },
    /// Re-mux a container: parse it and write a normalised copy (coded payloads preserved verbatim).
    Remux {
        /// Input `.avif` or `.heic` file.
        input: PathBuf,
        /// Output container file.
        output: PathBuf,
    },
    /// Build a synthetic container exercising the full model, with placeholder coded payloads.
    Build {
        /// Output container file.
        output: PathBuf,
    },
}

/// Runs an `isobmff` subcommand.
pub(crate) fn run(cmd: &IsobmffCommand) -> Result<(), CliError> {
    match cmd {
        IsobmffCommand::Inspect { input } => inspect(input),
        IsobmffCommand::Remux { input, output } => remux(input, output),
        IsobmffCommand::Build { output } => build(output),
    }
}

/// Parses `input` and prints its container structure.
fn inspect(input: &Path) -> Result<(), CliError> {
    let data = read_file(input)?;
    let img = isobmff::read(&data)?;
    print_container(input, data.len(), &img);
    Ok(())
}

/// Parses `input`, re-serialises it to `output`, and verifies the read→write round-trip.
fn remux(input: &Path, output: &Path) -> Result<(), CliError> {
    let data = read_file(input)?;
    let model = isobmff::read(&data)?;
    let bytes = isobmff::write(&model)?;
    // Round-trip guard: re-parsing our own output must reproduce the model exactly.
    if isobmff::read(&bytes)? != model {
        return Err(CliError::Codec(gamut::core::Error::InvalidInput(
            "isobmff remux: re-parsed container did not match the source model",
        )));
    }
    write_file(output, &bytes)?;
    println!(
        "read {} ({} bytes): {} item(s), primary item {}",
        input.display(),
        data.len(),
        model.items.len(),
        model.primary_item_id,
    );
    println!(
        "wrote {} ({} bytes) — normalised container (single-extent mdat, smallest box versions); \
         coded payloads preserved verbatim",
        output.display(),
        bytes.len(),
    );
    println!("round-trip verified: read(write(read(input))) == read(input)");
    Ok(())
}

/// Builds the demonstration container and writes it to `output`.
fn build(output: &Path) -> Result<(), CliError> {
    let img = demo_image()?;
    let bytes = isobmff::write(&img)?;
    write_file(output, &bytes)?;
    println!(
        "wrote {} ({} bytes): {} item(s) exercising the full still-image model, primary item {}",
        output.display(),
        bytes.len(),
        img.items.len(),
        img.primary_item_id,
    );
    println!(
        "note: coded payloads are placeholders — the container is structurally valid but pixels \
         will not decode",
    );
    Ok(())
}

/// Reads a file into memory, attaching the path to any I/O error.
fn read_file(path: &Path) -> Result<Vec<u8>, CliError> {
    std::fs::read(path).map_err(|source| CliError::Io {
        path: path.to_path_buf(),
        source,
    })
}

/// Writes `bytes` to `path`, attaching the path to any I/O error.
fn write_file(path: &Path, bytes: &[u8]) -> Result<(), CliError> {
    std::fs::write(path, bytes).map_err(|source| CliError::Io {
        path: path.to_path_buf(),
        source,
    })
}

// ---- structural printing -------------------------------------------------------------------

/// Prints the full container structure: brands, primary item, each item's properties/references,
/// and the entity groups.
fn print_container(path: &Path, byte_len: usize, img: &IsoBmffImage) {
    println!("container: {} ({} bytes)", path.display(), byte_len);
    println!(
        "  ftyp: {} (minor {}) compatible=[{}]",
        fourcc(&img.major_brand),
        img.minor_version,
        img.compatible_brands
            .iter()
            .map(fourcc)
            .collect::<Vec<_>>()
            .join(", "),
    );
    println!("  primary: item {}", img.primary_item_id);
    println!("  items ({}):", img.items.len());
    for item in &img.items {
        print_item(img, item);
    }
    if img.groups.is_empty() {
        println!("  groups: none");
    } else {
        println!("  groups ({}):", img.groups.len());
        for g in &img.groups {
            println!(
                "    {} #{} -> {:?}",
                fourcc(&g.group_type),
                g.group_id,
                g.entity_ids,
            );
        }
    }
}

/// Prints one item and its properties, grid geometry (if a `grid`), references, and payload size.
fn print_item(img: &IsoBmffImage, item: &Item) {
    let mut tags = Vec::new();
    if item.id == img.primary_item_id {
        tags.push("primary".to_string());
    }
    if item.hidden {
        tags.push("hidden".to_string());
    }
    if !item.name.is_empty() {
        tags.push(format!("name=\"{}\"", item.name));
    }
    if let Some(ct) = &item.content_type {
        let enc = item
            .content_encoding
            .as_deref()
            .map(|e| format!(";{e}"))
            .unwrap_or_default();
        tags.push(format!("mime={ct}{enc}"));
    }
    let suffix = if tags.is_empty() {
        String::new()
    } else {
        format!("  [{}]", tags.join(", "))
    };
    println!(
        "    item {}  {}{}",
        item.id,
        fourcc(&item.item_type),
        suffix
    );

    if item.properties.is_empty() {
        println!("      properties: none");
    } else {
        println!("      properties ({}):", item.properties.len());
        for p in &item.properties {
            println!("        {}", render_property(p));
        }
    }

    if item.item_type == *b"grid" {
        match ImageGrid::parse(&item.payload) {
            Ok(g) => println!(
                "      grid: {}x{} tiles -> {}x{}",
                g.rows, g.columns, g.output_width, g.output_height,
            ),
            Err(e) => println!("      grid: <unparsable payload: {e}>"),
        }
    }

    if item.references.is_empty() {
        println!("      references: none");
    } else {
        println!("      references:");
        for r in &item.references {
            println!(
                "        {} -> {:?}",
                fourcc(&r.reference_type),
                r.to_item_ids
            );
        }
    }
    println!("      payload: {} bytes", item.payload.len());
}

/// Renders one property as a single human-readable line.
fn render_property(p: &Property) -> String {
    let body = match &p.kind {
        PropertyKind::ImageSpatialExtents { width, height } => format!("ispe  {width}x{height}"),
        PropertyKind::PixelInformation { bits_per_channel } => format!(
            "pixi  {} bpc",
            bits_per_channel
                .iter()
                .map(u8::to_string)
                .collect::<Vec<_>>()
                .join("/"),
        ),
        PropertyKind::Colour(ColourInformation::Nclx(n)) => format!(
            "colr  nclx cp={} tc={} mc={} {}",
            n.colour_primaries,
            n.transfer_characteristics,
            n.matrix_coefficients,
            if n.full_range {
                "full-range"
            } else {
                "limited-range"
            },
        ),
        PropertyKind::Colour(ColourInformation::RestrictedIcc(d)) => {
            format!("colr  rICC ({} bytes)", d.len())
        }
        PropertyKind::Colour(ColourInformation::UnrestrictedIcc(d)) => {
            format!("colr  prof ({} bytes)", d.len())
        }
        PropertyKind::Rotation(n) => format!("irot  {n} (x90 ccw)"),
        PropertyKind::Mirror(axis) => format!(
            "imir  axis={axis} ({})",
            if *axis == 0 { "vertical" } else { "horizontal" },
        ),
        PropertyKind::CleanAperture {
            width_n,
            width_d,
            height_n,
            height_d,
            ..
        } => format!("clap  {width_n}/{width_d} x {height_n}/{height_d}"),
        PropertyKind::PixelAspectRatio {
            h_spacing,
            v_spacing,
        } => format!("pasp  {h_spacing}:{v_spacing}"),
        PropertyKind::AuxiliaryType {
            aux_type,
            aux_subtype,
        } => {
            let sub = if aux_subtype.is_empty() {
                String::new()
            } else {
                format!(" (+{} subtype bytes)", aux_subtype.len())
            };
            format!("auxC  {aux_type}{sub}")
        }
        PropertyKind::ContentLightLevel {
            max_content_light_level,
            max_pic_average_light_level,
        } => {
            format!("clli  MaxCLL={max_content_light_level} MaxPALL={max_pic_average_light_level}")
        }
        PropertyKind::CodecConfiguration { kind, data } => {
            format!("{}  codec config ({} bytes)", fourcc(kind), data.len())
        }
        PropertyKind::Other { kind, data } => {
            format!("{}  ({} bytes, carried verbatim)", fourcc(kind), data.len())
        }
        _ => "<unrecognised property>".to_string(),
    };
    if p.essential {
        format!("{body}  [essential]")
    } else {
        body
    }
}

/// Renders a four-character code as its ASCII text, or hex if it is not printable.
fn fourcc(code: &[u8; 4]) -> String {
    if code.iter().all(|b| b.is_ascii_graphic() || *b == b' ') {
        String::from_utf8_lossy(code).into_owned()
    } else {
        format!(
            "0x{:02x}{:02x}{:02x}{:02x}",
            code[0], code[1], code[2], code[3]
        )
    }
}

// ---- the demonstration model ---------------------------------------------------------------

/// Builds a synthetic still-image container that exercises the entire modelled surface: a `grid`
/// primary over two coded tiles, an alpha auxiliary, a thumbnail, Exif and XMP metadata items, all
/// three `colr` forms, every transform property, HDR `clli`, an unrecognised property carried
/// verbatim, every reference type, and an `altr` entity group. Coded payloads are placeholders.
fn demo_image() -> Result<IsoBmffImage, CliError> {
    let av1c = || PropertyKind::CodecConfiguration {
        kind: *b"av1C",
        data: vec![0x81, 0x20, 0x0c, 0x00],
    };
    let ispe = |width, height| PropertyKind::ImageSpatialExtents { width, height };
    let pixi = |bits: &[u8]| PropertyKind::PixelInformation {
        bits_per_channel: bits.to_vec(),
    };
    let essential = |kind| Property {
        essential: true,
        kind,
    };
    let descriptive = |kind| Property {
        essential: false,
        kind,
    };

    // Primary: a grid derived image over two tiles, carrying the transform properties.
    let grid = Item {
        id: 1,
        item_type: *b"grid",
        name: String::new(),
        content_type: None,
        content_encoding: None,
        hidden: false,
        references: vec![
            ItemReference {
                reference_type: *b"dimg",
                to_item_ids: vec![2, 3],
            },
            ItemReference {
                reference_type: *b"prem",
                to_item_ids: vec![4],
            },
        ],
        properties: vec![
            descriptive(ispe(256, 128)),
            essential(PropertyKind::Rotation(1)),
            essential(PropertyKind::Mirror(0)),
            essential(PropertyKind::CleanAperture {
                width_n: 256,
                width_d: 1,
                height_n: 128,
                height_d: 1,
                horiz_off_n: 0,
                horiz_off_d: 1,
                vert_off_n: 0,
                vert_off_d: 1,
            }),
            descriptive(PropertyKind::PixelAspectRatio {
                h_spacing: 1,
                v_spacing: 1,
            }),
        ],
        payload: ImageGrid {
            rows: 1,
            columns: 2,
            output_width: 256,
            output_height: 128,
        }
        .to_bytes()?,
    };

    // Tile A: nclx colour, plus an unrecognised `mdcv` property carried verbatim.
    let tile_a = Item {
        id: 2,
        item_type: *b"av01",
        name: String::new(),
        content_type: None,
        content_encoding: None,
        hidden: true,
        references: vec![],
        properties: vec![
            essential(av1c()),
            descriptive(ispe(128, 128)),
            descriptive(pixi(&[8, 8, 8])),
            descriptive(PropertyKind::Colour(ColourInformation::Nclx(NclxColr {
                colour_primaries: 1,
                transfer_characteristics: 13,
                matrix_coefficients: 6,
                full_range: true,
            }))),
            descriptive(PropertyKind::Other {
                kind: *b"mdcv",
                data: vec![0; 24],
            }),
        ],
        payload: vec![0xde, 0xad, 0xbe, 0xef],
    };

    // Tile B: an unrestricted ICC profile in `colr`.
    let tile_b = Item {
        id: 3,
        item_type: *b"av01",
        name: String::new(),
        content_type: None,
        content_encoding: None,
        hidden: true,
        references: vec![],
        properties: vec![
            essential(av1c()),
            descriptive(ispe(128, 128)),
            descriptive(pixi(&[8, 8, 8])),
            descriptive(PropertyKind::Colour(ColourInformation::UnrestrictedIcc(
                vec![0u8; 12],
            ))),
        ],
        payload: vec![0xca, 0xfe, 0xba, 0xbe],
    };

    // Alpha auxiliary for the grid, monochrome, essential auxC type.
    let alpha = Item {
        id: 4,
        item_type: *b"av01",
        name: String::new(),
        content_type: None,
        content_encoding: None,
        hidden: true,
        references: vec![ItemReference {
            reference_type: *b"auxl",
            to_item_ids: vec![1],
        }],
        properties: vec![
            essential(av1c()),
            descriptive(ispe(256, 128)),
            descriptive(pixi(&[8])),
            essential(PropertyKind::AuxiliaryType {
                aux_type: "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha".to_string(),
                aux_subtype: Vec::new(),
            }),
        ],
        payload: vec![0x00, 0x11, 0x22, 0x33],
    };

    // Exif metadata item describing the primary.
    let exif = Item {
        id: 5,
        item_type: *b"Exif",
        name: String::new(),
        content_type: None,
        content_encoding: None,
        hidden: false,
        references: vec![ItemReference {
            reference_type: *b"cdsc",
            to_item_ids: vec![1],
        }],
        properties: vec![],
        payload: b"\x00\x00\x00\x00II*\x00\x08\x00\x00\x00".to_vec(),
    };

    // XMP metadata item (a `mime` item with an RDF/XML content type).
    let xmp = Item {
        id: 6,
        item_type: *b"mime",
        name: String::new(),
        content_type: Some("application/rdf+xml".to_string()),
        content_encoding: None,
        hidden: false,
        references: vec![ItemReference {
            reference_type: *b"cdsc",
            to_item_ids: vec![1],
        }],
        properties: vec![],
        payload: b"<?xpacket?><x:xmpmeta/>".to_vec(),
    };

    // Thumbnail: a restricted ICC profile plus an HDR content-light-level property.
    let thumb = Item {
        id: 7,
        item_type: *b"av01",
        name: String::new(),
        content_type: None,
        content_encoding: None,
        hidden: true,
        references: vec![ItemReference {
            reference_type: *b"thmb",
            to_item_ids: vec![1],
        }],
        properties: vec![
            essential(av1c()),
            descriptive(ispe(64, 32)),
            descriptive(pixi(&[8, 8, 8])),
            descriptive(PropertyKind::Colour(ColourInformation::RestrictedIcc(
                vec![0u8; 12],
            ))),
            descriptive(PropertyKind::ContentLightLevel {
                max_content_light_level: 1000,
                max_pic_average_light_level: 400,
            }),
        ],
        payload: vec![0x44, 0x55, 0x66, 0x77],
    };

    Ok(IsoBmffImage {
        major_brand: *b"avif",
        minor_version: 0,
        compatible_brands: vec![*b"avif", *b"mif1", *b"miaf", *b"MA1A"],
        primary_item_id: 1,
        items: vec![grid, tile_a, tile_b, alpha, exif, xmp, thumb],
        groups: vec![EntityGroup {
            group_type: *b"altr",
            group_id: 1000,
            entity_ids: vec![1, 7],
        }],
    })
}

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

    #[test]
    fn demo_image_round_trips_through_write_read() {
        let img = demo_image().expect("demo image builds");
        let bytes = isobmff::write(&img).expect("write");
        let back = isobmff::read(&bytes).expect("read");
        assert_eq!(back, img);
    }

    #[test]
    fn demo_grid_payload_parses_back() {
        let img = demo_image().expect("demo image builds");
        let grid_item = img
            .items
            .iter()
            .find(|i| i.item_type == *b"grid")
            .expect("has a grid item");
        let g = ImageGrid::parse(&grid_item.payload).expect("grid payload parses");
        assert_eq!((g.rows, g.columns), (1, 2));
        assert_eq!((g.output_width, g.output_height), (256, 128));
    }
}