From db0044fa07e8635f410fc44025a20d9215afb3e7 Mon Sep 17 00:00:00 2001 From: 5ohue <86558263+5ohue@users.noreply.github.com> Date: Sat, 11 May 2024 14:22:49 +0300 Subject: [PATCH] Add `OrientationType` type --- src/types/mod.rs | 6 +++-- src/types/orientation_type.rs | 44 +++++++++++++++++++++++++++++++++++ src/wand/magick.rs | 12 ++++------ src/wand/pixel.rs | 7 +----- 4 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 src/types/orientation_type.rs diff --git a/src/types/mod.rs b/src/types/mod.rs index 9b68edb..deb75bb 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -8,8 +8,9 @@ mod endian_type; mod filter_type; mod gravity_type; mod interlace_type; -mod pixel_interpolate_method; mod metric_type; +mod orientation_type; +mod pixel_interpolate_method; mod resource_type; pub use self::alpha_channel_option::AlphaChannelOption; @@ -22,6 +23,7 @@ pub use self::endian_type::EndianType; pub use self::filter_type::FilterType; pub use self::gravity_type::GravityType; pub use self::interlace_type::InterlaceType; -pub use self::pixel_interpolate_method::PixelInterpolateMethod; pub use self::metric_type::MetricType; +pub use self::orientation_type::OrientationType; +pub use self::pixel_interpolate_method::PixelInterpolateMethod; pub use self::resource_type::ResourceType; diff --git a/src/types/orientation_type.rs b/src/types/orientation_type.rs new file mode 100644 index 0000000..af2e8e7 --- /dev/null +++ b/src/types/orientation_type.rs @@ -0,0 +1,44 @@ +use crate::bindings; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u32)] +pub enum OrientationType { + Undefined = bindings::OrientationType_UndefinedOrientation, + TopLeft = bindings::OrientationType_TopLeftOrientation, + TopRight = bindings::OrientationType_TopRightOrientation, + BottomRight = bindings::OrientationType_BottomRightOrientation, + BottomLeft = bindings::OrientationType_BottomLeftOrientation, + LeftTop = bindings::OrientationType_LeftTopOrientation, + RightTop = bindings::OrientationType_RightTopOrientation, + RightBottom = bindings::OrientationType_RightBottomOrientation, + LeftBottom = bindings::OrientationType_LeftBottomOrientation, +} + +impl Default for OrientationType { + fn default() -> Self { + return OrientationType::Undefined; + } +} + +impl From for bindings::OrientationType { + fn from(value: OrientationType) -> Self { + return value as bindings::OrientationType; + } +} + +impl From for OrientationType { + fn from(value: bindings::OrientationType) -> Self { + /* + * SAFETY: + * + * `OrientationType` has the same repr as `bindings::OrientationType` - u32 + * + * If `value` is less than LeftBottom than it is in the vaild range and can be safely + * reinterpreted as `OrientationType` + */ + if value <= bindings::OrientationType_LeftBottomOrientation { + return unsafe { std::mem::transmute(value) }; + } + return OrientationType::default(); + } +} diff --git a/src/wand/magick.rs b/src/wand/magick.rs index d5f8a94..d9de4e2 100644 --- a/src/wand/magick.rs +++ b/src/wand/magick.rs @@ -41,8 +41,9 @@ use crate::{ FilterType, GravityType, InterlaceType, - PixelInterpolateMethod, MetricType, + OrientationType, + PixelInterpolateMethod, ResourceType }; @@ -884,10 +885,7 @@ impl MagickWand { /// Detect if the loaded image is not in top-left orientation, and /// hence should be "auto" oriented so it is suitable for viewing. pub fn requires_orientation(&self) -> bool { - unsafe { - bindings::MagickGetImageOrientation(self.wand) - != bindings::OrientationType_TopLeftOrientation - } + return self.get_image_orientation() != OrientationType::TopLeft; } /// Automatically adjusts the loaded image so that its orientation is @@ -1192,7 +1190,7 @@ impl MagickWand { get_image_interlace_scheme, set_image_interlace_scheme, MagickGetImageInterlaceScheme, MagickSetImageInterlaceScheme, InterlaceType get_image_interpolate_method, set_image_interpolate_method, MagickGetImageInterpolateMethod, MagickSetImageInterpolateMethod, PixelInterpolateMethod get_image_iterations, set_image_iterations, MagickGetImageIterations, MagickSetImageIterations, usize - get_image_orientation, set_image_orientation, MagickGetImageOrientation, MagickSetImageOrientation, bindings::OrientationType + get_image_orientation, set_image_orientation, MagickGetImageOrientation, MagickSetImageOrientation, OrientationType get_image_rendering_intent, set_image_rendering_intent, MagickGetImageRenderingIntent, MagickSetImageRenderingIntent, bindings::RenderingIntent get_image_scene, set_image_scene, MagickGetImageScene, MagickSetImageScene, usize get_image_type, set_image_type, MagickGetImageType, MagickSetImageType, bindings::ImageType @@ -1200,7 +1198,7 @@ impl MagickWand { get_interlace_scheme, set_interlace_scheme, MagickGetInterlaceScheme, MagickSetInterlaceScheme, InterlaceType get_interpolate_method, set_interpolate_method, MagickGetInterpolateMethod, MagickSetInterpolateMethod, PixelInterpolateMethod get_iterator_index, set_iterator_index, MagickGetIteratorIndex, MagickSetIteratorIndex, isize - get_orientation, set_orientation, MagickGetOrientation, MagickSetOrientation, bindings::OrientationType + get_orientation, set_orientation, MagickGetOrientation, MagickSetOrientation, OrientationType get_pointsize, set_pointsize, MagickGetPointsize, MagickSetPointsize, f64 get_type, set_type, MagickGetType, MagickSetType, bindings::ImageType ); diff --git a/src/wand/pixel.rs b/src/wand/pixel.rs index d51f1d8..c63ff23 100644 --- a/src/wand/pixel.rs +++ b/src/wand/pixel.rs @@ -16,11 +16,6 @@ use std::ffi::{CStr, CString}; use std::fmt; -#[cfg(target_os = "freebsd")] -use libc::size_t; -#[cfg(not(target_os = "freebsd"))] -use size_t; - use bindings; use result::MagickError; @@ -99,7 +94,7 @@ impl PixelWand { ); set_get_unchecked!( - get_color_count, set_color_count, PixelGetColorCount, PixelSetColorCount, size_t + get_color_count, set_color_count, PixelGetColorCount, PixelSetColorCount, usize get_index, set_index, PixelGetIndex, PixelSetIndex, f32 get_fuzz, set_fuzz, PixelGetFuzz, PixelSetFuzz, f64 );