Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7643 views
1
#include "mupdf/xps.h"
2
3
static fz_image *
4
xps_load_image(fz_context *ctx, xps_document *doc, xps_part *part)
5
{
6
/* Ownership of data always passes in here */
7
unsigned char *data = part->data;
8
part->data = NULL;
9
return fz_new_image_from_data(ctx, data, part->size);
10
}
11
12
/* FIXME: area unused! */
13
static void
14
xps_paint_image_brush(fz_context *ctx, xps_document *doc, const fz_matrix *ctm, const fz_rect *area, char *base_uri, xps_resource *dict,
15
fz_xml *root, void *vimage)
16
{
17
fz_image *image = vimage;
18
float xs, ys;
19
fz_matrix local_ctm = *ctm;
20
21
if (image->xres == 0 || image->yres == 0)
22
return;
23
xs = image->w * 96 / image->xres;
24
ys = image->h * 96 / image->yres;
25
fz_pre_scale(&local_ctm, xs, ys);
26
fz_fill_image(ctx, doc->dev, image, &local_ctm, doc->opacity[doc->opacity_top]);
27
}
28
29
static void
30
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)
31
{
32
char *image_source_att;
33
char buf[1024];
34
char partname[1024];
35
char *image_name;
36
char *profile_name;
37
char *p;
38
39
image_source_att = fz_xml_att(root, "ImageSource");
40
if (!image_source_att)
41
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot find image source attribute");
42
43
/* "{ColorConvertedBitmap /Resources/Image.tiff /Resources/Profile.icc}" */
44
if (strstr(image_source_att, "{ColorConvertedBitmap") == image_source_att)
45
{
46
image_name = NULL;
47
profile_name = NULL;
48
49
fz_strlcpy(buf, image_source_att, sizeof buf);
50
p = strchr(buf, ' ');
51
if (p)
52
{
53
image_name = p + 1;
54
p = strchr(p + 1, ' ');
55
if (p)
56
{
57
*p = 0;
58
profile_name = p + 1;
59
p = strchr(p + 1, '}');
60
if (p)
61
*p = 0;
62
}
63
}
64
}
65
else
66
{
67
image_name = image_source_att;
68
profile_name = NULL;
69
}
70
71
if (!image_name)
72
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot find image source");
73
74
if (image_part)
75
{
76
xps_resolve_url(ctx, doc, partname, base_uri, image_name, sizeof partname);
77
*image_part = xps_read_part(ctx, doc, partname);
78
}
79
80
if (profile_part)
81
{
82
if (profile_name)
83
{
84
xps_resolve_url(ctx, doc, partname, base_uri, profile_name, sizeof partname);
85
*profile_part = xps_read_part(ctx, doc, partname);
86
}
87
else
88
*profile_part = NULL;
89
}
90
}
91
92
void
93
xps_parse_image_brush(fz_context *ctx, xps_document *doc, const fz_matrix *ctm, const fz_rect *area,
94
char *base_uri, xps_resource *dict, fz_xml *root)
95
{
96
xps_part *part;
97
fz_image *image;
98
99
fz_try(ctx)
100
{
101
xps_find_image_brush_source_part(ctx, doc, base_uri, root, &part, NULL);
102
}
103
fz_catch(ctx)
104
{
105
fz_rethrow_if(ctx, FZ_ERROR_TRYLATER);
106
fz_warn(ctx, "cannot find image source");
107
return;
108
}
109
110
fz_try(ctx)
111
{
112
image = xps_load_image(ctx, doc, part);
113
image->invert_cmyk_jpeg = 1;
114
}
115
fz_always(ctx)
116
{
117
xps_drop_part(ctx, doc, part);
118
}
119
fz_catch(ctx)
120
{
121
fz_rethrow_if(ctx, FZ_ERROR_TRYLATER);
122
fz_warn(ctx, "cannot decode image resource");
123
return;
124
}
125
126
xps_parse_tiling_brush(ctx, doc, ctm, area, base_uri, dict, root, xps_paint_image_brush, image);
127
128
fz_drop_image(ctx, image);
129
}
130
131