]> git.saurik.com Git - wxWidgets.git/blame - src/gdk_imlib/colors.c
couple of minor bug fixes/enhancements (interface unchanged)
[wxWidgets.git] / src / gdk_imlib / colors.c
CommitLineData
c801d85f 1#define _GNU_SOURCE
1f0299c1
KB
2#include "../gdk_imlib/gdk_imlib.h"
3#include "../gdk_imlib/gdk_imlib_private.h"
c801d85f
KB
4
5void
6g_PaletteAlloc(int num, int *cols)
7{
8 XColor xcl;
9 int i;
10 int r, g, b;
11
12 if (id->palette)
13 free(id->palette);
14 id->palette = malloc(sizeof(GdkImlibColor) * num);
15 if (id->palette_orig)
16 free(id->palette_orig);
17 id->palette_orig = malloc(sizeof(GdkImlibColor) * num);
18 for (i = 0; i < num; i++)
19 {
20 r = cols[(i * 3) + 0];
21 g = cols[(i * 3) + 1];
22 b = cols[(i * 3) + 2];
23 xcl.red = (unsigned short)((r << 8) | (r));
24 xcl.green = (unsigned short)((g << 8) | (g));
25 xcl.blue = (unsigned short)((b << 8) | (b));
26 xcl.flags = DoRed | DoGreen | DoBlue;
27 XAllocColor(id->x.disp, id->x.root_cmap, &xcl);
28 id->palette[i].r = xcl.red >> 8;
29 id->palette[i].g = xcl.green >> 8;
30 id->palette[i].b = xcl.blue >> 8;
31 id->palette[i].pixel = xcl.pixel;
32 id->palette_orig[i].r = r;
33 id->palette_orig[i].g = g;
34 id->palette_orig[i].b = b;
35 id->palette_orig[i].pixel = xcl.pixel;
36 }
37 id->num_colors = num;
38}
39
40gint
41gdk_imlib_load_colors(char *file)
42{
43 FILE *f;
44 char s[256];
45 int i;
46 int pal[768];
47 int r, g, b;
48 int rr, gg, bb;
49
50 f = fopen(file, "r");
51 if (!f)
52 {
53 fprintf(stderr, "GImLib ERROR: Cannot find palette file %s\n", file);
54 return 0;
55 }
56 i = 0;
57 while (fgets(s, 256, f))
58 {
59 if (s[0] == '0')
60 {
61 sscanf(s, "%x %x %x", &r, &g, &b);
62 if (r < 0)
63 r = 0;
64 if (r > 255)
65 r = 255;
66 if (g < 0)
67 g = 0;
68 if (g > 255)
69 g = 255;
70 if (b < 0)
71 b = 0;
72 if (b > 255)
73 b = 255;
74 pal[i++] = r;
75 pal[i++] = g;
76 pal[i++] = b;
77 }
78 if (i >= 768)
79 break;
80 }
81 fclose(f);
82 g_PaletteAlloc((i / 3), pal);
83 if (id->fast_rgb)
84 free(id->fast_rgb);
85 id->fast_rgb = malloc(sizeof(int) * 32 * 32 * 32);
86
87 for (r = 0; r < 32; r++)
88 {
89 for (g = 0; g < 32; g++)
90 {
91 for (b = 0; b < 32; b++)
92 {
93 rr = (r << 3) | (r >> 2);
94 gg = (g << 3) | (g >> 2);
95 bb = (b << 3) | (b >> 2);
96 INDEX_RGB(r, g, b) = gindex_best_color_match(&rr, &gg, &bb);
97 }
98 }
99 }
100 return 1;
101}
102
103void
104gdk_imlib_free_colors()
105{
106 int i;
107 unsigned long pixels[256];
108
109 for (i = 0; i < id->num_colors; i++)
110 pixels[i] = id->palette[i].pixel;
111 XFreeColors(id->x.disp, id->x.root_cmap, pixels, id->num_colors, 0);
112 id->num_colors = 0;
113}