Debian Codegraph @

examples/8_read_raw_blocks.rs

raw symbols SCIP
1
2 extern crate randrust-analyzer cargo exr 1.74.0 rand/;
3 extern crate halfrust-analyzer cargo exr 1.74.0 half/;
4
5 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#};
6 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#;
7 use exrrust-analyzer cargo exr 1.74.0 crate/::blockrust-analyzer cargo exr 1.74.0 block/::readerrust-analyzer cargo exr 1.74.0 block/reader/::ChunksReaderrust-analyzer cargo exr 1.74.0 block/reader/ChunksReader#;
8
9 // exr imports
10 extern crate exrrust-analyzer cargo exr 1.74.0 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 mainrust-analyzer cargo exr 1.74.0 main().() {
17 use exrrust-analyzer cargo exr 1.74.0 crate/::preluderust-analyzer cargo exr 1.74.0 prelude/::*;
18
19 let file = BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`]new().(
20 Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().("3GB.exr")
21 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]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 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
29 struct Layerrust-analyzer cargo exr 1.74.0 Layer# {
30 #[allow(unused)] // note: is used in Debug impl
31 layer_namerust-analyzer cargo exr 1.74.0 Layer#layer_name.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Textrust-analyzer cargo exr 1.74.0 meta/attribute/Text#>,
32
33 data_windowrust-analyzer cargo exr 1.74.0 Layer#data_window.: IntegerBoundsrust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#,
34
35 /// Collect one average float per channel in the layer
36 channelsrust-analyzer cargo exr 1.74.0 Layer#channels.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Channelrust-analyzer cargo exr 1.74.0 Channel#>,
37 }
38
39 /// A single channel in the layer, holds a single average value
40 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
41 struct Channelrust-analyzer cargo exr 1.74.0 Channel# {
42 #[allow(unused)] // note: is used in Debug impl
43 channel_namerust-analyzer cargo exr 1.74.0 Channel#channel_name.: Textrust-analyzer cargo exr 1.74.0 meta/attribute/Text#,
44
45 sample_typerust-analyzer cargo exr 1.74.0 Channel#sample_type.: SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#, // f32, u32, or f16
46 averagerust-analyzer cargo exr 1.74.0 Channel#average.: f32,
47 }
48
49 let start_time = ::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[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 = exrrust-analyzer cargo exr 1.74.0 crate/::blockrust-analyzer cargo exr 1.74.0 block/::readrust-analyzer cargo exr 1.74.0 block/read().(file, true).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]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.headersrust-analyzer cargo exr 1.74.0 block/reader/impl#[`Reader<R>`]headers().().iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
63 // create a layer for each header in the file
64 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|header| Layerrust-analyzer cargo exr 1.74.0 Layer# {
65 layer_namerust-analyzer cargo exr 1.74.0 Layer#layer_name.: header.own_attributesrust-analyzer cargo exr 1.74.0 meta/header/Header#own_attributes..layer_namerust-analyzer cargo exr 1.74.0 meta/header/LayerAttributes#layer_name..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Clone]clone().(),
66 data_windowrust-analyzer cargo exr 1.74.0 Layer#data_window.: header.data_windowrust-analyzer cargo exr 1.74.0 meta/header/impl#[Header]data_window().(),
67
68 // create a averaging channel for each channel in the file
69 channelsrust-analyzer cargo exr 1.74.0 Layer#channels.: header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels..listrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelList#list..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
70 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|channel| Channelrust-analyzer cargo exr 1.74.0 Channel# {
71 channel_namerust-analyzer cargo exr 1.74.0 Channel#channel_name.: channel.namerust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#name..clonerust-analyzer cargo exr 1.74.0 meta/attribute/impl#[Text][Clone]clone().(),
72 sample_typerust-analyzer cargo exr 1.74.0 Channel#sample_type.: channel.sample_typerust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#sample_type.,
73 averagerust-analyzer cargo exr 1.74.0 Channel#average.: 0.0
74 })
75 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
76 })
77 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/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_chunksrust-analyzer cargo exr 1.74.0 block/reader/impl#[`Reader<R>`]filter_chunks().(true, |meta_data, tile, block| {
84 let header = &meta_data.headersrust-analyzer cargo exr 1.74.0 meta/MetaData#headers.[rust-analyzer cargo smallvec 1.15.1 impl#[`SmallVec<A>`][`Index<I>`]index().block.layerrust-analyzer cargo exr 1.74.0 block/BlockIndex#layer.]rust-analyzer cargo smallvec 1.15.1 impl#[`SmallVec<A>`][`Index<I>`]index().;
85 !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().header.deeprust-analyzer cargo exr 1.74.0 meta/header/Header#deep. && tile.is_largest_resolution_levelrust-analyzer cargo exr 1.74.0 block/chunk/impl#[TileCoordinates]is_largest_resolution_level().()
86 }).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
87
88 .on_progressrust-analyzer cargo exr 1.74.0 block/reader/ChunksReader#on_progress().(|progress|{
89 let new_progress = (progress rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().100.0) as usize;
90 if new_progress rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().current_progress_percentage {
91 current_progress_percentage = new_progress;
92 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("progress: {}%", current_progress_percentage)
93 }
94 });
95
96 // read all pixel blocks from the image, decompressing in parallel
97 reader.decompress_parallelrust-analyzer cargo exr 1.74.0 block/reader/ChunksReader#decompress_parallel().(true, |meta_data, block|{
98 let header = &meta_data.headersrust-analyzer cargo exr 1.74.0 meta/MetaData#headers.[rust-analyzer cargo smallvec 1.15.1 impl#[`SmallVec<A>`][`Index<I>`]index().block.indexrust-analyzer cargo exr 1.74.0 block/UncompressedBlock#index..layerrust-analyzer cargo exr 1.74.0 block/BlockIndex#layer.]rust-analyzer cargo smallvec 1.15.1 impl#[`SmallVec<A>`][`Index<I>`]index().;
99
100 // collect all pixel values from the pixel block
101 for line in block.linesrust-analyzer cargo exr 1.74.0 block/impl#[UncompressedBlock]lines().(&header.channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels.) {
102 let layer = &mut averages[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().line.locationrust-analyzer cargo exr 1.74.0 block/lines/LineSlice#location..layerrust-analyzer cargo exr 1.74.0 block/lines/LineIndex#layer.]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().;
103 let channel = &mut layer.channelsrust-analyzer cargo exr 1.74.0 Layer#channels.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().line.locationrust-analyzer cargo exr 1.74.0 block/lines/LineSlice#location..channelrust-analyzer cargo exr 1.74.0 block/lines/LineIndex#channel.]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().;
104 let channel_sample_count = layer.data_windowrust-analyzer cargo exr 1.74.0 Layer#data_window..sizerust-analyzer cargo exr 1.74.0 meta/attribute/IntegerBounds#size..arearust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]area().() as f32;
105
106 // now sum the average based on the values in this line section of pixels
107 match channel.sample_typerust-analyzer cargo exr 1.74.0 Channel#sample_type. {
108 SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F16rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F16# => for value in line.read_samplesrust-analyzer cargo exr 1.74.0 block/lines/impl#[`LineSlice<&[u8]>`]read_samples().::<f16rust-analyzer cargo half 2.6.0 binary16/f16#>() {
109 channel.averagerust-analyzer cargo exr 1.74.0 Channel#average. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`AddAssign<Self>`]add_assign().value?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..to_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]to_f32().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().channel_sample_count;
110 },
111
112 SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F32# => for value in line.read_samplesrust-analyzer cargo exr 1.74.0 block/lines/impl#[`LineSlice<&[u8]>`]read_samples().::<f32>() {
113 channel.averagerust-analyzer cargo exr 1.74.0 Channel#average. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`AddAssign<Self>`]add_assign().value?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().channel_sample_count;
114 },
115
116 SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::U32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#U32# => for value in line.read_samplesrust-analyzer cargo exr 1.74.0 block/lines/impl#[`LineSlice<&[u8]>`]read_samples().::<u32>() {
117 channel.averagerust-analyzer cargo exr 1.74.0 Channel#average. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`AddAssign<Self>`]add_assign().(value?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). as f32) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().channel_sample_count;
118 },
119 }
120 }
121
122 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
123 }).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
124
125 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("average values: {:#?}", averages);
126
127 // warning: highly unscientific benchmarks ahead!
128 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("\nprocessed file in {:?}s", start_time.elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().().as_secs_f32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_secs_f32().());
129 }