| 1 | |
| 2 | extern crate image as pngrust-analyzer cargo exr 1.74.0 png/; |
| 3 | |
| 4 | // exr imports |
| 5 | extern crate exrrust-analyzer cargo exr 1.74.0 exr/; |
| 6 | |
| 7 | /// Converts one rgba exr with one layer to one png, or fail. |
| 8 | fn mainrust-analyzer cargo exr 1.74.0 main().() { |
| 9 | use exrrust-analyzer cargo exr 1.74.0 crate/::preluderust-analyzer cargo exr 1.74.0 prelude/::*; |
| 10 | use exrrust-analyzer cargo exr 1.74.0 crate/::preluderust-analyzer cargo exr 1.74.0 prelude/ as exrsrust-analyzer cargo exr 1.74.0 prelude/; |
| 11 | |
| 12 | // read from the exr file directly into a new `png::RgbaImage` image without intermediate buffers |
| 13 | let reader = exrsrust-analyzer cargo exr 1.74.0 prelude/::readrust-analyzer cargo exr 1.74.0 image/read/read().() |
| 14 | .no_deep_datarust-analyzer cargo exr 1.74.0 image/read/impl#[ReadBuilder]no_deep_data().() |
| 15 | .largest_resolution_levelrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples]largest_resolution_level().() |
| 16 | .rgba_channelsrust-analyzer cargo exr 1.74.0 image/read/levels/impl#[`ReadLargestLevel<DeepOrFlatSamples>`]rgba_channels().( |
| 17 | |resolution, _channels: &RgbaChannelsrust-analyzer cargo exr 1.74.0 image/RgbaChannels#| -> pngrust-analyzer cargo image 0.25.8 crate/::RgbaImagerust-analyzer cargo image 0.25.8 images/buffer/RgbaImage# { |
| 18 | pngrust-analyzer cargo image 0.25.8 crate/::ImageBufferrust-analyzer cargo image 0.25.8 images/buffer/ImageBuffer#::newrust-analyzer cargo image 0.25.8 images/buffer/impl#[`ImageBuffer<P, Vec<<P as Pixel>::Subpixel>>`]new().( |
| 19 | resolution.widthrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]width().() as u32, |
| 20 | resolution.heightrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]height().() as u32 |
| 21 | ) |
| 22 | }, |
| 23 | |
| 24 | // set each pixel in the png buffer from the exr file |
| 25 | |png_pixels, position, (r,g,b,a): (f32,f32,f32,f32)| { // TODO implicit argument types! |
| 26 | png_pixels.put_pixelrust-analyzer cargo image 0.25.8 images/buffer/impl#[`ImageBuffer<P, Container>`]put_pixel().( |
| 27 | position.xrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]x().() as u32, position.yrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]y().() as u32, |
| 28 | pngrust-analyzer cargo image 0.25.8 crate/::Rgbarust-analyzer cargo image 0.25.8 color/Rgba#([tone_maprust-analyzer cargo exr 1.74.0 tone_map().(r), tone_maprust-analyzer cargo exr 1.74.0 tone_map().(g), tone_maprust-analyzer cargo exr 1.74.0 tone_map().(b), (a rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().255.0) as u8]) |
| 29 | ); |
| 30 | } |
| 31 | ) |
| 32 | .first_valid_layerrust-analyzer cargo exr 1.74.0 image/read/layers/ReadChannels#first_valid_layer().() |
| 33 | .all_attributesrust-analyzer cargo exr 1.74.0 image/read/image/ReadLayers#all_attributes().(); |
| 34 | |
| 35 | // an image that contains a single layer containing an png rgba buffer |
| 36 | let image: Imagerust-analyzer cargo exr 1.74.0 image/Image#<Layerrust-analyzer cargo exr 1.74.0 image/Layer#<SpecificChannelsrust-analyzer cargo exr 1.74.0 image/SpecificChannels#<pngrust-analyzer cargo image 0.25.8 crate/::RgbaImagerust-analyzer cargo image 0.25.8 images/buffer/RgbaImage#, RgbaChannelsrust-analyzer cargo exr 1.74.0 image/RgbaChannels#>>> = reader |
| 37 | .from_filerust-analyzer cargo exr 1.74.0 image/read/image/impl#[`ReadImage<F, L>`]from_file().("generated_rgba.exr") |
| 38 | .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"); |
| 39 | |
| 40 | |
| 41 | /// compress any possible f32 into the range of [0,1]. |
| 42 | /// and then convert it to an unsigned byte. |
| 43 | fn tone_maprust-analyzer cargo exr 1.74.0 tone_map().(linear: f32) -> u8 { |
| 44 | // TODO does the `image` crate expect gamma corrected data? |
| 45 | let clamped = (linear rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub().0.5).tanhrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]tanh().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().0.5 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().0.5; |
| 46 | (clamped rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().255.0) as u8 |
| 47 | } |
| 48 | |
| 49 | // save the png buffer to a png file |
| 50 | let png_buffer = &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.; |
| 51 | png_buffer.saverust-analyzer cargo image 0.25.8 images/buffer/impl#[`ImageBuffer<P, Container>`]save().("rgb.png").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 52 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("created image rgb.png") |
| 53 | } |