]> Dogcows Code - chaz/openbox/blobdiff - otk/pseudorendercontrol.cc
Add the "obsetroot" tool. Use it to set the root background.
[chaz/openbox] / otk / pseudorendercontrol.cc
index 2b878200e7f034e38edd90e7b00658ad221fa1a1..cf9929248586e4e2c12d0b002eba771bcfb610fe 100644 (file)
@@ -9,14 +9,12 @@
 #include "rendertexture.hh"
 
 extern "C" {
-#ifdef    HAVE_STDLIB_H
-#  include <stdlib.h>
-#endif // HAVE_STDLIB_H
-
 #include "../src/gettext.h"
 #define _(str) gettext(str)
 }
 
+#include <cstdlib>
+
 namespace otk {
 
 PseudoRenderControl::PseudoRenderControl(int screen)
@@ -27,28 +25,31 @@ PseudoRenderControl::PseudoRenderControl(int screen)
   int depth = info->depth();
 
   // determine the number of colors and the bits-per-color
-  int bpc = 2; // XXX THIS SHOULD BE A USER OPTION
-  assert(bpc >= 1);
-  _ncolors = 1 << (bpc * 3);
+  _bpc = 2; // XXX THIS SHOULD BE A USER OPTION
+  assert(_bpc >= 1);
+  _ncolors = 1 << (_bpc * 3);
 
   if (_ncolors > 1 << depth) {
     fprintf(stderr,
             _("PseudoRenderControl: Invalid colormap size. Resizing.\n"));
-    bpc = 1 << (depth/3) >> 3;
-    _ncolors = 1 << (bpc * 3);
+    _bpc = 1 << (depth/3) >> 3;
+    _ncolors = 1 << (_bpc * 3);
   }
 
   // build a color cube
   _colors = new XColor[_ncolors];
-
-  int cpc = 1 << bpc; // colors per channel
-  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;
+int tr, tg, tb;
+  int cpc = 1 << _bpc; // colors per channel
+  for (int n = 0,
+         r = 0; r < cpc; r++)
+    for (int g = 0; g < cpc; g++)
+      for (int b = 0; b < cpc; b++, n++) {
+        tr = (int)(((float)(r)/(float)(cpc-1)) * 0xFF);
+        tg = (int)(((float)(g)/(float)(cpc-1)) * 0xFF);
+        tb = (int)(((float)(b)/(float)(cpc-1)) * 0xFF);
+        _colors[n].red = tr | tr << 8;
+        _colors[n].green = tg | tg << 8;
+        _colors[n].blue = tb | tb << 8;
         _colors[n].flags = DoRed|DoGreen|DoBlue; // used to track allocation
       }
 
@@ -87,6 +88,7 @@ PseudoRenderControl::PseudoRenderControl(int screen)
       _colors[i].red = icolors[close].red;
       _colors[i].green = icolors[close].green;
       _colors[i].blue = icolors[close].blue;
+      _colors[i].pixel = icolors[close].pixel;
 
       // try alloc this closest color, it had better succeed!
       if (XAllocColor(**display, info->colormap(), &_colors[i]))
@@ -101,16 +103,55 @@ PseudoRenderControl::~PseudoRenderControl()
 {
   printf("Destroying PseudoColor RenderControl\n");
 
-  unsigned long *pixels = new unsigned long [ncolors], *p = pixels;
+  unsigned long *pixels = new unsigned long [_ncolors], *p = pixels;
   for (int i = 0; i < _ncolors; ++i, ++p)
     *p = _colors[i].pixel;
   XFreeColors(**display, display->screenInfo(_screen)->colormap(), pixels,
               _ncolors, 0);
-  delete [] colors;
+  delete [] _colors;
+}
+
+inline const XColor *PseudoRenderControl::pickColor(int r, int g, int b) const
+{
+  r = (r & 0xff) >> (8-_bpc);
+  g = (g & 0xff) >> (8-_bpc);
+  b = (b & 0xff) >> (8-_bpc);
+  return &_colors[(r << (2*_bpc)) + (g << (1*_bpc)) + b];
 }
 
 void PseudoRenderControl::reduceDepth(Surface &sf, XImage *im) const
 {
+  pixel32 *data = sf.pixelData();
+  pixel32 *ret = (pixel32*)malloc(im->width * im->height * 4);
+  char *p = (char *)ret;
+  int x, y;
+  for (y = 0; y < im->height; y++) {
+    for (x = 0; x < im->width; x++) {
+      p[x] = pickColor(data[x] >> default_red_shift,
+                       data[x] >> default_green_shift,
+                       data[x] >> default_blue_shift)->pixel;
+    }
+    data += im->width;
+    p += im->bytes_per_line;
+  }
+  im->data = (char*)ret;
+}
+
+void PseudoRenderControl::allocateColor(XColor *color) const
+{
+  const XColor *c = pickColor(color->red, color->blue, color->green);
+
+  color->red = c->red;
+  color->green = c->green;
+  color->blue = c->blue;
+  color->pixel = c->pixel;
+
+  if (XAllocColor(**display, display->screenInfo(_screen)->colormap(), color))
+    color->flags = DoRed|DoGreen|DoBlue; // mark as alloced
+  else
+    assert(false); // wtf has gone wrong, its already alloced for chissake!
+
+  return;
 }
 
 }
This page took 0.023012 seconds and 4 git commands to generate.