xisf-core 0.5.2

Core engine for reading and writing XISF (Extensible Image Serialization Format) files
Documentation
//! Write a file exercising as much of the header as this writer can produce.
//!
//! This exists for `scripts/validate-headers.sh`. Validating the corpus checks
//! that we can *read* what other implementations write; only a file we wrote
//! ourselves checks that what we *emit* satisfies the authority's own schema,
//! and a header with one uncompressed grey image would not exercise much of
//! it. So this turns on as many attributes and child elements as the writer
//! knows how to produce.
//!
//! Run as: `cargo run --example write-showcase -p xisf-core -- out.xisf`
use xisf_core::block::ChecksumAlgorithm;
use xisf_core::image::*;
use xisf_core::writer::*;

fn img(w: u64, h: u64, f: SampleFormat, cs: ColorSpace, ch: u64) -> Image {
    Image {
        dimensions: vec![w, h],
        channels: ch,
        sample_format: f,
        color_space: cs,
        pixel_storage: PixelStorage::Planar,
        bounds: if f.is_float() { Some(Bounds { low: 0.0, high: 1.0 }) } else { None },
        id: None,
        uuid: None,
        image_type: None,
        offset: None,
        orientation: None,
    }
}

fn main() {
    let mut w = Writer::new();
    let base = img(4, 4, SampleFormat::UInt16, ColorSpace::Rgb, 3);
    let n = base.data_size().unwrap() as usize;

    let mut a = base.clone();
    a.id = Some("Light".into());
    a.image_type = Some(ImageType::MasterLight);
    a.orientation = Some(Orientation { rotation: 90, flip_horizontal: true });
    // A version 4 UUID: the third group starts with 4. The RFC's own example
    // is version 1, which Revision 1 does not allow.
    a.uuid = Some("2f1c8e6b-9a4d-4c7e-b3f5-6d80a1e42c9b".into());
    a.offset = Some(0.0);

    let thumb = img(2, 2, SampleFormat::UInt8, ColorSpace::Gray, 1);
    let tn = thumb.data_size().unwrap() as usize;

    w.add_image(
        PendingImage::new(
            a,
            vec![7u8; n],
            BlockOptions {
                compression: Some(CompressionRequest {
                    codec: Codec2::Zstd,
                    shuffle_item_size: Some(2),
                }),
                checksum: Some(ChecksumAlgorithm::Sha256),
            },
        )
        .with_resolution(Resolution {
            horizontal: 300.0,
            vertical: 300.0,
            unit: ResolutionUnit::Inch,
        })
        .with_rgb_working_space(RgbWorkingSpace::srgb())
        .with_display_function(DisplayFunction::identity())
        .with_thumbnail(PendingThumbnail::new(thumb, vec![1u8; tn], BlockOptions::default()))
        .with_fits_keyword("OBJECT", "M31", "the target")
        .with_icc_profile({
            // A 128-byte header with the acsp signature: the smallest thing
            // that is structurally an ICC profile rather than arbitrary bytes.
            let mut p = vec![0u8; 128];
            p[0..4].copy_from_slice(&128u32.to_be_bytes());
            p[36..40].copy_from_slice(b"acsp");
            p
        }),
    )
    .expect("add_image");

    let mut b = base.clone();
    b.id = Some("Dark".into());
    w.add_image(PendingImage::new(
        b,
        vec![3u8; n],
        BlockOptions { compression: None, checksum: Some(ChecksumAlgorithm::Sha512) },
    ))
    .expect("b");

    w.add_metadata("Instrument:Telescope:Name", "Newtonian").expect("metadata");
    let bytes = w.to_bytes().expect("to_bytes");
    std::fs::write(std::env::args().nth(1).expect("out path"), &bytes).expect("write");
    eprintln!("wrote {} bytes", bytes.len());
}