Debian Codegraph @

.cargo/vendor/exr/examples/8_read_raw_blocks.rs

raw
1
2 extern crate rand;
3 extern crate half;
4
5 use std::io::{BufReader};
6 use std::fs::File;
7 use exr::block::reader::ChunksReader;
8
9 // exr imports
10 extern crate exr;
11
12
13 /// Collects the average pixel value for each channel.
14 /// Does not load the whole image into memory at once: only processes the image block by block.
15 /// On my machine, this program analyzes a 3GB file while only allocating 1.1MB.
16 fn main() {
17 use exr::prelude::*;
18
19 let file = BufReader::new(
20 File::open("3GB.exr")
21 .expect("run example `7_write_raw_blocks` to generate this image file")
22 );
23
24
25 // -- the following structs will hold the collected data from the image --
26
27 /// Collect averages for each layer in the image
28 #[derive(Debug)]
29 struct Layer {
30 #[allow(unused)] // note: is used in Debug impl
31 layer_name: Option<Text>,
32
33 data_window: IntegerBounds,
34
35 /// Collect one average float per channel in the layer
36 channels: Vec<Channel>,
37 }
38
39 /// A single channel in the layer, holds a single average value
40 #[derive(Debug)]
41 struct Channel {
42 #[allow(unused)] // note: is used in Debug impl
43 channel_name: Text,
44
45 sample_type: SampleType, // f32, u32, or f16
46 average: f32,
47 }
48
49 let start_time = ::std::time::Instant::now();
50
51
52 // -- read the file, summing up the average pixel values --
53
54 // start reading the file, extracting the meta data of the image
55 let reader = exr::block::read(file, true).unwrap();
56
57 // print progress only if it advances more than 1%
58 let mut current_progress_percentage = 0;
59
60 // create the empty data structure that will collect the analyzed results,
61 // based on the extracted meta data of the file
62 let mut averages = reader.headers().iter()
63 // create a layer for each header in the file
64 .map(|header| Layer {
65 layer_name: header.own_attributes.layer_name.clone(),
66 data_window: header.data_window(),
67
68 // create a averaging channel for each channel in the file
69 channels: header.channels.list.iter()
70 .map(|channel| Channel {
71 channel_name: channel.name.clone(),
72 sample_type: channel.sample_type,
73 average: 0.0
74 })
75 .collect()
76 })
77 .collect::<Vec<_>>();
78
79 // create a reader that loads only relevant chunks from the file, and also prints something on progress
80 let reader = reader
81
82 // do not worry about multi-resolution levels or deep data
83 .filter_chunks(true, |meta_data, tile, block| {
84 let header = &meta_data.headers[block.layer];
85 !header.deep && tile.is_largest_resolution_level()
86 }).unwrap()
87
88 .on_progress(|progress|{
89 let new_progress = (progress * 100.0) as usize;
90 if new_progress != current_progress_percentage {
91 current_progress_percentage = new_progress;
92 println!("progress: {}%", current_progress_percentage)
93 }
94 });
95
96 // read all pixel blocks from the image, decompressing in parallel
97 reader.decompress_parallel(true, |meta_data, block|{
98 let header = &meta_data.headers[block.index.layer];
99
100 // collect all pixel values from the pixel block
101 for line in block.lines(&header.channels) {
102 let layer = &mut averages[line.location.layer];
103 let channel = &mut layer.channels[line.location.channel];
104 let channel_sample_count = layer.data_window.size.area() as f32;
105
106 // now sum the average based on the values in this line section of pixels
107 match channel.sample_type {
108 SampleType::F16 => for value in line.read_samples::<f16>() {
109 channel.average += value?.to_f32() / channel_sample_count;
110 },
111
112 SampleType::F32 => for value in line.read_samples::<f32>() {
113 channel.average += value? / channel_sample_count;
114 },
115
116 SampleType::U32 => for value in line.read_samples::<u32>() {
117 channel.average += (value? as f32) / channel_sample_count;
118 },
119 }
120 }
121
122 Ok(())
123 }).unwrap();
124
125 println!("average values: {:#?}", averages);
126
127 // warning: highly unscientific benchmarks ahead!
128 println!("\nprocessed file in {:?}s", start_time.elapsed().as_secs_f32());
129 }