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 _bpc = 2; // XXX THIS SHOULD BE A USER OPTION
31 _ncolors = 1 << (_bpc * 3);
33 if (_ncolors > 1 << depth) {
35 _("PseudoRenderControl: Invalid colormap size. Resizing.\n"));
36 _bpc = 1 << (depth/3) >> 3;
37 _ncolors = 1 << (_bpc * 3);
42 if (!(_colors = new XColor[_ncolors])) {
44 _("PseudoRenderControl: error allocating colormap\n"));
49 for (int n = _ncolors - 1,
50 r = (1 << (_bpc + 1)) -1, i = 0; i < _cpc; r >>= 1, ++i)
51 for (int g = (1 << (_bpc + 1)) -1, j = 0; j < _cpc; g >>= 1, ++j)
52 for (int b = (1 << (_bpc + 1)) -1, k = 0; k < _cpc; b >>= 1, ++k, --n) {
53 _colors[n].red = r | r << 8;
54 _colors[n].green = g | g << 8;
55 _colors[n].blue = b | b << 8;
56 _colors[n].flags = DoRed|DoGreen|DoBlue; // used to track allocation
59 // allocate the colors
60 for (int i = 0; i < _ncolors; i++)
61 if (!XAllocColor(**display, info->colormap(), &_colors[i]))
62 _colors[i].flags = 0; // mark it as unallocated
64 // try allocate any colors that failed allocation above
66 // get the allocated values from the X server (only the first 256 XXX why!?)
68 int incolors = (((1 << depth) > 256) ? 256 : (1 << depth));
69 for (int i = 0; i < incolors; i++)
71 XQueryColors(**display, info->colormap(), icolors, incolors);
73 // try match unallocated ones
74 for (int i = 0; i < _ncolors; i++) {
75 if (!_colors[i].flags) { // if it wasn't allocated...
76 unsigned long closest = 0xffffffff, close = 0;
77 for (int ii = 0; ii < incolors; ii++) {
79 int r = (_colors[i].red - icolors[ii].red) & 0xff;
80 int g = (_colors[i].green - icolors[ii].green) & 0xff;
81 int b = (_colors[i].blue - icolors[ii].blue) & 0xff;
82 // find a weighted absolute deviation
83 unsigned long dev = (r * r) + (g * g) + (b * b);
91 _colors[i].red = icolors[close].red;
92 _colors[i].green = icolors[close].green;
93 _colors[i].blue = icolors[close].blue;
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
99 assert(false); // wtf has gone wrong, its already alloced for chissake!
104 PseudoRenderControl::~PseudoRenderControl()
106 printf("Destroying PseudoColor RenderControl\n");
111 void PseudoRenderControl::reduceDepth(Surface &sf, XImage *im) const