]> icculus.org git repositories - mikachu/openbox.git/blob - render/gradient.c
make bevel strengths theme-selectable
[mikachu/openbox.git] / render / gradient.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    gradient.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6    Copyright (c) 2003        Derek Foreman
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    See the COPYING file for a copy of the GNU General Public License.
19 */
20
21 #include "render.h"
22 #include "gradient.h"
23 #include "color.h"
24 #include <glib.h>
25
26 static void highlight(RrSurface *s, RrPixel32 *x, RrPixel32 *y, gboolean raised);
27 static void gradient_parentrelative(RrAppearance *a, gint w, gint h);
28 static void gradient_solid(RrAppearance *l, gint w, gint h);
29 static void gradient_splitvertical(RrAppearance *a, gint w, gint h);
30 static void gradient_vertical(RrSurface *sf, gint w, gint h);
31 static void gradient_horizontal(RrSurface *sf, gint w, gint h);
32 static void gradient_mirrorhorizontal(RrSurface *sf, gint w, gint h);
33 static void gradient_diagonal(RrSurface *sf, gint w, gint h);
34 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h);
35 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh);
36
37 void RrRender(RrAppearance *a, gint w, gint h)
38 {
39     RrPixel32 *data = a->surface.pixel_data;
40     RrPixel32 current;
41     guint r,g,b;
42     gint off, x;
43
44     switch (a->surface.grad) {
45     case RR_SURFACE_PARENTREL:
46         gradient_parentrelative(a, w, h);
47         break;
48     case RR_SURFACE_SOLID:
49         gradient_solid(a, w, h);
50         break;
51     case RR_SURFACE_SPLIT_VERTICAL:
52         gradient_splitvertical(a, w, h);
53         break;
54     case RR_SURFACE_VERTICAL:
55         gradient_vertical(&a->surface, w, h);
56         break;
57     case RR_SURFACE_HORIZONTAL:
58         gradient_horizontal(&a->surface, w, h);
59         break;
60     case RR_SURFACE_MIRROR_HORIZONTAL:
61         gradient_mirrorhorizontal(&a->surface, w, h);
62         break;
63     case RR_SURFACE_DIAGONAL:
64         gradient_diagonal(&a->surface, w, h);
65         break;
66     case RR_SURFACE_CROSS_DIAGONAL:
67         gradient_crossdiagonal(&a->surface, w, h);
68         break;
69     case RR_SURFACE_PYRAMID:
70         gradient_pyramid(&a->surface, w, h);
71         break;
72     default:
73         g_assert_not_reached(); /* unhandled gradient */
74         return;
75     }
76   
77     if (a->surface.interlaced) {
78         gint i;
79         RrPixel32 *p;
80
81         r = a->surface.interlace_color->r;
82         g = a->surface.interlace_color->g;
83         b = a->surface.interlace_color->b;
84         current = (r << RrDefaultRedOffset)
85             + (g << RrDefaultGreenOffset)
86             + (b << RrDefaultBlueOffset);
87         p = data;
88         for (i = 0; i < h; i += 2, p += w)
89             for (x = 0; x < w; ++x, ++p)
90                 *p = current;
91     }
92
93     if (a->surface.relief == RR_RELIEF_FLAT && a->surface.border) {
94         r = a->surface.border_color->r;
95         g = a->surface.border_color->g;
96         b = a->surface.border_color->b;
97         current = (r << RrDefaultRedOffset)
98             + (g << RrDefaultGreenOffset)
99             + (b << RrDefaultBlueOffset);
100         for (off = 0, x = 0; x < w; ++x, off++) {
101             *(data + off) = current;
102             *(data + off + ((h-1) * w)) = current;
103         }
104         for (off = 0, x = 0; x < h; ++x, off++) {
105             *(data + (off * w)) = current;
106             *(data + (off * w) + w - 1) = current;
107         }
108     }
109
110     if (a->surface.relief != RR_RELIEF_FLAT) {
111         if (a->surface.bevel == RR_BEVEL_1) {
112             for (off = 1, x = 1; x < w - 1; ++x, off++)
113                 highlight(&a->surface, data + off,
114                           data + off + (h-1) * w,
115                           a->surface.relief==RR_RELIEF_RAISED);
116             for (off = 0, x = 0; x < h; ++x, off++)
117                 highlight(&a->surface, data + off * w,
118                           data + off * w + w - 1,
119                           a->surface.relief==RR_RELIEF_RAISED);
120         }
121
122         if (a->surface.bevel == RR_BEVEL_2) {
123             for (off = 2, x = 2; x < w - 2; ++x, off++)
124                 highlight(&a->surface, data + off + w,
125                           data + off + (h-2) * w,
126                           a->surface.relief==RR_RELIEF_RAISED);
127             for (off = 1, x = 1; x < h-1; ++x, off++)
128                 highlight(&a->surface, data + off * w + 1,
129                           data + off * w + w - 2,
130                           a->surface.relief==RR_RELIEF_RAISED);
131         }
132     }
133 }
134
135 static void highlight(RrSurface *s, RrPixel32 *x, RrPixel32 *y, gboolean raised)
136 {
137     gint r, g, b;
138
139     RrPixel32 *up, *down;
140     if (raised) {
141         up = x;
142         down = y;
143     } else {
144         up = y;
145         down = x;
146     }
147
148     r = (*up >> RrDefaultRedOffset) & 0xFF;
149     r += (r * s->bevel_light_adjust) >> 8;
150     g = (*up >> RrDefaultGreenOffset) & 0xFF;
151     g += (g * s->bevel_light_adjust) >> 8;
152     b = (*up >> RrDefaultBlueOffset) & 0xFF;
153     b += (b * s->bevel_light_adjust) >> 8;
154     if (r > 0xFF) r = 0xFF;
155     if (g > 0xFF) g = 0xFF;
156     if (b > 0xFF) b = 0xFF;
157     *up = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
158         + (b << RrDefaultBlueOffset);
159   
160     r = (*down >> RrDefaultRedOffset) & 0xFF;
161     r -= (r * s->bevel_dark_adjust) >> 8;
162     g = (*down >> RrDefaultGreenOffset) & 0xFF;
163     g -= (g * s->bevel_dark_adjust) >> 8;
164     b = (*down >> RrDefaultBlueOffset) & 0xFF;
165     b -= (b * s->bevel_dark_adjust) >> 8;
166     *down = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
167         + (b << RrDefaultBlueOffset);
168 }
169
170 static void create_bevel_colors(RrAppearance *l)
171 {
172     gint r, g, b;
173
174     /* light color */
175     r = l->surface.primary->r;
176     r += (r * l->surface.bevel_light_adjust) >> 8;
177     g = l->surface.primary->g;
178     g += (g * l->surface.bevel_light_adjust) >> 8;
179     b = l->surface.primary->b;
180     b += (b * l->surface.bevel_light_adjust) >> 8;
181     if (r > 0xFF) r = 0xFF;
182     if (g > 0xFF) g = 0xFF;
183     if (b > 0xFF) b = 0xFF;
184     g_assert(!l->surface.bevel_light);
185     l->surface.bevel_light = RrColorNew(l->inst, r, g, b);
186
187     /* dark color */
188     r = l->surface.primary->r;
189     r -= (r * l->surface.bevel_dark_adjust) >> 8;
190     g = l->surface.primary->g;
191     g -= (g * l->surface.bevel_dark_adjust) >> 8;
192     b = l->surface.primary->b;
193     b -= (b * l->surface.bevel_dark_adjust) >> 8;
194     g_assert(!l->surface.bevel_dark);
195     l->surface.bevel_dark = RrColorNew(l->inst, r, g, b);
196 }
197
198 static void gradient_parentrelative(RrAppearance *a, gint w, gint h)
199 {
200     RrPixel32 *source, *dest;
201     gint sw, sh, partial_w, partial_h, i;
202
203     g_assert (a->surface.parent);
204     g_assert (a->surface.parent->w);
205
206     sw = a->surface.parent->w;
207     sh = a->surface.parent->h;
208
209     /* This is a little hack. When a texture is parentrelative, and the same
210        area as the parent, and has a bevel, it will draw its bevel on top
211        of the parent's, amplifying it. So instead, rerender the child with
212        the parent's settings, but the child's bevel and interlace */
213     if (a->surface.relief != RR_RELIEF_FLAT &&
214         (a->surface.parent->surface.relief != RR_RELIEF_FLAT ||
215          a->surface.parent->surface.border) &&
216         !a->surface.parentx && !a->surface.parenty &&
217         sw == w && sh == h)
218     {
219         RrSurface old = a->surface;
220         a->surface = a->surface.parent->surface;
221
222         /* turn these off for the parent */
223         a->surface.relief = RR_RELIEF_FLAT;
224         a->surface.border = FALSE;
225
226         a->surface.pixel_data = old.pixel_data;
227
228         RrRender(a, w, h);
229         a->surface = old;
230     } else {
231         source = (a->surface.parent->surface.pixel_data +
232                   a->surface.parentx + sw * a->surface.parenty);
233         dest = a->surface.pixel_data;
234
235         if (a->surface.parentx + w > sw) {
236             partial_w = sw - a->surface.parentx;
237         } else partial_w = w;
238
239         if (a->surface.parenty + h > sh) {
240             partial_h = sh - a->surface.parenty;
241         } else partial_h = h;
242
243         for (i = 0; i < partial_h; i++, source += sw, dest += w) {
244             memcpy(dest, source, partial_w * sizeof(RrPixel32));
245         }
246     }
247 }
248
249 static void gradient_solid(RrAppearance *l, gint w, gint h) 
250 {
251     gint i;
252     RrPixel32 pix;
253     RrPixel32 *data = l->surface.pixel_data;
254     RrSurface *sp = &l->surface;
255     gint left = 0, top = 0, right = w - 1, bottom = h - 1;
256
257     pix = (sp->primary->r << RrDefaultRedOffset)
258         + (sp->primary->g << RrDefaultGreenOffset)
259         + (sp->primary->b << RrDefaultBlueOffset);
260
261     for (i = 0; i < w * h; i++)
262         *data++ = pix;
263
264     if (sp->interlaced)
265         return;
266
267     XFillRectangle(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->primary),
268                    0, 0, w, h);
269
270     switch (sp->relief) {
271     case RR_RELIEF_RAISED:
272         if (!sp->bevel_dark)
273             create_bevel_colors(l);
274
275         switch (sp->bevel) {
276         case RR_BEVEL_1:
277             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
278                       left, bottom, right, bottom);
279             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
280                       right, bottom, right, top);
281                 
282             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
283                       left, top, right, top);
284             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
285                       left, bottom, left, top);
286             break;
287         case RR_BEVEL_2:
288             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
289                       left + 2, bottom - 1, right - 2, bottom - 1);
290             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
291                       right - 1, bottom - 1, right - 1, top + 1);
292
293             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
294                       left + 2, top + 1, right - 2, top + 1);
295             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
296                       left + 1, bottom - 1, left + 1, top + 1);
297             break;
298         default:
299             g_assert_not_reached(); /* unhandled BevelType */
300         }
301         break;
302     case RR_RELIEF_SUNKEN:
303         if (!sp->bevel_dark)
304             create_bevel_colors(l);
305
306         switch (sp->bevel) {
307         case RR_BEVEL_1:
308             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
309                       left, bottom, right, bottom);
310             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
311                       right, bottom, right, top);
312       
313             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
314                       left, top, right, top);
315             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
316                       left, bottom, left, top);
317             break;
318         case RR_BEVEL_2:
319             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
320                       left + 2, bottom - 1, right - 2, bottom - 1);
321             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
322                       right - 1, bottom - 1, right - 1, top + 1);
323
324             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
325                       left + 2, top + 1, right - 2, top + 1);
326             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
327                       left + 1, bottom - 1, left + 1, top + 1);
328             break;
329         default:
330             g_assert_not_reached(); /* unhandled BevelType */
331         }
332         break;
333     case RR_RELIEF_FLAT:
334         if (sp->border) {
335             XDrawRectangle(RrDisplay(l->inst), l->pixmap,
336                            RrColorGC(sp->border_color),
337                            left, top, right, bottom);
338         }
339         break;
340     default:  
341         g_assert_not_reached(); /* unhandled ReliefType */
342     }
343 }
344
345 /* * * * * * * * * * * * * * GRADIENT MAGIC WOOT * * * * * * * * * * * * * * */
346
347 #define VARS(x)                                                     \
348     guint color##x[3];                                       \
349     gint len##x, cdelta##x[3], error##x[3] = { 0, 0, 0 }, inc##x[3]; \
350     gboolean bigslope##x[3] /* color slope > 1 */
351
352 #define SETUP(x, from, to, w)         \
353     len##x = w;                       \
354                                       \
355     color##x[0] = from->r;            \
356     color##x[1] = from->g;            \
357     color##x[2] = from->b;            \
358                                       \
359     cdelta##x[0] = to->r - from->r;   \
360     cdelta##x[1] = to->g - from->g;   \
361     cdelta##x[2] = to->b - from->b;   \
362                                       \
363     if (cdelta##x[0] < 0) {           \
364         cdelta##x[0] = -cdelta##x[0]; \
365         inc##x[0] = -1;               \
366     } else                            \
367         inc##x[0] = 1;                \
368     if (cdelta##x[1] < 0) {           \
369         cdelta##x[1] = -cdelta##x[1]; \
370         inc##x[1] = -1;               \
371     } else                            \
372         inc##x[1] = 1;                \
373     if (cdelta##x[2] < 0) {           \
374         cdelta##x[2] = -cdelta##x[2]; \
375         inc##x[2] = -1;               \
376     } else                            \
377         inc##x[2] = 1;                \
378     bigslope##x[0] = cdelta##x[0] > w;\
379     bigslope##x[1] = cdelta##x[1] > w;\
380     bigslope##x[2] = cdelta##x[2] > w
381
382 #define COLOR_RR(x, c)                       \
383     c->r = color##x[0];                      \
384     c->g = color##x[1];                      \
385     c->b = color##x[2]
386
387 #define COLOR(x)                             \
388     ((color##x[0] << RrDefaultRedOffset) +   \
389      (color##x[1] << RrDefaultGreenOffset) + \
390      (color##x[2] << RrDefaultBlueOffset))
391
392 #define INCREMENT(x, i) \
393     (inc##x[i])
394
395 #define NEXT(x)                                           \
396 {                                                         \
397     gint i;                                                \
398     for (i = 2; i >= 0; --i) {                            \
399         if (!cdelta##x[i]) continue;                      \
400                                                           \
401         if (!bigslope##x[i]) {                            \
402             /* Y (color) is dependant on X */             \
403             error##x[i] += cdelta##x[i];                  \
404             if ((error##x[i] << 1) >= len##x) {           \
405                 color##x[i] += INCREMENT(x, i);           \
406                 error##x[i] -= len##x;                    \
407             }                                             \
408         } else {                                          \
409             /* X is dependant on Y (color) */             \
410             while (1) {                                   \
411                 color##x[i] += INCREMENT(x, i);           \
412                 error##x[i] += len##x;                    \
413                 if ((error##x[i] << 1) >= cdelta##x[i]) { \
414                     error##x[i] -= cdelta##x[i];          \
415                     break;                                \
416                 }                                         \
417             }                                             \
418         }                                                 \
419     }                                                     \
420 }
421
422 static void gradient_splitvertical(RrAppearance *a, gint w, gint h)
423 {
424     gint x, y1, y2, y3, r, g, b;
425     RrSurface *sf = &a->surface;
426     RrPixel32 *data = sf->pixel_data;
427     RrPixel32 current;
428     RrColor *primary_light, *secondary_light;
429     gint y1sz, y2sz, y3sz;
430
431     VARS(y1);
432     VARS(y2);
433     VARS(y3);
434
435     r = sf->primary->r;
436     r += r >> 2;
437     g = sf->primary->g;
438     g += g >> 2;
439     b = sf->primary->b;
440     b += b >> 2;
441     if (r > 0xFF) r = 0xFF;
442     if (g > 0xFF) g = 0xFF;
443     if (b > 0xFF) b = 0xFF;
444     primary_light = RrColorNew(a->inst, r, g, b);
445
446     r = sf->secondary->r;
447     r += r >> 4;
448     g = sf->secondary->g;
449     g += g >> 4;
450     b = sf->secondary->b;
451     b += b >> 4;
452     if (r > 0xFF) r = 0xFF;
453     if (g > 0xFF) g = 0xFF;
454     if (b > 0xFF) b = 0xFF;
455     secondary_light = RrColorNew(a->inst, r, g, b);
456
457     y1sz = MAX(h/2 - 1, 1);
458     /* setup to get the colors _in between_ these other 2 */
459     y2sz = (h < 3 ? 0 : (h % 2 ? 3 : 2));
460     y3sz = MAX(h/2 - 1, 0);
461
462     SETUP(y1, primary_light, sf->primary, y1sz);
463     if (y2sz) {
464         SETUP(y2, sf->primary, sf->secondary, y2sz);
465         NEXT(y2); /* skip the first one, its the same as the last of y1 */
466     }
467     SETUP(y3, sf->secondary, secondary_light,  y3sz);
468
469     for (y1 = y1sz; y1 > 0; --y1) {
470         current = COLOR(y1);
471         for (x = w - 1; x >= 0; --x)
472             *(data++) = current;
473
474         NEXT(y1);
475     }
476
477     for (y2 = y2sz; y2 > 0; --y2) {
478         current = COLOR(y2);
479         for (x = w - 1; x >= 0; --x)
480             *(data++) = current;
481         
482         NEXT(y2);
483     }
484     
485     for (y3 = y3sz; y3 > 0; --y3) {
486         current = COLOR(y3);
487         for (x = w - 1; x >= 0; --x)
488             *(data++) = current;
489
490         NEXT(y3);
491     }
492
493     RrColorFree(primary_light);
494     RrColorFree(secondary_light);
495 }
496
497 static void gradient_horizontal(RrSurface *sf, gint w, gint h)
498 {
499     gint x, y;
500     RrPixel32 *data = sf->pixel_data, *datav;
501     RrPixel32 current;
502
503     VARS(x);
504     SETUP(x, sf->primary, sf->secondary, w);
505
506     for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
507         current = COLOR(x);
508         datav = data;
509         for (y = h - 1; y >= 0; --y) {  /* 0 -> h */
510             *datav = current;
511             datav += w;
512         }
513         ++data;
514
515         NEXT(x);
516     }
517     current = COLOR(x);
518     for (y = h - 1; y >= 0; --y)  /* 0 -> h */
519         *(data + y * w) = current;
520 }
521
522 static void gradient_mirrorhorizontal(RrSurface *sf, gint w, gint h)
523 {
524     gint x, y;
525     RrPixel32 *data = sf->pixel_data, *datav;
526     RrPixel32 current;
527
528     VARS(x);
529     SETUP(x, sf->primary, sf->secondary, w/2);
530
531     if (w > 1) {
532         for (x = w - 1; x > w/2-1; --x) {  /* 0 -> w-1 */
533             current = COLOR(x);
534             datav = data;
535             for (y = h - 1; y >= 0; --y) {  /* 0 -> h */
536                 *datav = current;
537                 datav += w;
538             }
539             ++data;
540
541             NEXT(x);
542         }
543         SETUP(x, sf->secondary, sf->primary, w/2);
544         for (x = w/2 - 1; x > 0; --x) {  /* 0 -> w-1 */
545             current = COLOR(x);
546             datav = data;
547             for (y = h - 1; y >= 0; --y) {  /* 0 -> h */
548                 *datav = current;
549                 datav += w;
550             }
551             ++data;
552
553             NEXT(x);
554         }
555     }
556     current = COLOR(x);
557     for (y = h - 1; y >= 0; --y)  /* 0 -> h */
558         *(data + y * w) = current;
559 }
560
561 static void gradient_vertical(RrSurface *sf, gint w, gint h)
562 {
563     gint x, y;
564     RrPixel32 *data = sf->pixel_data;
565     RrPixel32 current;
566
567     VARS(y);
568     SETUP(y, sf->primary, sf->secondary, h);
569
570     for (y = h - 1; y > 0; --y) {  /* 0 -> h-1 */
571         current = COLOR(y);
572         for (x = w - 1; x >= 0; --x)  /* 0 -> w */
573             *(data++) = current;
574
575         NEXT(y);
576     }
577     current = COLOR(y);
578     for (x = w - 1; x >= 0; --x)  /* 0 -> w */
579         *(data++) = current;
580 }
581
582
583 static void gradient_diagonal(RrSurface *sf, gint w, gint h)
584 {
585     gint x, y;
586     RrPixel32 *data = sf->pixel_data;
587     RrColor left, right;
588     RrColor extracorner;
589
590     VARS(lefty);
591     VARS(righty);
592     VARS(x);
593
594     extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
595     extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
596     extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
597
598     SETUP(lefty, sf->primary, (&extracorner), h);
599     SETUP(righty, (&extracorner), sf->secondary, h);
600
601     for (y = h - 1; y > 0; --y) {  /* 0 -> h-1 */
602         COLOR_RR(lefty, (&left));
603         COLOR_RR(righty, (&right));
604
605         SETUP(x, (&left), (&right), w);
606
607         for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
608             *(data++) = COLOR(x);
609
610             NEXT(x);
611         }
612         *(data++) = COLOR(x);
613
614         NEXT(lefty);
615         NEXT(righty);
616     }
617     COLOR_RR(lefty, (&left));
618     COLOR_RR(righty, (&right));
619
620     SETUP(x, (&left), (&right), w);
621
622     for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
623         *(data++) = COLOR(x);
624         
625         NEXT(x);
626     }
627     *data = COLOR(x);
628 }
629
630 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h)
631 {
632     gint x, y;
633     RrPixel32 *data = sf->pixel_data;
634     RrColor left, right;
635     RrColor extracorner;
636
637     VARS(lefty);
638     VARS(righty);
639     VARS(x);
640
641     extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
642     extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
643     extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
644
645     SETUP(lefty, (&extracorner), sf->secondary, h);
646     SETUP(righty, sf->primary, (&extracorner), h);
647
648     for (y = h - 1; y > 0; --y) {  /* 0 -> h-1 */
649         COLOR_RR(lefty, (&left));
650         COLOR_RR(righty, (&right));
651
652         SETUP(x, (&left), (&right), w);
653
654         for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
655             *(data++) = COLOR(x);
656
657             NEXT(x);
658         }
659         *(data++) = COLOR(x);
660
661         NEXT(lefty);
662         NEXT(righty);
663     }
664     COLOR_RR(lefty, (&left));
665     COLOR_RR(righty, (&right));
666
667     SETUP(x, (&left), (&right), w);
668
669     for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
670         *(data++) = COLOR(x);
671         
672         NEXT(x);
673     }
674     *data = COLOR(x);
675 }
676
677 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh)
678 {
679     gint x, y, w = (inw >> 1) + 1, h = (inh >> 1) + 1;
680     RrPixel32 *data = sf->pixel_data;
681     RrPixel32 *end = data + inw*inh - 1;
682     RrPixel32 current;
683     RrColor left, right;
684     RrColor extracorner;
685
686     VARS(lefty);
687     VARS(righty);
688     VARS(x);
689
690     extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
691     extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
692     extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
693
694     SETUP(lefty, (&extracorner), sf->secondary, h);
695     SETUP(righty, sf->primary, (&extracorner), h);
696
697     for (y = h - 1; y > 0; --y) {  /* 0 -> h-1 */
698         COLOR_RR(lefty, (&left));
699         COLOR_RR(righty, (&right));
700
701         SETUP(x, (&left), (&right), w);
702
703         for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
704             current = COLOR(x);
705             *(data+x) = current;
706             *(data+inw-x) = current;
707             *(end-x) = current;
708             *(end-(inw-x)) = current;
709
710             NEXT(x);
711         }
712         current = COLOR(x);
713         *(data+x) = current;
714         *(data+inw-x) = current;
715         *(end-x) = current;
716         *(end-(inw-x)) = current;
717
718         data+=inw;
719         end-=inw;
720
721         NEXT(lefty);
722         NEXT(righty);
723     }
724     COLOR_RR(lefty, (&left));
725     COLOR_RR(righty, (&right));
726
727     SETUP(x, (&left), (&right), w);
728
729     for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
730         current = COLOR(x);
731         *(data+x) = current;
732         *(data+inw-x) = current;
733         *(end-x) = current;
734         *(end-(inw-x)) = current;
735         
736         NEXT(x);
737     }
738     current = COLOR(x);
739     *(data+x) = current;
740     *(data+inw-x) = current;
741     *(end-x) = current;
742     *(end-(inw-x)) = current;
743 }
744