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