/*********************************************************/ /* program 12: alpha filter composite */ /*********************************************************/ #define MAX 255 #define XSIZE 640 #define YSIZE 480 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 r,g,b,a; /* allocate input, output and matte 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, background, and matte images. */ read_image("hands",fgr,fgg,fgb); read_image("mountain",bgr,bgg,bgb); read_image_matte("hands_matte",m); /* Perform alpha filter for each pixel. */ for (y = 0; y < YSIZE; y = y++) { for (x = 0; x < XSIZE; x = x++) { /* Calculate alpha value (which is simply inverse of matte). */ a = MAX - (long)m[(y*XSIZE)+x]; /* Calculate composite rgb values. */ r = ((long)fgr[(y*XSIZE)+x] * a) + ((long)bgr[(y*XSIZE)+x] * (MAX - a)); g = ((long)fgg[(y*XSIZE)+x] * a) + ((long)bgg[(y*XSIZE)+x] * (MAX - a)); b = ((long)fgb[(y*XSIZE)+x] * a) + ((long)bgb[(y*XSIZE)+x] * (MAX - a)); /* Normalize composite to 8-bit values and save. */ or[(y*XSIZE)+x] = (unsigned char)(r / (MAX + 1)); og[(y*XSIZE)+x] = (unsigned char)(g / (MAX + 1)); ob[(y*XSIZE)+x] = (unsigned char)(b / (MAX + 1)); } } /* Write the composite output image. */ write_image("output",or,og,ob); /* Free memory buffers. */ free(fgr); free(fgg); free(fgb); free(bgr); free(bgg); free(bgb); free(or); free(og); free(ob); free(m); }