summaryrefslogtreecommitdiff
path: root/src/gegl.rs
blob: d5655d272efb14e09ea7ce2a728ef61f22922b22 (plain)
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
use glib::Value;
use glib::translate::*;
use gegl_ffi as ffi;
use std::ptr;
use std::ffi::CString;
use std::ffi::CStr;
use libc;

pub struct GeglContext {
    _private: ()
}

unsafe fn unref<T>(object: *mut T) {
    ffi::g_object_unref(object as *mut libc::c_void);
}

impl GeglContext {
    pub fn new() -> GeglContext {
        unsafe { ffi::gegl_init(&mut 0, ptr::null_mut()) }
        GeglContext { _private: () }
    }

    pub fn new_node(&self) -> GeglNode {
        GeglNode(unsafe { ffi::gegl_node_new() })
    }
}

impl Drop for GeglContext {
    fn drop(&mut self) {
        // unsafe { ffi::gegl_exit(); }
    }
}

pub struct GeglNode(*mut ffi::CGeglNode);

impl GeglNode {
    pub fn create_child(&self, operation: &str) -> GeglNode {
        GeglNode(unsafe {
            let operation0 = CString::new(operation).unwrap();
            ffi::gegl_node_create_child(self.0, operation0.as_ptr())
        })
    }
}

impl Drop for GeglNode {
    fn drop(&mut self) {
        unsafe { unref(self.0); }
    }
}

pub trait GeglOperations {
    fn load(&self, path: &str) -> GeglNode;
}

impl GeglOperations for GeglNode {
    fn load(&self, path: &str) -> GeglNode {
        let result = self.create_child("gegl:load");
        unsafe {
            ffi::gegl_node_set_property(
                result.0,
                CString::new("path").unwrap().as_ptr(),
                Value::from(path).to_glib_none().0
                    as *const libc::c_void);
        }
        result
    }
}

pub fn list_operations() -> Vec<String> {
    let mut num_operations: usize = 0;
    let mut result = vec!();
    unsafe {
        let operations =
            ffi::gegl_list_operations(&mut num_operations as *mut _);
        for i in 0..(num_operations as isize) {
            result.push(CStr::from_ptr(*operations.offset(i))
                        .to_string_lossy().to_string());
        }
        ffi::g_free(operations as *mut libc::c_void);
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use gegl_ffi as ffi;

    #[test]
    fn implements_all_operations() {
        let context = GeglContext::new();
        // assert_eq!(Vec::<String>::new(), list_operations());
    }

    #[test]
    fn can_create_nodes() {
        let context = GeglContext::new();
        context.new_node();
        context.new_node();
    }
}