#define setbit(bitmap, n) (bitmap[n/8] |= (0x80 >> (n % 8))) #define clearbit(bitmap, n) (bitmap[n/8] &= (~(0x80 >> (n % 8)))) /*************************************************************************** * Func: dither * * * * Desc: dithers a pgm file, writes out a pbm file * * * * Params: image - pointer to image in memory * * rows - number of rows in the image * * cols - number of columns in the image * * matrix - dither matrix stored as a one-dimensional array * * m - height of dither matrix * * n - width of dither matrix * * filename - name of output file to create * * * * Returns: nothing * ***************************************************************************/ void dither(image_ptr image, int rows, int cols, unsigned char *matrix, int m, int n, char *filename) { FILE *fp; /* file pointer for output file */ unsigned long index; /* address of current input pixel */ int row_size; /* size of row in bytes */ unsigned long i, j; /* loop variable */ unsigned char line_buff[1024];/* storage for 1 line */ /* open new output file */ if((fp=fopen(filename, "wb")) == NULL) { printf("Unable to open %s for output\n",filename); exit(1); } /* print out the portable bitmap header */ fprintf(fp, "P4\n%d %d\n", cols, rows); /* calcute the number of bytes in each row */ row_size = cols/8 + (1 * ((cols % 8)) != 0) ; for(i=0; i (matrix[(i % m) * m + (j % n)])) clearbit(line_buff, j); /* this function can be swapped */ else setbit(line_buff, j); /* with this one if necessary */ } fwrite(line_buff, 1, row_size, fp); /* write out line */ } fclose(fp); }