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.
This commit is contained in:
Pete Gadomski 2016-08-02 11:20:34 -06:00
parent 1dcd0f6933
commit b46f8a47ac
2 changed files with 44 additions and 1 deletions

View file

@ -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<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!(
get_filename, set_filename, MagickGetFilename, MagickSetFilename
get_font, set_font, MagickGetFont, MagickSetFont

View file

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