6 static void highlight(RrPixel32 *x, RrPixel32 *y, gboolean raised);
7 static void gradient_solid(RrAppearance *l, int w, int h);
8 static void gradient_vertical(RrSurface *sf, int w, int h);
9 static void gradient_horizontal(RrSurface *sf, int w, int h);
10 static void gradient_diagonal(RrSurface *sf, int w, int h);
11 static void gradient_crossdiagonal(RrSurface *sf, int w, int h);
12 static void gradient_pyramid(RrSurface *sf, int inw, int inh);
14 void RrRender(RrAppearance *a, int w, int h)
16 RrPixel32 *data = a->surface.pixel_data;
21 switch (a->surface.grad) {
22 case RR_SURFACE_SOLID:
23 gradient_solid(a, w, h);
25 case RR_SURFACE_VERTICAL:
26 gradient_vertical(&a->surface, w, h);
28 case RR_SURFACE_HORIZONTAL:
29 gradient_horizontal(&a->surface, w, h);
31 case RR_SURFACE_DIAGONAL:
32 gradient_diagonal(&a->surface, w, h);
34 case RR_SURFACE_CROSS_DIAGONAL:
35 gradient_crossdiagonal(&a->surface, w, h);
37 case RR_SURFACE_PYRAMID:
38 gradient_pyramid(&a->surface, w, h);
41 g_assert_not_reached(); /* unhandled gradient */
45 if (a->surface.interlaced) {
49 r = a->surface.interlace_color->r;
50 g = a->surface.interlace_color->g;
51 b = a->surface.interlace_color->b;
52 current = (r << RrDefaultRedOffset)
53 + (g << RrDefaultGreenOffset)
54 + (b << RrDefaultBlueOffset);
56 for (i = 0; i < h; i += 2, p += w)
57 for (x = 0; x < w; ++x, ++p)
61 if (a->surface.relief == RR_RELIEF_FLAT && a->surface.border) {
62 r = a->surface.border_color->r;
63 g = a->surface.border_color->g;
64 b = a->surface.border_color->b;
65 current = (r << RrDefaultRedOffset)
66 + (g << RrDefaultGreenOffset)
67 + (b << RrDefaultBlueOffset);
68 for (off = 0, x = 0; x < w; ++x, off++) {
69 *(data + off) = current;
70 *(data + off + ((h-1) * w)) = current;
72 for (off = 0, x = 0; x < h; ++x, off++) {
73 *(data + (off * w)) = current;
74 *(data + (off * w) + w - 1) = current;
78 if (a->surface.relief != RR_RELIEF_FLAT) {
79 if (a->surface.bevel == RR_BEVEL_1) {
80 for (off = 1, x = 1; x < w - 1; ++x, off++)
82 data + off + (h-1) * w,
83 a->surface.relief==RR_RELIEF_RAISED);
84 for (off = 0, x = 0; x < h; ++x, off++)
85 highlight(data + off * w,
86 data + off * w + w - 1,
87 a->surface.relief==RR_RELIEF_RAISED);
90 if (a->surface.bevel == RR_BEVEL_2) {
91 for (off = 2, x = 2; x < w - 2; ++x, off++)
92 highlight(data + off + w,
93 data + off + (h-2) * w,
94 a->surface.relief==RR_RELIEF_RAISED);
95 for (off = 1, x = 1; x < h-1; ++x, off++)
96 highlight(data + off * w + 1,
97 data + off * w + w - 2,
98 a->surface.relief==RR_RELIEF_RAISED);
103 static void highlight(RrPixel32 *x, RrPixel32 *y, gboolean raised)
107 RrPixel32 *up, *down;
115 r = (*up >> RrDefaultRedOffset) & 0xFF;
117 g = (*up >> RrDefaultGreenOffset) & 0xFF;
119 b = (*up >> RrDefaultBlueOffset) & 0xFF;
121 if (r > 0xFF) r = 0xFF;
122 if (g > 0xFF) g = 0xFF;
123 if (b > 0xFF) b = 0xFF;
124 *up = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
125 + (b << RrDefaultBlueOffset);
127 r = (*down >> RrDefaultRedOffset) & 0xFF;
128 r = (r >> 1) + (r >> 2);
129 g = (*down >> RrDefaultGreenOffset) & 0xFF;
130 g = (g >> 1) + (g >> 2);
131 b = (*down >> RrDefaultBlueOffset) & 0xFF;
132 b = (b >> 1) + (b >> 2);
133 *down = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
134 + (b << RrDefaultBlueOffset);
137 static void create_bevel_colors(RrAppearance *l)
142 r = l->surface.primary->r;
144 g = l->surface.primary->g;
146 b = l->surface.primary->b;
148 if (r > 0xFF) r = 0xFF;
149 if (g > 0xFF) g = 0xFF;
150 if (b > 0xFF) b = 0xFF;
151 g_assert(!l->surface.bevel_light);
152 l->surface.bevel_light = RrColorNew(l->inst, r, g, b);
155 r = l->surface.primary->r;
156 r = (r >> 1) + (r >> 2);
157 g = l->surface.primary->g;
158 g = (g >> 1) + (g >> 2);
159 b = l->surface.primary->b;
160 b = (b >> 1) + (b >> 2);
161 g_assert(!l->surface.bevel_dark);
162 l->surface.bevel_dark = RrColorNew(l->inst, r, g, b);
165 static void gradient_solid(RrAppearance *l, int w, int h)
169 RrSurface *sp = &l->surface;
170 int left = 0, top = 0, right = w - 1, bottom = h - 1;
172 pix = (sp->primary->r << RrDefaultRedOffset)
173 + (sp->primary->g << RrDefaultGreenOffset)
174 + (sp->primary->b << RrDefaultBlueOffset);
176 for (a = 0; a < w; a++)
177 for (b = 0; b < h; b++)
178 sp->pixel_data[a + b * w] = pix;
180 XFillRectangle(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->primary),
183 if (sp->interlaced) {
184 for (i = 0; i < h; i += 2)
185 XDrawLine(RrDisplay(l->inst), l->pixmap,
186 RrColorGC(sp->interlace_color),
190 switch (sp->relief) {
191 case RR_RELIEF_RAISED:
193 create_bevel_colors(l);
197 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
198 left, bottom, right, bottom);
199 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
200 right, bottom, right, top);
202 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
203 left, top, right, top);
204 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
205 left, bottom, left, top);
208 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
209 left + 1, bottom - 2, right - 2, bottom - 2);
210 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
211 right - 2, bottom - 2, right - 2, top + 1);
213 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
214 left + 1, top + 1, right - 2, top + 1);
215 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
216 left + 1, bottom - 2, left + 1, top + 1);
219 g_assert_not_reached(); /* unhandled BevelType */
222 case RR_RELIEF_SUNKEN:
224 create_bevel_colors(l);
228 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
229 left, bottom, right, bottom);
230 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
231 right, bottom, right, top);
233 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
234 left, top, right, top);
235 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
236 left, bottom, left, top);
239 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
240 left + 1, bottom - 2, right - 2, bottom - 2);
241 XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
242 right - 2, bottom - 2, right - 2, top + 1);
244 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
245 left + 1, top + 1, right - 2, top + 1);
246 XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
247 left + 1, bottom - 2, left + 1, top + 1);
251 g_assert_not_reached(); /* unhandled BevelType */
256 XDrawRectangle(RrDisplay(l->inst), l->pixmap,
257 RrColorGC(sp->border_color),
258 left, top, right, bottom);
262 g_assert_not_reached(); /* unhandled ReliefType */
266 /* * * * * * * * * * * * * * GRADIENT MAGIC WOOT * * * * * * * * * * * * * * */
269 unsigned int color##x[3]; \
270 int len##x, cdelta##x[3], error##x[3] = { 0, 0, 0 }, inc##x[3]; \
271 gboolean bigslope##x[3] /* color slope > 1 */
273 #define SETUP(x, from, to, w) \
276 color##x[0] = from->r; \
277 color##x[1] = from->g; \
278 color##x[2] = from->b; \
280 cdelta##x[0] = to->r - from->r; \
281 cdelta##x[1] = to->g - from->g; \
282 cdelta##x[2] = to->b - from->b; \
284 if (cdelta##x[0] < 0) { \
285 cdelta##x[0] = -cdelta##x[0]; \
289 if (cdelta##x[1] < 0) { \
290 cdelta##x[1] = -cdelta##x[1]; \
294 if (cdelta##x[2] < 0) { \
295 cdelta##x[2] = -cdelta##x[2]; \
299 bigslope##x[0] = cdelta##x[0] > w;\
300 bigslope##x[1] = cdelta##x[1] > w;\
301 bigslope##x[2] = cdelta##x[2] > w
303 #define COLOR_RR(x, c) \
304 c->r = color##x[0]; \
305 c->g = color##x[1]; \
309 ((color##x[0] << RrDefaultRedOffset) + \
310 (color##x[1] << RrDefaultGreenOffset) + \
311 (color##x[2] << RrDefaultBlueOffset))
313 #define INCREMENT(x, i) \
319 for (i = 2; i >= 0; --i) { \
320 if (!cdelta##x[i]) continue; \
322 if (!bigslope##x[i]) { \
323 /* Y (color) is dependant on X */ \
324 error##x[i] += cdelta##x[i]; \
325 if ((error##x[i] << 1) >= len##x) { \
326 color##x[i] += INCREMENT(x, i); \
327 error##x[i] -= len##x; \
330 /* X is dependant on Y (color) */ \
332 color##x[i] += INCREMENT(x, i); \
333 error##x[i] += len##x; \
334 if ((error##x[i] << 1) >= cdelta##x[i]) { \
335 error##x[i] -= cdelta##x[i]; \
343 static void gradient_horizontal(RrSurface *sf, int w, int h)
346 RrPixel32 *data = sf->pixel_data, *datav;
350 SETUP(x, sf->primary, sf->secondary, w);
352 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
355 for (y = h - 1; y >= 0; --y) { /* 0 -> h */
364 for (y = h - 1; y >= 0; --y) /* 0 -> h */
365 *(data + y * w) = current;
368 static void gradient_vertical(RrSurface *sf, int w, int h)
371 RrPixel32 *data = sf->pixel_data;
375 SETUP(y, sf->primary, sf->secondary, h);
377 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
379 for (x = w - 1; x >= 0; --x) /* 0 -> w */
385 for (x = w - 1; x >= 0; --x) /* 0 -> w */
390 static void gradient_diagonal(RrSurface *sf, int w, int h)
393 RrPixel32 *data = sf->pixel_data;
401 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
402 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
403 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
405 SETUP(lefty, sf->primary, (&extracorner), h);
406 SETUP(righty, (&extracorner), sf->secondary, h);
408 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
409 COLOR_RR(lefty, (&left));
410 COLOR_RR(righty, (&right));
412 SETUP(x, (&left), (&right), w);
414 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
415 *(data++) = COLOR(x);
419 *(data++) = COLOR(x);
424 COLOR_RR(lefty, (&left));
425 COLOR_RR(righty, (&right));
427 SETUP(x, (&left), (&right), w);
429 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
430 *(data++) = COLOR(x);
437 static void gradient_crossdiagonal(RrSurface *sf, int w, int h)
440 RrPixel32 *data = sf->pixel_data;
448 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
449 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
450 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
452 SETUP(lefty, (&extracorner), sf->secondary, h);
453 SETUP(righty, sf->primary, (&extracorner), h);
455 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
456 COLOR_RR(lefty, (&left));
457 COLOR_RR(righty, (&right));
459 SETUP(x, (&left), (&right), w);
461 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
462 *(data++) = COLOR(x);
466 *(data++) = COLOR(x);
471 COLOR_RR(lefty, (&left));
472 COLOR_RR(righty, (&right));
474 SETUP(x, (&left), (&right), w);
476 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
477 *(data++) = COLOR(x);
484 static void gradient_pyramid(RrSurface *sf, int inw, int inh)
486 int x, y, w = (inw >> 1) + 1, h = (inh >> 1) + 1;
487 RrPixel32 *data = sf->pixel_data;
488 RrPixel32 *end = data + inw*inh - 1;
497 extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
498 extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
499 extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
501 SETUP(lefty, (&extracorner), sf->secondary, h);
502 SETUP(righty, sf->primary, (&extracorner), h);
504 for (y = h - 1; y > 0; --y) { /* 0 -> h-1 */
505 COLOR_RR(lefty, (&left));
506 COLOR_RR(righty, (&right));
508 SETUP(x, (&left), (&right), w);
510 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
513 *(data+inw-x) = current;
515 *(end-(inw-x)) = current;
521 *(data+inw-x) = current;
523 *(end-(inw-x)) = current;
531 COLOR_RR(lefty, (&left));
532 COLOR_RR(righty, (&right));
534 SETUP(x, (&left), (&right), w);
536 for (x = w - 1; x > 0; --x) { /* 0 -> w-1 */
539 *(data+inw-x) = current;
541 *(end-(inw-x)) = current;
547 *(data+inw-x) = current;
549 *(end-(inw-x)) = current;