]> icculus.org git repositories - dana/openbox.git/blob - otk/pseudorendercontrol.cc
alloc colors.. i think!
[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   _bpc = 2; // XXX THIS SHOULD BE A USER OPTION
30   assert(_bpc >= 1);
31   _ncolors = 1 << (_bpc * 3);
32
33   if (_ncolors > 1 << depth) {
34     fprintf(stderr,
35             _("PseudoRenderControl: Invalid colormap size. Resizing.\n"));
36     _bpc = 1 << (depth/3) >> 3;
37     _ncolors = 1 << (_bpc * 3);
38   }
39
40   _cpc = 1 << _bpc;
41   
42   if (!(_colors = new XColor[_ncolors])) {
43     fprintf(stderr,
44             _("PseudoRenderControl: error allocating colormap\n"));
45     ::exit(1);
46   }
47
48   // build a color cube
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
57       }
58
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
63
64   // try allocate any colors that failed allocation above
65
66   // get the allocated values from the X server (only the first 256 XXX why!?)
67   XColor icolors[256];
68   int incolors = (((1 << depth) > 256) ? 256 : (1 << depth));
69   for (int i = 0; i < incolors; i++)
70     icolors[i].pixel = i;
71   XQueryColors(**display, info->colormap(), icolors, incolors);
72
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++) {
78         // find deviations
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);
84
85         if (dev < closest) {
86           closest = dev;
87           close = ii;
88         }
89       }
90
91       _colors[i].red = icolors[close].red;
92       _colors[i].green = icolors[close].green;
93       _colors[i].blue = icolors[close].blue;
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   delete _colors;
109 }
110
111 void PseudoRenderControl::reduceDepth(Surface &sf, XImage *im) const
112 {
113 }
114
115 }