xps-image.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "xps-imp.h"
  24. #include <string.h>
  25. static fz_image *
  26. xps_load_image(fz_context *ctx, xps_document *doc, xps_part *part)
  27. {
  28. return fz_new_image_from_buffer(ctx, part->data);
  29. }
  30. /* FIXME: area unused! */
  31. static void
  32. xps_paint_image_brush(fz_context *ctx, xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict,
  33. fz_xml *root, void *vimage)
  34. {
  35. fz_image *image = vimage;
  36. float xs, ys;
  37. if (image->xres == 0 || image->yres == 0)
  38. return;
  39. xs = image->w * 96 / image->xres;
  40. ys = image->h * 96 / image->yres;
  41. ctm = fz_pre_scale(ctm, xs, ys);
  42. fz_fill_image(ctx, doc->dev, image, ctm, doc->opacity[doc->opacity_top], fz_default_color_params);
  43. }
  44. static void
  45. xps_find_image_brush_source_part(fz_context *ctx, xps_document *doc, char *base_uri, fz_xml *root, xps_part **image_part, xps_part **profile_part)
  46. {
  47. char *image_source_att;
  48. char buf[1024];
  49. char partname[1024];
  50. char *image_name;
  51. char *profile_name;
  52. char *p;
  53. image_source_att = fz_xml_att(root, "ImageSource");
  54. if (!image_source_att)
  55. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot find image source attribute");
  56. /* "{ColorConvertedBitmap /Resources/Image.tiff /Resources/Profile.icc}" */
  57. if (strstr(image_source_att, "{ColorConvertedBitmap") == image_source_att)
  58. {
  59. image_name = NULL;
  60. profile_name = NULL;
  61. fz_strlcpy(buf, image_source_att, sizeof buf);
  62. p = strchr(buf, ' ');
  63. if (p)
  64. {
  65. image_name = p + 1;
  66. p = strchr(p + 1, ' ');
  67. if (p)
  68. {
  69. *p = 0;
  70. profile_name = p + 1;
  71. p = strchr(p + 1, '}');
  72. if (p)
  73. *p = 0;
  74. }
  75. }
  76. }
  77. else
  78. {
  79. image_name = image_source_att;
  80. profile_name = NULL;
  81. }
  82. if (!image_name)
  83. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot find image source");
  84. if (image_part)
  85. {
  86. xps_resolve_url(ctx, doc, partname, base_uri, image_name, sizeof partname);
  87. *image_part = xps_read_part(ctx, doc, partname);
  88. }
  89. if (profile_part)
  90. {
  91. if (profile_name)
  92. {
  93. xps_resolve_url(ctx, doc, partname, base_uri, profile_name, sizeof partname);
  94. *profile_part = xps_read_part(ctx, doc, partname);
  95. }
  96. else
  97. *profile_part = NULL;
  98. }
  99. }
  100. void
  101. xps_parse_image_brush(fz_context *ctx, xps_document *doc, fz_matrix ctm, fz_rect area,
  102. char *base_uri, xps_resource *dict, fz_xml *root)
  103. {
  104. xps_part *part = NULL;
  105. fz_image *image = NULL;
  106. fz_var(image);
  107. fz_try(ctx)
  108. {
  109. xps_find_image_brush_source_part(ctx, doc, base_uri, root, &part, NULL);
  110. }
  111. fz_catch(ctx)
  112. {
  113. if (fz_caught(ctx) == FZ_ERROR_TRYLATER)
  114. {
  115. if (doc->cookie)
  116. {
  117. doc->cookie->incomplete = 1;
  118. fz_ignore_error(ctx);
  119. }
  120. else
  121. fz_rethrow(ctx);
  122. }
  123. else
  124. {
  125. fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
  126. fz_report_error(ctx);
  127. fz_warn(ctx, "cannot find image source");
  128. }
  129. return;
  130. }
  131. fz_try(ctx)
  132. {
  133. image = xps_load_image(ctx, doc, part);
  134. xps_parse_tiling_brush(ctx, doc, ctm, area, base_uri, dict, root, xps_paint_image_brush, image);
  135. }
  136. fz_always(ctx)
  137. {
  138. xps_drop_part(ctx, doc, part);
  139. fz_drop_image(ctx, image);
  140. }
  141. fz_catch(ctx)
  142. {
  143. fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
  144. fz_report_error(ctx);
  145. fz_warn(ctx, "cannot decode image resource");
  146. return;
  147. }
  148. }