diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2018-10-10 16:48:00 +0200 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2018-10-23 17:23:59 +0200 |
commit | ddd00f62888c908c84932c2a92a0c2d195b26c36 (patch) | |
tree | 4b49633f15741f34919506e5c69f85b4b8b9b803 | |
parent | 7b7b4de12bc50f97bd8fd32403319c6d1a758a90 (diff) | |
download | mupdf-ddd00f62888c908c84932c2a92a0c2d195b26c36.tar.xz |
Add fz_write_pixmap_as_data_uri helper function.
Also include "data:" schema in the data uri for fz_write_image_as_data_uri.
-rw-r--r-- | include/mupdf/fitz/util.h | 1 | ||||
-rw-r--r-- | source/fitz/stext-output.c | 4 | ||||
-rw-r--r-- | source/fitz/svg-device.c | 13 | ||||
-rw-r--r-- | source/fitz/util.c | 21 |
4 files changed, 25 insertions, 14 deletions
diff --git a/include/mupdf/fitz/util.h b/include/mupdf/fitz/util.h index 87595017..4f60aae7 100644 --- a/include/mupdf/fitz/util.h +++ b/include/mupdf/fitz/util.h @@ -74,5 +74,6 @@ fz_image *fz_new_image_from_svg(fz_context *ctx, fz_buffer *buf); Write image as a data URI (for HTML and SVG output). */ void fz_write_image_as_data_uri(fz_context *ctx, fz_output *out, fz_image *image); +void fz_write_pixmap_as_data_uri(fz_context *ctx, fz_output *out, fz_pixmap *pixmap); #endif diff --git a/source/fitz/stext-output.c b/source/fitz/stext-output.c index f60ce993..428cb7ab 100644 --- a/source/fitz/stext-output.c +++ b/source/fitz/stext-output.c @@ -80,7 +80,7 @@ fz_print_stext_image_as_html(fz_context *ctx, fz_output *out, fz_stext_block *bl int w = block->bbox.x1 - block->bbox.x0; int h = block->bbox.y1 - block->bbox.y0; - fz_write_printf(ctx, out, "<img style=\"position:absolute;top:%dpt;left:%dpt;width:%dpt;height:%dpt\" src=\"data:", y, x, w, h); + fz_write_printf(ctx, out, "<img style=\"position:absolute;top:%dpt;left:%dpt;width:%dpt;height:%dpt\" src=\"", y, x, w, h); fz_write_image_as_data_uri(ctx, out, block->u.i.image); fz_write_string(ctx, out, "\">\n"); } @@ -190,7 +190,7 @@ fz_print_stext_image_as_xhtml(fz_context *ctx, fz_output *out, fz_stext_block *b int w = block->bbox.x1 - block->bbox.x0; int h = block->bbox.y1 - block->bbox.y0; - fz_write_printf(ctx, out, "<p><img width=\"%d\" height=\"%d\" src=\"data:", w, h); + fz_write_printf(ctx, out, "<p><img width=\"%d\" height=\"%d\" src=\"", w, h); fz_write_image_as_data_uri(ctx, out, block->u.i.image); fz_write_string(ctx, out, "\"/></p>\n"); } diff --git a/source/fitz/svg-device.c b/source/fitz/svg-device.c index 7d2c7ee3..2876a89b 100644 --- a/source/fitz/svg-device.c +++ b/source/fitz/svg-device.c @@ -855,7 +855,7 @@ svg_send_image(fz_context *ctx, svg_device *sdev, fz_image *img, const fz_color_ out = start_def(ctx, sdev); fz_write_printf(ctx, out, "<symbol id=\"im%d\" viewBox=\"0 0 %d %d\">\n", id, img->w, img->h); - fz_write_printf(ctx, out, "<image width=\"%d\" height=\"%d\" xlink:href=\"data:", img->w, img->h); + fz_write_printf(ctx, out, "<image width=\"%d\" height=\"%d\" xlink:href=\"", img->w, img->h); fz_write_image_as_data_uri(ctx, out, img); fz_write_printf(ctx, out, "\"/>\n"); @@ -871,7 +871,7 @@ svg_send_image(fz_context *ctx, svg_device *sdev, fz_image *img, const fz_color_ } else { - fz_write_printf(ctx, out, "<image width=\"%d\" height=\"%d\" xlink:href=\"data:", img->w, img->h); + fz_write_printf(ctx, out, "<image width=\"%d\" height=\"%d\" xlink:href=\"", img->w, img->h); fz_write_image_as_data_uri(ctx, out, img); fz_write_printf(ctx, out, "\"/>\n"); } @@ -906,9 +906,6 @@ svg_dev_fill_shade(fz_context *ctx, fz_device *dev, fz_shade *shade, fz_matrix c fz_output *out = sdev->out; fz_irect bbox; fz_pixmap *pix; - fz_buffer *buf = NULL; - - fz_var(buf); if (dev->container_len == 0) return; @@ -922,18 +919,16 @@ svg_dev_fill_shade(fz_context *ctx, fz_device *dev, fz_shade *shade, fz_matrix c fz_try(ctx) { fz_paint_shade(ctx, shade, NULL, ctm, pix, color_params, bbox, NULL); - buf = fz_new_buffer_from_pixmap_as_png(ctx, pix, color_params); if (alpha != 1.0f) fz_write_printf(ctx, out, "<g opacity=\"%g\">\n", alpha); - fz_write_printf(ctx, out, "<image x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" xlink:href=\"data:image/png;base64,", pix->x, pix->y, pix->w, pix->h); - fz_write_base64_buffer(ctx, out, buf, 1); + fz_write_printf(ctx, out, "<image x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" xlink:href=\"", pix->x, pix->y, pix->w, pix->h); + fz_write_pixmap_as_data_uri(ctx, out, pix); fz_write_printf(ctx, out, "\"/>\n"); if (alpha != 1.0f) fz_write_printf(ctx, out, "</g>\n"); } fz_always(ctx) { - fz_drop_buffer(ctx, buf); fz_drop_pixmap(ctx, pix); } fz_catch(ctx) diff --git a/source/fitz/util.c b/source/fitz/util.c index d78a2981..e89cb419 100644 --- a/source/fitz/util.c +++ b/source/fitz/util.c @@ -527,14 +527,14 @@ fz_write_image_as_data_uri(fz_context *ctx, fz_output *out, fz_image *image) int type = fz_colorspace_type(ctx, image->colorspace); if (type == FZ_COLORSPACE_GRAY || type == FZ_COLORSPACE_RGB) { - fz_write_string(ctx, out, "image/jpeg;base64,"); + fz_write_string(ctx, out, "data:image/jpeg;base64,"); fz_write_base64_buffer(ctx, out, cbuf->buffer, 1); return; } } if (cbuf && cbuf->params.type == FZ_IMAGE_PNG) { - fz_write_string(ctx, out, "image/png;base64,"); + fz_write_string(ctx, out, "data:image/png;base64,"); fz_write_base64_buffer(ctx, out, cbuf->buffer, 1); return; } @@ -542,7 +542,22 @@ fz_write_image_as_data_uri(fz_context *ctx, fz_output *out, fz_image *image) buf = fz_new_buffer_from_image_as_png(ctx, image, NULL); fz_try(ctx) { - fz_write_string(ctx, out, "image/png;base64,"); + fz_write_string(ctx, out, "data:image/png;base64,"); + fz_write_base64_buffer(ctx, out, buf, 1); + } + fz_always(ctx) + fz_drop_buffer(ctx, buf); + fz_catch(ctx) + fz_rethrow(ctx); +} + +void +fz_write_pixmap_as_data_uri(fz_context *ctx, fz_output *out, fz_pixmap *pixmap) +{ + fz_buffer *buf = fz_new_buffer_from_pixmap_as_png(ctx, pixmap, NULL); + fz_try(ctx) + { + fz_write_string(ctx, out, "data:image/png;base64,"); fz_write_base64_buffer(ctx, out, buf, 1); } fz_always(ctx) |