aboutsummaryrefslogtreecommitdiff
path: root/vendor/exr/examples/3b_read_all_channels_with_metadata.rs
blob: cd9fc04fd84b90ea1b2461c2318212d0920c4a09 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// exr imports
extern crate exr;

/// Read an image and print information about the image into the console.
/// This example shows how to read an image with multiple layers and arbitrary channels.
/// For example, a layer with XYZ channels, and additionally a separate Depth layer.
/// This example does not include resolution levels (mipmaps or ripmaps).
fn main() {
    use exr::prelude::*;

    let image = read().no_deep_data()
        .largest_resolution_level().all_channels().all_layers().all_attributes()
        .on_progress(|progress| println!("progress: {:.1}", progress*100.0))
        .from_file("generated_rgba_with_meta.exr")
        .expect("run example `1_write_rgba_with_metadata` to generate this image file");

    println!("image was read: {:#?}", image);

    // output the average value for each channel of each layer
    for layer in &image.layer_data {
        for channel in &layer.channel_data.list {

            let sample_vec = &channel.sample_data;
            let average = sample_vec.values_as_f32().sum::<f32>() / sample_vec.len() as f32;

            if let Some(layer_name) = &layer.attributes.layer_name {
                println!("Channel `{}` of Layer `{}` has an average value of {}", channel.name, layer_name, average);
            }
            else {
                println!("Channel `{}` has an average value of {}", channel.name, average);
            }
        }
    }
}