summaryrefslogtreecommitdiff
path: root/gdk-pixbuf/make-inline-pixbuf.c
blob: 445fbd88a9a634f687d9091654ec93938662baff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* Program to convert an image file to inline C data
 *
 * Copyright (C) 2000 Red Hat, Inc.
 *
 * Developed by Havoc Pennington <hp@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include "gdk-pixbuf-private.h"
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

void
output_int (FILE *outfile, guint32 num, const char *comment)
{
  guchar bytes[4];

  /* Note, most significant bytes first */
  bytes[0] = num >> 24;
  bytes[1] = num >> 16;
  bytes[2] = num >> 8;
  bytes[3] = num;

  fprintf(outfile, "  /* %s (%u) */\n  0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x,\n",
          comment, num,
          bytes[0], bytes[1], bytes[2], bytes[3]);  
}

void
output_bool (FILE *outfile, gboolean val, const char *comment)
{
  fprintf(outfile, "  /* %s (%s) */\n  0x%.2x,\n",
          comment,
          val ? "TRUE" : "FALSE",
          val ? 1 : 0);
}

void
output_pixbuf (FILE *outfile, gboolean ext_symbols,
               const char *varname,
               GdkPixbuf *pixbuf)
{
  const char *modifier;
  const guchar *p;
  const guchar *end;
  gboolean has_alpha;
  int column;
  
  modifier = "static ";
  if (ext_symbols)
    modifier = "";
  
  fprintf (outfile, "%sconst guchar ", modifier);
  fputs (varname, outfile);
  fputs ("[] =\n", outfile);
  fputs ("{\n", outfile);

  p = gdk_pixbuf_get_pixels (pixbuf);
  end = p + gdk_pixbuf_get_rowstride (pixbuf) * gdk_pixbuf_get_height (pixbuf);
  has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);

  /* Sync the order of writing with the order of reading in
   * gdk-pixbuf-data.c
   */
  output_int (outfile, GDK_PIXBUF_INLINE_MAGIC_NUMBER, "File magic");
  output_int (outfile, GDK_PIXBUF_INLINE_RAW, "Format of following stuff");
  output_int (outfile, gdk_pixbuf_get_rowstride (pixbuf), "Rowstride");
  output_int (outfile, gdk_pixbuf_get_width (pixbuf), "Width");
  output_int (outfile, gdk_pixbuf_get_height (pixbuf), "Height");

  output_bool (outfile, has_alpha, "Has an alpha channel");

  output_int (outfile, gdk_pixbuf_get_colorspace (pixbuf), "Colorspace (0 == RGB, no other options implemented)");

  output_int (outfile, gdk_pixbuf_get_n_channels (pixbuf), "Number of channels");

  output_int (outfile, gdk_pixbuf_get_bits_per_sample (pixbuf), "Bits per sample");

  fputs ("  /* Image data */\n", outfile);
  
  /* Copy the data in the pixbuf */
  column = 0;
  while (p != end)
    {
      guchar r, g, b, a;
      
      r = *p;
      ++p;
      g = *p;
      ++p;
      b = *p;
      ++p;
      if (has_alpha)
        {
          a = *p;
          ++p;
        }
      else
        a = 0;

      
      if (has_alpha)
        fprintf(outfile, "  0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x", r, g, b, a);
      else
        fprintf(outfile, "  0x%.2x, 0x%.2x, 0x%.2x", r, g, b);

      if (p != end)
        fputs (",", outfile);
      else
        fputs ("\n", outfile);
      
      ++column;
      
      if (column > 2)
        {
          fputs ("\n", outfile);
          column = 0;
        }
    }

  fputs ("};\n\n", outfile);
}

void
usage (void)
{
  fprintf (stderr, "Usage: make-inline-pixbuf [--extern-symbols] OUTFILE varname1 imagefile1 varname2 imagefile2 ...\n");
  exit (1);
}

int
main (int argc, char **argv)
{
  gboolean ext_symbols = FALSE;
  FILE *outfile;
  int i;
  
  gdk_pixbuf_init ();
  
  if (argc < 4)
    usage ();

  i = 1;
  if (strcmp (argv[i], "--extern-symbols") == 0)
    {
      ext_symbols = TRUE;
      ++i;
      if (argc < 5)
        usage ();
    }

  outfile = fopen (argv[i], "w");
  if (outfile == NULL)
    {
      fprintf (stderr, "Failed to open output file `%s': %s\n",
               argv[i], strerror (errno));
      exit (1);
    }

  ++i;

  fputs ("/* This file was automatically generated by the make-inline-pixbuf program.\n"
         " * It contains inline RGB image data.\n"
         " */\n\n", outfile);

  /* Check for matched pairs of images/names */
  if (((argc - i) % 2) != 0)
    usage ();
  
  while (i < argc)
    {
      GdkPixbuf *pixbuf;
      GError *error;

      g_assert ((i + 1) < argc);

      error = NULL;
      pixbuf = gdk_pixbuf_new_from_file (argv[i+1], &error);

      if (pixbuf == NULL)
        {
          fprintf (stderr, "%s\n", error->message);

          exit (1);
        }

      output_pixbuf (outfile, ext_symbols, argv[i], pixbuf);
      
      gdk_pixbuf_unref (pixbuf);
      
      i += 2;
    }
  
  fclose (outfile);

  return 0;
}