1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//! MMX image filters

use std::mem;
use libc::{self,size_t, c_void, c_uint, c_int};
use ::get_error;
use c_vec::CVec;
use sys::gfx::imagefilter;

/// MMX detection routine (with override flag).
pub fn mmx_detect() -> bool {
    unsafe { imagefilter::SDL_imageFilterMMXdetect() == 1 }
}

/// Disable MMX check for filter functions and and force to use non-MMX C based code.
pub fn mmx_off() {
    unsafe { imagefilter::SDL_imageFilterMMXoff() }
}

/// Enable MMX check for filter functions and use MMX code if available.
pub fn mmx_on() {
    unsafe { imagefilter::SDL_imageFilterMMXon() }
}

#[inline]
fn cvec_with_size(sz: usize) -> CVec<u8> {
    unsafe {
        let p = libc::malloc(sz as size_t) as *mut u8;
        CVec::new_with_dtor(p, sz, move |p| {
            libc::free(p as *mut c_void)
        })
    }
}

/// Filter using Add: D = saturation255(S1 + S2).
pub fn add(src1: CVec<u8>, src2: CVec<u8>) -> Result<CVec<u8>, String> {
    assert_eq!(src1.len(), src2.len());
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterAdd(mem::transmute(src1.get(0)),
                                              mem::transmute(src2.get(0)),
                                              mem::transmute(dest.get(0)),
                                              size as c_uint) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using Mean: D = S1/2 + S2/2.
pub fn mean(src1: CVec<u8>, src2: CVec<u8>) -> Result<CVec<u8>, String> {
    assert_eq!(src1.len(), src2.len());
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterMean(mem::transmute(src1.get(0)),
                                               mem::transmute(src2.get(0)),
                                               mem::transmute(dest.get(0)),
                                               size as c_uint) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using Sub: D = saturation0(S1 - S2).
pub fn sub(src1: CVec<u8>, src2: CVec<u8>) -> Result<CVec<u8>, String> {
    assert_eq!(src1.len(), src2.len());
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterSub(mem::transmute(src1.get(0)),
                                              mem::transmute(src2.get(0)),
                                              mem::transmute(dest.get(0)),
                                              size as c_uint) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `AbsDiff`: D = | S1 - S2 |.
pub fn abs_diff(src1: CVec<u8>, src2: CVec<u8>) -> Result<CVec<u8>, String> {
    assert_eq!(src1.len(), src2.len());
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterAbsDiff(mem::transmute(src1.get(0)),
                                                  mem::transmute(src2.get(0)),
                                                  mem::transmute(dest.get(0)),
                                                  size as c_uint) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using Mult: D = saturation255(S1 * S2).
pub fn mult(src1: CVec<u8>, src2: CVec<u8>) -> Result<CVec<u8>, String> {
    assert_eq!(src1.len(), src2.len());
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterMult(mem::transmute(src1.get(0)),
                                               mem::transmute(src2.get(0)),
                                               mem::transmute(dest.get(0)),
                                               size as c_uint) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `MultNor`: D = S1 * S2.
pub fn mult_nor(src1: CVec<u8>, src2: CVec<u8>) -> Result<CVec<u8>, String> {
    assert_eq!(src1.len(), src2.len());
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterMultNor(mem::transmute(src1.get(0)),
                                                  mem::transmute(src2.get(0)),
                                                  mem::transmute(dest.get(0)),
                                                  size as c_uint) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `MultDivby2`: D = saturation255(S1/2 * S2).
pub fn mult_div_by2(src1: CVec<u8>, src2: CVec<u8>) -> Result<CVec<u8>, String> {
    assert_eq!(src1.len(), src2.len());
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterMultDivby2(mem::transmute(src1.get(0)),
                                                     mem::transmute(src2.get(0)),
                                                     mem::transmute(dest.get(0)),
                                                     size as c_uint) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `MultDivby4`: D = saturation255(S1/2 * S2/2).
pub fn mult_div_by4(src1: CVec<u8>, src2: CVec<u8>) -> Result<CVec<u8>, String> {
    assert_eq!(src1.len(), src2.len());
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterMultDivby4(mem::transmute(src1.get(0)),
                                                     mem::transmute(src2.get(0)),
                                                     mem::transmute(dest.get(0)),
                                                     size as c_uint) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `BitAnd`: D = S1 & S2.
pub fn bit_and(src1: CVec<u8>, src2: CVec<u8>) -> Result<CVec<u8>, String> {
    assert_eq!(src1.len(), src2.len());
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterBitAnd(mem::transmute(src1.get(0)),
                                                 mem::transmute(src2.get(0)),
                                                 mem::transmute(dest.get(0)),
                                                 size as c_uint) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `BitOr`: D = S1 | S2.
pub fn bit_or(src1: CVec<u8>, src2: CVec<u8>) -> Result<CVec<u8>, String> {
    assert_eq!(src1.len(), src2.len());
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterBitOr(mem::transmute(src1.get(0)),
                                                mem::transmute(src2.get(0)),
                                                mem::transmute(dest.get(0)),
                                                size as c_uint) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using Div: D = S1 / S2.
pub fn div(src1: CVec<u8>, src2: CVec<u8>) -> Result<CVec<u8>, String> {
    assert_eq!(src1.len(), src2.len());
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterDiv(mem::transmute(src1.get(0)),
                                              mem::transmute(src2.get(0)),
                                              mem::transmute(dest.get(0)),
                                              size as c_uint) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `BitNegation`: D = !S.
pub fn bit_negation(src1: CVec<u8>) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterBitNegation(mem::transmute(src1.get(0)),
                                                      mem::transmute(dest.get(0)),
                                                      size as c_uint) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `AddByte`: D = saturation255(S + C).
pub fn add_byte(src1: CVec<u8>, c: u8) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterAddByte(mem::transmute(src1.get(0)),
                                                  mem::transmute(dest.get(0)),
                                                  size as c_uint, c) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `AddUint`: D = saturation255((S[i] + Cs[i % 4]), Cs=Swap32((uint)C).
pub fn add_uint(src1: CVec<u8>, c: u32) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterAddUint(mem::transmute(src1.get(0)),
                                                  mem::transmute(dest.get(0)),
                                                  size as c_uint, c) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `AddByteToHalf`: D = saturation255(S/2 + C).
pub fn add_byte_to_half(src1: CVec<u8>, c: u8) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterAddByteToHalf(mem::transmute(src1.get(0)),
                                                        mem::transmute(dest.get(0)),
                                                        size as c_uint, c) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `SubByte`: D = saturation0(S - C).
pub fn sub_byte(src1: CVec<u8>, c: u8) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterSubByte(mem::transmute(src1.get(0)),
                                                  mem::transmute(dest.get(0)),
                                                  size as c_uint, c) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `SubUint`: D = saturation0(S[i] - Cs[i % 4]), Cs=Swap32((uint)C).
pub fn sub_uint(src1: CVec<u8>, c: u32) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterSubUint(mem::transmute(src1.get(0)),
                                                  mem::transmute(dest.get(0)),
                                                  size as c_uint, c) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `ShiftRight`: D = saturation0(S >> N).
pub fn shift_right(src1: CVec<u8>, n: u8) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterShiftRight(mem::transmute(src1.get(0)),
                                                     mem::transmute(dest.get(0)),
                                                     size as c_uint, n) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `ShiftRightUint`: D = saturation0((uint)S[i] >> N).
pub fn shift_right_uint(src1: CVec<u8>, n: u8) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterShiftRightUint(mem::transmute(src1.get(0)),
                                                         mem::transmute(dest.get(0)),
                                                         size as c_uint, n) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `MultByByte`: D = saturation255(S * C).
pub fn mult_by_byte(src1: CVec<u8>, c: u8) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterMultByByte(mem::transmute(src1.get(0)),
                                                     mem::transmute(dest.get(0)),
                                                     size as c_uint, c) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `ShiftRightAndMultByByte`: D = saturation255((S >> N) * C).
pub fn shift_right_and_mult_by_byte(src1: CVec<u8>, n: u8, c: u8) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterShiftRightAndMultByByte(mem::transmute(src1.get(0)),
                                                                  mem::transmute(dest.get(0)),
                                                                  size as c_uint, n, c) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `ShiftLeftByte`: D = (S << N).
pub fn shift_left_byte(src1: CVec<u8>, n: u8) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterShiftLeftByte(mem::transmute(src1.get(0)),
                                                        mem::transmute(dest.get(0)),
                                                        size as c_uint, n) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `ShiftLeftUint`: D = ((uint)S << N).
pub fn shift_left_uint(src1: CVec<u8>, n: u8) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterShiftLeftUint(mem::transmute(src1.get(0)),
                                                        mem::transmute(dest.get(0)),
                                                        size as c_uint, n) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter `ShiftLeft`: D = saturation255(S << N).
pub fn shift_left(src1: CVec<u8>, n: u8) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterShiftLeft(mem::transmute(src1.get(0)),
                                                    mem::transmute(dest.get(0)),
                                                    size as c_uint, n) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `BinarizeUsingThreshold`: D = (S >= T) ? 255:0.
pub fn binarize_using_threshold(src1: CVec<u8>, t: u8) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterBinarizeUsingThreshold(mem::transmute(src1.get(0)),
                                                                 mem::transmute(dest.get(0)),
                                                                 size as c_uint, t) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `ClipToRange`: D = (S >= Tmin) & (S <= Tmax) S:Tmin | Tmax.
pub fn clip_to_range(src1: CVec<u8>, tmin: u8, tmax: u8) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterClipToRange(mem::transmute(src1.get(0)),
                                                      mem::transmute(dest.get(0)),
                                                      size as c_uint, tmin, tmax) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}

/// Filter using `NormalizeLinear`: D = saturation255((Nmax - Nmin)/(Cmax - Cmin)*(S - Cmin) + Nmin).
pub fn normalize_linear(src1: CVec<u8>, cmin: i32, cmax: i32, nmin: i32, nmax: i32) -> Result<CVec<u8>, String> {
    let size = src1.len();
    let dest = cvec_with_size(size);
    let ret = unsafe { imagefilter::SDL_imageFilterNormalizeLinear(mem::transmute(src1.get(0)),
                                                          mem::transmute(dest.get(0)),
                                                          size as c_uint,
                                                          cmin as c_int, cmax as c_int,
                                                          nmin as c_int, nmax as c_int) };
    if ret == 0 { Ok(dest) }
    else { Err(get_error()) }
}