File: PPMLoad.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (256 lines) | stat: -rw-r--r-- 4,662 bytes parent folder | download
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
#include "stdafx.h"
#include "PPMLoad.h"
#include "Image.h"
#include "Exception.h"

namespace graphics {

	bool isPPM(IStream *file) {
		const char *spec[] = {
			"P",
			"123456",
			" \n\r\t",
		};

		const Nat len = ARRAY_COUNT(spec);
		Buffer buffer = file->peek(storm::buffer(file->engine(), len));
		if (!buffer.full())
			return false;

		for (Nat i = 0; i < len; i++) {
			bool ok = false;
			for (const char *at = spec[i]; *at; at++) {
				ok |= byte(*at) == buffer[i];
			}

			if (!ok)
				return false;
		}

		return true;
	}

	// Is this a whitespace character?
	static bool whitespace(Byte ch) {
		switch (ch) {
		case ' ':
		case '\n':
		case '\r':
		case '\t':
			return true;
		default:
			return false;
		}
	}

	// Is this a numeric character?
	static bool numeric(Byte ch) {
		return ch >= '0' && ch <= '9';
	}

	struct State {
		IStream *src;
		Buffer buffer;
		Nat pos;

		State(IStream *src) :
			src(src),
			buffer(src->read(1024)),
			pos(0) {}

		// Get the next byte in the file.
		Byte next() {
			if (pos >= buffer.filled()) {
				pos = 0;
				buffer.filled(0);
				buffer = src->read(buffer);

				if (buffer.empty())
					throw new (src) ImageLoadError(S("Unexpected end of stream!"));
			}

			return buffer[pos++];
		}

		// Get the next character, ignoring comments.
		Byte nextText() {
			Byte r;
			for (r = next(); r == '#'; r = next()) {
				// In a comment. Skip until newline.
				while (next() != '\n')
					;
			}
			return r;
		}

		// Get the next number (in ASCII) from the file.
		Nat nextNum() {
			// Skip whitespace.
			Byte ch;
			while (whitespace(ch = nextText()))
				;

			// Read the number.
			if (!numeric(ch))
				throw new (src) ImageLoadError(S("Not a number!"));

			Nat result = 0;
			while (numeric(ch)) {
				result *= 10;
				result += ch - '0';
				ch = nextText();
			}

			return result;
		}
	};

	struct Header {
		// Mode: We use 1, 2 and 3 regardless if binary or ascii.
		Byte mode;

		// Size.
		Nat width, height;
	};

	template <class T>
	Image *loadMonoPPM(State &src, const Header &header, T read) {
		Image *out = new (src.src) Image(header.width, header.height);

		for (Nat y = 0; y < header.height; y++) {
			for (Nat x = 0; x < header.width; x++) {
				Float c = read.next(src);
				out->set(x, y, Color(c, c, c));
			}
			read.flush();
		}

		return out;
	}

	template <class T>
	Image *loadColorPPM(State &src, const Header &header, T read) {
		Image *out = new (src.src) Image(header.width, header.height);

		for (Nat y = 0; y < header.height; y++) {
			for (Nat x = 0; x < header.width; x++) {
				Float r = read.next(src);
				Float g = read.next(src);
				Float b = read.next(src);
				out->set(x, y, Color(r, g, b));
			}
			read.flush();
		}

		return out;
	}

	template <class T>
	Image *loadPPM(State &src, const Header &header) {
		Nat maxval = 0;

		switch (header.mode) {
		case 1:
			return loadMonoPPM(src, header, typename T::Mono());
		case 2:
			maxval = src.nextNum();
			return loadMonoPPM(src, header, typename T::Multi(maxval));
		case 3:
			maxval = src.nextNum();
			return loadColorPPM(src, header, typename T::Multi(maxval));
		default:
			return null;
		}
	}

	struct Raw {
		struct Multi {
			Float maxval;
			Bool multibyte;

			Multi(Nat maxval) : maxval(Float(maxval)), multibyte(maxval > 255) {}

			Float next(State &src) const {
				Nat result = src.next();
				if (multibyte)
					result = (result << 8) | src.next();
				return Float(result) / maxval;
			}

			void flush() const {}
		};

		struct Mono {
			Byte data;
			Byte fill;

			Mono() : data(0), fill(0) {}

			Float next(State &src) {
				if (fill == 0) {
					data = src.next();
					fill = 8;
				}

				fill--;
				return Float(~(data >> fill) & 0x1);
			}

			void flush() {
				fill = 0;
			}
		};
	};

	struct Ascii {
		struct Multi {
			Float maxval;

			Multi(Nat maxval) : maxval(Float(maxval)) {}

			Float next(State &src) const {
				return Float(src.nextNum()) / maxval;
			}

			void flush() const {}
		};

		struct Mono {
			Float next(State &src) const {
				return Float(src.nextNum());
			}

			void flush() const {}
		};
	};

	Image *loadPPM(IStream *src, const wchar *&error) {
		State s(src);

		error = S("Not a supported PPM file.");
		if (s.next() != 'P')
			return null;

		Header header;

		header.mode = s.next();
		if (header.mode >= '1' && header.mode <= '6')
			header.mode -= '0';
		else
			return null;

		if (!whitespace(s.next()))
			return null;

		header.width = s.nextNum();
		header.height = s.nextNum();

		if (header.mode > 3) {
			header.mode -= 3;
			return loadPPM<Raw>(s, header);
		} else {
			return loadPPM<Ascii>(s, header);
		}
	}

}