diff options
author | Robin Watts <robin.watts@artifex.com> | 2013-05-03 13:02:10 +0100 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2013-05-03 16:39:02 +0100 |
commit | 09bb1dc2a0cbea13b9ad27f626d2c9e16984ebf9 (patch) | |
tree | 25e7a25b122b1c67805b6982e7f583bd4a34e009 /image | |
parent | f499ef89525fd596753f1a4e93e8a8c56953e21a (diff) | |
download | mupdf-09bb1dc2a0cbea13b9ad27f626d2c9e16984ebf9.tar.xz |
Simple Image file format recogniser
Now can open jpeg/png/tiff files within mupdf.
Diffstat (limited to 'image')
-rw-r--r-- | image/muimage.c | 142 | ||||
-rw-r--r-- | image/muimage.h | 51 |
2 files changed, 193 insertions, 0 deletions
diff --git a/image/muimage.c b/image/muimage.c new file mode 100644 index 00000000..0577f1c5 --- /dev/null +++ b/image/muimage.c @@ -0,0 +1,142 @@ +#include "fitz-internal.h" +#include "muimage.h" + +#include <ctype.h> /* for tolower */ + +#define DPI 72.0f + +static void image_init_document(image_document *doc); + +struct image_document_s +{ + fz_document super; + + fz_context *ctx; + fz_stream *file; + fz_image *image; +}; + +image_document * +image_open_document_with_stream(fz_context *ctx, fz_stream *file) +{ + image_document *doc; + fz_buffer *buffer = NULL; + + doc = fz_malloc_struct(ctx, image_document); + image_init_document(doc); + doc->ctx = ctx; + doc->file = fz_keep_stream(file); + + fz_var(buffer); + + fz_try(ctx) + { + buffer = fz_read_all(doc->file, 1024); + doc->image = fz_new_image_from_buffer(ctx, buffer); + buffer = NULL; + } + fz_catch(ctx) + { + fz_drop_buffer(ctx, buffer); + image_close_document(doc); + fz_rethrow(ctx); + } + + return doc; +} + +image_document * +image_open_document(fz_context *ctx, const char *filename) +{ + fz_stream *file; + image_document *doc; + + file = fz_open_file(ctx, filename); + if (!file) + fz_throw(ctx, "cannot open file '%s': %s", filename, strerror(errno)); + + fz_try(ctx) { + doc = image_open_document_with_stream(ctx, file); + } fz_always(ctx) { + fz_close(file); + } fz_catch(ctx) { + fz_rethrow(ctx); + } + + return doc; +} + +void +image_close_document(image_document *doc) +{ + fz_context *ctx = doc->ctx; + fz_drop_image(ctx, doc->image); + fz_close(doc->file); + fz_free(ctx, doc); +} + +int +image_count_pages(image_document *doc) +{ + return 1; +} + +image_page * +image_load_page(image_document *doc, int number) +{ + if (number != 0) + return NULL; + + return (image_page *)doc->image; +} + +void +image_free_page(image_document *doc, image_page *page) +{ +} + +fz_rect * +image_bound_page(image_document *doc, image_page *page, fz_rect *bbox) +{ + fz_image *image = (fz_image *)page; + bbox->x0 = bbox->y0 = 0; + bbox->x1 = image->w * DPI / image->xres; + bbox->y1 = image->h * DPI / image->yres; + return bbox; +} + +void +image_run_page(image_document *doc, image_page *page, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie) +{ + fz_matrix local_ctm = *ctm; + fz_image *image = (fz_image *)page; + float w = image->w * DPI / image->xres; + float h = image->h * DPI / image->yres; + fz_pre_scale(&local_ctm, w, h); + fz_fill_image(dev, image, &local_ctm, 1); +} + +static int +image_meta(image_document *doc, int key, void *ptr, int size) +{ + switch(key) + { + case FZ_META_FORMAT_INFO: + sprintf((char *)ptr, "IMAGE"); + return FZ_META_OK; + default: + return FZ_META_UNKNOWN_KEY; + } +} + +static void +image_init_document(image_document *doc) +{ + doc->super.close = (void*)image_close_document; + doc->super.count_pages = (void*)image_count_pages; + doc->super.load_page = (void*)image_load_page; + doc->super.bound_page = (void*)image_bound_page; + doc->super.run_page_contents = (void*)image_run_page; + doc->super.free_page = (void*)image_free_page; + doc->super.meta = (void*)image_meta; +} diff --git a/image/muimage.h b/image/muimage.h new file mode 100644 index 00000000..146132f0 --- /dev/null +++ b/image/muimage.h @@ -0,0 +1,51 @@ +#ifndef MUIMAGE_H +#define MUIMAGE_H + +#include "fitz.h" + +typedef struct image_document_s image_document; +typedef struct image_page_s image_page; + +/* + image_open_document: Open a document. + + Open a document for reading so the library is able to locate + objects and pages inside the file. + + The returned image_document should be used when calling most + other functions. Note that it wraps the context, so those + functions implicitly get access to the global state in + context. + + filename: a path to a file as it would be given to open(2). +*/ +image_document *image_open_document(fz_context *ctx, const char *filename); + +/* + image_open_document_with_stream: Opens a document. + + Same as image_open_document, but takes a stream instead of a + filename to locate the document to open. Increments the + reference count of the stream. See fz_open_file, + fz_open_file_w or fz_open_fd for opening a stream, and + fz_close for closing an open stream. +*/ +image_document *image_open_document_with_stream(fz_context *ctx, fz_stream *file); + +/* + image_close_document: Closes and frees an opened document. + + The resource store in the context associated with image_document + is emptied. + + Does not throw exceptions. +*/ +void image_close_document(image_document *doc); + +int image_count_pages(image_document *doc); +image_page *image_load_page(image_document *doc, int number); +fz_rect *image_bound_page(image_document *doc, image_page *page, fz_rect *rect); +void image_free_page(image_document *doc, image_page *page); +void image_run_page(image_document *doc, image_page *page, fz_device *dev, const fz_matrix *ctm, fz_cookie *cookie); + +#endif |