1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
5 #include "pseudorendercontrol.hh"
7 #include "screeninfo.hh"
9 #include "rendertexture.hh"
14 #endif // HAVE_STDLIB_H
16 #include "../src/gettext.h"
17 #define _(str) gettext(str)
22 PseudoRenderControl::PseudoRenderControl(int screen)
23 : RenderControl(screen)
25 printf("Initializing PseudoColor RenderControl\n");
26 const ScreenInfo *info = display->screenInfo(_screen);
27 int depth = info->depth();
29 // determine the number of colors and the bits-per-color
30 int bpc = 2; // XXX THIS SHOULD BE A USER OPTION
32 _ncolors = 1 << (bpc * 3);
34 if (_ncolors > 1 << depth) {
36 _("PseudoRenderControl: Invalid colormap size. Resizing.\n"));
37 bpc = 1 << (depth/3) >> 3;
38 _ncolors = 1 << (bpc * 3);
42 _colors = new XColor[_ncolors];
44 int cpc = 1 << bpc; // colors per channel
45 for (int n = _ncolors - 1,
46 r = (1 << (bpc + 1)) -1, i = 0; i < cpc; r >>= 1, ++i)
47 for (int g = (1 << (bpc + 1)) -1, j = 0; j < cpc; g >>= 1, ++j)
48 for (int b = (1 << (bpc + 1)) -1, k = 0; k < cpc; b >>= 1, ++k, --n) {
49 _colors[n].red = r | r << 8;
50 _colors[n].green = g | g << 8;
51 _colors[n].blue = b | b << 8;
52 _colors[n].flags = DoRed|DoGreen|DoBlue; // used to track allocation
55 // allocate the colors
56 for (int i = 0; i < _ncolors; i++)
57 if (!XAllocColor(**display, info->colormap(), &_colors[i]))
58 _colors[i].flags = 0; // mark it as unallocated
60 // try allocate any colors that failed allocation above
62 // get the allocated values from the X server (only the first 256 XXX why!?)
64 int incolors = (((1 << depth) > 256) ? 256 : (1 << depth));
65 for (int i = 0; i < incolors; i++)
67 XQueryColors(**display, info->colormap(), icolors, incolors);
69 // try match unallocated ones
70 for (int i = 0; i < _ncolors; i++) {
71 if (!_colors[i].flags) { // if it wasn't allocated...
72 unsigned long closest = 0xffffffff, close = 0;
73 for (int ii = 0; ii < incolors; ii++) {
75 int r = (_colors[i].red - icolors[ii].red) & 0xff;
76 int g = (_colors[i].green - icolors[ii].green) & 0xff;
77 int b = (_colors[i].blue - icolors[ii].blue) & 0xff;
78 // find a weighted absolute deviation
79 unsigned long dev = (r * r) + (g * g) + (b * b);
87 _colors[i].red = icolors[close].red;
88 _colors[i].green = icolors[close].green;
89 _colors[i].blue = icolors[close].blue;
91 // try alloc this closest color, it had better succeed!
92 if (XAllocColor(**display, info->colormap(), &_colors[i]))
93 _colors[i].flags = DoRed|DoGreen|DoBlue; // mark as alloced
95 assert(false); // wtf has gone wrong, its already alloced for chissake!
100 PseudoRenderControl::~PseudoRenderControl()
102 printf("Destroying PseudoColor RenderControl\n");
107 void PseudoRenderControl::reduceDepth(Surface &sf, XImage *im) const