-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab12extra.cpp
More file actions
360 lines (286 loc) · 7.78 KB
/
lab12extra.cpp
File metadata and controls
360 lines (286 loc) · 7.78 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
//Name: Autumn Henderson
//Date: November 14th, 2018
//Title: Lab 12
//Description: This program takes a picture and either does nothing to the picture,
//inverts it, flips it upside down, or flips it from left to right.
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <ostream>
#include <limits>
using namespace std;
//Struct Pixel creates three integers that store the red, green, and blue channel of a pixel
struct Pixel{
unsigned int red;
unsigned int green;
unsigned int blue;
};
//Class Picture creates several members for handling the picture provided
class Picture{
private:
vector<Pixel> pixelStructures;
unsigned int width;
unsigned int height;
unsigned int maxIntensity;
public:
Picture();
const Pixel& get_pixel(unsigned int row, unsigned int column) const;
Pixel& get_pixel(unsigned int row, unsigned int column);
void set_pixel(unsigned int row, unsigned int column, const Pixel &pixel);
void Invert();
void flip_x();
void flip_y();
bool read_input(istream &in);
void write_output(ostream &out);
};
int main(int argc, char *argv[]) {
Picture aboutPic;
ifstream ppmFile;
string cinPPM;
istringstream cinISS;
ofstream newPPMFile;
string consolePPM;
ostringstream out;
bool readFile = false;
string inputInfo;
string outputInfo;
string command;
//Initially checks argc to make sure there are at least 3 arguments provided. Warns user and quits program if there are not.
if (argc < 3) {
cout << "Not enough arguments. Need the following: <program_name> <file_to_open or '-'> <file_to_close or '-'> <OPTIONAL: I, Y, X commands>.\n";
return -1;
}
inputInfo = argv[1];
//Reads from cin
if (inputInfo == "-") {
cout << "Type in PPM file: ";
cin >> cinPPM;
cinISS.str(cinPPM);
readFile = aboutPic.read_input(cinISS);
}
//Reads from file
else {
ppmFile.open(inputInfo);
if(!ppmFile.is_open()) {
cout << "Unable to open " << inputInfo << '\n';
return -1;
}
readFile = aboutPic.read_input(ppmFile);
}
if (readFile == false) {
cout << "Unable to read file.\n";
return -1;
}
//Performs conversions
if (argc == 4 && readFile == true) {
command = argv[3];
if (command == "I" || command == "i") {
aboutPic.Invert();
}
else if (command == "Y" || command == "y") {
aboutPic.flip_y();
}
else if (command == "X" || command == "x") {
aboutPic.flip_x();
}
}
//Writes to cout
outputInfo = argv[2];
if (outputInfo == "-" && readFile == true) {
aboutPic.write_output(out);
consolePPM = out.str();
cout << consolePPM;
}
//Writes to file
else if (readFile == true) {
newPPMFile.open(outputInfo);
if(!newPPMFile.is_open()) {
cout << "Unable to open " << outputInfo << '\n';
return -1;
}
aboutPic.write_output(newPPMFile);
}
//Closes files
ppmFile.close();
newPPMFile.close();
return 0;
}
//Constructor Picture sets width, height, and maximum intensity to 0
Picture::Picture() {
width = 0;
height = 0;
maxIntensity = 0;
}
//Accessor get_pixel takes a row and a column and returns the given Pixel
const Pixel& Picture::get_pixel(unsigned int row, unsigned int column) const {
return pixelStructures.at((row * width) + column);
}
//Mutator get_pixel takes a row and column and returns the given Pixel
Pixel& Picture::get_pixel(unsigned int row, unsigned int column) {
return pixelStructures.at((row * width) + column);
}
//Mutator set_pixel takes a row and a column and a Pixel structure and sets the given
//pixel to the new Pixel value
void Picture::set_pixel(unsigned int row, unsigned int column, const Pixel &pixel) {
get_pixel(row, column) = pixel;
}
//Mutator Invert takes no arguments and returns nothing. It will invert the picture by
//updating the vector.
void Picture::Invert() {
Pixel oldPixel;
Pixel newPixel;
unsigned int x;
unsigned int y;
for(x = 0; x < height; ++x) {
for(y = 0; y < width; ++y) {
oldPixel = get_pixel(x, y);
newPixel.red = maxIntensity - oldPixel.red;
newPixel.green = maxIntensity - oldPixel.green;
newPixel.blue = maxIntensity - oldPixel.blue;
set_pixel(x, y, newPixel);
}
}
}
//Mutator flip_x takes no arguments and returns nothing. It will flip the picture
//upside down.
void Picture::flip_x() {
Pixel topPixel;
Pixel bottomPixel;
unsigned int x;
unsigned int y;
for(x = 0; x < height/2; ++x) {
for(y = 0; y < width; ++y) {
topPixel = get_pixel(x, y);
bottomPixel = get_pixel((height - 1 - x), y);
set_pixel(x, y, bottomPixel);
set_pixel(height - 1 - x, y, topPixel);
}
}
}
//Mutator flip_y takes no arguments and returns nothing. It will flip the picture
//from left to right.
void Picture::flip_y() {
Pixel leftPixel;
Pixel rightPixel;
unsigned int x;
unsigned int y;
for(x = 0; x < height; ++x) {
for(y = 0; y < (width/2); ++y) {
leftPixel = get_pixel(x, y);
rightPixel = get_pixel(x, width - 1 - y);
set_pixel(x, y, rightPixel);
set_pixel(x, width - 1 -y, leftPixel);
}
}
}
//Mutator read_input takes an istream and processes the file by assigning the P3, width,
//height, maximum intensity, and all rgb pixel values.
bool Picture::read_input(istream &in) {
bool ableToRead = false;
istringstream ISS;
string fileIsP3;
string widthHeight;
string maxI;
string process;
string pixels;
unsigned int i = 0;
unsigned int WH;
Pixel pix;
//Process P3 line
getline(in, fileIsP3);
if (fileIsP3 != "P3") {
cout << "File is not P3.\n";
return ableToRead;
}
//Process width and height
do {
getline(in, widthHeight);
ISS.clear();
ISS.str(widthHeight);
//Ignores comments to get width and height
if(widthHeight.at(0) == '#') {
ISS.clear();
ISS.ignore(numeric_limits<streamsize>::max(), '\n');
}
else{
break;
}
}while (true);
ISS >> width >> height;
WH = width * height;
//Reserves space using width * height
pixelStructures.reserve(WH);
//Process max intensity
do {
getline(in, maxI);
ISS.clear();
ISS.str(maxI);
//Ignores comments to get maximum intensity
if(maxI.at(0) == '#') {
ISS.clear();
ISS.ignore(numeric_limits<streamsize>::max(), '\n');
}
else{
break;
}
}while (true);
ISS >> maxIntensity;
//Process pixels
while (getline(in, process)) {
if (process.at(0) == '#')
continue;
else
pixels += process + " ";
}
ISS.clear();
ISS.str(pixels);
//Insert pixels into vector
while ((ISS >> pix.red >> pix.green >> pix.blue) && (i <= WH)) {
//Checks intensity
if (pix.red > maxIntensity) {
cout << "Red pixel being stored in vector location " << i
<< " is greater than the max intensity and is being changed to "
<< maxIntensity << '\n';
pix.red = maxIntensity;
}
if (pix.green > maxIntensity) {
cout << "Green pixel being stored in vector location " << i
<< " is greater than the max intensity and is being changed to "
<< maxIntensity << '\n';
pix.green = maxIntensity;
}
if (pix.blue > maxIntensity) {
cout << "Blue pixel being stored in vector location " << i
<< " is greater than the max intensity and is being changed to "
<< maxIntensity << '\n';
pix.blue = maxIntensity;
}
//Places pixel in vector
pixelStructures.push_back(pix);
++i;
}
//Checks to make sure there is enough information
if (i == (WH)) {
ableToRead = true;
}
else if (i < (WH)) {
cout << "Not enough information in file to change picture.\n";
return ableToRead;
}
return ableToRead;
}
//Accessor write_output takes an ostream and returns nothing. It places the new
//picture values into the ostream.
void Picture::write_output(ostream &out) {
Pixel newPixels;
out.clear();
out << "P3.\n";
out << width << " " << height << '\n';
out << maxIntensity << '\n';
for (unsigned int i = 0; i < (width * height); ++i) {
newPixels = pixelStructures.at(i);
out << newPixels.red << " " << newPixels.green << " " << newPixels.blue << '\n';
}
}