]> git.cworth.org Git - svg2png/blob - svg2png.c
Fix copy-paste bug in usage string.
[svg2png] / svg2png.c
1 /* gcc `pkg-config --cflags --libs librsvg-2.0 cairo-png` -o svg2png svg2png.c
2  *
3  * Copyright © 2005 Red Hat, Inc.
4  * Copyright © 2006 Red Hat, Inc.
5  * Copyright © 2007 Red Hat, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person
8  * obtaining a copy of this software and associated documentation
9  * files (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use, copy,
11  * modify, merge, publish, distribute, sublicense, and/or sell copies
12  * of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  *
27  * Authors: Kristian Høgsberg <krh@redhat.com>
28  *          Carl Worth <cworth@redhat.com>
29  *          Behdad Esfahbod <besfahbo@redhat.com>
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <librsvg/rsvg.h>
35 #include <librsvg/rsvg-cairo.h>
36
37 #include <cairo.h>
38
39 #define FAIL(msg)                                                       \
40     do { fprintf (stderr, "FAIL: %s\n", msg); exit (-1); } while (0)
41
42 int main (int argc, char *argv[])
43 {
44     GError *error = NULL;
45     RsvgHandle *handle;
46     RsvgDimensionData dim;
47     double width, height;
48     const char *filename = argv[1];
49     const char *output_filename = argv[2];
50     cairo_surface_t *surface;
51     cairo_t *cr;
52     cairo_status_t status;
53
54     if (argc != 3)
55         FAIL ("usage: svg2png input_file.svg output_file.png");
56
57     g_type_init ();
58
59     rsvg_set_default_dpi (72.0);
60     handle = rsvg_handle_new_from_file (filename, &error);
61     if (error != NULL)
62         FAIL (error->message);
63
64     rsvg_handle_get_dimensions (handle, &dim);
65     width = dim.width;
66     height = dim.height;
67
68     surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
69     cr = cairo_create (surface);
70
71     rsvg_handle_render_cairo (handle, cr);
72
73     status = cairo_status (cr);
74     if (status)
75         FAIL (cairo_status_to_string (status));
76
77     cairo_surface_write_to_png (surface, output_filename);
78
79     cairo_destroy (cr);
80     cairo_surface_destroy (surface);
81
82     return 0;
83 }