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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/*
* Copyright © 2008 Chris Wilson
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without
* fee, provided that the above copyright notice appear in all copies
* and that both that copyright notice and this permission notice
* appear in supporting documentation, and that the name of
* Chris Wilson not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission. Chris Wilson makes no representations about the
* suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "cairo-test.h"
#include <stdio.h>
#include <errno.h>
/* Basic test to exercise the new mime-data embedding. */
static cairo_status_t
read_file (const cairo_test_context_t *ctx,
const char *filename,
unsigned char **data_out,
unsigned int *length_out)
{
FILE *file;
unsigned char *buf;
unsigned int len;
file = fopen (filename, "rb");
if (file == NULL) {
char path[4096];
if (errno == ENOMEM)
return CAIRO_STATUS_NO_MEMORY;
/* try again with srcdir */
snprintf (path, sizeof (path),
"%s/%s", ctx->srcdir, filename);
file = fopen (path, "rb");
}
if (file == NULL) {
switch (errno) {
case ENOMEM:
return CAIRO_STATUS_NO_MEMORY;
default:
return CAIRO_STATUS_FILE_NOT_FOUND;
}
}
fseek (file, 0, SEEK_END);
len = ftell (file);
fseek (file, 0, SEEK_SET);
buf = xmalloc (len);
*length_out = fread (buf, 1, len, file);
fclose (file);
if (*length_out != len) {
free (buf);
return CAIRO_STATUS_READ_ERROR;
}
*data_out = buf;
return CAIRO_STATUS_SUCCESS;
}
static cairo_test_status_t
paint_file (cairo_t *cr,
const char *filename, const char *mime_type,
int x, int y)
{
const cairo_test_context_t *ctx = cairo_test_get_context (cr);
cairo_surface_t *image;
unsigned char *mime_data;
unsigned int mime_length;
cairo_status_t status;
/* Deliberately use a non-matching MIME images, so that we can identify
* when the MIME representation is used in preference to the plain image
* surface.
*/
status = read_file (ctx, filename, &mime_data, &mime_length);
if (status)
return cairo_test_status_from_status (ctx, status);
image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 200, 50);
status = cairo_surface_set_mime_data (image, mime_type,
mime_data, mime_length,
free, mime_data);
if (status) {
cairo_surface_destroy (image);
free (mime_data);
return cairo_test_status_from_status (ctx, status);
}
cairo_set_source_surface (cr, image, x, y);
cairo_surface_destroy (image);
cairo_paint (cr);
return CAIRO_TEST_SUCCESS;
}
static cairo_test_status_t
paint_jbig2_file (cairo_t *cr, int x, int y)
{
const cairo_test_context_t *ctx = cairo_test_get_context (cr);
cairo_surface_t *image;
unsigned char *mime_data;
unsigned int mime_length;
cairo_status_t status;
const char jbig2_image1_filename[] = "image1.jb2";
const char jbig2_image2_filename[] = "image2.jb2";
const char jbig2_global_filename[] = "global.jb2";
/* Deliberately use a non-matching MIME images, so that we can identify
* when the MIME representation is used in preference to the plain image
* surface.
*/
/* Image 1 */
status = read_file (ctx, jbig2_image1_filename, &mime_data, &mime_length);
if (status)
return cairo_test_status_from_status (ctx, status);
image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 200, 50);
status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID,
(unsigned char *)"global", 6, NULL, NULL);
if (status) {
cairo_surface_destroy (image);
return cairo_test_status_from_status (ctx, status);
}
status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2,
mime_data, mime_length,
free, mime_data);
if (status) {
cairo_surface_destroy (image);
free (mime_data);
return cairo_test_status_from_status (ctx, status);
}
cairo_set_source_surface (cr, image, x, y);
cairo_surface_destroy (image);
cairo_paint (cr);
/* Image 2 */
status = read_file (ctx, jbig2_image2_filename, &mime_data, &mime_length);
if (status)
return cairo_test_status_from_status (ctx, status);
image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 200, 50);
status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID,
(unsigned char *)"global", 6, NULL, NULL);
if (status) {
cairo_surface_destroy (image);
return cairo_test_status_from_status (ctx, status);
}
status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2,
mime_data, mime_length,
free, mime_data);
if (status) {
cairo_surface_destroy (image);
free (mime_data);
return cairo_test_status_from_status (ctx, status);
}
/* Set the global data */
status = read_file (ctx, jbig2_global_filename, &mime_data, &mime_length);
if (status)
return cairo_test_status_from_status (ctx, status);
status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2_GLOBAL,
mime_data, mime_length,
free, mime_data);
if (status) {
cairo_surface_destroy (image);
free (mime_data);
return cairo_test_status_from_status (ctx, status);
}
cairo_set_source_surface (cr, image, x, y + 50);
cairo_surface_destroy (image);
cairo_paint (cr);
return CAIRO_TEST_SUCCESS;
}
static cairo_test_status_t
paint_ccitt_file (cairo_t *cr, int x, int y)
{
const cairo_test_context_t *ctx = cairo_test_get_context (cr);
cairo_surface_t *image;
unsigned char *mime_data;
unsigned int mime_length;
cairo_status_t status;
const char *ccitt_image_filename = "ccitt.g3";
const char *ccitt_image_params = "Columns=200 Rows=50 K=-1";
/* Deliberately use a non-matching MIME images, so that we can identify
* when the MIME representation is used in preference to the plain image
* surface.
*/
status = read_file (ctx, ccitt_image_filename, &mime_data, &mime_length);
if (status)
return cairo_test_status_from_status (ctx, status);
image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 200, 50);
/* Set the CCITT image data */
status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_CCITT_FAX,
mime_data, mime_length,
free, mime_data);
if (status) {
cairo_surface_destroy (image);
free (mime_data);
return cairo_test_status_from_status (ctx, status);
}
/* Set the CCITT image parameters */
status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_CCITT_FAX_PARAMS,
(unsigned char *)ccitt_image_params,
strlen (ccitt_image_params),
NULL, NULL);
if (status) {
cairo_surface_destroy (image);
return cairo_test_status_from_status (ctx, status);
}
cairo_set_source_surface (cr, image, x, y);
cairo_surface_destroy (image);
cairo_paint (cr);
return CAIRO_TEST_SUCCESS;
}
static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
const char jpg_filename[] = "jpeg.jpg";
const char png_filename[] = "png.png";
const char jp2_filename[] = "jp2.jp2";
cairo_test_status_t status;
status = paint_file (cr, jpg_filename, CAIRO_MIME_TYPE_JPEG, 0, 0);
if (status)
return status;
status = paint_file (cr, png_filename, CAIRO_MIME_TYPE_PNG, 0, 50);
if (status)
return status;
status = paint_file (cr, jp2_filename, CAIRO_MIME_TYPE_JP2, 0, 100);
if (status)
return status;
status = paint_jbig2_file (cr, 0, 150);
if (status)
return status;
status = paint_ccitt_file (cr, 0, 250);
if (status)
return status;
return CAIRO_TEST_SUCCESS;
}
CAIRO_TEST (mime_data,
"Check that the mime-data embedding works",
"jpeg, api", /* keywords */
NULL, /* requirements */
200, 300,
NULL, draw)
|