| 1 | |
| 2 | // exr imports |
| 3 | extern crate exrrust-analyzer cargo exr 1.74.0 exr/; |
| 4 | |
| 5 | /// Read an image and print information about the image into the console. |
| 6 | /// This example shows how to read an image with multiple layers and specific channels. |
| 7 | /// This example does not include resolution levels (mipmaps or ripmaps). |
| 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 | |
| 11 | let 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().() |
| 12 | .largest_resolution_levelrust-analyzer cargo exr 1.74.0 image/read/samples/impl#[ReadFlatSamples]largest_resolution_level().() |
| 13 | |
| 14 | .specific_channelsrust-analyzer cargo exr 1.74.0 image/read/levels/impl#[`ReadLargestLevel<DeepOrFlatSamples>`]specific_channels().() |
| 15 | .optionalrust-analyzer cargo exr 1.74.0 image/read/specific_channels/ReadSpecificChannel#optional().("A", f16rust-analyzer cargo half 2.6.0 binary16/f16#::ONErust-analyzer cargo half 2.6.0 binary16/impl#[f16]ONE.) |
| 16 | .requiredrust-analyzer cargo exr 1.74.0 image/read/specific_channels/ReadSpecificChannel#required().("Y") // TODO also accept a closure with a detailed selection mechanism |
| 17 | .optionalrust-analyzer cargo exr 1.74.0 image/read/specific_channels/ReadSpecificChannel#optional().("right.Y", 0.0) |
| 18 | .collect_pixelsrust-analyzer cargo exr 1.74.0 image/read/specific_channels/ReadSpecificChannel#collect_pixels().( |
| 19 | |resolution, (a_channel, y_channel, y_right_channel)| { |
| 20 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("image contains alpha channel? {}", a_channel.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()); |
| 21 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("image contains stereoscopic luma channel? {}", y_right_channel.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()); |
| 22 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("the type of luma samples is {:?}", y_channel.sample_typerust-analyzer cargo exr 1.74.0 meta/attribute/ChannelDescription#sample_type.); |
| 23 | |
| 24 | vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![(f16rust-analyzer cargo half 2.6.0 binary16/f16#::ZEROrust-analyzer cargo half 2.6.0 binary16/impl#[f16]ZERO., 0.0, 0.0); resolution.widthrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]width().()]; resolution.heightrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]height().()] |
| 25 | }, |
| 26 | |
| 27 | // all samples will be converted to f32 (you can also use the enum `Sample` instead of `f32` here to retain the original data type from the file) |
| 28 | |vec, position, (a,y,yr): (f16rust-analyzer cargo half 2.6.0 binary16/f16#, f32, f32)| { |
| 29 | vec[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().position.yrust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]y().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#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(). = (a, y, yr) |
| 30 | } |
| 31 | ) |
| 32 | |
| 33 | .all_layersrust-analyzer cargo exr 1.74.0 image/read/layers/ReadChannels#all_layers().() |
| 34 | .all_attributesrust-analyzer cargo exr 1.74.0 image/read/image/ReadLayers#all_attributes().() |
| 35 | .on_progressrust-analyzer cargo exr 1.74.0 image/read/image/impl#[`ReadImage<F, L>`]on_progress().(|progress| printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("progress: {:.1}", progress*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().100.0)) |
| 36 | .from_filerust-analyzer cargo exr 1.74.0 image/read/image/impl#[`ReadImage<F, L>`]from_file().("custom_channels.exr") |
| 37 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("run example `4_write_custom_fixed_channels` to generate this image file"); |
| 38 | |
| 39 | // output a random color of each channel of each layer |
| 40 | for layer in &image.layer_datarust-analyzer cargo exr 1.74.0 image/Image#layer_data. { |
| 41 | let (alpha, luma, luma_right) = layer.channel_datarust-analyzer cargo exr 1.74.0 image/Layer#channel_data..pixelsrust-analyzer cargo exr 1.74.0 image/SpecificChannels#pixels..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().().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().(); |
| 42 | |
| 43 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!( |
| 44 | "top left color of layer `{}`: (a, y, yr) = {:?}", |
| 45 | layer.attributesrust-analyzer cargo exr 1.74.0 image/Layer#attributes..layer_namerust-analyzer cargo exr 1.74.0 meta/header/LayerAttributes#layer_name..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Clone]clone().().unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().(), |
| 46 | (alpha.to_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]to_f32().(), luma, luma_right) |
| 47 | ) |
| 48 | } |
| 49 | } |