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