fix when 1or2px splitvertical
[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(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(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(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(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(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(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     r = (*up >> RrDefaultRedOffset) & 0xFF;
148     r += r >> 1;
149     g = (*up >> RrDefaultGreenOffset) & 0xFF;
150     g += g >> 1;
151     b = (*up >> RrDefaultBlueOffset) & 0xFF;
152     b += b >> 1;
153     if (r > 0xFF) r = 0xFF;
154     if (g > 0xFF) g = 0xFF;
155     if (b > 0xFF) b = 0xFF;
156     *up = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
157         + (b << RrDefaultBlueOffset);
158   
159     r = (*down >> RrDefaultRedOffset) & 0xFF;
160     r = (r >> 1) + (r >> 2);
161     g = (*down >> RrDefaultGreenOffset) & 0xFF;
162     g = (g >> 1) + (g >> 2);
163     b = (*down >> RrDefaultBlueOffset) & 0xFF;
164     b = (b >> 1) + (b >> 2);
165     *down = (r << RrDefaultRedOffset) + (g << RrDefaultGreenOffset)
166         + (b << RrDefaultBlueOffset);
167 }
168
169 static void create_bevel_colors(RrAppearance *l)
170 {
171     gint r, g, b;
172
173     /* light color */
174     r = l->surface.primary->r;
175     r += r >> 1;
176     g = l->surface.primary->g;
177     g += g >> 1;
178     b = l->surface.primary->b;
179     b += b >> 1;
180     if (r > 0xFF) r = 0xFF;
181     if (g > 0xFF) g = 0xFF;
182     if (b > 0xFF) b = 0xFF;
183     g_assert(!l->surface.bevel_light);
184     l->surface.bevel_light = RrColorNew(l->inst, r, g, b);
185
186     /* dark color */
187     r = l->surface.primary->r;
188     r = (r >> 1) + (r >> 2);
189     g = l->surface.primary->g;
190     g = (g >> 1) + (g >> 2);
191     b = l->surface.primary->b;
192     b = (b >> 1) + (b >> 2);
193     g_assert(!l->surface.bevel_dark);
194     l->surface.bevel_dark = RrColorNew(l->inst, r, g, b);
195 }
196
197 static void gradient_parentrelative(RrAppearance *a, gint w, gint h)
198 {
199     RrPixel32 *source, *dest;
200     gint sw, sh, partial_w, partial_h, i;
201
202     g_assert (a->surface.parent);
203     g_assert (a->surface.parent->w);
204
205     sw = a->surface.parent->w;
206     sh = a->surface.parent->h;
207
208     /* This is a little hack. When a texture is parentrelative, and the same
209        area as the parent, and has a bevel, it will draw its bevel on top
210        of the parent's, amplifying it. So instead, rerender the child with
211        the parent's settings, but the child's bevel and interlace */
212     if (a->surface.relief != RR_RELIEF_FLAT &&
213         (a->surface.parent->surface.relief != RR_RELIEF_FLAT ||
214          a->surface.parent->surface.border) &&
215         !a->surface.parentx && !a->surface.parenty &&
216         sw == w && sh == h)
217     {
218         RrSurface old = a->surface;
219         a->surface = a->surface.parent->surface;
220
221         /* turn these off for the parent */
222         a->surface.relief = RR_RELIEF_FLAT;
223         a->surface.border = FALSE;
224
225         a->surface.pixel_data = old.pixel_data;
226
227         RrRender(a, w, h);
228         a->surface = old;
229     } else {
230         source = (a->surface.parent->surface.pixel_data +
231                   a->surface.parentx + sw * a->surface.parenty);
232         dest = a->surface.pixel_data;
233
234         if (a->surface.parentx + w > sw) {
235             partial_w = sw - a->surface.parentx;
236         } else partial_w = w;
237
238         if (a->surface.parenty + h > sh) {
239             partial_h = sh - a->surface.parenty;
240         } else partial_h = h;
241
242         for (i = 0; i < partial_h; i++, source += sw, dest += w) {
243             memcpy(dest, source, partial_w * sizeof(RrPixel32));
244         }
245     }
246 }
247
248 static void gradient_solid(RrAppearance *l, gint w, gint h) 
249 {
250     gint i;
251     RrPixel32 pix;
252     RrPixel32 *data = l->surface.pixel_data;
253     RrSurface *sp = &l->surface;
254     gint left = 0, top = 0, right = w - 1, bottom = h - 1;
255
256     pix = (sp->primary->r << RrDefaultRedOffset)
257         + (sp->primary->g << RrDefaultGreenOffset)
258         + (sp->primary->b << RrDefaultBlueOffset);
259
260     for (i = 0; i < w * h; i++)
261         *data++ = pix;
262
263     if (sp->interlaced)
264         return;
265
266     XFillRectangle(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->primary),
267                    0, 0, w, h);
268
269     switch (sp->relief) {
270     case RR_RELIEF_RAISED:
271         if (!sp->bevel_dark)
272             create_bevel_colors(l);
273
274         switch (sp->bevel) {
275         case RR_BEVEL_1:
276             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
277                       left, bottom, right, bottom);
278             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
279                       right, bottom, right, top);
280                 
281             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
282                       left, top, right, top);
283             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
284                       left, bottom, left, top);
285             break;
286         case RR_BEVEL_2:
287             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
288                       left + 2, bottom - 1, right - 2, bottom - 1);
289             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
290                       right - 1, bottom - 1, right - 1, top + 1);
291
292             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
293                       left + 2, top + 1, right - 2, top + 1);
294             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
295                       left + 1, bottom - 1, left + 1, top + 1);
296             break;
297         default:
298             g_assert_not_reached(); /* unhandled BevelType */
299         }
300         break;
301     case RR_RELIEF_SUNKEN:
302         if (!sp->bevel_dark)
303             create_bevel_colors(l);
304
305         switch (sp->bevel) {
306         case RR_BEVEL_1:
307             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
308                       left, bottom, right, bottom);
309             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
310                       right, bottom, right, top);
311       
312             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
313                       left, top, right, top);
314             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
315                       left, bottom, left, top);
316             break;
317         case RR_BEVEL_2:
318             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
319                       left + 2, bottom - 1, right - 2, bottom - 1);
320             XDrawLine(RrDisplay(l->inst), l->pixmap,RrColorGC(sp->bevel_light),
321                       right - 1, bottom - 1, right - 1, top + 1);
322
323             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
324                       left + 2, top + 1, right - 2, top + 1);
325             XDrawLine(RrDisplay(l->inst), l->pixmap, RrColorGC(sp->bevel_dark),
326                       left + 1, bottom - 1, left + 1, top + 1);
327             break;
328         default:
329             g_assert_not_reached(); /* unhandled BevelType */
330         }
331         break;
332     case RR_RELIEF_FLAT:
333         if (sp->border) {
334             XDrawRectangle(RrDisplay(l->inst), l->pixmap,
335                            RrColorGC(sp->border_color),
336                            left, top, right, bottom);
337         }
338         break;
339     default:  
340         g_assert_not_reached(); /* unhandled ReliefType */
341     }
342 }
343
344 /* * * * * * * * * * * * * * GRADIENT MAGIC WOOT * * * * * * * * * * * * * * */
345
346 #define VARS(x)                                                     \
347     guint color##x[3];                                       \
348     gint len##x, cdelta##x[3], error##x[3] = { 0, 0, 0 }, inc##x[3]; \
349     gboolean bigslope##x[3] /* color slope > 1 */
350
351 #define SETUP(x, from, to, w)         \
352     len##x = w;                       \
353                                       \
354     color##x[0] = from->r;            \
355     color##x[1] = from->g;            \
356     color##x[2] = from->b;            \
357                                       \
358     cdelta##x[0] = to->r - from->r;   \
359     cdelta##x[1] = to->g - from->g;   \
360     cdelta##x[2] = to->b - from->b;   \
361                                       \
362     if (cdelta##x[0] < 0) {           \
363         cdelta##x[0] = -cdelta##x[0]; \
364         inc##x[0] = -1;               \
365     } else                            \
366         inc##x[0] = 1;                \
367     if (cdelta##x[1] < 0) {           \
368         cdelta##x[1] = -cdelta##x[1]; \
369         inc##x[1] = -1;               \
370     } else                            \
371         inc##x[1] = 1;                \
372     if (cdelta##x[2] < 0) {           \
373         cdelta##x[2] = -cdelta##x[2]; \
374         inc##x[2] = -1;               \
375     } else                            \
376         inc##x[2] = 1;                \
377     bigslope##x[0] = cdelta##x[0] > w;\
378     bigslope##x[1] = cdelta##x[1] > w;\
379     bigslope##x[2] = cdelta##x[2] > w
380
381 #define COLOR_RR(x, c)                       \
382     c->r = color##x[0];                      \
383     c->g = color##x[1];                      \
384     c->b = color##x[2]
385
386 #define COLOR(x)                             \
387     ((color##x[0] << RrDefaultRedOffset) +   \
388      (color##x[1] << RrDefaultGreenOffset) + \
389      (color##x[2] << RrDefaultBlueOffset))
390
391 #define INCREMENT(x, i) \
392     (inc##x[i])
393
394 #define NEXT(x)                                           \
395 {                                                         \
396     gint i;                                                \
397     for (i = 2; i >= 0; --i) {                            \
398         if (!cdelta##x[i]) continue;                      \
399                                                           \
400         if (!bigslope##x[i]) {                            \
401             /* Y (color) is dependant on X */             \
402             error##x[i] += cdelta##x[i];                  \
403             if ((error##x[i] << 1) >= len##x) {           \
404                 color##x[i] += INCREMENT(x, i);           \
405                 error##x[i] -= len##x;                    \
406             }                                             \
407         } else {                                          \
408             /* X is dependant on Y (color) */             \
409             while (1) {                                   \
410                 color##x[i] += INCREMENT(x, i);           \
411                 error##x[i] += len##x;                    \
412                 if ((error##x[i] << 1) >= cdelta##x[i]) { \
413                     error##x[i] -= cdelta##x[i];          \
414                     break;                                \
415                 }                                         \
416             }                                             \
417         }                                                 \
418     }                                                     \
419 }
420
421 static void gradient_splitvertical(RrAppearance *a, gint w, gint h)
422 {
423     gint x, y1, y2, y3, r, g, b;
424     RrSurface *sf = &a->surface;
425     RrPixel32 *data = sf->pixel_data;
426     RrPixel32 current;
427     RrColor *primary_light, *secondary_light;
428     gint y1sz, y2sz, y3sz;
429
430     VARS(y1);
431     VARS(y2);
432     VARS(y3);
433
434     r = sf->primary->r;
435     r += r >> 2;
436     g = sf->primary->g;
437     g += g >> 2;
438     b = sf->primary->b;
439     b += b >> 2;
440     if (r > 0xFF) r = 0xFF;
441     if (g > 0xFF) g = 0xFF;
442     if (b > 0xFF) b = 0xFF;
443       primary_light = RrColorNew(a->inst, r, g, b);
444
445     r = sf->secondary->r;
446     r += r >> 4;
447     g = sf->secondary->g;
448     g += g >> 4;
449     b = sf->secondary->b;
450     b += b >> 4;
451     if (r > 0xFF) r = 0xFF;
452     if (g > 0xFF) g = 0xFF;
453     if (b > 0xFF) b = 0xFF;
454     secondary_light = RrColorNew(a->inst, r, g, b);
455
456     y1sz = MAX(h/2 - 1, 1);
457     /* setup to get the colors _in between_ these other 2 */
458     y2sz = (h < 3 ? 0 : (h % 2 ? 3 : 2));
459     y3sz = MAX(h/2 - 1, 0);
460
461     SETUP(y1, primary_light, sf->primary, y1sz);
462     if (y2sz) {
463         SETUP(y2, sf->primary, sf->secondary, y2sz);
464         NEXT(y2); /* skip the first one, its the same as the last of y1 */
465     }
466     SETUP(y3, sf->secondary, secondary_light,  y3sz);
467
468     for (y1 = y1sz; y1 > 0; --y1) {
469         current = COLOR(y1);
470         for (x = w - 1; x >= 0; --x)
471             *(data++) = current;
472
473         NEXT(y1);
474     }
475
476     for (y2 = y2sz; y2 > 0; --y2) {
477         current = COLOR(y2);
478         for (x = w - 1; x >= 0; --x)
479             *(data++) = current;
480         
481         NEXT(y2);
482     }
483     
484     for (y3 = y3sz; y3 > 0; --y3) {
485         current = COLOR(y3);
486         for (x = w - 1; x >= 0; --x)
487             *(data++) = current;
488
489         NEXT(y3);
490     }
491
492     RrColorFree(primary_light);
493     RrColorFree(secondary_light);
494 }
495
496 static void gradient_horizontal(RrSurface *sf, gint w, gint h)
497 {
498     gint x, y;
499     RrPixel32 *data = sf->pixel_data, *datav;
500     RrPixel32 current;
501
502     VARS(x);
503     SETUP(x, sf->primary, sf->secondary, w);
504
505     for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
506         current = COLOR(x);
507         datav = data;
508         for (y = h - 1; y >= 0; --y) {  /* 0 -> h */
509             *datav = current;
510             datav += w;
511         }
512         ++data;
513
514         NEXT(x);
515     }
516     current = COLOR(x);
517     for (y = h - 1; y >= 0; --y)  /* 0 -> h */
518         *(data + y * w) = current;
519 }
520
521 static void gradient_mirrorhorizontal(RrSurface *sf, gint w, gint h)
522 {
523     gint x, y;
524     RrPixel32 *data = sf->pixel_data, *datav;
525     RrPixel32 current;
526
527     VARS(x);
528     SETUP(x, sf->primary, sf->secondary, w/2);
529
530     if (w > 1) {
531         for (x = w - 1; x > w/2-1; --x) {  /* 0 -> w-1 */
532             current = COLOR(x);
533             datav = data;
534             for (y = h - 1; y >= 0; --y) {  /* 0 -> h */
535                 *datav = current;
536                 datav += w;
537             }
538             ++data;
539
540             NEXT(x);
541         }
542         SETUP(x, sf->secondary, sf->primary, w/2);
543         for (x = w/2 - 1; x > 0; --x) {  /* 0 -> w-1 */
544             current = COLOR(x);
545             datav = data;
546             for (y = h - 1; y >= 0; --y) {  /* 0 -> h */
547                 *datav = current;
548                 datav += w;
549             }
550             ++data;
551
552             NEXT(x);
553         }
554     }
555     current = COLOR(x);
556     for (y = h - 1; y >= 0; --y)  /* 0 -> h */
557         *(data + y * w) = current;
558 }
559
560 static void gradient_vertical(RrSurface *sf, gint w, gint h)
561 {
562     gint x, y;
563     RrPixel32 *data = sf->pixel_data;
564     RrPixel32 current;
565
566     VARS(y);
567     SETUP(y, sf->primary, sf->secondary, h);
568
569     for (y = h - 1; y > 0; --y) {  /* 0 -> h-1 */
570         current = COLOR(y);
571         for (x = w - 1; x >= 0; --x)  /* 0 -> w */
572             *(data++) = current;
573
574         NEXT(y);
575     }
576     current = COLOR(y);
577     for (x = w - 1; x >= 0; --x)  /* 0 -> w */
578         *(data++) = current;
579 }
580
581
582 static void gradient_diagonal(RrSurface *sf, gint w, gint h)
583 {
584     gint x, y;
585     RrPixel32 *data = sf->pixel_data;
586     RrColor left, right;
587     RrColor extracorner;
588
589     VARS(lefty);
590     VARS(righty);
591     VARS(x);
592
593     extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
594     extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
595     extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
596
597     SETUP(lefty, sf->primary, (&extracorner), h);
598     SETUP(righty, (&extracorner), sf->secondary, h);
599
600     for (y = h - 1; y > 0; --y) {  /* 0 -> h-1 */
601         COLOR_RR(lefty, (&left));
602         COLOR_RR(righty, (&right));
603
604         SETUP(x, (&left), (&right), w);
605
606         for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
607             *(data++) = COLOR(x);
608
609             NEXT(x);
610         }
611         *(data++) = COLOR(x);
612
613         NEXT(lefty);
614         NEXT(righty);
615     }
616     COLOR_RR(lefty, (&left));
617     COLOR_RR(righty, (&right));
618
619     SETUP(x, (&left), (&right), w);
620
621     for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
622         *(data++) = COLOR(x);
623         
624         NEXT(x);
625     }
626     *data = COLOR(x);
627 }
628
629 static void gradient_crossdiagonal(RrSurface *sf, gint w, gint h)
630 {
631     gint x, y;
632     RrPixel32 *data = sf->pixel_data;
633     RrColor left, right;
634     RrColor extracorner;
635
636     VARS(lefty);
637     VARS(righty);
638     VARS(x);
639
640     extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
641     extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
642     extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
643
644     SETUP(lefty, (&extracorner), sf->secondary, h);
645     SETUP(righty, sf->primary, (&extracorner), h);
646
647     for (y = h - 1; y > 0; --y) {  /* 0 -> h-1 */
648         COLOR_RR(lefty, (&left));
649         COLOR_RR(righty, (&right));
650
651         SETUP(x, (&left), (&right), w);
652
653         for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
654             *(data++) = COLOR(x);
655
656             NEXT(x);
657         }
658         *(data++) = COLOR(x);
659
660         NEXT(lefty);
661         NEXT(righty);
662     }
663     COLOR_RR(lefty, (&left));
664     COLOR_RR(righty, (&right));
665
666     SETUP(x, (&left), (&right), w);
667
668     for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
669         *(data++) = COLOR(x);
670         
671         NEXT(x);
672     }
673     *data = COLOR(x);
674 }
675
676 static void gradient_pyramid(RrSurface *sf, gint inw, gint inh)
677 {
678     gint x, y, w = (inw >> 1) + 1, h = (inh >> 1) + 1;
679     RrPixel32 *data = sf->pixel_data;
680     RrPixel32 *end = data + inw*inh - 1;
681     RrPixel32 current;
682     RrColor left, right;
683     RrColor extracorner;
684
685     VARS(lefty);
686     VARS(righty);
687     VARS(x);
688
689     extracorner.r = (sf->primary->r + sf->secondary->r) / 2;
690     extracorner.g = (sf->primary->g + sf->secondary->g) / 2;
691     extracorner.b = (sf->primary->b + sf->secondary->b) / 2;
692
693     SETUP(lefty, (&extracorner), sf->secondary, h);
694     SETUP(righty, sf->primary, (&extracorner), h);
695
696     for (y = h - 1; y > 0; --y) {  /* 0 -> h-1 */
697         COLOR_RR(lefty, (&left));
698         COLOR_RR(righty, (&right));
699
700         SETUP(x, (&left), (&right), w);
701
702         for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
703             current = COLOR(x);
704             *(data+x) = current;
705             *(data+inw-x) = current;
706             *(end-x) = current;
707             *(end-(inw-x)) = current;
708
709             NEXT(x);
710         }
711         current = COLOR(x);
712         *(data+x) = current;
713         *(data+inw-x) = current;
714         *(end-x) = current;
715         *(end-(inw-x)) = current;
716
717         data+=inw;
718         end-=inw;
719
720         NEXT(lefty);
721         NEXT(righty);
722     }
723     COLOR_RR(lefty, (&left));
724     COLOR_RR(righty, (&right));
725
726     SETUP(x, (&left), (&right), w);
727
728     for (x = w - 1; x > 0; --x) {  /* 0 -> w-1 */
729         current = COLOR(x);
730         *(data+x) = current;
731         *(data+inw-x) = current;
732         *(end-x) = current;
733         *(end-(inw-x)) = current;
734         
735         NEXT(x);
736     }
737     current = COLOR(x);
738     *(data+x) = current;
739     *(data+inw-x) = current;
740     *(end-x) = current;
741     *(end-(inw-x)) = current;
742 }
743