openbox/otk/pseudorendercontrol.cc

116 lines
3.3 KiB
C++
Raw Normal View History

2003-02-03 07:06:45 +00:00
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2003-02-09 23:07:54 +00:00
#include "config.h"
2003-02-03 07:06:45 +00:00
#include "pseudorendercontrol.hh"
#include "display.hh"
#include "screeninfo.hh"
#include "surface.hh"
#include "rendertexture.hh"
extern "C" {
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif // HAVE_STDLIB_H
#include "../src/gettext.h"
#define _(str) gettext(str)
}
namespace otk {
PseudoRenderControl::PseudoRenderControl(int screen)
: RenderControl(screen)
{
printf("Initializing PseudoColor RenderControl\n");
2003-02-12 00:42:19 +00:00
const ScreenInfo *info = display->screenInfo(_screen);
2003-02-12 01:12:34 +00:00
int depth = info->depth();
2003-02-12 06:14:25 +00:00
_bpc = 2; // XXX THIS SHOULD BE A USER OPTION
assert(_bpc >= 1);
_ncolors = 1 << (_bpc * 3);
2003-02-12 01:12:34 +00:00
2003-02-12 06:14:25 +00:00
if (_ncolors > 1 << depth) {
2003-02-12 01:12:34 +00:00
fprintf(stderr,
2003-02-12 06:14:25 +00:00
_("PseudoRenderControl: Invalid colormap size. Resizing.\n"));
_bpc = 1 << (depth/3) >> 3;
_ncolors = 1 << (_bpc * 3);
2003-02-12 01:12:34 +00:00
}
2003-02-12 06:14:25 +00:00
_cpc = 1 << _bpc;
2003-02-12 01:12:34 +00:00
if (!(_colors = new XColor[_ncolors])) {
fprintf(stderr,
_("PseudoRenderControl: error allocating colormap\n"));
::exit(1);
}
2003-02-12 06:14:25 +00:00
// build a color cube
for (int n = _ncolors - 1,
r = (1 << (_bpc + 1)) -1, i = 0; i < _cpc; r >>= 1, ++i)
for (int g = (1 << (_bpc + 1)) -1, j = 0; j < _cpc; g >>= 1, ++j)
for (int b = (1 << (_bpc + 1)) -1, k = 0; k < _cpc; b >>= 1, ++k, --n) {
_colors[n].red = r | r << 8;
_colors[n].green = g | g << 8;
_colors[n].blue = b | b << 8;
_colors[n].flags = DoRed|DoGreen|DoBlue; // used to track allocation
}
// allocate the colors
for (int i = 0; i < _ncolors; i++)
if (!XAllocColor(**display, info->colormap(), &_colors[i]))
_colors[i].flags = 0; // mark it as unallocated
// try allocate any colors that failed allocation above
// get the allocated values from the X server (only the first 256 XXX why!?)
XColor icolors[256];
int incolors = (((1 << depth) > 256) ? 256 : (1 << depth));
for (int i = 0; i < incolors; i++)
icolors[i].pixel = i;
XQueryColors(**display, info->colormap(), icolors, incolors);
// try match unallocated ones
for (int i = 0; i < _ncolors; i++) {
if (!_colors[i].flags) { // if it wasn't allocated...
unsigned long closest = 0xffffffff, close = 0;
for (int ii = 0; ii < incolors; ii++) {
// find deviations
int r = (_colors[i].red - icolors[ii].red) & 0xff;
int g = (_colors[i].green - icolors[ii].green) & 0xff;
int b = (_colors[i].blue - icolors[ii].blue) & 0xff;
// find a weighted absolute deviation
unsigned long dev = (r * r) + (g * g) + (b * b);
if (dev < closest) {
closest = dev;
close = ii;
}
}
_colors[i].red = icolors[close].red;
_colors[i].green = icolors[close].green;
_colors[i].blue = icolors[close].blue;
// try alloc this closest color, it had better succeed!
if (XAllocColor(**display, info->colormap(), &_colors[i]))
_colors[i].flags = DoRed|DoGreen|DoBlue; // mark as alloced
else
assert(false); // wtf has gone wrong, its already alloced for chissake!
}
}
2003-02-03 07:06:45 +00:00
}
PseudoRenderControl::~PseudoRenderControl()
{
printf("Destroying PseudoColor RenderControl\n");
2003-02-12 01:12:34 +00:00
delete _colors;
2003-02-03 07:06:45 +00:00
}
void PseudoRenderControl::reduceDepth(Surface &sf, XImage *im) const
2003-02-11 23:58:33 +00:00
{
}
2003-02-03 07:06:45 +00:00
}