aboutsummaryrefslogtreecommitdiff
path: root/vendor/exr/examples/7_crop_alpha_any_image.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/exr/examples/7_crop_alpha_any_image.rs')
-rw-r--r--vendor/exr/examples/7_crop_alpha_any_image.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/vendor/exr/examples/7_crop_alpha_any_image.rs b/vendor/exr/examples/7_crop_alpha_any_image.rs
new file mode 100644
index 0000000..726beda
--- /dev/null
+++ b/vendor/exr/examples/7_crop_alpha_any_image.rs
@@ -0,0 +1,46 @@
+
+extern crate image as png;
+
+extern crate exr;
+
+/// Read an arbitrary image, crop away transparent pixels,
+/// then write the cropped result to another file.
+pub fn main() {
+ use exr::prelude::*;
+
+ let path = "tests/images/valid/custom/oh crop.exr";
+
+ // loads any image (excluding deep data)
+ let image: FlatImage = read_all_flat_layers_from_file(path)
+ .expect("this file exists in the exrs repository. download that?");
+
+ // construct a cropped image
+ let image = Image {
+ attributes: image.attributes,
+
+ // crop each layer
+ layer_data: image.layer_data.into_iter().map(|layer|{
+ println!("cropping layer {:#?}", layer);
+
+ // find the alpha channel of the layer
+ let alpha_channel_index = layer.channel_data.list.iter()
+ .position(|channel| channel.name.eq_case_insensitive("A"));
+
+ // if has alpha, crop it where alpha is zero
+ if let Some(alpha_channel_index) = alpha_channel_index {
+ layer.crop_where(|pixel: FlatSamplesPixel| pixel[alpha_channel_index].is_zero())
+ .or_crop_to_1x1_if_empty() // do not remove empty layers from image, because it could result in an image without content
+ .reallocate_cropped() // actually perform the crop operation
+ }
+ else {
+ // return the original layer, as no alpha channel can be used for cropping
+ layer
+ }
+
+ }).collect::<Layers<_>>(),
+ };
+
+ image.write().to_file("cropped.exr").unwrap();
+ println!("cropped file to cropped.exr");
+}
+