+ {
+ int i;
+ unsigned long fg_pixel =
+ get_pixel_resource (st->dpy, st->xgwa.colormap,
+ "foreground", "Foreground");
+ unsigned long bg_pixel =
+ get_pixel_resource (st->dpy, st->xgwa.colormap,
+ "background", "Background");
+ if (!st->colors)
+ {
+ st->ncolors = 50;
+ st->colors = (XColor *) calloc (st->ncolors, sizeof(*st->colors));
+ }
+ st->colors[0].pixel = fg_pixel;
+ for (i = 1; i < st->ncolors; i++)
+ st->colors[i].pixel = bg_pixel;
+ }
+
+ XSetForeground (st->dpy, st->gc, st->colors[1].pixel);
+ XFillRectangle (st->dpy, st->window, st->gc, 0, 0,
+ st->xgwa.width, st->xgwa.height);
+
+ if (st->flip_xy)
+ {
+ st->xmax = st->xgwa.height;
+ st->ymax = st->xgwa.width;
+ }
+ else
+ {
+ st->xmax = st->xgwa.width;
+ st->ymax = st->xgwa.height;
+ }
+
+ if (st->cell) free (st->cell);
+ st->cell = (signed char *) calloc (st->xmax * st->ymax, 1);
+
+ CELL (0, 0) = 0;
+ st->xstep = COUNT;
+ st->ystep = COUNT;
+
+ st->iteration = 0;
+ st->cx = 0;
+}
+
+
+static void *
+imsmap_init (Display *dpy, Window window)
+{
+ struct state *st = (struct state *) calloc (1, sizeof(*st));
+ st->dpy = dpy;
+ st->window = window;
+ init_map (st);
+ return st;
+}
+
+
+static unsigned long
+imsmap_draw (Display *dpy, Window window, void *closure)
+{
+ struct state *st = (struct state *) closure;
+ int this_delay = st->delay2;
+ int i;
+
+ /* do this many lines at a time without pausing */
+ int col_chunk = st->iteration * 2 + 1;
+
+ if (st->iteration > st->iterations)
+ init_map (st);
+
+ if (st->cx == 0)
+ {
+ st->xnextStep = st->xstep / 2;
+ st->ynextStep = st->ystep / 2;
+ }
+
+ for (i = 0; i < col_chunk; i++)
+ {
+ int x1, x2, y1, y2;
+ int y;
+ int x = st->cx;
+
+ x1 = x + st->xnextStep;
+ if (x1 < 0)
+ x1 = st->xmax-1;
+ else if (x1 >= st->xmax)
+ x1 = 0;
+
+ x2 = x + st->xstep;
+ if (x2 < 0)
+ x2 = st->xmax-1;
+ else if (x2 >= st->xmax)
+ x2 = 0;
+
+ for (y = 0; y < st->ymax; y += st->ystep)
+ {
+ unsigned int pixel, qpixels [4];
+
+ y1 = y + st->ynextStep;
+ if (y1 < 0)
+ y1 = st->ymax-1;
+ else if (y1 >= st->ymax)
+ y1 = 0;
+
+ y2 = y + st->ystep;
+ if (y2 < 0)
+ y2 = st->ymax-1;
+ else if (y2 >= st->ymax)
+ y2 = 0;
+
+ qpixels [0] = st->colors [HEIGHT_TO_PIXEL (CELL (x, y))].pixel;
+ qpixels [1] = st->colors [HEIGHT_TO_PIXEL (CELL (x, y2))].pixel;
+ qpixels [2] = st->colors [HEIGHT_TO_PIXEL (CELL (x2, y))].pixel;
+ qpixels [3] = st->colors [HEIGHT_TO_PIXEL (CELL (x2, y2))].pixel;
+
+ pixel = set (st, x, y1, st->iteration,
+ ((int) CELL (x, y) + (int) CELL (x, y2) + 1) / 2);
+
+ if (! mono_p &&
+ (pixel != qpixels[0] || pixel != qpixels[1] ||
+ pixel != qpixels[2] || pixel != qpixels[3]))
+ draw (st, x, y1, pixel, st->ynextStep);
+
+ pixel = set (st, x1, y, st->iteration,
+ ((int) CELL (x, y) + (int) CELL (x2, y) + 1) / 2);
+ if (! mono_p &&
+ (pixel != qpixels[0] || pixel != qpixels[1] ||
+ pixel != qpixels[2] || pixel != qpixels[3]))
+ draw (st, x1, y, pixel, st->ynextStep);
+
+ pixel = set (st, x1, y1, st->iteration,
+ ((int) CELL (x, y) + (int) CELL (x, y2) +
+ (int) CELL (x2, y) + (int) CELL (x2, y2) + 2)
+ / 4);
+ if (! mono_p &&
+ (pixel != qpixels[0] || pixel != qpixels[1] ||
+ pixel != qpixels[2] || pixel != qpixels[3]))
+ draw (st, x1, y1, pixel, st->ynextStep);
+ }
+
+ st->cx += st->xstep;
+ if (st->cx >= st->xmax)
+ break;
+ }
+
+ if (st->cx >= st->xmax)
+ {
+ st->cx = 0;
+ st->xstep = st->xnextStep;
+ st->ystep = st->ynextStep;
+
+ st->iteration++;
+
+ if (st->iteration > st->iterations)
+ this_delay = st->delay * 1000000;
+
+ if (mono_p)
+ floyd_steinberg (st); /* in mono, do all drawing at the end */
+ }
+
+ return this_delay;
+}
+
+
+static void
+imsmap_reshape (Display *dpy, Window window, void *closure,
+ unsigned int w, unsigned int h)
+{
+ struct state *st = (struct state *) closure;
+ init_map (st);