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
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(improper_ctypes)]
#![allow(dead_code)]
#[macro_use]
extern crate num_derive;
extern crate num_traits;

mod bindings;
pub mod error;
mod image;
pub mod ops;
mod utils;

use error::Error;
use std::ffi::*;

pub use image::*;

pub type Result<T> = std::result::Result<T, error::Error>;

pub struct VipsApp;

/// That's the main type of this crate. Use it to initialize the system 
impl VipsApp {
    /// default constructor of a VIpsApp instance which will disable memory leak debugging
    pub fn default(name: &str) -> Result<VipsApp> {
        init(name, false)?;
        Ok(VipsApp)
    }

    /// new instance of VipsApp takes the application name and a flag indicating if the library should debug memory leak (good for testing purposes)
    pub fn new(name: &str, detect_leak: bool) -> Result<VipsApp> {
        init(name, detect_leak)?;
        Ok(VipsApp)
    }

    pub fn progress_set(&self, flag: bool) {
        unsafe {
            bindings::vips_progress_set(if flag { 1 } else { 0 });
        }
    }
    
    pub fn get_disc_threshold(&self) -> u64 {
        unsafe { bindings::vips_get_disc_threshold() }
    }
    
    pub fn version_string(&self) -> Result<&str> {
        unsafe {
            let version = CStr::from_ptr(bindings::vips_version_string());
            version
            .to_str()
            .map_err(|_| Error::InitializationError("Error initializing string"))
        }
    }
    
    pub fn thread_shutdown(&self) {
        unsafe {
            bindings::vips_thread_shutdown();
        }
    }

    pub fn error_buffer(&self) -> Result<&str> {
        unsafe {
            let buffer = CStr::from_ptr(bindings::vips_error_buffer());
            buffer
                .to_str()
                .map_err(|_| Error::InitializationError("Error initializing string"))
        }
    }

    pub fn error(&self, domain: &str, error: &str) -> Result<()> {
        unsafe {
            let c_str_error = utils::new_c_string(error)?;
            let c_str_domain = utils::new_c_string(domain)?;
            bindings::vips_error(c_str_domain.as_ptr(), c_str_error.as_ptr());
            Ok(())
        }
    }

    pub fn error_system(&self, code: i32, domain: &str, error: &str) -> Result<()> {
        unsafe {
            let c_str_error = utils::new_c_string(error)?;
            let c_str_domain = utils::new_c_string(domain)?;
            bindings::vips_error_system(code, c_str_domain.as_ptr(), c_str_error.as_ptr());
            Ok(())
        }
    }

    pub fn freeze_error_buffer(&self) {
        unsafe {
            bindings::vips_error_freeze();
        }
    }

    pub fn error_clear(&self) {
        unsafe {
            bindings::vips_error_clear();
        }
    }

    pub fn error_thaw(&self) {
        unsafe {
            bindings::vips_error_thaw();
        }
    }

    pub fn error_exit(&self, error: &str) -> Result<()> {
        unsafe {
            let c_str_error = utils::new_c_string(error)?;
            bindings::vips_error_exit(c_str_error.as_ptr());
            Ok(())
        }
    }

    pub fn cache_print(&self) {
        unsafe {
            bindings::vips_cache_print();
        }
    }

    pub fn cache_set_max(&self, max: i32) {
        unsafe {
            bindings::vips_cache_set_max(max);
        }
    }

    pub fn cache_set_max_mem(&self, max: u64) {
        unsafe {
            bindings::vips_cache_set_max_mem(max);
        }
    }

    pub fn cache_get_max(&self) -> i32 {
        unsafe {
            bindings::vips_cache_get_max()
        }
    }

    pub fn cache_get_max_mem(&self) -> u64 {
        unsafe {
            bindings::vips_cache_get_max_mem()
        }
    }

    pub fn cache_get_size(&self) -> i32 {
        unsafe {
            bindings::vips_cache_get_size()
        }
    }

    pub fn cache_set_max_files(&self, max: i32) {
        unsafe {
            bindings::vips_cache_set_max_files(max);
        }
    }

    pub fn cache_get_max_files(&self) -> i32 {
        unsafe {
            bindings::vips_cache_get_max_files()
        }
    }

    pub fn vips_cache_set_dump(&self, flag: bool) {
        unsafe {
            bindings::vips_cache_set_dump(if flag { 1 } else { 0 });
        }
    }

    pub fn vips_cache_set_trace(&self, flag: bool) {
        unsafe {
            bindings::vips_cache_set_trace(if flag { 1 } else { 0 });
        }
    }

    /// set the number of worker threads for vips to operate
    pub fn concurrency_set(&self, max: i32) {
        unsafe {
            bindings::vips_concurrency_set(max);
        }
    }

    /// get the number of worker threads that vips is operating
    pub fn concurency_get(&self) -> i32 {
        unsafe {
            bindings::vips_concurrency_get()
        }
    }

    pub fn tracked_get_mem(&self) -> u64 {
        unsafe {
            bindings::vips_tracked_get_mem()
        }
    }

    pub fn tracked_get_mem_highwater(&self) -> u64 {
        unsafe {
            bindings::vips_tracked_get_mem_highwater()
        }
    }

    pub fn tracked_get_allocs(&self) -> i32 {
        unsafe {
            bindings::vips_tracked_get_allocs()
        }
    }
}

impl Drop for VipsApp {
    fn drop(&mut self) {
        unsafe {
            bindings::vips_shutdown();
        }
    }
}

fn init(name: &str, detect_leak: bool) -> Result<i32> {
    let cstring = utils::new_c_string(name);
    if let Ok(c_name) = cstring {
        let res = unsafe { bindings::vips_init(c_name.as_ptr()) };
        let result = if res == 0 {
            Ok(res)
        } else {
            Err(Error::InitializationError("Failed to init libvips"))
        };
        unsafe {
            if detect_leak {
                bindings::vips_leak_set(1);
            };
        }
        result
    } else {
        Err(Error::InitializationError(
            "Failed to convert rust string to C string",
        ))
    }
}