]> icculus.org git repositories - dana/openbox.git/blob - otk/pseudorendercontrol.cc
8bpp pseudo color. it's horrid code. the graphics are horrid.
[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   // 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       }
99       else
100         assert(false); // wtf has gone wrong, its already alloced for chissake!
101     }
102   }
103 }
104
105 PseudoRenderControl::~PseudoRenderControl()
106 {
107   printf("Destroying PseudoColor RenderControl\n");
108
109   unsigned long *pixels = new unsigned long [_ncolors], *p = pixels;
110   for (int i = 0; i < _ncolors; ++i, ++p)
111     *p = _colors[i].pixel;
112   XFreeColors(**display, display->screenInfo(_screen)->colormap(), pixels,
113               _ncolors, 0);
114   delete [] _colors;
115 }
116
117 void PseudoRenderControl::reduceDepth(Surface &sf, XImage *im) const
118 {
119   pixel32 *data = sf.pixelData();
120   char *p = (char *)data;
121   int x, y, r, g, b;
122     for (y = 0; y < im->height; y++) {
123       for (x = 0; x < im->width; x++) {
124         r = (data[x] >> default_red_shift) & 0xFF;
125         r = r >> (8-_bpc);
126         g = (data[x] >> default_green_shift) & 0xFF;
127         g = g >> (8-_bpc);
128         b = (data[x] >> default_blue_shift) & 0xFF;
129         b = b >> (8-_bpc);
130         p[x] = _colors[(r << (2*_bpc)) + (g << (1*_bpc)) + b].pixel;
131       }
132       data += im->width;
133       p += im->bytes_per_line;
134     }
135
136 }
137
138 }