Read image data from a vector of bytes
cargo test passes
This commit is contained in:
parent
1781b39863
commit
78d681608d
2 changed files with 40 additions and 5 deletions
19
src/lib.rs
19
src/lib.rs
|
@ -27,7 +27,7 @@
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use libc::{c_uint, size_t, c_double};
|
use libc::{c_uint, size_t, c_double, c_void};
|
||||||
use filters::FilterType;
|
use filters::FilterType;
|
||||||
|
|
||||||
mod bindings;
|
mod bindings;
|
||||||
|
@ -59,10 +59,19 @@ impl MagickWand {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: make a Rustic wrapper for reading a blob from bytes
|
/// Read the image data from the vector of bytes.
|
||||||
// pub fn MagickReadImageBlob(arg1: *mut MagickWand,
|
pub fn read_image_blob(&self, data: Vec<u8>) -> Result<(), &'static str> {
|
||||||
// arg2: *const ::libc::c_void, arg3: size_t)
|
let int_slice = &data[..];
|
||||||
// -> MagickBooleanType;
|
let size = data.len();
|
||||||
|
let result = unsafe {
|
||||||
|
bindings::MagickReadImageBlob(
|
||||||
|
self.wand, int_slice.as_ptr() as *const c_void, size as u64)
|
||||||
|
};
|
||||||
|
match result {
|
||||||
|
bindings::MagickTrue => Ok(()),
|
||||||
|
_ => Err("failed to read image")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Retrieve the width of the image.
|
/// Retrieve the width of the image.
|
||||||
pub fn get_image_width(&self) -> usize {
|
pub fn get_image_width(&self) -> usize {
|
||||||
|
|
26
tests/lib.rs
26
tests/lib.rs
|
@ -19,6 +19,10 @@ extern crate magick_rust;
|
||||||
use magick_rust::{MagickWand, magick_wand_genesis};
|
use magick_rust::{MagickWand, magick_wand_genesis};
|
||||||
use magick_rust::filters::{FilterType};
|
use magick_rust::filters::{FilterType};
|
||||||
|
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
use std::path::Path;
|
||||||
use std::sync::{Once, ONCE_INIT};
|
use std::sync::{Once, ONCE_INIT};
|
||||||
|
|
||||||
// Used to make sure MagickWand is initialized exactly once. Note that we
|
// Used to make sure MagickWand is initialized exactly once. Note that we
|
||||||
|
@ -54,3 +58,25 @@ fn test_resize_image() {
|
||||||
assert_eq!(256, wand.get_image_width());
|
assert_eq!(256, wand.get_image_width());
|
||||||
assert_eq!(192, wand.get_image_height());
|
assert_eq!(192, wand.get_image_height());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_read_from_blob() {
|
||||||
|
START.call_once(|| {
|
||||||
|
magick_wand_genesis();
|
||||||
|
});
|
||||||
|
let wand = MagickWand::new();
|
||||||
|
|
||||||
|
let path = Path::new("tests/data/IMG_5745.JPG");
|
||||||
|
let mut file = match File::open(&path) {
|
||||||
|
Err(why) => panic!("couldn't open file: {}", Error::description(&why)),
|
||||||
|
Ok(file) => file
|
||||||
|
};
|
||||||
|
let mut data: Vec<u8> = Vec::new();
|
||||||
|
match file.read_to_end(&mut data) {
|
||||||
|
Err(why) => panic!("couldn't read file: {}", Error::description(&why)),
|
||||||
|
Ok(_) => ()
|
||||||
|
};
|
||||||
|
assert!(wand.read_image_blob(data).is_ok());
|
||||||
|
assert_eq!(512, wand.get_image_width());
|
||||||
|
assert_eq!(384, wand.get_image_height());
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue