extern crate rand;
extern crate half;
use std::io::{BufReader};
use std::fs::File;
extern crate exr;
use exr::prelude::*;
use exr::image;
use exr::meta::attributes::PixelType;
fn main() {
let file = BufReader::new(File::open("./testout/noisy.exr").unwrap());
#[derive(Debug)]
struct Layer {
name: Option<Text>,
data_window: IntRect,
channels: Vec<Channel>,
}
#[derive(Debug)]
struct Channel {
name: Text,
pixel_type: PixelType, average: f32,
}
let mut count_to_1000_and_then_print = 0;
let start_time = ::std::time::Instant::now();
let averages = image::read_filtered_lines_from_buffered(
file,
|headers| -> exr::error::Result<Vec<Layer>> { Ok(
headers.iter()
.map(|header| Layer {
name: header.own_attributes.name.clone(),
data_window: header.data_window(),
channels: header.channels.list.iter()
.map(|channel| Channel {
name: channel.name.clone(),
pixel_type: channel.pixel_type,
average: 0.0
})
.collect()
})
.collect()
) },
|_header, _meta, tile| {
tile.location.is_largest_resolution_level()
},
|averages, _meta, line| {
let layer = &mut averages[line.location.layer];
let channel = &mut layer.channels[line.location.channel];
let channel_sample_count = layer.data_window.size.area() as f32;
match channel.pixel_type {
PixelType::F16 => for value in line.read_samples::<f16>() {
channel.average += value?.to_f32() / channel_sample_count;
},
PixelType::F32 => for value in line.read_samples::<f32>() {
channel.average += value? / channel_sample_count;
},
PixelType::U32 => for value in line.read_samples::<f32>() {
channel.average += (value? as f32) / channel_sample_count;
},
}
Ok(())
},
ReadOptions {
parallel_decompression: false,
on_progress: |progress| {
count_to_1000_and_then_print += 1;
if count_to_1000_and_then_print == 1000 {
count_to_1000_and_then_print = 0;
println!("progress: {}%", (progress * 100.0) as usize);
}
Ok(())
}
},
).unwrap();
println!("Average values: {:#?}", averages);
let duration = start_time.elapsed();
let millis = duration.as_secs() * 1000 + duration.subsec_millis() as u64;
println!("\nRead exr file in {:?}s", millis as f32 * 0.001);
}