Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <sstream>
- using namespace std;
- struct PixelStruct {
- int red, green, blue;
- PixelStruct (int r, int g, int b);
- PixelStruct();
- };
- class PixelInfo {
- vector <vector<PixelStruct> > picture_data;
- int intensity, numRows, numCols;
- public:
- bool readPPM (ifstream& fin);
- bool writePPM (ofstream& fout);
- void flipX ();
- void flipY ();
- void flatRed ();
- void greyOut ();
- };
- int main(int argc, char* argv[]) {
- ifstream fin;
- ofstream fout;
- char funcCall;
- istringstream sin;
- PixelInfo info;
- fin.open(argv[1]);
- info.readPPM(fin);
- fout.open(argv[2]);
- if (fin.fail() && argc != 3)
- cout << "Usage: inputfile outputfile [XYFG]";
- if (argc == 4) {
- sin.clear();
- sin.str(argv[3]);
- sin >> funcCall;
- if (funcCall != 'X' && funcCall != 'Y' && funcCall != 'F' && funcCall != 'G') {
- cout << "Error: " << funcCall << " is an invalid command. Use either X, Y, F or G";
- return false;
- }
- switch (funcCall) {
- case 'X':
- info.flipX();
- break;
- case 'Y':
- info.flipY();
- break;
- case 'F':
- info.flatRed();
- break;
- case 'G':
- info.greyOut();
- break;
- default:
- cout << "Error: " << funcCall << " is an invalid command. Use either X, Y, F or G";
- }
- }
- info.writePPM(fout);
- fout.close();
- fin.close();
- return 0;
- }
- PixelStruct::PixelStruct(int r, int g, int b) {
- red = r;
- green = g;
- blue = b;
- }
- PixelStruct::PixelStruct() {
- PixelStruct(0, 0, 0); }
- bool PixelInfo::readPPM (ifstream& fin) {
- vector <PixelStruct> colorLine;
- PixelStruct temp;
- string fileNum;
- fin >> fileNum;
- fin >> numRows;
- fin >> numCols;
- fin >> intensity;
- for (int i = 0; i < numRows; i++) {
- colorLine.clear();
- for (int j = 0; j < numCols; j++) {
- fin >> temp.red >> temp.green >> temp.blue;
- colorLine.push_back (temp);
- }
- picture_data.push_back(colorLine);
- }
- return true;
- }
- bool PixelInfo::writePPM(ofstream& fout) {
- if (numRows == 0) {
- cout << "Error: tried to output an uninitialized PPM picture";
- return false;
- }
- fout << "P3" << endl << numRows << " " << numCols << endl << intensity << endl;
- for (int i = 0; i < numRows; i++) {
- for (int j = 0; j < numCols; j++) {
- fout << picture_data[i][j].red << " " << picture_data[i][j].green << " " << picture_data[i][j].blue << endl;
- }
- }
- return true;
- }
- void PixelInfo::flipX () {
- vector <vector <PixelStruct> > temp;
- temp = picture_data;
- for (int i = 0; i < numRows/2 ; i++) {
- for (int j = 0; j < numCols; j++) {
- picture_data[i][j] = temp[numRows - 1 - i][j];
- picture_data[numRows - 1 - i][j] = temp[i][j];
- }
- }
- }
- void PixelInfo::flipY() {
- vector <vector <PixelStruct> > temp;
- temp = picture_data;
- for (int i = 0; i < numRows; i++) {
- for (int j = 0; j < numCols/2; j++) {
- picture_data[i][j] = temp[i][numCols - 1 - j];
- picture_data[i][numCols - 1 - j] = temp[i][j];
- }
- }
- }
- void PixelInfo::flatRed() {
- for (int i = 0; i < numRows; i++ ) {
- for (int j = 0; j < numCols; j++) {
- picture_data[i][j].red = 0;
- }
- }
- }
- void PixelInfo::greyOut() {
- int tempVal;
- for (int i = 0; i < numRows; i++) {
- for (int j = 0; j < numCols; j++) {
- tempVal = (picture_data[i][j].red + picture_data[i][j].green + picture_data[i][j].blue) / 3;
- picture_data[i][j].red = tempVal;
- picture_data[i][j].green = tempVal;
- picture_data[i][j].blue = tempVal;
- tempVal = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement