Skip to main content

create_raw_image/
create_raw_image.rs

1use vsf::builders::*;
2use vsf::types::BitPackedTensor;
3
4fn main() -> Result<(), String> {
5    println!("=== VSF RAW Image Example ===\n");
6
7    // Example: 17x163 11-bit single-plane RGGB bayer sensor
8    let width = 17;
9    let height = 163;
10    let planes = 1;
11    let bits = 11;
12    let cfa = vec![b'R', b'G', b'C', b'Y']; // Red Green Cyan Yellow Bayer pattern
13    let blackpoint = 499;
14    let whitepoint = 8047;
15
16    println!("Sensor specs:");
17    println!("  Resolution: {}×{} (single plane)", width, height);
18    println!("  Bit depth: {} bits per sample", bits);
19    println!("  CFA pattern: RGCY (Red, Green, Cyan, Yellow)");
20    println!("  Black level: {}", blackpoint);
21    println!("  White level: {}\n", whitepoint);
22
23    // Generate simulated RAW sensor data
24    let total_samples = width * height * planes;
25    let mut samples = Vec::with_capacity(total_samples);
26    let mut rng = 0usize;
27    for sample in 0..total_samples {
28        rng ^= rng.rotate_left(13).wrapping_add(sample);
29        let value = rng as u8 as u16 + blackpoint; // Simulate RAW image data
30        samples.push(value);
31    }
32
33    // Pack into BitPackedTensor (efficient storage for 11-bit samples)
34    let image = BitPackedTensor::pack(bits, vec![width, height], &samples);
35    println!(
36        "Packed {} samples into {} bytes\n",
37        total_samples,
38        image.data.len()
39    );
40
41    // ========== BUILDER PATTERN API ========== The builder accepts raw types for ergonomics - validation happens at build() time
42    println!("Building VSF RAW image with metadata...");
43
44    let mut raw = RawImageBuilder::new(image);
45
46    // Set RAW metadata (sensor characteristics) Builder fields accept simple types - they get validated and wrapped when you call build()
47    raw.raw.cfa_pattern = Some(cfa);
48    raw.raw.black_level = Some(blackpoint as f32);
49    raw.raw.white_level = Some(whitepoint as f32);
50    raw.raw.magic_9 = Some(vec![
51        1.5, -0.3, -0.2, // Sensor RGB → LMS colour matrix (3×3)
52        -0.4, 1.6, -0.2, -0.1, -0.5, 1.6,
53    ]);
54
55    // Set camera settings
56    raw.camera.make = Some("Sony".to_string());
57    raw.camera.model = Some("α7 IV".to_string());
58    raw.camera.serial_number = Some("87654321".to_string());
59    raw.camera.iso_speed = Some(800.);
60    raw.camera.shutter_time_s = Some(1. / 60.); // 1/60 second
61    raw.camera.aperture_f_number = Some(2.8);
62    raw.camera.focal_length_m = Some(0.024); // 24mm = 0.024m
63    raw.camera.exposure_compensation = Some(0.);
64    raw.camera.focus_distance_m = Some(5.);
65    raw.camera.flash_fired = Some(false);
66    raw.camera.metering_mode = Some("matrix".to_string());
67
68    // Set lens info
69    raw.lens.make = Some("Sony".to_string());
70    raw.lens.model = Some("FE 24mm F2.8 G".to_string());
71    raw.lens.serial_number = Some("12345678".to_string());
72    raw.lens.min_focal_length_m = Some(0.024); // Prime lens
73    raw.lens.max_focal_length_m = Some(0.024);
74    raw.lens.min_aperture_f = Some(2.8);
75    raw.lens.max_aperture_f = Some(22.);
76
77    // Build validates all fields and wraps them in type-safe newtypes This is where validation errors would surface (e.g., invalid CFA colour codes)
78    let raw_bytes = raw.build()?;
79
80    println!("✓ Built VSF RAW image: {} bytes", raw_bytes.len());
81    println!("  (Includes mandatory BLAKE3 hash for integrity)\n");
82
83    // Write to file
84    std::fs::write("example_raw.vsf", &raw_bytes).expect("Failed to write file");
85    println!("✓ Saved to example_raw.vsf");
86
87    println!("\n=== Complete! ===");
88    println!("The RAW image includes:");
89    println!("  • Bitpacked sensor data ({}-bit samples)", bits);
90    println!("  • CFA pattern (RGCY Bayer)");
91    println!("  • Black/white levels");
92    println!("  • Colour transformation matrix (Magic 9)");
93    println!("  • Camera information (make, model, S/N, ISO, shutter, aperture, etc.)");
94    println!("  • Lens information (make, model, S/N, focal length range, etc.)");
95    println!("  • Mandatory BLAKE3 file hash (automatic)");
96    println!("\nAll metadata is validated and type-checked at compile time.");
97    println!("Try changing a value to something invalid - the compiler will catch it!");
98
99    Ok(())
100}