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;
11mod connection;
13pub mod error;
14mod image;
16mod interpolate;
18pub mod operator;
19pub mod ops;
21mod region;
23pub mod utils;
24pub 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
37impl Vips {
39 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 pub fn leak_set(leak: bool) {
52 unsafe { bindings::vips_leak_set(if leak { 1 } else { 0 }) };
53 }
54
55 pub fn progress_set(flag: bool) {
57 unsafe {
58 bindings::vips_progress_set(if flag { 1 } else { 0 });
59 }
60 }
61
62 pub fn get_disc_threshold() -> u64 {
64 unsafe { bindings::vips_get_disc_threshold() }
65 }
66
67 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 pub fn thread_shutdown() {
80 unsafe {
81 bindings::vips_thread_shutdown();
82 }
83 }
84
85 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 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 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 pub fn freeze_error_buffer() {
125 unsafe {
126 bindings::vips_error_freeze();
127 }
128 }
129
130 pub fn error_clear() {
132 unsafe {
133 bindings::vips_error_clear();
134 }
135 }
136
137 pub fn error_thaw() {
139 unsafe {
140 bindings::vips_error_thaw();
141 }
142 }
143
144 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 pub fn cache_print() {
154 unsafe {
155 bindings::vips_cache_print();
156 }
157 }
158
159 pub fn cache_set_max(max: i32) {
161 unsafe {
162 bindings::vips_cache_set_max(max);
163 }
164 }
165
166 pub fn cache_set_max_mem(max: u64) {
168 unsafe {
169 bindings::vips_cache_set_max_mem(max);
170 }
171 }
172
173 pub fn cache_set_max_files(max: i32) {
175 unsafe {
176 bindings::vips_cache_set_max_files(max);
177 }
178 }
179
180 pub fn cache_get_max() -> i32 {
182 unsafe { bindings::vips_cache_get_max() }
183 }
184
185 pub fn cache_get_max_mem() -> u64 {
187 unsafe { bindings::vips_cache_get_max_mem() }
188 }
189
190 pub fn cache_get_max_files() -> i32 {
192 unsafe { bindings::vips_cache_get_max_files() }
193 }
194
195 pub fn cache_get_size() -> i32 {
197 unsafe { bindings::vips_cache_get_size() }
198 }
199
200 pub fn cache_set_dump(flag: bool) {
202 unsafe {
203 bindings::vips_cache_set_dump(if flag { 1 } else { 0 });
204 }
205 }
206
207 pub fn cache_set_trace(flag: bool) {
209 unsafe {
210 bindings::vips_cache_set_trace(if flag { 1 } else { 0 });
211 }
212 }
213
214 pub fn concurrency_set(max: i32) {
216 unsafe {
217 bindings::vips_concurrency_set(max);
218 }
219 }
220
221 pub fn concurrency_get() -> i32 {
223 unsafe { bindings::vips_concurrency_get() }
224 }
225
226 pub fn tracked_get_mem() -> u64 {
228 unsafe { bindings::vips_tracked_get_mem() }
229 }
230
231 pub fn tracked_get_mem_highwater() -> u64 {
233 unsafe { bindings::vips_tracked_get_mem_highwater() }
234 }
235
236 pub fn tracked_get_allocs() -> i32 {
238 unsafe { bindings::vips_tracked_get_allocs() }
239 }
240
241 pub fn tracked_get_files() -> i32 {
243 unsafe { bindings::vips_tracked_get_files() }
244 }
245
246 pub fn pipe_read_limit_set(limit: i64) {
248 unsafe {
249 bindings::vips_pipe_read_limit_set(limit);
250 }
251 }
252
253 pub fn shutdown() {
256 unsafe {
257 bindings::vips_shutdown();
258 }
259 }
260}