]> icculus.org git repositories - manmower/openbox.git/blob - otk/pseudorendercontrol.cc
allocate colors in pseudocolor from the map we allocate in the rendercontrol
[manmower/openbox.git] / otk / pseudorendercontrol.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 #include "pseudorendercontrol.hh"
6 #include "display.hh"
7 #include "screeninfo.hh"
8 #include "surface.hh"
9 #include "rendertexture.hh"
10
11 extern "C" {
12 #ifdef    HAVE_STDLIB_H
13 #  include <stdlib.h>
14 #endif // HAVE_STDLIB_H
15
16 #include "../src/gettext.h"
17 #define _(str) gettext(str)
18 }
19
20 namespace otk {
21
22 PseudoRenderControl::PseudoRenderControl(int screen)
23   : RenderControl(screen)
24 {
25   printf("Initializing PseudoColor RenderControl\n");
26   const ScreenInfo *info = display->screenInfo(_screen);
27   int depth = info->depth();
28
29   // determine the number of colors and the bits-per-color
30   _bpc = 2; // XXX THIS SHOULD BE A USER OPTION
31   assert(_bpc >= 1);
32   _ncolors = 1 << (_bpc * 3);
33
34   if (_ncolors > 1 << depth) {
35     fprintf(stderr,
36             _("PseudoRenderControl: Invalid colormap size. Resizing.\n"));
37     _bpc = 1 << (depth/3) >> 3;
38     _ncolors = 1 << (_bpc * 3);
39   }
40
41   // build a color cube
42   _colors = new XColor[_ncolors];
43 int tr, tg, tb;
44   int cpc = 1 << _bpc; // colors per channel
45   for (int n = 0,
46          r = 0; r < cpc; r++)
47     for (int g = 0; g < cpc; g++)
48       for (int b = 0; b < cpc; b++, n++) {
49         tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
50         tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
51         tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
52         _colors[n].red = tr | tr << 8;
53         _colors[n].green = tg | tg << 8;
54         _colors[n].blue = tb | tb << 8;
55         _colors[n].flags = DoRed|DoGreen|DoBlue; // used to track allocation
56       }
57
58   // allocate the colors
59   for (int i = 0; i < _ncolors; i++)
60     if (!XAllocColor(**display, info->colormap(), &_colors[i]))
61       _colors[i].flags = 0; // mark it as unallocated
62
63   // try allocate any colors that failed allocation above
64
65   // get the allocated values from the X server (only the first 256 XXX why!?)
66   XColor icolors[256];
67   int incolors = (((1 << depth) > 256) ? 256 : (1 << depth));
68   for (int i = 0; i < incolors; i++)
69     icolors[i].pixel = i;
70   XQueryColors(**display, info->colormap(), icolors, incolors);
71
72   // try match unallocated ones
73   for (int i = 0; i < _ncolors; i++) {
74     if (!_colors[i].flags) { // if it wasn't allocated...
75       unsigned long closest = 0xffffffff, close = 0;
76       for (int ii = 0; ii < incolors; ii++) {
77         // find deviations
78         int r = (_colors[i].red - icolors[ii].red) & 0xff;
79         int g = (_colors[i].green - icolors[ii].green) & 0xff;
80         int b = (_colors[i].blue - icolors[ii].blue) & 0xff;
81         // find a weighted absolute deviation
82         unsigned long dev = (r * r) + (g * g) + (b * b);
83
84         if (dev < closest) {
85           closest = dev;
86           close = ii;
87         }
88       }
89
90       _colors[i].red = icolors[close].red;
91       _colors[i].green = icolors[close].green;
92       _colors[i].blue = icolors[close].blue;
93       _colors[i].pixel = icolors[close].pixel;
94
95       // try alloc this closest color, it had better succeed!
96       if (XAllocColor(**display, info->colormap(), &_colors[i]))
97         _colors[i].flags = DoRed|DoGreen|DoBlue; // mark as alloced
98       else
99         assert(false); // wtf has gone wrong, its already alloced for chissake!
100     }
101   }
102 }
103
104 PseudoRenderControl::~PseudoRenderControl()
105 {
106   printf("Destroying PseudoColor RenderControl\n");
107
108   unsigned long *pixels = new unsigned long [_ncolors], *p = pixels;
109   for (int i = 0; i < _ncolors; ++i, ++p)
110     *p = _colors[i].pixel;
111   XFreeColors(**display, display->screenInfo(_screen)->colormap(), pixels,
112               _ncolors, 0);
113   delete [] _colors;
114 }
115
116 void PseudoRenderControl::reduceDepth(Surface &sf, XImage *im) const
117 {
118   pixel32 *data = sf.pixelData();
119   char *p = (char *)data;
120   int x, y, r, g, b;
121     for (y = 0; y < im->height; y++) {
122       for (x = 0; x < im->width; x++) {
123         r = (data[x] >> default_red_shift) & 0xFF;
124         r = r >> (8-_bpc);
125         g = (data[x] >> default_green_shift) & 0xFF;
126         g = g >> (8-_bpc);
127         b = (data[x] >> default_blue_shift) & 0xFF;
128         b = b >> (8-_bpc);
129         p[x] = _colors[(r << (2*_bpc)) + (g << (1*_bpc)) + b].pixel;
130       }
131       data += im->width;
132       p += im->bytes_per_line;
133     }
134
135 }
136
137 void PseudoRenderControl::allocateColor(XColor *color) const
138 {
139   const ScreenInfo *info = display->screenInfo(_screen);
140   int depth = info->depth();
141
142   // get the allocated values from the X server (only the first 256 XXX why!?)
143   XColor icolors[256];
144   int incolors = (((1 << depth) > 256) ? 256 : (1 << depth));
145   for (int i = 0; i < incolors; i++)
146     icolors[i].pixel = i;
147   XQueryColors(**display, info->colormap(), icolors, incolors);
148
149   unsigned long closest = 0xffffffff, close = 0;
150   for (int ii = 0; ii < incolors; ii++) {
151     // find deviations
152     int r = (color->red - icolors[ii].red) & 0xff;
153     int g = (color->green - icolors[ii].green) & 0xff;
154     int b = (color->blue - icolors[ii].blue) & 0xff;
155     // find a weighted absolute deviation
156     unsigned long dev = (r * r) + (g * g) + (b * b);
157
158     if (dev < closest) {
159       closest = dev;
160       close = ii;
161     }
162   }
163
164   color->red = icolors[close].red;
165   color->green = icolors[close].green;
166   color->blue = icolors[close].blue;
167   color->pixel = icolors[close].pixel;
168
169   // try alloc this closest color, it had better succeed!
170   if (XAllocColor(**display, info->colormap(), color)) {
171     color->flags = DoRed|DoGreen|DoBlue; // mark as alloced
172   }
173   else
174     assert(false); // wtf has gone wrong, its already alloced for chissake!
175 }
176
177 }