summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fitz/fitz.h1
-rw-r--r--fitz/res_pixmap.c2
-rw-r--r--xps/muxps.h20
-rw-r--r--xps/xps_image.c35
-rw-r--r--xps/xps_jpeg.c15
-rw-r--r--xps/xps_png.c32
-rw-r--r--xps/xps_tiff.c80
7 files changed, 75 insertions, 110 deletions
diff --git a/fitz/fitz.h b/fitz/fitz.h
index d4ab8ff4..39542c0d 100644
--- a/fitz/fitz.h
+++ b/fitz/fitz.h
@@ -673,6 +673,7 @@ struct fz_pixmap_s
int x, y, w, h, n;
fz_pixmap *mask; /* explicit soft/image mask */
int interpolate;
+ int xres, yres;
fz_colorspace *colorspace;
unsigned char *samples;
int freesamples;
diff --git a/fitz/res_pixmap.c b/fitz/res_pixmap.c
index 93fdb307..9f336ab1 100644
--- a/fitz/res_pixmap.c
+++ b/fitz/res_pixmap.c
@@ -13,6 +13,8 @@ fz_newpixmapwithdata(fz_colorspace *colorspace, int x, int y, int w, int h, unsi
pix->h = h;
pix->mask = nil;
pix->interpolate = 1;
+ pix->xres = 96;
+ pix->yres = 96;
pix->colorspace = nil;
pix->n = 1;
diff --git a/xps/muxps.h b/xps/muxps.h
index f3f2cdf4..067d4780 100644
--- a/xps/muxps.h
+++ b/xps/muxps.h
@@ -112,23 +112,9 @@ void xps_free_page(xps_context *ctx, xps_page *page);
* Images.
*/
-typedef struct xps_image xps_image;
-
-/* type for the information derived directly from the raster file format */
-
-struct xps_image
-{
- fz_pixmap *pixmap;
- int xres;
- int yres;
-};
-
-int xps_decode_jpeg(xps_image **imagep, xps_context *ctx, byte *rbuf, int rlen);
-int xps_decode_png(xps_image **imagep, xps_context *ctx, byte *rbuf, int rlen);
-int xps_decode_tiff(xps_image **imagep, xps_context *ctx, byte *rbuf, int rlen);
-int xps_decode_jpegxr(xps_image **imagep, xps_context *ctx, byte *rbuf, int rlen);
-
-void xps_free_image(xps_context *ctx, xps_image *image);
+int xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen);
+int xps_decode_png(fz_pixmap **imagep, byte *rbuf, int rlen);
+int xps_decode_tiff(fz_pixmap **imagep, byte *rbuf, int rlen);
/*
* Fonts.
diff --git a/xps/xps_image.c b/xps/xps_image.c
index ced58bb5..f4d02f78 100644
--- a/xps/xps_image.c
+++ b/xps/xps_image.c
@@ -2,10 +2,8 @@
#include "muxps.h"
static int
-xps_decode_image(xps_image **imagep, xps_context *ctx, xps_part *part)
+xps_decode_image(fz_pixmap **imagep, byte *buf, int len)
{
- byte *buf = part->data;
- int len = part->size;
int error;
if (len < 8)
@@ -13,13 +11,13 @@ xps_decode_image(xps_image **imagep, xps_context *ctx, xps_part *part)
if (buf[0] == 0xff && buf[1] == 0xd8)
{
- error = xps_decode_jpeg(imagep, ctx, buf, len);
+ error = xps_decode_jpeg(imagep, buf, len);
if (error)
return fz_rethrow(error, "cannot decode jpeg image");
}
else if (memcmp(buf, "\211PNG\r\n\032\n", 8) == 0)
{
- error = xps_decode_png(imagep, ctx, buf, len);
+ error = xps_decode_png(imagep, buf, len);
if (error)
return fz_rethrow(error, "cannot decode png image");
}
@@ -29,7 +27,7 @@ xps_decode_image(xps_image **imagep, xps_context *ctx, xps_part *part)
}
else if (memcmp(buf, "MM", 2) == 0 || memcmp(buf, "II", 2) == 0)
{
- error = xps_decode_tiff(imagep, ctx, buf, len);
+ error = xps_decode_tiff(imagep, buf, len);
if (error)
return fz_rethrow(error, "cannot decode TIFF image");
}
@@ -40,12 +38,12 @@ xps_decode_image(xps_image **imagep, xps_context *ctx, xps_part *part)
}
static void
-xps_paint_image_brush(xps_context *ctx, fz_matrix ctm, char *base_uri, xps_resource *dict, xml_element *root, void *vimage)
+xps_paint_image_brush(xps_context *ctx, fz_matrix ctm, char *base_uri, xps_resource *dict,
+ xml_element *root, void *vimage)
{
- xps_image *image = vimage;
- fz_pixmap *pixmap = image->pixmap;
- float xs = pixmap->w * 96.0 / image->xres;
- float ys = pixmap->h * 96.0 / image->yres;
+ fz_pixmap *pixmap = vimage;
+ float xs = pixmap->w * 96.0 / pixmap->xres;
+ float ys = pixmap->h * 96.0 / pixmap->yres;
fz_matrix im = fz_scale(xs, -ys);
im.f = ys;
ctm = fz_concat(im, ctm);
@@ -107,7 +105,7 @@ xps_parse_image_brush(xps_context *ctx, fz_matrix ctm, fz_rect area,
char *base_uri, xps_resource *dict, xml_element *root)
{
xps_part *part;
- xps_image *image;
+ fz_pixmap *image;
int code;
part = xps_find_image_brush_source_part(ctx, base_uri, root);
@@ -116,22 +114,15 @@ xps_parse_image_brush(xps_context *ctx, fz_matrix ctm, fz_rect area,
return;
}
- code = xps_decode_image(&image, ctx, part);
+ code = xps_decode_image(&image, part->data, part->size);
if (code < 0) {
+ xps_free_part(ctx, part);
fz_catch(-1, "cannot decode image resource");
return;
}
xps_parse_tiling_brush(ctx, ctm, area, base_uri, dict, root, xps_paint_image_brush, image);
- xps_free_image(ctx, image);
+ fz_droppixmap(image);
xps_free_part(ctx, part);
}
-
-void
-xps_free_image(xps_context *ctx, xps_image *image)
-{
- if (image->pixmap)
- fz_droppixmap(image->pixmap);
- fz_free(image);
-}
diff --git a/xps/xps_jpeg.c b/xps/xps_jpeg.c
index 4dc2a7e4..a8ea40f5 100644
--- a/xps/xps_jpeg.c
+++ b/xps/xps_jpeg.c
@@ -48,7 +48,7 @@ static void skip_input_data(j_decompress_ptr cinfo, long num_bytes)
}
int
-xps_decode_jpeg(xps_image **imagep, xps_context *ctx, byte *rbuf, int rlen)
+xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr_jmp err;
@@ -57,12 +57,12 @@ xps_decode_jpeg(xps_image **imagep, xps_context *ctx, byte *rbuf, int rlen)
fz_colorspace *colorspace;
int x, k;
- xps_image *image = NULL;
+ fz_pixmap *image = NULL;
if (setjmp(err.env))
{
if (image)
- xps_free_image(ctx, image);
+ fz_droppixmap(image);
return fz_throw("jpeg error: %s", err.msg);
}
@@ -93,10 +93,7 @@ xps_decode_jpeg(xps_image **imagep, xps_context *ctx, byte *rbuf, int rlen)
else
return fz_throw("bad number of components in jpeg: %d", cinfo.output_components);
- image = fz_malloc(sizeof(xps_image));
- image->pixmap = fz_newpixmap(colorspace, 0, 0, cinfo.output_width, cinfo.output_height);
- image->xres = 96;
- image->yres = 96;
+ image = fz_newpixmap(colorspace, 0, 0, cinfo.output_width, cinfo.output_height);
if (cinfo.density_unit == 1)
{
@@ -109,10 +106,10 @@ xps_decode_jpeg(xps_image **imagep, xps_context *ctx, byte *rbuf, int rlen)
image->yres = cinfo.Y_density * 2.54;
}
- fz_clearpixmap(image->pixmap);
+ fz_clearpixmap(image);
row[0] = fz_malloc(cinfo.output_components * cinfo.output_width);
- dp = image->pixmap->samples;
+ dp = image->samples;
while (cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo, row, 1);
diff --git a/xps/xps_png.c b/xps/xps_png.c
index f59be56d..829bde90 100644
--- a/xps/xps_png.c
+++ b/xps/xps_png.c
@@ -489,6 +489,9 @@ png_expand_palette(struct info *info, fz_pixmap *src)
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++)
@@ -531,11 +534,10 @@ png_mask_transparency(struct info *info, fz_pixmap *dst)
}
int
-xps_decode_png(xps_image **imagep, xps_context *ctx, byte *p, int total)
+xps_decode_png(fz_pixmap **imagep, byte *p, int total)
{
- fz_pixmap *pixmap;
+ fz_pixmap *image;
fz_colorspace *colorspace;
- xps_image *image;
struct info png;
int code;
int stride;
@@ -551,30 +553,22 @@ xps_decode_png(xps_image **imagep, xps_context *ctx, byte *p, int total)
stride = (png.width * png.n * png.depth + 7) / 8;
- pixmap = fz_newpixmap(colorspace, 0, 0, png.width, png.height);
- fz_unpacktile(pixmap, png.samples, png.n, png.depth, stride, png.indexed);
+ image = fz_newpixmap(colorspace, 0, 0, png.width, png.height);
+ image->xres = png.xres;
+ image->yres = png.yres;
+
+ fz_unpacktile(image, png.samples, png.n, png.depth, stride, png.indexed);
if (png.indexed)
- {
- pixmap = png_expand_palette(&png, pixmap);
- }
+ image = png_expand_palette(&png, image);
else if (png.transparency)
- {
- png_mask_transparency(&png, pixmap);
- }
+ png_mask_transparency(&png, image);
if (png.transparency || png.n == 2 || png.n == 4)
- {
- fz_premultiplypixmap(pixmap);
- }
+ fz_premultiplypixmap(image);
fz_free(png.samples);
- image = fz_malloc(sizeof(xps_image));
- image->pixmap = pixmap;
- image->xres = png.xres;
- image->yres = png.yres;
-
*imagep = image;
return fz_okay;
}
diff --git a/xps/xps_tiff.c b/xps/xps_tiff.c
index 4c977ab1..d70d5f87 100644
--- a/xps/xps_tiff.c
+++ b/xps/xps_tiff.c
@@ -10,9 +10,7 @@
* TODO: RGBPal images
*/
-typedef struct xps_tiff xps_tiff;
-
-struct xps_tiff
+struct tiff
{
/* "file" */
byte *bp, *rp, *ep;
@@ -136,7 +134,7 @@ static const byte bitrev[256] =
};
static int
-xps_decode_tiff_uncompressed(xps_context *ctx, xps_tiff *tiff, fz_stream *stm, byte *wp, int wlen)
+xps_decode_tiff_uncompressed(struct tiff *tiff, fz_stream *stm, byte *wp, int wlen)
{
int n = fz_read(stm, wp, wlen);
fz_close(stm);
@@ -146,7 +144,7 @@ xps_decode_tiff_uncompressed(xps_context *ctx, xps_tiff *tiff, fz_stream *stm, b
}
static int
-xps_decode_tiff_packbits(xps_context *ctx, xps_tiff *tiff, fz_stream *chain, byte *wp, int wlen)
+xps_decode_tiff_packbits(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen)
{
fz_stream *stm = fz_openrld(chain);
int n = fz_read(stm, wp, wlen);
@@ -157,7 +155,7 @@ xps_decode_tiff_packbits(xps_context *ctx, xps_tiff *tiff, fz_stream *chain, byt
}
static int
-xps_decode_tiff_lzw(xps_context *ctx, xps_tiff *tiff, fz_stream *chain, byte *wp, int wlen)
+xps_decode_tiff_lzw(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen)
{
fz_stream *stm = fz_openlzwd(chain, NULL);
int n = fz_read(stm, wp, wlen);
@@ -167,7 +165,7 @@ xps_decode_tiff_lzw(xps_context *ctx, xps_tiff *tiff, fz_stream *chain, byte *wp
return fz_okay;
}
static int
-xps_decode_tiff_flate(xps_context *ctx, xps_tiff *tiff, fz_stream *chain, byte *wp, int wlen)
+xps_decode_tiff_flate(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen)
{
fz_stream *stm = fz_openflated(chain);
int n = fz_read(stm, wp, wlen);
@@ -178,7 +176,7 @@ xps_decode_tiff_flate(xps_context *ctx, xps_tiff *tiff, fz_stream *chain, byte *
}
static int
-xps_decode_tiff_fax(xps_context *ctx, xps_tiff *tiff, int comp, fz_stream *chain, byte *wp, int wlen)
+xps_decode_tiff_fax(struct tiff *tiff, int comp, fz_stream *chain, byte *wp, int wlen)
{
fz_stream *stm;
fz_obj *params;
@@ -215,7 +213,7 @@ xps_decode_tiff_fax(xps_context *ctx, xps_tiff *tiff, int comp, fz_stream *chain
}
static int
-xps_decode_tiff_jpeg(xps_context *ctx, xps_tiff *tiff, fz_stream *chain, byte *wp, int wlen)
+xps_decode_tiff_jpeg(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen)
{
fz_stream *stm = fz_opendctd(chain, NULL);
int n = fz_read(stm, wp, wlen);
@@ -302,7 +300,7 @@ xps_invert_tiff(byte *line, int width, int comps, int bits, int alpha)
}
static int
-xps_expand_tiff_colormap(xps_context *ctx, xps_tiff *tiff)
+xps_expand_tiff_colormap(struct tiff *tiff)
{
int maxval = 1 << tiff->bitspersample;
byte *samples;
@@ -358,7 +356,7 @@ xps_expand_tiff_colormap(xps_context *ctx, xps_tiff *tiff)
}
static int
-xps_decode_tiff_strips(xps_context *ctx, xps_tiff *tiff)
+xps_decode_tiff_strips(struct tiff *tiff)
{
fz_buffer buf;
fz_stream *stm;
@@ -468,31 +466,31 @@ xps_decode_tiff_strips(xps_context *ctx, xps_tiff *tiff)
switch (tiff->compression)
{
case 1:
- error = xps_decode_tiff_uncompressed(ctx, tiff, stm, wp, wlen);
+ error = xps_decode_tiff_uncompressed(tiff, stm, wp, wlen);
break;
case 2:
- error = xps_decode_tiff_fax(ctx, tiff, 2, stm, wp, wlen);
+ error = xps_decode_tiff_fax(tiff, 2, stm, wp, wlen);
break;
case 3:
- error = xps_decode_tiff_fax(ctx, tiff, 3, stm, wp, wlen);
+ error = xps_decode_tiff_fax(tiff, 3, stm, wp, wlen);
break;
case 4:
- error = xps_decode_tiff_fax(ctx, tiff, 4, stm, wp, wlen);
+ error = xps_decode_tiff_fax(tiff, 4, stm, wp, wlen);
break;
case 5:
- error = xps_decode_tiff_lzw(ctx, tiff, stm, wp, wlen);
+ error = xps_decode_tiff_lzw(tiff, stm, wp, wlen);
break;
case 6:
error = fz_throw("deprecated JPEG in TIFF compression not supported");
break;
case 7:
- error = xps_decode_tiff_jpeg(ctx, tiff, stm, wp, wlen);
+ error = xps_decode_tiff_jpeg(tiff, stm, wp, wlen);
break;
case 8:
- error = xps_decode_tiff_flate(ctx, tiff, stm, wp, wlen);
+ error = xps_decode_tiff_flate(tiff, stm, wp, wlen);
break;
case 32773:
- error = xps_decode_tiff_packbits(ctx, tiff, stm, wp, wlen);
+ error = xps_decode_tiff_packbits(tiff, stm, wp, wlen);
break;
default:
error = fz_throw("unknown TIFF compression: %d", tiff->compression);
@@ -524,7 +522,7 @@ xps_decode_tiff_strips(xps_context *ctx, xps_tiff *tiff)
/* RGBPal */
if (tiff->photometric == 3 && tiff->colormap)
{
- error = xps_expand_tiff_colormap(ctx, tiff);
+ error = xps_expand_tiff_colormap(tiff);
if (error)
return fz_rethrow(error, "cannot expand colormap");
}
@@ -543,14 +541,14 @@ xps_decode_tiff_strips(xps_context *ctx, xps_tiff *tiff)
return fz_okay;
}
-static inline int readbyte(xps_tiff *tiff)
+static inline int readbyte(struct tiff *tiff)
{
if (tiff->rp < tiff->ep)
return *tiff->rp++;
return EOF;
}
-static inline unsigned readshort(xps_tiff *tiff)
+static inline unsigned readshort(struct tiff *tiff)
{
unsigned a = readbyte(tiff);
unsigned b = readbyte(tiff);
@@ -559,7 +557,7 @@ static inline unsigned readshort(xps_tiff *tiff)
return (a << 8) | b;
}
-static inline unsigned readlong(xps_tiff *tiff)
+static inline unsigned readlong(struct tiff *tiff)
{
unsigned a = readbyte(tiff);
unsigned b = readbyte(tiff);
@@ -571,7 +569,7 @@ static inline unsigned readlong(xps_tiff *tiff)
}
static void
-xps_read_tiff_bytes(unsigned char *p, xps_tiff *tiff, unsigned ofs, unsigned n)
+xps_read_tiff_bytes(unsigned char *p, struct tiff *tiff, unsigned ofs, unsigned n)
{
tiff->rp = tiff->bp + ofs;
if (tiff->rp > tiff->ep)
@@ -582,7 +580,7 @@ xps_read_tiff_bytes(unsigned char *p, xps_tiff *tiff, unsigned ofs, unsigned n)
}
static void
-xps_read_tiff_tag_value(unsigned *p, xps_tiff *tiff, unsigned type, unsigned ofs, unsigned n)
+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)
@@ -606,7 +604,7 @@ xps_read_tiff_tag_value(unsigned *p, xps_tiff *tiff, unsigned type, unsigned ofs
}
static int
-xps_read_tiff_tag(xps_context *ctx, xps_tiff *tiff, unsigned offset)
+xps_read_tiff_tag(struct tiff *tiff, unsigned offset)
{
unsigned tag;
unsigned type;
@@ -739,7 +737,7 @@ xps_swap_byte_order(byte *buf, int n)
}
static int
-xps_decode_tiff_header(xps_context *ctx, xps_tiff *tiff, byte *buf, int len)
+xps_decode_tiff_header(struct tiff *tiff, byte *buf, int len)
{
unsigned version;
unsigned offset;
@@ -747,7 +745,7 @@ xps_decode_tiff_header(xps_context *ctx, xps_tiff *tiff, byte *buf, int len)
unsigned i;
int error;
- memset(tiff, 0, sizeof(xps_tiff));
+ memset(tiff, 0, sizeof(struct tiff));
tiff->bp = buf;
tiff->rp = buf;
@@ -795,7 +793,7 @@ xps_decode_tiff_header(xps_context *ctx, xps_tiff *tiff, byte *buf, int len)
offset += 2;
for (i = 0; i < count; i++)
{
- error = xps_read_tiff_tag(ctx, tiff, offset);
+ error = xps_read_tiff_tag(tiff, offset);
if (error)
return fz_rethrow(error, "cannot read TIFF header tag");
offset += 12;
@@ -805,14 +803,13 @@ xps_decode_tiff_header(xps_context *ctx, xps_tiff *tiff, byte *buf, int len)
}
int
-xps_decode_tiff(xps_image **imagep, xps_context *ctx, byte *buf, int len)
+xps_decode_tiff(fz_pixmap **imagep, byte *buf, int len)
{
int error;
- fz_pixmap *pixmap;
- xps_image *image;
- xps_tiff tiff;
+ fz_pixmap *image;
+ struct tiff tiff;
- error = xps_decode_tiff_header(ctx, &tiff, buf, len);
+ error = xps_decode_tiff_header(&tiff, buf, len);
if (error)
return fz_rethrow(error, "cannot decode tiff header");
@@ -821,7 +818,7 @@ xps_decode_tiff(xps_image **imagep, xps_context *ctx, byte *buf, int len)
if (tiff.rowsperstrip > tiff.imagelength)
tiff.rowsperstrip = tiff.imagelength;
- error = xps_decode_tiff_strips(ctx, &tiff);
+ error = xps_decode_tiff_strips(&tiff);
if (error)
return fz_rethrow(error, "cannot decode image data");
@@ -834,18 +831,15 @@ xps_decode_tiff(xps_image **imagep, xps_context *ctx, byte *buf, int len)
/* Expand into fz_pixmap struct */
- pixmap = fz_newpixmap(tiff.colorspace, 0, 0, tiff.imagewidth, tiff.imagelength);
+ image = fz_newpixmap(tiff.colorspace, 0, 0, tiff.imagewidth, tiff.imagelength);
+ image->xres = tiff.xresolution;
+ image->yres = tiff.yresolution;
- fz_unpacktile(pixmap, tiff.samples, tiff.samplesperpixel, tiff.bitspersample, tiff.stride, 0);
+ fz_unpacktile(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 */)
- fz_premultiplypixmap(pixmap);
-
- image = fz_malloc(sizeof(xps_image));
- image->pixmap = pixmap;
- image->xres = tiff.xresolution;
- image->yres = tiff.yresolution;
+ fz_premultiplypixmap(image);
/* Clean up scratch memory */