Coverage Report

Created: 2025-01-11 06:55

/src/mupdf/source/xps/xps-image.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright (C) 2004-2021 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
0
{
31
0
  return fz_new_image_from_buffer(ctx, part->data);
32
0
}
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
0
{
39
0
  fz_image *image = vimage;
40
0
  float xs, ys;
41
42
0
  if (image->xres == 0 || image->yres == 0)
43
0
    return;
44
0
  xs = image->w * 96 / image->xres;
45
0
  ys = image->h * 96 / image->yres;
46
0
  ctm = fz_pre_scale(ctm, xs, ys);
47
0
  fz_fill_image(ctx, doc->dev, image, ctm, doc->opacity[doc->opacity_top], fz_default_color_params);
48
0
}
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
0
{
53
0
  char *image_source_att;
54
0
  char buf[1024];
55
0
  char partname[1024];
56
0
  char *image_name;
57
0
  char *profile_name;
58
0
  char *p;
59
60
0
  image_source_att = fz_xml_att(root, "ImageSource");
61
0
  if (!image_source_att)
62
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "cannot find image source attribute");
63
64
  /* "{ColorConvertedBitmap /Resources/Image.tiff /Resources/Profile.icc}" */
65
0
  if (strstr(image_source_att, "{ColorConvertedBitmap") == image_source_att)
66
0
  {
67
0
    image_name = NULL;
68
0
    profile_name = NULL;
69
70
0
    fz_strlcpy(buf, image_source_att, sizeof buf);
71
0
    p = strchr(buf, ' ');
72
0
    if (p)
73
0
    {
74
0
      image_name = p + 1;
75
0
      p = strchr(p + 1, ' ');
76
0
      if (p)
77
0
      {
78
0
        *p = 0;
79
0
        profile_name = p + 1;
80
0
        p = strchr(p + 1, '}');
81
0
        if (p)
82
0
          *p = 0;
83
0
      }
84
0
    }
85
0
  }
86
0
  else
87
0
  {
88
0
    image_name = image_source_att;
89
0
    profile_name = NULL;
90
0
  }
91
92
0
  if (!image_name)
93
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "cannot find image source");
94
95
0
  if (image_part)
96
0
  {
97
0
    xps_resolve_url(ctx, doc, partname, base_uri, image_name, sizeof partname);
98
0
    *image_part = xps_read_part(ctx, doc, partname);
99
0
  }
100
101
0
  if (profile_part)
102
0
  {
103
0
    if (profile_name)
104
0
    {
105
0
      xps_resolve_url(ctx, doc, partname, base_uri, profile_name, sizeof partname);
106
0
      *profile_part = xps_read_part(ctx, doc, partname);
107
0
    }
108
0
    else
109
0
      *profile_part = NULL;
110
0
  }
111
0
}
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
0
{
117
0
  xps_part *part = NULL;
118
0
  fz_image *image = NULL;
119
120
0
  fz_try(ctx)
121
0
  {
122
0
    xps_find_image_brush_source_part(ctx, doc, base_uri, root, &part, NULL);
123
0
  }
124
0
  fz_catch(ctx)
125
0
  {
126
0
    if (fz_caught(ctx) == FZ_ERROR_TRYLATER)
127
0
    {
128
0
      if (doc->cookie)
129
0
      {
130
0
        doc->cookie->incomplete = 1;
131
0
        fz_ignore_error(ctx);
132
0
      }
133
0
      else
134
0
        fz_rethrow(ctx);
135
0
    }
136
0
    else
137
0
    {
138
0
      fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
139
0
      fz_report_error(ctx);
140
0
      fz_warn(ctx, "cannot find image source");
141
0
    }
142
0
    return;
143
0
  }
144
145
0
  fz_try(ctx)
146
0
  {
147
0
    image = xps_load_image(ctx, doc, part);
148
0
  }
149
0
  fz_always(ctx)
150
0
  {
151
0
    xps_drop_part(ctx, doc, part);
152
0
  }
153
0
  fz_catch(ctx)
154
0
  {
155
0
    fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
156
0
    fz_report_error(ctx);
157
0
    fz_warn(ctx, "cannot decode image resource");
158
0
    return;
159
0
  }
160
161
0
  fz_try(ctx)
162
0
    xps_parse_tiling_brush(ctx, doc, ctm, area, base_uri, dict, root, xps_paint_image_brush, image);
163
0
  fz_always(ctx)
164
0
    fz_drop_image(ctx, image);
165
0
  fz_catch(ctx)
166
0
    fz_rethrow(ctx);
167
0
}