Question: Optional Challenge: with PGM files and arrays Complete this program by adding three functions and their prototypes. This program should read a PGM from a

Optional Challenge: with PGM files and arrays

Complete this program by adding three functions and their prototypes. This program should read a PGM from a file, save it to a new file, and print the image header and pixel values to the console. You'll need one function to open a PGM file, a second one to save the PGM file already read into new PGM file, and the third one to display the array to the console.

(If the image is large, printing all the pixels may get rather awkward. If the image is large (say, more than 200 pixels total), you can prompt the user for a starting row and column and a number of rows and columns to print, then just display a selected portion of the array. This is optional, but you'll find this helpful when we get to the final project, so is worth the effort to do it now.)

Hints:

The readPGM function should take as an input parameter the input file name. Question: what are its outputs? How will you get that information back to the calling code? The savePGM and printPGM functions should receive all the information needed (type (P2), # of cols, etc., as well as the pixel array. Since they will not modify the array, how should you pass the array in? How should you pass in the other parameters?

Name your program PGM_input_output.cpp

#include  #include  #include  #include  // PGM Challenge program. Complete this program for extra credit. // using namespace std; bool get_filenames(string &infname, string &outfname); //...............pixel array is global, to avoid stack issues int pixels[512 * 512] = {0}; //...............put your prototypes here................... //.......................................................... int main (void) { string infilename, outfilename; // for image: string type; int ncols=0, nrows=0, maxval=0; // get input & output filenames, or bail out if problem if (!get_filenames(infilename, outfilename)) { cout << "Error opening input or output file "<> infname; ifstream ifs(infname.c_str()); // verify file is readable if (!ifs) { cout << "Error: can't open "<> outfname; ofstream ofs(outfname.c_str()); // verify we can write to the output file if (!ofs) { return false; // bail out if problem. } ofs.close(); // close it. you'll open it later in savePGM } //...............put your function implementations here................... 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!