source: trunk/third/librsvg/test-rsvg.c @ 18609

Revision 18609, 3.9 KB checked in by ghudson, 21 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18608, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
2
3   test-rsvg.c: Command line utility for exercising rsvg.
4 
5   Copyright (C) 2000 Eazel, Inc.
6   Copyright (C) 2002 Dom Lachowicz
7 
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU Library General Public License as
10   published by the Free Software Foundation; either version 2 of the
11   License, or (at your option) any later version.
12 
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   Library General Public License for more details.
17 
18   You should have received a copy of the GNU Library General Public
19   License along with this program; if not, write to the
20   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21   Boston, MA 02111-1307, USA.
22 
23   Author: Raph Levien <raph@artofcode.com>
24*/
25
26#include "config.h"
27#include "rsvg.h"
28
29#include <popt.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34int
35main (int argc, const char **argv)
36{
37        poptContext popt_context;
38        double x_zoom = 1.0;
39        double y_zoom = 1.0;
40        double dpi = -1.0;
41        int width  = -1;
42        int height = -1;
43        int bVersion = 0;
44        int quality = 100;
45        char * quality_str = NULL;
46        char * format = "png";
47
48        struct poptOption options_table[] = {
49                { "dpi"   ,  'd',  POPT_ARG_DOUBLE, &dpi,      0, "pixels per inch", "<float>"},
50                { "x-zoom",  'x',  POPT_ARG_DOUBLE, &x_zoom,   0, "x zoom factor", "<float>" },
51                { "y-zoom",  'y',  POPT_ARG_DOUBLE, &y_zoom,   0, "y zoom factor", "<float>" },
52                { "width",   'w',  POPT_ARG_INT,    &width,    0, "width", "<int>" },
53                { "height",  'h',  POPT_ARG_INT,    &height,   0, "height", "<int>" },
54                { "quality", 'q',  POPT_ARG_INT,    &quality,  0, "JPEG quality", "<int>"},
55                { "format",  'f',  POPT_ARG_STRING, &format,   0, "save format", "[png, jpeg]"},
56                { "version", 'v',  POPT_ARG_NONE,   &bVersion, 0, "show version information", NULL },
57                POPT_AUTOHELP
58                POPT_TABLEEND
59        };
60        int c;
61        const char * const *args;
62        gint n_args = 0;
63        GdkPixbuf *pixbuf;
64
65        popt_context = poptGetContext ("rsvg", argc, argv, options_table, 0);
66        poptSetOtherOptionHelp(popt_context, "[OPTIONS...] file.svg file.png");
67
68        c = poptGetNextOpt (popt_context);
69        args = poptGetArgs (popt_context);
70
71        if (bVersion != 0)
72                {
73                    g_print ("rsvg version %s\n", VERSION);
74                        return 0;
75                }
76
77        if (args)
78                while (args[n_args] != NULL)
79                        n_args++;
80
81        if (n_args != 2)
82                {
83                        poptPrintHelp (popt_context, stderr, 0);
84                        poptFreeContext (popt_context);
85                        return 1;
86                }
87
88        if (strstr (format, "jpeg") != NULL || strstr (format, "jpg") != NULL)
89                format = "jpeg";
90        else
91                format = "png";
92
93        g_type_init ();
94
95        if (dpi > 0.)
96                rsvg_set_default_dpi (dpi);
97
98        /* if both are unspecified, assume user wants to zoom the pixbuf in at least 1 dimension */
99        if (width == -1 && height == -1)
100                pixbuf = rsvg_pixbuf_from_file_at_zoom (args[0], x_zoom, y_zoom, NULL);
101        /* if both are unspecified, assume user wants to resize pixbuf in at least 1 dimension */
102        else if (x_zoom == 1.0 && y_zoom == 1.0)
103                pixbuf = rsvg_pixbuf_from_file_at_size (args[0], width, height, NULL);
104        else
105                /* assume the user wants to zoom the pixbuf, but cap the maximum size */
106                pixbuf = rsvg_pixbuf_from_file_at_zoom_with_max (args[0], x_zoom, y_zoom,
107                                                                                                                 width, height, NULL);
108
109        if (pixbuf)
110                if (strcmp (format, "jpeg") != 0 || (quality < 1 || quality > 100)) /* is a png or is an invalid quality */
111                        gdk_pixbuf_save (pixbuf, args[1], format, NULL, NULL);
112                else {
113                        quality_str = g_strdup_printf ("%d", quality);
114                        gdk_pixbuf_save (pixbuf, args[1], format, NULL, "quality", quality_str, NULL);
115                        g_free (quality_str);
116                }
117        else {
118                poptFreeContext (popt_context);
119                g_warning ("Error loading SVG file.\n");
120                return 1;
121        }
122
123        g_object_unref (G_OBJECT (pixbuf));
124
125        poptFreeContext (popt_context);
126        return 0;
127}
Note: See TracBrowser for help on using the repository browser.