-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
94 lines (83 loc) · 2.81 KB
/
main.cpp
File metadata and controls
94 lines (83 loc) · 2.81 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
// Fatemeh Saremi
// Assignment 2
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int MAX_SIZE = 700; //Largest length for array
typedef int array [MAX_SIZE][MAX_SIZE][3];
void ReadFile(ifstream &file, int colors[MAX_SIZE][MAX_SIZE][3], int &nRows,
int &nCols, int &brightness, string &header);
int main()
{
ifstream inputFile;
ofstream outputFile;
array colors; //Array for pixel values
//Variables for image header
int nRows, nCols, brightness;
string header, imgName;
cout << "Enter the directory: ";
cin >> imgName;
//Testing if a directory with user input name exists
inputFile.open(imgName + "/" + imgName + "_001" + ".ppm");
if(inputFile.is_open()) //if a file with the name entered exists, else program exists
{
inputFile.close(); //closing the file that was opened to validate user input
for(int i = 1; i <= 10; i++) //loop for opening images 1 - 10
{
if(i < 10)
{
inputFile.open(imgName + "/" + imgName + "_00" + to_string(i) + ".ppm");
}
if(i == 10)
{
inputFile.open(imgName + "/" + imgName + "_0" + to_string(i) + ".ppm");
}
//function to read pixels into the array
ReadFile(inputFile, colors, nRows, nCols, brightness, header);
inputFile.close();
}
//loop for averaging the array values between 10 images
for(int r = 0; r < nRows; r++) {
for(int c = 0; c < nCols; c++){
for(int color = 0; color < 3; color++) {
colors[r][c][color] /= 10;
}
}
}
outputFile.open(imgName + "_good.ppm");
outputFile << header << endl;
outputFile << nRows << " " << nCols << endl;
outputFile << brightness << endl;
for(int r = 0; r < nRows; r++) {
for(int c = 0; c < nCols; c++){
for(int color = 0; color < 3; color++) {
outputFile << colors[r][c][color] << " ";
}
outputFile << endl;
}
}
outputFile.close();
}
else
{
cout << "The directory does not exist." << endl
<< "Program exit." << endl;
}
return 0;
}
void ReadFile(ifstream &file, int colors[MAX_SIZE][MAX_SIZE][3], int &nRows,
int &nCols, int &brightness, string &header)
{
int temp;
file >> header;
file >> nRows >> nCols >> brightness;
for (int r = 0; r < nRows; r++) {
for (int c = 0; c < nCols; c++) {
for (int color = 0; color < 3; color++) {
file >> temp;
colors[r][c][color] += temp;
}
}
}
}