void *readImage(FILE *inf, int opt, FILE *outf){ int width, height; int max; int r,c; int pixel; // Process the pgm file as a bufft // I.e., eat the header eatLine(inf); eatLine(inf); fscanf(inf,"%d",&width); // the first integer on line 1 is the width fscanf(inf,"%d",&height); // second is the height fscanf(inf,"%d",&max); // third is max number of pixels float *array = malloc(sizeof(float)*width*height); int p; // iterate through rows and columns // scan in graymap values, convert to pixels // fill array with pixel values for (r=0; r < height; r++) { for (c=0; c < width; c++) { fscanf(inf,"%d",&pixel); p = c + r*width; array[p] = (float)pixel/(float)max; } } // Select function to call // switch (opt) { case(ASCII): echoASCII(array,height,width,outf); break; case(INV): invert(array,height,width,outf); case(BLUR): blur(array,height,width,outf); } // exit(-1); }
// main // // This program accepts three arguments: a processing option // ("blur", "invert", or "ascii"), a PGM file name for input, // and a text file name for output. It reads the PGM file and // creates an output file with either an appropriate PGM (if // one of the first two options are given) or a text file (if // the last option is given). // // Right now, only the "ascii" option works. // int main(int argc, char **argv) { // input and output file "handles" FILE *inf, *outf; if (argc < 4) { // whoops! not enough arguments fprintf(stderr,"Error: not enough arguments!\n"); usage(argv[0]); return -1; } else { // open the input (PGM) file inf = fopen(argv[2],"r"); if (inf == NULL) { fprintf(stderr,"Error: can't open file '%s' for reading.\n",argv[2]); return -1; } // open the output file outf = fopen(argv[3],"w"); if (outf == NULL) { fprintf(stderr,"Error: can't open file '%s' for writing.\n",argv[3]); return -1; } if (strcmp(argv[1],"--blur") == 0) { blurImage(inf,outf); } else if (strcmp(argv[1],"--invert") == 0) { invertImage(inf,outf); // // write the code that inverts the image // } else if (strcmp(argv[1],"--ascii") == 0) { echoASCII(inf,outf); // // change this so that it is given an image array and // the outf // } else { fprintf(stderr,"Error: unrecognized option '%s.'\n",argv[1]); usage(argv[0]); // return FAIL return -1; } // close the files fclose(inf); fclose(outf); // return OK return 0; } }