1/* Gdk-Pixbuf-Pixdata - GdkPixbuf to GdkPixdata
2 * Copyright (C) 1999, 2001 Tim Janik
3 * Copyright (C) 2012 Red Hat, Inc
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18#include "config.h"
19
20#include "gdk-pixbuf.h"
21#include "gdk-pixdata.h"
22#include <glib/gprintf.h>
23#include <stdlib.h>
24#include <string.h>
25
26
27/* --- defines --- */
28#undef G_LOG_DOMAIN
29#define G_LOG_DOMAIN "Gdk-Pixbuf-Pixdata"
30#define PRG_NAME "gdk-pixbuf-pixdata-3.0"
31#define PKG_NAME "gdk-pixbuf"
32#define PKG_HTTP_HOME "http://www.gtk.org"
33
34static gboolean use_rle = FALSE;
35
36/* --- prototypes --- */
37static void parse_args (gint *argc_p,
38 gchar ***argv_p);
39static void print_blurb (FILE *bout,
40 gboolean print_help);
41
42
43int
44main (int argc,
45 char *argv[])
46{
47 GdkPixbuf *pixbuf;
48 GError *error = NULL;
49 gchar *infilename;
50 gchar *outfilename;
51 gpointer free_me;
52 GdkPixdata pixdata;
53 guint8 *data;
54 guint data_len;
55
56 /* parse args and do fast exits */
57 parse_args (argc_p: &argc, argv_p: &argv);
58
59 if (argc != 3)
60 {
61 print_blurb (stderr, TRUE);
62 return 1;
63 }
64
65#ifdef G_OS_WIN32
66 infilename = g_locale_to_utf8 (argv[1], -1, NULL, NULL, NULL);
67#else
68 infilename = argv[1];
69#endif
70
71#ifdef G_OS_WIN32
72 outfilename = g_locale_to_utf8 (argv[2], -1, NULL, NULL, NULL);
73#else
74 outfilename = argv[2];
75#endif
76
77 pixbuf = gdk_pixbuf_new_from_file (filename: infilename, error: &error);
78 if (error != NULL)
79 {
80 g_printerr (format: "failed to load \"%s\": %s\n",
81 argv[1],
82 error->message);
83 g_error_free (error);
84 return 1;
85 }
86
87G_GNUC_BEGIN_IGNORE_DEPRECATIONS
88 free_me = gdk_pixdata_from_pixbuf (pixdata: &pixdata, pixbuf, use_rle);
89 data = gdk_pixdata_serialize (pixdata: &pixdata, stream_length_p: &data_len);
90G_GNUC_END_IGNORE_DEPRECATIONS
91 if (data == NULL)
92 {
93 g_printerr (format: "failed to serialize \"%s\"", argv[1]);
94 return 1;
95 }
96
97 if (!g_file_set_contents (filename: outfilename, contents: (char *)data, length: data_len, error: &error))
98 {
99 g_printerr (format: "failed to load \"%s\": %s\n",
100 argv[1],
101 error->message);
102 g_error_free (error);
103 return 1;
104 }
105
106 g_free (mem: data);
107 g_free (mem: free_me);
108 g_object_unref (object: pixbuf);
109
110 return 0;
111}
112
113static void
114parse_args (gint *argc_p,
115 gchar ***argv_p)
116{
117 guint argc = *argc_p;
118 gchar **argv = *argv_p;
119 guint i, e;
120
121 for (i = 1; i < argc; i++)
122 {
123 if (strcmp (s1: "-r", s2: argv[i]) == 0 ||
124 strcmp (s1: "--rle", s2: argv[i]) == 0)
125 {
126 use_rle = TRUE;
127 argv[i] = NULL;
128 }
129 else if (strcmp (s1: "-h", s2: argv[i]) == 0 ||
130 strcmp (s1: "--help", s2: argv[i]) == 0)
131 {
132 print_blurb (stderr, TRUE);
133 argv[i] = NULL;
134 exit (status: 0);
135 }
136 else if (strcmp (s1: "-v", s2: argv[i]) == 0 ||
137 strcmp (s1: "--version", s2: argv[i]) == 0)
138 {
139 print_blurb (stderr, FALSE);
140 argv[i] = NULL;
141 exit (status: 0);
142 }
143 else if (strcmp (s1: argv[i], s2: "--g-fatal-warnings") == 0)
144 {
145 GLogLevelFlags fatal_mask;
146
147 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
148 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
149 g_log_set_always_fatal (fatal_mask);
150
151 argv[i] = NULL;
152 }
153 }
154
155 e = 0;
156 for (i = 1; i < argc; i++)
157 {
158 if (e)
159 {
160 if (argv[i])
161 {
162 argv[e++] = argv[i];
163 argv[i] = NULL;
164 }
165 }
166 else if (!argv[i])
167 e = i;
168 }
169 if (e)
170 *argc_p = e;
171}
172
173static void
174print_blurb (FILE *bout,
175 gboolean print_help)
176{
177 if (!print_help)
178 {
179 g_fprintf (file: bout, format: "%s version ", PRG_NAME);
180 g_fprintf (file: bout, format: "%s", GDK_PIXBUF_VERSION);
181 g_fprintf (file: bout, format: "\n");
182 g_fprintf (file: bout, format: "%s comes with ABSOLUTELY NO WARRANTY.\n", PRG_NAME);
183 g_fprintf (file: bout, format: "You may redistribute copies of %s under the terms of\n", PRG_NAME);
184 g_fprintf (file: bout, format: "the GNU Lesser General Public License which can be found in the\n");
185 g_fprintf (file: bout, format: "%s source package. Sources, examples and contact\n", PKG_NAME);
186 g_fprintf (file: bout, format: "information are available at %s\n", PKG_HTTP_HOME);
187 }
188 else
189 {
190 g_fprintf (file: bout, format: "Usage: %s [options] [input-file] [output-file]\n", PRG_NAME);
191 g_fprintf (file: bout, format: " -r, --rle compress the image data using RLE\n");
192 g_fprintf (file: bout, format: " -h, --help show this help message\n");
193 g_fprintf (file: bout, format: " -v, --version print version informations\n");
194 g_fprintf (file: bout, format: " --g-fatal-warnings make warnings fatal (abort)\n");
195 }
196}
197

source code of gtk/subprojects/gdk-pixbuf/gdk-pixbuf/gdk-pixbuf-pixdata.c