]> Dogcows Code - chaz/openbox/blob - otk/pseudorendercontrol.cc
use the same alogo to pick a color in allocateColor as in reduceDepth, break it off...
[chaz/openbox] / 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 char *p = (char *)data;
128 int x, y;
129 for (y = 0; y < im->height; y++) {
130 for (x = 0; x < im->width; x++) {
131 p[x] = pickColor(data[x] >> default_red_shift,
132 data[x] >> default_green_shift,
133 data[x] >> default_blue_shift)->pixel;
134 }
135 data += im->width;
136 p += im->bytes_per_line;
137 }
138
139 }
140
141 void PseudoRenderControl::allocateColor(XColor *color) const
142 {
143 const XColor *c = pickColor(color->red, color->blue, color->green);
144
145 color->red = c->red;
146 color->green = c->green;
147 color->blue = c->blue;
148 color->pixel = c->pixel;
149
150 if (XAllocColor(**display, display->screenInfo(_screen)->colormap(), color))
151 color->flags = DoRed|DoGreen|DoBlue; // mark as alloced
152 else
153 assert(false); // wtf has gone wrong, its already alloced for chissake!
154
155 return;
156 }
157
158 }
This page took 0.04476 seconds and 5 git commands to generate.