Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/xps/xps-image.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 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 | |
| 23 #include "mupdf/fitz.h" | |
| 24 #include "xps-imp.h" | |
| 25 | |
| 26 #include <string.h> | |
| 27 | |
| 28 static fz_image * | |
| 29 xps_load_image(fz_context *ctx, xps_document *doc, xps_part *part) | |
| 30 { | |
| 31 return fz_new_image_from_buffer(ctx, part->data); | |
| 32 } | |
| 33 | |
| 34 /* FIXME: area unused! */ | |
| 35 static void | |
| 36 xps_paint_image_brush(fz_context *ctx, xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, | |
| 37 fz_xml *root, void *vimage) | |
| 38 { | |
| 39 fz_image *image = vimage; | |
| 40 float xs, ys; | |
| 41 | |
| 42 if (image->xres == 0 || image->yres == 0) | |
| 43 return; | |
| 44 xs = image->w * 96 / image->xres; | |
| 45 ys = image->h * 96 / image->yres; | |
| 46 ctm = fz_pre_scale(ctm, xs, ys); | |
| 47 fz_fill_image(ctx, doc->dev, image, ctm, doc->opacity[doc->opacity_top], fz_default_color_params); | |
| 48 } | |
| 49 | |
| 50 static void | |
| 51 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) | |
| 52 { | |
| 53 char *image_source_att; | |
| 54 char buf[1024]; | |
| 55 char partname[1024]; | |
| 56 char *image_name; | |
| 57 char *profile_name; | |
| 58 char *p; | |
| 59 | |
| 60 image_source_att = fz_xml_att(root, "ImageSource"); | |
| 61 if (!image_source_att) | |
| 62 fz_throw(ctx, FZ_ERROR_FORMAT, "cannot find image source attribute"); | |
| 63 | |
| 64 /* "{ColorConvertedBitmap /Resources/Image.tiff /Resources/Profile.icc}" */ | |
| 65 if (strstr(image_source_att, "{ColorConvertedBitmap") == image_source_att) | |
| 66 { | |
| 67 image_name = NULL; | |
| 68 profile_name = NULL; | |
| 69 | |
| 70 fz_strlcpy(buf, image_source_att, sizeof buf); | |
| 71 p = strchr(buf, ' '); | |
| 72 if (p) | |
| 73 { | |
| 74 image_name = p + 1; | |
| 75 p = strchr(p + 1, ' '); | |
| 76 if (p) | |
| 77 { | |
| 78 *p = 0; | |
| 79 profile_name = p + 1; | |
| 80 p = strchr(p + 1, '}'); | |
| 81 if (p) | |
| 82 *p = 0; | |
| 83 } | |
| 84 } | |
| 85 } | |
| 86 else | |
| 87 { | |
| 88 image_name = image_source_att; | |
| 89 profile_name = NULL; | |
| 90 } | |
| 91 | |
| 92 if (!image_name) | |
| 93 fz_throw(ctx, FZ_ERROR_FORMAT, "cannot find image source"); | |
| 94 | |
| 95 if (image_part) | |
| 96 { | |
| 97 xps_resolve_url(ctx, doc, partname, base_uri, image_name, sizeof partname); | |
| 98 *image_part = xps_read_part(ctx, doc, partname); | |
| 99 } | |
| 100 | |
| 101 if (profile_part) | |
| 102 { | |
| 103 if (profile_name) | |
| 104 { | |
| 105 xps_resolve_url(ctx, doc, partname, base_uri, profile_name, sizeof partname); | |
| 106 *profile_part = xps_read_part(ctx, doc, partname); | |
| 107 } | |
| 108 else | |
| 109 *profile_part = NULL; | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void | |
| 114 xps_parse_image_brush(fz_context *ctx, xps_document *doc, fz_matrix ctm, fz_rect area, | |
| 115 char *base_uri, xps_resource *dict, fz_xml *root) | |
| 116 { | |
| 117 xps_part *part = NULL; | |
| 118 fz_image *image = NULL; | |
| 119 | |
| 120 fz_var(image); | |
| 121 | |
| 122 fz_try(ctx) | |
| 123 { | |
| 124 xps_find_image_brush_source_part(ctx, doc, base_uri, root, &part, NULL); | |
| 125 } | |
| 126 fz_catch(ctx) | |
| 127 { | |
| 128 if (fz_caught(ctx) == FZ_ERROR_TRYLATER) | |
| 129 { | |
| 130 if (doc->cookie) | |
| 131 { | |
| 132 doc->cookie->incomplete = 1; | |
| 133 fz_ignore_error(ctx); | |
| 134 } | |
| 135 else | |
| 136 fz_rethrow(ctx); | |
| 137 } | |
| 138 else | |
| 139 { | |
| 140 fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); | |
| 141 fz_report_error(ctx); | |
| 142 fz_warn(ctx, "cannot find image source"); | |
| 143 } | |
| 144 return; | |
| 145 } | |
| 146 | |
| 147 fz_try(ctx) | |
| 148 { | |
| 149 image = xps_load_image(ctx, doc, part); | |
| 150 xps_parse_tiling_brush(ctx, doc, ctm, area, base_uri, dict, root, xps_paint_image_brush, image); | |
| 151 } | |
| 152 fz_always(ctx) | |
| 153 { | |
| 154 xps_drop_part(ctx, doc, part); | |
| 155 fz_drop_image(ctx, image); | |
| 156 } | |
| 157 fz_catch(ctx) | |
| 158 { | |
| 159 fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); | |
| 160 fz_report_error(ctx); | |
| 161 fz_warn(ctx, "cannot decode image resource"); | |
| 162 return; | |
| 163 } | |
| 164 } |
