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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use libc::{c_char, c_uchar, c_uint, c_void, size_t};
use std::mem;
pub use c_vec::CVec;
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Error(pub c_uint);
impl Error {
pub fn as_str(&self) -> &'static str {
unsafe {
let cstr = ::std::ffi::CStr::from_ptr(lodepng_error_text(self.0) as *const _);
::std::str::from_utf8(cstr.to_bytes()).unwrap()
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub enum ColorType {
LCT_GREY = 0,
LCT_RGB = 2,
LCT_PALETTE = 3,
LCT_GREY_ALPHA = 4,
LCT_RGBA = 6,
}
#[repr(C)]
#[derive(Debug)]
pub struct ColorMode {
pub colortype: ColorType,
pub bitdepth: c_uint,
pub palette: *const ::RGBA<u8>,
pub palettesize: size_t,
key_defined: c_uint,
key_r: c_uint,
key_g: c_uint,
key_b: c_uint,
}
impl ColorType {
pub fn to_color_mode(&self, bitdepth: c_uint) -> super::ColorMode {
unsafe {
super::ColorMode {
data: ColorMode {
colortype: *self,
bitdepth: bitdepth,
..mem::zeroed()
},
}
}
}
}
#[repr(C)]
pub struct DecompressSettings {
pub ignore_adler32: c_uint,
pub custom_zlib: ::std::option::Option<extern "C" fn
(arg1: *mut *mut c_uchar,
arg2: *mut size_t,
arg3: *const c_uchar,
arg4: size_t,
arg5: *const DecompressSettings)
-> c_uint>,
pub custom_inflate: ::std::option::Option<extern "C" fn
(arg1: *mut *mut c_uchar,
arg2: *mut size_t,
arg3: *const c_uchar,
arg4: size_t,
arg5: *const DecompressSettings)
-> c_uint>,
pub custom_context: *const c_void,
}
#[repr(C)]
#[allow(missing_copy_implementations)]
pub struct CompressSettings {
pub btype: c_uint,
pub use_lz77: c_uint,
pub windowsize: c_uint,
pub minmatch: c_uint,
pub nicematch: c_uint,
pub lazymatching: c_uint,
pub custom_zlib: ::std::option::Option<extern "C" fn
(arg1: *mut *mut c_uchar,
arg2: *mut size_t,
arg3: *const c_uchar,
arg4: size_t,
arg5: *const CompressSettings)
-> c_uint>,
pub custom_deflate: ::std::option::Option<extern "C" fn
(arg1: *mut *mut c_uchar,
arg2: *mut size_t,
arg3: *const c_uchar,
arg4: size_t,
arg5: *const CompressSettings)
-> c_uint>,
pub custom_context: *const c_void,
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Time {
pub year: c_uint,
pub month: c_uint,
pub day: c_uint,
pub hour: c_uint,
pub minute: c_uint,
pub second: c_uint,
}
#[repr(C)]
pub struct Info {
pub compression_method: c_uint,
pub filter_method: c_uint,
pub interlace_method: c_uint,
pub color: ColorMode,
pub background_defined: c_uint,
pub background_r: c_uint,
pub background_g: c_uint,
pub background_b: c_uint,
text_num: size_t,
text_keys: *const *const c_char,
text_strings: *const *const c_char,
itext_num: size_t,
itext_keys: *const *const c_char,
itext_langtags: *const *const c_char,
itext_transkeys: *const *const c_char,
itext_strings: *const *const c_char,
pub time_defined: c_uint,
pub time: Time,
pub phys_defined: c_uint,
pub phys_x: c_uint,
pub phys_y: c_uint,
pub phys_unit: c_uint,
pub unknown_chunks_data: [*mut c_uchar; 3],
pub unknown_chunks_size: [size_t; 3],
}
#[repr(C)]
pub struct DecoderSettings {
pub zlibsettings: DecompressSettings,
pub ignore_crc: c_uint,
pub color_convert: c_uint,
pub read_text_chunks: c_uint,
pub remember_unknown_chunks: c_uint,
}
impl DecoderSettings {
pub fn new() -> DecoderSettings {
unsafe {
let mut settings = ::std::mem::zeroed();
lodepng_decoder_settings_init(&mut settings);
settings
}
}
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub enum FilterStrategy {
LFS_ZERO = 0,
LFS_MINSUM,
LFS_ENTROPY,
LFS_BRUTE_FORCE,
LFS_PREDEFINED,
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub enum AutoConvert {
LAC_NO = 0,
LAC_ALPHA,
LAC_AUTO,
LAC_AUTO_NO_NIBBLES,
LAC_AUTO_NO_PALETTE,
LAC_AUTO_NO_NIBBLES_NO_PALETTE,
}
#[repr(C)]
pub struct EncoderSettings {
pub zlibsettings: CompressSettings,
pub auto_convert: AutoConvert,
pub filter_palette_zero: c_uint,
pub filter_strategy: FilterStrategy,
predefined_filters: *const u8,
pub force_palette: c_uint,
add_id: c_uint,
text_compression: c_uint,
}
impl EncoderSettings {
pub fn new() -> EncoderSettings {
unsafe {
let mut settings = ::std::mem::zeroed();
lodepng_encoder_settings_init(&mut settings);
settings
}
}
}
#[repr(C)]
pub struct State {
pub decoder: DecoderSettings,
pub encoder: EncoderSettings,
pub info_raw: ColorMode,
pub info_png: Info,
error: c_uint,
}
#[link(name="lodepng", kind="static")]
extern "C" {
pub fn lodepng_decode_memory(out: &mut *mut u8, w: &mut c_uint, h: &mut c_uint, input: *const u8, insize: size_t, colortype: ColorType, bitdepth: c_uint) -> Error;
pub fn lodepng_encode_memory(out: &mut *mut u8, outsize: &mut size_t, image: *const u8, w: c_uint, h: c_uint, colortype: ColorType, bitdepth: c_uint) -> Error;
fn lodepng_error_text(code: c_uint) -> *const i8;
pub fn lodepng_compress_settings_init(settings: &mut CompressSettings);
pub fn lodepng_color_mode_init(info: &mut ColorMode);
pub fn lodepng_color_mode_cleanup(info: &mut ColorMode);
pub fn lodepng_color_mode_copy(dest: &mut ColorMode, source: &ColorMode) -> Error;
pub fn lodepng_palette_clear(info: &mut ColorMode);
pub fn lodepng_palette_add(info: &mut ColorMode, r: c_uchar, g: c_uchar, b: c_uchar, a: c_uchar) -> Error;
pub fn lodepng_get_bpp(info: &ColorMode) -> c_uint;
pub fn lodepng_get_channels(info: &ColorMode) -> c_uint;
pub fn lodepng_is_greyscale_type(info: &ColorMode) -> c_uint;
pub fn lodepng_is_alpha_type(info: &ColorMode) -> c_uint;
pub fn lodepng_is_palette_type(info: &ColorMode) -> c_uint;
pub fn lodepng_has_palette_alpha(info: &ColorMode) -> c_uint;
pub fn lodepng_can_have_alpha(info: &ColorMode) -> c_uint;
pub fn lodepng_get_raw_size(w: c_uint, h: c_uint, color: &ColorMode) -> size_t;
pub fn lodepng_info_init(info: &mut Info);
pub fn lodepng_info_cleanup(info: &mut Info);
pub fn lodepng_info_copy(dest: &mut Info, source: &Info) -> Error;
pub fn lodepng_clear_text(info: &mut Info);
pub fn lodepng_add_text(info: &mut Info, key: *const c_char, str: *const c_char) -> Error;
pub fn lodepng_clear_itext(info: &mut Info);
pub fn lodepng_add_itext(info: &mut Info, key: *const c_char, langtag: *const c_char, transkey: *const c_char, str: *const c_char) -> Error;
pub fn lodepng_convert(out: *mut u8, input: *const u8, mode_out: &mut ColorMode, mode_in: &ColorMode, w: c_uint, h: c_uint, fix_png: c_uint) -> Error;
pub fn lodepng_decoder_settings_init(settings: &mut DecoderSettings);
pub fn lodepng_auto_choose_color(mode_out: &mut ColorMode, image: *const u8, w: c_uint, h: c_uint, mode_in: &ColorMode, auto_convert: AutoConvert) -> Error;
pub fn lodepng_encoder_settings_init(settings: &mut EncoderSettings);
pub fn lodepng_state_init(state: &mut State);
pub fn lodepng_state_cleanup(state: &mut State);
pub fn lodepng_state_copy(dest: &mut State, source: &State);
pub fn lodepng_decode(out: &mut *mut u8, w: &mut c_uint, h: &mut c_uint, state: &mut State, input: *const u8, insize: size_t) -> Error;
pub fn lodepng_inspect(w: &mut c_uint, h: &mut c_uint, state: &mut State, input: *const u8, insize: size_t) -> Error;
pub fn lodepng_encode(out: &mut *mut u8, outsize: &mut size_t, image: *const u8, w: c_uint, h: c_uint, state: &mut State) -> Error;
pub fn lodepng_chunk_length(chunk: *const c_uchar) -> c_uint;
pub fn lodepng_chunk_type(chtype: &mut [u8; 5], chunk: *const c_uchar);
pub fn lodepng_chunk_type_equals(chunk: *const c_uchar, chtype: *const u8) -> c_uchar;
pub fn lodepng_chunk_ancillary(chunk: *const c_uchar) -> c_uchar;
pub fn lodepng_chunk_private(chunk: *const c_uchar) -> c_uchar;
pub fn lodepng_chunk_safetocopy(chunk: *const c_uchar) -> c_uchar;
pub fn lodepng_chunk_data(chunk: *mut c_uchar) -> *mut c_uchar;
pub fn lodepng_chunk_check_crc(chunk: *const c_uchar) -> c_uint;
pub fn lodepng_chunk_generate_crc(chunk: *mut c_uchar);
pub fn lodepng_chunk_next(chunk: *mut c_uchar) -> *mut c_uchar;
pub fn lodepng_chunk_append(out: &mut *mut u8, outlength: &mut size_t, chunk: *const c_uchar) -> Error;
pub fn lodepng_chunk_create(out: &mut *mut u8, outlength: &mut size_t, length: c_uint, chtype: *const c_char, data: *const u8) -> Error;
pub fn lodepng_crc32(buf: *const u8, len: size_t) -> c_uint;
pub fn lodepng_zlib_compress(out: &mut *mut u8, outsize: &mut size_t, input: *const u8, insize: size_t, settings: &CompressSettings) -> Error;
pub fn lodepng_zlib_decompress(out: &mut *mut u8, outsize: &mut size_t, input: *const u8, insize: size_t, settings: &DecompressSettings) -> Error;
pub fn lodepng_deflate(out: &mut *mut u8, outsize: &mut size_t, input: *const u8, insize: size_t, settings: &CompressSettings) -> Error;
}
impl From<Error> for Result<(), Error> {
fn from(err: Error) -> Self {
err.to_result()
}
}
impl Error {
pub fn to_result(self) -> Result<(), Error> {
match self {
Error(0) => Ok(()),
err => Err(err),
}
}
}
impl CompressSettings {
pub fn new() -> CompressSettings {
unsafe {
let mut settings = mem::zeroed();
lodepng_compress_settings_init(&mut settings);
return settings;
}
}
}