-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgmManip.cpp
More file actions
196 lines (158 loc) · 3.84 KB
/
pgmManip.cpp
File metadata and controls
196 lines (158 loc) · 3.84 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
/*
* pgmManip.cpp
*
* Created on: Oct 25, 2013
* Author: Arthur Bousquet
*/
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include "functions.h"
using namespace std;
//int pgmRead (string somefile, string &tag, unsigned int &pgmCols, unsigned int &pgmRows, unsigned int &pgmMaxval, unsigned int pixels[][1024]);
int main ()
{
string tag = "null";
unsigned int pgmCols=0;
unsigned int pgmRows=0;
unsigned int pgmMaxval=0;
unsigned int pixels[1024][1024];
string defaultInFile = "somepic.pgm";
string defaultOutFile = "somepicMod.pgm";
cout << "*********************************"<<endl
<< "* Welcome to Hardy's *"<<endl
<< "* MAGNIFICENT PGM EDITOR *"<<endl
<< "* [and boring GIT presentation] *"<<endl
<< "*********************************"<<endl<<endl;
if (pgmRead (defaultInFile, tag, pgmCols, pgmRows, pgmMaxval, pixels))
{
cout << "Unexpected value in header. Please check source file.";
return 1;
}
unsigned int option = 5;
while (true)
{
//Options list
cout << "(1) Invert image"<<endl
<< "(2) Rotate image" <<endl
<< "(3) Output current image info to console"<<endl
<< "(4) Save current image" <<endl
<< "(5) Exit"<<endl;
cout << "Please select an operation [exit]: ";
cin >> option;
switch (option)
{
case 1:
pgmInvert(pgmCols, pgmRows, pgmMaxval, pixels);
//cout << "pgmInvert"<<endl;
break;
case 2:
//pgmRotate(pgmCols, pgmRows, pixels)
cout << "pgmRotate not implimented"<<endl;
break;
case 3:
pgmOut( tag, pgmCols, pgmRows, pgmMaxval, pixels );
//cout << "pgmOut" <<endl;
break;
case 4:
pgmSave( defaultOutFile, tag, pgmCols, pgmRows, pgmMaxval, pixels );
//cout << "pgmSave" <<endl;
break;
default:
cout << endl <<"Thanks and goodbye!";
return 0;
break;
}
}
//else
/*----PASS CHECKS
{
cout << tag << endl << pgmCols << endl << pgmRows << endl << pgmMaxval << endl << endl;
for ( unsigned int j = 0; j < pgmRows; j++ )
{
for ( unsigned int i = 0; i < pgmCols; i++ )
cout << pixels[j][i] << " ";
cout << endl;
}
}
//---!PASS CHECKS
//*/
return 0;
}
int pgmRead (string somefile, string &tag, unsigned int &pgmCols, unsigned int &pgmRows, unsigned int &pgmMaxval, unsigned int pixels[][1024])
{
string thisLine;
cout << "Please input the desired PGM filename [somepic.pgm] : ";
getline (cin, thisLine);
if (thisLine != "") //input, set filename for read
somefile = thisLine;
ifstream inFile (somefile.c_str());
if (!inFile.good()) //input file sanity check
return 1;
int row = 0; //row tracker
while ( row <3 ) //pull line
{
//cout << "The line is: " << thisLine << endl;
if (row == 0) //top line tag
{
getline(inFile, thisLine);
stringstream ss(thisLine);
ss >> tag;
row++;
}
else if (row == 1) //cols rows
{
getline(inFile, thisLine);
istringstream is(thisLine);
is >> pgmCols >> pgmRows;
row++;
}
else if (row == 2) //maxval
{
getline(inFile, thisLine);
istringstream is(thisLine);
is >> pgmMaxval;
row++;
}
}
unsigned int col=1; //column tracker
unsigned int x=0;
unsigned int help=0;
while (getline(inFile, thisLine))
{
istringstream is(thisLine);
while (is.good())
{
if ( (col) % (pgmCols+1) == 0)
{
row++;
col=1;
}
is >> x;
//cout << x<<endl;
pixels[row-3][col-1] = x;
col++;
help++;
}
}
//row++;
/*
//----PARSE CHECKS
cout << tag << endl << pgmCols << endl << pgmRows << endl << pgmMaxval << endl << endl;
for ( unsigned int j = 0; j < pgmRows; j++ )
{
for ( unsigned int i = 0; i < pgmCols; i++ )
cout << pixels[j][i] << " ";
cout << endl;
}
*///---!PASE CHECKS
//Quick sanity check
if ( tag == "null" || pgmRows == 0 || pgmCols == 0 || pgmMaxval == 0 )
{
inFile.close();
return 1;
}
inFile.close();
return 0;
}