summaryrefslogtreecommitdiff
path: root/source/fitz
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2017-08-01 14:11:01 +0200
committerTor Andersson <tor.andersson@artifex.com>2017-08-09 14:02:19 +0200
commita644f7e67af05dca8601e379442690c724030738 (patch)
tree6e92f987323842dc397f48980852615f204138f7 /source/fitz
parent7f97f80d1a74a50d369b3bdf513de7fbcaed6bc3 (diff)
downloadmupdf-a644f7e67af05dca8601e379442690c724030738.tar.xz
Add common fz_write_image_as_data_uri function for HTML and SVG output.
Also ensure we don't write CMYK JPEG images.
Diffstat (limited to 'source/fitz')
-rw-r--r--source/fitz/stext-output.c58
-rw-r--r--source/fitz/svg-device.c30
-rw-r--r--source/fitz/util.c35
3 files changed, 54 insertions, 69 deletions
diff --git a/source/fitz/stext-output.c b/source/fitz/stext-output.c
index 8a769615..83018c3c 100644
--- a/source/fitz/stext-output.c
+++ b/source/fitz/stext-output.c
@@ -54,51 +54,16 @@ fz_print_style_end_html(fz_context *ctx, fz_output *out, fz_stext_style *style)
}
static void
-fz_print_stext_image_as_html(fz_context *ctx, fz_output *out, fz_image_block *block, int xhtml)
+fz_print_stext_image_as_html(fz_context *ctx, fz_output *out, fz_image_block *block)
{
- fz_compressed_buffer *cbuf;
- fz_buffer *buf;
-
int x = block->bbox.x0;
int y = block->bbox.y0;
int w = block->bbox.x1 - block->bbox.x0;
int h = block->bbox.y1 - block->bbox.y0;
- cbuf = fz_compressed_image_buffer(ctx, block->image);
-
- if (xhtml)
- fz_write_printf(ctx, out, "<img width=\"%d\" height=\"%d\" src=\"data:", w, h);
- else
- fz_write_printf(ctx, out, "<img style=\"top:%dpt;left:%dpt;width:%dpt;height:%dpt\" src=\"data:", y, x, w, h);
-
- switch (cbuf == NULL ? FZ_IMAGE_UNKNOWN : cbuf->params.type)
- {
- case FZ_IMAGE_JPEG:
- fz_write_string(ctx, out, "image/jpeg;base64,");
- fz_write_base64_buffer(ctx, out, cbuf->buffer, 1);
- break;
- case FZ_IMAGE_PNG:
- fz_write_string(ctx, out, "image/png;base64,");
- fz_write_base64_buffer(ctx, out, cbuf->buffer, 1);
- break;
- default:
- buf = fz_new_buffer_from_image_as_png(ctx, block->image, NULL);
- fz_try(ctx)
- {
- fz_write_string(ctx, out, "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);
- break;
- }
-
- if (xhtml)
- fz_write_string(ctx, out, "\"/>\n");
- else
- fz_write_string(ctx, out, "\">\n");
+ fz_write_printf(ctx, out, "<img style=\"top:%dpt;left:%dpt;width:%dpt;height:%dpt\" src=\"data:", y, x, w, h);
+ fz_write_image_as_data_uri(ctx, out, block->image);
+ fz_write_string(ctx, out, "\">\n");
}
void
@@ -178,7 +143,7 @@ fz_print_stext_page_as_html(fz_context *ctx, fz_output *out, fz_stext_page *page
for (block = page->blocks; block < page->blocks + page->len; ++block)
{
if (block->type == FZ_PAGE_BLOCK_IMAGE)
- fz_print_stext_image_as_html(ctx, out, block->u.image, 0);
+ fz_print_stext_image_as_html(ctx, out, block->u.image);
else if (block->type == FZ_PAGE_BLOCK_TEXT)
fz_print_stext_block_as_html(ctx, out, block->u.text);
}
@@ -212,6 +177,17 @@ fz_print_stext_trailer_as_html(fz_context *ctx, fz_output *out)
/* XHTML output (semantic, little layout, suitable for reflow) */
static void
+fz_print_stext_image_as_xhtml(fz_context *ctx, fz_output *out, fz_image_block *block)
+{
+ int w = block->bbox.x1 - block->bbox.x0;
+ int h = block->bbox.y1 - block->bbox.y0;
+
+ fz_write_printf(ctx, out, "<img width=\"%d\" height=\"%d\" src=\"data:", w, h);
+ fz_write_image_as_data_uri(ctx, out, block->image);
+ fz_write_string(ctx, out, "\"/>\n");
+}
+
+static void
fz_print_style_begin_xhtml(fz_context *ctx, fz_output *out, fz_stext_style *style)
{
int is_mono = fz_font_is_monospaced(ctx, style->font);
@@ -315,7 +291,7 @@ fz_print_stext_page_as_xhtml(fz_context *ctx, fz_output *out, fz_stext_page *pag
for (block = page->blocks; block < page->blocks + page->len; ++block)
{
if (block->type == FZ_PAGE_BLOCK_IMAGE)
- fz_print_stext_image_as_html(ctx, out, block->u.image, 1);
+ fz_print_stext_image_as_xhtml(ctx, out, block->u.image);
else if (block->type == FZ_PAGE_BLOCK_TEXT)
fz_print_stext_block_as_xhtml(ctx, out, block->u.text);
}
diff --git a/source/fitz/svg-device.c b/source/fitz/svg-device.c
index 4279761c..7098e0a0 100644
--- a/source/fitz/svg-device.c
+++ b/source/fitz/svg-device.c
@@ -828,7 +828,6 @@ static void
svg_send_image(fz_context *ctx, svg_device *sdev, fz_image *img, const fz_color_params *color_params)
{
fz_output *out = sdev->out;
- fz_compressed_buffer *buffer;
int i;
int id;
@@ -859,33 +858,8 @@ svg_send_image(fz_context *ctx, svg_device *sdev, fz_image *img, const fz_color_
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");
- buffer = fz_compressed_image_buffer(ctx, img);
- fz_write_printf(ctx, out, " width=\"%d\" height=\"%d\" xlink:href=\"data:", img->w, img->h);
- switch (buffer == NULL ? FZ_IMAGE_JPX : buffer->params.type)
- {
- case FZ_IMAGE_PNG:
- fz_write_printf(ctx, out, "image/png;base64,");
- fz_write_base64_buffer(ctx, out, buffer->buffer, 1);
- break;
- case FZ_IMAGE_JPEG:
- /* SVG cannot cope with CMYK images */
- if (img->colorspace != fz_device_cmyk(ctx))
- {
- fz_write_printf(ctx, out, "image/jpeg;base64,");
- fz_write_base64_buffer(ctx, out, buffer->buffer, 1);
- break;
- }
- /*@fallthough@*/
- default:
- {
- fz_buffer *buf = fz_new_buffer_from_image_as_png(ctx, img, color_params);
- fz_write_printf(ctx, out, "image/png;base64,");
- fz_write_base64_buffer(ctx, out, buf, 1);
- fz_drop_buffer(ctx, buf);
- break;
- }
- }
+ fz_write_printf(ctx, out, "<image width=\"%d\" height=\"%d\" xlink:href=\"data:", img->w, img->h);
+ fz_write_image_as_data_uri(ctx, out, img);
fz_write_printf(ctx, out, "\"/>\n");
if (sdev->reuse_images)
diff --git a/source/fitz/util.c b/source/fitz/util.c
index d532734a..6f900174 100644
--- a/source/fitz/util.c
+++ b/source/fitz/util.c
@@ -548,3 +548,38 @@ fz_new_buffer_from_page_number(fz_context *ctx, fz_document *doc, int number, co
fz_rethrow(ctx);
return buf;
}
+
+void
+fz_write_image_as_data_uri(fz_context *ctx, fz_output *out, fz_image *image)
+{
+ fz_compressed_buffer *cbuf;
+ fz_buffer *buf;
+ int n;
+
+ n = fz_colorspace_n(ctx, image->colorspace);
+ cbuf = fz_compressed_image_buffer(ctx, image);
+
+ if (cbuf && cbuf->params.type == FZ_IMAGE_JPEG && (n == 1 || n == 3))
+ {
+ fz_write_string(ctx, out, "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_base64_buffer(ctx, out, cbuf->buffer, 1);
+ return;
+ }
+
+ buf = fz_new_buffer_from_image_as_png(ctx, image, NULL);
+ fz_try(ctx)
+ {
+ fz_write_string(ctx, out, "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);
+}