Debian Codegraph @

examples/7_crop_alpha_any_image.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 /// Read an arbitrary image, crop away transparent pixels,
7 /// then write the cropped result to another file.
8 pub 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 path = "tests/images/valid/custom/oh crop.exr";
12
13 // loads any image (excluding deep data)
14 let image: FlatImagerust-analyzer cargo exr 1.74.0 image/FlatImage# = read_all_flat_layers_from_filerust-analyzer cargo exr 1.74.0 image/read/read_all_flat_layers_from_file().(path)
15 .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?");
16
17 // construct a cropped image
18 let image = Imagerust-analyzer cargo exr 1.74.0 image/Image# {
19 attributesrust-analyzer cargo exr 1.74.0 image/Image#attributes.: image.attributesrust-analyzer cargo exr 1.74.0 image/Image#attributes.,
20
21 // crop each layer
22 layer_datarust-analyzer cargo exr 1.74.0 image/Image#layer_data.: image.layer_datarust-analyzer cargo exr 1.74.0 image/Image#layer_data..into_iterrust-analyzer cargo smallvec 1.15.1 impl#[`SmallVec<A>`][IntoIterator]into_iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|layer|{
23 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cropping layer {:#?}", layer);
24
25 // find the alpha channel of the layer
26 let alpha_channel_index = layer.channel_datarust-analyzer cargo exr 1.74.0 image/Layer#channel_data..listrust-analyzer cargo exr 1.74.0 image/AnyChannels#list..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
27 .positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]position().(|channel| channel.namerust-analyzer cargo exr 1.74.0 image/AnyChannel#name..eq_case_insensitiverust-analyzer cargo exr 1.74.0 meta/attribute/impl#[Text]eq_case_insensitive().("A"));
28
29 // if has alpha, crop it where alpha is zero
30 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(alpha_channel_index) = alpha_channel_index {
31 layer.crop_whererust-analyzer cargo exr 1.74.0 image/crop/impl#[T][`CropWhere<<T as InspectSample>::Sample>`]crop_where().(|pixel: FlatSamplesPixelrust-analyzer cargo exr 1.74.0 image/FlatSamplesPixel#| pixel[rust-analyzer cargo smallvec 1.15.1 impl#[`SmallVec<A>`][`Index<I>`]index().alpha_channel_index]rust-analyzer cargo smallvec 1.15.1 impl#[`SmallVec<A>`][`Index<I>`]index()..is_zerorust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]is_zero().())
32 .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
33 .reallocate_croppedrust-analyzer cargo exr 1.74.0 image/crop/impl#[`Layer<CroppedChannels<AnyChannels<FlatSamples>>>`][ApplyCroppedView]reallocate_cropped().() // actually perform the crop operation
34 }
35 else {
36 // return the original layer, as no alpha channel can be used for cropping
37 layer
38 }
39
40 }).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Layersrust-analyzer cargo exr 1.74.0 image/Layers#<_>>(),
41 };
42
43 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.exr").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
44 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("cropped file to cropped.exr");
45 }
46