| 1 | |
| 2 | extern crate image as pngrust-analyzer cargo exr 1.74.0 png/; |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#; |
| 4 | |
| 5 | extern crate exrrust-analyzer cargo exr 1.74.0 exr/; |
| 6 | |
| 7 | /// Extract all exr pixel information into pngs. |
| 8 | /// Writes each channel of each mip map of each layer as one grayscale png. |
| 9 | /// May appear black for single-color images. |
| 10 | pub fn mainrust-analyzer cargo exr 1.74.0 main().() { |
| 11 | use exrrust-analyzer cargo exr 1.74.0 crate/::preluderust-analyzer cargo exr 1.74.0 prelude/::*; |
| 12 | |
| 13 | let path = "mip_maps.exr"; |
| 14 | let start_time = ::stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::timerust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/::Instantrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/Instant#::nowrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]now().(); |
| 15 | |
| 16 | // load the exr file from disk with multi-core decompression |
| 17 | let image = read_all_data_from_filerust-analyzer cargo exr 1.74.0 image/read/read_all_data_from_file().(path) |
| 18 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("run example `5c_write_mip_maps` to generate this image file"); |
| 19 | |
| 20 | // warning: highly unscientific benchmarks ahead! |
| 21 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("\nloaded file in {:?}s", start_time.elapsedrust-analyzer cargo std https://github.com/rust-lang/rust/library/std time/impl#[Instant]elapsed().().as_secs_f32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core time/impl#[Duration]as_secs_f32().()); |
| 22 | let _ = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::create_dir_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/create_dir_all().("pngs/"); |
| 23 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("writing images..."); |
| 24 | |
| 25 | for (layer_index, layer) in image.layer_datarust-analyzer cargo exr 1.74.0 image/Image#layer_data..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() { |
| 26 | let layer_name = layer.attributesrust-analyzer cargo exr 1.74.0 image/Layer#attributes..layer_namerust-analyzer cargo exr 1.74.0 meta/header/LayerAttributes#layer_name..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().() |
| 27 | .map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().("1"), Textrust-analyzer cargo exr 1.74.0 meta/attribute/Text#::to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().); |
| 28 | |
| 29 | for channel in &layer.channel_datarust-analyzer cargo exr 1.74.0 image/Layer#channel_data..listrust-analyzer cargo exr 1.74.0 image/AnyChannels#list. { |
| 30 | for (level, level_size) in layer.levels_with_resolutionrust-analyzer cargo exr 1.74.0 image/impl#[`Layer<ChannelData>`]levels_with_resolution().(&channel.sample_datarust-analyzer cargo exr 1.74.0 image/AnyChannel#sample_data.) { |
| 31 | let data : Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<f32> = level.values_as_f32rust-analyzer cargo exr 1.74.0 image/impl#[FlatSamples]values_as_f32().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 32 | |
| 33 | save_f32_image_as_pngrust-analyzer cargo exr 1.74.0 save_f32_image_as_png().(&data, level_size, formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 34 | "pngs/{} ({}) {}.{}x{}.png", |
| 35 | layer_index, layer_name, channel.namerust-analyzer cargo exr 1.74.0 image/AnyChannel#name., |
| 36 | level_size.widthrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]width().(), level_size.heightrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]height().(), |
| 37 | )) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /// Save raw float data to a PNG file, doing automatic brightness adjustments per channel |
| 43 | fn save_f32_image_as_pngrust-analyzer cargo exr 1.74.0 save_f32_image_as_png().(data: &[f32], size: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>, name: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) { |
| 44 | let mut png_buffer = pngrust-analyzer cargo image 0.25.8 crate/::GrayImagerust-analyzer cargo image 0.25.8 images/buffer/GrayImage#::newrust-analyzer cargo image 0.25.8 images/buffer/impl#[`ImageBuffer<P, Vec<<P as Pixel>::Subpixel>>`]new().(size.widthrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]width().() as u32, size.heightrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]height().() as u32); |
| 45 | let mut sorted = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`][`From<&[T]>`]from().(data); |
| 46 | sorted.sort_byrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort_by().(|a, b| a.partial_cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]partial_cmp().(b).unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Lessrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Less#)); |
| 47 | |
| 48 | // percentile normalization |
| 49 | let max = sorted[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().7 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().sorted.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().8]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().; |
| 50 | let min = sorted[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().sorted.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().8]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().; |
| 51 | |
| 52 | // primitive tone mapping |
| 53 | let tone = |v: f32| (v 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; |
| 54 | let max_toned = tone(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().sorted.lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()); |
| 55 | let min_toned = tone(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().sorted.firstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]first().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()); |
| 56 | |
| 57 | // for each pixel, tone map the value |
| 58 | for (x, y, pixel) in png_buffer.enumerate_pixels_mutrust-analyzer cargo image 0.25.8 images/buffer/impl#[`ImageBuffer<P, Container>`]enumerate_pixels_mut().() { |
| 59 | let v = data[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().y as usize rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().size.0rust-analyzer cargo exr 1.74.0 math/Vec2#0. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().x as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 60 | let v = (v 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().min) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().(max 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().min); |
| 61 | let v = tone(v); |
| 62 | |
| 63 | let v = (v 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().min_toned) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().(max_toned 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().min_toned); |
| 64 | |
| 65 | // TODO does the `image` crate expect gamma corrected data? |
| 66 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().pixel = pngrust-analyzer cargo image 0.25.8 crate/::Lumarust-analyzer cargo image 0.25.8 color/Luma#([(v.maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]max().(0.0).minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]min().(1.0) 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]); |
| 67 | } |
| 68 | |
| 69 | png_buffer.saverust-analyzer cargo image 0.25.8 images/buffer/impl#[`ImageBuffer<P, Container>`]save().(&name).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 70 | } |
| 71 | |
| 72 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("extracted all layers to folder `./pngs/*.png`"); |
| 73 | } |
| 74 |