Skip to content

Commit

Permalink
Add a function to apply Exif rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Shnatsel committed Jul 30, 2024
1 parent f8cea83 commit a799736
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/dynimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,43 @@ impl DynamicImage {
dynamic_map!(*self, ref p => imageops::rotate270(p))
}

/// Applies the [Exif orientation](https://web.archive.org/web/20200412005226/https://www.impulseadventure.com/photo/exif-orientation.html) to the image.
///
/// Orientation is specified in the Exif metadata, and is often written by cameras.
/// It is expressed as an integer in the range 1..=8; passing other values will return an error.
///
/// Due to an implementation detail, orientations 5..=8 copy the image internally.
/// This operation should become truly in-place in the future.
pub fn apply_exif_orientation_in_place(&mut self, orientation: u8) -> Result<(), ImageError> {
// Verified against `convert -auto-orient`
let image = self;
match orientation {
1 => Ok(()), // no transformations needed
2 => Ok(image.fliph_in_place()),
3 => Ok(image.rotate180_in_place()),
4 => Ok(image.flipv_in_place()),
5 => {
let mut new_image = image.rotate90();
new_image.fliph_in_place();
*image = new_image;
Ok(())
}
6 => Ok(*image = image.rotate90()),
7 => {
let mut new_image = image.rotate270();
new_image.fliph_in_place();
*image = new_image;
Ok(())
}
8 => Ok(*image = image.rotate270()),
0 | 9.. => {
return Err(ImageError::Parameter(ParameterError::from_kind(
ParameterErrorKind::Generic(format!("Invalid exif orientation: {orientation}")),
)))
}
}
}

/// Encode this image and write it to ```w```.
///
/// Assumes the writer is buffered. In most cases,
Expand Down

0 comments on commit a799736

Please sign in to comment.