use scirs2_core::ndarray::Array3;
use scirs2_io::image::{
get_image_info, load_image, read_exif_metadata, save_image, ColorMode, ImageData, ImageFormat,
ImageMetadata,
};
use std::error::Error;
#[allow(dead_code)]
fn main() -> Result<(), Box<dyn Error>> {
println!("=== Image Metadata Example ===\n");
println!("1. Creating a test image...");
let mut test_array = Array3::zeros((100, 100, 3));
for y in 0..100 {
for x in 0..100 {
test_array[[y, x, 0]] = ((x as f32 / 100.0) * 255.0) as u8; test_array[[y, x, 1]] = ((y as f32 / 100.0) * 255.0) as u8; test_array[[y, x, 2]] = (((x + y) as f32 / 200.0) * 255.0) as u8; }
}
let metadata = ImageMetadata {
width: 100,
height: 100,
color_mode: ColorMode::RGB,
format: ImageFormat::PNG,
file_size: 0, exif: None, };
let image_data = ImageData {
data: test_array,
metadata,
};
println!("2. Saving test image...");
save_image(&image_data, "test_metadata.png", Some(ImageFormat::PNG))?;
println!("✓ Created test_metadata.png");
println!("\n3. Reading image info without loading full data...");
let info = get_image_info("test_metadata.png")?;
println!("Image Information:");
println!(" - Dimensions: {}x{}", info.width, info.height);
println!(" - Format: {:?}", info.format);
println!(" - File size: {} bytes", info.file_size);
println!(" - Color mode: {:?}", info.color_mode);
println!("\n4. Loading image with metadata...");
let loaded_image = load_image("test_metadata.png")?;
println!("✓ Loaded image successfully");
println!(" - Data shape: {:?}", loaded_image.data.shape());
println!(" - Metadata format: {:?}", loaded_image.metadata.format);
println!("\n5. Checking for EXIF metadata...");
match read_exif_metadata("test_metadata.png")? {
Some(exif) => {
println!("EXIF metadata found:");
if let Some(datetime) = exif.datetime {
println!(" - Date taken: {}", datetime);
}
if let Some(gps) = exif.gps {
if let (Some(lat), Some(lon)) = (gps.latitude, gps.longitude) {
println!(" - GPS: {:.6}, {:.6}", lat, lon);
}
}
if let Some(make) = &exif.camera.make {
println!(" - Camera make: {}", make);
}
if let Some(model) = &exif.camera.model {
println!(" - Camera model: {}", model);
}
}
None => {
println!("No EXIF metadata found (expected for PNG files)");
}
}
println!("\n6. Converting to JPEG format...");
save_image(&loaded_image, "test_metadata.jpg", Some(ImageFormat::JPEG))?;
println!("✓ Created test_metadata.jpg");
let jpeg_info = get_image_info("test_metadata.jpg")?;
println!("JPEG Information:");
println!(" - Format: {:?}", jpeg_info.format);
println!(" - File size: {} bytes", jpeg_info.file_size);
println!("\n7. Example of processing multiple images...");
let formats = vec![
(ImageFormat::PNG, "output_test.png"),
(ImageFormat::JPEG, "output_test.jpg"),
(ImageFormat::BMP, "output_test.bmp"),
];
for (format, filename) in formats {
save_image(&image_data, filename, Some(format))?;
let info = get_image_info(filename)?;
println!(
" - {}: {} bytes ({:?})",
filename, info.file_size, info.format
);
}
println!("\n✓ All examples completed successfully!");
println!("\nCleaning up test files...");
let test_files = vec![
"test_metadata.png",
"test_metadata.jpg",
"output_test.png",
"output_test.jpg",
"output_test.bmp",
];
for file in test_files {
if std::path::Path::new(file).exists() {
std::fs::remove_file(file)?;
}
}
println!("✓ Test files cleaned up");
Ok(())
}