From b46f8a47acb6bbea71954482493a85d80791e23e Mon Sep 17 00:00:00 2001 From: Pete Gadomski Date: Tue, 2 Aug 2016 11:20:34 -0600 Subject: [PATCH] Add `write_images_blob` This method goes down to `MagickGetImagesBlob`, which allows for creating animated gifs. I didn't abstract out common operations between the two in the interest of not overly polluting the codebase. However, this new method is almost identical to `write_image_blob` so you could probably abstract something out of there. --- src/wand/magick.rs | 21 +++++++++++++++++++++ tests/lib.rs | 24 +++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/wand/magick.rs b/src/wand/magick.rs index 0861428..24330a8 100644 --- a/src/wand/magick.rs +++ b/src/wand/magick.rs @@ -218,6 +218,27 @@ impl MagickWand { 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, &'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!( get_filename, set_filename, MagickGetFilename, MagickSetFilename get_font, set_font, MagickGetFont, MagickSetFont diff --git a/tests/lib.rs b/tests/lib.rs index 8b0acfd..6d51a86 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -82,7 +82,7 @@ fn test_read_from_blob() { } #[test] -fn test_write_to_blob() { +fn test_write_image_to_blob() { START.call_once(|| { magick_wand_genesis(); }); @@ -103,6 +103,28 @@ fn test_write_to_blob() { 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] fn test_fit() { START.call_once(|| {