Merge pull request #12 from gadomski/write-images-blob

Add `write_images_blob`
This commit is contained in:
Nathan Fiedler
2016-08-03 20:31:15 -07:00
committed by GitHub
2 changed files with 44 additions and 1 deletions

View File

@ -218,6 +218,27 @@ impl MagickWand {
Ok(bytes) Ok(bytes)
} }
/// Write the images in the desired format to a new blob.
///
/// The `format` argument may be any ImageMagick supported image
/// format (e.g. GIF, JPEG, PNG, etc).
pub fn write_images_blob(&self, format: &str) -> Result<Vec<u8>, &'static str> {
let c_format = CString::new(format).unwrap();
let mut length: size_t = 0;
let blob = unsafe {
bindings::MagickSetImageIndex(self.wand, 0);
bindings::MagickSetImageFormat(self.wand, c_format.as_ptr());
bindings::MagickGetImagesBlob(self.wand, &mut length)
};
let mut bytes = Vec::with_capacity(length as usize);
unsafe {
bytes.set_len(length as usize);
ptr::copy_nonoverlapping(blob, bytes.as_mut_ptr(), length as usize);
bindings::MagickRelinquishMemory(blob as *mut c_void);
};
Ok(bytes)
}
string_set_get!( string_set_get!(
get_filename, set_filename, MagickGetFilename, MagickSetFilename get_filename, set_filename, MagickGetFilename, MagickSetFilename
get_font, set_font, MagickGetFont, MagickSetFont get_font, set_font, MagickGetFont, MagickSetFont

View File

@ -82,7 +82,7 @@ fn test_read_from_blob() {
} }
#[test] #[test]
fn test_write_to_blob() { fn test_write_image_to_blob() {
START.call_once(|| { START.call_once(|| {
magick_wand_genesis(); magick_wand_genesis();
}); });
@ -103,6 +103,28 @@ fn test_write_to_blob() {
assert_eq!(384, wand.get_image_height()); assert_eq!(384, wand.get_image_height());
} }
#[test]
fn test_write_images_to_blob() {
START.call_once(|| {
magick_wand_genesis();
});
let wand = MagickWand::new();
assert!(wand.read_image("tests/data/IMG_5745.JPG").is_ok());
assert_eq!(512, wand.get_image_width());
assert_eq!(384, wand.get_image_height());
let blob = wand.write_images_blob("jpeg").unwrap();
if cfg!(target_os = "macos") {
// yeah, don't know why...
assert_eq!(104061, blob.len());
} else {
assert_eq!(104060, blob.len());
}
// should be able to read it back again
assert!(wand.read_image_blob(&blob).is_ok());
assert_eq!(512, wand.get_image_width());
assert_eq!(384, wand.get_image_height());
}
#[test] #[test]
fn test_fit() { fn test_fit() {
START.call_once(|| { START.call_once(|| {