2 * Copyright © 2008 Chris Wilson
4 * Permission to use, copy, modify, distribute, and sell this software
5 * and its documentation for any purpose is hereby granted without
6 * fee, provided that the above copyright notice appear in all copies
7 * and that both that copyright notice and this permission notice
8 * appear in supporting documentation, and that the name of
9 * Chris Wilson not be used in advertising or publicity pertaining to
10 * distribution of the software without specific, written prior
11 * permission. Chris Wilson makes no representations about the
12 * suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
18 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Author: Chris Wilson <chris@chris-wilson.co.uk>
26 #include "cairo-test.h"
31 /* Basic test to exercise the new mime-data embedding. */
34 read_file (const cairo_test_context_t *ctx,
36 unsigned char **data_out,
37 unsigned int *length_out)
43 file = fopen (filename, "rb");
48 return CAIRO_STATUS_NO_MEMORY;
50 /* try again with srcdir */
51 snprintf (path, sizeof (path),
52 "%s/%s", ctx->srcdir, filename);
53 file = fopen (path, "rb");
58 return CAIRO_STATUS_NO_MEMORY;
60 return CAIRO_STATUS_FILE_NOT_FOUND;
64 fseek (file, 0, SEEK_END);
66 fseek (file, 0, SEEK_SET);
69 *length_out = fread (buf, 1, len, file);
71 if (*length_out != len) {
73 return CAIRO_STATUS_READ_ERROR;
77 return CAIRO_STATUS_SUCCESS;
80 static cairo_test_status_t
81 paint_file (cairo_t *cr,
82 const char *filename, const char *mime_type,
85 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
86 cairo_surface_t *image;
87 unsigned char *mime_data;
88 unsigned int mime_length;
89 cairo_status_t status;
91 /* Deliberately use a non-matching MIME images, so that we can identify
92 * when the MIME representation is used in preference to the plain image
95 status = read_file (ctx, filename, &mime_data, &mime_length);
97 return cairo_test_status_from_status (ctx, status);
99 image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 200, 50);
101 status = cairo_surface_set_mime_data (image, mime_type,
102 mime_data, mime_length,
105 cairo_surface_destroy (image);
107 return cairo_test_status_from_status (ctx, status);
110 cairo_set_source_surface (cr, image, x, y);
111 cairo_surface_destroy (image);
115 return CAIRO_TEST_SUCCESS;
118 static cairo_test_status_t
119 paint_jbig2_file (cairo_t *cr, int x, int y)
121 const cairo_test_context_t *ctx = cairo_test_get_context (cr);
122 cairo_surface_t *image;
123 unsigned char *mime_data;
124 unsigned int mime_length;
125 cairo_status_t status;
126 const char jbig2_image1_filename[] = "image1.jb2";
127 const char jbig2_image2_filename[] = "image2.jb2";
128 const char jbig2_global_filename[] = "global.jb2";
130 /* Deliberately use a non-matching MIME images, so that we can identify
131 * when the MIME representation is used in preference to the plain image
137 status = read_file (ctx, jbig2_image1_filename, &mime_data, &mime_length);
139 return cairo_test_status_from_status (ctx, status);
141 image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 200, 50);
143 status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID,
144 (unsigned char *)"global", 6, NULL, NULL);
146 cairo_surface_destroy (image);
147 return cairo_test_status_from_status (ctx, status);
150 status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2,
151 mime_data, mime_length,
154 cairo_surface_destroy (image);
156 return cairo_test_status_from_status (ctx, status);
159 cairo_set_source_surface (cr, image, x, y);
160 cairo_surface_destroy (image);
166 status = read_file (ctx, jbig2_image2_filename, &mime_data, &mime_length);
168 return cairo_test_status_from_status (ctx, status);
170 image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 200, 50);
172 status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID,
173 (unsigned char *)"global", 6, NULL, NULL);
175 cairo_surface_destroy (image);
176 return cairo_test_status_from_status (ctx, status);
179 status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2,
180 mime_data, mime_length,
183 cairo_surface_destroy (image);
185 return cairo_test_status_from_status (ctx, status);
188 /* Set the global data */
189 status = read_file (ctx, jbig2_global_filename, &mime_data, &mime_length);
191 return cairo_test_status_from_status (ctx, status);
193 status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2_GLOBAL,
194 mime_data, mime_length,
197 cairo_surface_destroy (image);
199 return cairo_test_status_from_status (ctx, status);
202 cairo_set_source_surface (cr, image, x, y + 50);
203 cairo_surface_destroy (image);
206 return CAIRO_TEST_SUCCESS;
209 static cairo_test_status_t
210 draw (cairo_t *cr, int width, int height)
212 const char jpg_filename[] = "jpeg.jpg";
213 const char png_filename[] = "png.png";
214 const char jp2_filename[] = "jp2.jp2";
215 cairo_test_status_t status;
217 status = paint_file (cr, jpg_filename, CAIRO_MIME_TYPE_JPEG, 0, 0);
221 status = paint_file (cr, png_filename, CAIRO_MIME_TYPE_PNG, 0, 50);
225 status = paint_file (cr, jp2_filename, CAIRO_MIME_TYPE_JP2, 0, 100);
229 status = paint_jbig2_file (cr, 0, 150);
233 return CAIRO_TEST_SUCCESS;
236 CAIRO_TEST (mime_data,
237 "Check that the mime-data embedding works",
238 "jpeg, api", /* keywords */
239 NULL, /* requirements */