Opensource Compliance Issue.
[platform/core/graphics/cairo.git] / test / mime-data.c
1 /*
2  * Copyright © 2008 Chris Wilson
3  *
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.
14  *
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.
22  *
23  * Author: Chris Wilson <chris@chris-wilson.co.uk>
24  */
25
26 #include "cairo-test.h"
27
28 #include <stdio.h>
29 #include <errno.h>
30
31 /* Basic test to exercise the new mime-data embedding. */
32
33 static cairo_status_t
34 read_file (const cairo_test_context_t *ctx,
35            const char *filename,
36            unsigned char **data_out,
37            unsigned int *length_out)
38 {
39     FILE *file;
40     unsigned char *buf;
41     unsigned int len;
42
43     file = fopen (filename, "rb");
44     if (file == NULL) {
45         char path[4096];
46
47         if (errno == ENOMEM)
48             return CAIRO_STATUS_NO_MEMORY;
49
50         /* try again with srcdir */
51         snprintf (path, sizeof (path),
52                   "%s/%s", ctx->srcdir, filename);
53         file = fopen (path, "rb");
54     }
55     if (file == NULL) {
56         switch (errno) {
57         case ENOMEM:
58             return CAIRO_STATUS_NO_MEMORY;
59         default:
60             return CAIRO_STATUS_FILE_NOT_FOUND;
61         }
62     }
63
64     fseek (file, 0, SEEK_END);
65     len = ftell (file);
66     fseek (file, 0, SEEK_SET);
67
68     buf = xmalloc (len);
69     *length_out = fread (buf, 1, len, file);
70     fclose (file);
71     if (*length_out != len) {
72         free (buf);
73         return CAIRO_STATUS_READ_ERROR;
74     }
75
76     *data_out = buf;
77     return CAIRO_STATUS_SUCCESS;
78 }
79
80 static cairo_test_status_t
81 paint_file (cairo_t *cr,
82             const char *filename, const char *mime_type,
83             int x, int y)
84 {
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;
90
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
93      * surface.
94      */
95     status = read_file (ctx, filename, &mime_data, &mime_length);
96     if (status)
97         return cairo_test_status_from_status (ctx, status);
98
99     image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 200, 50);
100
101     status = cairo_surface_set_mime_data (image, mime_type,
102                                           mime_data, mime_length,
103                                           free, mime_data);
104     if (status) {
105         cairo_surface_destroy (image);
106         free (mime_data);
107         return cairo_test_status_from_status (ctx, status);
108     }
109
110     cairo_set_source_surface (cr, image, x, y);
111     cairo_surface_destroy (image);
112
113     cairo_paint (cr);
114
115     return CAIRO_TEST_SUCCESS;
116 }
117
118 static cairo_test_status_t
119 paint_jbig2_file (cairo_t *cr, int x, int y)
120 {
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";
129
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
132      * surface.
133      */
134
135     /* Image 1 */
136
137     status = read_file (ctx, jbig2_image1_filename, &mime_data, &mime_length);
138     if (status)
139         return cairo_test_status_from_status (ctx, status);
140
141     image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 200, 50);
142
143     status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID,
144                                           (unsigned char *)"global", 6, NULL, NULL);
145     if (status) {
146         cairo_surface_destroy (image);
147         return cairo_test_status_from_status (ctx, status);
148     }
149
150     status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2,
151                                           mime_data, mime_length,
152                                           free, mime_data);
153     if (status) {
154         cairo_surface_destroy (image);
155         free (mime_data);
156         return cairo_test_status_from_status (ctx, status);
157     }
158
159     cairo_set_source_surface (cr, image, x, y);
160     cairo_surface_destroy (image);
161
162     cairo_paint (cr);
163
164     /* Image 2 */
165
166     status = read_file (ctx, jbig2_image2_filename, &mime_data, &mime_length);
167     if (status)
168         return cairo_test_status_from_status (ctx, status);
169
170     image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 200, 50);
171
172     status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2_GLOBAL_ID,
173                                           (unsigned char *)"global", 6, NULL, NULL);
174     if (status) {
175         cairo_surface_destroy (image);
176         return cairo_test_status_from_status (ctx, status);
177     }
178
179     status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2,
180                                           mime_data, mime_length,
181                                           free, mime_data);
182     if (status) {
183         cairo_surface_destroy (image);
184         free (mime_data);
185         return cairo_test_status_from_status (ctx, status);
186     }
187
188     /* Set the global data */
189     status = read_file (ctx, jbig2_global_filename, &mime_data, &mime_length);
190     if (status)
191         return cairo_test_status_from_status (ctx, status);
192
193     status = cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JBIG2_GLOBAL,
194                                           mime_data, mime_length,
195                                           free, mime_data);
196     if (status) {
197         cairo_surface_destroy (image);
198         free (mime_data);
199         return cairo_test_status_from_status (ctx, status);
200     }
201
202     cairo_set_source_surface (cr, image, x, y + 50);
203     cairo_surface_destroy (image);
204
205     cairo_paint (cr);
206     return CAIRO_TEST_SUCCESS;
207 }
208
209 static cairo_test_status_t
210 draw (cairo_t *cr, int width, int height)
211 {
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;
216
217     status = paint_file (cr, jpg_filename, CAIRO_MIME_TYPE_JPEG, 0, 0);
218     if (status)
219         return status;
220
221     status = paint_file (cr, png_filename, CAIRO_MIME_TYPE_PNG, 0, 50);
222     if (status)
223         return status;
224
225     status = paint_file (cr, jp2_filename, CAIRO_MIME_TYPE_JP2, 0, 100);
226     if (status)
227         return status;
228
229     status = paint_jbig2_file (cr, 0, 150);
230     if (status)
231         return status;
232
233     return CAIRO_TEST_SUCCESS;
234 }
235
236 CAIRO_TEST (mime_data,
237             "Check that the mime-data embedding works",
238             "jpeg, api", /* keywords */
239             NULL, /* requirements */
240             200, 250,
241             NULL, draw)