1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
|
// Copyright (C) 2004-2022 Artifex Software, Inc.
//
// This file is part of MuPDF.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.
#include "mupdf/fitz.h"
#include <float.h>
fz_display_list *
fz_new_display_list_from_page(fz_context *ctx, fz_page *page)
{
fz_display_list *list;
fz_device *dev = NULL;
fz_var(dev);
list = fz_new_display_list(ctx, fz_bound_page(ctx, page));
fz_try(ctx)
{
dev = fz_new_list_device(ctx, list);
fz_run_page(ctx, page, dev, fz_identity, NULL);
fz_close_device(ctx, dev);
}
fz_always(ctx)
{
fz_drop_device(ctx, dev);
}
fz_catch(ctx)
{
fz_drop_display_list(ctx, list);
fz_rethrow(ctx);
}
return list;
}
fz_display_list *
fz_new_display_list_from_page_number(fz_context *ctx, fz_document *doc, int number)
{
fz_page *page;
fz_display_list *list = NULL;
page = fz_load_page(ctx, doc, number);
fz_try(ctx)
list = fz_new_display_list_from_page(ctx, page);
fz_always(ctx)
fz_drop_page(ctx, page);
fz_catch(ctx)
fz_rethrow(ctx);
return list;
}
fz_display_list *
fz_new_display_list_from_page_contents(fz_context *ctx, fz_page *page)
{
fz_display_list *list;
fz_device *dev = NULL;
fz_var(dev);
list = fz_new_display_list(ctx, fz_bound_page(ctx, page));
fz_try(ctx)
{
dev = fz_new_list_device(ctx, list);
fz_run_page_contents(ctx, page, dev, fz_identity, NULL);
fz_close_device(ctx, dev);
}
fz_always(ctx)
{
fz_drop_device(ctx, dev);
}
fz_catch(ctx)
{
fz_drop_display_list(ctx, list);
fz_rethrow(ctx);
}
return list;
}
fz_pixmap *
fz_new_pixmap_from_display_list(fz_context *ctx, fz_display_list *list, fz_matrix ctm, fz_colorspace *cs, int alpha)
{
return fz_new_pixmap_from_display_list_with_separations(ctx, list, ctm, cs, NULL, alpha);
}
fz_pixmap *
fz_new_pixmap_from_display_list_with_separations(fz_context *ctx, fz_display_list *list, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha)
{
fz_rect rect;
fz_irect bbox;
fz_pixmap *pix;
rect = fz_bound_display_list(ctx, list);
rect = fz_transform_rect(rect, ctm);
bbox = fz_round_rect(rect);
pix = fz_new_pixmap_with_bbox(ctx, cs, bbox, seps, alpha);
if (alpha)
fz_clear_pixmap(ctx, pix);
else
fz_clear_pixmap_with_value(ctx, pix, 0xFF);
return fz_fill_pixmap_from_display_list(ctx, list, ctm, pix);
}
fz_pixmap *
fz_fill_pixmap_from_display_list(fz_context *ctx, fz_display_list *list, fz_matrix ctm, fz_pixmap *pix)
{
fz_device *dev = NULL;
fz_var(dev);
fz_try(ctx)
{
dev = fz_new_draw_device(ctx, ctm, pix);
fz_run_display_list(ctx, list, dev, fz_identity, fz_infinite_rect, NULL);
fz_close_device(ctx, dev);
}
fz_always(ctx)
{
fz_drop_device(ctx, dev);
}
fz_catch(ctx)
{
fz_drop_pixmap(ctx, pix);
fz_rethrow(ctx);
}
return pix;
}
fz_pixmap *
fz_new_pixmap_from_page_contents(fz_context *ctx, fz_page *page, fz_matrix ctm, fz_colorspace *cs, int alpha)
{
return fz_new_pixmap_from_page_contents_with_separations(ctx, page, ctm, cs, NULL, alpha);
}
fz_pixmap *
fz_new_pixmap_from_page_contents_with_separations(fz_context *ctx, fz_page *page, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha)
{
fz_rect rect;
fz_irect bbox;
fz_pixmap *pix;
fz_device *dev = NULL;
fz_var(dev);
rect = fz_bound_page(ctx, page);
rect = fz_transform_rect(rect, ctm);
bbox = fz_round_rect(rect);
pix = fz_new_pixmap_with_bbox(ctx, cs, bbox, seps, alpha);
if (alpha)
fz_clear_pixmap(ctx, pix);
else
fz_clear_pixmap_with_value(ctx, pix, 0xFF);
fz_try(ctx)
{
dev = fz_new_draw_device(ctx, ctm, pix);
fz_run_page_contents(ctx, page, dev, fz_identity, NULL);
fz_close_device(ctx, dev);
}
fz_always(ctx)
{
fz_drop_device(ctx, dev);
}
fz_catch(ctx)
{
fz_drop_pixmap(ctx, pix);
fz_rethrow(ctx);
}
return pix;
}
fz_pixmap *
fz_new_pixmap_from_page(fz_context *ctx, fz_page *page, fz_matrix ctm, fz_colorspace *cs, int alpha)
{
return fz_new_pixmap_from_page_with_separations(ctx, page, ctm, cs, NULL, alpha);
}
fz_pixmap *
fz_new_pixmap_from_page_with_separations(fz_context *ctx, fz_page *page, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha)
{
fz_rect rect;
fz_irect bbox;
fz_pixmap *pix;
fz_device *dev = NULL;
fz_var(dev);
rect = fz_bound_page(ctx, page);
rect = fz_transform_rect(rect, ctm);
bbox = fz_round_rect(rect);
pix = fz_new_pixmap_with_bbox(ctx, cs, bbox, seps, alpha);
fz_try(ctx)
{
if (alpha)
fz_clear_pixmap(ctx, pix);
else
fz_clear_pixmap_with_value(ctx, pix, 0xFF);
dev = fz_new_draw_device(ctx, ctm, pix);
fz_run_page(ctx, page, dev, fz_identity, NULL);
fz_close_device(ctx, dev);
}
fz_always(ctx)
{
fz_drop_device(ctx, dev);
}
fz_catch(ctx)
{
fz_drop_pixmap(ctx, pix);
fz_rethrow(ctx);
}
return pix;
}
fz_pixmap *
fz_new_pixmap_from_page_number(fz_context *ctx, fz_document *doc, int number, fz_matrix ctm, fz_colorspace *cs, int alpha)
{
return fz_new_pixmap_from_page_number_with_separations(ctx, doc, number, ctm, cs, NULL, alpha);
}
fz_pixmap *
fz_new_pixmap_from_page_number_with_separations(fz_context *ctx, fz_document *doc, int number, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha)
{
fz_page *page;
fz_pixmap *pix = NULL;
page = fz_load_page(ctx, doc, number);
fz_try(ctx)
pix = fz_new_pixmap_from_page_with_separations(ctx, page, ctm, cs, seps, alpha);
fz_always(ctx)
fz_drop_page(ctx, page);
fz_catch(ctx)
fz_rethrow(ctx);
return pix;
}
fz_stext_page *
fz_new_stext_page_from_display_list(fz_context *ctx, fz_display_list *list, const fz_stext_options *options)
{
fz_stext_page *text;
fz_device *dev = NULL;
fz_var(dev);
if (list == NULL)
return NULL;
text = fz_new_stext_page(ctx, fz_bound_display_list(ctx, list));
fz_try(ctx)
{
dev = fz_new_stext_device(ctx, text, options);
fz_run_display_list(ctx, list, dev, fz_identity, fz_infinite_rect, NULL);
fz_close_device(ctx, dev);
}
fz_always(ctx)
{
fz_drop_device(ctx, dev);
}
fz_catch(ctx)
{
fz_drop_stext_page(ctx, text);
fz_rethrow(ctx);
}
return text;
}
fz_stext_page *
fz_new_stext_page_from_page(fz_context *ctx, fz_page *page, const fz_stext_options *options)
{
fz_stext_page *text;
fz_device *dev = NULL;
fz_var(dev);
if (page == NULL)
return NULL;
text = fz_new_stext_page(ctx, fz_bound_page(ctx, page));
fz_try(ctx)
{
dev = fz_new_stext_device(ctx, text, options);
fz_run_page_contents(ctx, page, dev, fz_identity, NULL);
fz_close_device(ctx, dev);
}
fz_always(ctx)
{
fz_drop_device(ctx, dev);
}
fz_catch(ctx)
{
fz_drop_stext_page(ctx, text);
fz_rethrow(ctx);
}
return text;
}
fz_stext_page *
fz_new_stext_page_from_page_number(fz_context *ctx, fz_document *doc, int number, const fz_stext_options *options)
{
fz_page *page;
fz_stext_page *text = NULL;
page = fz_load_page(ctx, doc, number);
fz_try(ctx)
text = fz_new_stext_page_from_page(ctx, page, options);
fz_always(ctx)
fz_drop_page(ctx, page);
fz_catch(ctx)
fz_rethrow(ctx);
return text;
}
fz_stext_page *
fz_new_stext_page_from_chapter_page_number(fz_context *ctx, fz_document *doc, int chapter, int number, const fz_stext_options *options)
{
fz_page *page;
fz_stext_page *text = NULL;
page = fz_load_chapter_page(ctx, doc, chapter, number);
fz_try(ctx)
text = fz_new_stext_page_from_page(ctx, page, options);
fz_always(ctx)
fz_drop_page(ctx, page);
fz_catch(ctx)
fz_rethrow(ctx);
return text;
}
int
fz_search_display_list(fz_context *ctx, fz_display_list *list, const char *needle, int *hit_mark, fz_quad *hit_bbox, int hit_max)
{
fz_stext_page *text;
int count = 0;
text = fz_new_stext_page_from_display_list(ctx, list, NULL);
fz_try(ctx)
count = fz_search_stext_page(ctx, text, needle, hit_mark, hit_bbox, hit_max);
fz_always(ctx)
fz_drop_stext_page(ctx, text);
fz_catch(ctx)
fz_rethrow(ctx);
return count;
}
int
fz_search_display_list_cb(fz_context *ctx, fz_display_list *list, const char *needle, fz_search_callback_fn *cb, void *opaque)
{
fz_stext_page *text;
int count = 0;
text = fz_new_stext_page_from_display_list(ctx, list, NULL);
fz_try(ctx)
count = fz_search_stext_page_cb(ctx, text, needle, cb, opaque);
fz_always(ctx)
fz_drop_stext_page(ctx, text);
fz_catch(ctx)
fz_rethrow(ctx);
return count;
}
int
fz_search_page(fz_context *ctx, fz_page *page, const char *needle, int *hit_mark, fz_quad *hit_bbox, int hit_max)
{
fz_stext_options opts = { FZ_STEXT_DEHYPHENATE };
fz_stext_page *text;
int count = 0;
text = fz_new_stext_page_from_page(ctx, page, &opts);
fz_try(ctx)
count = fz_search_stext_page(ctx, text, needle, hit_mark, hit_bbox, hit_max);
fz_always(ctx)
fz_drop_stext_page(ctx, text);
fz_catch(ctx)
fz_rethrow(ctx);
return count;
}
int
fz_search_page_cb(fz_context *ctx, fz_page *page, const char *needle, fz_search_callback_fn *cb, void *opaque)
{
fz_stext_options opts = { FZ_STEXT_DEHYPHENATE };
fz_stext_page *text;
int count = 0;
text = fz_new_stext_page_from_page(ctx, page, &opts);
fz_try(ctx)
count = fz_search_stext_page_cb(ctx, text, needle, cb, opaque);
fz_always(ctx)
fz_drop_stext_page(ctx, text);
fz_catch(ctx)
fz_rethrow(ctx);
return count;
}
int
fz_search_page_number(fz_context *ctx, fz_document *doc, int number, const char *needle, int *hit_mark, fz_quad *hit_bbox, int hit_max)
{
fz_page *page;
int count = 0;
page = fz_load_page(ctx, doc, number);
fz_try(ctx)
count = fz_search_page(ctx, page, needle, hit_mark, hit_bbox, hit_max);
fz_always(ctx)
fz_drop_page(ctx, page);
fz_catch(ctx)
fz_rethrow(ctx);
return count;
}
int
fz_search_page_number_cb(fz_context *ctx, fz_document *doc, int number, const char *needle, fz_search_callback_fn *cb, void *opaque)
{
fz_page *page;
int count = 0;
page = fz_load_page(ctx, doc, number);
fz_try(ctx)
count = fz_search_page_cb(ctx, page, needle, cb, opaque);
fz_always(ctx)
fz_drop_page(ctx, page);
fz_catch(ctx)
fz_rethrow(ctx);
return count;
}
int
fz_search_chapter_page_number(fz_context *ctx, fz_document *doc, int chapter, int number, const char *needle, int *hit_mark, fz_quad *hit_bbox, int hit_max)
{
fz_page *page;
int count = 0;
page = fz_load_chapter_page(ctx, doc, chapter, number);
fz_try(ctx)
count = fz_search_page(ctx, page, needle, hit_mark, hit_bbox, hit_max);
fz_always(ctx)
fz_drop_page(ctx, page);
fz_catch(ctx)
fz_rethrow(ctx);
return count;
}
int
fz_search_chapter_page_number_cb(fz_context *ctx, fz_document *doc, int chapter, int number, const char *needle, fz_search_callback_fn *cb, void *opaque)
{
fz_page *page;
int count = 0;
page = fz_load_chapter_page(ctx, doc, chapter, number);
fz_try(ctx)
count = fz_search_page_cb(ctx, page, needle, cb, opaque);
fz_always(ctx)
fz_drop_page(ctx, page);
fz_catch(ctx)
fz_rethrow(ctx);
return count;
}
fz_buffer *
fz_new_buffer_from_stext_page(fz_context *ctx, fz_stext_page *page)
{
fz_stext_block *block;
fz_stext_line *line;
fz_stext_char *ch;
fz_buffer *buf;
buf = fz_new_buffer(ctx, 256);
fz_try(ctx)
{
for (block = page->first_block; block; block = block->next)
{
if (block->type == FZ_STEXT_BLOCK_TEXT)
{
for (line = block->u.t.first_line; line; line = line->next)
{
for (ch = line->first_char; ch; ch = ch->next)
fz_append_rune(ctx, buf, ch->c);
fz_append_byte(ctx, buf, '\n');
}
fz_append_byte(ctx, buf, '\n');
}
}
}
fz_catch(ctx)
{
fz_drop_buffer(ctx, buf);
fz_rethrow(ctx);
}
return buf;
}
fz_buffer *
fz_new_buffer_from_display_list(fz_context *ctx, fz_display_list *list, const fz_stext_options *options)
{
fz_stext_page *text;
fz_buffer *buf = NULL;
text = fz_new_stext_page_from_display_list(ctx, list, options);
fz_try(ctx)
buf = fz_new_buffer_from_stext_page(ctx, text);
fz_always(ctx)
fz_drop_stext_page(ctx, text);
fz_catch(ctx)
fz_rethrow(ctx);
return buf;
}
fz_buffer *
fz_new_buffer_from_page(fz_context *ctx, fz_page *page, const fz_stext_options *options)
{
fz_stext_page *text;
fz_buffer *buf = NULL;
text = fz_new_stext_page_from_page(ctx, page, options);
fz_try(ctx)
buf = fz_new_buffer_from_stext_page(ctx, text);
fz_always(ctx)
fz_drop_stext_page(ctx, text);
fz_catch(ctx)
fz_rethrow(ctx);
return buf;
}
fz_buffer *
fz_new_buffer_from_page_number(fz_context *ctx, fz_document *doc, int number, const fz_stext_options *options)
{
fz_page *page;
fz_buffer *buf = NULL;
page = fz_load_page(ctx, doc, number);
fz_try(ctx)
buf = fz_new_buffer_from_page(ctx, page, options);
fz_always(ctx)
fz_drop_page(ctx, page);
fz_catch(ctx)
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;
cbuf = fz_compressed_image_buffer(ctx, image);
if (cbuf && cbuf->params.type == FZ_IMAGE_JPEG)
{
int type = fz_colorspace_type(ctx, image->colorspace);
if (type == FZ_COLORSPACE_GRAY || type == FZ_COLORSPACE_RGB)
{
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, "data:image/png;base64,");
fz_write_base64_buffer(ctx, out, cbuf->buffer, 1);
return;
}
buf = fz_new_buffer_from_image_as_png(ctx, image, fz_default_color_params);
fz_try(ctx)
{
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);
}
static uint32_t read16(const uint8_t *d, size_t *pos, size_t len, int order)
{
size_t p = *pos;
uint32_t v;
if (p+1 >= len)
{
*pos = len;
return 0;
}
if (order)
{
v = d[p++]<<8; /* BE */
v |= d[p++];
}
else
{
v = d[p++]; /* LE */
v |= d[p++]<<8;
}
*pos = p;
return v;
}
static uint32_t read32(const uint8_t *d, size_t *pos, size_t len, int order)
{
size_t p = *pos;
uint32_t v;
if (p+3 >= len)
{
*pos = len;
return 0;
}
if (order)
{
v = d[p++]<<24; /* BE */
v |= d[p++]<<16;
v |= d[p++]<<8;
v |= d[p++];
}
else
{
v = d[p++];
v |= d[p++]<<8; /* LE */
v |= d[p++]<<16;
v |= d[p++]<<24;
}
*pos = p;
return v;
}
static void write16(uint8_t *d, size_t *pos, size_t len, int order, uint32_t v)
{
size_t p = *pos;
if (p+1 >= len)
{
*pos = len;
return;
}
if (order)
{
d[p++] = (v>>8);
d[p++] = v;
}
else
{
d[p++] = v;
d[p++] = (v>>8);
}
*pos = p;
}
static void write32( uint8_t *d, size_t *pos, size_t len, int order, uint32_t v)
{
size_t p = *pos;
if (p+3 >= len)
{
*pos = len;
return;
}
if (order)
{
d[p++] = (v>>24);
d[p++] = (v>>16);
d[p++] = (v>>8);
d[p++] = v;
}
else
{
d[p++] = v;
d[p++] = (v>>8);
d[p++] = (v>>16);
d[p++] = (v>>24);
}
*pos = p;
}
fz_buffer *
fz_sanitize_jpeg_buffer(fz_context *ctx, fz_buffer *in)
{
fz_buffer *out = fz_clone_buffer(ctx, in);
size_t len = out->len;
size_t pos = 0;
uint8_t *d = out->data;
/* We need at least 4 data bytes. */
while (pos+4 < len)
{
uint8_t m;
/* We should be on a marker. If not, inch forwards until we are. */
if (d[pos++] != 0xff)
continue;
m = d[pos++];
if (m == 0xDA)
break; /* Start Of Scan. All our rewriting happens before this. */
if (m == 0xE1)
{
uint8_t order;
uint32_t tmp;
size_t body_start;
/* APP1 tag. This is where the EXIF data lives. */
/* Read and discard the marker length. We're not continuing after this anyway. */
(void)read16(d, &pos, len, 0);
tmp = read32(d, &pos, len, 0);
if (tmp != 0x66697845) /* Exif */
break; /* Not exif - nothing to rewrite. */
tmp = read16(d, &pos, len, 0);
if (tmp != 0) /* Terminator + Pad */
break; /* Not exif - nothing to rewrite. */
/* Now we're at the APP1 Body. */
body_start = pos;
tmp = read16(d, &pos, len, 0);
if (tmp == 0x4949)
order = 0; /* LE */
else if (tmp == 0x4d4d)
order = 1; /* BE */
else
break; /* Bad TIFF type. Bale. */
tmp = read16(d, &pos, len, order);
if (tmp != 0x002a) /* 42 */
break; /* Bad version field. Bale. */
do
{
uint32_t i, n;
tmp = read32(d, &pos, len, order);
pos = body_start + tmp;
if (tmp == 0 || pos >= len)
break;
n = read16(d, &pos, len, order);
for (i = 0; i < n; i++)
{
if (read16(d, &pos, len, order) == 0x112)
{
/* Orientation tag! */
write16(d, &pos, len, order, 3); /* 3 = short */
write32(d, &pos, len, order, 1); /* Count = 1 */
write16(d, &pos, len, order, 1); /* Value = 1 */
write16(d, &pos, len, order, 0); /* padding */
i = n;
pos = len; /* Done! */
}
else
pos += 10;
}
}
while (pos+4 < len);
break;
}
else if (m >= 0xD0 && m <= 0xD7)
{
/* RSTm - no length code. But we shouldn't hit this! */
}
else if (m == 0x01)
{
/* TEM - temporary private use in arithmetic coding - shouldn't hit this either. */
}
else if (m == 0xD8)
{
/* SOI - start of image. */
}
else if (m == 0x01)
{
/* EOI - end of image. */
}
else
{
/* All other markers have a length. */
size_t marker_len = d[pos]*256 + d[pos+1];
pos += marker_len; /* The 2 length bytes are included in the marker_len */
}
}
return out;
}
void
fz_append_image_as_data_uri(fz_context *ctx, fz_buffer *out, fz_image *image)
{
fz_compressed_buffer *cbuf;
fz_buffer *buf;
cbuf = fz_compressed_image_buffer(ctx, image);
if (cbuf && cbuf->params.type == FZ_IMAGE_JPEG)
{
int type = fz_colorspace_type(ctx, image->colorspace);
if (type == FZ_COLORSPACE_GRAY || type == FZ_COLORSPACE_RGB)
{
fz_buffer *new_buf = fz_sanitize_jpeg_buffer(ctx, cbuf->buffer);
fz_append_string(ctx, out, "data:image/jpeg;base64,");
fz_try(ctx)
fz_append_base64_buffer(ctx, out, new_buf, 1);
fz_always(ctx)
fz_drop_buffer(ctx, new_buf);
fz_catch(ctx)
fz_rethrow(ctx);
return;
}
}
if (cbuf && cbuf->params.type == FZ_IMAGE_PNG)
{
fz_append_string(ctx, out, "data:image/png;base64,");
fz_append_base64_buffer(ctx, out, cbuf->buffer, 1);
return;
}
buf = fz_new_buffer_from_image_as_png(ctx, image, fz_default_color_params);
fz_try(ctx)
{
fz_append_string(ctx, out, "data:image/png;base64,");
fz_append_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, fz_default_color_params);
fz_try(ctx)
{
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_append_pixmap_as_data_uri(fz_context *ctx, fz_buffer *out, fz_pixmap *pixmap)
{
fz_buffer *buf = fz_new_buffer_from_pixmap_as_png(ctx, pixmap, fz_default_color_params);
fz_try(ctx)
{
fz_append_string(ctx, out, "data:image/png;base64,");
fz_append_base64_buffer(ctx, out, buf, 1);
}
fz_always(ctx)
fz_drop_buffer(ctx, buf);
fz_catch(ctx)
fz_rethrow(ctx);
}
fz_document *
fz_new_xhtml_document_from_document(fz_context *ctx, fz_document *old_doc, const fz_stext_options *opts)
{
fz_stext_options default_opts = { FZ_STEXT_PRESERVE_IMAGES | FZ_STEXT_DEHYPHENATE };
fz_document *new_doc;
fz_buffer *buf = NULL;
fz_output *out = NULL;
fz_stream *stm = NULL;
fz_stext_page *text = NULL;
int i;
fz_var(buf);
fz_var(out);
fz_var(stm);
fz_var(text);
if (!opts)
opts = &default_opts;
fz_try(ctx)
{
buf = fz_new_buffer(ctx, 8192);
out = fz_new_output_with_buffer(ctx, buf);
fz_print_stext_header_as_xhtml(ctx, out);
for (i = 0; i < fz_count_pages(ctx, old_doc); ++i)
{
text = fz_new_stext_page_from_page_number(ctx, old_doc, i, opts);
fz_print_stext_page_as_xhtml(ctx, out, text, i+1);
fz_drop_stext_page(ctx, text);
text = NULL;
}
fz_print_stext_trailer_as_xhtml(ctx, out);
fz_close_output(ctx, out);
fz_terminate_buffer(ctx, buf);
stm = fz_open_buffer(ctx, buf);
new_doc = fz_open_document_with_stream(ctx, "application/xhtml+xml", stm);
}
fz_always(ctx)
{
fz_drop_stream(ctx, stm);
fz_drop_buffer(ctx, buf);
fz_drop_output(ctx, out);
fz_drop_stext_page(ctx, text);
}
fz_catch(ctx)
fz_rethrow(ctx);
return new_doc;
}
fz_buffer *
fz_new_buffer_from_page_with_format(fz_context *ctx, fz_page *page, const char *format, const char *options, fz_matrix transform, fz_cookie *cookie)
{
fz_buffer *buf = NULL;
fz_output *out;
fz_document_writer *writer = NULL;
fz_device *dev = NULL;
fz_var(buf);
fz_var(writer);
fz_var(dev);
fz_try(ctx)
{
buf = fz_new_buffer(ctx, 0);
out = fz_new_output_with_buffer(ctx, buf);
writer = fz_new_document_writer_with_output(ctx, out, format, options);
dev = fz_begin_page(ctx, writer, fz_bound_page(ctx, page));
fz_run_page(ctx, page, dev, transform, cookie);
fz_end_page(ctx, writer);
fz_close_document_writer(ctx, writer);
}
fz_always(ctx)
fz_drop_document_writer(ctx, writer);
fz_catch(ctx)
{
fz_drop_buffer(ctx, buf);
fz_rethrow(ctx);
}
return buf;
}
|