diff options
author | Robin Watts <Robin.Watts@artifex.com> | 2011-04-06 15:13:55 +0100 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2011-04-06 16:42:33 +0200 |
commit | 72c4f265ad5659cace51190002dfde2ad1040608 (patch) | |
tree | f38d6cc9cf43c7a211a070912c0654f4572ee27d | |
parent | 83bf72e448e26f5b9c77abf0e646ec75ffc04c98 (diff) | |
download | mupdf-72c4f265ad5659cace51190002dfde2ad1040608.tar.xz |
First cut at halftone support. Mono only, 16x16 default halftone.
Add fz_bitmaps (1bpc versions of pixmaps, really).
Add fz_halftones (arrays of fz_pixmaps used as threshold arrays).
Add simple halftoning code.
Add pdfdraw usage of the above (ask for a .pbm and you get a halftoned image).
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | apps/pdfdraw.c | 9 | ||||
-rw-r--r-- | fitz/fitz.h | 43 | ||||
-rw-r--r-- | fitz/res_bitmap.c | 76 | ||||
-rw-r--r-- | fitz/res_halftone.c | 203 | ||||
-rw-r--r-- | win32/libmupdf.vcproj | 8 |
6 files changed, 341 insertions, 0 deletions
@@ -93,8 +93,10 @@ FITZ_SRC := \ fitz/obj_dict.c \ fitz/obj_print.c \ fitz/obj_simple.c \ + fitz/res_bitmap.c \ fitz/res_colorspace.c \ fitz/res_font.c \ + fitz/res_halftone.c \ fitz/res_path.c \ fitz/res_pixmap.c \ fitz/res_shade.c \ diff --git a/apps/pdfdraw.c b/apps/pdfdraw.c index baaac92c..69a7d3aa 100644 --- a/apps/pdfdraw.c +++ b/apps/pdfdraw.c @@ -186,6 +186,13 @@ static void drawpage(pdf_xref *xref, int pagenum) fz_write_pam(pix, buf, savealpha); else if (strstr(output, ".png")) fz_write_png(pix, buf, savealpha); + else if (strstr(output, ".pbm")) { + fz_halftone *ht = fz_get_default_halftone(1); + fz_bitmap *bit = fz_halftone_pixmap(pix, ht); + fz_write_pbm(bit, buf); + fz_drop_bitmap(bit); + fz_drop_halftone(ht); + } } if (showmd5) @@ -327,6 +334,8 @@ int main(int argc, char **argv) colorspace = fz_device_gray; if (output && strstr(output, ".ppm")) colorspace = fz_device_rgb; + if (output && strstr(output, ".pbm")) + colorspace = fz_device_gray; timing.count = 0; timing.total = 0; diff --git a/fitz/fitz.h b/fitz/fitz.h index 6e53a271..749c8919 100644 --- a/fitz/fitz.h +++ b/fitz/fitz.h @@ -700,6 +700,49 @@ fz_error fz_write_png(fz_pixmap *pixmap, char *filename, int savealpha); fz_error fz_load_jpx_image(fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace *dcs); /* + * Bitmaps have 1 component per bit. Only used for creating halftoned versions + * of contone buffers, and saving out. Samples are stored msb first, akin to + * pbms. + */ + +typedef struct fz_bitmap_s fz_bitmap; + +struct fz_bitmap_s +{ + int refs; + int w, h, span, n; + unsigned char *samples; +}; + +fz_bitmap *fz_new_bitmap(int w, int h, int n); +fz_bitmap *fz_keep_bitmap(fz_bitmap *bit); +void fz_clear_bitmap(fz_bitmap *bit); +void fz_drop_bitmap(fz_bitmap *bit); + +fz_error fz_write_pbm(fz_bitmap *bitmap, char *filename); + +/* + * A halftone is a set of threshold tiles, one per component. Each threshold + * tile is a pixmap, possibly of varying sizes and phases. + */ + +typedef struct fz_halftone_s fz_halftone; + +struct fz_halftone_s +{ + int refs; + int n; + fz_pixmap *comp[1]; +}; + +fz_halftone *fz_new_halftone(int num_comps); +fz_halftone *fz_get_default_halftone(int num_comps); +fz_halftone *fz_keep_halftone(fz_halftone *half); +void fz_drop_halftone(fz_halftone *half); + +fz_bitmap *fz_halftone_pixmap(fz_pixmap *pix, fz_halftone *ht); + +/* * Colorspace resources. */ diff --git a/fitz/res_bitmap.c b/fitz/res_bitmap.c new file mode 100644 index 00000000..57784076 --- /dev/null +++ b/fitz/res_bitmap.c @@ -0,0 +1,76 @@ +#include "fitz.h" + +fz_bitmap * +fz_new_bitmap(int w, int h, int n) +{ + fz_bitmap *bit; + + bit = fz_malloc(sizeof(fz_bitmap)); + bit->refs = 1; + bit->w = w; + bit->h = h; + bit->n = n; + /* Span is 32 bit aligned. We may want to make this 64 bit if we + * use SSE2 etc. */ + bit->span = ((n*w+31)&~31)>>3; + + bit->samples = fz_calloc(h, bit->span); + + return bit; +} + +fz_bitmap * +fz_keep_bitmap(fz_bitmap *pix) +{ + pix->refs++; + return pix; +} + +void +fz_drop_bitmap(fz_bitmap *bit) +{ + if (bit && --bit->refs == 0) + { + fz_free(bit->samples); + fz_free(bit); + } +} + +void +fz_clear_bitmap(fz_bitmap *bit) +{ + memset(bit->samples, 0, bit->span * bit->h); +} + +/* + * Write bitmap to PBM file + */ + +fz_error +fz_write_pbm(fz_bitmap *bitmap, char *filename) +{ + FILE *fp; + unsigned char *p; + int h, bytespan; + + fp = fopen(filename, "wb"); + if (!fp) + return fz_throw("cannot open file '%s': %s", filename, strerror(errno)); + + assert(bitmap->n == 1); + + fprintf(fp, "P4\n%d %d\n", bitmap->w, bitmap->h); + + p = bitmap->samples; + + h = bitmap->h; + bytespan = (bitmap->w+7)>>3; + while (h--) + { + fwrite(p, 1, bytespan, fp); + p += bitmap->span; + } + + fclose(fp); + return fz_okay; +} diff --git a/fitz/res_halftone.c b/fitz/res_halftone.c new file mode 100644 index 00000000..180c9c0a --- /dev/null +++ b/fitz/res_halftone.c @@ -0,0 +1,203 @@ +#include "fitz.h" + +fz_halftone * +fz_new_halftone(int comps) +{ + fz_halftone *ht; + int i; + + ht = fz_malloc(sizeof(fz_halftone) + (comps-1)*sizeof(fz_pixmap *)); + ht->refs = 1; + ht->n = comps; + for (i=0; i < comps; i++) + ht->comp[i] = NULL; + + return ht; +} + +fz_halftone * +fz_keep_halftone(fz_halftone *ht) +{ + ht->refs++; + return ht; +} + +void +fz_drop_halftone(fz_halftone *ht) +{ + int i; + + if (!ht || --ht->refs != 0) + return; + for (i=0; i < ht->n; i++) + { + fz_drop_pixmap(ht->comp[i]); + } + fz_free(ht); +} + +/* Default mono halftone, lifted from Ghostscript. */ +static unsigned char mono_ht[] = +{ + 0x0E, 0x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75, 0xD5, 0x55, 0xFF, 0x7F, 0xDF, 0x5F, 0xF7, 0x77, 0xD7, 0x57, + 0x02, 0x82, 0x22, 0xA2, 0x0A, 0x8A, 0x2A, 0xAA, 0x00, 0x80, 0x20, 0xA0, 0x08, 0x88, 0x28, 0xA8, + 0xC2, 0x42, 0xE2, 0x62, 0xCA, 0x4A, 0xEA, 0x6A, 0xC0, 0x40, 0xE0, 0x60, 0xC8, 0x48, 0xE8, 0x68, + 0x32, 0xB2, 0x12, 0x92, 0x3A, 0xBA, 0x1A, 0x9A, 0x30, 0xB0, 0x10, 0x90, 0x38, 0xB8, 0x18, 0x98, + 0xF2, 0x72, 0xD2, 0x52, 0xFA, 0x7A, 0xDA, 0x5A, 0xF0, 0x70, 0xD0, 0x50, 0xF8, 0x78, 0xD8, 0x58 +}; + +fz_halftone *fz_get_default_halftone(int num_comps) +{ + fz_halftone *ht = fz_new_halftone(num_comps); + + if (ht == NULL) + return NULL; + + /* Only support 1 component for now */ + assert(num_comps == 1); + + ht->comp[0] = fz_new_pixmap_with_data(NULL, 0, 0, 16, 16, mono_ht); + if (ht->comp[0] == NULL) + { + fz_drop_halftone(ht); + return NULL; + } + return ht; +} + +/* Finally, code to actually perform halftoning. */ +static void make_ht_line(unsigned char *buf, fz_halftone *ht, int x, int y, int w) +{ + /* FIXME: There is a potential optimisation here; in the case where + * the LCM of the halftone tile widths is smaller than w, we could + * form just one 'LCM' run, then copy it repeatedly. + */ + int k, n; + n = ht->n; + for (k=0; k < n; k++) + { + fz_pixmap *tile = ht->comp[k]; + unsigned char *b = buf++; + unsigned char *t; + unsigned char *tbase; + int px = x + tile->x; + int py = y + tile->y; + int tw = tile->w; + int th = tile->h; + int w2 = w; + int len; + px = px % tw; + if (px < 0) + px += tw; + py = py % th; + if (py < 0) + py += th; + + assert(tile->n == 1); + + /* Left hand section; from x to tile width */ + tbase = tile->samples + py * tw; + t = tbase + px; + len = tw - px; + if (len > w2) + len = w2; + w2 -= len; + while (len--) + { + *b = *t++; + b += n; + } + + /* Centre section - complete copies */ + w2 -= tw; + while (w2 >= 0) + { + len = tw; + t = tbase; + while (len--) + { + *b = *t++; + b += n; + } + w2 -= tw; + } + w2 += tw; + + /* Right hand section - stragglers */ + t = tbase; + while (w2--) + { + *b = *t++; + b += n; + } + } +} + +/* Inner mono thresholding code */ +static void do_threshold_1(unsigned char *ht_line, unsigned char *pixmap, unsigned char *out, int w) +{ + int bit = 0x80; + int h = 0; + + do + { + if (*pixmap < *ht_line++) + h |= bit; + pixmap += 2; /* Skip the alpha */ + bit >>= 1; + if (bit == 0) + { + *out++ = h; + h = 0; + bit = 0x80; + } + + } + while (--w); + if (bit != 0x80) + *out++ = h; +} + +fz_bitmap *fz_halftone_pixmap(fz_pixmap *pix, fz_halftone *ht) +{ + fz_bitmap *out; + unsigned char *ht_line, *o, *p; + int w, h, x, y, n, pspan, ospan; + + if ((pix == NULL) || (ht == NULL)) + return NULL; + + assert(pix->n == 2); /* Mono + Alpha */ + + n = pix->n-1; /* Remove alpha */ + ht_line = fz_malloc(sizeof(unsigned char)*pix->w*n); + out = fz_new_bitmap(pix->w, pix->h, n); + o = out->samples; + p = pix->samples; + + h = pix->h; + x = pix->x; + y = pix->y; + w = pix->w; + ospan = out->span; + pspan = pix->w * pix->n; + while (h--) + { + make_ht_line(ht_line, ht, x, y++, w); + do_threshold_1(ht_line, p, o, w); + o += ospan; + p += pspan; + } + return out; +} diff --git a/win32/libmupdf.vcproj b/win32/libmupdf.vcproj index 0017a340..d58dc6e4 100644 --- a/win32/libmupdf.vcproj +++ b/win32/libmupdf.vcproj @@ -387,6 +387,10 @@ > </File> <File + RelativePath="..\fitz\res_bitmap.c" + > + </File> + <File RelativePath="..\fitz\res_colorspace.c" > </File> @@ -395,6 +399,10 @@ > </File> <File + RelativePath="..\fitz\res_halftone.c" + > + </File> + <File RelativePath="..\fitz\res_path.c" > </File> |