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