Debian Codegraph @

.cargo/vendor/exr/examples/7_crop_alpha_rgba.rs

raw
1
2 extern crate image as png;
3
4 extern crate exr;
5
6
7 /// Read an rgba image, or fail if none can be found.
8 /// Then crop away transparent pixels,
9 /// and write the cropped result to another file.
10 /// This retains only the rgb pixels, and no other layers.
11 pub fn main() {
12 use exr::prelude::*;
13 use exr::image::pixel_vec::*; // import predefined pixel storage
14
15 let path = "tests/images/valid/custom/oh crop.exr";
16
17 type DynamicRgbaPixel = (Sample, Sample, Sample, Sample); // `Sample` is an enum containing the original data type (f16,f32, or u32)
18
19 // load an rgba image
20 // this specific example discards all but the first valid rgb layers and converts all pixels to f32 values
21 // TODO optional alpha channel!
22 let image: PixelImage<PixelVec<DynamicRgbaPixel>, RgbaChannels> = read_first_rgba_layer_from_file(
23 path,
24 PixelVec::<DynamicRgbaPixel>::constructor,
25
26 // use this predefined rgba pixel container from the exr crate, requesting any type of pixels with 3 or 4 values
27 PixelVec::set_pixel
28 ).expect("this file exists in the exrs repository. download that?");
29
30 // construct a ~simple~ cropped image
31 let image: Image<Layer<CroppedChannels<SpecificChannels<PixelVec<DynamicRgbaPixel>, RgbaChannels>>>> = Image {
32 attributes: image.attributes,
33
34 // crop each layer
35 layer_data: {
36 println!("cropping layer {:#?}", image.layer_data);
37
38 // if has alpha, crop it where alpha is zero
39 image.layer_data
40 .crop_where(|(_r, _g, _b, alpha)| alpha.is_zero())
41 .or_crop_to_1x1_if_empty() // do not remove empty layers from image, because it could result in an image without content
42 },
43 };
44
45 image.write().to_file("cropped_rgba.exr").unwrap();
46 println!("cropped file to cropped_rgba.exr");
47 }
48