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
//! PNG image decoding
//!
//! Rust bindings to [libspng](https://libspng.org).
//!
//! # Examples
//!
//! ```
//! # static TEST_PNG: &[u8] = include_bytes!("../tests/test-001.png");
//! let cursor = std::io::Cursor::new(TEST_PNG);
//! let decoder = spng::Decoder::new(cursor);
//! let (out_info, mut reader) = decoder.read_info()?;
//! let output_buffer_size = reader.output_buffer_size();
//! assert_eq!(300, out_info.width);
//! assert_eq!(300, out_info.height);
//! assert_eq!(8, out_info.bit_depth);
//! assert_eq!(4, out_info.color_type.samples());
//! assert_eq!(out_info.buffer_size, output_buffer_size);
//! let mut out = vec![0; output_buffer_size];
//! reader.next_frame(&mut out)?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use std::convert::TryFrom;
use std::{io, mem, slice};

use spng_sys as sys;

mod error;

use error::check_err;
pub use error::Error;

#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Format {
    Rgba8 = sys::spng_format_SPNG_FMT_RGBA8,
    Rgba16 = sys::spng_format_SPNG_FMT_RGBA16,
}

#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum ColorType {
    Grayscale = sys::spng_color_type_SPNG_COLOR_TYPE_GRAYSCALE,
    /// RGB
    Truecolor = sys::spng_color_type_SPNG_COLOR_TYPE_TRUECOLOR,
    Indexed = sys::spng_color_type_SPNG_COLOR_TYPE_INDEXED,
    GrayscaleAlpha = sys::spng_color_type_SPNG_COLOR_TYPE_GRAYSCALE_ALPHA,
    /// RGBA
    TruecolorAlpha = sys::spng_color_type_SPNG_COLOR_TYPE_TRUECOLOR_ALPHA,
}

impl TryFrom<u8> for ColorType {
    type Error = Error;
    fn try_from(c: u8) -> Result<ColorType, Error> {
        use ColorType::*;
        let c = c as i32;
        match c {
            sys::spng_color_type_SPNG_COLOR_TYPE_GRAYSCALE => Ok(Grayscale),
            sys::spng_color_type_SPNG_COLOR_TYPE_TRUECOLOR => Ok(Truecolor),
            sys::spng_color_type_SPNG_COLOR_TYPE_INDEXED => Ok(Indexed),
            sys::spng_color_type_SPNG_COLOR_TYPE_GRAYSCALE_ALPHA => Ok(GrayscaleAlpha),
            sys::spng_color_type_SPNG_COLOR_TYPE_TRUECOLOR_ALPHA => Ok(TruecolorAlpha),
            _ => Err(Error::ColorType),
        }
    }
}

impl ColorType {
    /// Returns the number of samples per pixel
    pub fn samples(self) -> usize {
        use ColorType::*;
        match self {
            Grayscale | Indexed => 1,
            GrayscaleAlpha => 2,
            Truecolor => 3,
            TruecolorAlpha => 4,
        }
    }
}

bitflags::bitflags! {
    /// Decoding flags
    pub struct DecodeFlags: i32 {
        /// Apply transparency
        const TRANSPARENCY = sys::spng_decode_flags_SPNG_DECODE_TRNS;
        /// Apply gamma correction
        const GAMMA = sys::spng_decode_flags_SPNG_DECODE_GAMMA;
        /// Initialize for progressive reads
        const PROGRESSIVE = sys::spng_decode_flags_SPNG_DECODE_PROGRESSIVE;
        #[doc(hidden)]
        const SIGNIFICANT_BIT = sys::spng_decode_flags_SPNG_DECODE_USE_SBIT;
    }
}

/// Decoding limits
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Limits {
    /// Maximum image width
    pub max_width: u32,
    /// Maximum image height
    pub max_height: u32,
}

impl Default for Limits {
    fn default() -> Limits {
        Limits {
            max_width: std::u32::MAX / 2 - 1,
            max_height: std::u32::MAX / 2 - 1,
        }
    }
}

/// PNG decoder
#[derive(Debug)]
pub struct Decoder<R> {
    reader: R,
    limits: Limits,
    decode_flags: DecodeFlags,
    output_format: Option<Format>,
}

/// Decoded output image information
#[derive(Debug)]
pub struct OutputInfo {
    /// The image width in pixels
    pub width: u32,
    /// The image height in pixels
    pub height: u32,
    /// The color channels
    pub color_type: ColorType,
    /// The per component bit depth
    pub bit_depth: u8,
    /// The minimum buffer size required for the decoded pixel output
    pub buffer_size: usize,
}

/// PNG image information
#[derive(Debug)]
pub struct Info {
    /// The image width in pixels
    pub width: u32,
    /// The image height in pixels
    pub height: u32,
    /// The color channels
    pub color_type: ColorType,
    /// The per component bit depth
    pub bit_depth: u8,
}

#[derive(Debug)]
/// PNG reader
pub struct Reader<R> {
    ctx: Context,
    out_format: Format,
    info: Info,
    #[allow(unused)]
    inner: Box<R>,
    decode_flags: DecodeFlags,
    output_buffer_size: usize,
}

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
}

#[derive(Debug)]
struct Context {
    raw: *mut sys::spng_ctx,
}

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

impl Context {
    fn new(flags: i32) -> Result<Context, Error> {
        unsafe {
            let raw = sys::spng_ctx_new(flags);
            if raw.is_null() {
                Err(Error::Mem)
            } else {
                Ok(Context { raw })
            }
        }
    }

    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)
    }

    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)) }
    }

    fn set_png_stream<R>(
        &mut self,
        read_fn: sys::spng_read_fn,
        reader: *mut R,
    ) -> Result<(), Error> {
        unsafe {
            check_err(sys::spng_set_png_stream(
                self.raw,
                read_fn,
                reader as *mut _,
            ))
        }
    }

    fn get_ihdr(&self) -> Result<sys::spng_ihdr, Error> {
        unsafe {
            let mut header = mem::zeroed();
            check_err(sys::spng_get_ihdr(self.raw, &mut header))?;
            Ok(header)
        }
    }

    fn decode_image(
        &mut self,
        output: &mut [u8],
        out_format: Format,
        flags: DecodeFlags,
    ) -> Result<(), Error> {
        unsafe {
            check_err(sys::spng_decode_image(
                self.raw,
                output.as_mut_ptr() as _,
                output.len(),
                out_format as _,
                flags.bits,
            ))
        }
    }
}

impl<R: io::Read> Decoder<R> {
    /// Create a new `png` decoder with the default limits
    pub fn new(r: R) -> Decoder<R> {
        Decoder::with_limits(r, Limits::default())
    }

    /// Create a new `png` decoder with the given limits
    pub fn with_limits(r: R, limits: Limits) -> Decoder<R> {
        let decode_flags = DecodeFlags::empty();
        Decoder {
            reader: r,
            limits,
            decode_flags,
            output_format: None,
        }
    }

    /// Set the limits
    pub fn set_limits(&mut self, limits: Limits) {
        self.limits = limits;
    }

    /// Set the decoding flags
    pub fn set_decode_flags(&mut self, decode_flags: DecodeFlags) {
        self.decode_flags = decode_flags;
    }

    /// Set the output image format
    pub fn set_output_format(&mut self, format: Format) {
        self.output_format = Some(format);
    }

    /// Read the `png` header and initialize decoding.
    pub fn read_info(self) -> Result<(OutputInfo, Reader<R>), Error> {
        let mut inner = Box::new(self.reader);
        let mut ctx = Context::new(self.decode_flags.bits)?;
        ctx.set_image_limits(self.limits.max_width, self.limits.max_height)?;
        ctx.set_png_stream(Some(read_fn::<R>), inner.as_mut() as *mut R as *mut _)?;
        let header = ctx.get_ihdr()?;

        let info = Info {
            bit_depth: header.bit_depth,
            color_type: ColorType::try_from(header.color_type)?,
            width: header.width,
            height: header.height,
        };
        let out_format =
            self.output_format
                .unwrap_or_else(|| match (info.bit_depth, info.color_type) {
                    (16, _) => Format::Rgba16,
                    (_, _) => Format::Rgba8,
                });
        let buffer_size = ctx.decoded_image_size(out_format)?;

        let (out_bit_depth, out_color_type) = match out_format {
            Format::Rgba8 => (8, ColorType::TruecolorAlpha),
            Format::Rgba16 => (16, ColorType::TruecolorAlpha),
        };
        let out_info = OutputInfo {
            width: info.width,
            height: info.height,
            bit_depth: out_bit_depth,
            color_type: out_color_type,
            buffer_size,
        };
        let reader = Reader {
            ctx,
            out_format,
            info,
            decode_flags: self.decode_flags,
            inner,
            output_buffer_size: buffer_size,
        };

        Ok((out_info, reader))
    }
}

impl<R> Reader<R> {
    /// Returns input information
    pub fn info(&self) -> &Info {
        &self.info
    }

    /// Returns the minimum buffer size required for `next_frame`
    pub fn output_buffer_size(&self) -> usize {
        self.output_buffer_size
    }

    /// Decodes the next frame of the `png`. This currently may only be called once.
    pub fn next_frame(&mut self, output: &mut [u8]) -> Result<(), Error> {
        self.ctx
            .decode_image(output, self.out_format, self.decode_flags)
    }
}