File: fillOp.c

package info (click to toggle)
xpaint 2.5.1-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,488 kB
  • ctags: 2,478
  • sloc: ansic: 25,980; makefile: 36; sh: 23
file content (752 lines) | stat: -rw-r--r-- 19,164 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/* +-------------------------------------------------------------------+ */
/* | Copyright 1992, 1993, David Koblas (koblas@netcom.com)            | */
/* | Copyright 1995, 1996 Torsten Martinsen (bullestock@dk-online.dk)  | */
/* |                                                                   | */
/* | Permission to use, copy, modify, and to distribute this software  | */
/* | and its documentation for any purpose is hereby granted without   | */
/* | fee, provided that the above copyright notice appear in all       | */
/* | copies and that both that copyright notice and this permission    | */
/* | notice appear in supporting documentation.  There is no           | */
/* | representations about the suitability of this software for        | */
/* | any purpose.  this software is provided "as is" without express   | */
/* | or implied warranty.                                              | */
/* |                                                                   | */
/* +-------------------------------------------------------------------+ */

/* $Id: fillOp.c,v 1.9 1996/04/19 07:58:52 torsten Exp $ */

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/cursorfont.h>
#ifndef NOSTDHDRS
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#endif
#include "Paint.h"
#include "protocol.h"
#include "xpaint.h"
#include "ops.h"
#include "image.h"
#include "misc.h"
#include "palette.h"
#include "color.h"

typedef struct {
    Boolean done;
    int type;			/* 0 = plain fill, 1 = gradient fill, 2 = fractal fill */
} LocalInfo;

#define ZINDEX8(x, y, img)  ((y) * img->bytes_per_line) + (x)

#define CLAMP(low, value, high) \
	if (value < low) value = low; else if (value > high) value = high
#define DEG2RAD	(M_PI/180.0)

static Pixmap buildGradientPixmap(Widget w, OpInfo * info,
			     int width, int height, int depth, int mode);
#if FEATURE_FRACTAL
static Pixmap buildFractalPixmap(Widget w, OpInfo * info,
			     int width, int height, int depth, int mode);
#endif

static Drawable drawable;
static Display *dpy;
static XImage *source;
static Boolean *mask;
static int maskW, maskH;
static int xMin, yMin, xMax, yMax;
static int timeCount = 0;
static int fillMode = 0, tfillMode = TFILL_RADIAL;
#if FEATURE_FRACTAL
static int ffillMode = 0;
#endif
static int deltaR, deltaG, deltaB;
static int deltaRV, deltaGV, deltaBV;
static Palette *deltaPal;

/*
 * A Seed Fill Algorithm
 * by Paul Heckbert
 * from "Graphics Gems", Academic Press, 1990
 */

/*
 * fill.c : simple seed fill program
 * Calls pixelread() to read pixels, pixelwrite() to write pixels.
 *
 * Paul Heckbert        13 Sept 1982, 28 Jan 1987
 */

#define pixelwrite(x,y)	do {	\
  mask[y * maskW + x] = True;	\
  xMin = MIN(xMin, x);		\
  yMin = MIN(yMin, y);		\
  xMax = MAX(xMax, x);		\
  yMax = MAX(yMax, y);		\
} while (0)

#define STEP	32

static Pixel
pixelread(int x, int y, Boolean first)
{
    static Pixel ov;
    static int xMin, yMin, xMax, yMax;
    Pixel p;
    int n;

    if (first) {
	xMin = MAX(x - STEP, 0);
	yMin = MAX(y - STEP, 0);
	xMax = MIN(x + STEP, maskW);
	yMax = MIN(y + STEP, maskH);

	XGetSubImage(dpy, drawable, xMin, yMin,
		     xMax - xMin, yMax - yMin,
		     AllPlanes, ZPixmap, source,
		     xMin, yMin);

	ov = XGetPixel(source, x, y);

	return ov;
    } else if (mask[y * maskW + x]) {
	return (Pixel) - 1;
    }
    if (x < xMin) {
	n = MAX(x - STEP, 0);
	XGetSubImage(dpy, drawable, n, yMin,
		     xMin - n, yMax - yMin,
		     AllPlanes, ZPixmap, source,
		     n, yMin);
	xMin = n;
    }
    if (y < yMin) {
	n = MAX(y - STEP, 0);
	XGetSubImage(dpy, drawable, xMin, n,
		     xMax - xMin, yMin - n,
		     AllPlanes, ZPixmap, source,
		     xMin, n);
	yMin = n;
    }
    if (x >= xMax) {
	n = MIN(x + STEP, maskW);
	XGetSubImage(dpy, drawable, xMax, yMin,
		     n - xMax, yMax - yMin,
		     AllPlanes, ZPixmap, source,
		     xMax, yMin);
	xMax = n;
    }
    if (y >= yMax) {
	n = MIN(y + STEP, maskH);
	XGetSubImage(dpy, drawable, xMin, yMax,
		     xMax - xMin, n - yMax,
		     AllPlanes, ZPixmap, source,
		     xMin, yMax);
	yMax = n;
    }
    /*
    **  Do it fast for those 8 bit displays...
     */
    if (source->bits_per_pixel == 8)
	/*
	 * This is disgusting. 'data' is declared as _signed_ char,
	 * so values above 127 become negative if you're not careful.
	 */
	p = (unsigned char) source->data[ZINDEX8(x, y, source)];
    else
	p = XGetPixel(source, x, y);

    return p;
}

/*
**  Exchange pixel 1 for filled pixel value 2
 */
static void 
change(int sx, int sy, int width, int height)
{
    int x, y;
    Pixel pix = XGetPixel(source, sx, sy);

    if (source->bits_per_pixel == 8) {
	for (y = 0; y < height; y++) {
	    for (x = 0; x < width; x++)
		if (((unsigned char) source->data[ZINDEX8(x, y, source)]) == pix)
		    pixelwrite(x, y);
	    if (y % 100 == 0)
		StateTimeStep();
	}
    } else {
	for (y = 0; y < height; y++) {
	    for (x = 0; x < width; x++)
		if (XGetPixel(source, x, y) == pix)
		    pixelwrite(x, y);
	    if (y % 64 == 0)
		StateTimeStep();
	}
    }
}

typedef struct {
    short y, xl, xr, dy;
} Segment;

/*
 * Filled horizontal segment of scanline y for xl<=x<=xr.
 * Parent segment was on line y-dy.  dy=1 or -1
 */

#define STACKSIZE 20000		/* max depth of stack */

#define PUSH(Y, XL, XR, DY)  /* push new segment on stack */ \
    if (sp<stack+STACKSIZE && Y+(DY)>=0 && Y+(DY)<=height) \
    {sp->y = Y; sp->xl = XL; sp->xr = XR; sp->dy = DY; sp++;}

#define POP(Y, XL, XR, DY)    /* pop segment off stack */ \
    {sp--; Y = sp->y+(DY = sp->dy); XL = sp->xl; XR = sp->xr;}

/*
 * fill: set the pixel at (x,y) and all of its 4-connected neighbors
 * with the same pixel value to the new pixel value nv.
 * A 4-connected neighbor is a pixel above, below, left, or right of a pixel.
 */

static void 
fill(int x, int y, int width, int height)
{
    int start, x1, x2, dy = 0;
    Pixel ov;			/* old pixel value */
    Segment stack[STACKSIZE], *sp = stack;	/* stack of filled segments */

    ov = pixelread(x, y, True);	/* read pv at seed point */
    PUSH(y, x, x, 1);		/* needed in some cases */
    PUSH(y + 1, x, x, -1);	/* seed segment (popped 1st) */

    while (sp > stack) {
	/* pop segment off stack and fill a neighboring scan line */
	POP(y, x1, x2, dy);
	/*
	 * segment of scan line y-dy for x1<=x<=x2 was previously filled,
	 */
	for (x = x1; x >= 0 && pixelread(x, y, False) == ov; x--)
	    pixelwrite(x, y);
	if (x >= x1) {
	    for (x++; x <= x2 && pixelread(x, y, False) != ov; x++);
	    start = x;
	    if (x > x2)
		continue;
	} else {
	    start = x + 1;
	    if (start < x1)
		PUSH(y, start, x1 - 1, -dy);	/* leak on left? */
	    x = x1 + 1;
	}
	do {
	    for (; x <= width && pixelread(x, y, False) == ov; x++)
		pixelwrite(x, y);
	    PUSH(y, start, x - 1, dy);
	    if (x > x2 + 1)
		PUSH(y, x2 + 1, x - 1, -dy);	/* leak on right? */
	    for (x++; x <= x2 && pixelread(x, y, False) != ov; x++);
	    start = x;
	}
	while (x <= x2);

	if (++timeCount % 32 == 0)
	    StateTimeStep();
    }
}

static int 
similar(Pixel p)
{
    XColor *col;
    int r, g, b;

    if (p == ((Pixel) - 1))
	return 0;
    col = PaletteLookup(deltaPal, p);
    r = col->red / 256;
    g = col->green / 256;
    b = col->blue / 256;

    return (deltaR - deltaRV <= r) && (r <= deltaR + deltaRV)
	&& (deltaG - deltaGV <= g) && (g <= deltaG + deltaGV)
	&& (deltaB - deltaBV <= b) && (b <= deltaB + deltaBV);
}

/*
 * deltafill: set the pixel at (x,y) and all of its 4-connected neighbors
 * with similar pixel values to the new pixel value nv.
 * A 4-connected neighbor is a pixel above, below, left, or right of a pixel.
 */

static void 
deltafill(int x, int y, int width, int height)
{
    int start, x1, x2, dy = 0;
    Pixel ov;			/* old pixel value */
    Segment stack[STACKSIZE], *sp = stack;	/* stack of filled segments */
    XColor *col;

    ov = pixelread(x, y, True);	/* read pv at seed point */
    col = PaletteLookup(deltaPal, ov);
    deltaR = col->red / 256;
    deltaG = col->green / 256;
    deltaB = col->blue / 256;
    GetChromaDelta(&deltaRV, &deltaGV, &deltaBV);
    PUSH(y, x, x, 1);		/* needed in some cases */
    PUSH(y + 1, x, x, -1);	/* seed segment (popped 1st) */

    while (sp > stack) {
	/* pop segment off stack and fill a neighboring scan line */
	POP(y, x1, x2, dy);
	/*
	 * segment of scan line y-dy for x1<=x<=x2 was previously filled,
	 */
	for (x = x1; x >= 0 && similar(pixelread(x, y, False)); x--)
	    pixelwrite(x, y);
	if (x >= x1) {
	    for (x++; x <= x2 && !similar(pixelread(x, y, False)); x++);
	    start = x;
	    if (x > x2)
		continue;
	} else {
	    start = x + 1;
	    if (start < x1)
		PUSH(y, start, x1 - 1, -dy);	/* leak on left? */
	    x = x1 + 1;
	}
	do {
	    for (; x <= width && similar(pixelread(x, y, False)); x++)
		pixelwrite(x, y);
	    PUSH(y, start, x - 1, dy);
	    if (x > x2 + 1)
		PUSH(y, x2 + 1, x - 1, -dy);	/* leak on right? */
	    for (x++; x <= x2 && !similar(pixelread(x, y, False)); x++);
	    start = x;
	}
	while (x <= x2);

	if (++timeCount % 32 == 0)
	    StateTimeStep();
    }
}

static void 
press(Widget w, LocalInfo * l, XButtonEvent * event, OpInfo * info)
{
    XRectangle undo, rect;
    int width, height;
    GC dgc;
    int isBusy, depth;
    Pixmap pix, gradient;
    Colormap cmap;


    if (event->button == Button1)
	dgc = info->first_gc;
    else if (event->button == Button2)
	dgc = info->second_gc;
    else
	return;

    dpy = XtDisplay(w);
    drawable = info->drawable;

    UndoStart(w, info);

    XtVaGetValues(w, XtNdrawWidth, &width,
		  XtNdrawHeight, &height,
		  XtNdepth, &depth,
		  NULL);
    if ((isBusy = (width * height > 6400)) != 0)
	StateSetBusy(True);

    if ((mask = (Boolean *) XtCalloc(sizeof(Boolean), width * height)) == NULL)
	return;
    maskW = width;
    maskH = height;

    memset(mask, False, width * height * sizeof(Boolean));

    if (fillMode != 1) {
	source = NewXImage(dpy, NULL, depth, width, height);
    } else {
	source = PwGetImage(w, NULL);
    }

    xMin = xMax = info->realX;
    yMin = yMax = info->realY;

    switch (fillMode) {
    case 0:
	fill(info->realX, info->realY, width - 1, height - 1);
	break;
    case 1:
	change(info->realX, info->realY, width, height);
	break;
    default:			/* 2 */
	XtVaGetValues(w, XtNcolormap, &cmap, NULL);
	deltaPal = PaletteFind(w, cmap);
	deltafill(info->realX, info->realY, width - 1, height - 1);
	break;
    }

    rect.x = xMin;
    rect.y = yMin;
    rect.width = (xMax - xMin) + 1;
    rect.height = (yMax - yMin) + 1;

    pix = MaskDataToPixmap(w, width, height, mask, &rect);

    XSetClipOrigin(dpy, dgc, rect.x, rect.y);
    XSetClipMask(dpy, dgc, pix);

    switch (l->type) {
    case 0:
	XFillRectangles(dpy, info->drawable, dgc, &rect, 1);
	if (!info->isFat)
	    XFillRectangles(dpy, XtWindow(w), dgc, &rect, 1);
	break;

    case 1:
    case 2:
	/*
	 * Gradient fill: Build a pixmap of the required size and
	 * copy that to the region to be filled
	 */
	if (l->type == 1)
	    gradient = buildGradientPixmap(w, info, rect.width, rect.height,
					   depth, tfillMode);
#ifdef FEATURE_FRACTAL
	else
	    gradient = buildFractalPixmap(w, info, rect.width, rect.height,
					  depth, ffillMode);
#endif
	XCopyArea(dpy, gradient, info->drawable, dgc,
		  0, 0, rect.width, rect.height, xMin, yMin);
	if (!info->isFat)
	    XCopyArea(dpy, gradient, XtWindow(w), dgc,
		      0, 0, rect.width, rect.height, xMin, yMin);
	XFreePixmap(dpy, gradient);
	break;
    }


    XSetClipMask(dpy, dgc, None);
    XFreePixmap(dpy, pix);

    XYtoRECT(xMin, yMin, xMax + 1, yMax + 1, &undo);
    UndoSetRectangle(w, &undo);
    PwUpdate(w, &undo, False);

    if (fillMode != 1)
	XDestroyImage(source);

    if (isBusy)
	StateSetBusy(False);
}

void *
FillAdd(Widget w)
{
    LocalInfo *l = (LocalInfo *) XtMalloc(sizeof(LocalInfo));
    l->done = False;
    l->type = 0;
    OpAddEventHandler(w, opPixmap, ButtonPressMask, FALSE,
		      (OpEventProc) press, l);
    SetCrossHairCursor(w);
    return l;
}

void 
FillRemove(Widget w, void *l)
{
    OpRemoveEventHandler(w, opPixmap, ButtonPressMask, FALSE,
			 (OpEventProc) press, l);
    XtFree((XtPointer) l);
}

void 
FillSetMode(int value)
{
    fillMode = value;
}

/*
 * Gradient fill
 */
static float TFillQuant = 50.0;
static double TFillVoffset = 0.0;
static double TFillHoffset = 0.0;
static double TFillPad = 0.0;
static int TFillTilt = 0;

void *
TFillAdd(Widget w)
{
    LocalInfo *l = (LocalInfo *) XtMalloc(sizeof(LocalInfo));
    l->done = False;
    l->type = 1;
    OpAddEventHandler(w, opPixmap, ButtonPressMask, FALSE,
		      (OpEventProc) press, l);
    SetCrossHairCursor(w);
    return l;
}

void 
TFillRemove(Widget w, void *l)
{
    OpRemoveEventHandler(w, opPixmap, ButtonPressMask, FALSE,
			 (OpEventProc) press, l);
    XtFree((XtPointer) l);
}

void 
TFillSetMode(int value)
{
    tfillMode = value;
}

/*
 * voffset, hoffset, pad range from -1.0 to 1.0
 * angle is in degrees
 */
void 
TfillSetParameters(float voffset, float hoffset, float pad, int angle, int steps)
{
    TFillVoffset = voffset;
    TFillHoffset = hoffset;
    TFillPad = pad;
    TFillTilt = angle;
    TFillQuant = steps;
}

/*
 * Fractal fills
 */
#ifdef FEATURE_FRACTAL
void *
FFillAdd(Widget w)
{
    LocalInfo *l = (LocalInfo *) XtMalloc(sizeof(LocalInfo));
    l->done = False;
    l->type = 2;
    OpAddEventHandler(w, opPixmap, ButtonPressMask, FALSE,
		      (OpEventProc) press, l);
    SetCrossHairCursor(w);
    return l;
}

void 
FFillRemove(Widget w, void *l)
{
    OpRemoveEventHandler(w, opPixmap, ButtonPressMask, FALSE,
			 (OpEventProc) press, l);
    XtFree((XtPointer) l);
}

void 
FFillSetMode(int value)
{
    ffillMode = value;
}

#endif

#define SWAP(a, b)	{ int t = (a); (a) = (b); (b) = t; }
#define	SWAP_COLORS	{ SWAP(rp, rs); SWAP(gp, gs); SWAP(bp, bs); }

/*
 * Builds gradient pixmaps.
 */
static Pixmap
buildGradientPixmap(Widget w, OpInfo * info,
		    int width, int height, int depth, int mode)
{
    Pixmap pixmap;
    Image *image;
    Colormap cmap;
    Palette *pal;
    unsigned char *op;
    XGCValues value;
    XColor *col;
    int dx, dy, maxdist, ox, oy, x, y, xx, yy, rp, gp, bp, rs, gs, bs,
     s;
    float fraction, a, cs, sn;


    image = ImageNew(width, height);
    op = image->data;
    XtVaGetValues(w, XtNcolormap, &cmap, NULL);
    pal = PaletteFind(w, cmap);

    /* get primary colour */
    XGetGCValues(XtDisplay(w), info->first_gc, GCForeground, &value);
    col = PaletteLookup(pal, value.foreground);
    rp = col->red / 256;
    gp = col->green / 256;
    bp = col->blue / 256;

    /* get secondary colour */
    XGetGCValues(XtDisplay(w), info->second_gc, GCForeground, &value);
    col = PaletteLookup(pal, value.foreground);
    rs = col->red / 256;
    gs = col->green / 256;
    bs = col->blue / 256;

    if (TFillTilt < 0)
	TFillTilt += 360;

    /* Some handy values */
    ox = width * (TFillHoffset + 0.5);	/* Centre point */
    oy = height * (0.5 - TFillVoffset);
    cs = cos(TFillTilt * DEG2RAD);	/* For rotation of coords */
    sn = sin(TFillTilt * DEG2RAD);
    maxdist = MAX(ox, width - ox);	/* Max distance to any point in pixmap */
    maxdist = MAX(maxdist, oy);
    maxdist = MAX(maxdist, height - oy);

    switch (mode) {
    case TFILL_RADIAL:
	/*
	 * This mixes colours according to distance from the point
	 * in question to a centre point.
	 */
	for (y = 0; y < height; y++) {
	    for (x = 0; x < width; x++) {
		dx = x - ox;
		dy = y - oy;
		fraction = (sqrt(dx * dx + dy * dy) / maxdist - TFillPad) /
		    (1.0 - 2 * TFillPad);
		CLAMP(0.0, fraction, 1.0);
		fraction = ((int) (fraction * TFillQuant)) / TFillQuant;
		*op++ = (1 - fraction) * rp + fraction * rs;
		*op++ = (1 - fraction) * gp + fraction * gs;
		*op++ = (1 - fraction) * bp + fraction * bs;
	    }
	}
	break;

    case TFILL_LINEAR:
	/*
	 * This mixes colours according to distance from the point
	 * in question to the x axis (in the transformed system).
	 */
	/*
	 * s is the maximum distance of any point
	 * (distance from the line y = ax+h to (w,0) or
	 * from y = ax to (w,h), whichever is greater)
	 */
	if (TFillTilt == 90)	/* tan(90) goes towards infinity */
	    s = width;
	else {
	    a = tan(TFillTilt * DEG2RAD);
	    s = abs(a * width + (a < 0 ? -height : height)) / sqrt(a * a + 1);
	}
	for (y = 0; y < height; y++)
	    for (x = 0; x < width; x++) {
		yy = x * sn + y * cs;	/* only transformed y coord is needed */
		fraction = (((float) yy) / s - TFillPad) / (1.0 - 2 * TFillPad);
		fraction = ((int) (fraction * TFillQuant)) / TFillQuant;
		CLAMP(0.0, fraction, 1.0);
		*op++ = (1 - fraction) * rp + fraction * rs;
		*op++ = (1 - fraction) * gp + fraction * gs;
		*op++ = (1 - fraction) * bp + fraction * bs;
	    }
	break;

    case TFILL_SQUARE:
	/*
	 * This mixes colours according to distance from the point
	 * in question to the axis farthest away (in the transformed system).
	 */
	for (y = 0; y < height; y++)
	    for (x = 0; x < width; x++) {
		/* transform coords and take absolute value */
		dx = x - ox;
		dy = y - oy;
		xx = abs(dx * cs - dy * sn);
		yy = abs(dx * sn + dy * cs);
		if (xx < yy)
		    s = yy;
		else
		    s = xx;
		fraction = (((float) s) / maxdist - TFillPad) / (1.0 - 2 * TFillPad);
		fraction = ((int) (fraction * TFillQuant)) / TFillQuant;
		CLAMP(0.0, fraction, 1.0);
		*op++ = (1 - fraction) * rp + fraction * rs;
		*op++ = (1 - fraction) * gp + fraction * gs;
		*op++ = (1 - fraction) * bp + fraction * bs;
	    }
	break;

    case TFILL_CONE:
	/*
	 * Convert (x,y) to polar coordinates (r,a) and mix
	 * colours according to the fraction a/360.
	 */
	for (y = 0; y < height; y++)
	    for (x = 0; x < width; x++) {
		dx = x - ox;
		dy = y - oy;
		if (dx == 0)
		    a = dy > 0 ? 270 : 90;
		else {
		    a = atan(((float) dy) / ((float) dx));
		    a *= 1.0 / DEG2RAD;
		    if (dx > 0)	/* atan() returns principal value -pi/2..pi/2 */
			a = a + 180.0;
		    while (a < 0)	/* normalize */
			a += 360.0;
		}
		a += TFillTilt;
		while (a > 360.0)
		    a -= 360.0;
		if (a > 180.0)	/* map (180,360) to (180,0) */
		    a = 360.0 - a;
		fraction = (a / 180.0 - TFillPad) / (1.0 - 2 * TFillPad);
		fraction = ((int) (fraction * TFillQuant)) / TFillQuant;
		CLAMP(0.0, fraction, 1.0);
		*op++ = (1 - fraction) * rp + fraction * rs;
		*op++ = (1 - fraction) * gp + fraction * gs;
		*op++ = (1 - fraction) * bp + fraction * bs;
	    }
	break;
    }

    pixmap = None;		/* force creation of new pixmap */
    ImageToPixmapCmap(image, w, &pixmap, cmap);
    return pixmap;
}

#ifdef FEATURE_FRACTAL
static Pixmap
buildFractalPixmap(Widget w, OpInfo * info,
		   int width, int height, int depth, int mode)
{
    Image *image;
    Pixmap pixmap;
    Colormap cmap;

    switch (ffillMode) {
    case 0:
	image = draw_plasma(width, height);
	break;
    case 1:
	image = draw_landscape(width, height, 1);
	break;
    case 2:
    default:
	image = draw_landscape(width, height, 0);
	break;
    }

    XtVaGetValues(w, XtNcolormap, &cmap, NULL);
    pixmap = None;		/* force creation of new pixmap */
    ImageToPixmapCmap(image, w, &pixmap, cmap);
    return pixmap;
}
#endif