| 1 | |
| 2 | #[macro_use] |
| 3 | extern crate smallvecrust-analyzer cargo exr 1.74.0 smallvec/; |
| 4 | extern crate randrust-analyzer cargo exr 1.74.0 rand/; |
| 5 | extern crate halfrust-analyzer cargo exr 1.74.0 half/; |
| 6 | |
| 7 | |
| 8 | // exr imports |
| 9 | extern crate exrrust-analyzer cargo exr 1.74.0 exr/; |
| 10 | |
| 11 | // TODO create a dedicated reader and writer for this scenario |
| 12 | |
| 13 | /// Generate an image with channel groups and write it to a file. |
| 14 | /// Some legacy software may group layers that contain a `.` in the layer name. |
| 15 | /// |
| 16 | /// Note: This is an OpenEXR legacy strategy. OpenEXR supports layers natively since 2013. |
| 17 | /// Use the natively supported exrs `Layer` types instead, if possible. |
| 18 | /// |
| 19 | fn mainrust-analyzer cargo exr 1.74.0 main().() { |
| 20 | use exrrust-analyzer cargo exr 1.74.0 crate/::preluderust-analyzer cargo exr 1.74.0 prelude/::*; |
| 21 | // TODO simplify handling these types of layers using read() and write() |
| 22 | |
| 23 | let size = Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#(512, 512); |
| 24 | |
| 25 | let create_channel = |name: &str| -> AnyChannelrust-analyzer cargo exr 1.74.0 image/AnyChannel#<FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#> { |
| 26 | let color: f16rust-analyzer cargo half 2.6.0 binary16/f16# = f16rust-analyzer cargo half 2.6.0 binary16/f16#::from_bitsrust-analyzer cargo half 2.6.0 binary16/impl#[f16]from_bits().(randrust-analyzer cargo rand 0.8.5 crate/::randomrust-analyzer cargo rand 0.8.5 random().::<u16>()); |
| 27 | |
| 28 | AnyChannelrust-analyzer cargo exr 1.74.0 image/AnyChannel#::newrust-analyzer cargo exr 1.74.0 image/impl#[`AnyChannel<SampleData>`]new().( |
| 29 | name, |
| 30 | FlatSamplesrust-analyzer cargo exr 1.74.0 image/FlatSamples#::F16rust-analyzer cargo exr 1.74.0 image/FlatSamples#F16#(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![color; size.arearust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]area().() ]) |
| 31 | ) |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | // The channels have the following structure: |
| 36 | // |
| 37 | // - Object |
| 38 | // - Red |
| 39 | // - Green |
| 40 | // - Blue |
| 41 | // - Alpha |
| 42 | |
| 43 | // - Background |
| 44 | // - Red |
| 45 | // - Green |
| 46 | // - Blue |
| 47 | |
| 48 | let foreground_r = create_channel("Object.R"); |
| 49 | let foreground_g = create_channel("Object.G"); |
| 50 | let foreground_b = create_channel("Object.B"); |
| 51 | let foreground_a = create_channel("Object.A"); |
| 52 | |
| 53 | let background_r = create_channel("Background.R"); |
| 54 | let background_g = create_channel("Background.G"); |
| 55 | let background_b = create_channel("Background.B"); |
| 56 | |
| 57 | let layer = Layerrust-analyzer cargo exr 1.74.0 image/Layer#::newrust-analyzer cargo exr 1.74.0 image/impl#[`Layer<ChannelData>`]new().( |
| 58 | size, |
| 59 | LayerAttributesrust-analyzer cargo exr 1.74.0 meta/header/LayerAttributes#::namedrust-analyzer cargo exr 1.74.0 meta/header/impl#[LayerAttributes]named().("test-image"), |
| 60 | Encodingrust-analyzer cargo exr 1.74.0 image/Encoding#::FAST_LOSSLESSrust-analyzer cargo exr 1.74.0 image/impl#[Encoding]FAST_LOSSLESS., |
| 61 | AnyChannelsrust-analyzer cargo exr 1.74.0 image/AnyChannels#::sortrust-analyzer cargo exr 1.74.0 image/impl#[`AnyChannels<SampleData>`]sort().(smallvecrust-analyzer cargo smallvec 1.15.1 smallvec!![ // the order does not actually matter |
| 62 | foreground_r, foreground_g, foreground_b, foreground_a, |
| 63 | background_r, background_g, background_b |
| 64 | ]), |
| 65 | ); |
| 66 | |
| 67 | let image = Imagerust-analyzer cargo exr 1.74.0 image/Image#::from_layerrust-analyzer cargo exr 1.74.0 image/impl#[`Image<Layer<ChannelData>>`]from_layer().(layer); |
| 68 | |
| 69 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("writing image {:#?}", image); |
| 70 | image.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().("groups.exr").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 71 | |
| 72 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("created file groups.exr"); |
| 73 | } |