/*********************************************************/ /* program 1: modify depth resolution */ /*********************************************************/ #include "math.h" #define DEPTH 3 #define XSIZE 640 #define YSIZE 480 unsigned char *ir,*ig,*ib; unsigned char *or,*og,*ob; main() { long int x, y; float scale; /* allocate input and output memory buffers */ ir = (unsigned char *) malloc (XSIZE*YSIZE); ig = (unsigned char *) malloc (XSIZE*YSIZE); ib = (unsigned char *) malloc (XSIZE*YSIZE); or = (unsigned char *) malloc (XSIZE*YSIZE); og = (unsigned char *) malloc (XSIZE*YSIZE); ob = (unsigned char *) malloc (XSIZE*YSIZE); /* read the input image (8-bit resolution) */ read_image("input",ir,ig,ib); /* calculate the scale factor that will reduce 8-bit values (0-255, inclusive) to evenly spaced values of 0-255 for the requested depth resolution. */ /* example: if DEPTH = 2, values are 0, 85, 171, and 255 */ scale = 255.0 / (powf (2.0, (float)DEPTH) - 1.0); /* reduce depth resolution by generating new 8-bit pixel values */ for (y = 0; y < YSIZE; y = y++) { for (x = 0; x < XSIZE; x = x++) { or[(y*XSIZE)+x] = (ir[(y*XSIZE)+x] >> (8 - DEPTH)) * scale; og[(y*XSIZE)+x] = (ig[(y*XSIZE)+x] >> (8 - DEPTH)) * scale; ob[(y*XSIZE)+x] = (ib[(y*XSIZE)+x] >> (8 - DEPTH)) * scale; } } /* write the output image */ write_image("output",or,og,ob); /* free memory buffers */ free (ir); free (ig); free (ib); free (or); free (og); free (ob); }