diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2018-04-26 14:43:22 +0200 |
---|---|---|
committer | Sebastian Rasmussen <sebras@gmail.com> | 2018-04-27 20:12:31 +0800 |
commit | 229761e8b7e8685620cf4c038d848bccd563aa12 (patch) | |
tree | 6c4d946d48201d2553fba98af07947546f1e8dc9 /source | |
parent | bc3cb253d79c03e373631eb7bc53da7a12365656 (diff) | |
download | mupdf-229761e8b7e8685620cf4c038d848bccd563aa12.tar.xz |
Handle multi-page image formats in muimg document driver.
Remove the mutiff document type.
Diffstat (limited to 'source')
-rw-r--r-- | source/cbz/muimg.c | 131 | ||||
-rw-r--r-- | source/cbz/mutiff.c | 175 | ||||
-rw-r--r-- | source/fitz/document-all.c | 4 |
3 files changed, 87 insertions, 223 deletions
diff --git a/source/cbz/muimg.c b/source/cbz/muimg.c index ef483c18..ff2cc3ff 100644 --- a/source/cbz/muimg.c +++ b/source/cbz/muimg.c @@ -16,20 +16,24 @@ struct img_page_s struct img_document_s { fz_document super; - fz_image *image; + fz_buffer *buffer; + const char *format; + int page_count; + fz_pixmap *(*load_subimage)(fz_context *ctx, const unsigned char *p, size_t total, int subimage); }; static void img_drop_document(fz_context *ctx, fz_document *doc_) { img_document *doc = (img_document*)doc_; - fz_drop_image(ctx, doc->image); + fz_drop_buffer(ctx, doc->buffer); } static int img_count_pages(fz_context *ctx, fz_document *doc_) { - return 1; + img_document *doc = (img_document*)doc_; + return doc->page_count; } static fz_rect * @@ -38,6 +42,7 @@ img_bound_page(fz_context *ctx, fz_page *page_, fz_rect *bbox) img_page *page = (img_page*)page_; fz_image *image = page->image; int xres, yres; + fz_image_resolution(image, &xres, &yres); bbox->x0 = bbox->y0 = 0; bbox->x1 = image->w * DPI / xres; @@ -53,6 +58,7 @@ img_run_page(fz_context *ctx, fz_page *page_, fz_device *dev, const fz_matrix *c fz_image *image = page->image; int xres, yres; float w, h; + fz_image_resolution(image, &xres, &yres); w = image->w * DPI / xres; h = image->h * DPI / yres; @@ -71,18 +77,48 @@ static fz_page * img_load_page(fz_context *ctx, fz_document *doc_, int number) { img_document *doc = (img_document*)doc_; + fz_pixmap *pixmap = NULL; + fz_image *image = NULL; img_page *page; - if (number != 0) + if (number < 0 || number >= doc->page_count) return NULL; - page = fz_new_derived_page(ctx, img_page); - - page->super.bound_page = img_bound_page; - page->super.run_page_contents = img_run_page; - page->super.drop_page = img_drop_page; + fz_var(pixmap); + fz_var(image); + fz_var(page); - page->image = fz_keep_image(ctx, doc->image); + fz_try(ctx) + { + if (doc->load_subimage) + { + size_t len; + unsigned char *data; + len = fz_buffer_storage(ctx, doc->buffer, &data); + pixmap = doc->load_subimage(ctx, data, len, number); + image = fz_new_image_from_pixmap(ctx, pixmap, NULL); + } + else + { + image = fz_new_image_from_buffer(ctx, doc->buffer); + } + + page = fz_new_derived_page(ctx, img_page); + page->super.bound_page = img_bound_page; + page->super.run_page_contents = img_run_page; + page->super.drop_page = img_drop_page; + page->image = fz_keep_image(ctx, image); + } + fz_always(ctx) + { + fz_drop_image(ctx, image); + fz_drop_pixmap(ctx, pixmap); + } + fz_catch(ctx) + { + fz_free(ctx, page); + fz_rethrow(ctx); + } return (fz_page*)page; } @@ -90,57 +126,59 @@ img_load_page(fz_context *ctx, fz_document *doc_, int number) static int img_lookup_metadata(fz_context *ctx, fz_document *doc_, const char *key, char *buf, int size) { + img_document *doc = (img_document*)doc_; if (!strcmp(key, "format")) - return (int)fz_strlcpy(buf, "Image", size); + return (int)fz_strlcpy(buf, doc->format, size); return -1; } -static fz_colorspace* -img_get_colorspace(fz_context *ctx, fz_document *doc_) +static fz_document * +img_open_document_with_stream(fz_context *ctx, fz_stream *file) { - img_document *doc = (img_document*)doc_; - return doc->image->colorspace; -} + img_document *doc = NULL; -static img_document * -img_new_document(fz_context *ctx, fz_image *image) -{ - img_document *doc = fz_new_derived_document(ctx, img_document); + doc = fz_new_derived_document(ctx, img_document); doc->super.drop_document = img_drop_document; doc->super.count_pages = img_count_pages; doc->super.load_page = img_load_page; doc->super.lookup_metadata = img_lookup_metadata; - doc->super.get_output_intent = img_get_colorspace; - - doc->image = fz_keep_image(ctx, image); - - return doc; -} - -static fz_document * -img_open_document_with_stream(fz_context *ctx, fz_stream *stm) -{ - fz_buffer *buffer = NULL; - fz_image *image = NULL; - img_document *doc = NULL; - - fz_var(buffer); - fz_var(image); fz_try(ctx) { - buffer = fz_read_all(ctx, stm, 1024); - image = fz_new_image_from_buffer(ctx, buffer); - doc = img_new_document(ctx, image); - } - fz_always(ctx) - { - fz_drop_image(ctx, image); - fz_drop_buffer(ctx, buffer); + int fmt; + size_t len; + unsigned char *data; + + doc->buffer = fz_read_all(ctx, file, 1024); + len = fz_buffer_storage(ctx, doc->buffer, &data); + + fmt = FZ_IMAGE_UNKNOWN; + if (len >= 8) + fmt = fz_recognize_image_format(ctx, data); + if (fmt == FZ_IMAGE_TIFF) + { + doc->page_count = fz_load_tiff_subimage_count(ctx, data, len); + doc->load_subimage = fz_load_tiff_subimage; + doc->format = "TIFF"; + } + else if (fmt == FZ_IMAGE_PNM) + { + doc->page_count = fz_load_pnm_subimage_count(ctx, data, len); + doc->load_subimage = fz_load_pnm_subimage; + doc->format = "PNM"; + } + else + { + doc->page_count = 1; + doc->format = "Image"; + } } fz_catch(ctx) + { + fz_drop_document(ctx, (fz_document*)doc); fz_rethrow(ctx); + } return (fz_document*)doc; } @@ -165,6 +203,8 @@ static const char *img_extensions[] = "png", "pnm", "ppm", + "tif", + "tiff", "wdp", NULL }; @@ -179,11 +219,14 @@ static const char *img_mimetypes[] = "image/jxr", "image/pjpeg", "image/png", + "image/tiff", "image/vnd.ms-photo", + "image/x-portable-anymap", "image/x-portable-arbitrarymap", "image/x-portable-bitmap", "image/x-portable-greymap", "image/x-portable-pixmap", + "image/x-tiff", NULL }; diff --git a/source/cbz/mutiff.c b/source/cbz/mutiff.c deleted file mode 100644 index b6500159..00000000 --- a/source/cbz/mutiff.c +++ /dev/null @@ -1,175 +0,0 @@ -#include "mupdf/fitz.h" - -#include <string.h> - -typedef struct tiff_document_s tiff_document; -typedef struct tiff_page_s tiff_page; - -#define DPI 72.0f - -struct tiff_page_s -{ - fz_page super; - fz_image *image; -}; - -struct tiff_document_s -{ - fz_document super; - fz_buffer *buffer; - int page_count; -}; - -static fz_rect * -tiff_bound_page(fz_context *ctx, fz_page *page_, fz_rect *bbox) -{ - tiff_page *page = (tiff_page*)page_; - fz_image *image = page->image; - int xres, yres; - - fz_image_resolution(image, &xres, &yres); - bbox->x0 = bbox->y0 = 0; - bbox->x1 = image->w * DPI / xres; - bbox->y1 = image->h * DPI / yres; - return bbox; -} - -static void -tiff_run_page(fz_context *ctx, fz_page *page_, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie) -{ - tiff_page *page = (tiff_page*)page_; - fz_matrix local_ctm = *ctm; - fz_image *image = page->image; - int xres, yres; - float w, h; - - fz_image_resolution(image, &xres, &yres); - w = image->w * DPI / xres; - h = image->h * DPI / yres; - fz_pre_scale(&local_ctm, w, h); - fz_fill_image(ctx, dev, image, &local_ctm, 1, NULL); -} - -static void -tiff_drop_page(fz_context *ctx, fz_page *page_) -{ - tiff_page *page = (tiff_page*)page_; - fz_drop_image(ctx, page->image); -} - -static fz_page * -tiff_load_page(fz_context *ctx, fz_document *doc_, int number) -{ - tiff_document *doc = (tiff_document*)doc_; - fz_pixmap *pixmap = NULL; - fz_image *image = NULL; - tiff_page *page = NULL; - - if (number < 0 || number >= doc->page_count) - return NULL; - - fz_var(pixmap); - fz_var(image); - fz_var(page); - - fz_try(ctx) - { - size_t len; - unsigned char *data; - len = fz_buffer_storage(ctx, doc->buffer, &data); - pixmap = fz_load_tiff_subimage(ctx, data, len, number); - image = fz_new_image_from_pixmap(ctx, pixmap, NULL); - - page = fz_new_derived_page(ctx, tiff_page); - page->super.bound_page = tiff_bound_page; - page->super.run_page_contents = tiff_run_page; - page->super.drop_page = tiff_drop_page; - page->image = fz_keep_image(ctx, image); - } - fz_always(ctx) - { - fz_drop_image(ctx, image); - fz_drop_pixmap(ctx, pixmap); - } - fz_catch(ctx) - { - fz_free(ctx, page); - fz_rethrow(ctx); - } - - return (fz_page*)page; -} - -static int -tiff_count_pages(fz_context *ctx, fz_document *doc_) -{ - tiff_document *doc = (tiff_document*)doc_; - return doc->page_count; -} - -static int -tiff_lookup_metadata(fz_context *ctx, fz_document *doc_, const char *key, char *buf, int size) -{ - if (!strcmp(key, "format")) - return (int)fz_strlcpy(buf, "TIFF", size); - return -1; -} - -static void -tiff_drop_document(fz_context *ctx, fz_document *doc_) -{ - tiff_document *doc = (tiff_document*)doc_; - fz_drop_buffer(ctx, doc->buffer); -} - -static fz_document * -tiff_open_document_with_stream(fz_context *ctx, fz_stream *file) -{ - tiff_document *doc; - - doc = fz_new_derived_document(ctx, tiff_document); - - doc->super.drop_document = tiff_drop_document; - doc->super.count_pages = tiff_count_pages; - doc->super.load_page = tiff_load_page; - doc->super.lookup_metadata = tiff_lookup_metadata; - - fz_try(ctx) - { - size_t len; - unsigned char *data; - doc->buffer = fz_read_all(ctx, file, 1024); - len = fz_buffer_storage(ctx, doc->buffer, &data); - doc->page_count = fz_load_tiff_subimage_count(ctx, data, len); - } - fz_catch(ctx) - { - fz_drop_document(ctx, (fz_document*)doc); - fz_rethrow(ctx); - } - - return (fz_document*)doc; -} - -static const char *tiff_extensions[] = -{ - "tif", - "tiff", - NULL -}; - -static const char *tiff_mimetypes[] = -{ - "image/tiff", - "image/x-tiff", - NULL -}; - -fz_document_handler tiff_document_handler = -{ - NULL, - NULL, - tiff_open_document_with_stream, - tiff_extensions, - tiff_mimetypes -}; diff --git a/source/fitz/document-all.c b/source/fitz/document-all.c index 53969e2d..acbbbd42 100644 --- a/source/fitz/document-all.c +++ b/source/fitz/document-all.c @@ -5,7 +5,6 @@ extern fz_document_handler xps_document_handler; extern fz_document_handler svg_document_handler; extern fz_document_handler cbz_document_handler; extern fz_document_handler img_document_handler; -extern fz_document_handler tiff_document_handler; extern fz_document_handler html_document_handler; extern fz_document_handler epub_document_handler; extern fz_document_handler gprf_document_handler; @@ -27,9 +26,6 @@ void fz_register_document_handlers(fz_context *ctx) #if FZ_ENABLE_IMG fz_register_document_handler(ctx, &img_document_handler); #endif /* FZ_ENABLE_IMG */ -#if FZ_ENABLE_TIFF - fz_register_document_handler(ctx, &tiff_document_handler); -#endif /* FZ_ENABLE_TIFF */ #if FZ_ENABLE_HTML fz_register_document_handler(ctx, &html_document_handler); #endif /* FZ_ENABLE_HTML */ |