@@ -4,7 +4,8 @@ use array::Array;
4
4
use defines:: { AfError , BorderType , ColorSpace , Connectivity , InterpType , YCCStd , MomentType } ;
5
5
use error:: HANDLE_ERROR ;
6
6
use util:: { AfArray , DimT , HasAfEnum , MutAfArray } ;
7
- use self :: libc:: { uint8_t, c_uint, c_int, c_float, c_double} ;
7
+ use self :: libc:: { uint8_t, c_uint, c_int, c_float, c_double, c_char} ;
8
+ use std:: ffi:: CString ;
8
9
9
10
// unused functions from image.h header
10
11
// af_load_image_memory
@@ -14,10 +15,10 @@ use self::libc::{uint8_t, c_uint, c_int, c_float, c_double};
14
15
#[ allow( dead_code) ]
15
16
extern {
16
17
fn af_gradient ( dx : MutAfArray , dy : MutAfArray , arr : AfArray ) -> c_int ;
17
- fn af_load_image ( out : MutAfArray , filename : * const u8 , iscolor : c_int ) -> c_int ;
18
- fn af_save_image ( filename : * const u8 , input : AfArray ) -> c_int ;
19
- fn af_load_image_native ( out : MutAfArray , filename : * const u8 ) -> c_int ;
20
- fn af_save_image_native ( filename : * const u8 , input : AfArray ) -> c_int ;
18
+ fn af_load_image ( out : MutAfArray , filename : * const c_char , iscolor : c_int ) -> c_int ;
19
+ fn af_save_image ( filename : * const c_char , input : AfArray ) -> c_int ;
20
+ fn af_load_image_native ( out : MutAfArray , filename : * const c_char ) -> c_int ;
21
+ fn af_save_image_native ( filename : * const c_char , input : AfArray ) -> c_int ;
21
22
22
23
fn af_resize ( out : MutAfArray , input : AfArray ,
23
24
odim0 : DimT , odim1 : DimT , method : uint8_t ) -> c_int ;
@@ -135,11 +136,13 @@ pub fn gradient(input: &Array) -> (Array, Array) {
135
136
/// An Array with pixel values loaded from the image
136
137
#[ allow( unused_mut) ]
137
138
pub fn load_image ( filename : String , is_color : bool ) -> Array {
139
+ let cstr_param = match CString :: new ( filename) {
140
+ Ok ( cstr) => cstr,
141
+ Err ( _) => panic ! ( "CString creation from input filename failed" ) ,
142
+ } ;
138
143
unsafe {
139
144
let mut temp: i64 = 0 ;
140
- let err_val = af_load_image ( & mut temp as MutAfArray ,
141
- filename. clone ( ) . as_bytes ( ) . as_ptr ( ) as * const u8 ,
142
- is_color as c_int ) ;
145
+ let err_val = af_load_image ( & mut temp as MutAfArray , cstr_param. as_ptr ( ) , is_color as c_int ) ;
143
146
HANDLE_ERROR ( AfError :: from ( err_val) ) ;
144
147
Array :: from ( temp)
145
148
}
@@ -165,10 +168,13 @@ pub fn load_image(filename: String, is_color: bool) -> Array {
165
168
/// An Array with pixel values loaded from the image
166
169
#[ allow( unused_mut) ]
167
170
pub fn load_image_native ( filename : String ) -> Array {
171
+ let cstr_param = match CString :: new ( filename) {
172
+ Ok ( cstr) => cstr,
173
+ Err ( _) => panic ! ( "CString creation from input filename failed" ) ,
174
+ } ;
168
175
unsafe {
169
176
let mut temp: i64 = 0 ;
170
- let err_val = af_load_image_native ( & mut temp as MutAfArray ,
171
- filename. clone ( ) . as_bytes ( ) . as_ptr ( ) as * const u8 ) ;
177
+ let err_val = af_load_image_native ( & mut temp as MutAfArray , cstr_param. as_ptr ( ) ) ;
172
178
HANDLE_ERROR ( AfError :: from ( err_val) ) ;
173
179
Array :: from ( temp)
174
180
}
@@ -182,9 +188,12 @@ pub fn load_image_native(filename: String) -> Array {
182
188
/// - `input` is the Array to be stored into the image file
183
189
#[ allow( unused_mut) ]
184
190
pub fn save_image ( filename : String , input : & Array ) {
191
+ let cstr_param = match CString :: new ( filename) {
192
+ Ok ( cstr) => cstr,
193
+ Err ( _) => panic ! ( "CString creation from input filename failed" ) ,
194
+ } ;
185
195
unsafe {
186
- let err_val = af_save_image ( filename. clone ( ) . as_bytes ( ) . as_ptr ( ) as * const u8 ,
187
- input. get ( ) as AfArray ) ;
196
+ let err_val = af_save_image ( cstr_param. as_ptr ( ) , input. get ( ) as AfArray ) ;
188
197
HANDLE_ERROR ( AfError :: from ( err_val) ) ;
189
198
}
190
199
}
@@ -207,9 +216,12 @@ pub fn save_image(filename: String, input: &Array) {
207
216
/// - `input` is the Array to be saved. Should be U8 for saving 8-bit image, U16 for 16-bit image, and F32 for 32-bit image.
208
217
#[ allow( unused_mut) ]
209
218
pub fn save_image_native ( filename : String , input : & Array ) {
219
+ let cstr_param = match CString :: new ( filename) {
220
+ Ok ( cstr) => cstr,
221
+ Err ( _) => panic ! ( "CString creation from input filename failed" ) ,
222
+ } ;
210
223
unsafe {
211
- let err_val = af_save_image_native ( filename. clone ( ) . as_bytes ( ) . as_ptr ( ) as * const u8 ,
212
- input. get ( ) as AfArray ) ;
224
+ let err_val = af_save_image_native ( cstr_param. as_ptr ( ) , input. get ( ) as AfArray ) ;
213
225
HANDLE_ERROR ( AfError :: from ( err_val) ) ;
214
226
}
215
227
}
0 commit comments