#include #include //count # of pixels of specified intensity in pgm image int main (int argc, char *args[]) { char c; char imageType[3]; int width, height, maxIntensity; int frequency[256]; int i,j; int gray = 0; //read magic number scanf ( "%s", imageType); //Stop if image type is not P5 if (strcmp(imageType,"P5") != 0) { printf("Bad image format. Check image format and run again\n"); exit(1); } //Read and discard characters till you see a newline do { c = fgetc(stdin); } while ( c != '\n'); c = fgetc(stdin); //Read next character. This may be the # character if ( c == '#') { //file has a comment //Read and discard characters till you see a newline do { c = fgetc(stdin); } while ( c != '\n'); } else {// file has no comment ungetc(c, stdin); } scanf("%d %d", &width, &height); scanf("%d", &maxIntensity); //Read and discard characters till you see a newline do { c = fgetc(stdin); } while ( c != '\n'); //end of header processing //initialize the frequency array for(i=0; i < 256; i++) { frequency[i] = 0; } //read grayscale image data and compute the histogram for ( i=0; i < width*height; i++ ) { scanf ( "%c", &gray); frequency[gray]++; } //Print the # of pixels with the specified intensities for(i=1; i < argc; i++) { int intensity = atoi(args[i]); printf("There are %d pixels with intensity %d\n", frequency[intensity], intensity); } }