]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-csource.c
Bug 556527 - The current page property is not passed to GtkPrintUnixDialog
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-csource.c
1 /* Gdk-Pixbuf-CSource - GdkPixbuf based image CSource generator
2  * Copyright (C) 1999, 2001 Tim Janik
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include "config.h"
20
21 #define __GTK_H_INSIDE__
22 #include "../gtk/gtkversion.h"  /* versioning */
23 #undef __GTK_H_INSIDE__
24 #include "gdk-pixbuf.h"
25 #include "gdk-pixdata.h"
26 #include <glib/gprintf.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30
31 /* --- defines --- */
32 #undef  G_LOG_DOMAIN
33 #define G_LOG_DOMAIN    "Gdk-Pixbuf-CSource"
34 #define PRG_NAME        "gdk-pixbuf-csource"
35 #define PKG_NAME        "Gtk+"
36 #define PKG_HTTP_HOME   "http://www.gtk.org"
37
38
39 /* --- prototypes --- */
40 static void     parse_args      (gint    *argc_p,
41                                  gchar ***argv_p);
42 static void     print_blurb     (FILE    *bout,
43                                  gboolean print_help);
44
45
46 /* --- variables --- */
47 static guint    gen_type = GDK_PIXDATA_DUMP_PIXDATA_STREAM;
48 static guint    gen_ctype = GDK_PIXDATA_DUMP_GTYPES | GDK_PIXDATA_DUMP_STATIC | GDK_PIXDATA_DUMP_CONST;
49 static gboolean use_rle = TRUE;
50 static gboolean with_decoder = FALSE;
51 static gchar   *image_name = "my_pixbuf";
52 static gboolean build_list = FALSE;
53
54
55 /* --- functions --- */
56 static void
57 print_csource (FILE *f_out,
58                GdkPixbuf *pixbuf)
59 {
60   GdkPixdata pixdata;
61   gpointer free_me;
62   GString *gstring;
63
64   free_me = gdk_pixdata_from_pixbuf (&pixdata, pixbuf, use_rle);
65   gstring = gdk_pixdata_to_csource (&pixdata, image_name,
66                                     gen_type | gen_ctype |
67                                     (with_decoder ? GDK_PIXDATA_DUMP_RLE_DECODER : 0));
68
69   g_fprintf (f_out, "%s\n", gstring->str);
70
71   g_free (free_me);
72 }
73
74 int
75 main (int   argc,
76       char *argv[])
77 {
78   GdkPixbuf *pixbuf;
79   GError *error = NULL;
80   gchar *infilename;
81
82   /* initialize glib/GdkPixbuf */
83   g_type_init ();
84
85   /* parse args and do fast exits */
86   parse_args (&argc, &argv);
87
88   if (!build_list)
89     {
90       if (argc != 2)
91         {
92           print_blurb (stderr, TRUE);
93           return 1;
94         }
95       
96 #ifdef G_OS_WIN32
97       infilename = g_locale_to_utf8 (argv[1], -1, NULL, NULL, NULL);
98 #else
99       infilename = argv[1];
100 #endif
101
102       pixbuf = gdk_pixbuf_new_from_file (infilename, &error);
103       if (!pixbuf)
104         {
105           g_fprintf (stderr, "failed to load \"%s\": %s\n",
106                    argv[1],
107                    error->message);
108           g_error_free (error);
109           return 1;
110         }
111       
112       print_csource (stdout, pixbuf);
113       g_object_unref (pixbuf);
114     }
115   else /* parse name, file pairs */
116     {
117       gchar **p = argv + 1;
118       guint j = argc - 1;
119       gboolean toggle = FALSE;
120
121       while (j--)
122         {
123 #ifdef G_OS_WIN32
124           infilename = g_locale_to_utf8 (*p, -1, NULL, NULL, NULL);
125 #else
126           infilename = *p;
127 #endif
128
129           if (!toggle)
130             {
131               image_name = infilename;
132               p++;
133             }
134           else
135             {
136               pixbuf = gdk_pixbuf_new_from_file (infilename, &error);
137               if (!pixbuf)
138                 {
139                   g_fprintf (stderr, "failed to load \"%s\": %s\n",
140                            *p,
141                            error->message);
142                   g_error_free (error);
143                   return 1;
144                 }
145               print_csource (stdout, pixbuf);
146               g_object_unref (pixbuf);
147               p++;
148             }
149           toggle = !toggle;
150         }
151     }
152   
153   return 0;
154 }
155
156 static void
157 parse_args (gint    *argc_p,
158             gchar ***argv_p)
159 {
160   guint argc = *argc_p;
161   gchar **argv = *argv_p;
162   guint i, e;
163
164   for (i = 1; i < argc; i++)
165     {
166       if (strcmp ("--macros", argv[i]) == 0)
167         {
168           gen_type = GDK_PIXDATA_DUMP_MACROS;
169           argv[i] = NULL;
170         }
171       else if (strcmp ("--struct", argv[i]) == 0)
172         {
173           gen_type = GDK_PIXDATA_DUMP_PIXDATA_STRUCT;
174           argv[i] = NULL;
175         }
176       else if (strcmp ("--stream", argv[i]) == 0)
177         {
178           gen_type = GDK_PIXDATA_DUMP_PIXDATA_STREAM;
179           argv[i] = NULL;
180         }
181       else if (strcmp ("--rle", argv[i]) == 0)
182         {
183           use_rle = TRUE;
184           argv[i] = NULL;
185         }
186       else if (strcmp ("--raw", argv[i]) == 0)
187         {
188           use_rle = FALSE;
189           argv[i] = NULL;
190         }
191       else if (strcmp ("--extern", argv[i]) == 0)
192         {
193           gen_ctype &= ~GDK_PIXDATA_DUMP_STATIC;
194           argv[i] = NULL;
195         }
196       else if (strcmp ("--static", argv[i]) == 0)
197         {
198           gen_ctype |= GDK_PIXDATA_DUMP_STATIC;
199           argv[i] = NULL;
200         }
201       else if (strcmp ("--decoder", argv[i]) == 0)
202         {
203           with_decoder = TRUE;
204           argv[i] = NULL;
205         }
206       else if ((strcmp ("--name", argv[i]) == 0) ||
207                (strncmp ("--name=", argv[i], 7) == 0))
208         {
209           gchar *equal = argv[i] + 6;
210
211           if (*equal == '=')
212             image_name = g_strdup (equal + 1);
213           else if (i + 1 < argc)
214             {
215               image_name = g_strdup (argv[i + 1]);
216               argv[i] = NULL;
217               i += 1;
218             }
219           argv[i] = NULL;
220         }
221       else if (strcmp ("--build-list", argv[i]) == 0)
222         {
223           build_list = TRUE;
224           argv[i] = NULL;
225         }
226       else if (strcmp ("-h", argv[i]) == 0 ||
227                strcmp ("--help", argv[i]) == 0)
228         {
229           print_blurb (stderr, TRUE);
230           argv[i] = NULL;
231           exit (0);
232         }
233       else if (strcmp ("-v", argv[i]) == 0 ||
234                strcmp ("--version", argv[i]) == 0)
235         {
236           print_blurb (stderr, FALSE);
237           argv[i] = NULL;
238           exit (0);
239         }
240       else if (strcmp (argv[i], "--g-fatal-warnings") == 0)
241         {
242           GLogLevelFlags fatal_mask;
243
244           fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
245           fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
246           g_log_set_always_fatal (fatal_mask);
247
248           argv[i] = NULL;
249         }
250     }
251
252   e = 0;
253   for (i = 1; i < argc; i++)
254     {
255       if (e)
256         {
257           if (argv[i])
258             {
259               argv[e++] = argv[i];
260               argv[i] = NULL;
261             }
262         }
263       else if (!argv[i])
264         e = i;
265     }
266   if (e)
267     *argc_p = e;
268 }
269
270 static void
271 print_blurb (FILE    *bout,
272              gboolean print_help)
273 {
274   if (!print_help)
275     {
276       g_fprintf (bout, "%s version ", PRG_NAME);
277       g_fprintf (bout, "%d.%d.%d", GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
278       g_fprintf (bout, "\n");
279       g_fprintf (bout, "%s comes with ABSOLUTELY NO WARRANTY.\n", PRG_NAME);
280       g_fprintf (bout, "You may redistribute copies of %s under the terms of\n", PRG_NAME);
281       g_fprintf (bout, "the GNU Lesser General Public License which can be found in the\n");
282       g_fprintf (bout, "%s source package. Sources, examples and contact\n", PKG_NAME);
283       g_fprintf (bout, "information are available at %s\n", PKG_HTTP_HOME);
284     }
285   else
286     {
287       g_fprintf (bout, "Usage: %s [options] [image]\n", PRG_NAME);
288       g_fprintf (bout, "       %s [options] --build-list [[name image]...]\n", PRG_NAME);
289       g_fprintf (bout, "  --stream                   generate pixbuf data stream\n");
290       g_fprintf (bout, "  --struct                   generate GdkPixdata structure\n");
291       g_fprintf (bout, "  --macros                   generate image size/pixel macros\n");
292       g_fprintf (bout, "  --rle                      use one byte run-length-encoding\n");
293       g_fprintf (bout, "  --raw                      provide raw image data copy\n");
294       g_fprintf (bout, "  --extern                   generate extern symbols\n");
295       g_fprintf (bout, "  --static                   generate static symbols\n");
296       g_fprintf (bout, "  --decoder                  provide rle decoder\n");
297       g_fprintf (bout, "  --name=identifier          C macro/variable name\n");
298       g_fprintf (bout, "  --build-list               parse (name, image) pairs\n");
299       g_fprintf (bout, "  -h, --help                 show this help message\n");
300       g_fprintf (bout, "  -v, --version              print version informations\n");
301       g_fprintf (bout, "  --g-fatal-warnings         make warnings fatal (abort)\n");
302     }
303 }
304