1 /*
2 * Copyright © 2008 Adrian Johnson
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Author: Adrian Johnson <ajohnson@redneon.com>
25 */
26
27 #include "cairo-test.h"
28
29 #include <stdio.h>
30 #include <errno.h>
31 #include <cairo.h>
32 #include <cairo-pdf.h>
33
34 /* This test checks that the mime data is correctly used by the PDF
35 * surface when embedding images..
36 */
37
38 /* Both a *.png and *.jpg file with this name is required since we
39 * are not using a jpeg library */
40 #define IMAGE_FILE "romedalen"
41
42 #define BASENAME "pdf-mime-data.out"
43
44 static cairo_test_status_t
read_file(const cairo_test_context_t * ctx,const char * file,unsigned char ** data,unsigned int * len)45 read_file (const cairo_test_context_t *ctx,
46 const char *file,
47 unsigned char **data,
48 unsigned int *len)
49 {
50 FILE *fp;
51
52 fp = fopen (file, "rb");
53 if (fp == NULL) {
54 char filename[4096];
55
56 /* try again with srcdir */
57 snprintf (filename, sizeof (filename),
58 "%s/%s", ctx->srcdir, file);
59 fp = fopen (filename, "rb");
60 }
61 if (fp == NULL) {
62 switch (errno) {
63 case ENOMEM:
64 cairo_test_log (ctx, "Could not create file handle for %s due to \
65 lack of memory\n", file);
66 return CAIRO_TEST_NO_MEMORY;
67 default:
68 cairo_test_log (ctx, "Could not get the file handle for %s\n", file);
69 return CAIRO_TEST_FAILURE;
70 }
71 }
72
73 fseek (fp, 0, SEEK_END);
74 *len = ftell(fp);
75 fseek (fp, 0, SEEK_SET);
76 *data = malloc (*len);
77 if (*data == NULL) {
78 fclose(fp);
79 cairo_test_log (ctx, "Could not allocate memory for buffer to read \
80 from file %s\n", file);
81 return CAIRO_TEST_NO_MEMORY;
82 }
83
84 if (fread(*data, *len, 1, fp) != 1) {
85 free (*data);
86 fclose(fp);
87 cairo_test_log (ctx, "Could not read data from file %s\n", file);
88 return CAIRO_TEST_FAILURE;
89 }
90
91 fclose(fp);
92 return CAIRO_TEST_SUCCESS;
93 }
94
95 static cairo_test_status_t
preamble(cairo_test_context_t * ctx)96 preamble (cairo_test_context_t *ctx)
97 {
98 cairo_surface_t *image;
99 cairo_surface_t *surface;
100 cairo_t *cr;
101 cairo_status_t status, status2;
102 cairo_test_status_t test_status;
103 int width, height;
104 unsigned char *data;
105 unsigned char *out_data;
106 unsigned int len, out_len;
107 char command[4096];
108 int exit_status;
109 char *filename;
110 const char *path = cairo_test_mkdir (CAIRO_TEST_OUTPUT_DIR) ? CAIRO_TEST_OUTPUT_DIR : ".";
111
112 if (! cairo_test_is_target_enabled (ctx, "pdf"))
113 return CAIRO_TEST_UNTESTED;
114
115 image = cairo_image_surface_create_from_png (IMAGE_FILE ".png");
116 test_status = read_file (ctx, IMAGE_FILE ".jpg", &data, &len);
117 if (test_status) {
118 return test_status;
119 }
120
121 cairo_surface_set_mime_data (image, CAIRO_MIME_TYPE_JPEG,
122 data, len,
123 free, data);
124 width = cairo_image_surface_get_width (image);
125 height = cairo_image_surface_get_height (image);
126
127 xasprintf (&filename, "%s/%s.pdf", path, BASENAME);
128 surface = cairo_pdf_surface_create (filename, width + 20, height + 20);
129 cr = cairo_create (surface);
130 cairo_translate (cr, 10, 10);
131 cairo_set_source_surface (cr, image, 0, 0);
132 cairo_paint (cr);
133 status = cairo_status (cr);
134 cairo_destroy (cr);
135 cairo_surface_finish (surface);
136 status2 = cairo_surface_status (surface);
137 if (status != CAIRO_STATUS_SUCCESS)
138 status = status2;
139 cairo_surface_destroy (surface);
140 cairo_surface_destroy (image);
141
142 if (status) {
143 cairo_test_log (ctx, "Failed to create pdf surface for file %s: %s\n",
144 filename, cairo_status_to_string (status));
145 free (filename);
146 return CAIRO_TEST_FAILURE;
147 }
148
149 printf ("pdf-mime-data: Please check %s to ensure it looks/prints correctly.\n", filename);
150
151 sprintf (command, "pdfimages -j %s %s", filename, CAIRO_TEST_OUTPUT_DIR "/" BASENAME);
152 exit_status = system (command);
153 free (filename);
154 if (exit_status) {
155 cairo_test_log (ctx, "pdfimages failed with exit status %d\n", exit_status);
156 return CAIRO_TEST_FAILURE;
157 }
158
159 test_status = read_file (ctx, IMAGE_FILE ".jpg", &data, &len);
160 if (test_status) {
161 return test_status;
162 }
163
164 test_status = read_file (ctx, CAIRO_TEST_OUTPUT_DIR "/" BASENAME "-000.jpg", &out_data, &out_len);
165 if (test_status) {
166 return test_status;
167 }
168
169 if (len != out_len || memcmp(data, out_data, len) != 0) {
170 free (data);
171 free (out_data);
172 cairo_test_log (ctx, "output mime data does not match source mime data\n");
173 return CAIRO_TEST_FAILURE;
174 }
175
176 free (data);
177 free (out_data);
178
179 return CAIRO_TEST_SUCCESS;
180 }
181
182 CAIRO_TEST (pdf_mime_data,
183 "Check mime data correctly used by PDF surface",
184 "pdf, mime-data", /* keywords */
185 NULL, /* requirements */
186 0, 0,
187 preamble, NULL)
188