| 1 | |
| 2 | // exr imports |
| 3 | extern crate exr; |
| 4 | |
| 5 | /// Write an rgba exr file, generating the pixel values on the fly. |
| 6 | /// This streams the generated pixel directly to the file, |
| 7 | /// never allocating the actual total pixel memory of the image. |
| 8 | fn main() { |
| 9 | use exr::prelude::*; |
| 10 | use exr::meta::attribute::*; |
| 11 | |
| 12 | // this function can generate a color for any pixel |
| 13 | let generate_pixels = |position: Vec2<usize>| ( |
| 14 | position.x() as f32 / 2048.0, // red |
| 15 | position.y() as f32 / 2048.0, // green |
| 16 | 1.0 - (position.y() as f32 / 2048.0), // blue |
| 17 | 1.0 // alpha |
| 18 | ); |
| 19 | |
| 20 | let mut layer_attributes = LayerAttributes::named("generated rgba main layer"); |
| 21 | layer_attributes.comments = Some(Text::from("This image was generated as part of an example")); |
| 22 | layer_attributes.owner = Some(Text::from("The holy lambda function")); |
| 23 | layer_attributes.software_name = Some(Text::from("EXRS Project")); |
| 24 | layer_attributes.exposure = Some(1.0); |
| 25 | layer_attributes.focus = Some(12.4); |
| 26 | layer_attributes.frames_per_second = Some((60, 1)); |
| 27 | layer_attributes.other.insert( |
| 28 | Text::from("Layer Purpose (Custom Layer Attribute)"), |
| 29 | AttributeValue::Text(Text::from("This layer contains the rgb pixel data")) |
| 30 | ); |
| 31 | |
| 32 | let layer = Layer::new( |
| 33 | (2*2048, 2*2048), |
| 34 | layer_attributes, |
| 35 | Encoding::SMALL_FAST_LOSSLESS, // use fast but lossy compression |
| 36 | |
| 37 | SpecificChannels::rgba(generate_pixels) |
| 38 | ); |
| 39 | |
| 40 | // crop away black and transparent pixels from the border, if any |
| 41 | let layer = layer |
| 42 | .crop_where_eq((0.0, 0.0, 0.0, 0.0)) |
| 43 | .or_crop_to_1x1_if_empty(); |
| 44 | |
| 45 | let mut image = Image::from_layer(layer); |
| 46 | image.attributes.pixel_aspect = 1.0; |
| 47 | |
| 48 | image.attributes.time_code = Some(TimeCode { |
| 49 | hours: 0, |
| 50 | minutes: 1, |
| 51 | seconds: 59, |
| 52 | frame: 29, |
| 53 | ..TimeCode::default() |
| 54 | }); |
| 55 | |
| 56 | image.attributes.other.insert( |
| 57 | Text::from("Mice Count (Custom Image Attribute)"), |
| 58 | AttributeValue::I32(23333) |
| 59 | ); |
| 60 | |
| 61 | // write it to a file with all cores in parallel |
| 62 | image.write().to_file("generated_rgba_with_meta.exr").unwrap(); |
| 63 | println!("created file generated_rgba_with_meta.exr"); |
| 64 | } |