/*********************************************************/ /* program 8: halftone an image */ /*********************************************************/ #define HTONE 4 #define XSIZE 640 #define YSIZE 480 unsigned char *in; unsigned char *out; main() { long int x,y,h,hx,hy; char mask; unsigned char p; /* initialize halftone group patterns */ unsigned char htone[HTONE][HTONE*HTONE] = {0x0,0x0,0x0,0x0,0x0,0x4,0x4,0x4,0x4,0x4,0x6,0x6,0x6,0x7,0x7,0xf, 0x0,0x4,0x4,0x6,0x6,0x6,0x7,0x7,0x7,0xf,0x7,0xf,0xf,0xf,0xf,0xf, 0x0,0x0,0x2,0x2,0x6,0x6,0x6,0xe,0xf,0x7,0xf,0xf,0xf,0xf,0xf,0xf, 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x2,0x2,0x2,0x6,0x6,0xe,0xf}; /* allocate input and output memory buffers */ in = (unsigned char *)malloc(XSIZE*YSIZE); out= (unsigned char *)malloc(XSIZE*YSIZE*HTONE*HTONE); /* read the grey scale input image */ read_image_gs("input",in); /* halftone each pixel in the input image */ for (y = 0; y < YSIZE; y++) { for (x = 0; x < XSIZE; x++) { /* get the input pixel to halftone note: for odd lines, offset the pattern by 1/2 the group size */ if ((y % 2) == 0) h = (long)in[(y*XSIZE)+x] >> 4; else h = ((long)in[(y*XSIZE)+x] + (long)in[(y*XSIZE)+x+1]) >> 5; /* generate the halftone pattern for this input pixel */ for (hy = 0; hy < HTONE; hy++) { mask = htone[hy][h] << 4; for (hx = 0; hx < HTONE; hx++) { if ((mask & 0x80) != 0) p = 0xff; else p = 0x00; /* for odd lines, offset the pattern by 1/2 the group size */ if ((y % 2) == 0) out[(((y*HTONE)+hy)*XSIZE*HTONE)+(x*HTONE)+hx] = p; else out[(((y*HTONE)+hy)*XSIZE*HTONE)+(x*HTONE)+hx+(HTONE/2)] = p; mask = mask << 1; } } } } /* write the output image */ write_image_gs_ht("output",out,HTONE); /* free memory buffers */ free(in); free(out); }