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
// lib.rs -- Aldaron's Codec Interface / PNG
// Copyright (c) 2017-2018  Jeron A. Lau <jeron.lau@plopgrizzly.com>
// Licensed under the MIT LICENSE

//! Aldaron's Codec Interface / PNG is a library developed by Plop Grizzly to
//! encode(TODO) and decode png and apng(TODO) image files.

#![warn(missing_docs)]
#![doc(html_logo_url = "http://plopgrizzly.com/icon.png",
	html_favicon_url = "http://plopgrizzly.com/icon.png",
	html_root_url = "http://plopgrizzly.com/aci_png/")]

extern crate afi;
extern crate png;

use afi::GraphicBuilder;
pub use afi::{ Graphic, GraphicDecodeErr };

/// Decode PNG data.  On success, returns the png as a `Graphic`.
pub fn decode(png: &[u8]) -> Result<Graphic, GraphicDecodeErr> {
	use png::ColorType::*;

	let decoder = png::Decoder::new(png);
	let (info, mut reader) = decoder.read_info().unwrap();

	let mut buf = vec![0; info.buffer_size()];
	reader.next_frame(&mut buf).unwrap();

	let size = (info.width * info.height) as usize;
	let mut out : Vec<u32> = Vec::with_capacity(size);

	let (color, bit) = reader.output_color_type();

	let graphic = match color {
		RGB => {
			let mut pixel : [u8;4] = [0xFF, 0xFF, 0xFF, 0xFF];

			for i in 0..size {
				pixel[0] = buf[i * 3 + 0];
				pixel[1] = buf[i * 3 + 1];
				pixel[2] = buf[i * 3 + 2];

				out.push(unsafe {::std::mem::transmute(pixel)});
			}

			GraphicBuilder::new().rgba(info.width, info.height, out)
		},
		RGBA => {
			let mut pixel : [u8;4] = [0xFF, 0xFF, 0xFF, 0xFF];

			for i in 0..size {
				pixel[0] = buf[i * 4 + 0];
				pixel[1] = buf[i * 4 + 1];
				pixel[2] = buf[i * 4 + 2];
				pixel[3] = buf[i * 4 + 3];

				out.push(unsafe {::std::mem::transmute(pixel)});
			}

			GraphicBuilder::new().rgba(info.width, info.height, out)
		},
		Grayscale => return Err(GraphicDecodeErr::GrayscaleNYI),
		Indexed => return Err(GraphicDecodeErr::IndexedNYI),
		GrayscaleAlpha => return Err(GraphicDecodeErr::AGrayscaleNYI),
	};

	if bit != png::BitDepth::Eight {
		return Err(GraphicDecodeErr::BitsNYI)
	}

	Ok(graphic)
}