]>
Dogcows Code - chaz/openbox/blob - otk/truerendercontrol.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
7 #include "truerendercontrol.hh"
9 #include "screeninfo.hh"
11 #include "rendertexture.hh"
16 #endif // HAVE_STDLIB_H
18 #include "../src/gettext.h"
19 #define _(str) gettext(str)
24 TrueRenderControl::TrueRenderControl(int screen
)
25 : RenderControl(screen
),
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");
35 unsigned long red_mask
, green_mask
, blue_mask
;
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
;
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; }
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
--; }
53 TrueRenderControl::~TrueRenderControl()
55 printf("Destroying TrueColor RenderControl\n");
61 static inline void renderPixel(XImage
*im
, unsigned char *dp
,
64 unsigned int bpp
= im
->bits_per_pixel
+ (im
->byte_order
== MSBFirst
? 1 : 0);
101 assert(false); // wtf?
105 void TrueRenderControl::drawGradientBackground(
106 Surface
&sf
, const RenderTexture
&texture
) const
109 int w
= sf
.width(), h
= sf
.height(), off
, x
;
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()];
118 switch (texture
.gradient()) {
119 case RenderTexture::Vertical
:
120 verticalGradient(sf
, texture
, data
);
122 case RenderTexture::Diagonal
:
123 diagonalGradient(sf
, texture
, data
);
125 case RenderTexture::CrossDiagonal
:
126 crossDiagonalGradient(sf
, texture
, data
);
129 printf("unhandled gradient\n");
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
;
143 for (off
= 0, x
= 0; x
< h
; ++x
, off
++) {
144 *(data
+ (off
* w
)) = current
;
145 *(data
+ (off
* w
) + w
- 1) = current
;
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
);
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
);
173 reduceDepth(im
, data
);
175 im
->data
= (char*) data
;
184 void TrueRenderControl::verticalGradient(Surface
&sf
,
185 const RenderTexture
&texture
,
192 dr
= (float)(texture
.secondary_color().red() - texture
.color().red());
193 dr
/= (float)sf
.height();
195 dg
= (float)(texture
.secondary_color().green() - texture
.color().green());
196 dg
/= (float)sf
.height();
198 db
= (float)(texture
.secondary_color().blue() - texture
.color().blue());
199 db
/= (float)sf
.height();
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
)
213 void TrueRenderControl::diagonalGradient(Surface
&sf
,
214 const RenderTexture
&texture
,
218 float drx
, dgx
, dbx
, dry
, dgy
, dby
;
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();
227 dgx
= (float)(texture
.secondary_color().green() - texture
.color().green());
228 dgy
= dgx
/(float)sf
.height();
229 dgx
/= (float)sf
.width();
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
);
246 void TrueRenderControl::crossDiagonalGradient(Surface
&sf
,
247 const RenderTexture
&texture
,
251 float drx
, dgx
, dbx
, dry
, dgy
, dby
;
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();
259 dgx
= (float)(texture
.secondary_color().green() - texture
.color().green());
260 dgy
= dgx
/(float)sf
.height();
261 dgx
/= (float)sf
.width();
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
);
277 void TrueRenderControl::reduceDepth(XImage
*im
, pixel32
*data
) const
279 // since pixel32 is the largest possible pixel size, we can share the array
282 pixel16
*p
= (pixel16
*)data
;
283 switch (im
->bits_per_pixel
) {
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
);
301 for (y
= 0; y
< im
->height
; y
++) {
302 for (x
= 0; x
< im
->width
; x
++) {
303 r
= (data
[x
] >> default_red_shift
) & 0xFF;
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
);
312 p
+= im
->bytes_per_line
/2;
316 printf("your bit depth is currently unhandled\n");
320 void TrueRenderControl::highlight(pixel32
*x
, pixel32
*y
, bool raised
) const
332 r
= (*up
>> default_red_shift
) & 0xFF;
334 g
= (*up
>> default_green_shift
) & 0xFF;
336 b
= (*up
>> default_blue_shift
) & 0xFF;
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
);
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
);
353 void TrueRenderControl::drawBackground(Surface
& sf
,
354 const RenderTexture
&texture
) const
356 assert(_screen
== sf
._screen
);
357 assert(_screen
== texture
.color().screen());
359 if (texture
.gradient() == RenderTexture::Solid
) {
360 drawSolidBackground(sf
, texture
);
362 drawGradientBackground(sf
, texture
);
This page took 0.050489 seconds and 4 git commands to generate.