Use the rust-bindgen crate properly

Using the changes from @gadomski along with some additional work, to get the
generated bindings working again. Works on macOS and FreeBSD 11. A couple of
hacks are needed for FreeBSD, but nothing too serious.

Changed to use the libc prefix, and changed to use the generated enums.

Fixes #22, #15, and #14

cargo test passes
This commit is contained in:
Nathan Fiedler
2017-04-08 16:03:58 -07:00
parent 4507d07c9c
commit e7054d3e35
14 changed files with 152 additions and 8244 deletions

View File

@ -22,9 +22,9 @@ pub trait FromRust<T> {
impl FromRust<bool> for bindings::MagickBooleanType {
fn from_rust(b: bool) -> Self {
if b {
bindings::MagickTrue
bindings::MagickBooleanType::MagickTrue
} else {
bindings::MagickFalse
bindings::MagickBooleanType::MagickFalse
}
}
}

View File

@ -1,51 +0,0 @@
/*
* Copyright 2016 Mattis Marjak
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use ::bindings;
pub enum FilterType {
UndefinedFilter = bindings::UndefinedFilter as isize,
PointFilter = bindings::PointFilter as isize,
BoxFilter = bindings::BoxFilter as isize,
TriangleFilter = bindings::TriangleFilter as isize,
HermiteFilter = bindings::HermiteFilter as isize,
HanningFilter = bindings::HanningFilter as isize,
HammingFilter = bindings::HammingFilter as isize,
BlackmanFilter = bindings::BlackmanFilter as isize,
GaussianFilter = bindings::GaussianFilter as isize,
QuadraticFilter = bindings::QuadraticFilter as isize,
CubicFilter = bindings::CubicFilter as isize,
CatromFilter = bindings::CatromFilter as isize,
MitchellFilter = bindings::MitchellFilter as isize,
JincFilter = bindings::JincFilter as isize,
SincFilter = bindings::SincFilter as isize,
SincFastFilter = bindings::SincFastFilter as isize,
KaiserFilter = bindings::KaiserFilter as isize,
WelshFilter = bindings::WelshFilter as isize,
ParzenFilter = bindings::ParzenFilter as isize,
BohmanFilter = bindings::BohmanFilter as isize,
BartlettFilter = bindings::BartlettFilter as isize,
LagrangeFilter = bindings::LagrangeFilter as isize,
LanczosFilter = bindings::LanczosFilter as isize,
LanczosSharpFilter = bindings::LanczosSharpFilter as isize,
Lanczos2Filter = bindings::Lanczos2Filter as isize,
Lanczos2SharpFilter = bindings::Lanczos2SharpFilter as isize,
RobidouxFilter = bindings::RobidouxFilter as isize,
RobidouxSharpFilter = bindings::RobidouxSharpFilter as isize,
CosineFilter = bindings::CosineFilter as isize,
SplineFilter = bindings::SplineFilter as isize,
LanczosRadiusFilter = bindings::LanczosRadiusFilter as isize,
SentinelFilter = bindings::SentinelFilter as isize
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 Nathan Fiedler
* Copyright 2015-2017 Nathan Fiedler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,26 +28,26 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(improper_ctypes)] // the siginfo_t in waitid() definition in bindings.rs
extern crate libc;
mod wand;
mod conversions;
pub mod filters;
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
pub use wand::*;
pub type size_t = ::bindings::size_t;
pub type ssize_t = ::bindings::ssize_t;
use libc::size_t;
#[cfg(not(target_os = "freebsd"))]
use libc::ssize_t;
/// This function must be called before any other ImageMagick operations
/// are attempted. This function is safe to be called repeatedly.
pub fn magick_wand_genesis() {
unsafe {
match bindings::IsMagickWandInstantiated() {
bindings::MagickTrue => (),
bindings::MagickBooleanType::MagickTrue => (),
_ => bindings::MagickWandGenesis()
}
}
@ -58,7 +58,7 @@ pub fn magick_wand_genesis() {
pub fn magick_wand_terminus() {
unsafe {
match bindings::IsMagickWandInstantiated() {
bindings::MagickTrue => bindings::MagickWandTerminus(),
bindings::MagickBooleanType::MagickTrue => bindings::MagickWandTerminus(),
_ => ()
}
}

View File

@ -16,6 +16,9 @@
use std::fmt;
use std::ffi::{CStr, CString};
use ::bindings;
#[cfg(target_os = "freebsd")]
use libc::size_t;
#[cfg(not(target_os = "freebsd"))]
use ::size_t;
wand_common!(
@ -50,30 +53,30 @@ impl DrawingWand {
);
set_get_unchecked!(
get_gravity, set_gravity, DrawGetGravity, DrawSetGravity, u32
get_gravity, set_gravity, DrawGetGravity, DrawSetGravity, bindings::GravityType
get_opacity, set_opacity, DrawGetOpacity, DrawSetOpacity, f64
get_clip_rule, set_clip_rule, DrawGetClipRule, DrawSetClipRule, u32
get_clip_units, set_clip_units, DrawGetClipUnits, DrawSetClipUnits, u32
get_fill_rule, set_fill_rule, DrawGetFillRule, DrawSetFillRule, u32
get_clip_rule, set_clip_rule, DrawGetClipRule, DrawSetClipRule, bindings::FillRule
get_clip_units, set_clip_units, DrawGetClipUnits, DrawSetClipUnits, bindings::ClipPathUnits
get_fill_rule, set_fill_rule, DrawGetFillRule, DrawSetFillRule, bindings::FillRule
get_fill_opacity, set_fill_opacity, DrawGetFillOpacity, DrawSetFillOpacity, f64
get_font_size, set_font_size, DrawGetFontSize, DrawSetFontSize, f64
get_font_style, set_font_style, DrawGetFontStyle, DrawSetFontStyle, u32
get_font_style, set_font_style, DrawGetFontStyle, DrawSetFontStyle, bindings::StyleType
get_font_weight, set_font_weight, DrawGetFontWeight, DrawSetFontWeight, size_t
get_font_stretch, set_font_stretch, DrawGetFontStretch, DrawSetFontStretch, u32
get_font_stretch, set_font_stretch, DrawGetFontStretch, DrawSetFontStretch, bindings::StretchType
get_stroke_dash_offset, set_stroke_dash_offset, DrawGetStrokeDashOffset, DrawSetStrokeDashOffset, f64
get_stroke_line_cap, set_stroke_line_cap, DrawGetStrokeLineCap, DrawSetStrokeLineCap, u32
get_stroke_line_join, set_stroke_line_join, DrawGetStrokeLineJoin, DrawSetStrokeLineJoin, u32
get_stroke_line_cap, set_stroke_line_cap, DrawGetStrokeLineCap, DrawSetStrokeLineCap, bindings::LineCap
get_stroke_line_join, set_stroke_line_join, DrawGetStrokeLineJoin, DrawSetStrokeLineJoin, bindings::LineJoin
get_stroke_miter_limit, set_stroke_miter_limit, DrawGetStrokeMiterLimit, DrawSetStrokeMiterLimit, size_t
get_stroke_opacity, set_stroke_opacity, DrawGetStrokeOpacity, DrawSetStrokeOpacity, f64
get_stroke_width, set_stroke_width, DrawGetStrokeWidth, DrawSetStrokeWidth, f64
get_stroke_antialias, set_stroke_antialias, DrawGetStrokeAntialias, DrawSetStrokeAntialias, u32
get_stroke_antialias, set_stroke_antialias, DrawGetStrokeAntialias, DrawSetStrokeAntialias, bindings::MagickBooleanType
get_text_alignment, set_text_alignment, DrawGetTextAlignment, DrawSetTextAlignment, u32
get_text_antialias, set_text_antialias, DrawGetTextAntialias, DrawSetTextAntialias, u32
get_text_decoration, set_text_decoration, DrawGetTextDecoration, DrawSetTextDecoration, u32
get_text_direction, set_text_direction, DrawGetTextDirection, DrawSetTextDirection, u32
get_text_alignment, set_text_alignment, DrawGetTextAlignment, DrawSetTextAlignment, bindings::AlignType
get_text_antialias, set_text_antialias, DrawGetTextAntialias, DrawSetTextAntialias, bindings::MagickBooleanType
get_text_decoration, set_text_decoration, DrawGetTextDecoration, DrawSetTextDecoration, bindings::DecorationType
get_text_direction, set_text_direction, DrawGetTextDirection, DrawSetTextDirection, bindings::DirectionType
get_text_kerning, set_text_kerning, DrawGetTextKerning, DrawSetTextKerning, f64
get_text_interline_spacing, set_text_interline_spacing, DrawGetTextInterlineSpacing, DrawSetTextInterlineSpacing, f64
get_text_interword_spacing, set_text_interword_spacing, DrawGetTextInterwordSpacing, DrawSetTextInterwordSpacing, f64

View File

@ -35,17 +35,17 @@ macro_rules! wand_common {
fn clear_exception(&mut self) -> Result<(), &'static str> {
match unsafe { ::bindings::$clear_exc(self.wand) } {
::bindings::MagickTrue => Ok(()),
::bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err(concat!("failed to clear", stringify!($wand), "exception"))
}
}
fn get_exception_type(&self) -> u32 {
fn get_exception_type(&self) -> ::bindings::ExceptionType {
unsafe { ::bindings::$get_exc_type(self.wand) }
}
fn get_exception(&self) -> Result<(String, u32), &'static str> {
let mut severity: u32 = 0;
fn get_exception(&self) -> Result<(String, ::bindings::ExceptionType), &'static str> {
let mut severity: ::bindings::ExceptionType = ::bindings::ExceptionType::UndefinedException;
// TODO: memory management
let ptr = unsafe { ::bindings::$get_exc(self.wand, &mut severity as *mut _) };
if ptr.is_null() {
@ -58,7 +58,7 @@ macro_rules! wand_common {
pub fn is_wand(&self) -> Result<(), &'static str> {
match unsafe { ::bindings::$is_wand(self.wand) } {
::bindings::MagickTrue => Ok(()),
::bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err(concat!(stringify!($wand), " not a wand"))
}
}
@ -91,7 +91,7 @@ macro_rules! set_get {
}
pub fn $set(&mut self, v: $typ) -> Result<(), &'static str> {
match unsafe { ::bindings::$c_set(self.wand, v) } {
::bindings::MagickTrue => Ok(()),
::bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err(concat!(stringify!($set), " returned false"))
}
}
@ -142,7 +142,7 @@ macro_rules! string_set_get {
pub fn $set(&mut self, s: &str) -> Result<(), &'static str> {
let c_string = try!(::std::ffi::CString::new(s).map_err(|_| "could not convert to cstring"));
match unsafe { ::bindings::$c_set(self.wand, c_string.as_ptr()) } {
::bindings::MagickTrue => Ok(()),
::bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err(concat!(stringify!($set), " returned false"))
}
}

View File

@ -16,14 +16,21 @@
use std::fmt;
use std::ptr;
use std::ffi::{CStr, CString};
use libc::{c_uint, c_double, c_void};
use libc::{c_double, c_void};
#[cfg(target_os = "freebsd")]
use libc::{size_t, ssize_t};
#[cfg(not(target_os = "freebsd"))]
use ::{size_t, ssize_t};
use ::filters::FilterType;
use ::bindings;
use ::conversions::*;
use super::{DrawingWand, PixelWand};
wand_common!(
MagickWand,
NewMagickWand, ClearMagickWand, IsMagickWand, CloneMagickWand, DestroyMagickWand,
MagickClearException, MagickGetExceptionType, MagickGetException
);
/// MagickWand is a Rustic wrapper to the Rust bindings to ImageMagick.
///
@ -31,17 +38,11 @@ use super::{DrawingWand, PixelWand};
/// on which operations can be performed via the `MagickWand` functions.
/// When the `MagickWand` is dropped, the ImageMagick wand will be
/// destroyed as well.
wand_common!(
MagickWand,
NewMagickWand, ClearMagickWand, IsMagickWand, CloneMagickWand, DestroyMagickWand,
MagickClearException, MagickGetExceptionType, MagickGetException
);
impl MagickWand {
pub fn new_image(&self, columns: size_t, rows: size_t, pixel_wand: &PixelWand) -> Result<(), &'static str> {
match unsafe { bindings::MagickNewImage(self.wand, columns, rows, pixel_wand.wand) } {
bindings::MagickTrue => Ok(()),
bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err("Could not create image"),
}
}
@ -53,7 +54,7 @@ impl MagickWand {
bindings::MagickSetOption(self.wand, c_key.as_ptr(), c_value.as_ptr())
};
match result {
bindings::MagickTrue => Ok(()),
bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err("failed to set option"),
}
}
@ -61,7 +62,7 @@ impl MagickWand {
pub fn annotate_image(&mut self, drawing_wand: &DrawingWand, x: f64, y: f64, angle: f64, text: &str) -> Result<(), &'static str> {
let c_string = try!(CString::new(text).map_err(|_| "could not convert to cstring"));
match unsafe { bindings::MagickAnnotateImage(self.wand, drawing_wand.wand, x, y, angle, c_string.as_ptr() as *const _) } {
bindings::MagickTrue => Ok(()),
bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err("unable to annotate image")
}
}
@ -79,7 +80,7 @@ impl MagickWand {
bindings::MagickLabelImage(self.wand, c_label.as_ptr())
};
match result {
bindings::MagickTrue => Ok(()),
bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err("failed to add label")
}
}
@ -90,7 +91,7 @@ impl MagickWand {
bindings::MagickWriteImages(self.wand, c_name.as_ptr(), adjoin.to_magick())
};
match result {
bindings::MagickTrue => Ok(()),
bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err("failed to write images")
}
}
@ -102,7 +103,7 @@ impl MagickWand {
bindings::MagickReadImage(self.wand, c_name.as_ptr())
};
match result {
bindings::MagickTrue => Ok(()),
bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err("failed to read image")
}
}
@ -116,7 +117,7 @@ impl MagickWand {
self.wand, int_slice.as_ptr() as *const c_void, size as size_t)
};
match result {
bindings::MagickTrue => Ok(()),
bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err("failed to read image")
}
}
@ -160,11 +161,11 @@ impl MagickWand {
/// blur_factor values greater than 1 create blurriness, while values
/// less than 1 create sharpness.
pub fn resize_image(&self, width: usize, height: usize,
filter: FilterType, blur_factor: f64) {
filter: bindings::FilterTypes, blur_factor: f64) {
unsafe {
bindings::MagickResizeImage(
self.wand, width as size_t, height as size_t,
filter as c_uint, blur_factor as c_double
filter, blur_factor as c_double
);
}
}
@ -187,9 +188,9 @@ impl MagickWand {
}
unsafe {
bindings::MagickResetIterator(self.wand);
while bindings::MagickNextImage(self.wand) != bindings::MagickFalse {
while bindings::MagickNextImage(self.wand) != bindings::MagickBooleanType::MagickFalse {
bindings::MagickResizeImage(self.wand, new_width, new_height,
FilterType::LanczosFilter as c_uint, 1.0);
bindings::FilterTypes::LanczosFilter, 1.0);
}
}
}
@ -198,7 +199,7 @@ impl MagickWand {
/// hence should be "auto" oriented so it is suitable for viewing.
pub fn requires_orientation(&self) -> bool {
unsafe {
bindings::MagickGetImageOrientation(self.wand) != bindings::TopLeftOrientation
bindings::MagickGetImageOrientation(self.wand) != bindings::OrientationType::TopLeftOrientation
}
}
@ -208,7 +209,7 @@ impl MagickWand {
/// Returns `true` if successful or `false` if an error occurred.
pub fn auto_orient(&self) -> bool {
unsafe {
bindings::MagickAutoOrientImage(self.wand) == bindings::MagickTrue
bindings::MagickAutoOrientImage(self.wand) == bindings::MagickBooleanType::MagickTrue
}
}
@ -219,7 +220,7 @@ impl MagickWand {
bindings::MagickWriteImage(self.wand, c_name.as_ptr())
};
match result {
bindings::MagickTrue => Ok(()),
bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err("failed to write image")
}
}
@ -275,36 +276,36 @@ impl MagickWand {
);
set_get!(
get_colorspace, set_colorspace, MagickGetColorspace, MagickSetColorspace, u32
get_compression, set_compression, MagickGetCompression, MagickSetCompression, u32
get_colorspace, set_colorspace, MagickGetColorspace, MagickSetColorspace, bindings::ColorspaceType
get_compression, set_compression, MagickGetCompression, MagickSetCompression, bindings::CompressionType
get_compression_quality, set_compression_quality, MagickGetCompressionQuality, MagickSetCompressionQuality, size_t
get_gravity, set_gravity, MagickGetGravity, MagickSetGravity, u32
get_image_colorspace, set_image_colorspace, MagickGetImageColorspace, MagickSetImageColorspace, u32
get_image_compose, set_image_compose, MagickGetImageCompose, MagickSetImageCompose, u32
get_image_compression, set_image_compression, MagickGetImageCompression, MagickSetImageCompression, u32
get_gravity, set_gravity, MagickGetGravity, MagickSetGravity, bindings::GravityType
get_image_colorspace, set_image_colorspace, MagickGetImageColorspace, MagickSetImageColorspace, bindings::ColorspaceType
get_image_compose, set_image_compose, MagickGetImageCompose, MagickSetImageCompose, bindings::CompositeOperator
get_image_compression, set_image_compression, MagickGetImageCompression, MagickSetImageCompression, bindings::CompressionType
get_image_compression_quality, set_image_compression_quality, MagickGetImageCompressionQuality, MagickSetImageCompressionQuality, size_t
get_image_delay, set_image_delay, MagickGetImageDelay, MagickSetImageDelay, size_t
get_image_depth, set_image_depth, MagickGetImageDepth, MagickSetImageDepth, size_t
get_image_dispose, set_image_dispose, MagickGetImageDispose, MagickSetImageDispose, u32
get_image_endian, set_image_endian, MagickGetImageEndian, MagickSetImageEndian, u32
get_image_dispose, set_image_dispose, MagickGetImageDispose, MagickSetImageDispose, bindings::DisposeType
get_image_endian, set_image_endian, MagickGetImageEndian, MagickSetImageEndian, bindings::EndianType
get_image_fuzz, set_image_fuzz, MagickGetImageFuzz, MagickSetImageFuzz, f64
get_image_gamma, set_image_gamma, MagickGetImageGamma, MagickSetImageGamma, f64
get_image_gravity, set_image_gravity, MagickGetImageGravity, MagickSetImageGravity, u32
get_image_gravity, set_image_gravity, MagickGetImageGravity, MagickSetImageGravity, bindings::GravityType
get_image_index, set_image_index, MagickGetImageIndex, MagickSetImageIndex, ssize_t
get_image_interlace_scheme, set_image_interlace_scheme, MagickGetImageInterlaceScheme, MagickSetImageInterlaceScheme, u32
get_image_interpolate_method, set_image_interpolate_method, MagickGetImageInterpolateMethod, MagickSetImageInterpolateMethod, u32
get_image_interlace_scheme, set_image_interlace_scheme, MagickGetImageInterlaceScheme, MagickSetImageInterlaceScheme, bindings::InterlaceType
get_image_interpolate_method, set_image_interpolate_method, MagickGetImageInterpolateMethod, MagickSetImageInterpolateMethod, bindings::InterpolatePixelMethod
get_image_iterations, set_image_iterations, MagickGetImageIterations, MagickSetImageIterations, size_t
get_image_orientation, set_image_orientation, MagickGetImageOrientation, MagickSetImageOrientation, u32
get_image_rendering_intent, set_image_rendering_intent, MagickGetImageRenderingIntent, MagickSetImageRenderingIntent, u32
get_image_orientation, set_image_orientation, MagickGetImageOrientation, MagickSetImageOrientation, bindings::OrientationType
get_image_rendering_intent, set_image_rendering_intent, MagickGetImageRenderingIntent, MagickSetImageRenderingIntent, bindings::RenderingIntent
get_image_scene, set_image_scene, MagickGetImageScene, MagickSetImageScene, size_t
get_image_type, set_image_type, MagickGetImageType, MagickSetImageType, u32
get_image_units, set_image_units, MagickGetImageUnits, MagickSetImageUnits, u32
get_interlace_scheme, set_interlace_scheme, MagickGetInterlaceScheme, MagickSetInterlaceScheme, u32
get_interpolate_method, set_interpolate_method, MagickGetInterpolateMethod, MagickSetInterpolateMethod, u32
get_image_type, set_image_type, MagickGetImageType, MagickSetImageType, bindings::ImageType
get_image_units, set_image_units, MagickGetImageUnits, MagickSetImageUnits, bindings::ResolutionType
get_interlace_scheme, set_interlace_scheme, MagickGetInterlaceScheme, MagickSetInterlaceScheme, bindings::InterlaceType
get_interpolate_method, set_interpolate_method, MagickGetInterpolateMethod, MagickSetInterpolateMethod, bindings::InterpolatePixelMethod
get_iterator_index, set_iterator_index, MagickGetIteratorIndex, MagickSetIteratorIndex, ssize_t
get_orientation, set_orientation, MagickGetOrientation, MagickSetOrientation, u32
get_orientation, set_orientation, MagickGetOrientation, MagickSetOrientation, bindings::OrientationType
get_pointsize, set_pointsize, MagickGetPointsize, MagickSetPointsize, f64
get_type, set_type, MagickGetType, MagickSetType, u32
get_type, set_type, MagickGetType, MagickSetType, bindings::ImageType
);
}

View File

@ -16,6 +16,9 @@
use std::fmt;
use std::ffi::{CStr, CString};
use ::bindings;
#[cfg(target_os = "freebsd")]
use libc::size_t;
#[cfg(not(target_os = "freebsd"))]
use ::size_t;
#[derive(Default, Debug)]
@ -34,7 +37,7 @@ wand_common!(
impl PixelWand {
pub fn is_similar(&self, other: &PixelWand, fuzz: f64) -> Result<(), &'static str> {
match unsafe { bindings::IsPixelWandSimilar(self.wand, other.wand, fuzz) } {
bindings::MagickTrue => Ok(()),
bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err("not similar")
}
}
@ -73,7 +76,7 @@ impl PixelWand {
pub fn set_color(&mut self, s: &str) -> Result<(), &'static str> {
let c_string = try!(CString::new(s).map_err(|_| "could not convert to cstring"));
match unsafe { bindings::PixelSetColor(self.wand, c_string.as_ptr())} {
bindings::MagickTrue => Ok(()),
bindings::MagickBooleanType::MagickTrue => Ok(()),
_ => Err("failed to set color")
}
}