-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppm.h
420 lines (362 loc) · 12.9 KB
/
ppm.h
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#ifndef _PPM_H_
#define _PPM_H_
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
#include "pixel.h"
namespace imageRegistration
{
/// Holds the image data read from a ppm file. First coordinate goes 0..h, second goes 0..w!
template <class PixelT>
class ppm
{
//class Optimizer<PixelT>;
//friend class Optimizer<PixelT>;
public:
typedef PixelT pixelType;
ppm(const std::string & filename_) : _fileName(filename_)
{
if (!read(filename_))
throw std::ios_base::failure("Could not open " + filename_ + "\n");
}
ppm(const ppm & image_)
{
_w = image_._w;
_h = image_._h;
_dataInt = image_._dataInt;
_fileName = image_._fileName;
}
ppm(const ppm & image_, int scale_)
{
if (scale_ <= 0)
throw std::ios_base::failure("Sorry, " + std::to_string(scale_) + " is an invalid scale!\n");
_w = image_._w / scale_;
bool lastWPieceBigger = (image_._w % scale_ != 0);
_h = image_._h / scale_;
bool lastHPieceBigger = (image_._h % scale_ != 0);
std::vector < PixelT > dummy(_w);
_dataInt = std::vector < std::vector < PixelT > >(_h, dummy);
typedef std::vector < std::pair < size_t, size_t > > pxGroup;
std::vector < pxGroup > pixelGroupRowDummy(_w);
std::vector < std::vector < pxGroup > > pixelGroups(_h, pixelGroupRowDummy);
PixelT black(0);
for (size_t i = 0; i < _h; ++i)
for (size_t j = 0; j < _w; ++j)
{
//collecting group indices
int groupWSize = scale_ + ((lastWPieceBigger && (j == _w - 1)) ? (image_._w % scale_) : 0);
int groupHSize = scale_ + ((lastHPieceBigger && (i == _h - 1)) ? (image_._h % scale_) : 0);
for (size_t k = 0; k < groupHSize; ++k)
for (size_t l = 0; l < groupWSize; ++l)
{
//_dataInt[i][j].r += image_._dataInt[i*scale_ + k][j*scale_ + l].r;
//_dataInt[i][j].g += image_._dataInt[i*scale_ + k][j*scale_ + l].g;
//_dataInt[i][j].b += image_._dataInt[i*scale_ + k][j*scale_ + l].b;
pixelGroups[i][j].push_back(std::pair<size_t, size_t>(i*scale_ + k, j*scale_ + l));
//_dataInt[i][j] += image_._dataInt[i*scale_ + k][j*scale_ + l];
}
if (pixelGroups[i][j].size() == 0)
throw std::ios_base::failure("Empty pixel group at (" + std::to_string(i) + ", " + std::to_string(j) + ")!\n");
//combining group
_dataInt[i][j] = black;
for (size_t m = 0; m < pixelGroups[i][j].size(); ++m)
{
_dataInt[i][j] += image_._dataInt[pixelGroups[i][j][m].first][pixelGroups[i][j][m].second];
}
//_dataInt[i][j] /= scale_ * scale_;
_dataInt[i][j] /= pixelGroups[i][j].size();
}
}
ppm(size_t width, size_t height)
{
setSize(width, height);
}
//ppm(const std::string & filename_);
//ppm(const ppm & image_);
/// Create a scaled down version of the input ppm to hold.
//ppm(const ppm & image_, size_t scale_);
/// Create an blank image of given widht and height. The image data will be noise.
/// (Probably not faster than copying an existing image. (?)
//ppm(size_t width, size_t height);
ppm() {}
//~ppm();
~ppm() {}
//ppm operator = (const ppm & sourcePic);
/// Read image data from filename_
//void operator= (const std::string & filename_);
void operator= (const std::string & filename_)
{
read(filename_);
}
//void setSize(size_t width, size_t height);
void setSize(size_t width, size_t height)
{
_w = width;
_h = height;
// todo: could be optimized by allocating the memory for _dataInt and setting
// the pointers.
std::vector < PixelT > dummy(_w);
_dataInt = std::vector < std::vector < PixelT > >(_h, dummy);
}
/// Swap in the image data from otherImage_ using vector swap, ie for cheap
//void swap(ppm & otherImage_);
void swap(ppm & otherImage_)
{
_w = otherImage_._w;
_h = otherImage_._h;
_dataInt.swap(otherImage_._dataInt);
}
//size_t getW() const;
//size_t getH() const;
size_t getW() const
{
return _w;
}
size_t getH() const
{
return _h;
}
//static double corr (const ppm & p1_, const ppm & p2_, std::pair <int, int> tl1_, std::pair <int, int> tl2_, std::pair <int, int> wh_);
//static double corr(const ppm & p1_, const ppm & p2_, std::pair <int, int> tl1_, std::pair <int, int> tl2_, std::pair <int, int> wh_)
//{
// const std::vector< std::vector<PixelT> > & d1(p1_._dataInt), &d2(p2_._dataInt);
// PixelT m1(0), v1(0), m2(0), v2(0), cov(0);
// double cor(0);
// int i, j;
// for (i = 0; i < wh_.first - 1; ++i)
// for (j = 0; j < wh_.second - 1; ++j)
// {
// m1 += d1[tl1_.first + i][tl1_.second + j];
// m2 += d2[tl2_.first + i][tl2_.second + j];
// v1 += d1[tl1_.first + i][tl1_.second + j] * d1[tl1_.first + i][tl1_.second + j];
// v2 += d2[tl2_.first + i][tl2_.second + j] * d2[tl2_.first + i][tl2_.second + j];
// cov += d1[tl1_.first + i][tl1_.second + j] * d2[tl2_.first + i][tl2_.second + j];
// }
// m1 /= wh_.first * wh_.second;
// m2 /= wh_.first * wh_.second;
// v1 /= wh_.first * wh_.second;
// v2 /= wh_.first * wh_.second;
// v1 -= m1 * m1;
// v2 -= m2 * m2;
// cov /= wh_.first * wh_.second;
// cov -= m1 * m2;
// PixelT corv(cov.r / pow(v1.r * v2.r, .5), cov.g / pow(v1.g * v2.g, .5), cov.b / pow(v1.b * v2.b, .5));
// //double r(cov.r), g(cov.g), b(cov.b);
// //r /= pow((double) v1.r * v2.r, 1/2);
// //g /= pow((double) v1.g * v2.g, 1/2);
// //b /= pow((double) v1.b * v2.b, 1/2);
// cor = (corv.r + corv.g + corv.b) / 3;
// return cor;
//}
//size_t read(const std::string & filename_);
//size_t write(const std::string & filename_) const;
size_t read(const std::string &filename_)
{
_fileName = filename_;
const int readBufferSize = 1000;
std::ifstream ifs(filename_.c_str(), std::ios_base::binary);
char buffer[readBufferSize];
if (!ifs.good())
return 0;
ifs.getline(buffer, readBufferSize);
if (!ifs.good())
return 0;
if (buffer != _magicword)
return 0;
while (true)
{
ifs.getline(buffer, readBufferSize);
if (buffer[0] != '#')
break;
}
std::stringstream buffstream(buffer);
buffstream >> _w >> _h;
ifs.getline(buffer, readBufferSize);
size_t imagePixs = _w * _h;
size_t imageBytes = imagePixs * 3;
std::vector<PixelT> dummy(_w);
_dataInt = std::vector<std::vector<PixelT> >(_h, dummy);
char *data = new char[imageBytes];
ifs.read(data, imageBytes);
for (size_t i = 0; i<imagePixs; ++i)
{
_dataInt[i / _w][i%_w] = PixelT((int)(unsigned char)data[3 * i], (int)(unsigned char)data[3 * i + 1], (int)(unsigned char)data[3 * i + 2]);
//_dataInt[i / _w][i%_w].r = (int)(unsigned char)data[3 * i];
//_dataInt[i / _w][i%_w].g = (int)(unsigned char)data[3 * i + 1];
//_dataInt[i / _w][i%_w].b = (int)(unsigned char)data[3 * i + 2];
}
ifs.close();
return imageBytes;
}
size_t write(const std::string &filename_) const
{
std::ofstream image(filename_.c_str(), std::ios_base::binary);
if (!image.good())
return 0;
image << _magicword << "\n" << "# proba\n" << _w << " " << _h << "\n" << "255\n";
if (!image.good())
return 0;
for (size_t i = 0; i< _w * _h; ++i)
{
char cr, cg, cb;
_dataInt[i / _w][i%_w].write(cr, cg, cb);
//const char cr = (const char)(_dataInt[i / _w][i%_w].r);
//const char cg = (const char)(_dataInt[i / _w][i%_w].g);
//const char cb = (const char)(_dataInt[i / _w][i%_w].b);
image.write(&cr, 1);
image.write(&cg, 1);
image.write(&cb, 1);
}
return 3 * _w * _h;
}
//PixelT getColor( std::vector< int > point ) const;
//PixelT getColor( int x, int y ) const;
PixelT getColor(std::vector< int > point) const
{
return _dataInt[point[0]][point[1]];
}
PixelT getColor(int x, int y) const
{
return _dataInt[x][y];
}
//void hulyeszin();
//void hulyeszin()
//{
// for (size_t i = 0; i < _w * _h; ++i)
// {
// double j = (_dataInt[i / _w][i%_w].r + _dataInt[i / _w][i%_w].g + _dataInt[i / _w][i%_w].b) / 3;
// _dataInt[i / _w][i%_w].r = j;
// _dataInt[i / _w][i%_w].g = j;
// _dataInt[i / _w][i%_w].b = j;
// }
//}
//void setColor(int x_, int y_, PixelT color_);
//void reWritePoint(std::pair <int, int> point_, PixelT szin_);
void setColor(int x_, int y_, PixelT color_)
{
_dataInt[x_][y_] = color_;
}
void reWritePoint(std::pair <int, int> point_, PixelT color_)
{
_dataInt[point_.first][point_.second] = color_;
}
const std::vector< std::vector<PixelT> > & getData(){return _dataInt;}
const std::string & getName() const {return _fileName;};
static void imageRemapSmooth(const ppm & image_, double x, double y, ppm & target_, int i, int j)
{
size_t h(image_.getH());
size_t w(image_.getW());
PixelT gray(127);
if ((x >= 0) && (x < h - 1) && (y >= 0) && (y < w - 1)) //TODO: not sure if boundaries are correct: h-1=>h, w-1=>w ?
{
int xl((int)x), xh((int)std::ceil(x)), yl((int)y), yh((int)std::ceil(y));
PixelT colorll = image_.getColor(xl, yl);
PixelT colorlh = image_.getColor(xl, yh);
PixelT colorhl = image_.getColor(xh, yl);
PixelT colorhh = image_.getColor(xh, yh);
double wll = (1 - x + xl)*(1 - y + yl);
double wlh = (1 - x + xl)*(1 + y - yh);
double whl = (1 + x - xh)*(1 - y + yl);
double whh = (1 + x - xh)*(1 + y - yh);
colorll *= wll;
colorlh *= wlh;
colorhl *= whl;
colorhh *= whh;
PixelT color = colorll;
color += colorlh;
color += colorhl;
color += colorhh;
target_.setColor(i, j, color);
}
else
{
target_.setColor(i, j, gray);
}
}
static void imageRemap(const ppm & image_, double x, double y, ppm & target_, int i, int j)
{
size_t h(image_.getH());
size_t w(image_.getW());
PixelT gray(127);
if ((x >= 0) && (x < h) && (y >= 0) && (y < w))
{
PixelT color = image_.getColor((int)x, (int)y);
target_.setColor(i, j, color);
}
else
{
target_.setColor(i, j, gray);
}
}
private:
size_t _w, _h; // width, height
std::vector< std::vector<PixelT> > _dataInt; // picture data
static std::string _magicword;
std::string _fileName;
};
/// Holds an array of ppms. The intent is to hold different resolutions of a ppm.
template <class PictureT>
class ppmArray
{
public:
/// Construct with one ppm to be scaled down.
//ppmArray(const PictureT & image_, size_t minSize_);
ppmArray(const PictureT & image_, size_t minSize_)
{
// todo: needs rewrite, suboptimal use of vectors
_minSize = minSize_;
//std::vector < const PictureT* > pics(1, & image_);
size_t dummy = image_.getW() < image_.getH() ? image_.getW() : image_.getH();
size_t numIter = 1;
while ((dummy /= 2) > _minSize)
{
++numIter;
}
_numLevels = numIter;
_pics.resize(numIter);
_pics[0] = &image_;
createScale();
//writeAll(image_.getName(), "scaling");
}
ppmArray() {}
//~ppmArray();
~ppmArray() {}
/// Create all(!) scaled down versions of the original.
//void createScale();
void createScale()
{
for (size_t i = 0; i < _numLevels - 1; ++i)
{
PictureT *pic = new PictureT(*_pics[i], 2);
_pics[i + 1] = pic;
}
}
PictureT getPic(size_t which_) const { return *_pics[which_]; }
size_t getNumLevels() const { return _numLevels; }
void picToFile(size_t which_, const std::string & filename_) const { _pics[which_]->write(filename_); }
const PictureT & getLastPic() const { return *_pics[_numLevels - 1]; }
/// For debugging purposes.
//void writeAll() const;
void writeAll(const std::string &prefix = "", const std::string &postfix = "debugged") const
{
//char buffer[500];
for (int k = 0; k < _pics.size(); ++k)
{
std::stringstream fns;
fns<< "/tmp/" << k << "_" << prefix << "_" << postfix << ".ppm";
//std::string fileName(buffer);
std::string fileName = fns.str().c_str();
picToFile(k, fileName);
}
}
private:
size_t _minSize;
std::vector < const PictureT* > _pics;
size_t _numLevels;
};
} //namespace imageRegistration
#endif //_PPM_H_