/* histogram.c ** this program ouputs the ** histogram of an image. */ #include #include #include #include #define pix_value_bound 255 main(int argc, char **argv) { FILE *fin, *fout; unsigned long int img_length; unsigned long int img_width, max_num, step1,step2,buf; unsigned long int hist[pix_value_bound],count1, count2, pix_num; unsigned char *store, *sub_row_start; float step; if(argc == 5) { img_length = atoi(argv[3]); img_width = atoi(argv[4]); store = (unsigned char *)malloc(img_width); if((fin = fopen(argv[1],"rb")) == NULL) { fprintf(stderr,"Failed to open %s\n",argv[1]); exit(EXIT_FAILURE); } if((fout = fopen(argv[2],"w")) == NULL) { fprintf(stderr,"Failed to open %s\n",argv[2]); exit(EXIT_FAILURE); } for(count1 = 0; count1 <= pix_value_bound; count1++) hist[count1] = 0; for(count1 = 0; count1 < img_length; count1++) { fread(store,1,img_width,fin); for(count2 = 0; count2 < img_width; count2++) { pix_num = store[count2]; hist[pix_num]++; } } max_num = 0; for(count1 = 0; count1 <= pix_value_bound; count1++) if(hist[count1] > max_num) max_num = hist[count1]; step1 = max_num/100; if(step1 == 0) step1 = 1; for(count1 = 0; count1 < 50; count1++) fprintf(fout,"="); fprintf(fout,"== HISTOGRAM =="); for(count1 = 0; count1 < 50; count1++) fprintf(fout,"="); fprintf(fout,"\n"); for(count1 = 0; count1 < 50; count1++) fprintf(fout,"*"); fprintf(fout," by Brandt Tso "); for(count1 = 0; count1 < 50; count1++) fprintf(fout,"*"); fprintf(fout,"\n"); fprintf(fout,"\n"); for(count1 = 0; count1 <= pix_value_bound; count1++) { buf = hist[count1]; step = buf/step1; step2= step; if(step2 > 130) step2 = 130; if(buf > 0) { fprintf(fout,"%4d %8d ",count1, hist[count1]); printf("pix[%d] = %8d",count1,hist[count1]); for(count2 = 0;count2