summaryrefslogtreecommitdiff
path: root/image/png/ihdr.ha
blob: bbb8e3efa2035bb3f6fabd44453bad9e1225dd3c (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
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
use endian;
use io;
use memio;

// A PNG IHDR chunk.
export type ihdr = struct {
	width: u32,
	height: u32,
	bitdepth: u8,
	colortype: colortype,
	compression: compression,
	filter_mode: filter_mode,
	interlace: interlace,
};

// Pixel formats supported by PNG files.
export type colortype = enum u8 {
	GRAYSCALE = 0,
	RGB = 2,
	PLTE = 3,
	GRAYALPHA = 4,
	RGBA = 6,
};

// Compression types supported by PNG files.
export type compression = enum u8 {
	DEFLATE = 0,
};

export type filter_mode = enum u8 {
	ADAPTIVE = 0,
};

// Interlace types supported by PNG files.
export type interlace = enum u8 {
	NONE = 0,
	ADAM7 = 1,
};

// Reads an [[ihdr]] from a [[reader]].
export fn ihdr_read(src: *reader) (ihdr | error) = {
	assert(chunk_type(src) == IHDR,
		"Attempted to call ihdr_read on non-IHDR chunk");
	let buf: [13]u8 = [0...];
	match (io::readall(src, buf)?) {
	case io::EOF =>
		return invalid;
	case size =>
		yield;
	};
	// Read checksum
	if (!(io::read(src, &[0u8])? is io::EOF)) {
		return invalid;
	};
	const hdr = ihdr {
		width = endian::begetu32(buf[..4]),
		height = endian::begetu32(buf[4..8]),
		bitdepth = buf[8],
		colortype = buf[9]: colortype,
		compression = buf[10]: compression,
		filter_mode = buf[11]: filter_mode,
		interlace = buf[12]: interlace,
	};
	if ((hdr.width | hdr.height) & 0x80000000 > 0) {
		return invalid;
	};
	switch (hdr.colortype) {
	case colortype::GRAYSCALE =>
		if (hdr.bitdepth != 1 && hdr.bitdepth != 2 && hdr.bitdepth != 4
				&& hdr.bitdepth != 8 && hdr.bitdepth != 16) {
			return invalid;
		};
	case colortype::RGB =>
		if (hdr.bitdepth != 8 && hdr.bitdepth != 16) {
			return invalid;
		};
	case colortype::PLTE =>
		if (hdr.bitdepth != 1 && hdr.bitdepth != 2
				&& hdr.bitdepth != 4 && hdr.bitdepth != 8) {
			return invalid;
		};
	case colortype::GRAYALPHA =>
		if (hdr.bitdepth != 8 && hdr.bitdepth != 16) {
			return invalid;
		};
	case colortype::RGBA =>
		if (hdr.bitdepth != 8 && hdr.bitdepth != 16) {
			return invalid;
		};
	case =>
		return invalid;
	};
	if (hdr.compression != compression::DEFLATE) {
		return unsupported;
	};
	if (hdr.filter_mode != filter_mode::ADAPTIVE) {
		return unsupported;
	};
	if (hdr.interlace > interlace::ADAM7) {
		return unsupported;
	};
	return hdr;
};

@test fn ihdr_reader() void = {
	const src = memio::fixed(simple_png);
	const read = newreader(&src) as reader;
	assert(nextchunk(&read) as u32 == IHDR);
	const ihdr = ihdr_read(&read)!;
	assert(ihdr.width == 4 && ihdr.height == 4);
	assert(ihdr.colortype == colortype::RGB);
	assert(ihdr.filter_mode == filter_mode::ADAPTIVE);
	assert(ihdr.interlace == interlace::NONE);
};