/*********************************************************/ /* program 23: erosion and dilation */ /*********************************************************/ #define XSIZE 640 #define YSIZE 480 main() { unsigned char *ip; unsigned char *op; unsigned char *i1,*i2,*i3; unsigned char *o1,*o2,*o3; long int x,y; /* erode mask: */ unsigned char m[3][3] = {0,0,0,0,0,0,0,0,0}; /* dilate mask: unsigned char m[3][3] = {255,255,255,255,255,255,255,255,255}; */ /* allocate input and output memory buffers */ ip = (unsigned char *) malloc (XSIZE*YSIZE); op = (unsigned char *) malloc (XSIZE*YSIZE); /* read the input image (one plane image) */ read_image("input",ip); /* initialize pointers into buffers */ i2 = ip; i3 = ip + XSIZE; o2 = op; o3 = op + XSIZE; /* process all lines of image */ for (y = 1; y < (YSIZE-1); y++) { /* set pointers for this line */ i1 = i2; i2 = i3; i3 = i3 + XSIZE; o1 = o2; o2 = o3; o3 = o3 + XSIZE; /* process all pixels in line */ for (x = 1; x < (XSIZE-1); x++) { /* check neighborhood */ if ((*(i1+x-1)==m[0][0]) && (*(i1+x+0)==m[0][1]) && (*(i1+x+1)==m[0][2]) && (*(i2+x-1)==m[1][0]) && (*(i2+x+0)==m[1][1]) && (*(i2+x+1)==m[1][2]) && (*(i3+x-1)==m[2][0]) && (*(i3+x+0)==m[2][1]) && (*(i3+x+1)==m[2][2])) { *(o2+x) = m[1][1]; } else { *(o2+x) = 255 - m[1][1]; } } } /* write the output image */ write_image("output",op); /* free memory buffers */ free(ip); free(op); }