Debian Codegraph @

gifenc/quantize.c

raw symbols SCIP
1 /* simple gif encoder
2 * Copyright (C) 2005 Benjamin Otte <[email protected]
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 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 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include <gtk/gtk.h>
23 #include <string.h>
24 #include "gifenc.h"
25
26 /*** GENERAL ***/
27
28 void
29 gifenc_palette_free (GifencPalette *palette)
30 {
31 g_return_if_fail (palette != NULL);
32
33 if (palette->free)
34 palette->free (palette->data);
35 g_free (palette);
36 }
37
38 guint
39 gifenc_palette_get_alpha_index (const GifencPalette *palette)
40 {
41 g_return_val_if_fail (palette != NULL, 0);
42 g_return_val_if_fail (palette->alpha, 0);
43
44 return palette->num_colors;
45 }
46
47 guint
48 gifenc_palette_get_num_colors (const GifencPalette *palette)
49 {
50 g_return_val_if_fail (palette != NULL, 0);
51
52 return palette->num_colors + (palette->alpha ? 1 : 0);
53 }
54
55 guint
56 gifenc_palette_get_color (const GifencPalette *palette, guint id)
57 {
58 g_return_val_if_fail (palette != NULL, 0);
59 g_return_val_if_fail (id < palette->num_colors, 0);
60
61 return palette->colors[id];
62 }
63
64 /*** SIMPLE ***/
65
66 static guint
67 gifenc_palette_simple_lookup (gpointer data, guint32 color, guint32 *resulting_color)
68 {
69 color &= 0xC0C0C0;
70 *resulting_color = color + 0x202020;
71 return ((color >> 18) & 0x30) |
72 ((color >> 12) & 0xC) |
73 ((color >> 6) & 0x3);
74 }
75
76 GifencPalette *
77 gifenc_palette_get_simple (gboolean alpha)
78 {
79 GifencPalette *palette;
80 guint r, g, b, i = 0;
81
82 palette = g_new (GifencPalette, 1);
83
84 palette->alpha = alpha;
85 palette->num_colors = 64;
86 palette->colors = g_new (guint, palette->num_colors);
87 for (r = 0; r < 4; r++) {
88 for (g = 0; g < 4; g++) {
89 for (b = 0; b < 4; b++) {
90 palette->colors[i++] = (r << 22) + (g << 14) + (b << 6) + 0x202020;
91 }
92 }
93 }
94 palette->data = GINT_TO_POINTER (alpha ? 1 : 0);
95 palette->lookup = gifenc_palette_simple_lookup;
96 palette->free = NULL;
97
98 return palette;
99 }
100
101 /*** OCTREE QUANTIZATION ***/
102
103 /* maximum number of leaves before starting color reduction */
104 #define MAX_LEAVES (12000)
105 /* maximum number of leaves before stopping a running color reduction */
106 #define STOP_LEAVES (MAX_LEAVES >> 2)
107
108 typedef struct _GifencOctree GifencOctree;
109 struct _GifencOctree {
110 GifencOctree * children[8]; /* children nodes or NULL */
111 guint level; /* how deep in tree are we? */
112 guint red; /* sum of all red pixels */
113 guint green; /* sum of green pixels */
114 guint blue; /* sum of blue pixels */
115 guint count; /* amount of pixels at this node */
116 guint32 color; /* representations (depending on value):
117 -1: random non-leaf node
118 -2: root node
119 0x1000000: leaf node with undefined color
120 0-0xFFFFFF: leaf node with defined color */
121 guint id; /* color index */
122 };
123 typedef struct {
124 GifencOctree * tree;
125 GSList * non_leaves;
126 guint num_leaves;
127 } OctreeInfo;
128 #define OCTREE_IS_LEAF(tree) ((tree)->color <= 0x1000000)
129
130 static GifencOctree *
131 gifenc_octree_new (void)
132 {
133 GifencOctree *ret = g_new0 (GifencOctree, 1);
134
135 ret->color = (guint) -1;
136 return ret;
137 }
138
139 static void
140 gifenc_octree_free (gpointer data)
141 {
142 GifencOctree *tree = data;
143 guint i;
144
145 for (i = 0; i < 8; i++) {
146 if (tree->children[i])
147 gifenc_octree_free (tree->children[i]);
148 }
149 g_free (tree);
150 }
151
152 #if 0
153 #define PRINT_NON_LEAVES 1
154 static void
155 gifenc_octree_print (GifencOctree *tree, guint flags)
156 {
157 #define FLAG_SET(flag) (flags & (flag))
158 if (OCTREE_IS_LEAF (tree)) {
159 g_print ("%*s %6d %2X-%2X-%2X\n", tree->level * 2, "", tree->count,
160 tree->red / tree->count, tree->green / tree->count, tree->blue / tree->count);
161 } else {
162 guint i;
163 if (FLAG_SET(PRINT_NON_LEAVES))
164 g_print ("%*s %6d\n", tree->level * 2, "", tree->count);
165 g_assert (tree->red == 0);
166 g_assert (tree->green == 0);
167 g_assert (tree->blue == 0);
168 for (i = 0; i < 8; i++) {
169 if (tree->children[i])
170 gifenc_octree_print (tree->children[i], flags);
171 }
172 }
173 #undef FLAG_SET
174 }
175 #endif
176
177 static guint
178 color_to_index (guint color, guint level)
179 {
180 guint ret;
181
182 g_assert (level < 8);
183
184 color >>= (7 - level);
185 ret = (color & 0x10000) ? 4 : 0;
186 if (color & 0x100)
187 ret += 2;
188 if (color & 0x1)
189 ret ++;
190 return ret;
191 }
192
193 static void
194 gifenc_octree_add_one (GifencOctree *tree, guint32 color, guint count)
195 {
196 tree->red += ((color >> 16) & 0xFF) * count;
197 tree->green += ((color >> 8) & 0xFF) * count;
198 tree->blue += (color & 0xFF) * count;
199 }
200
201 static void
202 gifenc_octree_add_color (OctreeInfo *info, guint32 color, guint count)
203 {
204 guint i;
205 GifencOctree *tree = info->tree;
206
207 color &= 0xFFFFFF;
208
209 for (;;) {
210 tree->count += count;
211 if (tree->level == 8 || OCTREE_IS_LEAF (tree)) {
212 if (tree->color < 0x1000000 && tree->color != color) {
213 GifencOctree *new = gifenc_octree_new ();
214 new->level = tree->level + 1;
215 new->count = tree->count - count;
216 new->red = tree->red; tree->red = 0;
217 new->green = tree->green; tree->green = 0;
218 new->blue = tree->blue; tree->blue = 0;
219 new->color = tree->color; tree->color = (guint) -1;
220 i = color_to_index (new->color, tree->level);
221 tree->children[i] = new;
222 info->non_leaves = g_slist_prepend (info->non_leaves, tree);
223 } else {
224 gifenc_octree_add_one (tree, color, count);
225 return;
226 }
227 }
228 i = color_to_index (color, tree->level);
229 if (tree->children[i]) {
230 tree = tree->children[i];
231 } else {
232 GifencOctree *new = gifenc_octree_new ();
233 new->level = tree->level + 1;
234 gifenc_octree_add_one (new, color, count);
235 new->count = count;
236 new->color = color;
237 tree->children[i] = new;
238 info->num_leaves++;
239 return;
240 }
241 }
242 }
243
244 static int
245 octree_compare_count (gconstpointer a, gconstpointer b)
246 {
247 return ((const GifencOctree *) a)->count - ((const GifencOctree *) b)->count;
248 }
249
250 static void
251 gifenc_octree_reduce_one (OctreeInfo *info, GifencOctree *tree)
252 {
253 guint i;
254
255 g_assert (!OCTREE_IS_LEAF (tree));
256 for (i = 0; i < 8; i++) {
257 if (!tree->children[i])
258 continue;
259 g_assert (OCTREE_IS_LEAF (tree->children[i]));
260 tree->red += tree->children[i]->red;
261 tree->green += tree->children[i]->green;
262 tree->blue += tree->children[i]->blue;
263 gifenc_octree_free (tree->children[i]);
264 tree->children[i] = NULL;
265 info->num_leaves--;
266 }
267 tree->color = 0x1000000;
268 info->num_leaves++;
269 info->non_leaves = g_slist_remove (info->non_leaves, tree);
270 }
271
272 static void
273 gifenc_octree_reduce_colors (OctreeInfo *info, guint stop)
274 {
275 info->non_leaves = g_slist_sort (info->non_leaves, octree_compare_count);
276 //g_print ("reducing %u leaves (%u non-leaves)\n", info->num_leaves,
277 // g_slist_length (info->non_leaves));
278 while (info->num_leaves > stop) {
279 gifenc_octree_reduce_one (info, info->non_leaves->data);
280 }
281 //g_print (" ==> to %u leaves\n", info->num_leaves);
282 }
283
284 static guint
285 gifenc_octree_finalize (GifencOctree *tree, guint start_id, guint *colors)
286 {
287 if (OCTREE_IS_LEAF (tree)) {
288 if (tree->color > 0xFFFFFF)
289 tree->color =
290 ((tree->red / tree->count) << 16) |
291 ((tree->green / tree->count) << 8) |
292 (tree->blue / tree->count);
293 tree->id = start_id;
294 colors[start_id] = tree->color;
295 return tree->id + 1;
296 } else {
297 guint i;
298 for (i = 0; i < 8; i++) {
299 if (tree->children[i])
300 start_id = gifenc_octree_finalize (tree->children[i], start_id, colors);
301 }
302 return start_id;
303 }
304 g_assert_not_reached ();
305 return 0;
306 }
307
308 static guint
309 gifenc_octree_lookup (gpointer data, guint32 color, guint32 *looked_up_color)
310 {
311 GifencOctree *tree = data;
312 guint idx;
313
314 if (OCTREE_IS_LEAF (tree)) {
315 *looked_up_color = tree->color;
316 return tree->id;
317 }
318 idx = color_to_index (color, tree->level);
319 if (tree->children[idx] == NULL) {
320 static const guint order[8][7] = {
321 { 2, 1, 4, 3, 6, 5, 7 },
322 { 3, 0, 5, 2, 7, 4, 6 },
323 { 0, 3, 6, 1, 4, 7, 5 },
324 { 1, 2, 7, 6, 5, 0, 4 },
325 { 6, 5, 0, 7, 2, 1, 3 },
326 { 7, 4, 1, 6, 3, 0, 2 },
327 { 4, 7, 2, 5, 0, 3, 1 },
328 { 5, 6, 3, 4, 1, 2, 0 }
329 };
330 guint i, tmp;
331 for (i = 0; i < 7; i++) {
332 tmp = order[idx][i];
333 if (!tree->children[tmp])
334 continue;
335 /* make selection smarter, like using closest match */
336 return gifenc_octree_lookup (
337 tree->children[tmp],
338 color, looked_up_color);
339 }
340 g_assert_not_reached ();
341 }
342 return gifenc_octree_lookup (
343 tree->children[idx],
344 color, looked_up_color);
345 }
346
347 GifencPalette *
348 gifenc_quantize_image (const guint8 *data, guint width, guint height,
349 guint rowstride, gboolean alpha, guint max_colors)
350 {
351 guint x, y;
352 const guint32 *row;
353 OctreeInfo info = { NULL, NULL, 0 };
354 GifencPalette *palette;
355
356 g_return_val_if_fail (width * height <= (G_MAXUINT >> 8), NULL);
357
358 info.tree = gifenc_octree_new ();
359 info.tree->color = (guint) -2; /* special node */
360
361 if (TRUE) {
362 guint r, g, b;
363 static const guint8 colors[] = { 0, 85, 170, 255 };
364 for (r = 0; r < 4; r++) {
365 for (g = 0; g < 4; g++) {
366 for (b = 0; b < 4; b++) {
367 gifenc_octree_add_color (&info,
368 (colors[r] << 16) + (colors[g] << 8) + colors[b], 1);
369 }
370 }
371 }
372 }
373
374 for (y = 0; y < height; y++) {
375 row = (const guint32 *) (void *) data;
376 for (x = 0; x < width; x++) {
377 gifenc_octree_add_color (&info, row[x] & 0xFFFFFF, 1);
378 }
379 //if (info.num_leaves > MAX_LEAVES)
380 // gifenc_octree_reduce_colors (&info, STOP_LEAVES);
381 data += rowstride;
382 }
383 //gifenc_octree_print (info.tree, 1);
384 gifenc_octree_reduce_colors (&info, max_colors - (alpha ? 1 : 0));
385
386 //gifenc_octree_print (info.tree, 1);
387 //g_print ("total: %u colors (%u non-leaves)\n", info.num_leaves,
388 // g_slist_length (info.non_leaves));
389
390 palette = g_new (GifencPalette, 1);
391 palette->alpha = alpha;
392 palette->colors = g_new (guint, info.num_leaves);
393 palette->num_colors = info.num_leaves;
394 palette->data = info.tree;
395 palette->lookup = gifenc_octree_lookup;
396 palette->free = gifenc_octree_free;
397
398 gifenc_octree_finalize (info.tree, 0, palette->colors);
399 g_slist_free (info.non_leaves);
400
401 return (GifencPalette *) palette;
402 }
403