Tizen 2.0 Release
[framework/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 draw (cairo_t *cr, int width, int height)
120 {
121     const char jpg_filename[] = "jpeg.jpg";
122     const char png_filename[] = "png.png";
123     const char jp2_filename[] = "jp2.jp2";
124     cairo_test_status_t status;
125
126     status = paint_file (cr, jpg_filename, CAIRO_MIME_TYPE_JPEG, 0, 0);
127     if (status)
128         return status;
129
130     status = paint_file (cr, png_filename, CAIRO_MIME_TYPE_PNG, 0, 50);
131     if (status)
132         return status;
133
134     status = paint_file (cr, jp2_filename, CAIRO_MIME_TYPE_JP2, 0, 100);
135     if (status)
136         return status;
137
138     return CAIRO_TEST_SUCCESS;
139 }
140
141 CAIRO_TEST (mime_data,
142             "Check that the mime-data embedding works",
143             "jpeg, api", /* keywords */
144             NULL, /* requirements */
145             200, 150,
146             NULL, draw)