| 1 | |
| 2 | // exr imports |
| 3 | extern crate exr; |
| 4 | |
| 5 | /// Read an rgba image, increase the exposure, and then write it back. |
| 6 | /// Uses multi-core compression where appropriate. |
| 7 | /// |
| 8 | /// All non-rgba channels and all layers except the first rgba layers will not be present in the new file. |
| 9 | fn main() { |
| 10 | use exr::prelude::*; |
| 11 | |
| 12 | /// This is an example of a custom image type. |
| 13 | /// You use your own image struct here. |
| 14 | // This struct trades sub-optimal memory-efficiency for clarity, |
| 15 | // because this is an example, and does not have to be perfectly efficient. |
| 16 | #[derive(Debug, PartialEq)] |
| 17 | struct CustomPixels { lines: Vec<Vec<RgbaF32Pixel>> } |
| 18 | type RgbaF32Pixel = (f32, f32, f32, f32); |
| 19 | |
| 20 | // read the image from a file |
| 21 | let mut image = read().no_deep_data() |
| 22 | .largest_resolution_level() |
| 23 | .rgba_channels( |
| 24 | // create our custom image based on the file info |
| 25 | |resolution, _channels| -> CustomPixels { |
| 26 | let default_rgba_pixel = (0.0, 0.0, 0.0, 0.0); |
| 27 | let default_line = vec![default_rgba_pixel; resolution.width()]; |
| 28 | let lines = vec![default_line; resolution.height()]; |
| 29 | CustomPixels { lines } |
| 30 | }, |
| 31 | |
| 32 | // request pixels with red, green, blue, and optionally and alpha values. |
| 33 | // transfer each pixel from the file to our image |
| 34 | |image, position, (r,g,b,a): RgbaF32Pixel| { |
| 35 | |
| 36 | // insert the values into our custom image |
| 37 | image.lines[position.y()][position.x()] = (r,g,b,a); |
| 38 | } |
| 39 | ) |
| 40 | .first_valid_layer() |
| 41 | .all_attributes() |
| 42 | .from_file("generated_rgba.exr") |
| 43 | .expect("run the `1_write_rgba` example to generate the required file"); |
| 44 | |
| 45 | let exposure_multiplier = 2.0; |
| 46 | |
| 47 | { // increase exposure of all pixels |
| 48 | for line in &mut image.layer_data.channel_data.pixels.lines { |
| 49 | for (r,g,b,_) in line { |
| 50 | // you should probably check the color space and white points |
| 51 | // for high quality color adjustments |
| 52 | *r *= exposure_multiplier; |
| 53 | *g *= exposure_multiplier; |
| 54 | *b *= exposure_multiplier; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // also update meta data after modifying the image |
| 59 | if let Some(exposure) = &mut image.layer_data.attributes.exposure { |
| 60 | println!("increased exposure from {}s to {}s", exposure, *exposure * exposure_multiplier); |
| 61 | *exposure *= exposure_multiplier; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // enable writing our custom pixel storage to a file |
| 66 | // FIXME this should be passed as a closure to the `write_with(|x| y)` call |
| 67 | impl GetPixel for CustomPixels { |
| 68 | type Pixel = RgbaF32Pixel; |
| 69 | fn get_pixel(&self, position: Vec2<usize>) -> Self::Pixel { |
| 70 | self.lines[position.y()][position.x()] |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // write the image to a file |
| 75 | image |
| 76 | .write().to_file("rgba_exposure_adjusted.exr") |
| 77 | .unwrap(); |
| 78 | |
| 79 | println!("created file rgba_exposure_adjusted.exr"); |
| 80 | } |