Write a C that does the following Read a PGM image store it
Write a C++ that does the following :
Read a PGM image, store it in an array, and output it to a new file (with a different name!). Once you get that working, see if you can get any of the following working: Create a photographic negative of it Create a thumbnail (max # rows or columns: 64) Rotate it 180 degrees (simple...read the data and store it in the array starting at the end instead of the beginning) And here\'s a cool one: subtract each pixel from the one right before it. (For column 0, set it to zero.) The add 1/2 maxval. Do it, look at it, and figure out why it looks the way it does. BTW, this is essentially doing a horizontal high pass filter of the image. What features are left, and what features disappear?Solution
this for to store pgm file as taken example
char matrix[80][800];
int index=0;
ifstream infile(\"something.pgm\");
infile.ignore(2000\'\ \'); //ignore the first line
infile.ignore(2000\'\ \'); //these can also be changed to
infile.ignore(2000\'\ \'); //infile.getline(...) so that you
infile.ignore(2000\'\ \'); //can use the data if need be.
while(!infile.eof())
{
infile.getline(matrix[index],90,\'\ \');
index++;
}
infile.close();
//This is reading binary file(pgm)
typedef unsigned char pixel;
typedef struct {
char filetype[6];
int width;
int height;
int maxgrey;
pixel* image;
} PgmType;
void readpgm (PgmType* pgmfile, char input[])
{
stream infile; //in stream
fpos_t filepos; //bytes in header text
infile = fopen(input, \"r\");
if (infile == NULL) fatal(\"Cannot open input file\");
do
{
if(fscanf(infile, \"%2s%d%d%d\", &pgmfile->filetype, &pgmfile->width, &pgmfile->height, &pgmfile->maxgrey) != 8) //read in header file and check for error
fatal(\"Input nt successfuly read\");
if(strcmp(pgmfile->filetype, \"P3\") != 0)
{
fatal(\"File invalid: Does not start with \\\"P5\\\"\");
break;
}
}
while(fgetc(infile) != \'\ \');
fgetpos(infile, &filepos);
//determines how many bytes the header text is
printf(\"%i\ \",filepos);
fclose(infile); //close input file
infile = fopen(input, \"rb\");
if(infile == NULL) fatal(\"Cannot open input file\");
fsetpos(infile, &filepos); //skip over header and position pointer to the first pixel
pgmfile->image = (pixel*) malloc(pgmfile->width * pgmfile->height * sizeof(pixel));
if(pgmfile->image == NULL)
fatal(\"Error allocating memory\");
while(!feof (infile))
{
printf(\"%i\",fread(pgmfile->image, sizeof(pixel), pgmfile->width * pgmfile->height, infile));
}
}

