/* histogram.c */ #include #include #include "mypgm.h" #define IMAGESIZE 256 /* = GRAYLEVEL */ void make_histogram_image( ) /* Histogram of image1 is output into image2 */ { long int histogram[GRAYLEVEL]; long int max_frequency; /* Maximum frequency */ int i, j, x, y; /* control variable */ int height; /* height of histogram */ int top; /* the most frequent graylevel */ /* Calculation of histogram */ for (i = 0; i < GRAYLEVEL; i++) { histogram[i] = 0; } printf("Now, histogram of input image is calculated\n\n"); for (y = 0; y < y_size1; y++) { for (x = 0; x < x_size1; x++) { histogram[image1[y][x]]++; } } /* calculation of maximum frequency */ max_frequency = histogram[0]; top = 0; for (i = 1; i < GRAYLEVEL; i++) { if (histogram[i] > max_frequency) { max_frequency = histogram[i]; top = i; } } printf("Maximum frequency = %d at level of %d\n\n", max_frequency, top); /* Histogram image generation */ x_size2 = IMAGESIZE; y_size2 = IMAGESIZE; for (y = 0; y < y_size2; y++) { for (x = 0; x < x_size2; x++) { image2[y][x] = 0; } } for (i = 0; i < GRAYLEVEL; i++) { height = (int)(MAX_BRIGHTNESS / (double)max_frequency * (double)histogram[i]); for (j = 0; j < height; j++) { image2[IMAGESIZE-1-j][i] = MAX_BRIGHTNESS; } } } main( ) { load_image_data( ); /* Input of image1 */ make_histogram_image( ); /* Histogram generation */ save_image_data( ); /* Output of image2 */ return 0; }