Debian Codegraph @

examples/7_write_raw_blocks.rs

raw symbols SCIP
1
2 #[macro_use]
3 extern crate smallvecrust-analyzer cargo exr 1.74.0 smallvec/;
4 extern crate randrust-analyzer cargo exr 1.74.0 rand/;
5 extern crate halfrust-analyzer cargo exr 1.74.0 half/;
6
7 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::convertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/::TryIntorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryInto#;
8 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/::BufWriterrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufwriter/BufWriter#;
9 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#;
10 use exrrust-analyzer cargo exr 1.74.0 crate/::blockrust-analyzer cargo exr 1.74.0 block/::{UncompressedBlockrust-analyzer cargo exr 1.74.0 block/UncompressedBlock#};
11 use exrrust-analyzer cargo exr 1.74.0 crate/::blockrust-analyzer cargo exr 1.74.0 block/::writerrust-analyzer cargo exr 1.74.0 block/writer/::ChunksWriterrust-analyzer cargo exr 1.74.0 block/writer/ChunksWriter#;
12
13 // exr imports
14 extern crate exrrust-analyzer cargo exr 1.74.0 exr/;
15
16 /// Generate a striped image on the fly and directly write that to a file without allocating the whole image at once.
17 /// On my machine, this program produces a 3GB file while only ever allocating 4MB memory (takes a while though).
18 fn mainrust-analyzer cargo exr 1.74.0 main().() {
19 use exrrust-analyzer cargo exr 1.74.0 crate/::preluderust-analyzer cargo exr 1.74.0 prelude/::*;
20 use attributerust-analyzer cargo exr 1.74.0 meta/attribute/::*;
21 use exrrust-analyzer cargo exr 1.74.0 crate/::mathrust-analyzer cargo exr 1.74.0 math/::*;
22
23 // pre-compute a list of random values
24 let random_values: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<f32> = (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#64)
25 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|_| randrust-analyzer cargo rand 0.8.5 crate/::randomrust-analyzer cargo rand 0.8.5 random().::<f32>())
26 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
27
28 // resulting resolution (268 megapixels for 3GB files)
29 let size = (2048*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().8, 2048*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().8);
30
31 // define meta data header that will be written
32 let header = exrrust-analyzer cargo exr 1.74.0 crate/::metarust-analyzer cargo exr 1.74.0 meta/::headerrust-analyzer cargo exr 1.74.0 meta/header/::Headerrust-analyzer cargo exr 1.74.0 meta/header/Header#::newrust-analyzer cargo exr 1.74.0 meta/header/impl#[Header]new().(
33 "test-image".try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
34 size,
35 smallvecrust-analyzer cargo smallvec 1.15.1 smallvec!![
36 attributerust-analyzer cargo exr 1.74.0 meta/attribute/::ChannelDescriptionrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#::newrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[ChannelDescription]new().("B", SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F32#, true),
37 attributerust-analyzer cargo exr 1.74.0 meta/attribute/::ChannelDescriptionrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#::newrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[ChannelDescription]new().("G", SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F32#, true),
38 attributerust-analyzer cargo exr 1.74.0 meta/attribute/::ChannelDescriptionrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#::newrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[ChannelDescription]new().("R", SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F32#, true),
39 attributerust-analyzer cargo exr 1.74.0 meta/attribute/::ChannelDescriptionrust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#::newrust-analyzer cargo exr 1.74.0 meta/attribute/impl#[ChannelDescription]new().("Z", SampleTyperust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#::F32rust-analyzer cargo exr 1.74.0 meta/attribute/SampleType#F32#, true),
40 ],
41 );
42
43 // define encoding that will be written
44 let mut header = header.with_encodingrust-analyzer cargo exr 1.74.0 meta/header/impl#[Header]with_encoding().(
45 Compressionrust-analyzer cargo exr 1.74.0 compression/Compression#::Uncompressedrust-analyzer cargo exr 1.74.0 compression/Compression#Uncompressed#,
46
47 exrrust-analyzer cargo exr 1.74.0 crate/::metarust-analyzer cargo exr 1.74.0 meta/::BlockDescriptionrust-analyzer cargo exr 1.74.0 meta/BlockDescription#::Tilesrust-analyzer cargo exr 1.74.0 meta/BlockDescription#Tiles#(TileDescriptionrust-analyzer cargo exr 1.74.0 meta/attribute/TileDescription# {
48 tile_sizerust-analyzer cargo exr 1.74.0 meta/attribute/TileDescription#tile_size.: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(64, 64),
49 level_moderust-analyzer cargo exr 1.74.0 meta/attribute/TileDescription#level_mode.: LevelModerust-analyzer cargo exr 1.74.0 meta/attribute/LevelMode#::Singularrust-analyzer cargo exr 1.74.0 meta/attribute/LevelMode#Singular#,
50 rounding_moderust-analyzer cargo exr 1.74.0 meta/attribute/TileDescription#rounding_mode.: RoundingModerust-analyzer cargo exr 1.74.0 math/RoundingMode#::Downrust-analyzer cargo exr 1.74.0 math/RoundingMode#Down#
51 }),
52
53 LineOrderrust-analyzer cargo exr 1.74.0 meta/attribute/LineOrder#::Increasingrust-analyzer cargo exr 1.74.0 meta/attribute/LineOrder#Increasing#
54 );
55
56 // add some random meta data
57 header.own_attributesrust-analyzer cargo exr 1.74.0 meta/header/Header#own_attributes..exposurerust-analyzer cargo exr 1.74.0 meta/header/LayerAttributes#exposure. = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(1.0);
58
59
60 let headers = smallvecrust-analyzer cargo smallvec 1.15.1 smallvec!![ header ];
61
62 // specify output path, and buffer it for better performance
63 let file = BufWriterrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufwriter/BufWriter#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufwriter/impl#[`BufWriter<W>`]new().(Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().("3GB.exr").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
64
65 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().();
66
67 // finally write the image
68 exrrust-analyzer cargo exr 1.74.0 crate/::blockrust-analyzer cargo exr 1.74.0 block/::writerust-analyzer cargo exr 1.74.0 block/write().(
69 file, headers, true,
70 |meta_data, chunk_writer|{
71
72
73 let blocks = meta_data.collect_ordered_blocksrust-analyzer cargo exr 1.74.0 meta/impl#[MetaData]collect_ordered_blocks().(|block_index|{
74 let channel_description = &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_index.layerrust-analyzer cargo exr 1.74.0 block/BlockIndex#layer.]rust-analyzer cargo smallvec 1.15.1 impl#[`SmallVec<A>`][`Index<I>`]index()..channelsrust-analyzer cargo exr 1.74.0 meta/header/Header#channels.;
75
76 // fill the image file contents with one of the precomputed random values,
77 // picking a different one per channel
78 UncompressedBlockrust-analyzer cargo exr 1.74.0 block/UncompressedBlock#::from_linesrust-analyzer cargo exr 1.74.0 block/impl#[UncompressedBlock]from_lines().(channel_description, block_index, |line_mut|{
79 // TODO iterate mut instead??
80
81 let chan = line_mut.locationrust-analyzer cargo exr 1.74.0 block/lines/LineSlice#location..channelrust-analyzer cargo exr 1.74.0 block/lines/LineIndex#channel.;
82
83 if chan rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().3 { // write time as depth (could also check for _meta.channels[chan].name == "Z")
84 line_mut.write_samplesrust-analyzer cargo exr 1.74.0 block/lines/impl#[`LineSlice<&'s mut [u8]>`]write_samples().(|_| 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().())
85 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("write to line bug");
86 }
87
88 else { // write rgba color
89 line_mut
90 .write_samplesrust-analyzer cargo exr 1.74.0 block/lines/impl#[`LineSlice<&'s mut [u8]>`]write_samples().(|sample_index| random_values[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().(sample_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().chan) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().random_values.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().)
91 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("write to line bug");
92 }
93 })
94 });
95
96 // print progress only if it advances more than 1%
97 let mut current_progress_percentage = 0;
98
99 chunk_writer
100 .on_progressrust-analyzer cargo exr 1.74.0 block/writer/ChunksWriter#on_progress().(|progress|{
101 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;
102 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 {
103 current_progress_percentage = new_progress;
104 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("progress: {}%", current_progress_percentage)
105 }
106 })
107 .compress_all_blocks_parallelrust-analyzer cargo exr 1.74.0 block/writer/ChunksWriter#compress_all_blocks_parallel().(&meta_data, blocks)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
108
109 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
110 }
111 ).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
112
113 // warning: highly unscientific benchmarks ahead!
114 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("\ncreated file 3GB.exr 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().());
115 }