/*********************************************************/ /* program 14: generalized convolution */ /*********************************************************/ #define XSIZE 640 #define YSIZE 480 #define XMASK 3 #define YMASK 3 #define RED 0 #define GREEN 1 #define BLUE 2 main() { unsigned char *ir,*ig,*ib; unsigned char *or,*og,*ob; unsigned char *i,*i1,*i2,*i3; unsigned char *o,*o1; long int x,y,c; long int xm,ym; long int *m1; long int val; long int w; long int m[XMASK][YMASK] = {-1,-1,-1, -1, 9,-1, -1,-1,-1}; /* 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 */ read_image("input",ir,ig,ib); /* calculate weight of the mask */ w = 0; for (ym = 0; ym < YMASK; ym++) { for (xm = 0; xm < YMASK; xm++) { w = w + m[xm][ym]; } } /* process the red, green, and blue planes one at a time */ for (c = RED; c <= BLUE; c++) { /* initialize pointers for current color plane */ if (c == RED ) i = ir; if (c == GREEN) i = ig; if (c == BLUE ) i = ib; if (c == RED ) o = or; if (c == GREEN) o = og; if (c == BLUE ) o = ob; /* process all lines in the image (ignoring lines that would produce edge effects) */ for (y = (YMASK/2); y < (YSIZE-(YMASK/2)); y++) { i1 = i + (y*XSIZE) + (XMASK/2); o1 = o + (y*XSIZE) + (XMASK/2); /* process all pixels in the line (ignoring pixels that would produce edge effects) */ for (x = (XMASK/2); x < (XSIZE-(XMASK/2)); x++) { i2 = i1 - ((YMASK/2)*XSIZE) - (XMASK/2); m1 = &m[0][0]; val = 0; /* generate convolution equation numerator */ for (ym = 0; ym < YMASK; ym++) { i3 = i2 + (ym*XSIZE); for (xm = 0; xm < XMASK; xm++) { val = val + (((long int)*i3++ & 0xff) * *m1++); } } /* divide numerator by kernel weight and scale if needed */ if (w != 0) val = val / w; if (val > 255) val = 255; if (val < 0) val = 0; /* save convoluted output pixel */ *o1++ = (unsigned int)val; i1++; } } } /* 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); }