]>
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");
58 void TrueRenderControl::drawGradientBackground(
59 Surface
&sf
, const RenderTexture
&texture
) const
62 int w
= sf
.width(), h
= sf
.height(), off
, x
;
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()];
71 switch (texture
.gradient()) {
72 case RenderTexture::Vertical
:
73 verticalGradient(sf
, texture
, data
);
75 case RenderTexture::Diagonal
:
76 diagonalGradient(sf
, texture
, data
);
78 case RenderTexture::CrossDiagonal
:
79 crossDiagonalGradient(sf
, texture
, data
);
82 printf("unhandled gradient\n");
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
;
96 for (off
= 0, x
= 0; x
< h
; ++x
, off
++) {
97 *(data
+ (off
* w
)) = current
;
98 *(data
+ (off
* w
) + w
- 1) = current
;
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
);
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
);
126 reduceDepth(im
, data
);
128 im
->data
= (char*) data
;
137 void TrueRenderControl::verticalGradient(Surface
&sf
,
138 const RenderTexture
&texture
,
145 dr
= (float)(texture
.secondary_color().red() - texture
.color().red());
146 dr
/= (float)sf
.height();
148 dg
= (float)(texture
.secondary_color().green() - texture
.color().green());
149 dg
/= (float)sf
.height();
151 db
= (float)(texture
.secondary_color().blue() - texture
.color().blue());
152 db
/= (float)sf
.height();
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
)
166 void TrueRenderControl::diagonalGradient(Surface
&sf
,
167 const RenderTexture
&texture
,
171 float drx
, dgx
, dbx
, dry
, dgy
, dby
;
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();
180 dgx
= (float)(texture
.secondary_color().green() - texture
.color().green());
181 dgy
= dgx
/(float)sf
.height();
182 dgx
/= (float)sf
.width();
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
);
199 void TrueRenderControl::crossDiagonalGradient(Surface
&sf
,
200 const RenderTexture
&texture
,
204 float drx
, dgx
, dbx
, dry
, dgy
, dby
;
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();
212 dgx
= (float)(texture
.secondary_color().green() - texture
.color().green());
213 dgy
= dgx
/(float)sf
.height();
214 dgx
/= (float)sf
.width();
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
);
231 void TrueRenderControl::reduceDepth(XImage
*im
, pixel32
*data
) const
233 // since pixel32 is the largest possible pixel size, we can share the array
236 pixel16
*p
= (pixel16
*) data
;
237 switch (im
->bits_per_pixel
) {
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
) +
256 for (y
= 0; y
< im
->height
; y
++) {
257 for (x
= 0; x
< im
->width
; x
++) {
258 r
= (data
[x
] >> default_red_shift
) & 0xFF;
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
);
267 p
+= im
->bytes_per_line
/2;
271 printf("your bit depth is currently unhandled\n");
275 void TrueRenderControl::highlight(pixel32
*x
, pixel32
*y
, bool raised
) const
287 r
= (*up
>> default_red_shift
) & 0xFF;
289 g
= (*up
>> default_green_shift
) & 0xFF;
291 b
= (*up
>> default_blue_shift
) & 0xFF;
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
);
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
);
309 void TrueRenderControl::drawBackground(Surface
& sf
,
310 const RenderTexture
&texture
) const
312 assert(_screen
== sf
._screen
);
313 assert(_screen
== texture
.color().screen());
315 if (texture
.gradient() == RenderTexture::Solid
)
316 drawSolidBackground(sf
, texture
);
318 drawGradientBackground(sf
, texture
);
This page took 0.047799 seconds and 4 git commands to generate.