]> Dogcows Code - chaz/openbox/blob - otk/truerendercontrol.cc
colors are back
[chaz/openbox] / otk / truerendercontrol.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 #include "truerendercontrol.hh"
8 #include "display.hh"
9 #include "screeninfo.hh"
10 #include "surface.hh"
11
12 extern "C" {
13 #ifdef HAVE_STDLIB_H
14 # include <stdlib.h>
15 #endif // HAVE_STDLIB_H
16
17 #include "gettext.h"
18 #define _(str) gettext(str)
19 }
20
21 namespace otk {
22
23 TrueRenderControl::TrueRenderControl(const ScreenInfo *screen)
24 : RenderControl(screen),
25 _red_offset(0),
26 _green_offset(0),
27 _blue_offset(0)
28 {
29 printf("Initializing TrueColor RenderControl\n");
30
31 unsigned long red_mask, green_mask, blue_mask;
32
33 // find the offsets for each color in the visual's masks
34 red_mask = screen->visual()->red_mask;
35 green_mask = screen->visual()->green_mask;
36 blue_mask = screen->visual()->blue_mask;
37
38 while (! (red_mask & 1)) { _red_offset++; red_mask >>= 1; }
39 while (! (green_mask & 1)) { _green_offset++; green_mask >>= 1; }
40 while (! (blue_mask & 1)) { _blue_offset++; blue_mask >>= 1; }
41
42 // scale available colorspace to match our 256x256x256 cube
43 _red_bits = 255 / red_mask;
44 _green_bits = 255 / green_mask;
45 _blue_bits = 255 / blue_mask;
46
47 for (int i = 0; i < 256; i++) {
48 _red_color_table[i] = i / _red_bits;
49 _green_color_table[i] = i / _green_bits;
50 _blue_color_table[i] = i / _blue_bits;
51 }
52 }
53
54 TrueRenderControl::~TrueRenderControl()
55 {
56 printf("Destroying TrueColor RenderControl\n");
57
58
59 }
60
61 static inline void renderPixel(XImage *im, unsigned char *dp,
62 unsigned long pixel)
63 {
64 unsigned int bpp = im->bits_per_pixel + (im->byte_order == MSBFirst ? 1 : 0);
65
66 switch (bpp) {
67 case 8: // 8bpp
68 *dp++ = pixel;
69 break;
70 case 16: // 16bpp LSB
71 *dp++ = pixel;
72 *dp++ = pixel >> 8;
73 break;
74 case 17: // 16bpp MSB
75 *dp++ = pixel >> 8;
76 *dp++ = pixel;
77 break;
78 case 24: // 24bpp LSB
79 *dp++ = pixel;
80 *dp++ = pixel >> 8;
81 *dp++ = pixel >> 16;
82 break;
83 case 25: // 24bpp MSB
84 *dp++ = pixel >> 16;
85 *dp++ = pixel >> 8;
86 *dp++ = pixel;
87 break;
88 case 32: // 32bpp LSB
89 *dp++ = pixel;
90 *dp++ = pixel >> 8;
91 *dp++ = pixel >> 16;
92 *dp++ = pixel >> 24;
93 break;
94 case 33: // 32bpp MSB
95 *dp++ = pixel >> 24;
96 *dp++ = pixel >> 16;
97 *dp++ = pixel >> 8;
98 *dp++ = pixel;
99 break;
100 default:
101 assert(false); // wtf?
102 }
103 }
104
105 void TrueRenderControl::render(Surface *sf)
106 {
107 assert(sf);
108
109 int w = sf->width(), h = sf->height();
110
111 XImage *im = XCreateImage(**display, _screen->visual(), _screen->depth(),
112 ZPixmap, 0, NULL, w, h, 32, 0);
113
114 unsigned char *data = new unsigned char[im->bytes_per_line * h];
115 unsigned char *dp = data;
116
117 for (int y = 0; y < h/3; ++y)
118 for (int x = 0; x < w; ++x, dp += im->bits_per_pixel/8)
119 renderPixel(im, dp, _red_color_table[255*x/w] << _red_offset);
120 for (int y = 0; y < h/3; ++y)
121 for (int x = 0; x < w; ++x, dp += im->bits_per_pixel/8)
122 renderPixel(im, dp, _green_color_table[255*x/w] << _green_offset);
123 for (int y = 0; y < h/3; ++y)
124 for (int x = 0; x < w; ++x, dp += im->bits_per_pixel/8)
125 renderPixel(im, dp, _blue_color_table[255*x/w] << _blue_offset);
126
127 printf("\nDone %d %d\n", im->bytes_per_line * h, dp - data);
128
129 im->data = (char*) data;
130
131 if (!sf->_pm)
132 sf->_pm = XCreatePixmap(**display, _screen->rootWindow(), w, h,
133 _screen->depth());
134 XPutImage(**display, sf->_pm, DefaultGC(**display, _screen->screen()),
135 im, 0, 0, 0, 0, w, h);
136
137 //delete [] image->data;
138 //image->data = NULL;
139 XDestroyImage(im);
140 }
141
142 }
This page took 0.041329 seconds and 4 git commands to generate.