1
/*
2
 * Copyright © 2008 Carlos Garcia Campos
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: Carlos Garcia Campos <carlosgc@gnome.org>
25
 */
26

            
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <cairo.h>
30
#include <libspectre/spectre.h>
31

            
32
#define FAIL(msg)                                                        \
33
    do { fprintf (stderr, "FAIL: %s\n", msg); exit (-1); } while (0)
34

            
35
static const char *
36
_spectre_render_page (const char *filename,
37
		      const char *page_label,
38
		      cairo_surface_t **surface_out)
39
{
40
    static const cairo_user_data_key_t key;
41

            
42
    SpectreDocument *document;
43
    SpectreStatus status;
44
    int width, height, stride;
45
    unsigned char *pixels;
46
    cairo_surface_t *surface;
47

            
48
    document = spectre_document_new ();
49
    spectre_document_load (document, filename);
50
    status = spectre_document_status (document);
51
    if (status) {
52
	spectre_document_free (document);
53
	return spectre_status_to_string (status);
54
    }
55

            
56
    if (page_label) {
57
	SpectrePage *page;
58
	SpectreRenderContext *rc;
59

            
60
	page = spectre_document_get_page_by_label (document, page_label);
61
	spectre_document_free (document);
62
	if (page == NULL)
63
	    return "page not found";
64

            
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);
72
	if (status) {
73
	    free (pixels);
74
	    return spectre_status_to_string (status);
75
	}
76
    } else {
77
	spectre_document_get_page_size (document, &width, &height);
78
	spectre_document_render (document, &pixels, &stride);
79
	spectre_document_free (document);
80
    }
81

            
82
    surface = cairo_image_surface_create_for_data (pixels,
83
						   CAIRO_FORMAT_RGB24,
84
						   width, height,
85
						   stride);
86
    cairo_surface_set_user_data (surface, &key,
87
				 pixels, (cairo_destroy_func_t) free);
88
    *surface_out = surface;
89
    return NULL;
90
}
91

            
92
int main
93
(int argc, char *argv[])
94
{
95
    const char *err;
96
    cairo_surface_t *surface = NULL; /* silence compiler warning */
97
    cairo_status_t status;
98

            
99
    if (argc < 3 || argc > 4)
100
        FAIL ("usage: ps2png input_file.ps output_file.png [page]");
101

            
102
    err = _spectre_render_page (argv[1], argv[3], &surface);
103
    if (err != NULL)
104
        FAIL (err);
105

            
106
    status = cairo_surface_write_to_png (surface, argv[2]);
107
    cairo_surface_destroy (surface);
108

            
109
    if (status != CAIRO_STATUS_SUCCESS)
110
        FAIL (cairo_status_to_string (status));
111

            
112
    return 0;
113
}