summaryrefslogtreecommitdiff
path: root/image/png/load.ha
blob: d3578f85fe7383ff2df2c32b078948d33e76fe70 (plain)
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
use errors;
use io;
use memio;

// A decoded PNG image.
export type image = struct {
	// Image header
	ihdr: ihdr,
	// RGBA palette data (if present, otherwise an empty slice)
	palette: []u32,
	// Raw pixel data
	pixels: []u8,
};

// Reads a PNG image from an [[io::handle]], returning the decoded header,
// palette (if present), and raw pixel data. The caller must pass the return
// value to [[image_finish]] to discard resources associated with the image.
export fn load(in: io::handle) (image | error) = {
	let success = false;

	const reader = newreader(in)?;

	match (nextchunk(&reader)?) {
	case io::EOF =>
		return invalid;
	case let ctype: u32 =>
		if (ctype != IHDR) {
			return invalid;
		};
	};
	const ihdr = ihdr_read(&reader)?;
	let pixels: ([]u8 | void) = void;
	defer if (!success && pixels is []u8) free(pixels as []u8);
	let palette: []u32 = [];
	defer if (!success) free(palette);

	for (true) match (nextchunk(&reader)?) {
	case io::EOF =>
		return invalid;
	case let ctype: u32 =>
		switch (ctype) {
		case IHDR =>
			return invalid;
		case PLTE =>
			if (len(palette) != 0) {
				return invalid;
			};
			let plte = new_plte_reader(&reader)?;
			palette = alloc([0...], plte_ncolors(&plte));
			plte_read(&plte, palette)?;
		case IDAT =>
			if (pixels is []u8) {
				return invalid;
			};
			let pixbuf: []u8 = alloc([0...], decoder_bufsiz(&ihdr));
			defer free(pixbuf);
			let idat = new_idat_reader(&reader);
			let dec = newdecoder(&idat, &ihdr, pixbuf)?;
			defer io::close(&dec)!;
			pixels = io::drain(&dec)?;
		case IEND =>
			iend_read(&reader)?;
			break;
		case =>
			if (is_critical(ctype)) {
				return unsupported;
			};
			io::copy(io::empty, &reader)?;
		};
	};

	if (pixels is void) {
		return invalid;
	};

	success = true;
	return image {
		ihdr = ihdr,
		palette = palette,
		pixels = pixels as []u8,
	};
};

// Frees resources associated with an [[image]].
export fn image_finish(img: *image) void = {
	free(img.palette);
	free(img.pixels);
};