-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
554 lines (489 loc) · 14.2 KB
/
main.cpp
File metadata and controls
554 lines (489 loc) · 14.2 KB
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
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
// Class to represent an image as a 3D matrix
class Image
{
private:
int width, height, maxVal, channels;
vector<vector<vector<int>>> data; // [height][width][channel]
public:
// Default constructor
Image()
{
width = 0;
height = 0;
maxVal = 255;
channels = 3;
}
// Create blank image
Image(int w, int h, int ch = 3)
{
width = w;
height = h;
maxVal = 255;
channels = ch;
data.resize(height, vector<vector<int>>(width, vector<int>(channels, 0)));
}
// Get image dimensions
int getWidth() const { return width; }
int getHeight() const { return height; }
int getChannels() const { return channels; }
// Set number of channels
void setChannels(int ch)
{
channels = ch;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
data[y][x].resize(channels, 0);
}
}
}
// Pixel access
int &operator()(int y, int x, int channel)
{
return data[y][x][channel];
}
const int &operator()(int y, int x, int channel) const
{
return data[y][x][channel];
}
// Load PPM image (P3 format)
bool loadPPM(const string &filename)
{
ifstream file(filename);
if (!file.is_open())
{
cerr << "Error: Could not open file " << filename << endl;
return false;
}
string format;
file >> format;
if (format != "P3")
{
cerr << "Error: Only P3 PPM format is supported" << endl;
return false;
}
file >> width >> height >> maxVal;
channels = 3;
data.resize(height, vector<vector<int>>(width, vector<int>(channels, 0)));
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
for (int c = 0; c < channels; c++)
{
file >> data[y][x][c];
}
}
}
file.close();
return true;
}
// Save PPM image (P3 format)
bool savePPM(const string &filename) const
{
ofstream file(filename);
if (!file.is_open())
{
cerr << "Error: Could not create file " << filename << endl;
return false;
}
file << "P3\n"
<< width << " " << height << "\n"
<< maxVal << "\n";
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
if (channels == 1)
{
// For grayscale images, write the same value for all three channels
int gray = data[y][x][0];
file << gray << " " << gray << " " << gray << " ";
}
else
{
// For color images, write all three channels
for (int c = 0; c < 3; c++)
{
file << data[y][x][c] << " ";
}
}
}
file << "\n";
}
file.close();
return true;
}
// Print image data to console (for small images)
void print() const
{
cout << "Image " << width << "x" << height << " (" << channels << " channels):\n";
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
cout << "(";
for (int c = 0; c < channels; c++)
{
cout << data[y][x][c];
if (c < channels - 1)
cout << ",";
}
cout << ") ";
}
cout << endl;
}
}
};
/**
* Converts a color image to grayscale
*
* Steps:
* 1. Create a new single-channel image with the same width and height
* 2. For each pixel in the input image:
* - Get the R, G, and B values
* - Calculate the grayscale value using the formula:
* gray = 0.299 * R + 0.587 * G + 0.114 * B
* - Set the grayscale value in the output image
* 3. Return the grayscale image
*/
Image convertToGrayscale(const Image &input)
{
int height = input.getHeight();
int width = input.getWidth();
Image output(width, height, 1); // Single channel for grayscale
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int R = input(y, x, 0);
int G = input(y, x, 1);
int B = input(y, x, 2);
int gray = (int)(0.299 * R + 0.587 * G + 0.114 * B);
output(y, x, 0) = gray;
}
}
return output;
}
/**
* Flips image horizontally (left to right)
*
* Steps:
* 1. Create a new image with the same dimensions as the input
* 2. For each pixel in the input image:
* - Copy the pixel from position (y, x) in the input
* - To position (y, width - 1 - x) in the output
* 3. Return the flipped image
*/
Image flipHorizontal(const Image &input)
{
int height = input.getHeight();
int width = input.getWidth();
int channels = input.getChannels();
Image output(width, height, channels);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
for (int c = 0; c < channels; c++)
{
// Mirror horizontally: column x maps to column (width-1-x)
output(y, width - 1 - x, c) = input(y, x, c);
}
}
}
return output;
}
/**
* Flips image vertically (top to bottom)
*
* Steps:
* 1. Create a new image with the same dimensions as the input
* 2. For each pixel in the input image:
* - Copy the pixel from position (y, x) in the input
* - To position (height - 1 - y, x) in the output
* 3. Return the flipped image
*/
Image flipVertical(const Image &input)
{
int height = input.getHeight();
int width = input.getWidth();
int channels = input.getChannels();
Image output(width, height, channels);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
for (int c = 0; c < channels; c++)
{
// Mirror vertically: row y maps to row (height-1-y)
output(height - 1 - y, x, c) = input(y, x, c);
}
}
}
return output;
}
/**
* Adjusts image brightness
*
* Steps:
* 1. Create a new image with the same dimensions as the input
* 2. For each pixel and each color channel:
* - Add the brightness value to the pixel value
* - Clamp the result between 0 and 255
* 3. Return the adjusted image
*/
Image adjustBrightness(const Image &input, int value)
{
int height = input.getHeight();
int width = input.getWidth();
int channels = input.getChannels();
Image output(width, height, channels);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
for (int c = 0; c < channels; c++)
{
// Add the brightness offset
int newVal = input(y, x, c) + value;
// Clamp the result to the valid pixel range [0, 255]
output(y, x, c) = max(0, min(255, newVal));
}
}
}
return output;
}
/**
* Adjusts image contrast
*
* Steps:
* 1. Create a new image with the same dimensions as the input
* 2. For each pixel and each color channel:
* - Subtract 128 from the pixel value to center around 0
* - Multiply by the contrast factor
* - Add 128 to center back around 128
* - Clamp the result between 0 and 255
* 3. Return the adjusted image
*/
Image adjustContrast(const Image &input, float factor)
{
int height = input.getHeight();
int width = input.getWidth();
int channels = input.getChannels();
Image output(width, height, channels);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
for (int c = 0; c < channels; c++)
{
// Scale around midpoint 128 using float arithmetic first
float newVal = factor * (input(y, x, c) - 128.0f) + 128.0f;
// Cast to int, then clamp to valid range [0, 255]
output(y, x, c) = max(0, min(255, (int)newVal));
}
}
}
return output;
}
/**
* Applies a simple blur filter
*
* Steps:
* 1. Create a new image with the same dimensions as the input
* 2. For each pixel (excluding borders):
* - For each color channel:
* - Calculate the average of the 3x3 neighborhood
* - Set the output pixel to this average value
* 3. Return the blurred image
*/
Image applyBlur(const Image &input)
{
int height = input.getHeight();
int width = input.getWidth();
int channels = input.getChannels();
Image output(width, height, channels);
for (int y = 1; y <= height - 2; y++)
{
for (int x = 1; x <= width - 2; x++)
{
for (int c = 0; c < channels; c++)
{
int sum = 0;
for (int ky = -1; ky <= 1; ky++)
{
for (int kx = -1; kx <= 1; kx++)
{
sum += input(y + ky, x + kx, c);
}
}
output(y, x, c) = sum / 9;
}
}
}
return output;
}
/**
* Rotates image 90 degrees clockwise
*
* Steps:
* 1. Create a new image with swapped dimensions (height becomes width, width becomes height)
* 2. For each pixel in the input image:
* - Copy the pixel from position (y, x) in the input
* - To position (x, height - 1 - y) in the output
* 3. Return the rotated image
*/
Image rotate90(const Image &input)
{
int height = input.getHeight();
int width = input.getWidth();
int channels = input.getChannels();
Image output(height, width, channels); // Width and height are swapped
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
for (int c = 0; c < channels; c++)
{
output(x, height - 1 - y, c) = input(y, x, c);
}
}
}
return output;
}
// Creates a simple 4x4 test image with a pattern
void createTestImage(const string &filename)
{
Image img(4, 4);
// Create a simple 4x4 pattern
// Row 0
img(0, 0, 0) = 255;
img(0, 0, 1) = 0;
img(0, 0, 2) = 0; // Red
img(0, 1, 0) = 0;
img(0, 1, 1) = 255;
img(0, 1, 2) = 0; // Green
img(0, 2, 0) = 0;
img(0, 2, 1) = 0;
img(0, 2, 2) = 255; // Blue
img(0, 3, 0) = 255;
img(0, 3, 1) = 255;
img(0, 3, 2) = 255; // White
// Row 1
img(1, 0, 0) = 255;
img(1, 0, 1) = 255;
img(1, 0, 2) = 0; // Yellow
img(1, 1, 0) = 255;
img(1, 1, 1) = 0;
img(1, 1, 2) = 255; // Magenta
img(1, 2, 0) = 0;
img(1, 2, 1) = 255;
img(1, 2, 2) = 255; // Cyan
img(1, 3, 0) = 128;
img(1, 3, 1) = 128;
img(1, 3, 2) = 128; // Gray
// Row 2
img(2, 0, 0) = 255;
img(2, 0, 1) = 128;
img(2, 0, 2) = 0; // Orange
img(2, 1, 0) = 128;
img(2, 1, 1) = 255;
img(2, 1, 2) = 0; // Light Green
img(2, 2, 0) = 128;
img(2, 2, 1) = 0;
img(2, 2, 2) = 255; // Purple
img(2, 3, 0) = 255;
img(2, 3, 1) = 128;
img(2, 3, 2) = 128; // Pink
// Row 3
img(3, 0, 0) = 128;
img(3, 0, 1) = 255;
img(3, 0, 2) = 128; // Light Green
img(3, 1, 0) = 128;
img(3, 1, 1) = 128;
img(3, 1, 2) = 255; // Light Blue
img(3, 2, 0) = 255;
img(3, 2, 1) = 255;
img(3, 2, 2) = 128; // Light Yellow
img(3, 3, 0) = 0;
img(3, 3, 1) = 0;
img(3, 3, 2) = 0; // Black
img.savePPM(filename);
cout << "Created 4x4 test image: " << filename << endl;
// Print the image data to console
cout << "\nOriginal image data:\n";
img.print();
}
int main()
{
cout << "Image Processing with Matrices - Student Project\n";
cout << "================================================\n\n";
// Create a 4x4 test image
createTestImage("test_image.ppm");
// Load the image
Image input;
if (!input.loadPPM("test_image.ppm"))
{
cerr << "Failed to load image. Exiting.\n";
return 1;
}
cout << "\nImage loaded successfully. Dimensions: "
<< input.getWidth() << "x" << input.getHeight() << "\n\n";
// Apply various transformations
cout << "Applying image transformations...\n";
Image gray = convertToGrayscale(input);
gray.savePPM("gray_image.ppm");
cout << "- Grayscale conversion completed\n";
cout << "Grayscale image data:\n";
gray.print();
cout << endl;
Image flippedH = flipHorizontal(input);
flippedH.savePPM("flipped_horizontal.ppm");
cout << "- Horizontal flip completed\n";
cout << "Horizontally flipped image data:\n";
flippedH.print();
cout << endl;
Image flippedV = flipVertical(input);
flippedV.savePPM("flipped_vertical.ppm");
cout << "- Vertical flip completed\n";
cout << "Vertically flipped image data:\n";
flippedV.print();
cout << endl;
Image bright = adjustBrightness(input, 50);
bright.savePPM("bright_image.ppm");
cout << "- Brightness adjustment completed\n";
cout << "Brightness adjusted image data:\n";
bright.print();
cout << endl;
Image contrast = adjustContrast(input, 1.5f);
contrast.savePPM("contrast_image.ppm");
cout << "- Contrast adjustment completed\n";
cout << "Contrast adjusted image data:\n";
contrast.print();
cout << endl;
Image blur = applyBlur(input);
blur.savePPM("blurred_image.ppm");
cout << "- Blur filter completed\n";
cout << "Blurred image data:\n";
blur.print();
cout << endl;
Image rotated = rotate90(input);
rotated.savePPM("rotated90_image.ppm");
cout << "- 90-degree rotation completed\n";
cout << "Rotated image data:\n";
rotated.print();
cout << endl;
cout << "\nAll operations completed successfully!\n";
cout << "Check the generated PPM files to see the results.\n";
cout << "Use an image viewer that supports PPM format or convert them to PNG/JPG.\n";
return 0;
}