Debian Codegraph @

examples/7_crop_alpha_rgba.rs

raw symbols SCIP
1
2 extern crate image as pngrust-analyzer cargo exr 1.74.0 png/;
3
4 extern crate exrrust-analyzer cargo exr 1.74.0 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 mainrust-analyzer cargo exr 1.74.0 main().() {
12 use exrrust-analyzer cargo exr 1.74.0 crate/::preluderust-analyzer cargo exr 1.74.0 prelude/::*;
13 use exrrust-analyzer cargo exr 1.74.0 crate/::imagerust-analyzer cargo exr 1.74.0 image/::pixel_vecrust-analyzer cargo exr 1.74.0 image/pixel_vec/::*; // import predefined pixel storage
14
15 let path = "tests/images/valid/custom/oh crop.exr";
16
17 type DynamicRgbaPixelrust-analyzer cargo exr 1.74.0 DynamicRgbaPixel# = (Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#, Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#, Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#, Samplerust-analyzer cargo exr 1.74.0 block/samples/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: PixelImagerust-analyzer cargo exr 1.74.0 image/PixelImage#<PixelVecrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#<DynamicRgbaPixelrust-analyzer cargo exr 1.74.0 DynamicRgbaPixel#>, RgbaChannelsrust-analyzer cargo exr 1.74.0 image/RgbaChannels#> = read_first_rgba_layer_from_filerust-analyzer cargo exr 1.74.0 image/read/read_first_rgba_layer_from_file().(
23 path,
24 PixelVecrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#::<DynamicRgbaPixelrust-analyzer cargo exr 1.74.0 DynamicRgbaPixel#>::constructorrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`]constructor().,
25
26 // use this predefined rgba pixel container from the exr crate, requesting any type of pixels with 3 or 4 values
27 PixelVecrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#::set_pixelrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`]set_pixel().
28 ).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("this file exists in the exrs repository. download that?");
29
30 // construct a ~simple~ cropped image
31 let image: Imagerust-analyzer cargo exr 1.74.0 image/Image#<Layerrust-analyzer cargo exr 1.74.0 image/Layer#<CroppedChannelsrust-analyzer cargo exr 1.74.0 image/crop/CroppedChannels#<SpecificChannelsrust-analyzer cargo exr 1.74.0 image/SpecificChannels#<PixelVecrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#<DynamicRgbaPixelrust-analyzer cargo exr 1.74.0 DynamicRgbaPixel#>, RgbaChannelsrust-analyzer cargo exr 1.74.0 image/RgbaChannels#>>>> = Imagerust-analyzer cargo exr 1.74.0 image/Image# {
32 attributesrust-analyzer cargo exr 1.74.0 image/Image#attributes.: image.attributesrust-analyzer cargo exr 1.74.0 image/Image#attributes.,
33
34 // crop each layer
35 layer_datarust-analyzer cargo exr 1.74.0 image/Image#layer_data.: {
36 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cropping layer {:#?}", image.layer_datarust-analyzer cargo exr 1.74.0 image/Image#layer_data.);
37
38 // if has alpha, crop it where alpha is zero
39 image.layer_datarust-analyzer cargo exr 1.74.0 image/Image#layer_data.
40 .crop_whererust-analyzer cargo exr 1.74.0 image/crop/impl#[T][`CropWhere<<T as InspectSample>::Sample>`]crop_where().(|(_r, _g, _b, alpha)| alpha.is_zerorust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]is_zero().())
41 .or_crop_to_1x1_if_emptyrust-analyzer cargo exr 1.74.0 image/crop/impl#[`CropResult<Cropped, Original>`]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.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().("cropped_rgba.exr").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
46 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cropped file to cropped_rgba.exr");
47 }
48