1#![allow(non_upper_case_globals)]
4#![allow(non_camel_case_types)]
5#![allow(non_snake_case)]
6#![allow(improper_ctypes)]
7#![allow(dead_code)]
8#[macro_use]
9extern crate num_derive;
10extern crate num_traits;
11
12pub mod bindings;
13mod connection;
15pub mod error;
16mod image;
18mod interpolate;
20pub mod operator;
21pub mod ops;
23mod region;
25pub mod utils;
26pub mod voption;
28
29pub use connection::*;
30use error::Error;
31pub use image::*;
32pub use interpolate::*;
33pub use region::*;
34use std::ffi::*;
35pub type Result<T> = std::result::Result<T, error::Error>;
36
37pub struct Vips;
38
39impl Vips {
41 pub fn init(name: &str) -> Result<()> {
43 let c_name = utils::new_c_string(name)?;
44 let res = unsafe { bindings::vips_init(c_name.as_ptr()) };
45 if res == 0 {
46 Ok(())
47 } else {
48 Err(Error::InitializationError("Failed to init libvips".to_string()))
49 }
50 }
51
52 pub fn leak_set(leak: bool) {
54 unsafe { bindings::vips_leak_set(if leak { 1 } else { 0 }) };
55 }
56
57 pub fn progress_set(flag: bool) {
59 unsafe {
60 bindings::vips_progress_set(if flag { 1 } else { 0 });
61 }
62 }
63
64 pub fn get_disc_threshold() -> u64 {
66 unsafe { bindings::vips_get_disc_threshold() }
67 }
68
69 pub fn version_string() -> Result<String> {
71 unsafe {
72 let version = CStr::from_ptr(bindings::vips_version_string());
73 let version_str = version
74 .to_str()
75 .map_err(|_| Error::InitializationError("Error initializing string".to_string()))?;
76 Ok(version_str.to_string())
77 }
78 }
79
80 pub fn thread_shutdown() {
82 unsafe {
83 bindings::vips_thread_shutdown();
84 }
85 }
86
87 pub fn error_buffer() -> Result<String> {
89 unsafe {
90 let buffer = CStr::from_ptr(bindings::vips_error_buffer());
91 let buffer_str = buffer
92 .to_str()
93 .map_err(|_| Error::InitializationError("Error initializing string".to_string()))?;
94 Ok(buffer_str.to_string())
95 }
96 }
97
98 pub fn error(domain: &str, error: &str) -> Result<()> {
100 unsafe {
101 let c_str_error = utils::new_c_string(error)?;
102 let c_str_domain = utils::new_c_string(domain)?;
103 bindings::vips_error(
104 c_str_domain.as_ptr(),
105 c_str_error.as_ptr(),
106 );
107 Ok(())
108 }
109 }
110
111 pub fn error_system(code: i32, domain: &str, error: &str) -> Result<()> {
113 unsafe {
114 let c_str_error = utils::new_c_string(error)?;
115 let c_str_domain = utils::new_c_string(domain)?;
116 bindings::vips_error_system(
117 code,
118 c_str_domain.as_ptr(),
119 c_str_error.as_ptr(),
120 );
121 Ok(())
122 }
123 }
124
125 pub fn freeze_error_buffer() {
127 unsafe {
128 bindings::vips_error_freeze();
129 }
130 }
131
132 pub fn error_clear() {
134 unsafe {
135 bindings::vips_error_clear();
136 }
137 }
138
139 pub fn error_thaw() {
141 unsafe {
142 bindings::vips_error_thaw();
143 }
144 }
145
146 pub fn error_exit(error: &str) -> Result<()> {
148 unsafe {
149 let c_str_error = utils::new_c_string(error)?;
150 bindings::vips_error_exit(c_str_error.as_ptr());
151 }
152 }
153
154 pub fn cache_print() {
156 unsafe {
157 bindings::vips_cache_print();
158 }
159 }
160
161 pub fn cache_set_max(max: i32) {
163 unsafe {
164 bindings::vips_cache_set_max(max);
165 }
166 }
167
168 pub fn cache_set_max_mem(max: u64) {
170 unsafe {
171 bindings::vips_cache_set_max_mem(max);
172 }
173 }
174
175 pub fn cache_set_max_files(max: i32) {
177 unsafe {
178 bindings::vips_cache_set_max_files(max);
179 }
180 }
181
182 pub fn cache_get_max() -> i32 {
184 unsafe { bindings::vips_cache_get_max() }
185 }
186
187 pub fn cache_get_max_mem() -> u64 {
189 unsafe { bindings::vips_cache_get_max_mem() }
190 }
191
192 pub fn cache_get_max_files() -> i32 {
194 unsafe { bindings::vips_cache_get_max_files() }
195 }
196
197 pub fn cache_get_size() -> i32 {
199 unsafe { bindings::vips_cache_get_size() }
200 }
201
202 pub fn cache_set_dump(flag: bool) {
204 unsafe {
205 bindings::vips_cache_set_dump(if flag { 1 } else { 0 });
206 }
207 }
208
209 pub fn cache_set_trace(flag: bool) {
211 unsafe {
212 bindings::vips_cache_set_trace(if flag { 1 } else { 0 });
213 }
214 }
215
216 pub fn concurrency_set(max: i32) {
218 unsafe {
219 bindings::vips_concurrency_set(max);
220 }
221 }
222
223 pub fn concurrency_get() -> i32 {
225 unsafe { bindings::vips_concurrency_get() }
226 }
227
228 pub fn tracked_get_mem() -> u64 {
230 unsafe { bindings::vips_tracked_get_mem() }
231 }
232
233 pub fn tracked_get_mem_highwater() -> u64 {
235 unsafe { bindings::vips_tracked_get_mem_highwater() }
236 }
237
238 pub fn tracked_get_allocs() -> i32 {
240 unsafe { bindings::vips_tracked_get_allocs() }
241 }
242
243 pub fn tracked_get_files() -> i32 {
245 unsafe { bindings::vips_tracked_get_files() }
246 }
247
248 pub fn pipe_read_limit_set(limit: i64) {
250 unsafe {
251 bindings::vips_pipe_read_limit_set(limit);
252 }
253 }
254
255 pub fn shutdown() {
258 unsafe {
259 bindings::vips_shutdown();
260 }
261 }
262}