2 * Copyright © 2008 Carlos Garcia Campos
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 * Author: Carlos Garcia Campos <carlosgc@gnome.org>
30 #include <libspectre/spectre.h>
33 do { fprintf (stderr, "FAIL: %s\n", msg); exit (-1); } while (0)
36 _spectre_render_page (const char *filename,
37 const char *page_label,
38 cairo_surface_t **surface_out)
40 static const cairo_user_data_key_t key;
42 SpectreDocument *document;
44 int width, height, stride;
45 unsigned char *pixels;
46 cairo_surface_t *surface;
48 document = spectre_document_new ();
49 spectre_document_load (document, filename);
50 status = spectre_document_status (document);
52 spectre_document_free (document);
53 return spectre_status_to_string (status);
58 SpectreRenderContext *rc;
60 page = spectre_document_get_page_by_label (document, page_label);
61 spectre_document_free (document);
63 return "page not found";
65 spectre_page_get_size (page, &width, &height);
66 rc = spectre_render_context_new ();
67 spectre_render_context_set_page_size (rc, width, height);
68 spectre_page_render (page, rc, &pixels, &stride);
69 spectre_render_context_free (rc);
70 status = spectre_page_status (page);
71 spectre_page_free (page);
74 return spectre_status_to_string (status);
77 spectre_document_get_page_size (document, &width, &height);
78 spectre_document_render (document, &pixels, &stride);
79 spectre_document_free (document);
82 surface = cairo_image_surface_create_for_data (pixels,
86 cairo_surface_set_user_data (surface, &key,
87 pixels, (cairo_destroy_func_t) free);
88 *surface_out = surface;
93 (int argc, char *argv[])
96 cairo_surface_t *surface = NULL; /* silence compiler warning */
97 cairo_status_t status;
99 if (argc < 3 || argc > 4)
100 FAIL ("usage: ps2png input_file.ps output_file.png [page]");
102 err = _spectre_render_page (argv[1], argv[3], &surface);
106 status = cairo_surface_write_to_png (surface, argv[2]);
107 cairo_surface_destroy (surface);
109 if (status != CAIRO_STATUS_SUCCESS)
110 FAIL (cairo_status_to_string (status));