]> Dogcows Code - chaz/openbox/blob - otk/pseudorendercontrol.cc
borked the build
[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 int 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
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
53 }
54
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
59
60 // try allocate any colors that failed allocation above
61
62 // get the allocated values from the X server (only the first 256 XXX why!?)
63 XColor icolors[256];
64 int incolors = (((1 << depth) > 256) ? 256 : (1 << depth));
65 for (int i = 0; i < incolors; i++)
66 icolors[i].pixel = i;
67 XQueryColors(**display, info->colormap(), icolors, incolors);
68
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++) {
74 // find deviations
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);
80
81 if (dev < closest) {
82 closest = dev;
83 close = ii;
84 }
85 }
86
87 _colors[i].red = icolors[close].red;
88 _colors[i].green = icolors[close].green;
89 _colors[i].blue = icolors[close].blue;
90
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
94 else
95 assert(false); // wtf has gone wrong, its already alloced for chissake!
96 }
97 }
98 }
99
100 PseudoRenderControl::~PseudoRenderControl()
101 {
102 printf("Destroying PseudoColor RenderControl\n");
103
104 unsigned long *pixels = new unsigned long [_ncolors], *p = pixels;
105 for (int i = 0; i < _ncolors; ++i, ++p)
106 *p = _colors[i].pixel;
107 XFreeColors(**display, display->screenInfo(_screen)->colormap(), pixels,
108 _ncolors, 0);
109 delete [] _colors;
110 }
111
112 void PseudoRenderControl::reduceDepth(Surface &sf, XImage *im) const
113 {
114 }
115
116 }
This page took 0.039885 seconds and 5 git commands to generate.