summaryrefslogtreecommitdiff
path: root/xps/xps_image.c
blob: 1f46a7567d1b6ab138e7cd84537c2c0986784aac (plain)
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
#include "muxps-internal.h"

typedef struct xps_image_s xps_image;

struct xps_image_s
{
	fz_image base;
	fz_pixmap *pix;
	int xres;
	int yres;
};

static void
xps_free_image(fz_context *ctx, fz_storable *image_)
{
	xps_image *image = (xps_image *)image_;

	if (image == NULL)
		return;

	fz_drop_colorspace(ctx, image->base.colorspace);
	fz_drop_pixmap(ctx, image->pix);
	fz_free(ctx, image);
}

static fz_pixmap *
xps_image_to_pixmap(fz_context *ctx, fz_image *image_, int x, int w)
{
	xps_image *image = (xps_image *)image_;

	return fz_keep_pixmap(ctx, image->pix);
}

static fz_image *
xps_load_image(fz_context *ctx, byte *buf, int len)
{
	fz_pixmap *pix;
	xps_image *image;

	if (len < 8)
		fz_throw(ctx, "unknown image file format");

	if (buf[0] == 0xff && buf[1] == 0xd8)
		pix = fz_load_jpeg(ctx, buf, len);
	else if (memcmp(buf, "\211PNG\r\n\032\n", 8) == 0)
		pix = fz_load_png(ctx, buf, len);
	else if (memcmp(buf, "II", 2) == 0 && buf[2] == 0xBC)
		fz_throw(ctx, "JPEG-XR codec is not available");
	else if (memcmp(buf, "MM", 2) == 0 || memcmp(buf, "II", 2) == 0)
		pix = fz_load_tiff(ctx, buf, len);
	else
		fz_throw(ctx, "unknown image file format");

	fz_try(ctx)
	{
		image = fz_malloc_struct(ctx, xps_image);

		FZ_INIT_STORABLE(&image->base, 1, xps_free_image);
		image->base.w = pix->w;
		image->base.h = pix->h;
		image->base.mask = NULL;
		image->base.colorspace = pix->colorspace;
		image->base.get_pixmap = xps_image_to_pixmap;
		image->xres = pix->xres;
		image->yres = pix->yres;
		image->pix = pix;
	}
	fz_catch(ctx)
	{
		fz_drop_pixmap(ctx, pix);
		fz_rethrow(ctx);
	}

	return &image->base;
}

static void
xps_paint_image_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict,
	xml_element *root, void *vimage)
{
	xps_image *image = vimage;
	float xs, ys;

	if (image->xres == 0 || image->yres == 0)
		return;
	xs = image->base.w * 96 / image->xres;
	ys = image->base.h * 96 / image->yres;
	ctm = fz_concat(fz_scale(xs, ys), ctm);
	fz_fill_image(doc->dev, &image->base, ctm, doc->opacity[doc->opacity_top]);
}

static xps_part *
xps_find_image_brush_source_part(xps_document *doc, char *base_uri, xml_element *root)
{
	char *image_source_att;
	char buf[1024];
	char partname[1024];
	char *image_name;
	char *profile_name;
	char *p;

	image_source_att = xml_att(root, "ImageSource");
	if (!image_source_att)
		fz_throw(doc->ctx, "cannot find image source attribute");

	/* "{ColorConvertedBitmap /Resources/Image.tiff /Resources/Profile.icc}" */
	if (strstr(image_source_att, "{ColorConvertedBitmap") == image_source_att)
	{
		image_name = NULL;
		profile_name = NULL;

		fz_strlcpy(buf, image_source_att, sizeof buf);
		p = strchr(buf, ' ');
		if (p)
		{
			image_name = p + 1;
			p = strchr(p + 1, ' ');
			if (p)
			{
				*p = 0;
				profile_name = p + 1;
				p = strchr(p + 1, '}');
				if (p)
					*p = 0;
			}
		}
	}
	else
	{
		image_name = image_source_att;
		profile_name = NULL;
	}

	if (!image_name)
		fz_throw(doc->ctx, "cannot find image source");

	xps_resolve_url(partname, base_uri, image_name, sizeof partname);

	return xps_read_part(doc, partname);
}

void
xps_parse_image_brush(xps_document *doc, fz_matrix ctm, fz_rect area,
	char *base_uri, xps_resource *dict, xml_element *root)
{
	xps_part *part;
	fz_image *image;

	fz_try(doc->ctx)
	{
		part = xps_find_image_brush_source_part(doc, base_uri, root);
	}
	fz_catch(doc->ctx)
	{
		fz_warn(doc->ctx, "cannot find image source");
		return;
	}

	fz_try(doc->ctx)
	{
		image = xps_load_image(doc->ctx, part->data, part->size);
	}
	fz_catch(doc->ctx)
	{
		fz_warn(doc->ctx, "cannot decode image resource");
		xps_free_part(doc, part);
		return;
	}
	xps_free_part(doc, part);

	xps_parse_tiling_brush(doc, ctm, area, base_uri, dict, root, xps_paint_image_brush, image);

	fz_drop_image(doc->ctx, image);
}