PGMs with arrays

My question has to deal with creating an array turning it into a PGM file, then writing it into a file, and then reading it back out into the console.
My question is, what are the functions i'll need? I've tried a few but can't seem to get everything that is needed in them.
Any advice would be appreciated.
Here's what I have so far...

#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

void get_filenames(string &infname, string &outfname);

//............. prototypes here...................


//..........................................................

int main (void)

{

string infilename, outfilename;
// for image:
string type;
int ncols=0, nrows=0, maxval=0;
static int pixels[1024 * 1024] = {0};

get_filenames(infilename, outfilename);

cout << "reading "<<infilename<<", displaying, and copying to "<<outfilename<<endl;

// Read the PGM file

readPGM(infilename, type, ncols, nrows, maxval, pixels);
if (ncols*nrows == 0) // if image array is size 0, problem!
{
cout << "error reading file "<<infilename<<endl;
return 0;
}




return 0;
}


// get input & output filenames, verify input file can be read
// outputs: infname and outfname, both strings.
void get_filenames(string &infname, string &outfname)
{
cout << "Enter the name of the PGM file: ";
cin >> infname;
ifstream ifs(infname.c_str()); // verify file is readable
if (!ifs)
{
cout << "Error: can't open "<<infname<< " for reading "<<endl;
exit(1); // bail out of program altogether.
}
ifs.close();

cout << "enter the outputname to copy the PGM to: ";
cin >> outfname;
ofstream ofs(outfname.c_str()); // verify can write to the output file
if (!ofs)
{
cout << "Error: can't open "<<outfname<< " for writing "<<endl;
exit(1); // bail out of program altogether.
}
ofs.close(); // close it. open it later in savePGM
}

//............ functions here...................
You seem to have all the information you need to read and write a PGM (ncols, nrows, maxval, pixels[]). You might want to implement that readPGM() function. If you want more functions, a subfunction just to read the image pixel gray values would work.

Writing a PGM would require a similar function, writePGM().

Please use [code] tags.

I also assume you have this information: http://netpbm.sourceforge.net/doc/pgm.html
Topic archived. No new replies allowed.