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
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
string get_filename( );
void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix);
void negatePGM(int nc, int nr, int mv, int *pix);
void writePGM(string fname, int nc, int nr, int mv, int *pix);
//.......................................
int main( )
{
int ncols=0, nrows=0, maxval=255;
int *pix = NULL; // instead of static int pix[1024*1024];
string filename = get_filename( );
readPGM(filename, ncols, nrows, maxval, pix);
negatePGM(ncols, nrows, maxval, pix);
writePGM("neg_"+filename, ncols, nrows, maxval, pix);
// Need to delete the pixel array
}
//.......................................
// get the filename of an existing file.
string get_filename( )
{
bool ok = false;
string fn = "";
do
{
if (fn.size()==0) cout << "filename? ";
else cout << "can't open "<<fn<<". re-enter filename: ";
cin >> fn;
ifstream ifs(fn.c_str());
if (ifs) ok = true;
} while (!ok);
return fn;
}
//readPGM: opens file, reads header, allocates pixel array, reads in pixels.
//note that nc, nr, mv and pix are all passed by reference.
void readPGM(string fname, int &nc, int &nr, int &mv, int * &pix)
{
// This is where I have trouble
}
//.......................................
//writePGM: opens output file, outputs header line, outputs pixel array.
void writePGM(string fname, int nc, int nr, int mv, int *pix)
{
// And here
}
//.......................................
//negatePGM: note that pix is passed by value. but since it is a pointer,
//we can still modify the pixel array by indexing it.
void negatePGM(int nc, int nr, int mv, int *pix)
{
for (int r=0;r<nr;r++)
for (int c=0;c<nc;c++)
pix[r*nc + c] = mv - pix[r*nc + c];
}
|