| 1 |
|
| 2 |
#[macro_use] |
| 3 |
extern crate smallvec; |
| 4 |
extern crate rand; |
| 5 |
extern crate half; |
| 6 |
|
| 7 |
|
| 8 |
// exr imports |
| 9 |
extern crate 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 main() { |
| 20 |
use exr::prelude::*; |
| 21 |
// TODO simplify handling these types of layers using read() and write() |
| 22 |
|
| 23 |
let size = Vec2(512, 512); |
| 24 |
|
| 25 |
let create_channel = |name: &str| -> AnyChannel<FlatSamples> { |
| 26 |
let color: f16 = f16::from_bits(rand::random::<u16>()); |
| 27 |
|
| 28 |
AnyChannel::new( |
| 29 |
name, |
| 30 |
FlatSamples::F16(vec![color; size.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 = Layer::new( |
| 58 |
size, |
| 59 |
LayerAttributes::named("test-image"), |
| 60 |
Encoding::FAST_LOSSLESS, |
| 61 |
AnyChannels::sort(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 = Image::from_layer(layer); |
| 68 |
|
| 69 |
println!("writing image {:#?}", image); |
| 70 |
image.write().to_file("groups.exr").unwrap(); |
| 71 |
|
| 72 |
println!("created file groups.exr"); |
| 73 |
} |