diff options
author | Robin Watts <robin.watts@artifex.com> | 2013-05-09 20:08:09 +0100 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2013-05-10 18:44:57 +0100 |
commit | 13577a1ec82a9443ddb1943582a4d387ba15a99f (patch) | |
tree | 2ecaec84d1322954df8f786fe8204b6b632d3825 /fitz | |
parent | 81626cb122701420a15dae49ed0e7ec3c442e9d3 (diff) | |
download | mupdf-13577a1ec82a9443ddb1943582a4d387ba15a99f.tar.xz |
Tweak png outputting functions.
Allow us to get an image as a png in a buffer.
Diffstat (limited to 'fitz')
-rw-r--r-- | fitz/fitz.h | 111 | ||||
-rw-r--r-- | fitz/res_pixmap.c | 40 | ||||
-rw-r--r-- | fitz/text_output.c | 7 |
3 files changed, 97 insertions, 61 deletions
diff --git a/fitz/fitz.h b/fitz/fitz.h index 6e9d3515..2cea5424 100644 --- a/fitz/fitz.h +++ b/fitz/fitz.h @@ -1786,6 +1786,64 @@ fz_bitmap *fz_halftone_pixmap(fz_context *ctx, fz_pixmap *pix, fz_halftone *ht); typedef struct fz_font_s fz_font; /* + Generic output streams - generalise between outputting to a file, + a buffer, etc. +*/ +typedef struct fz_output_s fz_output; + +struct fz_output_s +{ + fz_context *ctx; + void *opaque; + int (*printf)(fz_output *, const char *, va_list ap); + int (*write)(fz_output *, const void *, int n); + void (*close)(fz_output *); +}; + +/* + fz_new_output_with_file: Open an output stream onto a FILE *. + + The stream does NOT take ownership of the FILE *. +*/ +fz_output *fz_new_output_with_file(fz_context *, FILE *); + +/* + fz_new_output_with_buffer: Open an output stream onto a buffer. + + The stream does NOT take ownership of the buffer. +*/ +fz_output *fz_new_output_with_buffer(fz_context *, fz_buffer *); + +/* + fz_printf: fprintf equivalent for output streams. +*/ +int fz_printf(fz_output *, const char *, ...); + +/* + fz_write: fwrite equivalent for output streams. +*/ +int fz_write(fz_output *out, const void *data, int len); + +/* + Output a pixmap to an output stream as a png. +*/ +void fz_output_png(fz_context *ctx, const fz_pixmap *pixmap, fz_output *out, int savealpha); + +/* + Get an image as a png in a buffer. +*/ +fz_buffer *fz_image_as_png(fz_context *ctx, fz_image *image, int w, int h); + +/* + fz_close_output: Close a previously opened fz_output stream. + + Note: whether or not this closes the underlying output method is + method dependent. FILE * streams created by fz_new_output_with_file + are NOT closed. +*/ +void fz_close_output(fz_output *); + +/* The different format handlers (pdf, xps etc) interpret pages to a device. These devices can then process the stream of calls they recieve in various ways: @@ -2100,59 +2158,6 @@ void fz_free_text_page(fz_context *ctx, fz_text_page *page); void fz_analyze_text(fz_context *ctx, fz_text_sheet *sheet, fz_text_page *page); /* - Generic output streams - generalise between outputting to a file, - a buffer, etc. -*/ -typedef struct fz_output_s fz_output; - -struct fz_output_s -{ - fz_context *ctx; - void *opaque; - int (*printf)(fz_output *, const char *, va_list ap); - int (*write)(fz_output *, const void *, int n); - void (*close)(fz_output *); -}; - -/* - fz_new_output_with_file: Open an output stream onto a FILE *. - - The stream does NOT take ownership of the FILE *. -*/ -fz_output *fz_new_output_with_file(fz_context *, FILE *); - -/* - fz_new_output_with_buffer: Open an output stream onto a buffer. - - The stream doesn NOT take ownership of the buffer. -*/ -fz_output *fz_new_output_with_buffer(fz_context *, fz_buffer *); - -/* - fz_printf: fprintf equivalent for output streams. -*/ -int fz_printf(fz_output *, const char *, ...); - -/* - fz_write: fwrite equivalent for output streams. -*/ -int fz_write(fz_output *out, const void *data, int len); - -/* - Output a pixmap to an output stream as a png. -*/ -void fz_output_pixmap_to_png(fz_context *ctx, fz_pixmap *pixmap, fz_output *out, int savealpha); - -/* - fz_close_output: Close a previously opened fz_output stream. - - Note: whether or not this closes the underlying output method is - method dependent. FILE * streams created by fz_new_output_with_file - are NOT closed. -*/ -void fz_close_output(fz_output *); - -/* fz_print_text_sheet: Output a text sheet to a file as CSS. */ void fz_print_text_sheet(fz_context *ctx, fz_output *out, fz_text_sheet *sheet); diff --git a/fitz/res_pixmap.c b/fitz/res_pixmap.c index 12d5956e..5c12f19b 100644 --- a/fitz/res_pixmap.c +++ b/fitz/res_pixmap.c @@ -574,7 +574,7 @@ fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha) fz_try(ctx) { - fz_output_pixmap_to_png(ctx, pixmap, fz_new_output_with_file(ctx, fp), savealpha); + fz_output_png(ctx, pixmap, fz_new_output_with_file(ctx, fp), savealpha); } fz_always(ctx) { @@ -587,7 +587,7 @@ fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha) } void -fz_output_pixmap_to_png(fz_context *ctx, fz_pixmap *pixmap, fz_output *out, int savealpha) +fz_output_png(fz_context *ctx, const fz_pixmap *pixmap, fz_output *out, int savealpha) { static const unsigned char pngsig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 }; unsigned char head[13]; @@ -676,6 +676,42 @@ fz_output_pixmap_to_png(fz_context *ctx, fz_pixmap *pixmap, fz_output *out, int fz_free(ctx, cdata); } +fz_buffer * +fz_image_as_png(fz_context *ctx, fz_image *image, int w, int h) +{ + fz_pixmap *pix = fz_image_get_pixmap(ctx, image, image->w, image->h); + fz_buffer *buf = NULL; + fz_output *out; + + fz_var(buf); + fz_var(out); + + fz_try(ctx) + { + if (pix->colorspace != fz_device_gray || pix->colorspace != fz_device_rgb) + { + fz_pixmap *pix2 = fz_new_pixmap(ctx, fz_device_rgb, pix->w, pix->h); + fz_convert_pixmap(ctx, pix2, pix); + fz_drop_pixmap(ctx, pix); + pix = pix2; + } + buf = fz_new_buffer(ctx, 1024); + out = fz_new_output_with_buffer(ctx, buf); + fz_output_png(ctx, pix, out, 0); + } + fz_always(ctx) + { + fz_close_output(out); + fz_drop_pixmap(ctx, pix); + } + fz_catch(ctx) + { + fz_drop_buffer(ctx, buf); + fz_rethrow(ctx); + } + return buf; +} + unsigned int fz_pixmap_size(fz_context *ctx, fz_pixmap * pix) { diff --git a/fitz/text_output.c b/fitz/text_output.c index 7aceddef..6942eb67 100644 --- a/fitz/text_output.c +++ b/fitz/text_output.c @@ -258,15 +258,10 @@ fz_print_text_page_html(fz_context *ctx, fz_output *out, fz_text_page *page) break; default: { - fz_pixmap *pix = fz_image_get_pixmap(ctx, image->image, image->image->w, image->image->h); - fz_buffer *buf = fz_new_buffer(ctx, 1024); - fz_output *out2 = fz_new_output_with_buffer(ctx, buf); - fz_output_pixmap_to_png(ctx, pix, out2, 0); - fz_close_output(out2); + fz_buffer *buf = fz_image_as_png(ctx, image->image, image->image->w, image->image->h); fz_printf(out, "image/png;base64,"); send_data_base64(out, buf); fz_drop_buffer(ctx, buf); - fz_drop_pixmap(ctx, pix); break; } } |