]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/make-inline-pixbuf.c
Move more text widget headers into the private header list
[~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 <stdio.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   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   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   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         fprintf(outfile, "  0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x", r, g, b, a);
121       else
122         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   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   int i;
154   
155   gdk_pixbuf_init ();
156   
157   if (argc < 4)
158     usage ();
159
160   i = 1;
161   if (strcmp (argv[i], "--extern-symbols") == 0)
162     {
163       ext_symbols = TRUE;
164       ++i;
165       if (argc < 5)
166         usage ();
167     }
168
169   outfile = fopen (argv[i], "w");
170   if (outfile == NULL)
171     {
172       fprintf (stderr, "Failed to open output file `%s': %s\n",
173                argv[i], strerror (errno));
174       exit (1);
175     }
176
177   ++i;
178
179   fputs ("/* This file was automatically generated by the make-inline-pixbuf program.\n"
180          " * It contains inline RGB image data.\n"
181          " */\n\n", outfile);
182
183   /* Check for matched pairs of images/names */
184   if (((argc - i) % 2) != 0)
185     usage ();
186   
187   while (i < argc)
188     {
189       GdkPixbuf *pixbuf;
190
191       g_assert ((i + 1) < argc);
192       
193       pixbuf = gdk_pixbuf_new_from_file (argv[i+1]);
194
195       if (pixbuf == NULL)
196         {
197           fprintf (stderr, "Failed to open image file `%s': %s\n",
198                    argv[i+1], strerror (errno));
199
200           exit (1);
201         }
202
203       output_pixbuf (outfile, ext_symbols, argv[i], pixbuf);
204       
205       gdk_pixbuf_unref (pixbuf);
206       
207       i += 2;
208     }
209   
210   fclose (outfile);
211
212   return 0;
213 }