summaryrefslogtreecommitdiff
path: root/cbz
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2012-02-21 17:09:10 +0000
committerRobin Watts <robin.watts@artifex.com>2012-02-25 08:14:27 -0800
commitf86c02284c259b9034f324dffe76969efa8b2e42 (patch)
tree6e2a0c86ff040ade748d767ecd377282810d0ad4 /cbz
parent7c19fd50ea7ee9dfd253220a0ea2cac4df7d6345 (diff)
downloadmupdf-f86c02284c259b9034f324dffe76969efa8b2e42.tar.xz
Rework image handling for on demand decode
Introduce a new 'fz_image' type; this type contains rudimentary information about images (such as native, size, colorspace etc) and a function to call to get a pixmap of that image (with a size hint). Instead of passing pixmaps through the device interface (and holding pixmaps in the display list) we now pass images instead. The rendering routines therefore call fz_image_to_pixmap to get pixmaps to render, and fz_pixmap_drop those afterwards. The file format handling routines therefore need to produce images rather than pixmaps; xps and cbz currently just wrap pixmaps as images. PDF is more involved. The stream handling routines in PDF have been altered so that they can recognise when the last stream entry in a filter dictionary is an image decoding filter. Rather than applying this filter, they read and store the parameters into a pdf_image_params structure, and stop decoding at that point. This allows us to read the compressed data for an image into memory as a block. We can then restart the image decode process later. pdf_images therefore consist of the compressed image data for images. When a pixmap is requested for such an image, the code checks to see if we have one (of an appropriate size), and if not, decodes it. The size hint is used to determine whether it is possible to subsample the image; currently this is only supported for JPEGs, but we could add generic subsampling code later. In order to handle caching the produced images, various changes have been made to the store and the underlying hash table. Previously the store was indexed purely by fz_obj keys; we don't have an fz_obj key any more, so have extended the store by adding a concept of a key 'type'. A key type is a pointer to a set of functions that keep/drop/compare and make a hashable key from a key pointer. We make a pdf_store.c file that contains functions to offer the existing fz_obj based functions, and add a new 'type' for keys (based on the fz_image handle, and the subsample factor) in the pdf_image.c file. While working on this, a problem became apparent in the existing store codel; fz_obj objects had no protection on their reference counts, hence an interpreter thread could try to alter a ref count at the same time as a malloc caused an eviction from the store. This has been solved by using the alloc lock as protection. This in turn requires some tweaks to the code to make sure we don't try and keep/drop fz_obj's from the store code while the alloc lock is held. A side effect of this work is that when a hash table is created, we inform it what lock should be used to protect its innards (if any). If the alloc lock is used, the insert method knows to drop/retake it to allow it to safely expand the hash table. Callers to the hash functions have the responsibility of taking/dropping the appropriate lock, and ensuring that they cope with the possibility that insert might drop the alloc lock, causing race conditions.
Diffstat (limited to 'cbz')
-rw-r--r--cbz/mucbz.c73
1 files changed, 58 insertions, 15 deletions
diff --git a/cbz/mucbz.c b/cbz/mucbz.c
index 83c463bd..89c9d4dd 100644
--- a/cbz/mucbz.c
+++ b/cbz/mucbz.c
@@ -19,9 +19,18 @@ static const char *cbz_ext_list[] = {
NULL
};
+typedef struct cbz_image_s cbz_image;
+
+struct cbz_image_s
+{
+ fz_image base;
+ int xres, yres;
+ fz_pixmap *pix;
+};
+
struct cbz_page_s
{
- fz_pixmap *image;
+ cbz_image *image;
};
typedef struct cbz_entry_s cbz_entry;
@@ -343,12 +352,34 @@ cbz_count_pages(cbz_document *doc)
return doc->page_count;
}
+static void
+cbz_free_image(fz_context *ctx, fz_storable *image_)
+{
+ cbz_image *image = (cbz_image *)image_;
+
+ if (image == NULL)
+ return;
+ fz_drop_pixmap(ctx, image->pix);
+ fz_free(ctx, image);
+}
+
+static fz_pixmap *
+cbz_image_to_pixmap(fz_context *ctx, fz_image *image_, int x, int w)
+{
+ cbz_image *image = (cbz_image *)image_;
+
+ return fz_keep_pixmap(ctx, image->pix);
+}
+
+
cbz_page *
cbz_load_page(cbz_document *doc, int number)
{
fz_context *ctx = doc->ctx;
unsigned char *data = NULL;
cbz_page *page = NULL;
+ cbz_image *image = NULL;
+ fz_pixmap *pixmap = NULL;
int size;
if (number < 0 || number >= doc->page_count)
@@ -358,19 +389,31 @@ cbz_load_page(cbz_document *doc, int number)
fz_var(data);
fz_var(page);
+ fz_var(image);
+ fz_var(pixmap);
fz_try(ctx)
{
- page = fz_malloc_struct(doc->ctx, cbz_page);
+ page = fz_malloc_struct(ctx, cbz_page);
page->image = NULL;
data = cbz_read_zip_entry(doc, doc->entry[number].offset, &size);
if (data[0] == 0xff && data[1] == 0xd8)
- page->image = fz_load_jpeg(ctx, data, size);
+ pixmap = fz_load_jpeg(ctx, data, size);
else if (memcmp(data, "\211PNG\r\n\032\n", 8) == 0)
- page->image = fz_load_png(ctx, data, size);
+ pixmap = fz_load_png(ctx, data, size);
else
fz_throw(ctx, "unknown image format");
+
+ image = fz_malloc_struct(ctx, cbz_image);
+ FZ_INIT_STORABLE(&image->base, 1, cbz_free_image);
+ image->base.w = pixmap->w;
+ image->base.h = pixmap->h;
+ image->base.get_pixmap = cbz_image_to_pixmap;
+ image->xres = pixmap->xres;
+ image->yres = pixmap->yres;
+ image->pix = pixmap;
+ page->image = image;
}
fz_always(ctx)
{
@@ -378,9 +421,7 @@ cbz_load_page(cbz_document *doc, int number)
}
fz_catch(ctx)
{
- if (page && page->image)
- fz_drop_pixmap(ctx, page->image);
- fz_free(ctx, page);
+ cbz_free_page(doc, page);
fz_rethrow(ctx);
}
@@ -390,29 +431,31 @@ cbz_load_page(cbz_document *doc, int number)
void
cbz_free_page(cbz_document *doc, cbz_page *page)
{
- fz_drop_pixmap(doc->ctx, page->image);
+ if (!page)
+ return;
+ fz_drop_image(doc->ctx, &page->image->base);
fz_free(doc->ctx, page);
}
fz_rect
cbz_bound_page(cbz_document *doc, cbz_page *page)
{
- fz_pixmap *image = page->image;
+ cbz_image *image = page->image;
fz_rect bbox;
bbox.x0 = bbox.y0 = 0;
- bbox.x1 = image->w * DPI / image->xres;
- bbox.y1 = image->h * DPI / image->yres;
+ bbox.x1 = image->base.w * DPI / image->xres;
+ bbox.y1 = image->base.h * DPI / image->yres;
return bbox;
}
void
cbz_run_page(cbz_document *doc, cbz_page *page, fz_device *dev, fz_matrix ctm, fz_cookie *cookie)
{
- fz_pixmap *image = page->image;
- float w = image->w * DPI / image->xres;
- float h = image->h * DPI / image->yres;
+ cbz_image *image = page->image;
+ float w = image->base.w * DPI / image->xres;
+ float h = image->base.h * DPI / image->yres;
ctm = fz_concat(fz_scale(w, h), ctm);
- fz_fill_image(dev, image, ctm, 1);
+ fz_fill_image(dev, &image->base, ctm, 1);
}
/* Document interface wrappers */