]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-csource.c
converted a Tab to spaces.
[~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 #include "../gtk/gtkversion.h"  /* versioning */
22 #include "gdk-pixbuf.h"
23 #include "gdk-pixdata.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28
29 /* --- defines --- */
30 #undef  G_LOG_DOMAIN
31 #define G_LOG_DOMAIN    "Gdk-Pixbuf-CSource"
32 #define PRG_NAME        "gdk-pixbuf-csource"
33 #define PKG_NAME        "Gtk+"
34 #define PKG_HTTP_HOME   "http://www.gtk.org"
35
36
37 /* --- prototypes --- */
38 static void     parse_args      (gint    *argc_p,
39                                  gchar ***argv_p);
40 static void     print_blurb     (FILE    *bout,
41                                  gboolean print_help);
42
43
44 /* --- variables --- */
45 static guint    gen_type = GDK_PIXDATA_DUMP_PIXDATA_STREAM;
46 static guint    gen_ctype = GDK_PIXDATA_DUMP_GTYPES | GDK_PIXDATA_DUMP_STATIC | GDK_PIXDATA_DUMP_CONST;
47 static gboolean use_rle = TRUE;
48 static gboolean with_decoder = FALSE;
49 static gchar   *image_name = "my_pixbuf";
50 static gboolean build_list = FALSE;
51
52
53 /* --- functions --- */
54 static void
55 print_csource (FILE *f_out,
56                GdkPixbuf *pixbuf)
57 {
58   GdkPixdata pixdata;
59   gpointer free_me;
60   GString *gstring;
61
62   free_me = gdk_pixdata_from_pixbuf (&pixdata, pixbuf, use_rle);
63   gstring = gdk_pixdata_to_csource (&pixdata, image_name,
64                                     gen_type | gen_ctype |
65                                     (with_decoder ? GDK_PIXDATA_DUMP_RLE_DECODER : 0));
66
67   fprintf (f_out, "%s\n", gstring->str);
68
69   g_free (free_me);
70 }
71
72 int
73 main (int   argc,
74       char *argv[])
75 {
76   GdkPixbuf *pixbuf;
77   GError *error = NULL;
78
79   /* initialize glib/GdkPixbuf */
80   g_type_init ();
81
82   /* parse args and do fast exits */
83   parse_args (&argc, &argv);
84
85   if (!build_list)
86     {
87       if (argc != 2)
88         {
89           print_blurb (stderr, TRUE);
90           return 1;
91         }
92       
93       pixbuf = gdk_pixbuf_new_from_file (argv[1], &error);
94       if (!pixbuf)
95         {
96           fprintf (stderr, "failed to load \"%s\": %s\n",
97                    argv[1],
98                    error->message);
99           g_error_free (error);
100           return 1;
101         }
102       
103       print_csource (stdout, pixbuf);
104       g_object_unref (pixbuf);
105     }
106   else /* parse name, file pairs */
107     {
108       gchar **p = argv + 1;
109       guint j = argc - 1;
110       gboolean toggle = FALSE;
111
112       while (j--)
113         {
114           if (!toggle)
115             image_name = *p++;
116           else
117             {
118               pixbuf = gdk_pixbuf_new_from_file (*p, &error);
119               if (!pixbuf)
120                 {
121                   fprintf (stderr, "failed to load \"%s\": %s\n",
122                            *p,
123                            error->message);
124                   g_error_free (error);
125                   return 1;
126                 }
127               print_csource (stdout, pixbuf);
128               g_object_unref (pixbuf);
129               p++;
130             }
131           toggle = !toggle;
132         }
133     }
134   
135   return 0;
136 }
137
138 static void
139 parse_args (gint    *argc_p,
140             gchar ***argv_p)
141 {
142   guint argc = *argc_p;
143   gchar **argv = *argv_p;
144   guint i, e;
145
146   for (i = 1; i < argc; i++)
147     {
148       if (strcmp ("--macros", argv[i]) == 0)
149         {
150           gen_type = GDK_PIXDATA_DUMP_MACROS;
151           argv[i] = NULL;
152         }
153       else if (strcmp ("--struct", argv[i]) == 0)
154         {
155           gen_type = GDK_PIXDATA_DUMP_PIXDATA_STRUCT;
156           argv[i] = NULL;
157         }
158       else if (strcmp ("--stream", argv[i]) == 0)
159         {
160           gen_type = GDK_PIXDATA_DUMP_PIXDATA_STREAM;
161           argv[i] = NULL;
162         }
163       else if (strcmp ("--rle", argv[i]) == 0)
164         {
165           use_rle = TRUE;
166           argv[i] = NULL;
167         }
168       else if (strcmp ("--raw", argv[i]) == 0)
169         {
170           use_rle = FALSE;
171           argv[i] = NULL;
172         }
173       else if (strcmp ("--extern", argv[i]) == 0)
174         {
175           gen_ctype &= ~GDK_PIXDATA_DUMP_STATIC;
176           argv[i] = NULL;
177         }
178       else if (strcmp ("--static", argv[i]) == 0)
179         {
180           gen_ctype |= GDK_PIXDATA_DUMP_STATIC;
181           argv[i] = NULL;
182         }
183       else if (strcmp ("--decoder", argv[i]) == 0)
184         {
185           with_decoder = TRUE;
186           argv[i] = NULL;
187         }
188       else if ((strcmp ("--name", argv[i]) == 0) ||
189                (strncmp ("--name=", argv[i], 7) == 0))
190         {
191           gchar *equal = argv[i] + 6;
192
193           if (*equal == '=')
194             image_name = g_strdup (equal + 1);
195           else if (i + 1 < argc)
196             {
197               image_name = g_strdup (argv[i + 1]);
198               argv[i] = NULL;
199               i += 1;
200             }
201           argv[i] = NULL;
202         }
203       else if (strcmp ("--build-list", argv[i]) == 0)
204         {
205           build_list = TRUE;
206           argv[i] = NULL;
207         }
208       else if (strcmp ("-h", argv[i]) == 0 ||
209                strcmp ("--help", argv[i]) == 0)
210         {
211           print_blurb (stderr, TRUE);
212           argv[i] = NULL;
213           exit (0);
214         }
215       else if (strcmp ("-v", argv[i]) == 0 ||
216                strcmp ("--version", argv[i]) == 0)
217         {
218           print_blurb (stderr, FALSE);
219           argv[i] = NULL;
220           exit (0);
221         }
222       else if (strcmp (argv[i], "--g-fatal-warnings") == 0)
223         {
224           GLogLevelFlags fatal_mask;
225
226           fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
227           fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
228           g_log_set_always_fatal (fatal_mask);
229
230           argv[i] = NULL;
231         }
232     }
233
234   e = 0;
235   for (i = 1; i < argc; i++)
236     {
237       if (e)
238         {
239           if (argv[i])
240             {
241               argv[e++] = argv[i];
242               argv[i] = NULL;
243             }
244         }
245       else if (!argv[i])
246         e = i;
247     }
248   if (e)
249     *argc_p = e;
250 }
251
252 static void
253 print_blurb (FILE    *bout,
254              gboolean print_help)
255 {
256   if (!print_help)
257     {
258       fprintf (bout, "%s version ", PRG_NAME);
259       fprintf (bout, "%u.%u.%u", GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
260       fprintf (bout, "\n");
261       fprintf (bout, "%s comes with ABSOLUTELY NO WARRANTY.\n", PRG_NAME);
262       fprintf (bout, "You may redistribute copies of %s under the terms of\n", PRG_NAME);
263       fprintf (bout, "the GNU Lesser General Public License which can be found in the\n");
264       fprintf (bout, "%s source package. Sources, examples and contact\n", PKG_NAME);
265       fprintf (bout, "information are available at %s\n", PKG_HTTP_HOME);
266     }
267   else
268     {
269       fprintf (bout, "Usage: %s [options] [image]\n", PRG_NAME);
270       fprintf (bout, "       %s [options] --build-list [[name image]...]\n", PRG_NAME);
271       fprintf (bout, "  --stream                   generate pixbuf data stream\n");
272       fprintf (bout, "  --struct                   generate GdkPixdata structure\n");
273       fprintf (bout, "  --macros                   generate image size/pixel macros\n");
274       fprintf (bout, "  --rle                      use one byte run-length-encoding\n");
275       fprintf (bout, "  --raw                      provide raw image data copy\n");
276       fprintf (bout, "  --extern                   generate extern symbols\n");
277       fprintf (bout, "  --static                   generate static symbols\n");
278       fprintf (bout, "  --decoder                  provide rle decoder\n");
279       fprintf (bout, "  --name=identifier          C macro/variable name\n");
280       fprintf (bout, "  --build-list               parse (name, image) pairs\n");
281       fprintf (bout, "  -h, --help                 show this help message\n");
282       fprintf (bout, "  -v, --version              print version informations\n");
283       fprintf (bout, "  --g-fatal-warnings         make warnings fatal (abort)\n");
284     }
285 }
286