/*********************************************************/ /* program 13: blue screen composite */ /*********************************************************/ #define XSIZE 640 #define YSIZE 480 #define LUTMIN 80 #define LUTMAX 150 unsigned char *fgr,*fgg,*fgb; unsigned char *bgr,*bgg,*bgb; unsigned char *or,*og,*ob; unsigned char *m; main() { long int x,y; long int i; float val,incr; unsigned char lut[256]; /* allocate input and output memory buffers */ fgr = (unsigned char *) malloc (XSIZE*YSIZE); fgg = (unsigned char *) malloc (XSIZE*YSIZE); fgb = (unsigned char *) malloc (XSIZE*YSIZE); bgr = (unsigned char *) malloc (XSIZE*YSIZE); bgg = (unsigned char *) malloc (XSIZE*YSIZE); bgb = (unsigned char *) malloc (XSIZE*YSIZE); or = (unsigned char *) malloc (XSIZE*YSIZE); og = (unsigned char *) malloc (XSIZE*YSIZE); ob = (unsigned char *) malloc (XSIZE*YSIZE); m = (unsigned char *) malloc (XSIZE*YSIZE); /* read the foreground and background images */ read_image("foreground",fgr,fgg,fgb); read_image("background",bgr,bgg,bgb); /* build the lut that will generate mask in step 3 */ for (i = 0; i < LUTMIN; i++) lut[i] = 255; incr = 255.0 / (float)(LUTMAX - LUTMIN); val = 255; for (i = LUTMIN; i < LUTMAX; i++) { lut[i] = (unsigned char)(val + 0.5); val = val - incr; } for (i = LUTMAX; i < 256; i++) lut[i] = 0; /* perform composite for all pixels in image */ for (y = 0; y < YSIZE; y = y++) { for (x = 0; x < XSIZE; x = x++) { /* step 1: r = -r */ m[(y*XSIZE)+x] = -fgr[(y*XSIZE)+x]; /* step 2: r = r * b / 256 */ m[(y*XSIZE)+x] = (unsigned char) ((long)m[(y*XSIZE)+x] * (long)fgb[(y*XSIZE)+x] >> 8); /* step 3: invert/contrast enhance r */ m[(y*XSIZE)+x] = lut[m[(y*XSIZE)+x]]; /* step 4: generate synthetic blue (min g,b) */ if (fgg[(y*XSIZE)+x] < fgb[(y*XSIZE)+x]) fgb[(y*XSIZE)+x] = fgg[(y*XSIZE)+x]; /* step 5: alpha filter */ bgr[(y*XSIZE)+x] = (unsigned char) ((((long)fgr[(y*XSIZE)+x] * (long)m[(y*XSIZE)+x]) + ((long)bgr[(y*XSIZE)+x] * (255-(long)m[(y*XSIZE)+x]))) >> 8); bgg[(y*XSIZE)+x] = (unsigned char) ((((long)fgg[(y*XSIZE)+x] * (long)m[(y*XSIZE)+x]) + ((long)bgg[(y*XSIZE)+x] * (255-(long)m[(y*XSIZE)+x]))) >> 8); bgb[(y*XSIZE)+x] = (unsigned char) ((((long)fgb[(y*XSIZE)+x] * (long)m[(y*XSIZE)+x]) + ((long)bgb[(y*XSIZE)+x] * (255-(long)m[(y*XSIZE)+x]))) >> 8); } } /* write the output image */ write_image("output",bgr,bgg,bgb); /* free memory buffers */ free (fgr); free (fgg); free (fgb); free (bgr); free (bgg); free (bgb); free (or); free (og); free (ob); free (m); }