diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2017-07-26 15:58:33 +0200 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2017-07-27 14:44:04 +0200 |
commit | 44cff76eb82cf11097e4e01348240e3556e22a7f (patch) | |
tree | 9289a3eea90f621846a6c31d0ae82bb6394a0789 /source/fitz | |
parent | 1840cf480522ee03e22a0f36b76a85dde62c4c8f (diff) | |
download | mupdf-44cff76eb82cf11097e4e01348240e3556e22a7f.tar.xz |
Add fz_write_base64 function.
Diffstat (limited to 'source/fitz')
-rw-r--r-- | source/fitz/output.c | 44 | ||||
-rw-r--r-- | source/fitz/stext-output.c | 44 | ||||
-rw-r--r-- | source/fitz/svg-device.c | 46 |
3 files changed, 51 insertions, 83 deletions
diff --git a/source/fitz/output.c b/source/fitz/output.c index c9f04b96..75872ab2 100644 --- a/source/fitz/output.c +++ b/source/fitz/output.c @@ -373,6 +373,50 @@ fz_write_rune(fz_context *ctx, fz_output *out, int rune) } void +fz_write_base64(fz_context *ctx, fz_output *out, const unsigned char *data, int size, int newline) +{ + static const char set[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + int i; + for (i = 0; i + 3 < size; i += 3) + { + int c = data[i]; + int d = data[i+1]; + int e = data[i+2]; + if (newline && (i & 15) == 0) + fz_write_byte(ctx, out, '\n'); + fz_write_byte(ctx, out, set[c>>2]); + fz_write_byte(ctx, out, set[((c&3)<<4)|(d>>4)]); + fz_write_byte(ctx, out, set[((d&15)<<2)|(e>>6)]); + fz_write_byte(ctx, out, set[e&63]); + } + if (size - i == 2) + { + int c = data[i]; + int d = data[i+1]; + fz_write_byte(ctx, out, set[c>>2]); + fz_write_byte(ctx, out, set[((c&3)<<4)|(d>>4)]); + fz_write_byte(ctx, out, set[((d&15)<<2)]); + fz_write_byte(ctx, out, '='); + } + else if (size - i == 1) + { + int c = data[i]; + fz_write_byte(ctx, out, set[c>>2]); + fz_write_byte(ctx, out, set[((c&3)<<4)]); + fz_write_byte(ctx, out, '='); + fz_write_byte(ctx, out, '='); + } +} + +void +fz_write_base64_buffer(fz_context *ctx, fz_output *out, fz_buffer *buf, int newline) +{ + unsigned char *data; + size_t size = fz_buffer_storage(ctx, buf, &data); + fz_write_base64(ctx, out, data, size, newline); +} + +void fz_save_buffer(fz_context *ctx, fz_buffer *buf, const char *filename) { fz_output *out = fz_new_output_with_path(ctx, filename, 0); diff --git a/source/fitz/stext-output.c b/source/fitz/stext-output.c index a247fc8d..d2fe2d69 100644 --- a/source/fitz/stext-output.c +++ b/source/fitz/stext-output.c @@ -53,44 +53,6 @@ fz_print_stext_sheet(fz_context *ctx, fz_output *out, fz_stext_sheet *sheet) fz_print_style(ctx, out, style); } -static void -send_data_base64_stext(fz_context *ctx, fz_output *out, fz_buffer *buffer) -{ - size_t i, len; - static const char set[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - - len = buffer->len/3; - for (i = 0; i < len; i++) - { - int c = buffer->data[3*i]; - int d = buffer->data[3*i+1]; - int e = buffer->data[3*i+2]; - if ((i & 15) == 0) - fz_write_printf(ctx, out, "\n"); - fz_write_printf(ctx, out, "%c%c%c%c", set[c>>2], set[((c&3)<<4)|(d>>4)], set[((d&15)<<2)|(e>>6)], set[e & 63]); - } - i *= 3; - switch (buffer->len-i) - { - case 2: - { - int c = buffer->data[i]; - int d = buffer->data[i+1]; - fz_write_printf(ctx, out, "%c%c%c=", set[c>>2], set[((c&3)<<4)|(d>>4)], set[((d&15)<<2)]); - break; - } - case 1: - { - int c = buffer->data[i]; - fz_write_printf(ctx, out, "%c%c==", set[c>>2], set[(c&3)<<4]); - break; - } - default: - case 0: - break; - } -} - void fz_print_stext_page_html(fz_context *ctx, fz_output *out, fz_stext_page *page) { @@ -232,17 +194,17 @@ fz_print_stext_page_html(fz_context *ctx, fz_output *out, fz_stext_page *page) { case FZ_IMAGE_JPEG: fz_write_printf(ctx, out, "image/jpeg;base64,"); - send_data_base64_stext(ctx, out, buffer->buffer); + fz_write_base64_buffer(ctx, out, buffer->buffer, 1); break; case FZ_IMAGE_PNG: fz_write_printf(ctx, out, "image/png;base64,"); - send_data_base64_stext(ctx, out, buffer->buffer); + fz_write_base64_buffer(ctx, out, buffer->buffer, 1); break; default: { fz_buffer *buf = fz_new_buffer_from_image_as_png(ctx, image->image, NULL); fz_write_printf(ctx, out, "image/png;base64,"); - send_data_base64_stext(ctx, out, buf); + fz_write_base64_buffer(ctx, out, buf, 1); fz_drop_buffer(ctx, buf); break; } diff --git a/source/fitz/svg-device.c b/source/fitz/svg-device.c index 036c8563..4279761c 100644 --- a/source/fitz/svg-device.c +++ b/source/fitz/svg-device.c @@ -819,44 +819,6 @@ svg_dev_ignore_text(fz_context *ctx, fz_device *dev, const fz_text *text, const } } -static void -send_data_base64(fz_context *ctx, fz_output *out, fz_buffer *buffer) -{ - size_t i, len; - static const char set[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - - len = buffer->len/3; - for (i = 0; i < len; i++) - { - int c = buffer->data[3*i]; - int d = buffer->data[3*i+1]; - int e = buffer->data[3*i+2]; - if ((i & 15) == 0) - fz_write_printf(ctx, out, "\n"); - fz_write_printf(ctx, out, "%c%c%c%c", set[c>>2], set[((c&3)<<4)|(d>>4)], set[((d&15)<<2)|(e>>6)], set[e & 63]); - } - i *= 3; - switch (buffer->len-i) - { - case 2: - { - int c = buffer->data[i]; - int d = buffer->data[i+1]; - fz_write_printf(ctx, out, "%c%c%c=", set[c>>2], set[((c&3)<<4)|(d>>4)], set[((d&15)<<2)]); - break; - } - case 1: - { - int c = buffer->data[i]; - fz_write_printf(ctx, out, "%c%c==", set[c>>2], set[(c&3)<<4]); - break; - } - default: - case 0: - break; - } -} - /* We spot repeated images, and send them just once using * symbols. Unfortunately, for pathological files, such * as the example in Bug695988, this can cause viewers to @@ -904,14 +866,14 @@ svg_send_image(fz_context *ctx, svg_device *sdev, fz_image *img, const fz_color_ { case FZ_IMAGE_PNG: fz_write_printf(ctx, out, "image/png;base64,"); - send_data_base64(ctx, out, buffer->buffer); + 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,"); - send_data_base64(ctx, out, buffer->buffer); + fz_write_base64_buffer(ctx, out, buffer->buffer, 1); break; } /*@fallthough@*/ @@ -919,7 +881,7 @@ svg_send_image(fz_context *ctx, svg_device *sdev, fz_image *img, const fz_color_ { fz_buffer *buf = fz_new_buffer_from_image_as_png(ctx, img, color_params); fz_write_printf(ctx, out, "image/png;base64,"); - send_data_base64(ctx, out, buf); + fz_write_base64_buffer(ctx, out, buf, 1); fz_drop_buffer(ctx, buf); break; } @@ -990,7 +952,7 @@ svg_dev_fill_shade(fz_context *ctx, fz_device *dev, fz_shade *shade, const fz_ma 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); - send_data_base64(ctx, out, buf); + fz_write_base64_buffer(ctx, out, buf, 1); fz_write_printf(ctx, out, "\"/>\n"); if (alpha != 1.0f) fz_write_printf(ctx, out, "</g>\n"); |