diff options
Diffstat (limited to 'xps')
-rw-r--r-- | xps/muxps.h | 4 | ||||
-rw-r--r-- | xps/xps_image.c | 10 | ||||
-rw-r--r-- | xps/xps_jpeg.c | 140 | ||||
-rw-r--r-- | xps/xps_png.c | 565 | ||||
-rw-r--r-- | xps/xps_tiff.c | 817 |
5 files changed, 5 insertions, 1531 deletions
diff --git a/xps/muxps.h b/xps/muxps.h index e7bdfa41..fe0e5de1 100644 --- a/xps/muxps.h +++ b/xps/muxps.h @@ -116,10 +116,6 @@ int xps_find_link_target(xps_document *doc, char *target_uri); * Images, fonts, and colorspaces. */ -fz_pixmap *xps_decode_jpeg(fz_context *doc, byte *rbuf, int rlen); -fz_pixmap *xps_decode_png(fz_context *doc, byte *rbuf, int rlen); -fz_pixmap *xps_decode_tiff(fz_context *doc, byte *rbuf, int rlen); - typedef struct xps_font_cache_s xps_font_cache; struct xps_font_cache_s diff --git a/xps/xps_image.c b/xps/xps_image.c index 6ba514b7..2c159d14 100644 --- a/xps/xps_image.c +++ b/xps/xps_image.c @@ -2,7 +2,7 @@ #include "muxps.h" static fz_pixmap * -xps_decode_image(fz_context *ctx, byte *buf, int len) +xps_load_image(fz_context *ctx, byte *buf, int len) { fz_pixmap *image; @@ -10,13 +10,13 @@ xps_decode_image(fz_context *ctx, byte *buf, int len) fz_throw(ctx, "unknown image file format"); if (buf[0] == 0xff && buf[1] == 0xd8) - image = xps_decode_jpeg(ctx, buf, len); + image = fz_load_jpeg(ctx, buf, len); else if (memcmp(buf, "\211PNG\r\n\032\n", 8) == 0) - image = xps_decode_png(ctx, buf, len); + image = fz_load_png(ctx, buf, len); else if (memcmp(buf, "II", 2) == 0 && buf[2] == 0xBC) fz_throw(ctx, "JPEG-XR codec is not available"); else if (memcmp(buf, "MM", 2) == 0 || memcmp(buf, "II", 2) == 0) - image = xps_decode_tiff(ctx, buf, len); + image = fz_load_tiff(ctx, buf, len); else fz_throw(ctx, "unknown image file format"); @@ -107,7 +107,7 @@ xps_parse_image_brush(xps_document *doc, fz_matrix ctm, fz_rect area, fz_try(doc->ctx) { - image = xps_decode_image(doc->ctx, part->data, part->size); + image = xps_load_image(doc->ctx, part->data, part->size); } fz_catch(doc->ctx) { diff --git a/xps/xps_jpeg.c b/xps/xps_jpeg.c deleted file mode 100644 index 4e937a31..00000000 --- a/xps/xps_jpeg.c +++ /dev/null @@ -1,140 +0,0 @@ -#include "fitz.h" -#include "muxps.h" - -#include <jpeglib.h> -#include <setjmp.h> - -struct jpeg_error_mgr_jmp -{ - struct jpeg_error_mgr super; - jmp_buf env; - char msg[JMSG_LENGTH_MAX]; -}; - -static void error_exit(j_common_ptr cinfo) -{ - struct jpeg_error_mgr_jmp *err = (struct jpeg_error_mgr_jmp *)cinfo->err; - cinfo->err->format_message(cinfo, err->msg); - longjmp(err->env, 1); -} - -static void init_source(j_decompress_ptr cinfo) -{ - /* nothing to do */ -} - -static void term_source(j_decompress_ptr cinfo) -{ - /* nothing to do */ -} - -static boolean fill_input_buffer(j_decompress_ptr cinfo) -{ - static unsigned char eoi[2] = { 0xFF, JPEG_EOI }; - struct jpeg_source_mgr *src = cinfo->src; - src->next_input_byte = eoi; - src->bytes_in_buffer = 2; - return 1; -} - -static void skip_input_data(j_decompress_ptr cinfo, long num_bytes) -{ - struct jpeg_source_mgr *src = cinfo->src; - if (num_bytes > 0) - { - src->next_input_byte += num_bytes; - src->bytes_in_buffer -= num_bytes; - } -} - -fz_pixmap * -xps_decode_jpeg(fz_context *ctx, byte *rbuf, int rlen) -{ - struct jpeg_decompress_struct cinfo; - struct jpeg_error_mgr_jmp err; - struct jpeg_source_mgr src; - unsigned char *row[1], *sp, *dp; - fz_colorspace *colorspace; - unsigned int x; - int k; - - fz_pixmap *image = NULL; - - if (setjmp(err.env)) - { - if (image) - fz_drop_pixmap(ctx, image); - fz_throw(ctx, "jpeg error: %s", err.msg); - } - - cinfo.err = jpeg_std_error(&err.super); - err.super.error_exit = error_exit; - - jpeg_create_decompress(&cinfo); - - cinfo.src = &src; - src.init_source = init_source; - src.fill_input_buffer = fill_input_buffer; - src.skip_input_data = skip_input_data; - src.resync_to_restart = jpeg_resync_to_restart; - src.term_source = term_source; - src.next_input_byte = rbuf; - src.bytes_in_buffer = rlen; - - jpeg_read_header(&cinfo, 1); - - jpeg_start_decompress(&cinfo); - - if (cinfo.output_components == 1) - colorspace = fz_device_gray; - else if (cinfo.output_components == 3) - colorspace = fz_device_rgb; - else if (cinfo.output_components == 4) - colorspace = fz_device_cmyk; - else - fz_throw(ctx, "bad number of components in jpeg: %d", cinfo.output_components); - - fz_try(ctx) - { - image = fz_new_pixmap(ctx, colorspace, cinfo.output_width, cinfo.output_height); - } - fz_catch(ctx) - { - jpeg_finish_decompress(&cinfo); - jpeg_destroy_decompress(&cinfo); - fz_throw(ctx, "out of memory"); - } - - if (cinfo.density_unit == 1) - { - image->xres = cinfo.X_density; - image->yres = cinfo.Y_density; - } - else if (cinfo.density_unit == 2) - { - image->xres = cinfo.X_density * 254 / 100; - image->yres = cinfo.Y_density * 254 / 100; - } - - fz_clear_pixmap(image); - - row[0] = fz_malloc(ctx, cinfo.output_components * cinfo.output_width); - dp = image->samples; - while (cinfo.output_scanline < cinfo.output_height) - { - jpeg_read_scanlines(&cinfo, row, 1); - sp = row[0]; - for (x = 0; x < cinfo.output_width; x++) - { - for (k = 0; k < cinfo.output_components; k++) - *dp++ = *sp++; - *dp++ = 255; - } - } - fz_free(ctx, row[0]); - - jpeg_finish_decompress(&cinfo); - jpeg_destroy_decompress(&cinfo); - - return image; -} diff --git a/xps/xps_png.c b/xps/xps_png.c deleted file mode 100644 index f938c1bc..00000000 --- a/xps/xps_png.c +++ /dev/null @@ -1,565 +0,0 @@ -#include "fitz.h" -#include "muxps.h" - -#include <zlib.h> - -struct info -{ - fz_context *ctx; - int width, height, depth, n; - int interlace, indexed; - int size; - unsigned char *samples; - unsigned char palette[256*4]; - int transparency; - int trns[3]; - int xres, yres; -}; - -static inline int getint(unsigned char *p) -{ - return p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]; -} - -static inline int getcomp(unsigned char *line, int x, int bpc) -{ - switch (bpc) - { - case 1: return (line[x >> 3] >> ( 7 - (x & 7) ) ) & 1; - case 2: return (line[x >> 2] >> ( ( 3 - (x & 3) ) << 1 ) ) & 3; - case 4: return (line[x >> 1] >> ( ( 1 - (x & 1) ) << 2 ) ) & 15; - case 8: return line[x]; - case 16: return line[x << 1] << 8 | line[(x << 1) + 1]; - } - return 0; -} - -static inline void putcomp(unsigned char *line, int x, int bpc, int value) -{ - int maxval = (1 << bpc) - 1; - - switch (bpc) - { - case 1: line[x >> 3] &= ~(maxval << (7 - (x & 7))); break; - case 2: line[x >> 2] &= ~(maxval << ((3 - (x & 3)) << 1)); break; - case 4: line[x >> 1] &= ~(maxval << ((1 - (x & 1)) << 2)); break; - } - - switch (bpc) - { - case 1: line[x >> 3] |= value << (7 - (x & 7)); break; - case 2: line[x >> 2] |= value << ((3 - (x & 3)) << 1); break; - case 4: line[x >> 1] |= value << ((1 - (x & 1)) << 2); break; - case 8: line[x] = value; break; - case 16: line[x << 1] = value >> 8; line[(x << 1) + 1] = value & 0xFF; break; - } -} - -static const unsigned char png_signature[8] = -{ - 137, 80, 78, 71, 13, 10, 26, 10 -}; - -static void *zalloc(void *opaque, unsigned int items, unsigned int size) -{ - return fz_malloc_array(opaque, items, size); -} - -static void zfree(void *opaque, void *address) -{ - fz_free(opaque, address); -} - -static inline int paeth(int a, int b, int c) -{ - /* The definitions of ac and bc are correct, not a typo. */ - int ac = b - c, bc = a - c, abcc = ac + bc; - int pa = (ac < 0 ? -ac : ac); - int pb = (bc < 0 ? -bc : bc); - int pc = (abcc < 0 ? -abcc : abcc); - return pa <= pb && pa <= pc ? a : pb <= pc ? b : c; -} - -static void -png_predict(unsigned char *samples, int width, int height, int n, int depth) -{ - int stride = (width * n * depth + 7) / 8; - int bpp = (n * depth + 7) / 8; - int i, row; - - for (row = 0; row < height; row ++) - { - unsigned char *src = samples + (stride + 1) * row; - unsigned char *dst = samples + stride * row; - - unsigned char *a = dst; - unsigned char *b = dst - stride; - unsigned char *c = dst - stride; - - switch (*src++) - { - default: - case 0: /* None */ - for (i = 0; i < stride; i++) - *dst++ = *src++; - break; - - case 1: /* Sub */ - for (i = 0; i < bpp; i++) - *dst++ = *src++; - for (i = bpp; i < stride; i++) - *dst++ = *src++ + *a++; - break; - - case 2: /* Up */ - if (row == 0) - for (i = 0; i < stride; i++) - *dst++ = *src++; - else - for (i = 0; i < stride; i++) - *dst++ = *src++ + *b++; - break; - - case 3: /* Average */ - if (row == 0) - { - for (i = 0; i < bpp; i++) - *dst++ = *src++; - for (i = bpp; i < stride; i++) - *dst++ = *src++ + (*a++ >> 1); - } - else - { - for (i = 0; i < bpp; i++) - *dst++ = *src++ + (*b++ >> 1); - for (i = bpp; i < stride; i++) - *dst++ = *src++ + ((*b++ + *a++) >> 1); - } - break; - - case 4: /* Paeth */ - if (row == 0) - { - for (i = 0; i < bpp; i++) - *dst++ = *src++ + paeth(0, 0, 0); - for (i = bpp; i < stride; i++) - *dst++ = *src++ + paeth(*a++, 0, 0); - } - else - { - for (i = 0; i < bpp; i++) - *dst++ = *src++ + paeth(0, *b++, 0); - for (i = bpp; i < stride; i++) - *dst++ = *src++ + paeth(*a++, *b++, *c++); - } - break; - } - } -} - -static const int adam7_ix[7] = { 0, 4, 0, 2, 0, 1, 0 }; -static const int adam7_dx[7] = { 8, 8, 4, 4, 2, 2, 1 }; -static const int adam7_iy[7] = { 0, 0, 4, 0, 2, 0, 1 }; -static const int adam7_dy[7] = { 8, 8, 8, 4, 4, 2, 2 }; - -static void -png_deinterlace_passes(struct info *info, int *w, int *h, int *ofs) -{ - int p, bpp = info->depth * info->n; - ofs[0] = 0; - for (p = 0; p < 7; p++) - { - w[p] = (info->width + adam7_dx[p] - adam7_ix[p] - 1) / adam7_dx[p]; - h[p] = (info->height + adam7_dy[p] - adam7_iy[p] - 1) / adam7_dy[p]; - if (w[p] == 0) h[p] = 0; - if (h[p] == 0) w[p] = 0; - if (w[p] && h[p]) - ofs[p + 1] = ofs[p] + h[p] * (1 + (w[p] * bpp + 7) / 8); - else - ofs[p + 1] = ofs[p]; - } -} - -static void -png_deinterlace(struct info *info, int *passw, int *passh, int *passofs) -{ - int n = info->n; - int depth = info->depth; - int stride = (info->width * n * depth + 7) / 8; - unsigned char *output; - int p, x, y, k; - - output = fz_malloc_array(info->ctx, info->height, stride); - - for (p = 0; p < 7; p++) - { - unsigned char *sp = info->samples + passofs[p]; - int w = passw[p]; - int h = passh[p]; - - png_predict(sp, w, h, n, depth); - for (y = 0; y < h; y++) - { - for (x = 0; x < w; x++) - { - int outx = x * adam7_dx[p] + adam7_ix[p]; - int outy = y * adam7_dy[p] + adam7_iy[p]; - unsigned char *dp = output + outy * stride; - for (k = 0; k < n; k++) - { - int v = getcomp(sp, x * n + k, depth); - putcomp(dp, outx * n + k, depth, v); - } - } - sp += (w * depth * n + 7) / 8; - } - } - - fz_free(info->ctx, info->samples); - info->samples = output; -} - -static void -png_read_ihdr(struct info *info, unsigned char *p, int size) -{ - int color, compression, filter; - - if (size != 13) - fz_throw(info->ctx, "IHDR chunk is the wrong size"); - - info->width = getint(p + 0); - info->height = getint(p + 4); - info->depth = p[8]; - - color = p[9]; - compression = p[10]; - filter = p[11]; - info->interlace = p[12]; - - if (info->width <= 0) - fz_throw(info->ctx, "image width must be > 0"); - if (info->height <= 0) - fz_throw(info->ctx, "image height must be > 0"); - - if (info->depth != 1 && info->depth != 2 && info->depth != 4 && - info->depth != 8 && info->depth != 16) - fz_throw(info->ctx, "image bit depth must be one of 1, 2, 4, 8, 16"); - if (color == 2 && info->depth < 8) - fz_throw(info->ctx, "illegal bit depth for truecolor"); - if (color == 3 && info->depth > 8) - fz_throw(info->ctx, "illegal bit depth for indexed"); - if (color == 4 && info->depth < 8) - fz_throw(info->ctx, "illegal bit depth for grayscale with alpha"); - if (color == 6 && info->depth < 8) - fz_throw(info->ctx, "illegal bit depth for truecolor with alpha"); - - info->indexed = 0; - if (color == 0) /* gray */ - info->n = 1; - else if (color == 2) /* rgb */ - info->n = 3; - else if (color == 4) /* gray alpha */ - info->n = 2; - else if (color == 6) /* rgb alpha */ - info->n = 4; - else if (color == 3) /* indexed */ - { - info->indexed = 1; - info->n = 1; - } - else - fz_throw(info->ctx, "unknown color type"); - - if (compression != 0) - fz_throw(info->ctx, "unknown compression method"); - if (filter != 0) - fz_throw(info->ctx, "unknown filter method"); - if (info->interlace != 0 && info->interlace != 1) - fz_throw(info->ctx, "interlace method not supported"); -} - -static void -png_read_plte(struct info *info, unsigned char *p, int size) -{ - int n = size / 3; - int i; - - if (n > 256 || n > (1 << info->depth)) - fz_throw(info->ctx, "too many samples in palette"); - - for (i = 0; i < n; i++) - { - info->palette[i * 4] = p[i * 3]; - info->palette[i * 4 + 1] = p[i * 3 + 1]; - info->palette[i * 4 + 2] = p[i * 3 + 2]; - } -} - -static void -png_read_trns(struct info *info, unsigned char *p, int size) -{ - int i; - - info->transparency = 1; - - if (info->indexed) - { - if (size > 256 || size > (1 << info->depth)) - fz_throw(info->ctx, "too many samples in transparency table"); - for (i = 0; i < size; i++) - info->palette[i * 4 + 3] = p[i]; - } - else - { - if (size != info->n * 2) - fz_throw(info->ctx, "tRNS chunk is the wrong size"); - for (i = 0; i < info->n; i++) - info->trns[i] = (p[i * 2] << 8 | p[i * 2 + 1]) & ((1 << info->depth) - 1); - } -} - -static void -png_read_idat(struct info *info, unsigned char *p, int size, z_stream *stm) -{ - int code; - - stm->next_in = p; - stm->avail_in = size; - - code = inflate(stm, Z_SYNC_FLUSH); - if (code != Z_OK && code != Z_STREAM_END) - fz_throw(info->ctx, "zlib error: %s", stm->msg); - if (stm->avail_in != 0) - { - if (stm->avail_out == 0) - fz_throw(info->ctx, "ran out of output before input"); - fz_throw(info->ctx, "inflate did not consume buffer (%d remaining)", stm->avail_in); - } -} - -static void -png_read_phys(struct info *info, unsigned char *p, int size) -{ - if (size != 9) - fz_throw(info->ctx, "pHYs chunk is the wrong size"); - if (p[8] == 1) - { - info->xres = getint(p) * 254 / 10000; - info->yres = getint(p + 4) * 254 / 10000; - } -} - -static void -png_read_image(fz_context *ctx, struct info *info, unsigned char *p, int total) -{ - int passw[7], passh[7], passofs[8]; - int code, size; - z_stream stm; - - memset(info, 0, sizeof (struct info)); - info->ctx = ctx; - memset(info->palette, 255, sizeof(info->palette)); - info->xres = 96; - info->yres = 96; - - /* Read signature */ - - if (total < 8 + 12 || memcmp(p, png_signature, 8)) - fz_throw(info->ctx, "not a png image (wrong signature)"); - - p += 8; - total -= 8; - - /* Read IHDR chunk (must come first) */ - - size = getint(p); - - if (size + 12 > total) - fz_throw(info->ctx, "premature end of data in png image"); - - if (!memcmp(p + 4, "IHDR", 4)) - png_read_ihdr(info, p + 8, size); - else - fz_throw(info->ctx, "png file must start with IHDR chunk"); - - p += size + 12; - total -= size + 12; - - /* Prepare output buffer */ - - if (!info->interlace) - { - info->size = info->height * (1 + (info->width * info->n * info->depth + 7) / 8); - } - else - { - png_deinterlace_passes(info, passw, passh, passofs); - info->size = passofs[7]; - } - - info->samples = fz_malloc(ctx, info->size); - - stm.zalloc = zalloc; - stm.zfree = zfree; - stm.opaque = info->ctx; - - stm.next_out = info->samples; - stm.avail_out = info->size; - - code = inflateInit(&stm); - if (code != Z_OK) - { - fz_free(ctx, info->samples); - fz_throw(ctx, "zlib error: %s", stm.msg); - } - - fz_try(ctx) - { - /* Read remaining chunks until IEND */ - while (total > 8) - { - size = getint(p); - - if (size + 12 > total) - fz_throw(info->ctx, "premature end of data in png image"); - - if (!memcmp(p + 4, "PLTE", 4)) - png_read_plte(info, p + 8, size); - if (!memcmp(p + 4, "tRNS", 4)) - png_read_trns(info, p + 8, size); - if (!memcmp(p + 4, "pHYs", 4)) - png_read_phys(info, p + 8, size); - if (!memcmp(p + 4, "IDAT", 4)) - png_read_idat(info, p + 8, size, &stm); - if (!memcmp(p + 4, "IEND", 4)) - break; - - p += size + 12; - total -= size + 12; - } - } - fz_catch(ctx) - { - inflateEnd(&stm); - fz_free(ctx, info->samples); - fz_rethrow(ctx); - } - - code = inflateEnd(&stm); - if (code != Z_OK) - { - fz_free(ctx, info->samples); - fz_throw(info->ctx, "zlib error: %s", stm.msg); - } - - /* Apply prediction filter and deinterlacing */ - fz_try(ctx) - { - if (!info->interlace) - png_predict(info->samples, info->width, info->height, info->n, info->depth); - else - png_deinterlace(info, passw, passh, passofs); - } - fz_catch(ctx) - { - fz_free(ctx, info->samples); - fz_rethrow(ctx); - } -} - -static fz_pixmap * -png_expand_palette(fz_context *ctx, struct info *info, fz_pixmap *src) -{ - fz_pixmap *dst = fz_new_pixmap(ctx, fz_device_rgb, src->w, src->h); - unsigned char *sp = src->samples; - unsigned char *dp = dst->samples; - int x, y; - - dst->xres = src->xres; - dst->yres = src->yres; - - for (y = 0; y < info->height; y++) - { - for (x = 0; x < info->width; x++) - { - int v = *sp << 2; - *dp++ = info->palette[v]; - *dp++ = info->palette[v + 1]; - *dp++ = info->palette[v + 2]; - *dp++ = info->palette[v + 3]; - sp += 2; - } - } - - fz_drop_pixmap(info->ctx, src); - return dst; -} - -static void -png_mask_transparency(struct info *info, fz_pixmap *dst) -{ - int stride = (info->width * info->n * info->depth + 7) / 8; - int depth = info->depth; - int n = info->n; - int x, y, k, t; - - for (y = 0; y < info->height; y++) - { - unsigned char *sp = info->samples + y * stride; - unsigned char *dp = dst->samples + y * dst->w * dst->n; - for (x = 0; x < info->width; x++) - { - t = 1; - for (k = 0; k < n; k++) - if (getcomp(sp, x * n + k, depth) != info->trns[k]) - t = 0; - if (t) - dp[x * dst->n + dst->n - 1] = 0; - } - } -} - -fz_pixmap * -xps_decode_png(fz_context *ctx, byte *p, int total) -{ - fz_pixmap *image; - fz_colorspace *colorspace; - struct info png; - int stride; - - png_read_image(ctx, &png, p, total); - - if (png.n == 3 || png.n == 4) - colorspace = fz_device_rgb; - else - colorspace = fz_device_gray; - - stride = (png.width * png.n * png.depth + 7) / 8; - - fz_try(ctx) - { - image = fz_new_pixmap(ctx, colorspace, png.width, png.height); - } - fz_catch(ctx) - { - fz_free(png.ctx, png.samples); - fz_throw(ctx, "out of memory"); - } - - image->xres = png.xres; - image->yres = png.yres; - - fz_unpack_tile(image, png.samples, png.n, png.depth, stride, png.indexed); - - if (png.indexed) - image = png_expand_palette(ctx, &png, image); - else if (png.transparency) - png_mask_transparency(&png, image); - - if (png.transparency || png.n == 2 || png.n == 4) - fz_premultiply_pixmap(image); - - fz_free(png.ctx, png.samples); - - return image; -} diff --git a/xps/xps_tiff.c b/xps/xps_tiff.c deleted file mode 100644 index 934dcc24..00000000 --- a/xps/xps_tiff.c +++ /dev/null @@ -1,817 +0,0 @@ -#include "fitz.h" -#include "muxps.h" - -/* - * TIFF image loader. Should be enough to support TIFF files in XPS. - * Baseline TIFF 6.0 plus CMYK, LZW, Flate and JPEG support. - * Limited bit depths (1,2,4,8). - * Limited planar configurations (1=chunky). - * No tiles (easy fix if necessary). - * TODO: RGBPal images - */ - -struct tiff -{ - fz_context *ctx; - - /* "file" */ - byte *bp, *rp, *ep; - - /* byte order */ - unsigned order; - - /* where we can find the strips of image data */ - unsigned rowsperstrip; - unsigned *stripoffsets; - unsigned *stripbytecounts; - - /* colormap */ - unsigned *colormap; - - /* assorted tags */ - unsigned subfiletype; - unsigned photometric; - unsigned compression; - unsigned imagewidth; - unsigned imagelength; - unsigned samplesperpixel; - unsigned bitspersample; - unsigned planar; - unsigned extrasamples; - unsigned xresolution; - unsigned yresolution; - unsigned resolutionunit; - unsigned fillorder; - unsigned g3opts; - unsigned g4opts; - unsigned predictor; - - unsigned ycbcrsubsamp[2]; - - byte *jpegtables; /* point into "file" buffer */ - unsigned jpegtableslen; - - byte *profile; - int profilesize; - - /* decoded data */ - fz_colorspace *colorspace; - byte *samples; - int stride; -}; - -enum -{ - TII = 0x4949, /* 'II' */ - TMM = 0x4d4d, /* 'MM' */ - TBYTE = 1, - TASCII = 2, - TSHORT = 3, - TLONG = 4, - TRATIONAL = 5 -}; - -#define NewSubfileType 254 -#define ImageWidth 256 -#define ImageLength 257 -#define BitsPerSample 258 -#define Compression 259 -#define PhotometricInterpretation 262 -#define FillOrder 266 -#define StripOffsets 273 -#define SamplesPerPixel 277 -#define RowsPerStrip 278 -#define StripByteCounts 279 -#define XResolution 282 -#define YResolution 283 -#define PlanarConfiguration 284 -#define T4Options 292 -#define T6Options 293 -#define ResolutionUnit 296 -#define Predictor 317 -#define ColorMap 320 -#define TileWidth 322 -#define TileLength 323 -#define TileOffsets 324 -#define TileByteCounts 325 -#define ExtraSamples 338 -#define JPEGTables 347 -#define YCbCrSubSampling 520 -#define ICCProfile 34675 - -static const byte bitrev[256] = -{ - 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, - 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, - 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, - 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, - 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, - 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, - 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, - 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, - 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, - 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, - 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, - 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, - 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, - 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, - 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, - 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, - 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, - 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, - 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, - 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, - 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, - 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, - 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, - 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, - 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, - 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, - 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, - 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, - 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, - 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, - 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, - 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff -}; - -static void -xps_decode_tiff_uncompressed(struct tiff *tiff, fz_stream *stm, byte *wp, int wlen) -{ - fz_read(stm, wp, wlen); - fz_close(stm); -} - -static void -xps_decode_tiff_packbits(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen) -{ - fz_stream *stm = fz_open_rld(chain); - fz_read(stm, wp, wlen); - fz_close(stm); -} - -static void -xps_decode_tiff_lzw(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen) -{ - fz_stream *stm = fz_open_lzwd(chain, 1); - fz_read(stm, wp, wlen); - fz_close(stm); -} - -static void -xps_decode_tiff_flate(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen) -{ - fz_stream *stm = fz_open_flated(chain); - fz_read(stm, wp, wlen); - fz_close(stm); -} - -static void -xps_decode_tiff_fax(struct tiff *tiff, int comp, fz_stream *chain, byte *wp, int wlen) -{ - fz_stream *stm; - int black_is_1 = tiff->photometric == 0; - int k = comp == 4 ? -1 : 0; - int encoded_byte_align = comp == 2; - stm = fz_open_faxd(chain, - k, 0, encoded_byte_align, - tiff->imagewidth, tiff->imagelength, 0, black_is_1); - fz_read(stm, wp, wlen); - fz_close(stm); -} - -static void -xps_decode_tiff_jpeg(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen) -{ - fz_stream *stm = fz_open_dctd(chain, -1); - fz_read(stm, wp, wlen); - fz_close(stm); -} - -static inline int getcomp(byte *line, int x, int bpc) -{ - switch (bpc) - { - case 1: return (line[x >> 3] >> ( 7 - (x & 7) ) ) & 1; - case 2: return (line[x >> 2] >> ( ( 3 - (x & 3) ) << 1 ) ) & 3; - case 4: return (line[x >> 1] >> ( ( 1 - (x & 1) ) << 2 ) ) & 15; - case 8: return line[x]; - case 16: return line[x << 1] << 8 | line[(x << 1) + 1]; - } - return 0; -} - -static inline void putcomp(byte *line, int x, int bpc, int value) -{ - int maxval = (1 << bpc) - 1; - - switch (bpc) - { - case 1: line[x >> 3] &= ~(maxval << (7 - (x & 7))); break; - case 2: line[x >> 2] &= ~(maxval << ((3 - (x & 3)) << 1)); break; - case 4: line[x >> 1] &= ~(maxval << ((1 - (x & 1)) << 2)); break; - } - - switch (bpc) - { - case 1: line[x >> 3] |= value << (7 - (x & 7)); break; - case 2: line[x >> 2] |= value << ((3 - (x & 3)) << 1); break; - case 4: line[x >> 1] |= value << ((1 - (x & 1)) << 2); break; - case 8: line[x] = value; break; - case 16: line[x << 1] = value >> 8; line[(x << 1) + 1] = value & 0xFF; break; - } -} - -static void -xps_unpredict_tiff(byte *line, int width, int comps, int bits) -{ - byte left[32]; - int i, k, v; - - for (k = 0; k < comps; k++) - left[k] = 0; - - for (i = 0; i < width; i++) - { - for (k = 0; k < comps; k++) - { - v = getcomp(line, i * comps + k, bits); - v = v + left[k]; - v = v % (1 << bits); - putcomp(line, i * comps + k, bits, v); - left[k] = v; - } - } -} - -static void -xps_invert_tiff(byte *line, int width, int comps, int bits, int alpha) -{ - int i, k, v; - int m = (1 << bits) - 1; - - for (i = 0; i < width; i++) - { - for (k = 0; k < comps; k++) - { - v = getcomp(line, i * comps + k, bits); - if (!alpha || k < comps - 1) - v = m - v; - putcomp(line, i * comps + k, bits, v); - } - } -} - -static void -xps_expand_tiff_colormap(struct tiff *tiff) -{ - int maxval = 1 << tiff->bitspersample; - byte *samples; - byte *src, *dst; - unsigned int x, y; - unsigned int stride; - - /* colormap has first all red, then all green, then all blue values */ - /* colormap values are 0..65535, bits is 4 or 8 */ - /* image can be with or without extrasamples: comps is 1 or 2 */ - - if (tiff->samplesperpixel != 1 && tiff->samplesperpixel != 2) - fz_throw(tiff->ctx, "invalid number of samples for RGBPal"); - - if (tiff->bitspersample != 4 && tiff->bitspersample != 8) - fz_throw(tiff->ctx, "invalid number of bits for RGBPal"); - - stride = tiff->imagewidth * (tiff->samplesperpixel + 2); - - samples = fz_malloc(tiff->ctx, stride * tiff->imagelength); - - for (y = 0; y < tiff->imagelength; y++) - { - src = tiff->samples + (tiff->stride * y); - dst = samples + (stride * y); - - for (x = 0; x < tiff->imagewidth; x++) - { - if (tiff->extrasamples) - { - int c = getcomp(src, x * 2, tiff->bitspersample); - int a = getcomp(src, x * 2 + 1, tiff->bitspersample); - *dst++ = tiff->colormap[c + 0] >> 8; - *dst++ = tiff->colormap[c + maxval] >> 8; - *dst++ = tiff->colormap[c + maxval * 2] >> 8; - *dst++ = a << (8 - tiff->bitspersample); - } - else - { - int c = getcomp(src, x, tiff->bitspersample); - *dst++ = tiff->colormap[c + 0] >> 8; - *dst++ = tiff->colormap[c + maxval] >> 8; - *dst++ = tiff->colormap[c + maxval * 2] >> 8; - } - } - } - - tiff->samplesperpixel += 2; - tiff->bitspersample = 8; - tiff->stride = stride; - tiff->samples = samples; -} - -static void -xps_decode_tiff_strips(struct tiff *tiff) -{ - fz_stream *stm; - - /* switch on compression to create a filter */ - /* feed each strip to the filter */ - /* read out the data and pack the samples into an xps_image */ - - /* type 32773 / packbits -- nothing special (same row-padding as PDF) */ - /* type 2 / ccitt rle -- no EOL, no RTC, rows are byte-aligned */ - /* type 3 and 4 / g3 and g4 -- each strip starts new section */ - /* type 5 / lzw -- each strip is handled separately */ - - byte *wp; - unsigned row; - unsigned strip; - unsigned i; - - if (!tiff->rowsperstrip || !tiff->stripoffsets || !tiff->rowsperstrip) - fz_throw(tiff->ctx, "no image data in tiff; maybe it is tiled"); - - if (tiff->planar != 1) - fz_throw(tiff->ctx, "image data is not in chunky format"); - - tiff->stride = (tiff->imagewidth * tiff->samplesperpixel * tiff->bitspersample + 7) / 8; - - switch (tiff->photometric) - { - case 0: /* WhiteIsZero -- inverted */ - tiff->colorspace = fz_device_gray; - break; - case 1: /* BlackIsZero */ - tiff->colorspace = fz_device_gray; - break; - case 2: /* RGB */ - tiff->colorspace = fz_device_rgb; - break; - case 3: /* RGBPal */ - tiff->colorspace = fz_device_rgb; - break; - case 5: /* CMYK */ - tiff->colorspace = fz_device_cmyk; - break; - case 6: /* YCbCr */ - /* it's probably a jpeg ... we let jpeg convert to rgb */ - tiff->colorspace = fz_device_rgb; - break; - default: - fz_throw(tiff->ctx, "unknown photometric: %d", tiff->photometric); - } - - switch (tiff->resolutionunit) - { - case 2: - /* no unit conversion needed */ - break; - case 3: - tiff->xresolution = tiff->xresolution * 254 / 100; - tiff->yresolution = tiff->yresolution * 254 / 100; - break; - default: - tiff->xresolution = 96; - tiff->yresolution = 96; - break; - } - - /* Note xres and yres could be 0 even if unit was set. If so default to 96dpi. */ - if (tiff->xresolution == 0 || tiff->yresolution == 0) - { - tiff->xresolution = 96; - tiff->yresolution = 96; - } - - tiff->samples = fz_malloc_array(tiff->ctx, tiff->imagelength, tiff->stride); - memset(tiff->samples, 0x55, tiff->imagelength * tiff->stride); - wp = tiff->samples; - - strip = 0; - for (row = 0; row < tiff->imagelength; row += tiff->rowsperstrip) - { - unsigned offset = tiff->stripoffsets[strip]; - unsigned rlen = tiff->stripbytecounts[strip]; - unsigned wlen = tiff->stride * tiff->rowsperstrip; - byte *rp = tiff->bp + offset; - - if (wp + wlen > tiff->samples + tiff->stride * tiff->imagelength) - wlen = tiff->samples + tiff->stride * tiff->imagelength - wp; - - if (rp + rlen > tiff->ep) - fz_throw(tiff->ctx, "strip extends beyond the end of the file"); - - /* the bits are in un-natural order */ - if (tiff->fillorder == 2) - for (i = 0; i < rlen; i++) - rp[i] = bitrev[rp[i]]; - - /* the strip decoders will close this */ - stm = fz_open_memory(tiff->ctx, rp, rlen); - - switch (tiff->compression) - { - case 1: - xps_decode_tiff_uncompressed(tiff, stm, wp, wlen); - break; - case 2: - xps_decode_tiff_fax(tiff, 2, stm, wp, wlen); - break; - case 3: - xps_decode_tiff_fax(tiff, 3, stm, wp, wlen); - break; - case 4: - xps_decode_tiff_fax(tiff, 4, stm, wp, wlen); - break; - case 5: - xps_decode_tiff_lzw(tiff, stm, wp, wlen); - break; - case 6: - fz_throw(tiff->ctx, "deprecated JPEG in TIFF compression not supported"); - break; - case 7: - xps_decode_tiff_jpeg(tiff, stm, wp, wlen); - break; - case 8: - xps_decode_tiff_flate(tiff, stm, wp, wlen); - break; - case 32773: - xps_decode_tiff_packbits(tiff, stm, wp, wlen); - break; - default: - fz_throw(tiff->ctx, "unknown TIFF compression: %d", tiff->compression); - } - - /* scramble the bits back into original order */ - if (tiff->fillorder == 2) - for (i = 0; i < rlen; i++) - rp[i] = bitrev[rp[i]]; - - wp += tiff->stride * tiff->rowsperstrip; - strip ++; - } - - /* Predictor (only for LZW and Flate) */ - if ((tiff->compression == 5 || tiff->compression == 8) && tiff->predictor == 2) - { - byte *p = tiff->samples; - for (i = 0; i < tiff->imagelength; i++) - { - xps_unpredict_tiff(p, tiff->imagewidth, tiff->samplesperpixel, tiff->bitspersample); - p += tiff->stride; - } - } - - /* RGBPal */ - if (tiff->photometric == 3 && tiff->colormap) - xps_expand_tiff_colormap(tiff); - - /* WhiteIsZero .. invert */ - if (tiff->photometric == 0) - { - byte *p = tiff->samples; - for (i = 0; i < tiff->imagelength; i++) - { - xps_invert_tiff(p, tiff->imagewidth, tiff->samplesperpixel, tiff->bitspersample, tiff->extrasamples); - p += tiff->stride; - } - } - - /* Premultiplied transparency */ - if (tiff->extrasamples == 1) - { - /* In GhostXPS we undo the premultiplication here; muxps holds - * all our images premultiplied by default, so nothing to do. - */ - } - - /* Non-premultiplied transparency */ - if (tiff->extrasamples == 2) - { - /* Premultiplied files are corrected for elsewhere */ - } -} - -static inline int readbyte(struct tiff *tiff) -{ - if (tiff->rp < tiff->ep) - return *tiff->rp++; - return EOF; -} - -static inline unsigned readshort(struct tiff *tiff) -{ - unsigned a = readbyte(tiff); - unsigned b = readbyte(tiff); - if (tiff->order == TII) - return (b << 8) | a; - return (a << 8) | b; -} - -static inline unsigned readlong(struct tiff *tiff) -{ - unsigned a = readbyte(tiff); - unsigned b = readbyte(tiff); - unsigned c = readbyte(tiff); - unsigned d = readbyte(tiff); - if (tiff->order == TII) - return (d << 24) | (c << 16) | (b << 8) | a; - return (a << 24) | (b << 16) | (c << 8) | d; -} - -static void -xps_read_tiff_bytes(unsigned char *p, struct tiff *tiff, unsigned ofs, unsigned n) -{ - tiff->rp = tiff->bp + ofs; - if (tiff->rp > tiff->ep) - tiff->rp = tiff->bp; - - while (n--) - *p++ = readbyte(tiff); -} - -static void -xps_read_tiff_tag_value(unsigned *p, struct tiff *tiff, unsigned type, unsigned ofs, unsigned n) -{ - tiff->rp = tiff->bp + ofs; - if (tiff->rp > tiff->ep) - tiff->rp = tiff->bp; - - while (n--) - { - switch (type) - { - case TRATIONAL: - *p = readlong(tiff); - *p = *p / readlong(tiff); - p ++; - break; - case TBYTE: *p++ = readbyte(tiff); break; - case TSHORT: *p++ = readshort(tiff); break; - case TLONG: *p++ = readlong(tiff); break; - default: *p++ = 0; break; - } - } -} - -static void -xps_read_tiff_tag(struct tiff *tiff, unsigned offset) -{ - unsigned tag; - unsigned type; - unsigned count; - unsigned value; - - tiff->rp = tiff->bp + offset; - - tag = readshort(tiff); - type = readshort(tiff); - count = readlong(tiff); - - if ((type == TBYTE && count <= 4) || - (type == TSHORT && count <= 2) || - (type == TLONG && count <= 1)) - value = tiff->rp - tiff->bp; - else - value = readlong(tiff); - - switch (tag) - { - case NewSubfileType: - xps_read_tiff_tag_value(&tiff->subfiletype, tiff, type, value, 1); - break; - case ImageWidth: - xps_read_tiff_tag_value(&tiff->imagewidth, tiff, type, value, 1); - break; - case ImageLength: - xps_read_tiff_tag_value(&tiff->imagelength, tiff, type, value, 1); - break; - case BitsPerSample: - xps_read_tiff_tag_value(&tiff->bitspersample, tiff, type, value, 1); - break; - case Compression: - xps_read_tiff_tag_value(&tiff->compression, tiff, type, value, 1); - break; - case PhotometricInterpretation: - xps_read_tiff_tag_value(&tiff->photometric, tiff, type, value, 1); - break; - case FillOrder: - xps_read_tiff_tag_value(&tiff->fillorder, tiff, type, value, 1); - break; - case SamplesPerPixel: - xps_read_tiff_tag_value(&tiff->samplesperpixel, tiff, type, value, 1); - break; - case RowsPerStrip: - xps_read_tiff_tag_value(&tiff->rowsperstrip, tiff, type, value, 1); - break; - case XResolution: - xps_read_tiff_tag_value(&tiff->xresolution, tiff, type, value, 1); - break; - case YResolution: - xps_read_tiff_tag_value(&tiff->yresolution, tiff, type, value, 1); - break; - case PlanarConfiguration: - xps_read_tiff_tag_value(&tiff->planar, tiff, type, value, 1); - break; - case T4Options: - xps_read_tiff_tag_value(&tiff->g3opts, tiff, type, value, 1); - break; - case T6Options: - xps_read_tiff_tag_value(&tiff->g4opts, tiff, type, value, 1); - break; - case Predictor: - xps_read_tiff_tag_value(&tiff->predictor, tiff, type, value, 1); - break; - case ResolutionUnit: - xps_read_tiff_tag_value(&tiff->resolutionunit, tiff, type, value, 1); - break; - case YCbCrSubSampling: - xps_read_tiff_tag_value(tiff->ycbcrsubsamp, tiff, type, value, 2); - break; - case ExtraSamples: - xps_read_tiff_tag_value(&tiff->extrasamples, tiff, type, value, 1); - break; - - case ICCProfile: - tiff->profile = fz_malloc(tiff->ctx, count); - /* ICC profile data type is set to UNDEFINED. - * TBYTE reading not correct in xps_read_tiff_tag_value */ - xps_read_tiff_bytes(tiff->profile, tiff, value, count); - tiff->profilesize = count; - break; - - case JPEGTables: - fz_warn(tiff->ctx, "jpeg tables in tiff not implemented"); - tiff->jpegtables = tiff->bp + value; - tiff->jpegtableslen = count; - break; - - case StripOffsets: - tiff->stripoffsets = fz_malloc_array(tiff->ctx, count, sizeof(unsigned)); - xps_read_tiff_tag_value(tiff->stripoffsets, tiff, type, value, count); - break; - - case StripByteCounts: - tiff->stripbytecounts = fz_malloc_array(tiff->ctx, count, sizeof(unsigned)); - xps_read_tiff_tag_value(tiff->stripbytecounts, tiff, type, value, count); - break; - - case ColorMap: - tiff->colormap = fz_malloc_array(tiff->ctx, count, sizeof(unsigned)); - xps_read_tiff_tag_value(tiff->colormap, tiff, type, value, count); - break; - - case TileWidth: - case TileLength: - case TileOffsets: - case TileByteCounts: - fz_throw(tiff->ctx, "tiled tiffs not supported"); - - default: - /* printf("unknown tag: %d t=%d n=%d\n", tag, type, count); */ - break; - } -} - -static void -xps_swap_byte_order(byte *buf, int n) -{ - int i, t; - for (i = 0; i < n; i++) - { - t = buf[i * 2 + 0]; - buf[i * 2 + 0] = buf[i * 2 + 1]; - buf[i * 2 + 1] = t; - } -} - -static void -xps_decode_tiff_header(fz_context *ctx, struct tiff *tiff, byte *buf, int len) -{ - unsigned version; - unsigned offset; - unsigned count; - unsigned i; - - memset(tiff, 0, sizeof(struct tiff)); - tiff->ctx = ctx; - tiff->bp = buf; - tiff->rp = buf; - tiff->ep = buf + len; - - /* tag defaults, where applicable */ - tiff->bitspersample = 1; - tiff->compression = 1; - tiff->samplesperpixel = 1; - tiff->resolutionunit = 2; - tiff->rowsperstrip = 0xFFFFFFFF; - tiff->fillorder = 1; - tiff->planar = 1; - tiff->subfiletype = 0; - tiff->predictor = 1; - tiff->ycbcrsubsamp[0] = 2; - tiff->ycbcrsubsamp[1] = 2; - - /* - * Read IFH - */ - - /* get byte order marker */ - tiff->order = TII; - tiff->order = readshort(tiff); - if (tiff->order != TII && tiff->order != TMM) - fz_throw(tiff->ctx, "not a TIFF file, wrong magic marker"); - - /* check version */ - version = readshort(tiff); - if (version != 42) - fz_throw(tiff->ctx, "not a TIFF file, wrong version marker"); - - /* get offset of IFD */ - offset = readlong(tiff); - - /* - * Read IFD - */ - - tiff->rp = tiff->bp + offset; - - count = readshort(tiff); - - offset += 2; - for (i = 0; i < count; i++) - { - xps_read_tiff_tag(tiff, offset); - offset += 12; - } -} - -fz_pixmap * -xps_decode_tiff(fz_context *ctx, byte *buf, int len) -{ - fz_pixmap *image; - struct tiff tiff; - - fz_try(ctx) - { - xps_decode_tiff_header(ctx, &tiff, buf, len); - - /* Decode the image strips */ - - if (tiff.rowsperstrip > tiff.imagelength) - tiff.rowsperstrip = tiff.imagelength; - - xps_decode_tiff_strips(&tiff); - - /* Byte swap 16-bit images to big endian if necessary */ - if (tiff.bitspersample == 16) - if (tiff.order == TII) - xps_swap_byte_order(tiff.samples, tiff.imagewidth * tiff.imagelength * tiff.samplesperpixel); - - /* Expand into fz_pixmap struct */ - image = fz_new_pixmap(tiff.ctx, tiff.colorspace, tiff.imagewidth, tiff.imagelength); - image->xres = tiff.xresolution; - image->yres = tiff.yresolution; - - fz_unpack_tile(image, tiff.samples, tiff.samplesperpixel, tiff.bitspersample, tiff.stride, 0); - - /* We should only do this on non-pre-multiplied images, but files in the wild are bad */ - if (tiff.extrasamples /* == 2 */) - { - /* CMYK is a subtractive colorspace, we want additive for premul alpha */ - if (image->n == 5) - { - fz_pixmap *rgb = fz_new_pixmap(tiff.ctx, fz_device_rgb, image->w, image->h); - fz_convert_pixmap(tiff.ctx, image, rgb); - rgb->xres = image->xres; - rgb->yres = image->yres; - fz_drop_pixmap(ctx, image); - image = rgb; - } - fz_premultiply_pixmap(image); - } - } - fz_always(ctx) - { - /* Clean up scratch memory */ - if (tiff.colormap) fz_free(ctx, tiff.colormap); - if (tiff.stripoffsets) fz_free(ctx, tiff.stripoffsets); - if (tiff.stripbytecounts) fz_free(ctx, tiff.stripbytecounts); - if (tiff.samples) fz_free(ctx, tiff.samples); - if (tiff.profile) fz_free(ctx, tiff.profile); - } - fz_catch(ctx) - { - fz_throw(ctx, "out of memory"); - } - - return image; -} |