Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Amelia Fass
- //Got help from programming lab
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <iomanip>
- using namespace std;
- class PgmPicture
- {
- private:
- short picture[1024][1024];
- //represents number of rows and columns in pgm image
- int numRows, numCols;
- //constructor intitializes numrows and numcols to 0
- public:
- PgmPicture();
- //reads pgm file into object, returns false if fails to open inputfile
- bool readPgmFile(string inputfile);
- //writes pgm image to output file, should return 0(succeeds),-1(pgm image unitnitialized)
- //or -2(function failed to open outputfile)
- int writePgmFile(string outputFile);
- void makeNegative ();
- void hflip();
- void vflip();
- };
- int main(int argc, char **argv)
- {
- if(argc != 3) {
- perror("fail");
- return -1;
- }
- PgmPicture mypic;
- mypic.readPgmFile(argv[1]);
- //mypic.makeNegative();
- //mypic.hflip();
- mypic.vflip();
- mypic.writePgmFile(argv[2]);
- return 0;
- }
- PgmPicture::PgmPicture()
- {
- numRows = 0;
- numCols = 0;
- }
- bool PgmPicture::readPgmFile(string inputfile)
- {
- ifstream fin;
- int i,j,pixel;
- string string;
- fin.open(inputfile.c_str());
- if (fin.fail()) {
- return false;
- } else {
- fin >>string >> numCols >> numRows>>pixel;
- for (i=0; i < numRows; i++) {
- for (j=0; j < numCols; j++) {
- fin >> pixel;
- picture[i][j] = pixel;
- }
- }
- fin.close();
- return true;
- }
- }
- int PgmPicture::writePgmFile(string outputFile)
- {
- int i,j;
- ofstream fout;
- fout.open(outputFile.c_str());
- if(fout.fail()) {
- perror("fail");
- return -2;
- }
- fout << "P2 " << numCols << " " << numRows << endl << "255" <<endl;
- for (i = 0; i < numRows; i++) {
- for (j=0; j < numCols; j++) {
- fout << picture[i][j] << " ";
- }
- fout << endl;
- }
- fout.close();
- if (numRows == 0) {
- perror("Error: Tried to output an uninitialized pgm object");
- return -1;
- } else
- return 0;
- }
- void PgmPicture:: makeNegative ()
- {
- int i,j;
- for (i = 0; i < numRows; i++) {
- for (j=0; j < numCols; j++) {
- picture[i][j] = 255 - picture[i][j];
- }
- }
- return;
- }
- void PgmPicture::hflip(){
- int i,j;
- short tmp;
- for ( i = 0; i < numRows; i++){
- for(j = 0;j < (numCols/2);j++){
- tmp=picture[i][j];
- picture[i][j]=picture[i][numCols-j-1];
- picture[i][numCols-j-1]=tmp;
- }
- }
- return;
- }
- void PgmPicture::vflip(){
- int i,j;
- short tmp;
- for ( i = 0; i < (numRows/2); i++){
- for(j = 0;j < numCols;j++){
- tmp=picture[i][j];
- picture[i][j]=picture[numRows-i][j];
- picture[numRows-i][j]=tmp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement