Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You\'ll write a program named imgGen that creates a simple Portable GrayMap (PGM

ID: 3875554 • Letter: Y

Question

You'll write a program named imgGen that creates a simple Portable GrayMap (PGM) image file » You'll create the file data in memory (in a 2-d array) and then write it to a disk file You'll then view the resulting image file using irfanview or gimp to verify it's correct. o irfanview is an image viewer. Fairly small download. o gimp is an image editor. Much large download. You can download either of those for free. Use google to find them. Details The format of the PGM image file you'll create is described here: http://en.wikipedia.org/wiki Netpbm format#PGM example The image must: » be at least 300 pixels wide by 200 pixels tall » consist of grayscale values from 0 to 255 » contain at least two rectangles » contain at least two circles (to have the possibility of getting an A) Your program's main will declare the array, then call the following functions two functions (which you'll write) void createlmage(unsigned char image[][WIDTH], int height); bool writeImage(const unsigned char image[][WIDTH], int height, const string fileName ); The createlmage() function will create all the image's pixel values. It must make use of the following function (which you'll write) void drawrect(unsigned char image][WIDTH], int height, int rectTop, int rectLeft, int rectHeight, int rectWidth, int grayLevel); And to have the possibility of getting an A: void drawcircle(unsigned char image[][WIDTH], int height, int centerX, int centerY, int radius, int grayLevel) The writelmage) function will write the array data to the specified text file (along with a few header lines) Things to keep in mind about arrays Arrays are always passed to functions by reference: no & required. You can pass one row of a two-dimensional array to a function that will then receive it as a single dimensional array Again, it's passed by reference and no & is required. When things are passed by reference and are not going to be changed), you should put const in front of the parameter declaration. Arrays can be big, and the stack might not be big enough to hold it, so if it doesn't fit on the stack you can move it to the heap by making the array variable static (put the word static before it) · » . »

Explanation / Answer

PGM images are grayscale image file formats. It is of two types, containing magic identifiers namely P2 and P5. P2 stores pixel values as the ASCII characters for the digits, delimited by spaces between consecutive pixel values. P5 writes the bytes without delimiters binary numbers from 0 to 255.In P5 PGM image file is considered where each pixel is stored as a byte that is a binary number from 0 to 255 (text editors show 0 through 127 as ASCII characters and 128 through 255 at whatever code the editor uses for those values).

#include <stdio.h>

#include <stdlib.h>

#include "PGM.h"

int pgmRead (char *fileName,long *rows,long *cols, unsigned char image[MAXROWS][MAXCOLS]) {

      FILE *filePointer;   

      char line[MAXLENGTH];

      int maximumValue = 0;

      int binary;          

      long numberRead = 0; /* counter for number of pixels read */

      long i,j;            

      int test,temp;       

      /* Open the file, return an error if necessary. */

     

    if ((filePointer = fopen(fileName,"r")) == NULL) {

          printf ("ERROR: cannot open file ");

          fclose (filePointer);

          return (0);

      }

   

      /* Initialize columnsidth, and height */

      *cols = *rows =0;

     

      fgets (line,MAXLENGTH,filePointer);

      while (line[0]=='#' || line[0]==' ') fgets (line,MAXLENGTH,filePointer);

      if (line[0]=='P' && (line[1]=='2')) {

             binary = 0;

          /*   printf (" File Format: P2 "); */

      }

      else if (line[0]=='P' && (line[1]=='5')) {

             binary = 1;

          /* printf (" FORMAT: P5 "); */

      }

      else {

             printf ("ERROR: incorrect file format ");

             fclose (filePointer);

             return (0);

      }         

     

      fgets (line,MAXLENGTH,filePointer);

      while (line[0]=='#' || line[0]==' ') fgets (line,MAXLENGTH,filePointer);

      sscanf (line,"%ld %ld",cols,rows);

      fgets (line,MAXLENGTH,filePointer);

      while (line[0]=='#' || line[0]==' ') fgets(line,MAXLENGTH,filePointer);

      scanf (line,"%d",&maximumValue);

     

   printf("Rows: %ld   Columns: %ld ",*rows,*cols);

   printf("Maximum value: %d ",maximumValue);

      if ((*cols)<1 ||(*rows)<1 || maximumValue<0 || maximumValue>MAXVALUE){

printf ("ERROR: invalid file specifications (cols/rows/max value) ");

             fclose (filePointer);

             return (0);

      }

      else if ((*cols) > MAXCOLS || (*rows) > MAXROWS) {

             printf ("ERROR: increase MAXROWS/MAXCOLS in PGM.h");

             fclose (filePointer);

             return (0);

      }

     

      if (binary) {

             for (i = 0; i < (*rows); i++) {

                  numberRead += fread((void *)&(image[i][0]),

                   sizeof(unsigned char),(*cols),filePointer);

                   if (feof(filePointer)) break;

             }

      }

      else {

             for (i= 0; i < (*rows); i++) {

                  for (j =0; j < (*cols); j++) {

                       test = fscanf (filePointer,"%d",&temp);

                        if (test == EOF) break;

                        image[i][j] = (unsigned char)temp;

                        numberRead++;

                   }

                   if (test == EOF) break;

             }

      }

   /*    printf (" Rows * Columns = %ld ",(*rows)*(*cols)); */

/*       printf (" Number of pixels read = %ld ",numberRead); */

      /* Insure the number of pixels read is at least the

       *   number indicated by w*h.

       * If not, return an error message, but proceed */

      if (numberRead < ((*rows)*(*cols))) {

             printf ("ERROR: fewer pixels than rows*cols indicates ");

      }

    

      fclose (filePointer);

      return (1);

}

   

int pgmWrite(char* filename, long rows,long cols,

               unsigned char image[MAXROWS][MAXCOLS],char*comment_string) {

      FILE* file;       

      int maxval;       

      long nwritten = 0;

      long i,j;         

      if (rows > MAXROWS || cols > MAXCOLS) {

           printf ("ERROR: row/col specifications larger than image array: ");

             return (0);

      }

     

      if ((file = fopen(filename, "w")) == NULL)     {

           printf("ERROR: file open failed ");

             return(0);

      }

      fprintf(file,"P5 ");

      if (comment_string != NULL) fprintf(file,"# %s ", comment_string);

   

      /* write the dimensions of the image */

      fprintf(file,"%ld %ld ", cols, rows);

  

      fprintf(file, "%d ", (int)255);

      /* Write data */

      for (i=0; i < rows; i++) {

          nwritten += fwrite((void*)&(image[i][0]),sizeof(unsigned char),

                              cols, file);

      }

     

printf (" Number of pixels total(from rows*cols): %ld ",rows*cols);

printf ("Number of pixels written: %ld ",nwritten);

      fclose(file);

      return(1);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote