Skip to content

Commit

Permalink
Add crop operation implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Shnatsel committed Jul 28, 2024
1 parent fa3f645 commit d2459b9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/arg_parsers/filename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ use super::{Geometry, ResizeGeometry};
pub struct InputFileArg {
path: PathBuf,
//format: Option<String>, // TODO: turn into an enum and enable
read_mod: ReadModifier,
read_mod: Option<ReadModifier>,
}

/// The action to be taken upon loading the image.
/// `convert` accepts any single one of: frame selection, resize, or crop.
///
/// See <https://imagemagick.org/Usage/files/#read_mods> for details.
/// I've also verified it behaves according to the documentation.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ReadModifier {
Resize(ResizeGeometry),
Crop(LoadCropGeometry),
Expand Down
10 changes: 10 additions & 0 deletions src/operations/crop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::{arg_parsers::LoadCropGeometry, wm_try};

pub fn crop_on_load(
image: &mut image::DynamicImage,
geom: &LoadCropGeometry,
) -> Result<(), crate::error::MagickError> {
let cropped = wm_try!(image.crop_imm(geom.xoffset, geom.yoffset, geom.width, geom.height));
*image = cropped;
Ok(())
}
8 changes: 7 additions & 1 deletion src/operations/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
mod crop;
mod resize;

use image::DynamicImage;

use crate::{arg_parsers::ResizeGeometry, error::MagickError};
use crate::{
arg_parsers::{LoadCropGeometry, ResizeGeometry},
error::MagickError,
};

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Operation {
Resize(ResizeGeometry),
Thumbnail(ResizeGeometry),
Scale(ResizeGeometry),
Sample(ResizeGeometry),
CropOnLoad(LoadCropGeometry),
}

impl Operation {
Expand All @@ -19,6 +24,7 @@ impl Operation {
Operation::Thumbnail(geom) => resize::thumbnail(image, geom),
Operation::Scale(geom) => resize::scale(image, geom),
Operation::Sample(geom) => resize::sample(image, geom),
Operation::CropOnLoad(geom) => crop::crop_on_load(image, geom),
}
}
}

0 comments on commit d2459b9

Please sign in to comment.