extern crate libc;
extern crate num;
use self::libc::{c_int, c_uint, c_void, size_t};
use self::num::Complex;
use crate::defines::{
AfError, ColorMap, ConvDomain, ConvMode, DType, InterpType, MatProp, MatchType,
};
use crate::defines::{BinaryOp, RandomEngineType, SparseFormat};
use crate::error::HANDLE_ERROR;
use crate::num::Zero;
use std::mem;
pub type AfArray = self::libc::c_longlong;
pub type AfIndex = self::libc::c_longlong;
pub type CellPtr = *const self::libc::c_void;
pub type Complex32 = Complex<f32>;
pub type Complex64 = Complex<f64>;
pub type DimT = self::libc::c_longlong;
pub type Feat = *const self::libc::c_void;
pub type Intl = self::libc::c_longlong;
pub type MutAfArray = *mut self::libc::c_longlong;
pub type MutAfIndex = *mut self::libc::c_longlong;
pub type MutDimT = *mut self::libc::c_longlong;
pub type MutDouble = *mut self::libc::c_double;
pub type MutFeat = *mut *mut self::libc::c_void;
pub type MutRandEngine = *mut self::libc::c_longlong;
pub type MutUint = *mut self::libc::c_uint;
pub type MutVoidPtr = *mut self::libc::c_ulonglong;
pub type MutWndHandle = *mut self::libc::c_ulonglong;
pub type RandEngine = self::libc::c_longlong;
pub type Uintl = self::libc::c_ulonglong;
pub type WndHandle = self::libc::c_ulonglong;
#[allow(dead_code)]
extern "C" {
fn af_get_size_of(size: *mut size_t, aftype: c_uint) -> c_int;
fn af_alloc_host(ptr: *mut *const c_void, bytes: DimT) -> c_int;
fn af_free_host(ptr: *mut c_void) -> c_int;
}
pub fn get_size(value: DType) -> usize {
unsafe {
let mut ret_val: usize = 0;
let err_val = af_get_size_of(&mut ret_val as *mut size_t, value as c_uint);
HANDLE_ERROR(AfError::from(err_val));
ret_val
}
}
#[allow(dead_code)]
pub fn alloc_host<T>(elements: usize, _type: DType) -> *const T {
let ptr: *const T = ::std::ptr::null();
let bytes = (elements * get_size(_type)) as DimT;
unsafe {
let err_val = af_alloc_host(&mut (ptr as *const c_void), bytes);
HANDLE_ERROR(AfError::from(err_val));
}
ptr
}
pub fn free_host<T>(ptr: *mut T) {
unsafe {
let err_val = af_free_host(ptr as *mut c_void);
HANDLE_ERROR(AfError::from(err_val));
}
}
impl From<i32> for AfError {
fn from(t: i32) -> Self {
assert!(AfError::SUCCESS as i32 <= t && t <= AfError::ERR_UNKNOWN as i32);
unsafe { mem::transmute(t) }
}
}
impl From<u32> for DType {
fn from(t: u32) -> Self {
assert!(DType::F32 as u32 <= t && t <= DType::U64 as u32);
unsafe { mem::transmute(t) }
}
}
impl From<u32> for InterpType {
fn from(t: u32) -> Self {
assert!(InterpType::NEAREST as u32 <= t && t <= InterpType::BICUBIC_SPLINE as u32);
unsafe { mem::transmute(t) }
}
}
impl From<u32> for ConvMode {
fn from(t: u32) -> Self {
assert!(ConvMode::DEFAULT as u32 <= t && t <= ConvMode::EXPAND as u32);
unsafe { mem::transmute(t) }
}
}
impl From<u32> for ConvDomain {
fn from(t: u32) -> Self {
assert!(ConvDomain::AUTO as u32 <= t && t <= ConvDomain::FREQUENCY as u32);
unsafe { mem::transmute(t) }
}
}
impl From<u32> for MatchType {
fn from(t: u32) -> Self {
assert!(MatchType::SAD as u32 <= t && t <= MatchType::SHD as u32);
unsafe { mem::transmute(t) }
}
}
pub fn to_u32(t: MatProp) -> u32 {
match t {
MatProp::NONE => 0,
MatProp::TRANS => 1,
MatProp::CTRANS => 2,
MatProp::UPPER => 32,
MatProp::LOWER => 64,
MatProp::DIAGUNIT => 128,
MatProp::SYM => 512,
MatProp::POSDEF => 1024,
MatProp::ORTHOG => 2048,
MatProp::TRIDIAG => 4096,
MatProp::BLOCKDIAG => 8192,
}
}
impl From<u32> for ColorMap {
fn from(t: u32) -> Self {
assert!(ColorMap::DEFAULT as u32 <= t && t <= ColorMap::BLUE as u32);
unsafe { mem::transmute(t) }
}
}
pub trait HasAfEnum {
type InType;
type BaseType;
type AbsOutType;
type ArgOutType;
type UnaryOutType;
type ComplexOutType;
type MeanOutType;
type AggregateOutType;
type SobelOutType;
fn get_af_dtype() -> DType;
}
impl HasAfEnum for Complex<f32> {
type InType = Self;
type BaseType = f32;
type AbsOutType = f32;
type ArgOutType = f32;
type UnaryOutType = Self;
type ComplexOutType = Self;
type MeanOutType = Self;
type AggregateOutType = Self;
type SobelOutType = Self;
fn get_af_dtype() -> DType {
DType::C32
}
}
impl HasAfEnum for Complex<f64> {
type InType = Self;
type BaseType = f64;
type AbsOutType = f64;
type ArgOutType = f64;
type UnaryOutType = Self;
type ComplexOutType = Self;
type MeanOutType = Self;
type AggregateOutType = Self;
type SobelOutType = Self;
fn get_af_dtype() -> DType {
DType::C64
}
}
impl HasAfEnum for f32 {
type InType = Self;
type BaseType = Self;
type AbsOutType = f32;
type ArgOutType = f32;
type UnaryOutType = Self;
type ComplexOutType = Complex<f32>;
type MeanOutType = Self;
type AggregateOutType = Self;
type SobelOutType = Self;
fn get_af_dtype() -> DType {
DType::F32
}
}
impl HasAfEnum for f64 {
type InType = Self;
type BaseType = Self;
type AbsOutType = f64;
type ArgOutType = f64;
type UnaryOutType = Self;
type ComplexOutType = Complex<f64>;
type MeanOutType = Self;
type AggregateOutType = Self;
type SobelOutType = Self;
fn get_af_dtype() -> DType {
DType::F64
}
}
impl HasAfEnum for bool {
type InType = Self;
type BaseType = Self;
type AbsOutType = f32;
type ArgOutType = bool;
type UnaryOutType = f32;
type ComplexOutType = Complex<f32>;
type MeanOutType = f32;
type AggregateOutType = u32;
type SobelOutType = i32;
fn get_af_dtype() -> DType {
DType::B8
}
}
impl HasAfEnum for u8 {
type InType = Self;
type BaseType = Self;
type AbsOutType = f32;
type ArgOutType = u8;
type UnaryOutType = f32;
type ComplexOutType = Complex<f32>;
type MeanOutType = f32;
type AggregateOutType = u32;
type SobelOutType = i32;
fn get_af_dtype() -> DType {
DType::U8
}
}
impl HasAfEnum for i16 {
type InType = Self;
type BaseType = Self;
type AbsOutType = f32;
type ArgOutType = i16;
type UnaryOutType = f32;
type ComplexOutType = Complex<f32>;
type MeanOutType = f32;
type AggregateOutType = i32;
type SobelOutType = i32;
fn get_af_dtype() -> DType {
DType::S16
}
}
impl HasAfEnum for u16 {
type InType = Self;
type BaseType = Self;
type AbsOutType = f32;
type ArgOutType = u16;
type UnaryOutType = f32;
type ComplexOutType = Complex<f32>;
type MeanOutType = f32;
type AggregateOutType = u32;
type SobelOutType = i32;
fn get_af_dtype() -> DType {
DType::U16
}
}
impl HasAfEnum for i32 {
type InType = Self;
type BaseType = Self;
type AbsOutType = f32;
type ArgOutType = i32;
type UnaryOutType = f32;
type ComplexOutType = Complex<f32>;
type MeanOutType = f32;
type AggregateOutType = i32;
type SobelOutType = i32;
fn get_af_dtype() -> DType {
DType::S32
}
}
impl HasAfEnum for u32 {
type InType = Self;
type BaseType = Self;
type AbsOutType = f32;
type ArgOutType = u32;
type UnaryOutType = f32;
type ComplexOutType = Complex<f32>;
type MeanOutType = f32;
type AggregateOutType = u32;
type SobelOutType = i32;
fn get_af_dtype() -> DType {
DType::U32
}
}
impl HasAfEnum for i64 {
type InType = Self;
type BaseType = Self;
type AbsOutType = f64;
type ArgOutType = i64;
type UnaryOutType = f64;
type ComplexOutType = Complex<f64>;
type MeanOutType = f64;
type AggregateOutType = Self;
type SobelOutType = i64;
fn get_af_dtype() -> DType {
DType::S64
}
}
impl HasAfEnum for u64 {
type InType = Self;
type BaseType = Self;
type AbsOutType = f64;
type ArgOutType = u64;
type UnaryOutType = f64;
type ComplexOutType = Complex<f64>;
type MeanOutType = f64;
type AggregateOutType = Self;
type SobelOutType = i64;
fn get_af_dtype() -> DType {
DType::U64
}
}
impl From<u32> for SparseFormat {
fn from(t: u32) -> Self {
assert!(SparseFormat::DENSE as u32 <= t && t <= SparseFormat::COO as u32);
unsafe { mem::transmute(t) }
}
}
impl From<u32> for BinaryOp {
fn from(t: u32) -> Self {
assert!(BinaryOp::ADD as u32 <= t && t <= BinaryOp::MAX as u32);
unsafe { mem::transmute(t) }
}
}
impl From<u32> for RandomEngineType {
fn from(t: u32) -> Self {
assert!(
RandomEngineType::PHILOX_4X32_10 as u32 <= t
&& t <= RandomEngineType::MERSENNE_GP11213 as u32
);
unsafe { mem::transmute(t) }
}
}
pub trait ImplicitPromote<RHS> {
type Output;
}
macro_rules! implicit {
[$implType: ident, $rhsType: ident => $outType: ident] => (
impl ImplicitPromote< $rhsType > for $implType {
type Output = $outType;
}
)
}
implicit!(Complex64, Complex64 => Complex64);
implicit!(Complex64, Complex32 => Complex64);
implicit!(Complex64, f64 => Complex64);
implicit!(Complex64, f32 => Complex64);
implicit!(Complex64, i64 => Complex64);
implicit!(Complex64, u64 => Complex64);
implicit!(Complex64, i32 => Complex64);
implicit!(Complex64, u32 => Complex64);
implicit!(Complex64, i16 => Complex64);
implicit!(Complex64, u16 => Complex64);
implicit!(Complex64, bool => Complex64);
implicit!(Complex64, u8 => Complex64);
implicit!(Complex32, Complex64 => Complex64);
implicit!(Complex32, Complex32 => Complex32);
implicit!(Complex32, f64 => Complex64);
implicit!(Complex32, f32 => Complex32);
implicit!(Complex32, i64 => Complex32);
implicit!(Complex32, u64 => Complex32);
implicit!(Complex32, i32 => Complex32);
implicit!(Complex32, u32 => Complex32);
implicit!(Complex32, i16 => Complex32);
implicit!(Complex32, u16 => Complex32);
implicit!(Complex32, bool => Complex32);
implicit!(Complex32, u8 => Complex32);
implicit!(f64, Complex64 => Complex64);
implicit!(f64, Complex32 => Complex64);
implicit!(f64, f64 => f64);
implicit!(f64, f32 => f64);
implicit!(f64, i64 => f64);
implicit!(f64, u64 => f64);
implicit!(f64, i32 => f64);
implicit!(f64, u32 => f64);
implicit!(f64, i16 => f64);
implicit!(f64, u16 => f64);
implicit!(f64, bool => f64);
implicit!(f64, u8 => f64);
implicit!(f32, Complex64 => Complex64);
implicit!(f32, Complex32 => Complex32);
implicit!(f32, f64 => f64);
implicit!(f32, f32 => f32);
implicit!(f32, i64 => f32);
implicit!(f32, u64 => f32);
implicit!(f32, i32 => f32);
implicit!(f32, u32 => f32);
implicit!(f32, i16 => f32);
implicit!(f32, u16 => f32);
implicit!(f32, bool => f32);
implicit!(f32, u8 => f32);
implicit!(i64, Complex64 => Complex64);
implicit!(i64, Complex32 => Complex32);
implicit!(i64, f64 => f64);
implicit!(i64, f32 => f32);
implicit!(i64, i64 => i64);
implicit!(i64, u64 => u64);
implicit!(i64, i32 => i64);
implicit!(i64, u32 => i64);
implicit!(i64, i16 => i64);
implicit!(i64, u16 => i64);
implicit!(i64, bool => i64);
implicit!(i64, u8 => i64);
implicit!(u64, Complex64 => Complex64);
implicit!(u64, Complex32 => Complex32);
implicit!(u64, f64 => f64);
implicit!(u64, f32 => f32);
implicit!(u64, i64 => u64);
implicit!(u64, u64 => u64);
implicit!(u64, i32 => u64);
implicit!(u64, u32 => u64);
implicit!(u64, i16 => u64);
implicit!(u64, u16 => u64);
implicit!(u64, bool => u64);
implicit!(u64, u8 => u64);
implicit!(i32, Complex64 => Complex64);
implicit!(i32, Complex32 => Complex32);
implicit!(i32, f64 => f64);
implicit!(i32, f32 => f32);
implicit!(i32, i64 => i64);
implicit!(i32, u64 => u64);
implicit!(i32, i32 => i32);
implicit!(i32, u32 => u32);
implicit!(i32, i16 => i32);
implicit!(i32, u16 => i32);
implicit!(i32, bool => i32);
implicit!(i32, u8 => i32);
implicit!(u32, Complex64 => Complex64);
implicit!(u32, Complex32 => Complex32);
implicit!(u32, f64 => f64);
implicit!(u32, f32 => f32);
implicit!(u32, i64 => i64);
implicit!(u32, u64 => u64);
implicit!(u32, i32 => u32);
implicit!(u32, u32 => u32);
implicit!(u32, i16 => u32);
implicit!(u32, u16 => u32);
implicit!(u32, bool => u32);
implicit!(u32, u8 => u32);
implicit!(i16, Complex64 => Complex64);
implicit!(i16, Complex32 => Complex32);
implicit!(i16, f64 => f64);
implicit!(i16, f32 => f32);
implicit!(i16, i64 => i64);
implicit!(i16, u64 => u64);
implicit!(i16, i32 => i32);
implicit!(i16, u32 => u32);
implicit!(i16, i16 => i16);
implicit!(i16, u16 => u16);
implicit!(i16, bool => u16);
implicit!(i16, u8 => u16);
implicit!(u16, Complex64 => Complex64);
implicit!(u16, Complex32 => Complex32);
implicit!(u16, f64 => f64);
implicit!(u16, f32 => f32);
implicit!(u16, i64 => i64);
implicit!(u16, u64 => u64);
implicit!(u16, i32 => i32);
implicit!(u16, u32 => u32);
implicit!(u16, i16 => u16);
implicit!(u16, u16 => u16);
implicit!(u16, bool => u16);
implicit!(u16, u8 => u16);
implicit!(u8, Complex64 => Complex64);
implicit!(u8, Complex32 => Complex32);
implicit!(u8, f64 => f64);
implicit!(u8, f32 => f32);
implicit!(u8, i64 => i64);
implicit!(u8, u64 => u64);
implicit!(u8, i32 => i32);
implicit!(u8, u32 => u32);
implicit!(u8, i16 => i16);
implicit!(u8, u16 => u16);
implicit!(u8, bool => u8);
implicit!(u8, u8 => u8);
implicit!(bool, Complex64 => Complex64);
implicit!(bool, Complex32 => Complex32);
implicit!(bool, f64 => f64);
implicit!(bool, f32 => f32);
implicit!(bool, i64 => i64);
implicit!(bool, u64 => u64);
implicit!(bool, i32 => i32);
implicit!(bool, u32 => u32);
implicit!(bool, i16 => i16);
implicit!(bool, u16 => u16);
implicit!(bool, bool => bool);
implicit!(bool, u8 => u8);
impl Zero for Complex64 {
fn zero() -> Self {
Self { re: 0.0, im: 0.0 }
}
}
impl Zero for Complex32 {
fn zero() -> Self {
Self { re: 0.0, im: 0.0 }
}
}
pub trait FloatingPoint {
fn is_real() -> bool {
false
}
fn is_complex() -> bool {
false
}
}
impl FloatingPoint for Complex<f64> {
fn is_complex() -> bool {
true
}
}
impl FloatingPoint for Complex<f32> {
fn is_complex() -> bool {
true
}
}
impl FloatingPoint for f64 {
fn is_real() -> bool {
true
}
}
impl FloatingPoint for f32 {
fn is_real() -> bool {
true
}
}
pub trait RealFloating {}
impl RealFloating for f64 {}
impl RealFloating for f32 {}
pub trait ComplexFloating {}
impl ComplexFloating for Complex64 {}
impl ComplexFloating for Complex32 {}
pub trait RealNumber {}
impl RealNumber for f64 {}
impl RealNumber for f32 {}
impl RealNumber for i32 {}
impl RealNumber for u32 {}
impl RealNumber for i16 {}
impl RealNumber for u16 {}
impl RealNumber for u8 {}
impl RealNumber for bool {}
impl RealNumber for u64 {}
impl RealNumber for i64 {}
pub trait Scanable {}
impl Scanable for i32 {}
impl Scanable for u32 {}
impl Scanable for u64 {}
impl Scanable for i64 {}
pub trait ImageNativeType {}
impl ImageNativeType for f32 {}
impl ImageNativeType for u16 {}
impl ImageNativeType for u8 {}
pub trait ImageFilterType {}
impl ImageFilterType for f64 {}
impl ImageFilterType for f32 {}
impl ImageFilterType for i32 {}
impl ImageFilterType for u32 {}
impl ImageFilterType for i16 {}
impl ImageFilterType for u16 {}
impl ImageFilterType for u8 {}
impl ImageFilterType for bool {}
pub trait GrayRGBConvertible {}
impl GrayRGBConvertible for f64 {}
impl GrayRGBConvertible for f32 {}
impl GrayRGBConvertible for i32 {}
impl GrayRGBConvertible for u32 {}
impl GrayRGBConvertible for i16 {}
impl GrayRGBConvertible for u16 {}
impl GrayRGBConvertible for u8 {}
pub trait MomentsComputable {}
impl MomentsComputable for f64 {}
impl MomentsComputable for f32 {}
impl MomentsComputable for i32 {}
impl MomentsComputable for u32 {}
impl MomentsComputable for i16 {}
impl MomentsComputable for u16 {}
impl MomentsComputable for u8 {}
pub trait MedianComputable {}
impl MedianComputable for f64 {}
impl MedianComputable for f32 {}
impl MedianComputable for i32 {}
impl MedianComputable for u32 {}
impl MedianComputable for i16 {}
impl MedianComputable for u16 {}
impl MedianComputable for u8 {}
pub trait EdgeComputable {}
impl EdgeComputable for f64 {}
impl EdgeComputable for f32 {}
impl EdgeComputable for i32 {}
impl EdgeComputable for u32 {}
impl EdgeComputable for i16 {}
impl EdgeComputable for u16 {}
impl EdgeComputable for u8 {}
pub trait CovarianceComputable {}
impl CovarianceComputable for f64 {}
impl CovarianceComputable for f32 {}
impl CovarianceComputable for i32 {}
impl CovarianceComputable for u32 {}
impl CovarianceComputable for i16 {}
impl CovarianceComputable for u16 {}
impl CovarianceComputable for u8 {}
impl CovarianceComputable for u64 {}
impl CovarianceComputable for i64 {}