-
Notifications
You must be signed in to change notification settings - Fork 5
/
Image.cpp
364 lines (290 loc) · 6.72 KB
/
Image.cpp
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
361
362
363
364
#include "Image.h"
#include "Color.h"
#include "Mathtools.h"
#include <fstream>
#include <iostream>
Image::Image(int width, int height) : width_(width), height_(height)
{
int pixels = width_*height_;
pixel_ = new Color[pixels];
}
Image::Image(int width, int height, Color* buffer) : width_(width), height_(height)
{
int pixels = width_*height_;
pixel_ = new Color[pixels];
for (int p = 0; p < pixels; p++)
{
pixel_[p].setColor(buffer[p]);
}
}
Image::Image(const Image& src) : width_(src.width_), height_(src.height_)
{
int pixels = width_*height_;
pixel_ = new Color[pixels];
for (int p = 0; p < pixels; p++)
{
pixel_[p].setColor(src.pixel_[p]);
}
}
Image::~Image()
{
// delete pixels
delete[] pixel_;
}
void Image::eat_comment(std::ifstream& f)
{
// we assume a comment in PNM header is not longer than 1024 characters
char buffer[1024];
char p;
// newline Unix: "\n"
// newline Windows: "\r\n"
while (p = f.peek(), p == '\n' || p == '\r')
{
f.get();
}
if (p == '#')
{
f.getline(buffer, 1023);
}
}
void Image::initialize(int width, int height)
{
if (pixel_)
delete[] pixel_;
width_ = width;
height_ = height;
int pixels = width * height;
pixel_ = new Color[pixels];
}
void Image::copy(Image& src)
{
width_ = src.width_;
height_ = src.height_;
int pixels = width_*height_;
pixel_ = new Color[pixels];
for (int p = 0; p < pixels; p++)
{
pixel_[p].setColor(src.pixel_[p]);
}
}
Color Image::getPixel(const int row, const int col)
{
if (row < 0 || row >= height_ || col < 0 || col >= width_)
return Color();
return pixel_[Mathtools::pixelIndex(width_, row, col)];
}
int Image::getWidth()
{
return width_;
}
int Image::getHeight()
{
return height_;
}
void Image::save(const std::string& filename)
{
std::ofstream of;
std::string name = filename + ".ppm";
of.open(name.c_str(), std::ios::binary);
if (!of.is_open())
{
std::cout << "Error: Couldn't create file " << filename << ".ppm\n";
return;
}
/*
PPM binary format:
P6
<width string> <height string>
<max color value string>
<RGB binary color code>
*/
of << "P6\n" << width_ << " " << height_ << "\n255\n";
int pixels = width_ * height_;
for (int p = 0; p < pixels; p++)
{
// write 8Bit color value instead of float value
of << pixel_[p].getRed8B() << pixel_[p].getGreen8B() << pixel_[p].getBlue8B();
}
of.close();
}
// load PNM file
// inspired source: http://josiahmanson.com/prose/optimize_ppm/
void Image::load(const std::string& filename)
{
std::ifstream ifs(filename.c_str(), std::ios::binary);
// check if file exists
if (!ifs.is_open())
{
std::cout << "Error: Unable to open file \"" << filename << "\"" << std::endl;
return;
}
// we got file
std::cout << "Reading file header of \"" << filename << "\"" << std::endl;
eat_comment(ifs);
// read and evaluate magic number
std::string magic_number;
ifs >> magic_number;
ANYMAP map = PPM;
bool ascii = false;
if (magic_number.compare("P1") == 0)
{
// P1 = PBM in ASCII
map = PBM;
ascii = true;
}
else if (magic_number.compare("P2") == 0)
{
// P2 = PGM in ASCII
map = PGM;
ascii = true;
}
else if (magic_number.compare("P3") == 0)
{
// P3 = PPM in ASCII
map = PPM;
ascii = true;
}
else if (magic_number.compare("P4") == 0)
{
// P4 = PBM in binary
map = PBM;
ascii = false;
}
else if (magic_number.compare("P5") == 0)
{
// P5 = PGM in binary
map = PGM;
ascii = false;
}
else if (magic_number.compare("P6") == 0)
{
// P6 = PPM in binary
map = PPM;
ascii = false;
}
else
{
std::cout << "Error, Magic number doesn't fit in anymap format!" << std::endl;
ifs.close();
return;
}
// read width and height
int width = 1, height = 1;
eat_comment(ifs);
ifs >> width;
eat_comment(ifs);
ifs >> height;
// let's hope size is not smaller than 1
if (width < 1 || height < 1)
{
std::cout << "Error: Unsupported size " << width << "/" << height << std::endl;
ifs.close();
return;
}
// read color depth if not PBM
int color_depth = 1;
if (magic_number.compare("P1") != 0 && magic_number.compare("P4") != 0)
{
eat_comment(ifs);
ifs >> color_depth;
if (color_depth < 1 || color_depth > 255)
{
std::cout << "Error: Unsupported color depth " << color_depth << std::endl;
ifs.close();
return;
}
}
// stringstream is currently at a ' ' or '\n', move it by 1
ifs.get();
std::cout << "Loading content file \"" << filename << "\"" << std::endl;
initialize(width, height);
if (ascii)
getColorsFromAscii(ifs, map, color_depth);
else
getColorsFromBinary(ifs, map, color_depth);
ifs.close();
}
void Image::getColorsFromAscii(std::ifstream& ss, ANYMAP mode, unsigned int color_depth)
{
if (mode == PBM)
{
int color = 0;
int index = 0;
while (!ss.eof())
{
ss >> color;
pixel_[index++].setColor(color == 0 ? 0xffffff : 0x000000);
}
}
else if (mode == PGM)
{
int color = 0;
int index = 0;
while (!ss.eof())
{
ss >> color;
float red = static_cast<float>(color) / static_cast<float>(color_depth);
float green = static_cast<float>(color) / static_cast<float>(color_depth);
float blue = static_cast<float>(color) / static_cast<float>(color_depth);
pixel_[index++].setColor(red, green, blue);
}
}
else if (mode == PPM)
{
int r = 0, g = 0, b = 0;
int index = 0;
while (!ss.eof())
{
ss >> r >> g >> b;
float red = static_cast<float>(r) / static_cast<float>(color_depth);
float green = static_cast<float>(g) / static_cast<float>(color_depth);
float blue = static_cast<float>(b) / static_cast<float>(color_depth);
pixel_[index++].setColor(red, green, blue);
}
}
}
void Image::getColorsFromBinary(std::ifstream& ss, ANYMAP mode, unsigned int color_depth)
{
if (mode == PBM)
{
int index = 0;
int row = 0;
while (!ss.eof())
{
int color = ss.get();
int b = 0;
while (b < 8 && index < Mathtools::pixelIndex(width_, row, width_-1))
{
pixel_[index++].setColor((color >> (7-b)) & 1 ? 0x000000 : 0xffffff);
b++;
}
row++;
}
}
else if (mode == PGM)
{
int index = 0;
while (!ss.eof())
{
int color = ss.get();
float red = static_cast<float>(color) / static_cast<float>(color_depth);
float green = static_cast<float>(color) / static_cast<float>(color_depth);
float blue = static_cast<float>(color) / static_cast<float>(color_depth);
pixel_[index++].setColor(red, green, blue);
}
}
else if (mode == PPM)
{
int index = 0;
while (!ss.eof())
{
int color = ss.get();
float red = static_cast<float>(color) / static_cast<float>(color_depth);
color = ss.get();
float green = static_cast<float>(color) / static_cast<float>(color_depth);
color = ss.get();
float blue = static_cast<float>(color) / static_cast<float>(color_depth);
pixel_[index++].setColor(red, green, blue);
}
}
}