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

Revision 20920, 4.1 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r20919, 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#include "rsvg-private.h"
29
30#include <popt.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35int
36main (int argc, const char **argv)
37{
38        poptContext popt_context;
39        double x_zoom = 1.0;
40        double y_zoom = 1.0;
41        double dpi_x = -1.0;
42        double dpi_y = -1.0;
43        int width  = -1;
44        int height = -1;
45        int bVersion = 0;
46        int quality = 100;
47        char * quality_str = NULL;
48        char * format = NULL;
49
50        struct poptOption options_table[] = {
51                { "dpi-x",   'd',  POPT_ARG_DOUBLE, &dpi_x,    0, "pixels per inch", "<float>"},
52                { "dpi-y",   'p',  POPT_ARG_DOUBLE, &dpi_y,    0, "pixels per inch", "<float>"},
53                { "x-zoom",  'x',  POPT_ARG_DOUBLE, &x_zoom,   0, "x zoom factor", "<float>" },
54                { "y-zoom",  'y',  POPT_ARG_DOUBLE, &y_zoom,   0, "y zoom factor", "<float>" },
55                { "width",   'w',  POPT_ARG_INT,    &width,    0, "width", "<int>" },
56                { "height",  'h',  POPT_ARG_INT,    &height,   0, "height", "<int>" },
57                { "quality", 'q',  POPT_ARG_INT,    &quality,  0, "JPEG quality", "<int>"},
58                { "format",  'f',  POPT_ARG_STRING, &format,   0, "save format", "[png, jpeg]"},
59                { "version", 'v',  POPT_ARG_NONE,   &bVersion, 0, "show version information", NULL },
60                POPT_AUTOHELP
61                POPT_TABLEEND
62        };
63        int c;
64        const char * const *args;
65        gint n_args = 0;
66        GdkPixbuf *pixbuf;
67
68        popt_context = poptGetContext ("rsvg", argc, argv, options_table, 0);
69        poptSetOtherOptionHelp(popt_context, "[OPTIONS...] file.svg file.png");
70
71        c = poptGetNextOpt (popt_context);
72        args = poptGetArgs (popt_context);
73
74        if (bVersion != 0)
75                {
76                    g_print ("rsvg version %s\n", VERSION);
77                        return 0;
78                }
79
80        if (args)
81                while (args[n_args] != NULL)
82                        n_args++;
83
84        if (n_args != 2)
85                {
86                        poptPrintHelp (popt_context, stderr, 0);
87                        poptFreeContext (popt_context);
88                        return 1;
89                }
90
91        if(format == NULL)
92                format = "png";
93        else if (strstr (format, "jpg") != NULL) /* backward compatibility */
94                format = "jpeg";
95
96        g_type_init ();
97
98        rsvg_set_default_dpi_x_y (dpi_x, dpi_y);
99
100        /* if both are unspecified, assume user wants to zoom the pixbuf in at least 1 dimension */
101        if (width == -1 && height == -1)
102                pixbuf = rsvg_pixbuf_from_file_at_zoom (args[0], x_zoom, y_zoom, NULL);
103        /* if both are unspecified, assume user wants to resize pixbuf in at least 1 dimension */
104        else if (x_zoom == 1.0 && y_zoom == 1.0)
105                pixbuf = rsvg_pixbuf_from_file_at_size (args[0], width, height, NULL);
106        else
107                /* assume the user wants to zoom the pixbuf, but cap the maximum size */
108                pixbuf = rsvg_pixbuf_from_file_at_zoom_with_max (args[0], x_zoom, y_zoom,
109                                                                                                                 width, height, NULL);
110
111        if (pixbuf)
112                if (strcmp (format, "jpeg") == 0) {
113                        if (quality < 1 || quality > 100) /* is an invalid quality */
114                                gdk_pixbuf_save (pixbuf, args[1], format, NULL, NULL);
115                        else {
116                                quality_str = g_strdup_printf ("%d", quality);
117                                gdk_pixbuf_save (pixbuf, args[1], format, NULL, "quality", quality_str, NULL);
118                                g_free (quality_str);
119                        }
120                }
121                else {
122                        gdk_pixbuf_save (pixbuf, args[1], format, NULL, NULL);
123                }
124        else {
125                poptFreeContext (popt_context);
126                g_warning (_("Error loading SVG file.\n"));
127                return 1;
128        }
129
130        g_object_unref (G_OBJECT (pixbuf));
131
132        poptFreeContext (popt_context);
133        return 0;
134}
Note: See TracBrowser for help on using the repository browser.