]> Dogcows Code - chaz/openbox/blob - otk/truerendercontrol.cc
Added color reduction for 16bpp displays
[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 #include "rendertexture.hh"
12
13 extern "C" {
14 #ifdef HAVE_STDLIB_H
15 # include <stdlib.h>
16 #endif // HAVE_STDLIB_H
17
18 #include "gettext.h"
19 #define _(str) gettext(str)
20 }
21
22 namespace otk {
23
24 TrueRenderControl::TrueRenderControl(int screen)
25 : RenderControl(screen),
26 _red_offset(0),
27 _green_offset(0),
28 _blue_offset(0)
29 {
30 printf("Initializing TrueColor RenderControl\n");
31
32 Visual *visual = display->screenInfo(_screen)->visual();
33 unsigned long red_mask, green_mask, blue_mask;
34
35 // find the offsets for each color in the visual's masks
36 red_mask = visual->red_mask;
37 green_mask = visual->green_mask;
38 blue_mask = visual->blue_mask;
39
40 while (! (red_mask & 1)) { _red_offset++; red_mask >>= 1; }
41 while (! (green_mask & 1)) { _green_offset++; green_mask >>= 1; }
42 while (! (blue_mask & 1)) { _blue_offset++; blue_mask >>= 1; }
43
44 _red_shift = _green_shift = _blue_shift = 8;
45 while (red_mask) { red_mask >>= 1; _red_shift--; }
46 while (green_mask) { green_mask >>= 1; _green_shift--; }
47 while (blue_mask) { blue_mask >>= 1; _blue_shift--; }
48 }
49
50 TrueRenderControl::~TrueRenderControl()
51 {
52 printf("Destroying TrueColor RenderControl\n");
53
54
55 }
56
57
58 static inline void renderPixel(XImage *im, unsigned char *dp,
59 unsigned long pixel)
60 {
61 unsigned int bpp = im->bits_per_pixel + (im->byte_order == MSBFirst ? 1 : 0);
62
63 switch (bpp) {
64 case 8: // 8bpp
65 *dp++ = pixel;
66 break;
67 case 16: // 16bpp LSB
68 *dp++ = pixel;
69 *dp++ = pixel >> 8;
70 break;
71 case 17: // 16bpp MSB
72 *dp++ = pixel >> 8;
73 *dp++ = pixel;
74 break;
75 case 24: // 24bpp LSB
76 *dp++ = pixel;
77 *dp++ = pixel >> 8;
78 *dp++ = pixel >> 16;
79 break;
80 case 25: // 24bpp MSB
81 *dp++ = pixel >> 16;
82 *dp++ = pixel >> 8;
83 *dp++ = pixel;
84 break;
85 case 32: // 32bpp LSB
86 *dp++ = pixel;
87 *dp++ = pixel >> 8;
88 *dp++ = pixel >> 16;
89 *dp++ = pixel >> 24;
90 break;
91 case 33: // 32bpp MSB
92 *dp++ = pixel >> 24;
93 *dp++ = pixel >> 16;
94 *dp++ = pixel >> 8;
95 *dp++ = pixel;
96 break;
97 default:
98 assert(false); // wtf?
99 }
100 }
101
102 void TrueRenderControl::drawGradientBackground(
103 Surface &sf, const RenderTexture &texture) const
104 {
105 int w = sf.width(), h = sf.height(), off, x, y;
106
107 const ScreenInfo *info = display->screenInfo(_screen);
108 XImage *im = XCreateImage(**display, info->visual(), info->depth(),
109 ZPixmap, 0, NULL, w, h, 32, 0);
110
111 pixel32 *data = new pixel32[sf.height()*sf.width()];
112 pixel32 current;
113 pixel32 *dp = data;
114 float dr, dg, db;
115 unsigned int r,g,b;
116 //XXX: move this to seperate vgrad function
117 dr = (float)(texture.secondary_color().red() - texture.color().red());
118 dr/= (float)sf.height();
119
120 dg = (float)(texture.secondary_color().green() - texture.color().green());
121 dg/= (float)sf.height();
122
123 db = (float)(texture.secondary_color().blue() - texture.color().blue());
124 db/= (float)sf.height();
125
126 for (y = 0; y < h; ++y) {
127 r = texture.color().red() + (int)(dr * y);
128 g = texture.color().green() + (int)(dg * y);
129 b = texture.color().blue() + (int)(db * y);
130 current = (r << 16)
131 + (g << 8)
132 + b;
133 for (x = 0; x < w; ++x, dp ++)
134 *dp = current;
135 }
136 //XXX: end of vgrad
137
138 if (texture.relief() == RenderTexture::Flat && texture.border()) {
139 r = texture.borderColor().red();
140 g = texture.borderColor().green();
141 b = texture.borderColor().blue();
142 current = (r << 16)
143 + (g << 8)
144 + b;
145 for (off = 0, x = 0; x < w; ++x, off++) {
146 *(data + off) = current;
147 *(data + off + ((h-1) * w)) = current;
148 }
149 for (off = 0, x = 0; x < h; ++x, off++) {
150 *(data + (off * w)) = current;
151 *(data + (off * w) + w - 1) = current;
152 }
153 }
154
155 if (texture.relief() != RenderTexture::Flat) {
156 if (texture.bevel() == RenderTexture::Bevel1) {
157 for (off = 1, x = 1; x < w - 1; ++x, off++)
158 highlight(data + off,
159 data + off + (h-1) * w,
160 texture.relief()==RenderTexture::Raised);
161 for (off = 0, x = 0; x < h; ++x, off++)
162 highlight(data + off * w,
163 data + off * w + w - 1,
164 texture.relief()==RenderTexture::Raised);
165 }
166
167 if (texture.bevel() == RenderTexture::Bevel2) {
168 for (off = 2, x = 2; x < w - 2; ++x, off++)
169 highlight(data + off + w,
170 data + off + (h-2) * w,
171 texture.relief()==RenderTexture::Raised);
172 for (off = 1, x = 1; x < h-1; ++x, off++)
173 highlight(data + off * w + 1,
174 data + off * w + w - 2,
175 texture.relief()==RenderTexture::Raised);
176 }
177 }
178
179 reduceDepth(im, data);
180
181 im->data = (char*) data;
182
183 sf.setPixmap(im);
184
185 delete [] im->data;
186 im->data = NULL;
187 XDestroyImage(im);
188 }
189
190 void TrueRenderControl::reduceDepth(XImage *im, pixel32 *data) const
191 {
192 int r, g, b;
193 int x,y;
194 pixel16 *p = (pixel16 *)data;
195 switch (im->bits_per_pixel) {
196 case 32:
197 return;
198 case 16:
199 for (y = 0; y < im->height; y++) {
200 for (x = 0; x < im->width; x++) {
201 r = (data[x] >> 16) & 0xFF;
202 r = r >> _red_shift;
203 g = (data[x] >> 8) & 0xFF;
204 g = g >> _green_shift;
205 b = data[x] & 0xFF;
206 b = b >> _blue_shift;
207 p[x] = (r << _red_offset) + (g << _green_offset) + (b << _blue_offset);
208 }
209 data += im->width;
210 p += im->bytes_per_line/2;
211 }
212 break;
213 default:
214 printf("your bit depth is currently unhandled\n");
215 }
216 }
217
218 void TrueRenderControl::highlight(pixel32 *x, pixel32 *y, bool raised) const
219 {
220 int r, g, b;
221
222 pixel32 *up, *down;
223 if (raised) {
224 up = x;
225 down = y;
226 } else {
227 up = y;
228 down = x;
229 }
230 r = (*up >> 16) & 0xFF;
231 r += r >> 1;
232 g = (*up >> 8) & 0xFF;
233 g += g >> 1;
234 b = *up & 0xFF;
235 b += b >> 1;
236 if (r > 255) r = 255;
237 if (g > 255) g = 255;
238 if (b > 255) b = 255;
239 *up = (r << 16) + (g << 8) + b;
240
241 r = (*down >> 16) & 0xFF;
242 r = (r >> 1) + (r >> 2);
243 g = (*down >> 8) & 0xFF;
244 g = (g >> 1) + (g >> 2);
245 b = *down & 0xFF;
246 b = (b >> 1) + (b >> 2);
247 *down = (r << 16) + (g << 8) + b;
248 }
249 void TrueRenderControl::drawBackground(Surface& sf,
250 const RenderTexture &texture) const
251 {
252 assert(_screen == sf._screen);
253 assert(_screen == texture.color().screen());
254
255 if (texture.gradient() == RenderTexture::Solid) {
256 drawSolidBackground(sf, texture);
257 } else {
258 drawGradientBackground(sf, texture);
259 }
260 }
261
262 }
This page took 0.049396 seconds and 5 git commands to generate.