-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.cpp
561 lines (463 loc) · 16.7 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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/* -----------------------------------------------------------------
* File: Image.cpp
* Created: 2015-08-29
* -----------------------------------------------------------------
*
* The 6.815/6.865 Image class
*
* ---------------------------------------------------------------*/
#include "Image.h"
using namespace std;
// --------- HANDOUT PS07 ------------------------------
// ------------------------------------------------------
// get the mean of the pixel values
float Image::mean() const {
float sum = 0;
for (int i = 0; i < number_of_elements(); i++) {
sum = sum + (*this)(i);
}
return sum / number_of_elements();
}
// get the variance of the pixel values
float Image::var() const {
float mean = (*this).mean();
float sum = 0;
for (int i = 0; i < number_of_elements(); i++) {
sum = sum + pow((*this)(i) - mean, 2);
}
return sum / number_of_elements();
}
// ---------------- END of PS07 -------------------------------------
// --------- HANDOUT PS04 ------------------------------
// ------------------------------------------------------
// obtain minimum pixel value
float Image::min() const {
float minf = FLT_MAX;
for (int i = 0; i < number_of_elements(); i++) {
minf = std::min(minf, (*this)(i));
}
return minf;
}
// obtain maximum pixel value
float Image::max() const {
float maxf = -FLT_MAX;
for (int i = 0; i < number_of_elements(); i++) {
maxf = std::max(maxf, (*this)(i));
}
return maxf;
}
// ---------------- END of PS04 -------------------------------------
// --------- HANDOUT PS02 ------------------------------
// ------------------------------------------------------
// Safe Accessor that will return a black pixel (clamp = false) or the
// nearest pixel value (clamp = true) when indexing out of the bounds of the image
float Image::smartAccessor(int x, int y, int z, bool clamp) const{
// // --------- HANDOUT PS02 ------------------------------
// return 0.0f; // change this
// --------- SOLUTION PS02 ------------------------------
float black = 0.0;
int x0 = x;
int y0 = y;
if (y >= height() ){
if (clamp) {
y0 = height() - 1;
} else{
return black;
}
}
if (y < 0 ){
if (clamp) {
y0 = 0;
} else{
return black;
}
}
if (x >= width() ){
if (clamp) {
x0 = width() - 1;
} else{
return black;
}
}
if (x < 0 ){
if (clamp) {
x0 = 0;
} else{
return black;
}
}
return (*this)(x0, y0, z);
}
// ---------------- END of PS02 -------------------------------------
// --------- HANDOUT PS01 ------------------------------
// ------------------------------------------------------
long long Image::number_of_elements()const {
// // --------- HANDOUT PS01 ------------------------------
// returns the number of elements in the im- age. An RGB (3 color channels)
// image of 100 × 100 pixels has 30000 elements
// return 0; // change this
// --------- SOLUTION PS01 ------------------------------
return image_data.size();
}
// -------------- Accessors and Setters -------------------------
const float & Image::operator()(int x) const {
// // --------- HANDOUT PS01 ------------------------------
// // Linear accessor to the image data
// throw NotImplementedException(); // change this
// --------- SOLUTION PS01 ------------------------------
if (x < 0 || x>= number_of_elements())
throw OutOfBoundsException();
return image_data[x];
}
const float & Image::operator()(int x, int y) const {
// // --------- HANDOUT PS01 ------------------------------
// // Accessor to the image data at channel 0
// throw NotImplementedException(); // change this
// --------- SOLUTION PS01 ------------------------------
if (x < 0 || x >= width())
throw OutOfBoundsException();
if ( y < 0 || y >= height())
throw OutOfBoundsException();
return image_data[x*stride_[0]+y*stride_[1]];
}
const float & Image::operator()(int x, int y, int z) const {
// // --------- HANDOUT PS01 ------------------------------
// // Accessor to the image data at channel z
// throw NotImplementedException(); // change this
// --------- SOLUTION PS01 ------------------------------
if ( x < 0 || x >= width())
throw OutOfBoundsException();
if ( y < 0 || y >= height())
throw OutOfBoundsException();
if ( z < 0 || z >= channels())
throw OutOfBoundsException();
return image_data[x*stride_[0]+y*stride_[1]+stride_[2]*z];
}
float & Image::operator()(int x) {
// // --------- HANDOUT PS01 ------------------------------
// // Linear setter to the image data
// throw NotImplementedException(); // change this
// --------- SOLUTION PS01 ------------------------------
if (x < 0 || x>= number_of_elements())
throw OutOfBoundsException();
return image_data[x];
}
float & Image::operator()(int x, int y) {
// // --------- HANDOUT PS01 ------------------------------
// // Setter to the image data at channel 0
// throw NotImplementedException(); // change this
// --------- SOLUTION PS01 ------------------------------
if (x < 0 || x >= width())
throw OutOfBoundsException();
if ( y < 0 || y >= height())
throw OutOfBoundsException();
return image_data[x*stride_[0]+y*stride_[1]];
}
float & Image::operator()(int x, int y, int z) {
// // --------- HANDOUT PS01 ------------------------------
// // Setter to the image data at channel z
// throw NotImplementedException(); // change this
// --------- SOLUTION PS01 ------------------------------
if (x < 0 || x >= width())
throw OutOfBoundsException();
if ( y < 0 || y >= height())
throw OutOfBoundsException();
if ( z < 0 || z >= channels())
throw OutOfBoundsException();
return image_data[x*stride_[0]+y*stride_[1]+stride_[2]*z];
}
void Image::set_color(float r, float g, float b) {
// --------- HANDOUT PS01 ------------------------------
// Set the image pixels to the corresponding values
// throw NotImplementedException(); // change this
// --------- SOLUTION PS01 ------------------------------
if (dimensions() < 3) {
// Everything is one channel (set to first channel)
image_data = std::vector<float>(number_of_elements(), r);
} else if (dimensions() >= 3) {
for(int i = 0; i < width() * height(); ++i) {
image_data[i] = r;
if (channels() > 1) // have second channel
image_data[i + stride_[2]] = g;
if (channels() > 2) // have third channel
image_data[i + 2 * stride_[2]] = b;
}
}
}
void Image::create_rectangle(int xstart, int ystart, int xend, int yend,
float r, float g, float b) {
// --------- HANDOUT PS01 ------------------------------
// Set the pixels inside the rectangle to the specified color
// throw NotImplementedException(); // change this
// --------- SOLUTION PS01 ------------------------------
if (xstart < 0 || xstart >= width() || ystart < 0 || ystart >= height())
throw OutOfBoundsException();
if (xend < 0 || xend >= width() || yend < 0 || yend >= height())
throw OutOfBoundsException();
if (dimensions() == 1) {
for(int w = xstart; w <= xend; ++w)
image_data[w] = r;
} else if (dimensions() >= 2) {
int valid_channels = channels() > 3 ? 3 : channels();
float col[3] = {r, g, b};
for (int w = xstart; w <= xend; ++w) {
for (int h = ystart; h <= yend; ++h) {
for (int c = 0; c < valid_channels; ++c) {
(*this)(w,h,c) = col[c];
}
}
}
}
}
void Image::create_line(int xstart, int ystart, int xend, int yend,
float r, float g, float b) {
// --------- HANDOUT PS01 ------------------------------
// Create a line segment with specified color
// throw NotImplementedException(); // change this
// --------- SOLUTION PS01 ------------------------------
if (xstart < 0 || xstart >= width() || ystart < 0 || ystart >= height())
throw OutOfBoundsException();
if (xend < 0 || xend >= width() || yend < 0 || yend >= height())
throw OutOfBoundsException();
if (dimensions() == 1)
throw std::runtime_error("Can't create_line on a 1-d image");
int valid_channels = channels() > 3 ? 3 : channels();
float col[3] = {r, g, b};
// 2-d DDA
float x = xstart;
float y = ystart;
int delta_x = xend - xstart;
int delta_y = yend - ystart;
int delta = std::max(std::abs(delta_x), std::abs(delta_y));
for (int i = 0; i <= delta; i++) {
int ix = int(x), iy = int(y);
if (ix >= 0 && ix < width() && iy >= 0 && iy < height()) {
for(int c = 0; c < valid_channels; ++c) {
(*this)(ix, iy, c) = col[c];
}
}
x += (float(delta_x) / float(delta));
y += (float(delta_y) / float(delta));
}
}
// ---------------- END of PS01 -------------------------------------
/*********************************************************************
* DO NOT EDIT BELOW THIS LINE *
*********************************************************************/
int Image::debugWriteNumber = 0;
Image::Image(int x, int y, int z, const std::string &name_) {
initialize_image_metadata(x,y,z,name_);
long long size_of_data = 1;
for (int k = 0; k < dimensions(); k++) {
size_of_data *= dim_values[k];
}
image_data = std::vector<float>(size_of_data,0);
}
void Image::initialize_image_metadata(int x, int y, int z, const std::string &name_) {
dim_values[0] = 0;
dim_values[1] = 0;
dim_values[2] = 0;
stride_[0] = 0;
stride_[1] = 0;
stride_[2] = 0;
dims = 0;
long long size_of_data = 1;
if ( x < 0 )
throw NegativeDimensionException();
if ( y< 0)
throw NegativeDimensionException();
if (z < 0 )
throw NegativeDimensionException();
image_name = name_;
dims++;
dim_values[0] = x;
size_of_data *= x;
stride_[0] = 1;
if (y > 0 ) {
dims++;
dim_values[1] = y;
size_of_data *= y;
stride_[1] = x;
} else {
return;
}
if (z>0) {
dims++;
dim_values[2] =z;
size_of_data *= z;
stride_[2] = x*y;
} else {
return;
}
}
Image::Image(const std::string & filename) {
std::vector<unsigned char> uint8_image;
unsigned int height_;
unsigned int width_;
unsigned int channels_ = 4;
unsigned int outputchannels_ = 3; // Throw away transparency
unsigned err = lodepng::decode(uint8_image, width_, height_, filename.c_str()); // In column major order with packed color values
if(err == 48) {
throw FileNotFoundException();
}
image_data = std::vector<float>(height_*width_*outputchannels_,0);
for (unsigned int x= 0; x < width_; x++) {
for (unsigned int y = 0; y < height_; y++) {
for (unsigned int c = 0; c < outputchannels_; c++) {
image_data[x+y*width_+c*width_*height_] = uint8_to_float(uint8_image[c + x*channels_ + y*channels_*width_]);
}
}
}
initialize_image_metadata(width_, height_, outputchannels_, filename);
}
Image::~Image() { } // Nothing to clean up
void Image::write(const std::string &filename) const {
if (channels() != 1 && channels() != 3 && channels() != 4)
throw ChannelException();
int png_channels = 4;
std::vector<unsigned char> uint8_image(height()*width()*png_channels, 255);
int c;
for (int x= 0; x < width(); x++) {
for (int y = 0; y < height(); y++) {
for (c = 0; c < channels(); c++) {
uint8_image[c + x*png_channels + y*png_channels*width()] = float_to_uint8(image_data[x+y*width()+c*width()*height()]);
}
for ( ; c < 3; c++) { // Only executes when there is one channel
uint8_image[c + x*png_channels + y*png_channels*width()] = float_to_uint8(image_data[x+y*width()+0*width()*height()]);
}
}
}
lodepng::encode(filename.c_str(), uint8_image, width(), height());
}
void Image::debug_write() const {
std::ostringstream ss;
ss << "./Output/" << debugWriteNumber << ".png";
std::string filename = ss.str();
write(filename);
debugWriteNumber++;
}
float Image::uint8_to_float(const unsigned char &in) {
return ((float) in)/(255.0f);
}
unsigned char Image::float_to_uint8(const float &in) {
float out = in;
if (out < 0)
out = 0;
if (out > 1)
out = 1;
return (unsigned char) (255.0f*out);
}
void compareDimensions(const Image & im1, const Image & im2) {
if(im1.dimensions() != im2.dimensions())
throw MismatchedDimensionsException();
for (int i = 0; i < im1.dimensions(); i++ ) {
if (im1.extent(i) != im2.extent(i))
throw MismatchedDimensionsException();
}
}
Image operator+ (const Image & im1, const Image & im2) {
compareDimensions(im1, im2);
long long total_pixels = im1.number_of_elements();
Image output(im1.extent(0), im1.extent(1), im1.extent(2));
for (int i = 0 ; i < total_pixels; i++) {
output(i) = im1(i) + im2(i);
}
return output;
}
Image operator- (const Image & im1, const Image & im2) {
compareDimensions(im1, im2);
long long total_pixels = im1.number_of_elements();
Image output(im1.extent(0), im1.extent(1), im1.extent(2));
for (int i = 0 ; i < total_pixels; i++) {
output(i) = im1(i) - im2(i);
}
return output;
}
Image operator* (const Image & im1, const Image & im2) {
compareDimensions(im1, im2);
long long total_pixels = im1.number_of_elements();
Image output(im1.extent(0), im1.extent(1), im1.extent(2));
for (int i = 0 ; i < total_pixels; i++) {
output(i) = im1(i) * im2(i);
}
return output;
}
Image operator/ (const Image & im1, const Image & im2) {
compareDimensions(im1, im2);
long long total_pixels = im1.number_of_elements();
Image output(im1.extent(0), im1.extent(1), im1.extent(2));
for (int i = 0 ; i < total_pixels; i++) {
if (im2(i) == 0)
throw DivideByZeroException();
output(i) = im1(i) / im2(i);
}
return output;
}
Image operator+ (const Image & im1, const float & c) {
long long total_pixels = im1.number_of_elements();
Image output(im1.extent(0), im1.extent(1), im1.extent(2));
for (int i = 0 ; i < total_pixels; i++) {
output(i) = im1(i) + c;
}
return output;
}
Image operator- (const Image & im1, const float & c) {
long long total_pixels = im1.number_of_elements();
Image output(im1.extent(0), im1.extent(1), im1.extent(2));
for (int i = 0 ; i < total_pixels; i++) {
output(i) = im1(i) - c;
}
return output;
}
Image operator* (const Image & im1, const float & c) {
long long total_pixels = im1.number_of_elements();
Image output(im1.extent(0), im1.extent(1), im1.extent(2));
for (int i = 0 ; i < total_pixels; i++) {
output(i) = im1(i) * c;
}
return output;
}
Image operator/ (const Image & im1, const float & c) {
long long total_pixels = im1.number_of_elements();
Image output(im1.extent(0), im1.extent(1), im1.extent(2));
if (c==0)
throw DivideByZeroException();
for (int i = 0 ; i < total_pixels; i++) {
output(i) = im1(i)/c;
}
return output;
}
Image operator+(const float & c, const Image & im1) {
long long total_pixels = im1.number_of_elements();
Image output(im1.extent(0), im1.extent(1), im1.extent(2));
for (int i = 0 ; i < total_pixels; i++) {
output(i) = im1(i) + c;
}
return output;
}
Image operator- (const float & c, const Image & im1) {
long long total_pixels = im1.number_of_elements();
Image output(im1.extent(0), im1.extent(1), im1.extent(2));
for (int i = 0 ; i < total_pixels; i++) {
output(i) = c - im1(i);
}
return output;
}
Image operator* (const float & c, const Image & im1) {
long long total_pixels = im1.number_of_elements();
Image output(im1.extent(0), im1.extent(1), im1.extent(2));
for (int i = 0 ; i < total_pixels; i++) {
output(i) = im1(i) * c;
}
return output;
}
Image operator/ (const float & c, const Image & im1) {
long long total_pixels = im1.number_of_elements();
Image output(im1.extent(0), im1.extent(1), im1.extent(2));
for (int i = 0 ; i < total_pixels; i++) {
if (im1(i) == 0)
throw DivideByZeroException();
output(i) = c/im1(i);
}
return output;
}