| 1 |
|
| 2 |
#[macro_use] |
| 3 |
extern crate smallvec; |
| 4 |
extern crate rand; |
| 5 |
extern crate half; |
| 6 |
|
| 7 |
use std::convert::TryInto; |
| 8 |
use std::io::BufWriter; |
| 9 |
use std::fs::File; |
| 10 |
use exr::block::{UncompressedBlock}; |
| 11 |
use exr::block::writer::ChunksWriter; |
| 12 |
|
| 13 |
// exr imports |
| 14 |
extern crate 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 main() { |
| 19 |
use exr::prelude::*; |
| 20 |
use attribute::*; |
| 21 |
use exr::math::*; |
| 22 |
|
| 23 |
// pre-compute a list of random values |
| 24 |
let random_values: Vec<f32> = (0..64) |
| 25 |
.map(|_| rand::random::<f32>()) |
| 26 |
.collect(); |
| 27 |
|
| 28 |
// resulting resolution (268 megapixels for 3GB files) |
| 29 |
let size = (2048*8, 2048*8); |
| 30 |
|
| 31 |
// define meta data header that will be written |
| 32 |
let header = exr::meta::header::Header::new( |
| 33 |
"test-image".try_into().unwrap(), |
| 34 |
size, |
| 35 |
smallvec![ |
| 36 |
attribute::ChannelDescription::new("B", SampleType::F32, true), |
| 37 |
attribute::ChannelDescription::new("G", SampleType::F32, true), |
| 38 |
attribute::ChannelDescription::new("R", SampleType::F32, true), |
| 39 |
attribute::ChannelDescription::new("Z", SampleType::F32, true), |
| 40 |
], |
| 41 |
); |
| 42 |
|
| 43 |
// define encoding that will be written |
| 44 |
let mut header = header.with_encoding( |
| 45 |
Compression::Uncompressed, |
| 46 |
|
| 47 |
exr::meta::BlockDescription::Tiles(TileDescription { |
| 48 |
tile_size: Vec2(64, 64), |
| 49 |
level_mode: LevelMode::Singular, |
| 50 |
rounding_mode: RoundingMode::Down |
| 51 |
}), |
| 52 |
|
| 53 |
LineOrder::Increasing |
| 54 |
); |
| 55 |
|
| 56 |
// add some random meta data |
| 57 |
header.own_attributes.exposure = Some(1.0); |
| 58 |
|
| 59 |
|
| 60 |
let headers = smallvec![ header ]; |
| 61 |
|
| 62 |
// specify output path, and buffer it for better performance |
| 63 |
let file = BufWriter::new(File::create("3GB.exr").unwrap()); |
| 64 |
|
| 65 |
let start_time = ::std::time::Instant::now(); |
| 66 |
|
| 67 |
// finally write the image |
| 68 |
exr::block::write( |
| 69 |
file, headers, true, |
| 70 |
|meta_data, chunk_writer|{ |
| 71 |
|
| 72 |
|
| 73 |
let blocks = meta_data.collect_ordered_blocks(|block_index|{ |
| 74 |
let channel_description = &meta_data.headers[block_index.layer].channels; |
| 75 |
|
| 76 |
// fill the image file contents with one of the precomputed random values, |
| 77 |
// picking a different one per channel |
| 78 |
UncompressedBlock::from_lines(channel_description, block_index, |line_mut|{ |
| 79 |
// TODO iterate mut instead?? |
| 80 |
|
| 81 |
let chan = line_mut.location.channel; |
| 82 |
|
| 83 |
if chan == 3 { // write time as depth (could also check for _meta.channels[chan].name == "Z") |
| 84 |
line_mut.write_samples(|_| start_time.elapsed().as_secs_f32()) |
| 85 |
.expect("write to line bug"); |
| 86 |
} |
| 87 |
|
| 88 |
else { // write rgba color |
| 89 |
line_mut |
| 90 |
.write_samples(|sample_index| random_values[(sample_index + chan) % random_values.len()]) |
| 91 |
.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_progress(|progress|{ |
| 101 |
let new_progress = (progress * 100.0) as usize; |
| 102 |
if new_progress != current_progress_percentage { |
| 103 |
current_progress_percentage = new_progress; |
| 104 |
println!("progress: {}%", current_progress_percentage) |
| 105 |
} |
| 106 |
}) |
| 107 |
.compress_all_blocks_parallel(&meta_data, blocks)?; |
| 108 |
|
| 109 |
Ok(()) |
| 110 |
} |
| 111 |
).unwrap(); |
| 112 |
|
| 113 |
// warning: highly unscientific benchmarks ahead! |
| 114 |
println!("\ncreated file 3GB.exr in {:?}s", start_time.elapsed().as_secs_f32()); |
| 115 |
} |