]> Dogcows Code - chaz/openbox/blob - otk/truerendercontrol.cc
rm a XXX.. comment it out :)
[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 void TrueRenderControl::drawGradientBackground(
59 Surface &sf, const RenderTexture &texture) const
60 {
61 unsigned int r,g,b;
62 int w = sf.width(), h = sf.height(), off, x;
63
64 const ScreenInfo *info = display->screenInfo(_screen);
65 XImage *im = XCreateImage(**display, info->visual(), info->depth(),
66 ZPixmap, 0, NULL, w, h, 32, 0);
67 im->byte_order = endian;
68 pixel32 *data = new pixel32[sf.height()*sf.width()];
69 pixel32 current;
70
71 switch (texture.gradient()) {
72 case RenderTexture::Vertical:
73 verticalGradient(sf, texture, data);
74 break;
75 case RenderTexture::Diagonal:
76 diagonalGradient(sf, texture, data);
77 break;
78 case RenderTexture::CrossDiagonal:
79 crossDiagonalGradient(sf, texture, data);
80 break;
81 default:
82 printf("unhandled gradient\n");
83 }
84
85 if (texture.relief() == RenderTexture::Flat && texture.border()) {
86 r = texture.borderColor().red();
87 g = texture.borderColor().green();
88 b = texture.borderColor().blue();
89 current = (r << default_red_shift)
90 + (g << default_green_shift)
91 + (b << default_blue_shift);
92 for (off = 0, x = 0; x < w; ++x, off++) {
93 *(data + off) = current;
94 *(data + off + ((h-1) * w)) = current;
95 }
96 for (off = 0, x = 0; x < h; ++x, off++) {
97 *(data + (off * w)) = current;
98 *(data + (off * w) + w - 1) = current;
99 }
100 }
101
102 if (texture.relief() != RenderTexture::Flat) {
103 if (texture.bevel() == RenderTexture::Bevel1) {
104 for (off = 1, x = 1; x < w - 1; ++x, off++)
105 highlight(data + off,
106 data + off + (h-1) * w,
107 texture.relief()==RenderTexture::Raised);
108 for (off = 0, x = 0; x < h; ++x, off++)
109 highlight(data + off * w,
110 data + off * w + w - 1,
111 texture.relief()==RenderTexture::Raised);
112 }
113
114 if (texture.bevel() == RenderTexture::Bevel2) {
115 for (off = 2, x = 2; x < w - 2; ++x, off++)
116 highlight(data + off + w,
117 data + off + (h-2) * w,
118 texture.relief()==RenderTexture::Raised);
119 for (off = 1, x = 1; x < h-1; ++x, off++)
120 highlight(data + off * w + 1,
121 data + off * w + w - 2,
122 texture.relief()==RenderTexture::Raised);
123 }
124 }
125
126 reduceDepth(im, data);
127
128 im->data = (char*) data;
129
130 sf.setPixmap(im);
131
132 delete [] im->data;
133 im->data = NULL;
134 XDestroyImage(im);
135 }
136
137 void TrueRenderControl::verticalGradient(Surface &sf,
138 const RenderTexture &texture,
139 pixel32 *data) const
140 {
141 pixel32 current;
142 float dr, dg, db;
143 unsigned int r,g,b;
144
145 dr = (float)(texture.secondary_color().red() - texture.color().red());
146 dr/= (float)sf.height();
147
148 dg = (float)(texture.secondary_color().green() - texture.color().green());
149 dg/= (float)sf.height();
150
151 db = (float)(texture.secondary_color().blue() - texture.color().blue());
152 db/= (float)sf.height();
153
154 for (int y = 0; y < sf.height(); ++y) {
155 r = texture.color().red() + (int)(dr * y);
156 g = texture.color().green() + (int)(dg * y);
157 b = texture.color().blue() + (int)(db * y);
158 current = (r << default_red_shift)
159 + (g << default_green_shift)
160 + (b << default_blue_shift);
161 for (int x = 0; x < sf.width(); ++x, ++data)
162 *data = current;
163 }
164 }
165
166 void TrueRenderControl::diagonalGradient(Surface &sf,
167 const RenderTexture &texture,
168 pixel32 *data) const
169 {
170 pixel32 current;
171 float drx, dgx, dbx, dry, dgy, dby;
172 unsigned int r,g,b;
173
174
175 for (int y = 0; y < sf.height(); ++y) {
176 drx = (float)(texture.secondary_color().red() - texture.color().red());
177 dry = drx/(float)sf.height();
178 drx/= (float)sf.width();
179
180 dgx = (float)(texture.secondary_color().green() - texture.color().green());
181 dgy = dgx/(float)sf.height();
182 dgx/= (float)sf.width();
183
184 dbx = (float)(texture.secondary_color().blue() - texture.color().blue());
185 dby = dbx/(float)sf.height();
186 dbx/= (float)sf.width();
187 for (int x = 0; x < sf.width(); ++x, ++data) {
188 r = texture.color().red() + ((int)(drx * x) + (int)(dry * y))/2;
189 g = texture.color().green() + ((int)(dgx * x) + (int)(dgy * y))/2;
190 b = texture.color().blue() + ((int)(dbx * x) + (int)(dby * y))/2;
191 current = (r << default_red_shift)
192 + (g << default_green_shift)
193 + (b << default_blue_shift);
194 *data = current;
195 }
196 }
197 }
198
199 void TrueRenderControl::crossDiagonalGradient(Surface &sf,
200 const RenderTexture &texture,
201 pixel32 *data) const
202 {
203 pixel32 current;
204 float drx, dgx, dbx, dry, dgy, dby;
205 unsigned int r,g,b;
206
207 for (int y = 0; y < sf.height(); ++y) {
208 drx = (float)(texture.secondary_color().red() - texture.color().red());
209 dry = drx/(float)sf.height();
210 drx/= (float)sf.width();
211
212 dgx = (float)(texture.secondary_color().green() - texture.color().green());
213 dgy = dgx/(float)sf.height();
214 dgx/= (float)sf.width();
215
216 dbx = (float)(texture.secondary_color().blue() - texture.color().blue());
217 dby = dbx/(float)sf.height();
218 dbx/= (float)sf.width();
219 for (int x = sf.width(); x > 0; --x, ++data) {
220 r = texture.color().red() + ((int)(drx * (x-1)) + (int)(dry * y))/2;
221 g = texture.color().green() + ((int)(dgx * (x-1)) + (int)(dgy * y))/2;
222 b = texture.color().blue() + ((int)(dbx * (x-1)) + (int)(dby * y))/2;
223 current = (r << default_red_shift)
224 + (g << default_green_shift)
225 + (b << default_blue_shift);
226 *data = current;
227 }
228 }
229 }
230
231 void TrueRenderControl::reduceDepth(XImage *im, pixel32 *data) const
232 {
233 // since pixel32 is the largest possible pixel size, we can share the array
234 int r, g, b;
235 int x,y;
236 pixel16 *p = (pixel16*) data;
237 switch (im->bits_per_pixel) {
238 case 32:
239 if ((_red_offset != default_red_shift) ||
240 (_blue_offset != default_blue_shift) ||
241 (_green_offset != default_green_shift)) {
242 printf("cross endian conversion\n");
243 for (y = 0; y < im->height; y++) {
244 for (x = 0; x < im->width; x++) {
245 r = (data[x] >> default_red_shift) & 0xFF;
246 g = (data[x] >> default_green_shift) & 0xFF;
247 b = (data[x] >> default_blue_shift) & 0xFF;
248 data[x] = (r << _red_offset) + (g << _green_offset) +
249 (b << _blue_offset);
250 }
251 data += im->width;
252 }
253 }
254 return;
255 case 16:
256 for (y = 0; y < im->height; y++) {
257 for (x = 0; x < im->width; x++) {
258 r = (data[x] >> default_red_shift) & 0xFF;
259 r = r >> _red_shift;
260 g = (data[x] >> default_green_shift) & 0xFF;
261 g = g >> _green_shift;
262 b = (data[x] >> default_blue_shift) & 0xFF;
263 b = b >> _blue_shift;
264 p[x] = (r << _red_offset) + (g << _green_offset) + (b << _blue_offset);
265 }
266 data += im->width;
267 p += im->bytes_per_line/2;
268 }
269 break;
270 default:
271 printf("your bit depth is currently unhandled\n");
272 }
273 }
274
275 void TrueRenderControl::highlight(pixel32 *x, pixel32 *y, bool raised) const
276 {
277 int r, g, b;
278
279 pixel32 *up, *down;
280 if (raised) {
281 up = x;
282 down = y;
283 } else {
284 up = y;
285 down = x;
286 }
287 r = (*up >> default_red_shift) & 0xFF;
288 r += r >> 1;
289 g = (*up >> default_green_shift) & 0xFF;
290 g += g >> 1;
291 b = (*up >> default_blue_shift) & 0xFF;
292 b += b >> 1;
293 if (r > 255) r = 255;
294 if (g > 255) g = 255;
295 if (b > 255) b = 255;
296 *up = (r << default_red_shift) + (g << default_green_shift)
297 + (b << default_blue_shift);
298
299 r = (*down >> default_red_shift) & 0xFF;
300 r = (r >> 1) + (r >> 2);
301 g = (*down >> default_green_shift) & 0xFF;
302 g = (g >> 1) + (g >> 2);
303 b = (*down >> default_blue_shift) & 0xFF;
304 b = (b >> 1) + (b >> 2);
305 *down = (r << default_red_shift) + (g << default_green_shift)
306 + (b << default_blue_shift);
307 }
308
309 void TrueRenderControl::drawBackground(Surface& sf,
310 const RenderTexture &texture) const
311 {
312 assert(_screen == sf._screen);
313 assert(_screen == texture.color().screen());
314
315 if (texture.gradient() == RenderTexture::Solid)
316 drawSolidBackground(sf, texture);
317 else
318 drawGradientBackground(sf, texture);
319 }
320
321 }
This page took 0.047799 seconds and 4 git commands to generate.