rs_vips/
lib.rs

1#![allow(non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4#![allow(improper_ctypes)]
5#![allow(dead_code)]
6#[macro_use]
7extern crate num_derive;
8extern crate num_traits;
9
10pub mod bindings;
11/// VipsConnection, VipsSource, VipsTarget
12mod connection;
13pub mod error;
14/// VipsImage
15mod image;
16/// VipsInterpolate
17mod interpolate;
18pub mod operator;
19/// Vips Enumerations
20pub mod ops;
21/// VipsBlob
22mod region;
23pub mod utils;
24/// VOption, a list of name-value pairs
25pub mod voption;
26
27pub use connection::*;
28use error::Error;
29pub use image::*;
30pub use interpolate::*;
31pub use region::*;
32use std::ffi::*;
33pub type Result<T> = std::result::Result<T, error::Error>;
34
35pub struct Vips;
36
37/// That's the main type of this crate. Use it to initialize the system
38impl Vips {
39    /// Starts up libvips
40    pub fn init(name: &str) -> Result<()> {
41        let c_name = utils::new_c_string(name)?;
42        let res = unsafe { bindings::vips_init(c_name.as_ptr()) };
43        if res == 0 {
44            Ok(())
45        } else {
46            Err(Error::InitializationError("Failed to init libvips".to_string()))
47        }
48    }
49
50    /// Turn on or off vips leak checking.
51    pub fn leak_set(leak: bool) {
52        unsafe { bindings::vips_leak_set(if leak { 1 } else { 0 }) };
53    }
54
55    /// A structure available to eval callbacks giving information on evaluation progress
56    pub fn progress_set(flag: bool) {
57        unsafe {
58            bindings::vips_progress_set(if flag { 1 } else { 0 });
59        }
60    }
61
62    /// Return the number of bytes at which we flip between open via memory and open via disc
63    pub fn get_disc_threshold() -> u64 {
64        unsafe { bindings::vips_get_disc_threshold() }
65    }
66
67    /// Get the VIPS version as a static string, including a build date and time.
68    pub fn version_string() -> Result<String> {
69        unsafe {
70            let version = CStr::from_ptr(bindings::vips_version_string());
71            let version_str = version
72                .to_str()
73                .map_err(|_| Error::InitializationError("Error initializing string".to_string()))?;
74            Ok(version_str.to_string())
75        }
76    }
77
78    /// Free any thread-private data and flush any profiling information.
79    pub fn thread_shutdown() {
80        unsafe {
81            bindings::vips_thread_shutdown();
82        }
83    }
84
85    /// Get a pointer to the start of the error buffer as string
86    pub fn error_buffer() -> Result<String> {
87        unsafe {
88            let buffer = CStr::from_ptr(bindings::vips_error_buffer());
89            let buffer_str = buffer
90                .to_str()
91                .map_err(|_| Error::InitializationError("Error initializing string".to_string()))?;
92            Ok(buffer_str.to_string())
93        }
94    }
95
96    /// Format the string in the style of printf() and append to the error buffer.
97    pub fn error(domain: &str, error: &str) -> Result<()> {
98        unsafe {
99            let c_str_error = utils::new_c_string(error)?;
100            let c_str_domain = utils::new_c_string(domain)?;
101            bindings::vips_error(
102                c_str_domain.as_ptr(),
103                c_str_error.as_ptr(),
104            );
105            Ok(())
106        }
107    }
108
109    /// Format the string in the style of printf() and append to the error buffer. Then create and append a localised message based on the system error code, usually the value of errno
110    pub fn error_system(code: i32, domain: &str, error: &str) -> Result<()> {
111        unsafe {
112            let c_str_error = utils::new_c_string(error)?;
113            let c_str_domain = utils::new_c_string(domain)?;
114            bindings::vips_error_system(
115                code,
116                c_str_domain.as_ptr(),
117                c_str_error.as_ptr(),
118            );
119            Ok(())
120        }
121    }
122
123    /// Stop errors being logged. Use [func@error_thaw] to unfreeze
124    pub fn freeze_error_buffer() {
125        unsafe {
126            bindings::vips_error_freeze();
127        }
128    }
129
130    /// Clear and reset the error buffer.
131    pub fn error_clear() {
132        unsafe {
133            bindings::vips_error_clear();
134        }
135    }
136
137    /// Re-enable error logging.
138    pub fn error_thaw() {
139        unsafe {
140            bindings::vips_error_thaw();
141        }
142    }
143
144    /// Sends a formatted error message to stderr, then sends the contents of the error buffer, if any, then shuts down vips and terminates the program with an error code.
145    pub fn error_exit(error: &str) -> Result<()> {
146        unsafe {
147            let c_str_error = utils::new_c_string(error)?;
148            bindings::vips_error_exit(c_str_error.as_ptr());
149        }
150    }
151
152    /// Print the whole operation cache to stdout. Handy for debugging.
153    pub fn cache_print() {
154        unsafe {
155            bindings::vips_cache_print();
156        }
157    }
158
159    /// Set the maximum number of operations we keep in cache.
160    pub fn cache_set_max(max: i32) {
161        unsafe {
162            bindings::vips_cache_set_max(max);
163        }
164    }
165
166    /// Set the maximum amount of tracked memory we allow before we start dropping cached operations.
167    pub fn cache_set_max_mem(max: u64) {
168        unsafe {
169            bindings::vips_cache_set_max_mem(max);
170        }
171    }
172
173    /// Set the maximum number of tracked files we allow before we start dropping cached operations.
174    pub fn cache_set_max_files(max: i32) {
175        unsafe {
176            bindings::vips_cache_set_max_files(max);
177        }
178    }
179
180    /// Get the maximum number of operations we keep in cache.
181    pub fn cache_get_max() -> i32 {
182        unsafe { bindings::vips_cache_get_max() }
183    }
184
185    /// Get the maximum amount of tracked memory we allow before we start dropping cached operations.
186    pub fn cache_get_max_mem() -> u64 {
187        unsafe { bindings::vips_cache_get_max_mem() }
188    }
189
190    /// Get the maximum number of tracked files we allow before we start dropping cached operations.
191    pub fn cache_get_max_files() -> i32 {
192        unsafe { bindings::vips_cache_get_max_files() }
193    }
194
195    /// Get the current number of operations in cache.
196    pub fn cache_get_size() -> i32 {
197        unsafe { bindings::vips_cache_get_size() }
198    }
199
200    /// Handy for debugging. Print the operation cache to stdout just before exit.
201    pub fn cache_set_dump(flag: bool) {
202        unsafe {
203            bindings::vips_cache_set_dump(if flag { 1 } else { 0 });
204        }
205    }
206
207    /// Handy for debugging. Print operation cache actions to stdout as we run
208    pub fn cache_set_trace(flag: bool) {
209        unsafe {
210            bindings::vips_cache_set_trace(if flag { 1 } else { 0 });
211        }
212    }
213
214    /// set the number of worker threads for vips to operate
215    pub fn concurrency_set(max: i32) {
216        unsafe {
217            bindings::vips_concurrency_set(max);
218        }
219    }
220
221    /// get the number of worker threads that vips is operating
222    pub fn concurrency_get() -> i32 {
223        unsafe { bindings::vips_concurrency_get() }
224    }
225
226    /// Returns the number of bytes currently allocated via vips_malloc() and friends.
227    pub fn tracked_get_mem() -> u64 {
228        unsafe { bindings::vips_tracked_get_mem() }
229    }
230
231    /// Returns the largest number of bytes simultaneously allocated via vips_tracked_malloc().
232    pub fn tracked_get_mem_highwater() -> u64 {
233        unsafe { bindings::vips_tracked_get_mem_highwater() }
234    }
235
236    /// Returns the number of active allocations.
237    pub fn tracked_get_allocs() -> i32 {
238        unsafe { bindings::vips_tracked_get_allocs() }
239    }
240
241    /// Returns the number of open files.
242    pub fn tracked_get_files() -> i32 {
243        unsafe { bindings::vips_tracked_get_files() }
244    }
245
246    /// If a source does not support mmap or seek and the source is used with a loader that can only work from memory, then the data will be automatically read into memory to EOF before the loader starts.
247    pub fn pipe_read_limit_set(limit: i64) {
248        unsafe {
249            bindings::vips_pipe_read_limit_set(limit);
250        }
251    }
252
253    /// Call this to drop caches, close plugins, terminate background threads, and finalize any internal library testing.
254    /// vips_shutdown() is optional.
255    pub fn shutdown() {
256        unsafe {
257            bindings::vips_shutdown();
258        }
259    }
260}