From f7098ecf6d5fccf6b3283d497e3c9b1f14470e1e Mon Sep 17 00:00:00 2001 From: Robin Watts Date: Thu, 10 Oct 2013 10:26:48 +0100 Subject: Add fz_new_png_from_pixmap This accompanies the function formerly known as fz_image_as_png (now renamed to fz_new_png_from_image). --- include/mupdf/fitz/output-png.h | 4 +++- source/fitz/pixmap.c | 32 +++++++++++++++++++++++++------- source/fitz/stext-output.c | 2 +- source/fitz/svg-device.c | 4 ++-- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/include/mupdf/fitz/output-png.h b/include/mupdf/fitz/output-png.h index 47a67c49..55c9a3fa 100644 --- a/include/mupdf/fitz/output-png.h +++ b/include/mupdf/fitz/output-png.h @@ -25,7 +25,9 @@ void fz_output_png(fz_output *out, const fz_pixmap *pixmap, 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_buffer *fz_new_png_from_image(fz_context *ctx, fz_image *image, int w, int h); + +fz_buffer *fz_new_png_from_pixmap(fz_context *ctx, fz_pixmap *pixmap); typedef struct fz_png_output_context_s fz_png_output_context; diff --git a/source/fitz/pixmap.c b/source/fitz/pixmap.c index 3c99d724..3f580c9e 100644 --- a/source/fitz/pixmap.c +++ b/source/fitz/pixmap.c @@ -804,24 +804,28 @@ fz_output_png_trailer(fz_output *out, fz_png_output_context *poc) putchunk("IEND", block, 0, out); } - -fz_buffer * -fz_image_as_png(fz_context *ctx, fz_image *image, int w, int h) +/* We use an auxiliary function to do pixmap_as_png, as it can enable us to + * drop pix early in the case where we have to convert, potentially saving + * us having to have 2 copies of the pixmap and a buffer open at once. */ +static fz_buffer * +png_from_pixmap(fz_context *ctx, fz_pixmap *pix, int drop) { - fz_pixmap *pix = fz_image_get_pixmap(ctx, image, image->w, image->h); fz_buffer *buf = NULL; fz_output *out; + fz_pixmap *pix2 = NULL; fz_var(buf); fz_var(out); + fz_var(pix2); fz_try(ctx) { if (pix->colorspace && pix->colorspace != fz_device_gray(ctx) && pix->colorspace != fz_device_rgb(ctx)) { - fz_pixmap *pix2 = fz_new_pixmap(ctx, fz_device_rgb(ctx), pix->w, pix->h); + pix2 = fz_new_pixmap(ctx, fz_device_rgb(ctx), pix->w, pix->h); fz_convert_pixmap(ctx, pix2, pix); - fz_drop_pixmap(ctx, pix); + if (drop) + fz_drop_pixmap(ctx, pix); pix = pix2; } buf = fz_new_buffer(ctx, 1024); @@ -830,8 +834,8 @@ fz_image_as_png(fz_context *ctx, fz_image *image, int w, int h) } fz_always(ctx) { + fz_drop_pixmap(ctx, drop ? pix : pix2); fz_close_output(out); - fz_drop_pixmap(ctx, pix); } fz_catch(ctx) { @@ -841,6 +845,20 @@ fz_image_as_png(fz_context *ctx, fz_image *image, int w, int h) return buf; } +fz_buffer * +fz_new_png_from_image(fz_context *ctx, fz_image *image, int w, int h) +{ + fz_pixmap *pix = fz_image_get_pixmap(ctx, image, image->w, image->h); + + return png_from_pixmap(ctx, pix, 1); +} + +fz_buffer * +fz_new_png_from_pixmap(fz_context *ctx, fz_pixmap *pix) +{ + return png_from_pixmap(ctx, pix, 0); +} + /* * Write pixmap to TGA file (with or without alpha channel) */ diff --git a/source/fitz/stext-output.c b/source/fitz/stext-output.c index d3241131..6ed595fc 100644 --- a/source/fitz/stext-output.c +++ b/source/fitz/stext-output.c @@ -258,7 +258,7 @@ fz_print_text_page_html(fz_context *ctx, fz_output *out, fz_text_page *page) break; default: { - fz_buffer *buf = fz_image_as_png(ctx, image->image, image->image->w, image->image->h); + fz_buffer *buf = fz_new_png_from_image(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); diff --git a/source/fitz/svg-device.c b/source/fitz/svg-device.c index 11194abc..d76f14b3 100644 --- a/source/fitz/svg-device.c +++ b/source/fitz/svg-device.c @@ -700,7 +700,7 @@ svg_dev_fill_image(fz_device *dev, fz_image *image, const fz_matrix *ctm, float break; default: { - fz_buffer *buf = fz_image_as_png(ctx, image, image->w, image->h); + fz_buffer *buf = fz_new_png_from_image(ctx, image, image->w, image->h); fz_printf(out, "image/png;base64,"); send_data_base64(out, buf); fz_drop_buffer(ctx, buf); @@ -742,7 +742,7 @@ fz_colorspace *colorspace, float *color, float alpha) break; default: { - fz_buffer *buf = fz_image_as_png(ctx, image, image->w, image->h); + fz_buffer *buf = fz_new_png_from_image(ctx, image, image->w, image->h); fz_printf(out, "image/png;base64,"); send_data_base64(out, buf); fz_drop_buffer(ctx, buf); -- cgit v1.2.3