diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b89580..44a0597 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [0.4.0] - 2016-03-29 +### Changed +- Add functions for detecting and correcting image orientation. + ## [0.3.3] - 2016-03-17 ### Changed - Allow libc version 0.2 or higher diff --git a/Cargo.toml b/Cargo.toml index 5a029f0..4d19446 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "magick_rust" -version = "0.3.3" +version = "0.4.0" authors = ["Nathan Fiedler "] description = "Selection of Rust bindings for the ImageMagick library." homepage = "https://github.com/nlfiedler/magick-rust" diff --git a/src/lib.rs b/src/lib.rs index 9a41161..b6fc056 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ /* - * Copyright 2015 Nathan Fiedler + * Copyright 2015-2016 Nathan Fiedler * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -157,6 +157,24 @@ impl MagickWand { } } + /// Detect if the loaded image is not in top-left orientation, and + /// hence should be "auto" oriented so it is suitable for viewing. + pub fn requires_orientation(&self) -> bool { + unsafe { + bindings::MagickGetImageOrientation(self.wand) != bindings::TopLeftOrientation + } + } + + /// Automatically adjusts the loaded image so that its orientation is + /// suitable for viewing (i.e. top-left orientation). + /// + /// Returns `true` if successful or `false` if an error occurred. + pub fn auto_orient(&self) -> bool { + unsafe { + bindings::MagickAutoOrientImage(self.wand) == bindings::MagickTrue + } + } + /// Write the current image to the provided path. pub fn write_image(&self, path: &str) -> Result<(), &'static str> { let c_name = CString::new(path).unwrap(); @@ -181,7 +199,6 @@ impl MagickWand { bindings::MagickResetIterator(self.wand); bindings::MagickGetImageBlob(self.wand, &mut length) }; - // would have used Vec::from_raw_buf() but it is unstable let mut bytes = Vec::with_capacity(length as usize); unsafe { bytes.set_len(length as usize); diff --git a/tests/data/IMG_5745_rotl.JPG b/tests/data/IMG_5745_rotl.JPG new file mode 100644 index 0000000..a2177d0 Binary files /dev/null and b/tests/data/IMG_5745_rotl.JPG differ diff --git a/tests/lib.rs b/tests/lib.rs index 18f614f..8f5dca5 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1,5 +1,5 @@ /* - * Copyright 2015 Nathan Fiedler + * Copyright 2015-2016 Nathan Fiedler * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -133,3 +133,25 @@ fn test_get_image_property() { assert!(missing_value.is_err()); assert_eq!("missing property", missing_value.unwrap_err()); } + +#[test] +fn test_requires_orientation() { + START.call_once(|| { + magick_wand_genesis(); + }); + let wand = MagickWand::new(); + assert!(wand.read_image("tests/data/IMG_5745.JPG").is_ok()); + assert_eq!(false, wand.requires_orientation()); +} + +#[test] +fn test_auto_orient() { + START.call_once(|| { + magick_wand_genesis(); + }); + let wand = MagickWand::new(); + assert!(wand.read_image("tests/data/IMG_5745_rotl.JPG").is_ok()); + assert_eq!(true, wand.requires_orientation()); + assert!(wand.auto_orient()); + assert_eq!(false, wand.requires_orientation()); +}