7af57192461443d65b5a5ac21df4a9aa51fa4dd9
[dana/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 inline const XColor *PseudoRenderControl::pickColor(int r, int g, int b) const
117 {
118   r = (r & 0xff) >> (8-_bpc);
119   g = (g & 0xff) >> (8-_bpc);
120   b = (b & 0xff) >> (8-_bpc);
121   return &_colors[(r << (2*_bpc)) + (g << (1*_bpc)) + b];
122 }
123
124 void PseudoRenderControl::reduceDepth(Surface &sf, XImage *im) const
125 {
126   pixel32 *data = sf.pixelData();
127   pixel32 *ret = (pixel32*)malloc(im->width * im->height * 4);
128   char *p = (char *)ret;
129   int x, y;
130   for (y = 0; y < im->height; y++) {
131     for (x = 0; x < im->width; x++) {
132       p[x] = pickColor(data[x] >> default_red_shift,
133                        data[x] >> default_green_shift,
134                        data[x] >> default_blue_shift)->pixel;
135     }
136     data += im->width;
137     p += im->bytes_per_line;
138   }
139   im->data = (char*)ret;
140 }
141
142 void PseudoRenderControl::allocateColor(XColor *color) const
143 {
144   const XColor *c = pickColor(color->red, color->blue, color->green);
145
146   color->red = c->red;
147   color->green = c->green;
148   color->blue = c->blue;
149   color->pixel = c->pixel;
150
151   if (XAllocColor(**display, display->screenInfo(_screen)->colormap(), color))
152     color->flags = DoRed|DoGreen|DoBlue; // mark as alloced
153   else
154     assert(false); // wtf has gone wrong, its already alloced for chissake!
155
156   return;
157 }
158
159 }