Detect and correct non-optimal image orientation

cargo test passes
This commit is contained in:
Nathan Fiedler 2016-03-29 18:28:33 -07:00
parent 3732fd5747
commit faebc468ba
5 changed files with 47 additions and 4 deletions

View file

@ -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

View file

@ -1,6 +1,6 @@
[package]
name = "magick_rust"
version = "0.3.3"
version = "0.4.0"
authors = ["Nathan Fiedler <nathanfiedler@fastmail.fm>"]
description = "Selection of Rust bindings for the ImageMagick library."
homepage = "https://github.com/nlfiedler/magick-rust"

View file

@ -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);

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

View file

@ -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());
}