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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//! Raw decoding context

use crate::{
    error::{check_err, Error},
    ContextFlags, CrcAction, DecodeFlags, Format,
};

use self::chunk::*;

use spng_sys as sys;
use std::{io, marker::PhantomData, mem, mem::MaybeUninit, slice};

unsafe extern "C" fn read_fn<R: io::Read>(
    _: *mut sys::spng_ctx,
    user: *mut libc::c_void,
    dest: *mut libc::c_void,
    len: usize,
) -> libc::c_int {
    let reader: &mut R = &mut *(user as *mut R as *mut _);
    let dest = slice::from_raw_parts_mut(dest as *mut u8, len);
    let mut offset = 0;
    while offset < len {
        let buf = &mut dest[offset..];
        let ret = reader.read(buf);
        match ret {
            Ok(0) => return sys::spng_errno_SPNG_IO_EOF,
            Ok(n) => offset += n,
            Err(_) => return sys::spng_errno_SPNG_IO_ERROR,
        }
    }
    sys::spng_errno_SPNG_OK
}

/// Helper trait for converting optional ancillary chunks into `Option<T>`.
///
/// <http://www.libpng.org/pub/png/spec/1.1/PNG-Chunks.html#C.Ancillary-chunks>
///
/// ## Ancilary chunks
///
/// * BKGD
/// * CHRM
/// * GAMA
/// * HIST
/// * ICCP
/// * PHYS
/// * SBIT
/// * SPLT
/// * SRGB
/// * TEXT
/// * TIME
/// * TRNS
/// * ZTXT
pub trait ChunkAvail<T> {
    /// Converts `Err(Error::Chunkavail)` into `Ok(None)`.
    fn chunk_avail(self) -> Result<Option<T>, Error>;
}

impl<T> ChunkAvail<T> for Result<T, Error> {
    fn chunk_avail(self) -> Result<Option<T>, Error> {
        match self {
            Ok(value) => Ok(Some(value)),
            Err(Error::Chunkavail) => Ok(None),
            Err(error) => Err(error),
        }
    }
}

/// The raw decoding context.
///
/// * <https://libspng.org/>
/// * <http://www.libpng.org/pub/png/spec/1.1/PNG-Contents.html>
#[derive(Debug)]
pub struct RawContext<R> {
    raw: *mut sys::spng_ctx,
    reader: Option<Box<R>>,
}

impl<R> Drop for RawContext<R> {
    fn drop(&mut self) {
        if !self.raw.is_null() {
            unsafe {
                sys::spng_ctx_free(self.raw);
            }
        }
    }
}

impl<R> RawContext<R> {
    pub fn new() -> Result<RawContext<R>, Error> {
        RawContext::with_flags(ContextFlags::empty())
    }

    pub fn with_flags(flags: ContextFlags) -> Result<RawContext<R>, Error> {
        unsafe {
            let raw = sys::spng_ctx_new(flags.bits() as _);
            if raw.is_null() {
                Err(Error::Mem)
            } else {
                Ok(RawContext { raw, reader: None })
            }
        }
    }

    /// Set how chunk CRC errors should be handled for critical and ancillary chunks.
    pub fn set_crc_action(
        &mut self,
        critical: CrcAction,
        ancillary: CrcAction,
    ) -> Result<(), Error> {
        unsafe {
            check_err(sys::spng_set_crc_action(
                self.raw,
                critical as i32,
                ancillary as i32,
            ))
        }
    }

    /// Get image width and height limits.
    ///
    /// Returns `(width, height)`
    pub fn get_image_limits(&self) -> Result<(u32, u32), Error> {
        let mut width = 0;
        let mut height = 0;
        unsafe {
            check_err(sys::spng_get_image_limits(
                self.raw,
                &mut width,
                &mut height,
            ))?;
            Ok((width, height))
        }
    }

    /// Set image width and height limits, these may not be larger than `(2^31)-1`.
    pub fn set_image_limits(&mut self, max_width: u32, max_height: u32) -> Result<(), Error> {
        unsafe { check_err(sys::spng_set_image_limits(self.raw, max_width, max_height)) }
    }

    /// Get chunk size and chunk cache limits.
    ///
    /// Returns `(chunk_size, cache_size)`
    pub fn get_chunk_limits(&self) -> Result<(usize, usize), Error> {
        let mut chunk_size = 0;
        let mut cache_size = 0;
        unsafe {
            check_err(sys::spng_get_chunk_limits(
                self.raw,
                &mut chunk_size,
                &mut cache_size,
            ))?;
            Ok((chunk_size, cache_size))
        }
    }

    /// Set chunk size and chunk cache limits, the default chunk size limit is `(2^31)-1`, the default
    /// chunk cache limit is `SIZE_MAX`.
    pub fn set_chunk_limits(&mut self, chunk_size: usize, cache_size: usize) -> Result<(), Error> {
        unsafe { check_err(sys::spng_set_chunk_limits(self.raw, chunk_size, cache_size)) }
    }

    /// Get the image header.
    pub fn get_ihdr(&self) -> Result<Ihdr, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_ihdr(self.raw, chunk.as_mut_ptr()))?;
            Ok(chunk.assume_init())
        }
    }

    /// Get the image palette.
    pub fn get_plte(&self) -> Result<Ref<Plte>, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_plte(self.raw, chunk.as_mut_ptr()))?;
            Ok(Ref::from(Plte(chunk.assume_init())))
        }
    }

    /// Get the image transparency.
    pub fn get_trns(&self) -> Result<Trns, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_trns(self.raw, chunk.as_mut_ptr()))?;
            Ok(chunk.assume_init())
        }
    }

    /// Get primary chromacities and white point as floating point numbers.
    pub fn get_chrm(&self) -> Result<Chrm, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_chrm(self.raw, chunk.as_mut_ptr()))?;
            Ok(chunk.assume_init())
        }
    }

    /// Get primary chromacities and white point in the PNG's internal representation.
    pub fn get_chrm_int(&self) -> Result<ChrmInt, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_chrm_int(self.raw, chunk.as_mut_ptr()))?;
            Ok(chunk.assume_init())
        }
    }

    /// Get the image gamma.
    pub fn get_gama(&self) -> Result<f64, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_gama(self.raw, chunk.as_mut_ptr()))?;
            Ok(chunk.assume_init())
        }
    }

    /// Get the ICC profile.
    ///
    /// ### Note
    /// ICC profiles are not validated.
    pub fn get_iccp(&self) -> Result<Ref<Iccp>, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_iccp(self.raw, chunk.as_mut_ptr()))?;
            let chunk: Iccp = mem::transmute(chunk.assume_init());
            Ok(Ref::from(chunk))
        }
    }

    /// Get the significant bits.
    pub fn get_sbit(&self) -> Result<Sbit, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_sbit(self.raw, chunk.as_mut_ptr()))?;
            Ok(chunk.assume_init())
        }
    }

    /// Get the `sRGB` rendering intent.
    pub fn get_srgb(&self) -> Result<u8, Error> {
        unsafe {
            let mut rendering_intent = 0;
            check_err(sys::spng_get_srgb(self.raw, &mut rendering_intent))?;
            Ok(rendering_intent)
        }
    }

    /// Get text information.
    ///
    /// ### Note
    /// Due to the structure of PNG files it is recommended to call this function after [`decode_image`].
    ///
    /// [`decode_image`]: method@RawContext::decode_image
    pub fn get_text(&self) -> Result<Ref<Vec<Text>>, Error> {
        unsafe {
            use std::ptr;
            let mut len = 0;
            check_err(sys::spng_get_text(self.raw, ptr::null_mut(), &mut len))?;
            let mut vec = Vec::<Text>::new();
            vec.reserve_exact(len as usize);
            vec.set_len(len as usize);
            let text_ptr = vec.as_mut_ptr() as *mut sys::spng_text;
            check_err(sys::spng_get_text(self.raw, text_ptr, &mut len))?;
            Ok(Ref::from(vec))
        }
    }

    /// Get the image background color.
    pub fn get_bkgd(&self) -> Result<Bkgd, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_bkgd(self.raw, chunk.as_mut_ptr()))?;
            Ok(chunk.assume_init())
        }
    }

    /// Get the image histogram.
    pub fn get_hist(&self) -> Result<Hist, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_hist(self.raw, chunk.as_mut_ptr()))?;
            Ok(chunk.assume_init())
        }
    }

    /// Get physical pixel dimensions.
    pub fn get_phys(&self) -> Result<Phys, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_phys(self.raw, chunk.as_mut_ptr()))?;
            Ok(chunk.assume_init())
        }
    }

    /// Get the suggested palettes.
    pub fn get_splt(&self) -> Result<Ref<Vec<Splt>>, Error> {
        unsafe {
            use std::ptr;
            let mut len = 0;
            check_err(sys::spng_get_splt(self.raw, ptr::null_mut(), &mut len))?;
            let mut vec = Vec::<Splt>::new();
            vec.reserve_exact(len as usize);
            vec.set_len(len as usize);
            let splt_ptr = vec.as_mut_ptr() as *mut sys::spng_splt;
            check_err(sys::spng_get_splt(self.raw, splt_ptr, &mut len))?;
            Ok(Ref::from(vec))
        }
    }

    /// Get the modification time.
    ///
    /// ### Note
    /// Due to the structure of PNG files it is recommended to call this function after [`decode_image`].
    ///
    /// [`decode_image`]: method@RawContext::decode_image
    pub fn get_time(&self) -> Result<Time, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_time(self.raw, chunk.as_mut_ptr()))?;
            Ok(chunk.assume_init())
        }
    }

    /// Get the image offset.
    pub fn get_offs(&self) -> Result<Offs, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_offs(self.raw, chunk.as_mut_ptr()))?;
            Ok(chunk.assume_init())
        }
    }

    /// Get the `EXIF` data.
    ///
    /// ### Note
    /// Due to the structure of PNG files it is recommended to call this function after [`decode_image`].
    ///
    /// [`decode_image`]: method@RawContext::decode_image
    pub fn get_exif(&self) -> Result<Ref<Exif>, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_exif(self.raw, chunk.as_mut_ptr()))?;
            let chunk: Exif = mem::transmute(chunk.assume_init());
            Ok(Ref::from(chunk))
        }
    }

    /// Get the current, to-be-decoded row's information.
    pub fn get_row_info(&self) -> Result<RowInfo, Error> {
        unsafe {
            let mut chunk = MaybeUninit::uninit();
            check_err(sys::spng_get_row_info(self.raw, chunk.as_mut_ptr()))?;
            Ok(chunk.assume_init())
        }
    }

    /// Returns unknown chunk information.
    ///
    /// ### Note
    /// Due to the structure of PNG files it is recommended to call this function after [`decode_image`].
    ///
    /// [`decode_image`]: method@RawContext::decode_image
    pub fn get_unknown_chunks(&self) -> Result<Ref<Vec<UnknownChunk>>, Error> {
        unsafe {
            use std::ptr;
            let mut len = 0;
            check_err(sys::spng_get_unknown_chunks(
                self.raw,
                ptr::null_mut(),
                &mut len,
            ))?;
            let mut vec = Vec::<UnknownChunk>::new();
            vec.reserve_exact(len as usize);
            vec.set_len(len as usize);
            let chunk_ptr = vec.as_mut_ptr() as *mut sys::spng_unknown_chunk;
            check_err(sys::spng_get_unknown_chunks(self.raw, chunk_ptr, &mut len))?;
            Ok(Ref::from(vec))
        }
    }

    /// Calculates decoded image buffer size for the given output format.
    ///
    /// PNG data must have been set prior with [`set_png_stream`] or [`set_png_buffer`].
    ///
    /// [`set_png_stream`]: method@RawContext::set_png_stream
    /// [`set_png_buffer`]: method@RawContext::set_png_buffer
    pub fn decoded_image_size(&self, out_format: Format) -> Result<usize, Error> {
        let mut len = 0;
        unsafe {
            check_err(sys::spng_decoded_image_size(
                self.raw,
                out_format as _,
                &mut len,
            ))?;
        }
        Ok(len)
    }

    /// Decodes the PNG file and writes the image to `out`. The image is converted from any PNG format to the
    /// destination format `out_format`. Interlaced images are deinterlaced and `16-bit` images are converted to
    /// host-endian.
    ///
    /// The `out` buffer must have a length greater or equal to the size returned by [`decoded_image_size`] with
    /// the same `out_format`.
    ///
    /// If the `SPNG_DECODE_PROGRESSIVE` flag is set, the context will be initialied with `out_format` for
    /// progressive decoding. The image is not immediately decoded and the `out` buffer is ignored.
    ///
    /// The `SPNG_DECODE_TRNS` flag is ignored if the PNG has an alpha channel or does not contain a `TRNS`
    /// chunk. It is also ignored for gray `1/2/4`-bit images.
    ///
    /// The function may only be called **once** per context.
    ///
    /// [`decode_image`]: method@RawContext::decode_image
    /// [`decoded_image_size`]: method@RawContext::decoded_image_size
    pub fn decode_image(
        &mut self,
        out: &mut [u8],
        out_format: Format,
        flags: DecodeFlags,
    ) -> Result<(), Error> {
        unsafe {
            check_err(sys::spng_decode_image(
                self.raw,
                out.as_mut_ptr() as _,
                out.len(),
                out_format as _,
                flags.bits as _,
            ))
        }
    }

    /// Decodes and deinterlaces a scanline to `out`.
    ///
    /// This function requires the decoder to be initialized by calling [`decode_image`] with the
    /// `SPNG_DECODE_PROGRESSIVE` flag set.
    ///
    /// The widest scanline is the decoded image size divided by `ihdr.height`.
    ///
    /// For the last scanline and subsequent calls the return value is `SPNG_EOI`.
    ///
    /// If the image is not interlaced this function's behavior is identical to [`decode_scanline`].
    ///
    /// [`decode_image`]: method@RawContext::decode_image
    /// [`decode_scanline`]: method@RawContext::decode_scanline
    pub fn decode_row(&mut self, out: &mut [u8]) -> Result<(), Error> {
        unsafe {
            check_err(sys::spng_decode_row(
                self.raw,
                out.as_mut_ptr() as _,
                out.len(),
            ))
        }
    }

    /// Decodes a scanline to `out`.
    ///
    /// This function requires the decoder to be initialized by calling [`decode_image`] with the
    /// `SPNG_DECODE_PROGRESSIVE` flag set.
    ///
    /// The widest scanline is the decoded image size divided by `ihdr.height`.
    ///
    /// For the last scanline and subsequent calls the return value is `SPNG_EOI`.
    ///
    /// [`decode_image`]: method@RawContext::decode_image
    pub fn decode_scanline(&mut self, output: &mut [u8]) -> Result<(), Error> {
        unsafe {
            check_err(sys::spng_decode_scanline(
                self.raw,
                output.as_mut_ptr() as _,
                output.len(),
            ))
        }
    }
}

impl<R: io::Read> RawContext<R> {
    /// Set the input `png` stream reader. The input buffer or stream may only be set once per context.
    pub fn set_png_stream(&mut self, reader: R) -> Result<(), Error> {
        let mut boxed = Box::new(reader);
        let user = boxed.as_mut() as *mut R as *mut _;
        self.reader = Some(boxed);
        let read_fn: sys::spng_read_fn = Some(read_fn::<R>);
        unsafe { check_err(sys::spng_set_png_stream(self.raw, read_fn, user)) }
    }
}

impl<'a> RawContext<&'a [u8]> {
    /// Set the input `png` buffer. The input buffer or stream may only be set once per context.
    pub fn set_png_buffer(&mut self, buf: &'a [u8]) -> Result<(), Error> {
        unsafe {
            check_err(sys::spng_set_png_buffer(
                self.raw,
                buf.as_ptr() as *const _,
                buf.len(),
            ))
        }
    }
}

/// Attaches lifetime `'a` to `T`.
pub struct Ref<'a, T: 'a> {
    data: T,
    _p: PhantomData<&'a ()>,
}

impl<'a, T: 'a> std::ops::Deref for Ref<'a, T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl<'a, T: 'a> From<T> for Ref<'a, T> {
    fn from(t: T) -> Ref<'a, T> {
        Ref {
            data: t,
            _p: PhantomData,
        }
    }
}

/// `PNG` chunk data
pub mod chunk {
    use spng_sys as sys;
    use std::{ffi::CStr, slice};

    /// Safe wrapper for [`spng_sys::spng_splt`]
    #[repr(C)]
    pub struct Splt(pub(crate) sys::spng_splt);

    impl Splt {
        pub fn name(&self) -> Result<&str, std::str::Utf8Error> {
            unsafe { CStr::from_ptr(self.0.name.as_ptr() as _).to_str() }
        }

        pub fn sample_depth(&self) -> u8 {
            self.0.sample_depth
        }

        pub fn entries(&self) -> &[sys::spng_splt_entry] {
            unsafe { slice::from_raw_parts(self.0.entries, self.0.n_entries as usize) }
        }
    }

    /// Safe wrapper for [`spng_sys::spng_plte`]
    #[repr(C)]
    pub struct Plte(pub(crate) sys::spng_plte);

    impl Plte {
        pub fn entries(&self) -> &[PlteEntry] {
            unsafe { slice::from_raw_parts(self.0.entries.as_ptr(), self.0.n_entries as usize) }
        }
    }

    /// Safe wrapper for [`spng_sys::spng_exif`]
    #[repr(C)]
    pub struct Exif(pub(crate) sys::spng_exif);

    impl Exif {
        pub fn data(&self) -> &[u8] {
            unsafe { slice::from_raw_parts(self.0.data as _, self.0.length as usize) }
        }
    }

    /// Safe wrapper for [`spng_sys::spng_text`]
    #[repr(C)]
    pub struct Text(pub(crate) sys::spng_text);

    impl Text {
        pub fn keyword(&self) -> Result<&str, std::str::Utf8Error> {
            unsafe { CStr::from_ptr(self.0.keyword.as_ptr() as _).to_str() }
        }

        pub fn type_(&self) -> i32 {
            self.0.type_
        }

        pub fn length(&self) -> usize {
            self.0.length
        }

        pub fn text(&self) -> Result<&str, std::str::Utf8Error> {
            unsafe { CStr::from_ptr(self.0.text).to_str() }
        }

        pub fn compression_flag(&self) -> u8 {
            self.0.compression_flag
        }

        pub fn compression_method(&self) -> u8 {
            self.0.compression_method
        }

        pub fn language_tag(&self) -> Result<&str, std::str::Utf8Error> {
            unsafe { CStr::from_ptr(self.0.language_tag).to_str() }
        }

        pub fn translated_keyword(&self) -> Result<&str, std::str::Utf8Error> {
            unsafe { CStr::from_ptr(self.0.translated_keyword).to_str() }
        }
    }

    /// Safe wrapper for [`spng_sys::spng_iccp`]
    #[repr(C)]
    pub struct Iccp(pub(crate) sys::spng_iccp);

    impl Iccp {
        pub fn profile_name(&self) -> Result<&str, std::str::Utf8Error> {
            unsafe { CStr::from_ptr(self.0.profile_name.as_ptr()).to_str() }
        }

        pub fn profile(&self) -> &[u8] {
            unsafe { slice::from_raw_parts(self.0.profile as _, self.0.profile_len as usize) }
        }
    }

    /// Safe wrapper for [`spng_sys::spng_unknown_chunk`]
    #[repr(C)]
    pub struct UnknownChunk(pub(crate) spng_sys::spng_unknown_chunk);

    impl UnknownChunk {
        /// Returns the chunk type.
        pub fn type_(&self) -> Result<&str, std::str::Utf8Error> {
            std::str::from_utf8(&self.0.type_)
        }

        /// Returns the chunk data.
        pub fn data(&self) -> &[u8] {
            unsafe { slice::from_raw_parts(self.0.data as _, self.0.length as usize) }
        }
    }

    /// Image header
    pub type Ihdr = sys::spng_ihdr;
    /// Transparency
    pub type Trns = sys::spng_trns;
    /// Primary chromacities and white point as floating point numbers
    pub type Chrm = sys::spng_chrm;
    /// Primary chromacities and white point in the PNG's internal representation
    pub type ChrmInt = sys::spng_chrm_int;
    /// Significant bits
    pub type Sbit = sys::spng_sbit;
    /// Background color
    pub type Bkgd = sys::spng_bkgd;
    /// Histogram
    pub type Hist = sys::spng_hist;
    /// Physical pixel dimensions
    pub type Phys = sys::spng_phys;
    /// Modification time
    pub type Time = sys::spng_time;
    /// Offset
    pub type Offs = sys::spng_offs;
    /// To-be-decoded row information
    pub type RowInfo = sys::spng_row_info;
    /// Palette entry
    pub type PlteEntry = spng_sys::spng_plte_entry;
}