[gs-commits] mupdf/master - 0_6-242-g257b769 - Refactor image loading.

Tor Andersson tor at ghostscript.com
Tue Aug 3 12:09:46 UTC 2010


commit 257b76952b90e99b8754497731ad6851f8c19204
Author: Tor Andersson <tor at ghostscript.com>
Date:   Fri Jul 30 12:14:36 2010 +0000

    Refactor image loading.
    
    Ignore-this: 2018585b59f3f4f3c223126b1580e5b1
    
    darcs-hash:20100730121436-f546f-60131a638b6878fcf10737d73a09e546703c74ed.gz

 3 files changed, 58 insertions(+), 82 deletions(-)

diff --git a/fitz/filt_jpxd.c b/fitz/filt_jpxd.c
index f4530ad..a78d093 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 1a0aebb..0963a2f 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 839107c..53b5619 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);

--
git/hooks/post-receive


More information about the gs-commits mailing list