| 1 | |
| 2 | // exr imports |
| 3 | extern crate exrrust-analyzer cargo exr 1.74.0 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 mainrust-analyzer cargo exr 1.74.0 main().() { |
| 10 | use exrrust-analyzer cargo exr 1.74.0 crate/::preluderust-analyzer cargo exr 1.74.0 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 | #[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:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 17 | struct CustomPixelsrust-analyzer cargo exr 1.74.0 CustomPixels# { linesrust-analyzer cargo exr 1.74.0 CustomPixels#lines.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<RgbaF32Pixelrust-analyzer cargo exr 1.74.0 RgbaF32Pixel#>> } |
| 18 | type RgbaF32Pixelrust-analyzer cargo exr 1.74.0 RgbaF32Pixel# = (f32, f32, f32, f32); |
| 19 | |
| 20 | // read the image from a file |
| 21 | let mut image = readrust-analyzer cargo exr 1.74.0 image/read/read().().no_deep_datarust-analyzer cargo exr 1.74.0 image/read/impl#[ReadBuilder]no_deep_data().() |
| 22 | .largest_resolution_levelrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples]largest_resolution_level().() |
| 23 | .rgba_channelsrust-analyzer cargo exr 1.74.0 image/read/levels/impl#[`ReadLargestLevel<DeepOrFlatSamples>`]rgba_channels().( |
| 24 | // create our custom image based on the file info |
| 25 | |resolution, _channels| -> CustomPixelsrust-analyzer cargo exr 1.74.0 CustomPixels# { |
| 26 | let default_rgba_pixel = (0.0, 0.0, 0.0, 0.0); |
| 27 | let default_line = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![default_rgba_pixel; resolution.widthrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]width().()]; |
| 28 | let lines = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![default_line; resolution.heightrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]height().()]; |
| 29 | CustomPixelsrust-analyzer cargo exr 1.74.0 CustomPixels# { linesrust-analyzer cargo exr 1.74.0 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): RgbaF32Pixelrust-analyzer cargo exr 1.74.0 RgbaF32Pixel#| { |
| 35 | |
| 36 | // insert the values into our custom image |
| 37 | image.linesrust-analyzer cargo exr 1.74.0 CustomPixels#lines.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().position.yrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]y().()]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().position.xrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]x().()]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut(). = (r,g,b,a); |
| 38 | } |
| 39 | ) |
| 40 | .first_valid_layerrust-analyzer cargo exr 1.74.0 image/read/layers/ReadChannels#first_valid_layer().() |
| 41 | .all_attributesrust-analyzer cargo exr 1.74.0 image/read/image/ReadLayers#all_attributes().() |
| 42 | .from_filerust-analyzer cargo exr 1.74.0 image/read/image/impl#[`ReadImage<F, L>`]from_file().("generated_rgba.exr") |
| 43 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]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_datarust-analyzer cargo exr 1.74.0 image/Image#layer_data..channel_datarust-analyzer cargo exr 1.74.0 image/Layer#channel_data..pixelsrust-analyzer cargo exr 1.74.0 image/SpecificChannels#pixels..linesrust-analyzer cargo exr 1.74.0 CustomPixels#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 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().r rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`MulAssign<Self>`]mul_assign().*=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`MulAssign<Self>`]mul_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`MulAssign<Self>`]mul_assign().exposure_multiplier; |
| 53 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().g rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`MulAssign<Self>`]mul_assign().*=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`MulAssign<Self>`]mul_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`MulAssign<Self>`]mul_assign().exposure_multiplier; |
| 54 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`MulAssign<Self>`]mul_assign().*=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`MulAssign<Self>`]mul_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`MulAssign<Self>`]mul_assign().exposure_multiplier; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // also update meta data after modifying the image |
| 59 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(exposure) = &mut image.layer_datarust-analyzer cargo exr 1.74.0 image/Image#layer_data..attributesrust-analyzer cargo exr 1.74.0 image/Layer#attributes..exposurerust-analyzer cargo exr 1.74.0 meta/header/LayerAttributes#exposure. { |
| 60 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("increased exposure from {}s to {}s", exposure, *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().exposure *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). exposure_multiplier); |
| 61 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().exposure rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`MulAssign<Self>`]mul_assign().*=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`MulAssign<Self>`]mul_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`MulAssign<Self>`]mul_assign().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 GetPixelrust-analyzer cargo exr 1.74.0 image/write/channels/GetPixel# for CustomPixelsrust-analyzer cargo exr 1.74.0 CustomPixels# { |
| 68 | type Pixelrust-analyzer cargo exr 1.74.0 impl#[CustomPixels][GetPixel]Pixel# = RgbaF32Pixelrust-analyzer cargo exr 1.74.0 RgbaF32Pixel#; |
| 69 | fn get_pixelrust-analyzer cargo exr 1.74.0 impl#[CustomPixels][GetPixel]get_pixel().(&self, position: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>) -> Selfrust-analyzer cargo exr 1.74.0 impl#[CustomPixels][GetPixel]::Pixelrust-analyzer cargo exr 1.74.0 image/write/channels/GetPixel#Pixel# { |
| 70 | self.linesrust-analyzer cargo exr 1.74.0 CustomPixels#lines.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().position.yrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]y().()]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().position.xrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]x().()]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // write the image to a file |
| 75 | image |
| 76 | .writerust-analyzer cargo exr 1.74.0 image/write/impl#[`&'img Image<WritableLayers>`][`WritableImage<'img, WritableLayers>`]write().().to_filerust-analyzer cargo exr 1.74.0 image/write/impl#[`WriteImageWithOptions<'img, L, F>`]to_file().("rgba_exposure_adjusted.exr") |
| 77 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 78 | |
| 79 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("created file rgba_exposure_adjusted.exr"); |
| 80 | } |