Skip to content

Commit 01277aa

Browse files
committed
API for arrayfire image header
1 parent e2ee445 commit 01277aa

File tree

2 files changed

+345
-0
lines changed

2 files changed

+345
-0
lines changed

src/image/mod.rs

Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
extern crate libc;
2+
3+
use super::Aftype;
4+
use super::Array;
5+
use super::BorderType;
6+
use super::ColorSpace;
7+
use super::Connectivity;
8+
use super::InterpType;
9+
use self::libc::{uint8_t, c_uint, c_int, c_float, c_double};
10+
11+
type MutAfArray = *mut self::libc::c_longlong;
12+
type AfArray = self::libc::c_longlong;
13+
type DimT = self::libc::c_longlong;
14+
15+
#[allow(dead_code)]
16+
extern {
17+
fn af_gradient(dx: MutAfArray, dy: MutAfArray, arr: AfArray) -> c_int;
18+
fn af_load_image(out: MutAfArray, filename: *const u8, iscolor: c_int) -> c_int;
19+
fn af_save_image(filename: *const u8, input: AfArray) -> c_int;
20+
21+
fn af_resize(out: MutAfArray, input: AfArray,
22+
odim0: DimT, odim1: DimT, method: uint8_t) -> c_int;
23+
24+
fn af_transform(out: MutAfArray, input: AfArray, trans: AfArray,
25+
odim0: DimT, odim1: DimT, method: uint8_t, is_inverse: c_int) -> c_int;
26+
27+
fn af_rotate(out: MutAfArray, input: AfArray, theta: c_float, crop: c_int,
28+
method: uint8_t) -> c_int;
29+
30+
fn af_translate(out: MutAfArray, input: AfArray, trans0: c_float, trans1: c_float,
31+
odim0: DimT, odim1: DimT, method: uint8_t) -> c_int;
32+
33+
fn af_scale(out: MutAfArray, input: AfArray, scale0: c_float, scale1: c_float,
34+
odim0: DimT, odim1: DimT, method: uint8_t) -> c_int;
35+
36+
fn af_skew(out: MutAfArray, input: AfArray, skew0: c_float, skew1: c_float,
37+
odim0: DimT, odim1: DimT, method: uint8_t, is_inverse: c_int) -> c_int;
38+
39+
fn af_histogram(out: MutAfArray, input: AfArray, nbins: c_uint,
40+
minval: c_double, maxval: c_double) -> c_int;
41+
42+
fn af_dilate(out: MutAfArray, input: AfArray, mask: AfArray) -> c_int;
43+
fn af_dilate3(out: MutAfArray, input: AfArray, mask: AfArray) -> c_int;
44+
fn af_erode(out: MutAfArray, input: AfArray, mask: AfArray) -> c_int;
45+
fn af_erode3(out: MutAfArray, input: AfArray, mask: AfArray) -> c_int;
46+
fn af_regions(out: MutAfArray, input: AfArray, conn: uint8_t, aftype: uint8_t) -> c_int;
47+
fn af_sobel_operator(dx: MutAfArray, dy: MutAfArray, i: AfArray, ksize: c_uint) -> c_int;
48+
fn af_rgb2gray(out: MutAfArray, input: AfArray, r: c_float, g: c_float, b: c_float) -> c_int;
49+
fn af_gray2rgb(out: MutAfArray, input: AfArray, r: c_float, g: c_float, b: c_float) -> c_int;
50+
fn af_hist_equal(out: MutAfArray, input: AfArray, hist: AfArray) -> c_int;
51+
fn af_hsv2rgb(out: MutAfArray, input: AfArray) -> c_int;
52+
fn af_rgb2hsv(out: MutAfArray, input: AfArray) -> c_int;
53+
54+
fn af_bilateral(out: MutAfArray, input: AfArray,
55+
sp_sig: c_float, ch_sig: c_float, iscolor: c_int) -> c_int;
56+
57+
fn af_mean_shift(out: MutAfArray, input: AfArray, sp_sig: c_float,
58+
ch_sig: c_float, iter: c_uint, iscolor: c_int) -> c_int;
59+
60+
fn af_medfilt(out: MutAfArray, input: AfArray,
61+
wlen: DimT, wwid: DimT, etype: uint8_t) -> c_int;
62+
63+
fn af_minfilt(out: MutAfArray, input: AfArray,
64+
wlen: DimT, wwid: DimT, etype: uint8_t) -> c_int;
65+
66+
fn af_maxfilt(out: MutAfArray, input: AfArray,
67+
wlen: DimT, wwid: DimT, etype: uint8_t) -> c_int;
68+
69+
fn af_gaussian_kernel(out: MutAfArray, rows: c_int, cols: c_int,
70+
sigma_r: c_double, sigma_c: c_double) -> c_int;
71+
72+
fn af_color_space(out: MutAfArray, input: AfArray,
73+
tospace: uint8_t, fromspace: uint8_t) -> c_int;
74+
}
75+
76+
#[allow(unused_mut)]
77+
pub fn gradient(input: &Array) -> (Array, Array) {
78+
unsafe {
79+
let mut dx: i64 = 0;
80+
let mut dy: i64 = 0;
81+
af_gradient(&mut dx as MutAfArray, &mut dy as MutAfArray, input.get() as AfArray);
82+
(Array {handle: dx}, Array {handle: dy})
83+
}
84+
}
85+
86+
#[allow(unused_mut)]
87+
pub fn load_image(filename: &[u8], is_color: bool) -> Array {
88+
unsafe {
89+
let mut temp: i64 = 0;
90+
af_load_image(&mut temp as MutAfArray,
91+
filename.as_ptr() as *const u8, is_color as c_int);
92+
Array {handle: temp}
93+
}
94+
}
95+
96+
#[allow(unused_mut)]
97+
pub fn save_image(filename: &[u8], input: &Array) {
98+
unsafe {
99+
af_save_image(filename.as_ptr() as *const u8, input.get() as AfArray);
100+
}
101+
}
102+
103+
#[allow(unused_mut)]
104+
pub fn resize(input: &Array, odim0: i64, odim1: i64, method: InterpType) -> Array {
105+
unsafe {
106+
let mut temp: i64 = 0;
107+
af_resize(&mut temp as MutAfArray, input.get() as AfArray, odim0 as DimT,
108+
odim1 as DimT, method as uint8_t);
109+
Array {handle: temp}
110+
}
111+
}
112+
113+
#[allow(unused_mut)]
114+
pub fn transform(input: &Array, trans: &Array, odim0: i64, odim1: i64,
115+
method: InterpType, is_inverse: bool) -> Array {
116+
unsafe {
117+
let mut temp: i64 = 0;
118+
af_transform(&mut temp as MutAfArray, input.get() as AfArray, trans.get() as AfArray,
119+
odim0 as DimT, odim1 as DimT, method as uint8_t, is_inverse as c_int);
120+
Array {handle: temp}
121+
}
122+
}
123+
124+
#[allow(unused_mut)]
125+
pub fn rotate(input: &Array, theta: f64, crop: bool, method: InterpType) -> Array {
126+
unsafe {
127+
let mut temp: i64 = 0;
128+
af_rotate(&mut temp as MutAfArray, input.get() as AfArray, theta as c_float,
129+
crop as c_int, method as uint8_t);
130+
Array {handle: temp}
131+
}
132+
}
133+
134+
macro_rules! trans_func_def {
135+
($fn_name: ident, $ffi_name: ident) => (
136+
#[allow(unused_mut)]
137+
pub fn $fn_name(input: &Array, p0: f32, p1: f32,
138+
odim0: i64, odim1: i64, method: InterpType) -> Array {
139+
unsafe {
140+
let mut temp: i64 = 0;
141+
$ffi_name(&mut temp as MutAfArray,
142+
input.get() as AfArray,
143+
p0 as c_float, p1 as c_float,
144+
odim0 as DimT, odim1 as DimT,
145+
method as uint8_t);
146+
Array {handle: temp}
147+
}
148+
}
149+
)
150+
}
151+
152+
trans_func_def!(translate, af_translate);
153+
trans_func_def!(scale, af_scale);
154+
155+
#[allow(unused_mut)]
156+
pub fn skew(input: &Array, skew0: f32, skew1: f32, odim0: i64, odim1: i64,
157+
method: InterpType, is_inverse: bool) -> Array {
158+
unsafe {
159+
let mut temp: i64 = 0;
160+
af_skew(&mut temp as MutAfArray, input.get() as AfArray,
161+
skew0 as c_float, skew1 as c_float, odim0 as DimT, odim1 as DimT,
162+
method as uint8_t, is_inverse as c_int);
163+
Array {handle: temp}
164+
}
165+
}
166+
167+
#[allow(unused_mut)]
168+
pub fn histogram(input: &Array, nbins: u32, minval: f64, maxval: f64) -> Array {
169+
unsafe {
170+
let mut temp: i64 = 0;
171+
af_histogram(&mut temp as MutAfArray, input.get() as AfArray,
172+
nbins as c_uint, minval as c_double, maxval as c_double);
173+
Array {handle: temp}
174+
}
175+
}
176+
177+
macro_rules! morph_func_def {
178+
($fn_name: ident, $ffi_name: ident) => (
179+
#[allow(unused_mut)]
180+
pub fn $fn_name(input: &Array, mask: &Array) -> Array {
181+
unsafe {
182+
let mut temp: i64 = 0;
183+
$ffi_name(&mut temp as MutAfArray, input.get() as AfArray, mask.get() as AfArray);
184+
Array {handle: temp}
185+
}
186+
}
187+
)
188+
}
189+
190+
morph_func_def!(dilate, af_dilate);
191+
morph_func_def!(erode, af_erode);
192+
morph_func_def!(dilate3, af_dilate3);
193+
morph_func_def!(erode3, af_erode3);
194+
195+
#[allow(unused_mut)]
196+
pub fn bilateral(input: &Array, spatial_sigma: f32, chromatic_sigma: f32,
197+
iscolor: bool) -> Array {
198+
unsafe {
199+
let mut temp: i64 = 0;
200+
af_bilateral(&mut temp as MutAfArray, input.get() as AfArray,
201+
spatial_sigma as c_float, chromatic_sigma as c_float,
202+
iscolor as c_int);
203+
Array {handle: temp}
204+
}
205+
}
206+
207+
#[allow(unused_mut)]
208+
pub fn mean_shift(input: &Array, spatial_sigma: f32, chromatic_sigma: f32,
209+
iter: u32, iscolor: bool) -> Array {
210+
unsafe {
211+
let mut temp: i64 = 0;
212+
af_mean_shift(&mut temp as MutAfArray, input.get() as AfArray,
213+
spatial_sigma as c_float, chromatic_sigma as c_float,
214+
iter as c_uint, iscolor as c_int);
215+
Array {handle: temp}
216+
}
217+
}
218+
219+
macro_rules! filt_func_def {
220+
($fn_name: ident, $ffi_name: ident) => (
221+
#[allow(unused_mut)]
222+
pub fn $fn_name(input: &Array, wlen: i64, wwid: i64, etype: BorderType) -> Array {
223+
unsafe {
224+
let mut temp: i64 = 0;
225+
$ffi_name(&mut temp as MutAfArray, input.get() as AfArray,
226+
wlen as DimT, wwid as DimT, etype as uint8_t);
227+
Array {handle: temp}
228+
}
229+
}
230+
)
231+
}
232+
233+
filt_func_def!(medfilt, af_medfilt);
234+
filt_func_def!(minfilt, af_minfilt);
235+
filt_func_def!(maxfilt, af_maxfilt);
236+
237+
#[allow(unused_mut)]
238+
pub fn gaussian_kernel(rows: i32, cols: i32, sigma_r: f64, sigma_c: f64) -> Array {
239+
unsafe {
240+
let mut temp: i64 = 0;
241+
af_gaussian_kernel(&mut temp as MutAfArray, rows as c_int, cols as c_int,
242+
sigma_r as c_double, sigma_c as c_double);
243+
Array {handle: temp}
244+
}
245+
}
246+
247+
#[allow(unused_mut)]
248+
pub fn color_space(input: &Array, tospace: ColorSpace, fromspace: ColorSpace) -> Array {
249+
unsafe {
250+
let mut temp: i64 = 0;
251+
af_color_space(&mut temp as MutAfArray, input.get() as AfArray,
252+
tospace as uint8_t, fromspace as uint8_t);
253+
Array {handle: temp}
254+
}
255+
}
256+
257+
#[allow(unused_mut)]
258+
pub fn regions(input: &Array, conn: Connectivity, aftype: Aftype) -> Array {
259+
unsafe {
260+
let mut temp: i64 = 0;
261+
af_regions(&mut temp as MutAfArray, input.get() as AfArray,
262+
conn as uint8_t, aftype as uint8_t);
263+
Array {handle: temp}
264+
}
265+
}
266+
267+
#[allow(unused_mut)]
268+
pub fn sobel(input: &Array, ker_size: u32) -> (Array, Array) {
269+
unsafe {
270+
let mut dx: i64 = 0;
271+
let mut dy: i64 = 0;
272+
af_sobel_operator(&mut dx as MutAfArray, &mut dy as MutAfArray,
273+
input.get() as AfArray, ker_size as c_uint);
274+
(Array {handle: dx}, Array{handle: dy})
275+
}
276+
}
277+
278+
#[allow(unused_mut)]
279+
pub fn hist_equal(input: &Array, hist: &Array) -> Array {
280+
unsafe {
281+
let mut temp: i64 = 0;
282+
af_hist_equal(&mut temp as MutAfArray, input.get() as AfArray, hist.get() as AfArray);
283+
Array {handle: temp}
284+
}
285+
}
286+
287+
macro_rules! grayrgb_func_def {
288+
($fn_name: ident, $ffi_name: ident) => (
289+
#[allow(unused_mut)]
290+
pub fn $fn_name(input: &Array, r: f32, g: f32, b: f32) -> Array {
291+
unsafe {
292+
let mut temp: i64 = 0;
293+
$ffi_name(&mut temp as MutAfArray, input.get() as AfArray,
294+
r as c_float, g as c_float, b as c_float);
295+
Array {handle: temp}
296+
}
297+
}
298+
)
299+
}
300+
301+
grayrgb_func_def!(rgb2gray, af_rgb2gray);
302+
grayrgb_func_def!(gray2rgb, af_gray2rgb);
303+
304+
macro_rules! hsvrgb_func_def {
305+
($fn_name: ident, $ffi_name: ident) => (
306+
#[allow(unused_mut)]
307+
pub fn $fn_name(input: &Array) -> Array {
308+
unsafe {
309+
let mut temp: i64 = 0;
310+
$ffi_name(&mut temp as MutAfArray, input.get() as AfArray);
311+
Array {handle: temp}
312+
}
313+
}
314+
)
315+
}
316+
317+
hsvrgb_func_def!(hsv2rgb, af_hsv2rgb);
318+
hsvrgb_func_def!(rgb2hsv, af_rgb2hsv);

src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ pub enum InterpType {
2020
CUBIC = 3,
2121
}
2222

23+
#[derive(Copy, Clone)]
24+
pub enum BorderType {
25+
ZERO = 0,
26+
SYMMETRIC = 1,
27+
}
28+
29+
#[derive(Copy, Clone)]
30+
pub enum Connectivity {
31+
FOUR = 4,
32+
EIGHT = 8
33+
}
34+
2335
#[derive(Copy, Clone)]
2436
pub enum ConvMode {
2537
DEFAULT = 0,
@@ -46,6 +58,13 @@ pub enum MatchType {
4658
SHD = 8,
4759
}
4860

61+
#[derive(Copy, Clone)]
62+
pub enum ColorSpace {
63+
GRAY = 0,
64+
RGB = 1,
65+
HSV = 2,
66+
}
67+
4968
#[derive(Copy, Clone)]
5069
pub enum MatProp {
5170
NONE,
@@ -117,6 +136,14 @@ mod device;
117136

118137
mod dim4;
119138

139+
pub use image::{gaussian_kernel, load_image, save_image};
140+
pub use image::{resize, transform, rotate, translate, scale, skew};
141+
pub use image::{dilate, dilate3, erode, erode3, minfilt, maxfilt};
142+
pub use image::{gradient, histogram, hist_equal, regions};
143+
pub use image::{gray2rgb, rgb2gray, hsv2rgb, rgb2hsv, color_space};
144+
pub use image::{bilateral, mean_shift, medfilt, sobel};
145+
mod image;
146+
120147
pub use signal::{approx1, approx2};
121148
pub use signal::{fft, fft2, fft3, ifft, ifft2, ifft3};
122149
pub use signal::{convolve1, convolve2, convolve3, convolve2_sep};

0 commit comments

Comments
 (0)