/*********************************************************/ /* program 5: histogram collection */ /*********************************************************/ #define MAX 256 #define XSIZE 640 #define YSIZE 480 unsigned char *ir,*ig,*ib; unsigned char *or,*og,*ob; main() { long int i,x,y; long int maxr, maxg, maxb; long int hr[MAX], hg[MAX], hb[MAX]; /* allocate input and histogram image memory buffers */ ir = (unsigned char *) malloc (XSIZE*YSIZE); ig = (unsigned char *) malloc (XSIZE*YSIZE); ib = (unsigned char *) malloc (XSIZE*YSIZE); or = (unsigned char *) malloc (MAX*MAX); og = (unsigned char *) malloc (MAX*MAX); ob = (unsigned char *) malloc (MAX*MAX); /* read the input image */ read_image("input",ir,ig,ib); /* be sure histogram collection bins start at zero */ for (i = 0; i < MAX; i++) { hr[i] = 0; hg[i] = 0; hb[i] = 0; } /* collect histogram data for each color plane */ for (y = 0; y < YSIZE; y++) { for (x = 0; x < XSIZE; x++) { hr[ir[(y*XSIZE)+x]]++; hg[ig[(y*XSIZE)+x]]++; hb[ib[(y*XSIZE)+x]]++; } } /* find maximum bin value of each color plane */ maxr = 0; maxg = 0; maxb = 0; for (i = 0; i < MAX; i++) { if (hr[i] > maxr) maxr = hr[i]; if (hg[i] > maxg) maxg = hg[i]; if (hb[i] > maxb) maxb = hb[i]; } /* normalize all bins to maximum */ for (x = 0; x < MAX; x++) { hr[x] = hr[x] * (MAX - 1) / maxr; hg[x] = hg[x] * (MAX - 1) / maxg; hb[x] = hb[x] * (MAX - 1) / maxb; } /* generate rgb histogram image */ for (x = 0; x < MAX; x++) { for (y = (MAX - 1); y >= 0; y--) { /* draw red line */ if (y > hr[x]) or[((MAX-y-1)*MAX)+x] = 0x00; else or[((MAX-y-1)*MAX)+x] = 0xff; /* draw green line */ if (y > hg[x]) og[((MAX-y-1)*MAX)+x] = 0x00; else og[((MAX-y-1)*MAX)+x] = 0xff; /* draw blue line */ if (y > hb[x]) ob[((MAX-y-1)*MAX)+x] = 0x00; else ob[((MAX-y-1)*MAX)+x] = 0xff; } } /* write the output image */ write_image("histo",or,og,ob); /* free memory buffers */ free (ir); free (ig); free (ib); free (or); free (og); free (ob); }