aboutsummaryrefslogtreecommitdiff
path: root/vendor/exr/examples/7_crop_alpha_any_image.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/exr/examples/7_crop_alpha_any_image.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
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");
+}
+