summaryrefslogtreecommitdiff
path: root/gtk/theme-bits/decompose-bits.c
blob: 6adf7a92b94114b4574a4f8f863fd754aee6f03a (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
/* GTK - The GTK+ Toolkit
 * Copyright (C) 2002, Owen Taylor
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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/gdk-pixbuf.h>
#include <glib/gprintf.h>
#include <stdlib.h>
#include <string.h>

#define BYTES_PER_OUTPUT_LINE 15

static gint
output_byte (guchar byte,
	     gint   online)
{
  if (online == BYTES_PER_OUTPUT_LINE)
    {
      g_printf (",\n  ");
      online = 0;
    }
  else if (online)
    {
      g_printf (",");
    }

  g_printf ("0x%02x", byte);
  return online + 1;
}

static void
do_part (GdkPixbuf  *pixbuf,
	 gint        part1_index,
	 gint        part2_index,
	 gint        part3_index,
	 const char *base_name,
	 const char *part_name)
{
  const guchar *pixels = gdk_pixbuf_get_pixels (pixbuf);
  const guchar *color1;
  const guchar *color2;
  const guchar *color3;
  gint rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  gint n_channels = gdk_pixbuf_get_n_channels (pixbuf);
  gint width = gdk_pixbuf_get_width (pixbuf);
  gint height = gdk_pixbuf_get_height (pixbuf);
  gint online = 0;

  color1 = pixels + part1_index * n_channels;
  color2 = pixels + part2_index * n_channels;
  color3 = pixels + part3_index * n_channels;
  pixels += rowstride;
  
  g_printf ("static const guchar %s_%s_bits[] = {\n", base_name, part_name);
  g_printf ("  ");

  while (height--)
    {
      guchar bit = 1;
      guchar byte = 0;
      const guchar *p = pixels;
      gint n = width;

      while (n--)
	{
	  if ((part1_index >= 0 && memcmp (p, color1, n_channels) == 0) ||
	      (part2_index >= 0 && memcmp (p, color2, n_channels) == 0) ||
	      (part3_index >= 0 && memcmp (p, color3, n_channels) == 0))
	    byte |= bit;

	  if (bit == 0x80)
	    {
	      online = output_byte (byte, online);
	      byte = 0;
	      bit = 1;
	    }
	  else
	    bit <<= 1;

	  p += n_channels;
	}

      if (width & 7)		/* a leftover partial byte */
	online = output_byte (byte, online);

      pixels += rowstride;
    }
  
  g_printf ("};\n");
}

typedef enum {
  PART_BLACK,
  PART_DARK,
  PART_MID,
  PART_LIGHT,
  PART_TEXT,
  PART_TEXT_AA,
  PART_BASE,
  PART_LAST
} Part;

static const char *part_names[PART_LAST] = {
  "black",
  "dark",
  "mid",
  "light",
  "text",
  "aa",
  "base",
};

int main (int argc, char **argv)
{
  gchar *progname = g_path_get_basename (argv[0]);
  GdkPixbuf *pixbuf;
  GError *error = NULL;
  gint i;

  if (argc != 3)
    {
      g_fprintf (stderr, "%s: Usage: %s FILE BASE\n", progname, progname);
      exit (1);
    }

  g_type_init ();
  
  pixbuf = gdk_pixbuf_new_from_file (argv[1], &error);
  if (!pixbuf)
    {
      g_fprintf (stderr, "%s: cannot open file '%s': %s\n", progname, argv[1], error->message);
      exit (1);
    }
  
  if (gdk_pixbuf_get_width (pixbuf) < PART_LAST)
    {
      g_fprintf (stderr, "%s: source image must be at least %d pixels wide\n", progname, PART_LAST);
      exit (1);
    }

  if (gdk_pixbuf_get_height (pixbuf) < 1)
    {
      g_fprintf (stderr, "%s: source image must be at least 1 pixel height\n", progname);
      exit (1);
    }

  g_printf ("/*\n * Extracted from %s, width=%d, height=%d\n */\n", argv[1],
	  gdk_pixbuf_get_width (pixbuf), gdk_pixbuf_get_height (pixbuf) - 1);

  for (i = 0; i < PART_LAST; i++)
    {
      /* As a bit of a hack, we want the base image to extend over the text
       * and text_aa parts so that we can draw the image either with or without
       * the indicator
       */
      if (i == PART_BASE)
	do_part (pixbuf, PART_BASE, PART_TEXT_AA, PART_TEXT, argv[2], part_names[i]);
      else
	do_part (pixbuf, i, -1, -1, argv[2], part_names[i]);
    }
  return 0;
}