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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#![allow(clippy::missing_safety_doc)]
use super::*;
use std::ffi::CStr;
use std::fs;
use std::fs::File;
use std::io;
use std::mem;
use std::os::raw::{c_char, c_int, c_void};
use std::path::{Path, PathBuf};
use std::ptr;
use std::slice;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
mod c_api_error;
use self::c_api_error::*;
#[repr(C)]
#[derive(Copy, Clone)]
pub struct GifskiSettings {
pub width: u32,
pub height: u32,
pub quality: u8,
pub fast: bool,
pub repeat: i16,
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct ARGB8 {
pub a: u8,
pub r: u8,
pub g: u8,
pub b: u8,
}
#[repr(C)]
pub struct GifskiHandle {
_opaque: usize,
}
pub struct GifskiHandleInternal {
writer: Mutex<Option<Writer>>,
collector: Mutex<Option<Collector>>,
progress: Mutex<Option<ProgressCallback>>,
write_thread: Mutex<(bool, Option<thread::JoinHandle<GifskiError>>)>,
}
#[no_mangle]
pub unsafe extern "C" fn gifski_new(settings: *const GifskiSettings) -> *const GifskiHandle {
let settings = if let Some(s) = settings.as_ref() {s} else {
return ptr::null_mut();
};
let s = Settings {
width: if settings.width > 0 { Some(settings.width) } else { None },
height: if settings.height > 0 { Some(settings.height) } else { None },
quality: settings.quality,
fast: settings.fast,
repeat: if settings.repeat == -1 { Repeat::Finite(0) } else if settings.repeat == 0 { Repeat::Infinite } else { Repeat::Finite(settings.repeat as u16) },
};
if let Ok((collector, writer)) = new(s) {
Arc::into_raw(Arc::new(GifskiHandleInternal {
writer: Mutex::new(Some(writer)),
write_thread: Mutex::new((false, None)),
collector: Mutex::new(Some(collector)),
progress: Mutex::new(None),
})) as *const GifskiHandle
} else {
ptr::null_mut()
}
}
#[no_mangle]
pub unsafe extern "C" fn gifski_add_frame_png_file(handle: *const GifskiHandle, frame_number: u32, file_path: *const c_char, presentation_timestamp: f64) -> GifskiError {
if file_path.is_null() {
return GifskiError::NULL_ARG;
}
let g = match borrow(handle) {
Some(g) => g,
None => return GifskiError::NULL_ARG,
};
let path = if let Ok(s) = CStr::from_ptr(file_path).to_str() {
PathBuf::from(s)
} else {
return GifskiError::INVALID_INPUT;
};
if let Some(ref mut c) = *g.collector.lock().unwrap() {
c.add_frame_png_file(frame_number as usize, path, presentation_timestamp).into()
} else {
eprintln!("frames can't be added any more, because gifski_end_adding_frames has been called already");
GifskiError::INVALID_STATE
}
}
#[no_mangle]
pub unsafe extern "C" fn gifski_add_frame_rgba(handle: *const GifskiHandle, frame_number: u32, width: u32, height: u32, pixels: *const RGBA8, presentation_timestamp: f64) -> GifskiError {
if pixels.is_null() {
return GifskiError::NULL_ARG;
}
if width < 1 || height < 1 || width > 0xFFFF || height > 0xFFFF {
return GifskiError::INVALID_INPUT;
}
let pixels = slice::from_raw_parts(pixels, width as usize * height as usize);
add_frame_rgba(handle, frame_number, Img::new(pixels.into(), width as usize, height as usize), presentation_timestamp)
}
#[no_mangle]
pub unsafe extern "C" fn gifski_add_frame_rgba_stride(handle: *const GifskiHandle, frame_number: u32, width: u32, height: u32, bytes_per_row: u32, pixels: *const RGBA8, presentation_timestamp: f64) -> GifskiError {
if pixels.is_null() {
return GifskiError::NULL_ARG;
}
let stride = bytes_per_row as usize / mem::size_of_val(&*pixels);
let width = width as usize;
let height = height as usize;
if stride < width || height < 1 {
return GifskiError::INVALID_INPUT;
}
let pixels = slice::from_raw_parts(pixels, stride * height + width - stride);
let img = Img::new_stride(pixels.into(), width, height, stride);
add_frame_rgba(handle, frame_number, img, presentation_timestamp)
}
fn add_frame_rgba(handle: *const GifskiHandle, frame_number: u32, frame: Img<Cow<[RGBA8]>>, presentation_timestamp: f64) -> GifskiError {
let g = match unsafe { borrow(handle) } {
Some(g) => g,
None => return GifskiError::NULL_ARG,
};
if let Some(ref mut c) = *g.collector.lock().unwrap() {
c.add_frame_rgba_cow(frame_number as usize, frame, presentation_timestamp).into()
} else {
eprintln!("frames can't be added any more, because gifski_end_adding_frames has been called already");
GifskiError::INVALID_STATE
}
}
#[no_mangle]
pub unsafe extern "C" fn gifski_add_frame_argb(handle: *const GifskiHandle, frame_number: u32, width: u32, bytes_per_row: u32, height: u32, pixels: *const ARGB8, presentation_timestamp: f64) -> GifskiError {
if pixels.is_null() {
return GifskiError::NULL_ARG;
}
let width = width as usize;
let stride = bytes_per_row as usize / mem::size_of_val(&*pixels);
if stride < width {
return GifskiError::INVALID_INPUT;
}
let pixels = slice::from_raw_parts(pixels, stride * height as usize);
let img = ImgVec::new(pixels.chunks(stride).flat_map(|r| r[0..width].iter().map(|p| RGBA8 {
r: p.r,
g: p.g,
b: p.b,
a: p.a,
})).collect(), width as usize, height as usize);
add_frame_rgba(handle, frame_number, img.into(), presentation_timestamp)
}
#[no_mangle]
pub unsafe extern "C" fn gifski_add_frame_rgb(handle: *const GifskiHandle, frame_number: u32, width: u32, bytes_per_row: u32, height: u32, pixels: *const RGB8, presentation_timestamp: f64) -> GifskiError {
if pixels.is_null() {
return GifskiError::NULL_ARG;
}
let width = width as usize;
let stride = bytes_per_row as usize / mem::size_of_val(&*pixels);
if stride < width {
return GifskiError::INVALID_INPUT;
}
let pixels = slice::from_raw_parts(pixels, stride * height as usize);
let img = ImgVec::new(pixels.chunks(stride).flat_map(|r| r[0..width].iter().map(|&p| p.alpha(255))).collect(), width as usize, height as usize);
add_frame_rgba(handle, frame_number, img.into(), presentation_timestamp)
}
#[no_mangle]
pub unsafe extern "C" fn gifski_set_progress_callback(handle: *const GifskiHandle, cb: unsafe extern fn(*mut c_void) -> c_int, user_data: *mut c_void) -> GifskiError {
let g = match borrow(handle) {
Some(g) => g,
None => return GifskiError::NULL_ARG,
};
let t = g.write_thread.lock().unwrap();
if t.0 {
eprintln!("tried to set progress callback after writing has already started");
return GifskiError::INVALID_STATE;
}
*g.progress.lock().unwrap() = Some(ProgressCallback::new(cb, user_data));
GifskiError::OK
}
#[no_mangle]
pub unsafe extern "C" fn gifski_set_file_output(handle: *const GifskiHandle, destination: *const c_char) -> GifskiError {
let g = match borrow(handle) {
Some(g) => g,
None => return GifskiError::NULL_ARG,
};
let (file, path) = match prepare_for_file_writing(g, destination) {
Ok(res) => res,
Err(err) => return err,
};
gifski_write_thread_start(g, file, Some(path))
}
fn prepare_for_file_writing(g: &GifskiHandleInternal, destination: *const c_char) -> Result<(File, PathBuf), GifskiError> {
if destination.is_null() {
return Err(GifskiError::NULL_ARG);
}
let path = if let Ok(s) = unsafe { CStr::from_ptr(destination).to_str() } {
Path::new(s)
} else {
return Err(GifskiError::INVALID_INPUT);
};
let t = g.write_thread.lock().unwrap();
if t.0 {
eprintln!("tried to start writing for the second time, after it has already started");
return Err(GifskiError::INVALID_STATE);
}
match File::create(path) {
Ok(file) => Ok((file, path.into())),
Err(err) => Err(err.kind().into()),
}
}
struct CallbackWriter {
cb: unsafe extern "C" fn(usize, *const u8, *mut c_void) -> c_int,
user_data: *mut c_void,
}
unsafe impl Send for CallbackWriter {}
impl io::Write for CallbackWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match unsafe { (self.cb)(buf.len(), buf.as_ptr(), self.user_data) } {
0 => Ok(buf.len()),
x => Err(GifskiError::from(x).into()),
}
}
fn flush(&mut self) -> io::Result<()> {
match unsafe { (self.cb)(0, ptr::null(), self.user_data) } {
0 => Ok(()),
x => Err(GifskiError::from(x).into()),
}
}
}
#[no_mangle]
pub unsafe extern "C" fn gifski_set_write_callback(handle: *const GifskiHandle, cb: Option<unsafe extern fn(usize, *const u8, *mut c_void) -> c_int>, user_data: *mut c_void) -> GifskiError {
let g = match borrow(handle) {
Some(g) => g,
None => return GifskiError::NULL_ARG,
};
let cb = match cb {
Some(cb) => cb,
None => return GifskiError::NULL_ARG,
};
let writer = CallbackWriter { cb, user_data };
gifski_write_thread_start(g, writer, None)
}
fn gifski_write_thread_start<W: 'static + Write + Send>(g: &GifskiHandleInternal, file: W, path: Option<PathBuf>) -> GifskiError {
let mut t = g.write_thread.lock().unwrap();
if t.0 {
eprintln!("gifski_set_file_output/gifski_set_write_callback has been called already");
return GifskiError::INVALID_STATE;
}
let writer = g.writer.lock().unwrap().take();
let mut user_progress = g.progress.lock().unwrap().take();
let handle = thread::Builder::new().name("c-write".into()).spawn(move || {
if let Some(writer) = writer {
let mut progress: &mut dyn ProgressReporter = &mut NoProgress {};
if let Some(cb) = &mut user_progress {
progress = &mut *cb;
}
match writer.write(file, progress).into() {
res @ GifskiError::OK |
res @ GifskiError::ALREADY_EXISTS => res,
err => {
if let Some(path) = path {
let _ = fs::remove_file(path);
}
err
},
}
} else {
eprintln!("gifski_set_file_output or gifski_write_* has been called once already");
GifskiError::INVALID_STATE
}
});
match handle {
Ok(handle) => {
*t = (true, Some(handle));
GifskiError::OK
},
Err(_) => GifskiError::THREAD_LOST,
}
}
unsafe fn borrow<'a>(handle: *const GifskiHandle) -> Option<&'a GifskiHandleInternal> {
let g = handle as *const GifskiHandleInternal;
g.as_ref()
}
#[no_mangle]
pub unsafe extern "C" fn gifski_finish(g: *const GifskiHandle) -> GifskiError {
if g.is_null() {
return GifskiError::NULL_ARG;
}
let g = Arc::from_raw(g as *const GifskiHandleInternal);
*g.collector.lock().unwrap() = None;
let thread = g.write_thread.lock().unwrap().1.take();
if let Some(thread) = thread {
thread.join().expect("writer thread failed")
} else {
eprintln!("gifski_finish called before any output has been set");
GifskiError::OK
}
}
#[test]
fn c_cb() {
let g = unsafe {
gifski_new(&GifskiSettings {
width: 1,
height: 1,
quality: 100,
fast: false,
repeat: -1,
})
};
assert!(!g.is_null());
let mut write_called = false;
unsafe extern "C" fn cb(_s: usize, _buf: *const u8, user_data: *mut c_void) -> c_int {
let write_called = user_data as *mut bool;
*write_called = true;
0
}
let mut progress_called = 0u32;
unsafe extern "C" fn pcb(user_data: *mut c_void) -> c_int {
let progress_called = user_data as *mut u32;
*progress_called += 1;
1
}
unsafe {
assert_eq!(GifskiError::OK, gifski_set_progress_callback(g, pcb, (&mut progress_called) as *mut _ as _));
assert_eq!(GifskiError::OK, gifski_set_write_callback(g, Some(cb), (&mut write_called) as *mut _ as _));
assert_eq!(GifskiError::INVALID_STATE, gifski_set_progress_callback(g, pcb, (&mut progress_called) as *mut _ as _));
assert_eq!(GifskiError::OK, gifski_add_frame_rgb(g, 0, 1, 3, 1, &RGB::new(0,0,0), 3.));
assert_eq!(GifskiError::OK, gifski_add_frame_rgb(g, 0, 1, 3, 1, &RGB::new(0,0,0), 10.));
assert_eq!(GifskiError::OK, gifski_finish(g));
}
assert!(write_called);
assert_eq!(2, progress_called);
}
#[test]
fn cant_write_after_finish() {
let g = unsafe { gifski_new(&GifskiSettings {
width: 1, height: 1,
quality: 100,
fast: false,
repeat: -1,
})};
assert!(!g.is_null());
unsafe extern "C" fn cb(_s: usize, _buf: *const u8, _: *mut c_void) -> c_int {
0
}
unsafe {
assert_eq!(GifskiError::OK, gifski_set_write_callback(g, Some(cb), 0 as _));
assert_eq!(GifskiError::OTHER, gifski_finish(g));
}
}
#[test]
fn c_write_failure_propagated() {
let g = unsafe { gifski_new(&GifskiSettings {
width: 1, height: 1,
quality: 100,
fast: false,
repeat: -1,
})};
assert!(!g.is_null());
unsafe extern fn cb(_s: usize, _buf: *const u8, _user: *mut c_void) -> c_int {
GifskiError::WRITE_ZERO as c_int
}
unsafe {
assert_eq!(GifskiError::OK, gifski_set_write_callback(g, Some(cb), ptr::null_mut()));
assert_eq!(GifskiError::OK, gifski_add_frame_rgb(g, 0, 1, 3, 1, &RGB::new(0,0,0), 5.0));
assert_eq!(GifskiError::WRITE_ZERO, gifski_finish(g));
}
}
#[test]
fn cant_write_twice() {
let g = unsafe { gifski_new(&GifskiSettings {
width: 1, height: 1,
quality: 100,
fast: false,
repeat: -1,
})};
assert!(!g.is_null());
unsafe extern "C" fn cb(_s: usize, _buf: *const u8, _user: *mut c_void) -> c_int {
GifskiError::WRITE_ZERO as c_int
}
unsafe {
assert_eq!(GifskiError::OK, gifski_set_write_callback(g, Some(cb), ptr::null_mut()));
assert_eq!(GifskiError::INVALID_STATE, gifski_set_write_callback(g, Some(cb), ptr::null_mut()));
}
}
#[test]
fn c_incomplete() {
let g = unsafe { gifski_new(&GifskiSettings {
width: 0, height: 0,
quality: 100,
fast: true,
repeat: 0,
})};
let rgb: *const RGB8 = ptr::null();
assert_eq!(3, mem::size_of_val(unsafe { &*rgb }));
assert!(!g.is_null());
unsafe {
assert_eq!(GifskiError::NULL_ARG, gifski_add_frame_rgba(g, 0, 1, 1, ptr::null(), 5.0));
}
extern "C" fn cb(_: *mut c_void) -> c_int {
1
}
unsafe {
gifski_set_progress_callback(g, cb, ptr::null_mut());
assert_eq!(GifskiError::OK, gifski_add_frame_rgba(g, 0, 1, 1, &RGBA8::new(0, 0, 0, 0), 5.0));
assert_eq!(GifskiError::OK, gifski_add_frame_rgb(g, 1, 1, 3, 1, &RGB::new(0, 0, 0), 5.0));
assert_eq!(GifskiError::OK, gifski_finish(g));
}
}