]> Dogcows Code - chaz/openbox/blob - otk/truerendercontrol.cc
better historyplacement
[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 "../src/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 const ScreenInfo *info = display->screenInfo(_screen);
31 XImage *timage = XCreateImage(**display, info->visual(), info->depth(),
32 ZPixmap, 0, NULL, 1, 1, 32, 0);
33 printf("Initializing TrueColor RenderControl\n");
34
35 unsigned long red_mask, green_mask, blue_mask;
36
37 // find the offsets for each color in the visual's masks
38 red_mask = timage->red_mask;
39 green_mask = timage->green_mask;
40 blue_mask = timage->blue_mask;
41
42 while (! (red_mask & 1)) { _red_offset++; red_mask >>= 1; }
43 while (! (green_mask & 1)) { _green_offset++; green_mask >>= 1; }
44 while (! (blue_mask & 1)) { _blue_offset++; blue_mask >>= 1; }
45
46 _red_shift = _green_shift = _blue_shift = 8;
47 while (red_mask) { red_mask >>= 1; _red_shift--; }
48 while (green_mask) { green_mask >>= 1; _green_shift--; }
49 while (blue_mask) { blue_mask >>= 1; _blue_shift--; }
50 XFree(timage);
51 }
52
53 TrueRenderControl::~TrueRenderControl()
54 {
55 printf("Destroying TrueColor RenderControl\n");
56
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::drawGradientBackground(
106 Surface &sf, const RenderTexture &texture) const
107 {
108 unsigned int r,g,b;
109 int w = sf.width(), h = sf.height(), off, x;
110
111 const ScreenInfo *info = display->screenInfo(_screen);
112 XImage *im = XCreateImage(**display, info->visual(), info->depth(),
113 ZPixmap, 0, NULL, w, h, 32, 0);
114 im->byte_order = endian;
115 pixel32 *data = new pixel32[sf.height()*sf.width()];
116 pixel32 current;
117
118 switch (texture.gradient()) {
119 case RenderTexture::Vertical:
120 verticalGradient(sf, texture, data);
121 break;
122 case RenderTexture::Diagonal:
123 diagonalGradient(sf, texture, data);
124 break;
125 case RenderTexture::CrossDiagonal:
126 crossDiagonalGradient(sf, texture, data);
127 break;
128 default:
129 printf("unhandled gradient\n");
130 }
131
132 if (texture.relief() == RenderTexture::Flat && texture.border()) {
133 r = texture.borderColor().red();
134 g = texture.borderColor().green();
135 b = texture.borderColor().blue();
136 current = (r << default_red_shift)
137 + (g << default_green_shift)
138 + (b << default_blue_shift);
139 for (off = 0, x = 0; x < w; ++x, off++) {
140 *(data + off) = current;
141 *(data + off + ((h-1) * w)) = current;
142 }
143 for (off = 0, x = 0; x < h; ++x, off++) {
144 *(data + (off * w)) = current;
145 *(data + (off * w) + w - 1) = current;
146 }
147 }
148
149 if (texture.relief() != RenderTexture::Flat) {
150 if (texture.bevel() == RenderTexture::Bevel1) {
151 for (off = 1, x = 1; x < w - 1; ++x, off++)
152 highlight(data + off,
153 data + off + (h-1) * w,
154 texture.relief()==RenderTexture::Raised);
155 for (off = 0, x = 0; x < h; ++x, off++)
156 highlight(data + off * w,
157 data + off * w + w - 1,
158 texture.relief()==RenderTexture::Raised);
159 }
160
161 if (texture.bevel() == RenderTexture::Bevel2) {
162 for (off = 2, x = 2; x < w - 2; ++x, off++)
163 highlight(data + off + w,
164 data + off + (h-2) * w,
165 texture.relief()==RenderTexture::Raised);
166 for (off = 1, x = 1; x < h-1; ++x, off++)
167 highlight(data + off * w + 1,
168 data + off * w + w - 2,
169 texture.relief()==RenderTexture::Raised);
170 }
171 }
172
173 reduceDepth(im, data);
174
175 im->data = (char*) data;
176
177 sf.setPixmap(im);
178
179 delete [] im->data;
180 im->data = NULL;
181 XDestroyImage(im);
182 }
183
184 void TrueRenderControl::verticalGradient(Surface &sf,
185 const RenderTexture &texture,
186 pixel32 *data) const
187 {
188 pixel32 current;
189 float dr, dg, db;
190 unsigned int r,g,b;
191
192 dr = (float)(texture.secondary_color().red() - texture.color().red());
193 dr/= (float)sf.height();
194
195 dg = (float)(texture.secondary_color().green() - texture.color().green());
196 dg/= (float)sf.height();
197
198 db = (float)(texture.secondary_color().blue() - texture.color().blue());
199 db/= (float)sf.height();
200
201 for (int y = 0; y < sf.height(); ++y) {
202 r = texture.color().red() + (int)(dr * y);
203 g = texture.color().green() + (int)(dg * y);
204 b = texture.color().blue() + (int)(db * y);
205 current = (r << default_red_shift)
206 + (g << default_green_shift)
207 + (b << default_blue_shift);
208 for (int x = 0; x < sf.width(); ++x, ++data)
209 *data = current;
210 }
211 }
212
213 void TrueRenderControl::diagonalGradient(Surface &sf,
214 const RenderTexture &texture,
215 pixel32 *data) const
216 {
217 pixel32 current;
218 float drx, dgx, dbx, dry, dgy, dby;
219 unsigned int r,g,b;
220
221
222 for (int y = 0; y < sf.height(); ++y) {
223 drx = (float)(texture.secondary_color().red() - texture.color().red());
224 dry = drx/(float)sf.height();
225 drx/= (float)sf.width();
226
227 dgx = (float)(texture.secondary_color().green() - texture.color().green());
228 dgy = dgx/(float)sf.height();
229 dgx/= (float)sf.width();
230
231 dbx = (float)(texture.secondary_color().blue() - texture.color().blue());
232 dby = dbx/(float)sf.height();
233 dbx/= (float)sf.width();
234 for (int x = 0; x < sf.width(); ++x, ++data) {
235 r = texture.color().red() + ((int)(drx * x) + (int)(dry * y))/2;
236 g = texture.color().green() + ((int)(dgx * x) + (int)(dgy * y))/2;
237 b = texture.color().blue() + ((int)(dbx * x) + (int)(dby * y))/2;
238 current = (r << default_red_shift)
239 + (g << default_green_shift)
240 + (b << default_blue_shift);
241 *data = current;
242 }
243 }
244 }
245
246 void TrueRenderControl::crossDiagonalGradient(Surface &sf,
247 const RenderTexture &texture,
248 pixel32 *data) const
249 {
250 pixel32 current;
251 float drx, dgx, dbx, dry, dgy, dby;
252 unsigned int r,g,b;
253
254 for (int y = 0; y < sf.height(); ++y) {
255 drx = (float)(texture.secondary_color().red() - texture.color().red());
256 dry = drx/(float)sf.height();
257 drx/= (float)sf.width();
258
259 dgx = (float)(texture.secondary_color().green() - texture.color().green());
260 dgy = dgx/(float)sf.height();
261 dgx/= (float)sf.width();
262
263 dbx = (float)(texture.secondary_color().blue() - texture.color().blue());
264 dby = dbx/(float)sf.height();
265 dbx/= (float)sf.width();
266 for (int x = sf.width(); x > 0; --x, ++data) {
267 r = texture.color().red() + ((int)(drx * (x-1)) + (int)(dry * y))/2;
268 g = texture.color().green() + ((int)(dgx * (x-1)) + (int)(dgy * y))/2;
269 b = texture.color().blue() + ((int)(dbx * (x-1)) + (int)(dby * y))/2;
270 current = (r << default_red_shift)
271 + (g << default_green_shift)
272 + (b << default_blue_shift);
273 *data = current;
274 }
275 }
276 }
277 void TrueRenderControl::reduceDepth(XImage *im, pixel32 *data) const
278 {
279 // since pixel32 is the largest possible pixel size, we can share the array
280 int r, g, b;
281 int x,y;
282 pixel16 *p = (pixel16 *)data;
283 switch (im->bits_per_pixel) {
284 case 32:
285 if ((_red_offset != default_red_shift) ||
286 (_blue_offset != default_blue_shift) ||
287 (_green_offset != default_green_shift)) {
288 printf("cross endian conversion\n");
289 for (y = 0; y < im->height; y++) {
290 for (x = 0; x < im->width; x++) {
291 r = (data[x] >> default_red_shift) & 0xFF;
292 g = (data[x] >> default_green_shift) & 0xFF;
293 b = (data[x] >> default_blue_shift) & 0xFF;
294 data[x] = (r << _red_offset) + (g << _green_offset) + (b << _blue_offset);
295 }
296 data += im->width;
297 }
298 }
299 return;
300 case 16:
301 for (y = 0; y < im->height; y++) {
302 for (x = 0; x < im->width; x++) {
303 r = (data[x] >> default_red_shift) & 0xFF;
304 r = r >> _red_shift;
305 g = (data[x] >> default_green_shift) & 0xFF;
306 g = g >> _green_shift;
307 b = (data[x] >> default_blue_shift) & 0xFF;
308 b = b >> _blue_shift;
309 p[x] = (r << _red_offset) + (g << _green_offset) + (b << _blue_offset);
310 }
311 data += im->width;
312 p += im->bytes_per_line/2;
313 }
314 break;
315 default:
316 printf("your bit depth is currently unhandled\n");
317 }
318 }
319
320 void TrueRenderControl::highlight(pixel32 *x, pixel32 *y, bool raised) const
321 {
322 int r, g, b;
323
324 pixel32 *up, *down;
325 if (raised) {
326 up = x;
327 down = y;
328 } else {
329 up = y;
330 down = x;
331 }
332 r = (*up >> default_red_shift) & 0xFF;
333 r += r >> 1;
334 g = (*up >> default_green_shift) & 0xFF;
335 g += g >> 1;
336 b = (*up >> default_blue_shift) & 0xFF;
337 b += b >> 1;
338 if (r > 255) r = 255;
339 if (g > 255) g = 255;
340 if (b > 255) b = 255;
341 *up = (r << default_red_shift) + (g << default_green_shift)
342 + (b << default_blue_shift);
343
344 r = (*down >> default_red_shift) & 0xFF;
345 r = (r >> 1) + (r >> 2);
346 g = (*down >> default_green_shift) & 0xFF;
347 g = (g >> 1) + (g >> 2);
348 b = (*down >> default_blue_shift) & 0xFF;
349 b = (b >> 1) + (b >> 2);
350 *down = (r << default_red_shift) + (g << default_green_shift)
351 + (b << default_blue_shift);
352 }
353 void TrueRenderControl::drawBackground(Surface& sf,
354 const RenderTexture &texture) const
355 {
356 assert(_screen == sf._screen);
357 assert(_screen == texture.color().screen());
358
359 if (texture.gradient() == RenderTexture::Solid) {
360 drawSolidBackground(sf, texture);
361 } else {
362 drawGradientBackground(sf, texture);
363 }
364 }
365
366 }
This page took 0.050489 seconds and 4 git commands to generate.