Debian Codegraph @

gtk/theme-bits/decompose-bits.c

raw
1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2002, Owen Taylor
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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 #include "config.h"
21 #include <gdk-pixbuf/gdk-pixbuf.h>
22 #include <glib/gprintf.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #define BYTES_PER_OUTPUT_LINE 15
27
28 static gint
29 output_byte (guchar byte,
30 gint online)
31 {
32 if (online == BYTES_PER_OUTPUT_LINE)
33 {
34 g_printf (",\n ");
35 online = 0;
36 }
37 else if (online)
38 {
39 g_printf (",");
40 }
41
42 g_printf ("0x%02x", byte);
43 return online + 1;
44 }
45
46 static void
47 do_part (GdkPixbuf *pixbuf,
48 gint part1_index,
49 gint part2_index,
50 gint part3_index,
51 const char *base_name,
52 const char *part_name)
53 {
54 const guchar *pixels = gdk_pixbuf_get_pixels (pixbuf);
55 const guchar *color1;
56 const guchar *color2;
57 const guchar *color3;
58 gint rowstride = gdk_pixbuf_get_rowstride (pixbuf);
59 gint n_channels = gdk_pixbuf_get_n_channels (pixbuf);
60 gint width = gdk_pixbuf_get_width (pixbuf);
61 gint height = gdk_pixbuf_get_height (pixbuf);
62 gint online = 0;
63
64 color1 = pixels + part1_index * n_channels;
65 color2 = pixels + part2_index * n_channels;
66 color3 = pixels + part3_index * n_channels;
67 pixels += rowstride;
68
69 g_printf ("static const guchar %s_%s_bits[] = {\n", base_name, part_name);
70 g_printf (" ");
71
72 while (height--)
73 {
74 guchar bit = 1;
75 guchar byte = 0;
76 const guchar *p = pixels;
77 gint n = width;
78
79 while (n--)
80 {
81 if ((part1_index >= 0 && memcmp (p, color1, n_channels) == 0) ||
82 (part2_index >= 0 && memcmp (p, color2, n_channels) == 0) ||
83 (part3_index >= 0 && memcmp (p, color3, n_channels) == 0))
84 byte |= bit;
85
86 if (bit == 0x80)
87 {
88 online = output_byte (byte, online);
89 byte = 0;
90 bit = 1;
91 }
92 else
93 bit <<= 1;
94
95 p += n_channels;
96 }
97
98 if (width & 7) /* a leftover partial byte */
99 online = output_byte (byte, online);
100
101 pixels += rowstride;
102 }
103
104 g_printf ("};\n");
105 }
106
107 typedef enum {
108 PART_BLACK,
109 PART_DARK,
110 PART_MID,
111 PART_LIGHT,
112 PART_TEXT,
113 PART_TEXT_AA,
114 PART_BASE,
115 PART_LAST
116 } Part;
117
118 static const char *part_names[PART_LAST] = {
119 "black",
120 "dark",
121 "mid",
122 "light",
123 "text",
124 "aa",
125 "base",
126 };
127
128 int main (int argc, char **argv)
129 {
130 gchar *progname = g_path_get_basename (argv[0]);
131 GdkPixbuf *pixbuf;
132 GError *error = NULL;
133 gint i;
134
135 if (argc != 3)
136 {
137 g_fprintf (stderr, "%s: Usage: %s FILE BASE\n", progname, progname);
138 exit (1);
139 }
140
141 g_type_init ();
142
143 pixbuf = gdk_pixbuf_new_from_file (argv[1], &error);
144 if (!pixbuf)
145 {
146 g_fprintf (stderr, "%s: cannot open file '%s': %s\n", progname, argv[1], error->message);
147 exit (1);
148 }
149
150 if (gdk_pixbuf_get_width (pixbuf) < PART_LAST)
151 {
152 g_fprintf (stderr, "%s: source image must be at least %d pixels wide\n", progname, PART_LAST);
153 exit (1);
154 }
155
156 if (gdk_pixbuf_get_height (pixbuf) < 1)
157 {
158 g_fprintf (stderr, "%s: source image must be at least 1 pixel height\n", progname);
159 exit (1);
160 }
161
162 g_printf ("/*\n * Extracted from %s, width=%d, height=%d\n */\n", argv[1],
163 gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf) - 1);
164
165 for (i = 0; i < PART_LAST; i++)
166 {
167 /* As a bit of a hack, we want the base image to extend over the text
168 * and text_aa parts so that we can draw the image either with or without
169 * the indicator
170 */
171 if (i == PART_BASE)
172 do_part (pixbuf, PART_BASE, PART_TEXT_AA, PART_TEXT, argv[2], part_names[i]);
173 else
174 do_part (pixbuf, i, -1, -1, argv[2], part_names[i]);
175 }
176 return 0;
177 }