summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ghostscript.com>2010-07-30 12:14:36 +0000
committerTor Andersson <tor@ghostscript.com>2010-07-30 12:14:36 +0000
commite2823074458016584251b962f23ae7fc0b707b32 (patch)
treef5d99633e074c8a81bfee58cb096f0646ca4eaeb
parent4f49317deff939df854a8d9fecd7502cf2b4c9b2 (diff)
downloadmupdf-e2823074458016584251b962f23ae7fc0b707b32.tar.xz
Refactor image loading.
-rw-r--r--fitz/filt_jpxd.c9
-rw-r--r--mupdf/mupdf.h2
-rw-r--r--mupdf/pdf_image.c129
3 files changed, 58 insertions, 82 deletions
diff --git a/fitz/filt_jpxd.c b/fitz/filt_jpxd.c
index f4530ad5..a78d0932 100644
--- a/fitz/filt_jpxd.c
+++ b/fitz/filt_jpxd.c
@@ -38,8 +38,6 @@ pdf_loadjpximage(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict)
int n, w, h, depth, sgnd;
int x, y, k, v;
- pdf_logimage("load jpx image (%d %d R) {\n", fz_tonum(dict), fz_togen(dict));
-
error = pdf_loadstream(&buf, xref, fz_tonum(dict), fz_togen(dict));
if (error)
return fz_throw("read error in jpx image");
@@ -102,8 +100,7 @@ pdf_loadjpximage(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict)
img->bpc = 8;
img->n = n;
img->stride = w * n;
- img->samples = fz_newbuffer(w * n * h);
- img->samples->len = img->samples->cap;
+ img->samples = fz_malloc(w * n * h);
switch (n)
{
@@ -119,7 +116,7 @@ pdf_loadjpximage(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict)
img->decode[k * 2 + 1] = 1;
}
- p = img->samples->data;
+ p = img->samples;
for (y = 0; y < h; y++)
{
for (x = 0; x < w; x++)
@@ -138,8 +135,6 @@ pdf_loadjpximage(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict)
opj_image_destroy(jpx);
- pdf_logimage("}\n");
-
*imgp = img;
return fz_okay;
}
diff --git a/mupdf/mupdf.h b/mupdf/mupdf.h
index 1a0aebb3..0963a2fd 100644
--- a/mupdf/mupdf.h
+++ b/mupdf/mupdf.h
@@ -270,7 +270,7 @@ struct pdf_image_s
int colorkey[FZ_MAXCOLORS * 2];
float decode[FZ_MAXCOLORS * 2];
int stride;
- fz_buffer *samples;
+ unsigned char *samples;
};
fz_error pdf_loadinlineimage(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict, fz_stream *file);
diff --git a/mupdf/pdf_image.c b/mupdf/pdf_image.c
index 839107c6..53b56194 100644
--- a/mupdf/pdf_image.c
+++ b/mupdf/pdf_image.c
@@ -22,18 +22,19 @@ pdf_dropimage(pdf_image *img)
if (img->mask)
pdf_dropimage(img->mask);
if (img->samples)
- fz_dropbuffer(img->samples);
+ fz_free(img->samples);
fz_free(img);
}
}
static fz_error
-pdf_loadimageheader(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict)
+pdf_loadimageimp(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict, fz_stream *cstm)
{
pdf_image *img;
+ fz_stream *stm;
fz_error error;
fz_obj *obj, *res;
- int i;
+ int i, n;
img = fz_malloc(sizeof(pdf_image));
memset(img, 0, sizeof(pdf_image));
@@ -129,6 +130,47 @@ pdf_loadimageheader(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict)
}
img->stride = (img->w * img->n * img->bpc + 7) / 8;
+ img->samples = fz_malloc(img->h * img->stride);
+
+ if (cstm)
+ {
+ stm = pdf_openinlinestream(cstm, xref, dict, img->stride * img->h);
+ }
+ else
+ {
+ error = pdf_openstream(&stm, xref, fz_tonum(dict), fz_togen(dict));
+ if (error)
+ {
+ pdf_dropimage(img);
+ return fz_rethrow(error, "cannot open image data stream (%d 0 R)", fz_tonum(dict));
+ }
+ }
+
+ n = fz_read(stm, img->samples, img->h * img->stride);
+ if (n < 0)
+ {
+ fz_close(stm);
+ pdf_dropimage(img);
+ return fz_rethrow(n, "cannot read image data");
+ }
+
+ fz_close(stm);
+
+ /* Pad truncated images */
+ if (n < img->stride * img->h)
+ {
+ fz_warn("padding truncated image");
+ memset(img->samples + n, 0, img->stride * img->h - n);
+ }
+
+ if (img->imagemask)
+ {
+ /* 0=opaque and 1=transparent so we need to invert */
+ unsigned char *p = img->samples;
+ n = img->h * img->stride;
+ for (i = 0; i < n; i++)
+ p[i] = ~p[i];
+ }
pdf_logimage("size %dx%d n=%d bpc=%d (imagemask=%d)\n", img->w, img->h, img->n, img->bpc, img->imagemask);
@@ -137,41 +179,17 @@ pdf_loadimageheader(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict)
}
fz_error
-pdf_loadinlineimage(pdf_image **imgp, pdf_xref *xref,
- fz_obj *rdb, fz_obj *dict, fz_stream *file)
+pdf_loadinlineimage(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict, fz_stream *file)
{
fz_error error;
pdf_image *img;
- fz_stream *subfile;
- int i, n;
pdf_logimage("load inline image {\n");
- error = pdf_loadimageheader(&img, xref, rdb, dict);
+ error = pdf_loadimageimp(&img, xref, rdb, dict, file);
if (error)
return fz_rethrow(error, "cannot load inline image");
- subfile = pdf_openinlinestream(file, xref, dict, img->stride * img->h);
-
- img->samples = fz_newbuffer(img->h * img->stride);
- n = fz_read(subfile, img->samples->data, img->h * img->stride);
- if (n < 0)
- {
- pdf_dropimage(img);
- return fz_rethrow(n, "cannot load inline image data");
- }
- img->samples->len = n;
-
- fz_close(subfile);
-
- if (img->imagemask)
- {
- /* 0=opaque and 1=transparent so we need to invert */
- unsigned char *p = img->samples->data;
- for (i = 0; i < img->samples->len; i++)
- p[i] = ~p[i];
- }
-
pdf_logimage("}\n");
*imgp = img;
@@ -194,10 +212,8 @@ fz_error
pdf_loadimage(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict)
{
fz_error error;
- fz_stream *stm;
pdf_image *img;
fz_obj *obj;
- int i, n;
if ((*imgp = pdf_finditem(xref->store, pdf_dropimage, dict)))
{
@@ -205,6 +221,8 @@ pdf_loadimage(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict)
return fz_okay;
}
+ pdf_logimage("load image (%d %d R) {\n", fz_tonum(dict), fz_togen(dict));
+
/* special case for JPEG2000 images */
obj = fz_dictgets(dict, "Filter");
if (pdf_isjpximage(obj))
@@ -212,55 +230,18 @@ pdf_loadimage(pdf_image **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict)
error = pdf_loadjpximage(&img, xref, rdb, dict);
if (error)
return fz_rethrow(error, "cannot load jpx image");
- goto skip;
- }
-
- pdf_logimage("load image (%d %d R) {\n", fz_tonum(dict), fz_togen(dict));
-
- error = pdf_loadimageheader(&img, xref, rdb, dict);
- if (error)
- return fz_rethrow(error, "cannot load image (%d %d R)", fz_tonum(dict), fz_togen(dict));
-
- error = pdf_openstream(&stm, xref, fz_tonum(dict), fz_togen(dict));
- if (error)
- {
- pdf_dropimage(img);
- return fz_rethrow(error, "cannot load image data (%d %d R)", fz_tonum(dict), fz_togen(dict));
- }
-
- img->samples = fz_newbuffer(img->h * img->stride);
- n = fz_read(stm, img->samples->data, img->h * img->stride);
- if (n < 0)
- {
- pdf_dropimage(img);
- fz_close(stm);
- return fz_rethrow(n, "cannot load image data");
}
- img->samples->len = n;
-
- fz_close(stm);
-
- /* Pad truncated images */
- if (img->samples->len < img->stride * img->h)
+ else
{
- fz_warn("padding truncated image");
- memset(img->samples->data + img->samples->len, 0, img->samples->cap - img->samples->len);
- img->samples->len = img->samples->cap;
+ error = pdf_loadimageimp(&img, xref, rdb, dict, nil);
+ if (error)
+ return fz_rethrow(error, "cannot load image (%d %d R)", fz_tonum(dict), fz_togen(dict));
}
- if (img->imagemask)
- {
- /* 0=opaque and 1=transparent so we need to invert */
- unsigned char *p = img->samples->data;
- for (i = 0; i < img->samples->len; i++)
- p[i] = ~p[i];
- }
+ pdf_storeitem(xref->store, pdf_keepimage, pdf_dropimage, dict, img);
pdf_logimage("}\n");
-skip:
- pdf_storeitem(xref->store, pdf_keepimage, pdf_dropimage, dict, img);
-
*imgp = img;
return fz_okay;
}
@@ -303,7 +284,7 @@ pdf_loadtile(pdf_image *img /* ...bbox/y+h should go here... */)
}
}
- fz_unpacktile(tile, img->samples->data, img->n, img->bpc, img->stride, scale);
+ fz_unpacktile(tile, img->samples, img->n, img->bpc, img->stride, scale);
if (img->usecolorkey)
pdf_maskcolorkey(tile, img->n, img->colorkey);