/*********************************************************/ /* program 24: hit-and-miss LUT 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; unsigned char lut[512]; long int x,y; long int l; /* generate erode lut: */ for (x = 0; x < 512; x++) lut[x] = 0x00; lut[0x1ff] = 0xff; /* generate dilate lut: for (x = 0; x < 512; x++) lut[x] = 0xff; lut[0x000] = 0x00; */ /* 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++) { /* generate index into lut from binary pixels */ l = ((*(i2+x+0) & 0x01) << 8) + ((*(i2+x+1) & 0x01) << 7) + ((*(i1+x+1) & 0x01) << 6) + ((*(i1+x+0) & 0x01) << 5) + ((*(i1+x-1) & 0x01) << 4) + ((*(i2+x-1) & 0x01) << 3) + ((*(i3+x-1) & 0x01) << 2) + ((*(i3+x+0) & 0x01) << 1) + (*(i3+x+1) & 0x01); /* perform look-up table operation */ *(o2+x) = lut[l]; } } /* write the output image */ write_image("output",op); /* free memory buffers */ free(ip); free(op); }