]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/make-inline-pixbuf.c
Use g_printf instead of system printf. (#99327)
[~andy/gtk] / gdk-pixbuf / make-inline-pixbuf.c
1 /* Program to convert an image file to inline C data
2  *
3  * Copyright (C) 2000 Red Hat, Inc.
4  *
5  * Developed by Havoc Pennington <hp@redhat.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <config.h>
24 #include "gdk-pixbuf-private.h"
25 #include <glib/gprintf.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 void
31 output_int (FILE *outfile, guint32 num, const char *comment)
32 {
33   guchar bytes[4];
34
35   /* Note, most significant bytes first */
36   bytes[0] = num >> 24;
37   bytes[1] = num >> 16;
38   bytes[2] = num >> 8;
39   bytes[3] = num;
40
41   g_fprintf (outfile, "  /* %s (%u) */\n  0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x,\n",
42           comment, num,
43           bytes[0], bytes[1], bytes[2], bytes[3]);  
44 }
45
46 void
47 output_bool (FILE *outfile, gboolean val, const char *comment)
48 {
49   g_fprintf (outfile, "  /* %s (%s) */\n  0x%.2x,\n",
50           comment,
51           val ? "TRUE" : "FALSE",
52           val ? 1 : 0);
53 }
54
55 void
56 output_pixbuf (FILE *outfile, gboolean ext_symbols,
57                const char *varname,
58                GdkPixbuf *pixbuf)
59 {
60   const char *modifier;
61   const guchar *p;
62   const guchar *end;
63   gboolean has_alpha;
64   int column;
65   
66   modifier = "static ";
67   if (ext_symbols)
68     modifier = "";
69   
70   g_fprintf (outfile, "%sconst guchar ", modifier);
71   fputs (varname, outfile);
72   fputs ("[] =\n", outfile);
73   fputs ("{\n", outfile);
74
75   p = gdk_pixbuf_get_pixels (pixbuf);
76   end = p + gdk_pixbuf_get_rowstride (pixbuf) * gdk_pixbuf_get_height (pixbuf);
77   has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
78
79   /* Sync the order of writing with the order of reading in
80    * gdk-pixbuf-data.c
81    */
82   output_int (outfile, GDK_PIXBUF_INLINE_MAGIC_NUMBER, "File magic");
83   output_int (outfile, GDK_PIXBUF_INLINE_RAW, "Format of following stuff");
84   output_int (outfile, gdk_pixbuf_get_rowstride (pixbuf), "Rowstride");
85   output_int (outfile, gdk_pixbuf_get_width (pixbuf), "Width");
86   output_int (outfile, gdk_pixbuf_get_height (pixbuf), "Height");
87
88   output_bool (outfile, has_alpha, "Has an alpha channel");
89
90   output_int (outfile, gdk_pixbuf_get_colorspace (pixbuf), "Colorspace (0 == RGB, no other options implemented)");
91
92   output_int (outfile, gdk_pixbuf_get_n_channels (pixbuf), "Number of channels");
93
94   output_int (outfile, gdk_pixbuf_get_bits_per_sample (pixbuf), "Bits per sample");
95
96   fputs ("  /* Image data */\n", outfile);
97   
98   /* Copy the data in the pixbuf */
99   column = 0;
100   while (p != end)
101     {
102       guchar r, g, b, a;
103       
104       r = *p;
105       ++p;
106       g = *p;
107       ++p;
108       b = *p;
109       ++p;
110       if (has_alpha)
111         {
112           a = *p;
113           ++p;
114         }
115       else
116         a = 0;
117
118       
119       if (has_alpha)
120         g_fprintf (outfile, "  0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x", r, g, b, a);
121       else
122         g_fprintf (outfile, "  0x%.2x, 0x%.2x, 0x%.2x", r, g, b);
123
124       if (p != end)
125         fputs (",", outfile);
126       else
127         fputs ("\n", outfile);
128       
129       ++column;
130       
131       if (column > 2)
132         {
133           fputs ("\n", outfile);
134           column = 0;
135         }
136     }
137
138   fputs ("};\n\n", outfile);
139 }
140
141 void
142 usage (void)
143 {
144   g_fprintf (stderr, "Usage: make-inline-pixbuf [--extern-symbols] OUTFILE varname1 imagefile1 varname2 imagefile2 ...\n");
145   exit (1);
146 }
147
148 int
149 main (int argc, char **argv)
150 {
151   gboolean ext_symbols = FALSE;
152   FILE *outfile;
153   gchar *outfilename;
154   int i;
155   
156   g_type_init ();
157   
158   
159   if (argc < 4)
160     usage ();
161
162   i = 1;
163   if (strcmp (argv[i], "--extern-symbols") == 0)
164     {
165       ext_symbols = TRUE;
166       ++i;
167       if (argc < 5)
168         usage ();
169     }
170
171   outfile = fopen (argv[i], "w");
172   if (outfile == NULL)
173     {
174       g_fprintf (stderr, "Failed to open output file `%s': %s\n",
175                argv[i], strerror (errno));
176       exit (1);
177     }
178
179   outfilename = argv[i];
180   ++i;
181
182   fputs ("/* This file was automatically generated by the make-inline-pixbuf program.\n"
183          " * It contains inline RGB image data.\n"
184          " */\n\n", outfile);
185
186   /* Check for matched pairs of images/names */
187   if (((argc - i) % 2) != 0)
188     usage ();
189   
190   while (i < argc)
191     {
192       GdkPixbuf *pixbuf;
193       GError *error;
194
195       g_assert ((i + 1) < argc);
196
197       error = NULL;
198       pixbuf = gdk_pixbuf_new_from_file (argv[i+1], &error);
199
200       if (pixbuf == NULL)
201         {
202           g_fprintf (stderr, "%s\n", error->message);
203           fclose (outfile);
204           remove (outfilename);
205           exit (1);
206         }
207
208       output_pixbuf (outfile, ext_symbols, argv[i], pixbuf);
209       
210       gdk_pixbuf_unref (pixbuf);
211       
212       i += 2;
213     }
214   
215   fclose (outfile);
216
217   return 0;
218 }