+/* * * * * * * * * * * * * * GRADIENT MAGIC WOOT * * * * * * * * * * * * * * */
+
+#define VARS(x) \
+ register gint len##x; \
+ guint color##x[3]; \
+ gint cdelta##x[3], error##x[3] = { 0, 0, 0 }, inc##x[3]; \
+ gboolean bigslope##x[3] /* color slope > 1 */
+
+#define SETUP(x, from, to, w) \
+ len##x = w; \
+ \
+ color##x[0] = from->r; \
+ color##x[1] = from->g; \
+ color##x[2] = from->b; \
+ \
+ cdelta##x[0] = to->r - from->r; \
+ cdelta##x[1] = to->g - from->g; \
+ cdelta##x[2] = to->b - from->b; \
+ \
+ if (cdelta##x[0] < 0) { \
+ cdelta##x[0] = -cdelta##x[0]; \
+ inc##x[0] = -1; \
+ } else \
+ inc##x[0] = 1; \
+ if (cdelta##x[1] < 0) { \
+ cdelta##x[1] = -cdelta##x[1]; \
+ inc##x[1] = -1; \
+ } else \
+ inc##x[1] = 1; \
+ if (cdelta##x[2] < 0) { \
+ cdelta##x[2] = -cdelta##x[2]; \
+ inc##x[2] = -1; \
+ } else \
+ inc##x[2] = 1; \
+ bigslope##x[0] = cdelta##x[0] > w;\
+ bigslope##x[1] = cdelta##x[1] > w;\
+ bigslope##x[2] = cdelta##x[2] > w
+
+#define COLOR_RR(x, c) \
+ c->r = color##x[0]; \
+ c->g = color##x[1]; \
+ c->b = color##x[2]
+
+#define COLOR(x) \
+ ((color##x[0] << RrDefaultRedOffset) + \
+ (color##x[1] << RrDefaultGreenOffset) + \
+ (color##x[2] << RrDefaultBlueOffset))
+
+#define INCREMENT(x, i) \
+ (inc##x[i])
+
+#define NEXT(x) \
+{ \
+ register gint i; \
+ for (i = 2; i >= 0; --i) { \
+ if (!cdelta##x[i]) continue; \
+ \
+ if (!bigslope##x[i]) { \
+ /* Y (color) is dependant on X */ \
+ error##x[i] += cdelta##x[i]; \
+ if ((error##x[i] << 1) >= len##x) { \
+ color##x[i] += INCREMENT(x, i); \
+ error##x[i] -= len##x; \
+ } \
+ } else { \
+ /* X is dependant on Y (color) */ \
+ while (1) { \
+ color##x[i] += INCREMENT(x, i); \
+ error##x[i] += len##x; \
+ if ((error##x[i] << 1) >= cdelta##x[i]) { \
+ error##x[i] -= cdelta##x[i]; \
+ break; \
+ } \
+ } \
+ } \
+ } \
+}
+
+static void gradient_splitvertical(RrAppearance *a, gint w, gint h)
+{
+ register gint y1, y2, y3;
+ RrSurface *sf = &a->surface;
+ RrPixel32 *data;
+ register gint y1sz, y2sz, y3sz;
+
+ VARS(y1);
+ VARS(y2);
+ VARS(y3);
+
+ /* if h <= 5, then a 0 or 1px middle gradient.
+ if h > 5, then always a 1px middle gradient.
+ */
+ if (h <= 5) {
+ y1sz = MAX(h/2, 0);
+ y2sz = (h < 3 ? 0 : h % 2);
+ y3sz = MAX(h/2, 1);
+ }
+ else {
+ y1sz = h/2 - (1 - (h % 2));
+ y2sz = 1;
+ y3sz = h/2;
+ }
+
+ SETUP(y1, sf->split_primary, sf->primary, y1sz);
+ if (y2sz) {
+ /* setup to get the colors _in between_ these other 2 */
+ SETUP(y2, sf->primary, sf->secondary, y2sz + 2);
+ NEXT(y2); /* skip the first one, its the same as the last of y1 */
+ }
+ SETUP(y3, sf->secondary, sf->split_secondary, y3sz);
+
+ /* find the color for the first pixel of each row first */
+ data = sf->pixel_data;
+
+ for (y1 = y1sz-1; y1 > 0; --y1) {
+ *data = COLOR(y1);
+ data += w;
+ NEXT(y1);
+ }
+ *data = COLOR(y1);
+ data += w;
+ for (y2 = y2sz-1; y2 > 0; --y2) {
+ *data = COLOR(y2);
+ data += w;
+ NEXT(y2);
+ }
+ *data = COLOR(y2);
+ data += w;
+ for (y3 = y3sz-1; y3 > 0; --y3) {
+ *data = COLOR(y3);
+ data += w;
+ NEXT(y3);
+ }
+ *data = COLOR(y3);
+
+ /* copy the first pixels into the whole rows */
+ data = sf->pixel_data;
+ for (y1 = h; y1 > 0; --y1) {
+ repeat_pixel(data, w);
+ data += w;
+ }
+}
+
+static void gradient_horizontal(RrSurface *sf, gint w, gint h)
+{
+ register gint x, y, cpbytes;
+ RrPixel32 *data = sf->pixel_data, *datav;
+ gchar *datac;
+
+ VARS(x);
+ SETUP(x, sf->primary, sf->secondary, w);
+
+ /* set the color values for the first row */
+ datav = data;
+ for (x = w - 1; x > 0; --x) { /* 0 -> w - 1 */
+ *datav = COLOR(x);
+ ++datav;
+ NEXT(x);
+ }
+ *datav = COLOR(x);
+ ++datav;
+
+ /* copy the first row to the rest in O(logn) copies */
+ datac = (gchar*)datav;
+ cpbytes = 1 * w * sizeof(RrPixel32);
+ for (y = (h - 1) * w * sizeof(RrPixel32); y > 0;) {
+ memcpy(datac, data, cpbytes);
+ y -= cpbytes;
+ datac += cpbytes;
+ cpbytes <<= 1;
+ if (cpbytes > y)
+ cpbytes = y;
+ }
+}
+
+static void gradient_mirrorhorizontal(RrSurface *sf, gint w, gint h)