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 
34 static gboolean use_rle = FALSE;
35 
36 /* --- prototypes --- */
37 static void	parse_args	(gint    *argc_p,
38 				 gchar ***argv_p);
39 static void	print_blurb	(FILE    *bout,
40 				 gboolean print_help);
41 
42 
43 int
main(int argc,char * argv[])44 main (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, &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 (infilename, &error);
78   if (!pixbuf)
79     {
80       g_printerr ("failed to load \"%s\": %s\n",
81 		  argv[1],
82 		  error->message);
83       g_error_free (error);
84       return 1;
85     }
86 
87 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
88   free_me = gdk_pixdata_from_pixbuf (&pixdata, pixbuf, use_rle);
89   data = gdk_pixdata_serialize (&pixdata, &data_len);
90 G_GNUC_END_IGNORE_DEPRECATIONS
91 
92   if (!g_file_set_contents (outfilename, (char *)data, data_len, &error))
93     {
94       g_printerr ("failed to load \"%s\": %s\n",
95 		  argv[1],
96 		  error->message);
97       g_error_free (error);
98       return 1;
99     }
100 
101   g_free (data);
102   g_free (free_me);
103   g_object_unref (pixbuf);
104 
105   return 0;
106 }
107 
108 static void
parse_args(gint * argc_p,gchar *** argv_p)109 parse_args (gint    *argc_p,
110 	    gchar ***argv_p)
111 {
112   guint argc = *argc_p;
113   gchar **argv = *argv_p;
114   guint i, e;
115 
116   for (i = 1; i < argc; i++)
117     {
118       if (strcmp ("-r", argv[i]) == 0 ||
119 	  strcmp ("--rle", argv[i]) == 0)
120 	{
121 	  use_rle = TRUE;
122 	  argv[i] = NULL;
123 	}
124       else if (strcmp ("-h", argv[i]) == 0 ||
125 	  strcmp ("--help", argv[i]) == 0)
126 	{
127 	  print_blurb (stderr, TRUE);
128 	  argv[i] = NULL;
129 	  exit (0);
130 	}
131       else if (strcmp ("-v", argv[i]) == 0 ||
132 	       strcmp ("--version", argv[i]) == 0)
133 	{
134 	  print_blurb (stderr, FALSE);
135 	  argv[i] = NULL;
136 	  exit (0);
137 	}
138       else if (strcmp (argv[i], "--g-fatal-warnings") == 0)
139 	{
140 	  GLogLevelFlags fatal_mask;
141 
142 	  fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
143 	  fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
144 	  g_log_set_always_fatal (fatal_mask);
145 
146 	  argv[i] = NULL;
147 	}
148     }
149 
150   e = 0;
151   for (i = 1; i < argc; i++)
152     {
153       if (e)
154 	{
155 	  if (argv[i])
156 	    {
157 	      argv[e++] = argv[i];
158 	      argv[i] = NULL;
159 	    }
160 	}
161       else if (!argv[i])
162 	e = i;
163     }
164   if (e)
165     *argc_p = e;
166 }
167 
168 static void
print_blurb(FILE * bout,gboolean print_help)169 print_blurb (FILE    *bout,
170 	     gboolean print_help)
171 {
172   if (!print_help)
173     {
174       g_fprintf (bout, "%s version ", PRG_NAME);
175       g_fprintf (bout, "%s", GDK_PIXBUF_VERSION);
176       g_fprintf (bout, "\n");
177       g_fprintf (bout, "%s comes with ABSOLUTELY NO WARRANTY.\n", PRG_NAME);
178       g_fprintf (bout, "You may redistribute copies of %s under the terms of\n", PRG_NAME);
179       g_fprintf (bout, "the GNU Lesser General Public License which can be found in the\n");
180       g_fprintf (bout, "%s source package. Sources, examples and contact\n", PKG_NAME);
181       g_fprintf (bout, "information are available at %s\n", PKG_HTTP_HOME);
182     }
183   else
184     {
185       g_fprintf (bout, "Usage: %s [options] [input-file] [output-file]\n", PRG_NAME);
186       g_fprintf (bout, "  -r, --rle                  compress the image data using RLE\n");
187       g_fprintf (bout, "  -h, --help                 show this help message\n");
188       g_fprintf (bout, "  -v, --version              print version informations\n");
189       g_fprintf (bout, "  --g-fatal-warnings         make warnings fatal (abort)\n");
190     }
191 }
192