summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/XIE/mixie/process/mpgeomaa.c
blob: dd23295f2680cf05259fe3a44f83481beafd2528 (plain)
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
/* $XConsortium: mpgeomaa.c,v 1.6 93/11/06 15:40:41 rws Exp $ */
/**** module mpgeomaa.c ****/
/******************************************************************************
				NOTICE
                              
This software is being provided by AGE Logic, Inc. and MIT under the
following license.  By obtaining, using and/or copying this software,
you agree that you have read, understood, and will comply with these
terms and conditions:

     Permission to use, copy, modify, distribute and sell this
     software and its documentation for any purpose and without
     fee or royalty and to grant others any or all rights granted
     herein is hereby granted, provided that you agree to comply
     with the following copyright notice and statements, including
     the disclaimer, and that the same appears on all copies and
     derivative works of the software and documentation you make.
     
     "Copyright 1993, 1994 by AGE Logic, Inc. and the Massachusetts
     Institute of Technology"
     
     THIS SOFTWARE IS PROVIDED "AS IS".  AGE LOGIC AND MIT MAKE NO
     REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  By way of
     example, but not limitation, AGE LOGIC AND MIT MAKE NO
     REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS
     FOR ANY PARTICULAR PURPOSE OR THAT THE SOFTWARE DOES NOT
     INFRINGE THIRD-PARTY PROPRIETARY RIGHTS.  AGE LOGIC AND MIT
     SHALL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE.  IN NO
     EVENT SHALL EITHER PARTY BE LIABLE FOR ANY INDIRECT,
     INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS
     OF PROFITS, REVENUE, DATA OR USE, INCURRED BY EITHER PARTY OR
     ANY THIRD PARTY, WHETHER IN AN ACTION IN CONTRACT OR TORT OR
     BASED ON A WARRANTY, EVEN IF AGE LOGIC OR MIT OR LICENSEES
     HEREUNDER HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
     DAMAGES.
    
     The names of AGE Logic, Inc. and MIT may not be used in
     advertising or publicity pertaining to this software without
     specific, written prior permission from AGE Logic and MIT.

     Title to this software shall at all times remain with AGE
     Logic, Inc.
*****************************************************************************
  
	mpgeomaa.c -- DDXIE element for handling antialias
			geometry technique
  
	Ben Fahy && Larry Hare -- AGE Logic, Inc. July, 1993
  
*****************************************************************************/


#define _XIEC_MPGEOM
#define _XIEC_PGEOM

/*
 *  Include files
 */
#include <stdio.h>

/*
 *  Core X Includes
 */
#include <X.h>
#include <Xproto.h>
/*
 *  XIE Includes
 */
#include <XIE.h>
#include <XIEproto.h>
/*
 *  more X server includes.
 */
#include <misc.h>
#include <dixstruct.h>
/*
 *  Server XIE Includes
 */
#include <error.h>
#include <macro.h>
#include <element.h>
#include <texstr.h>
#include <xiemd.h>
#include <mpgeom.h>
#include <technq.h>


/*
 *  routines referenced by other DDXIE modules
 */
int	miAnalyzeGeomAA();

/*
 *  routines used internal to this module, technique dependent
 */

/* antialias by lowpass using boxcar filter*/
static int CreateGeomAA();
static int InitializeGeomAA();
static int ActivateGeomAA();
static int ResetGeomAA();
static int DestroyGeomAA();


/*
 * DDXIE Geometry entry points
 */
static ddElemVecRec AntiAliasVec = {
  CreateGeomAA,
  InitializeGeomAA,
  ActivateGeomAA,
  (xieIntProc)NULL,
  ResetGeomAA,
  DestroyGeomAA
  };

static void AASL_R(), AASL_b(), AASL_B(), AASL_P(), AASL_Q(); /* AA scale */
static void AAGL_R(), AAGL_b(), AAGL_B(), AAGL_P(), AAGL_Q(); /* AA general */
static void XXFL_R(), XXFL_b(), XXFL_B(), XXFL_P(), XXFL_Q(); /* fill */
static void (*aa_scl_lines[5])() = { AASL_R, AASL_b, AASL_B, AASL_P, AASL_Q };
static void (*aa_gen_lines[5])() = { AAGL_R, AAGL_b, AAGL_B, AAGL_P, AAGL_Q };
static void (*fill_lines[5])()   = { XXFL_R, XXFL_b, XXFL_B, XXFL_P, XXFL_Q };

#if XIE_FULL
static void GAGL_R(),           GAGL_B(), GAGL_P(), GAGL_Q(); /* GA general */
static void (*ga_scl_lines[5])() = { GAGL_R, AASL_b, GAGL_B, GAGL_P, GAGL_Q };
static void (*ga_gen_lines[5])() = { GAGL_R, AAGL_b, GAGL_B, GAGL_P, GAGL_Q };
#endif

typedef struct _bounding_rect {
	double 	xmin,xmax,ymin,ymax;
} brect;

#define FLG_A_NOT_ZERO 0x01
#define FLG_B_NOT_ZERO 0x02
#define FLG_C_NOT_ZERO 0x04
#define FLG_D_NOT_ZERO 0x08
#define FLG_BACKWARDS  0x10
#define FLG_SKIP_BAND  0x20

typedef struct _mpaabanddef {
	CARD32  flags;		/* see FLG_xxx above */
	int	yOut;		/* what output line we are on */

	int	first_ilow,	/* rounded first_mlow   */
		first_ihigh;	/* rounded first_mhigh  */

	double	first_mlow,	/* lowest  input line mapped by first output */
		first_mhigh;	/* highest input line mapped by first output */

	brect 	left_br;	/* bounding rectangle, left  side */

	int	*ixmin;		/* useful data precalculated for scaling */
	int	*ixmax;

	int	level_clip;	/* in case we need to clip our pixel */
	int	int_constant;	/* precalculated for Constrained data fill */
	RealPixel flt_constant;	/* precalculated for UnConstrained data fill */

	int	in_width;	/* source image size */
	int	in_height;

	int	lo_src_avail;	/* lines we think are available */
	int	hi_src_avail;

	void    (*linefunc) ();	/* action routines based on pixel size */
	void    (*fillfunc) ();
  }
mpAABandRec, *mpAABandPtr;

/*------------------------------------------------------------------------
------------------------  fill in the vector  ---------------------------
------------------------------------------------------------------------*/
int miAnalyzeGeomAA(flo,ped)
    floDefPtr flo;
    peDefPtr  ped;
{
    switch(ped->techVec->number) {
    case xieValGeomAntialias:
    case xieValGeomGaussian:	/* safe, but slow and sometimes ugly */
	ped->ddVec = AntiAliasVec;
	break;
    default:
    	return(FALSE);
    }
    return(TRUE);
}                               /* end miAnalyzeGeomAA */


/*------------------------------------------------------------------------
---------------------------- create peTex . . . --------------------------
------------------------------------------------------------------------*/
static int CreateGeomAA(flo,ped)
    floDefPtr flo;
    peDefPtr  ped;
{
    int auxsize = sizeof(mpAABandRec) * xieValMaxBands;

    return(MakePETex(flo, ped, auxsize, NO_SYNC, NO_SYNC));

}                               /* end CreateGeomAA */
/*------------------------------------------------------------------------
---------------------------- free private data . . . ---------------------
------------------------------------------------------------------------*/
static int FreeBandData(flo,ped)
    floDefPtr flo;
    peDefPtr  ped;
{
    mpAABandPtr pvtband = (mpAABandPtr) (ped->peTex->private); 
    int band, nbands = ped->inFloLst[SRCtag].bands;
  
    for (band = 0 ; band < nbands ; band++, pvtband++) { 
	if (pvtband->flags & FLG_SKIP_BAND)
	    continue;
	if (pvtband->ixmin != NULL)
	    pvtband->ixmin = (int *) XieFree(pvtband->ixmin);

    }
}
/*------------------------------------------------------------------------
/*------------------------------------------------------------------------
---------------------------- initialize peTex . . . ----------------------
------------------------------------------------------------------------*/
static int InitializeGeomAA(flo,ped)
  floDefPtr flo;
  peDefPtr  ped;
{
  peTexPtr	   pet = ped->peTex;
  xieFloGeometry  *raw = (xieFloGeometry *)ped->elemRaw;
  pGeomDefPtr	pedpvt = (pGeomDefPtr)ped->elemPvt; 
  mpAABandPtr  pvtband = (mpAABandPtr) (pet->private);
  bandPtr	 iband = &(pet->receptor[SRCtag].band[0]);
  bandPtr	 oband = &(pet->emitter[0]);
  int		nbands = ped->inFloLst[SRCtag].bands;
#if XIE_FULL
  BOOL	      gaussian = (ped->techVec->number == xieValGeomGaussian);
#endif
  int		 band;
 /*
  * access coordinates for y_in = c * x_out + d * y_out + ty
  */
  double a  = pedpvt->coeffs[0];
  double b  = pedpvt->coeffs[1];
  double c  = pedpvt->coeffs[2];
  double d  = pedpvt->coeffs[3];
  double tx = pedpvt->coeffs[4];
  double ty = pedpvt->coeffs[5];
  int width = raw->width;
  int threshold;
  double xmin,xmax,ymin,ymax,tot_ymin,tot_ymax;
  CARD32 dataclass;
  CARD32 band_flags;

/*
 *  Initialize parameters for tracking input lines, etc.
 */
  band_flags = 0;

  if (a != 0.0) band_flags |= FLG_A_NOT_ZERO;
  if (b != 0.0) band_flags |= FLG_B_NOT_ZERO;
  if (c != 0.0) band_flags |= FLG_C_NOT_ZERO;
  if (d != 0.0) band_flags |= FLG_D_NOT_ZERO;
  if (d  < 0.0) band_flags |= FLG_BACKWARDS;

  for (band = 0 ; band < nbands; band++, iband++, oband++, pvtband++) { 

    if (pedpvt->do_band[band]) {

	pvtband->flags = band_flags;
	pvtband->yOut  = 0;		/* what output line we are on */

	dataclass =oband->format->class;
	if (IsConstrained(dataclass))
	    pvtband->int_constant = ConstrainConst(pedpvt->constant[band],
						   iband->format->levels);
	else
	    pvtband->flt_constant = (RealPixel) pedpvt->constant[band];	

	pvtband->fillfunc = fill_lines[IndexClass(dataclass)];
#if XIE_FULL
	pvtband->linefunc = gaussian
		? ga_gen_lines[IndexClass(dataclass)]
		: aa_gen_lines[IndexClass(dataclass)];
#else
	pvtband->linefunc = aa_gen_lines[IndexClass(dataclass)];
#endif

	pvtband->level_clip = oband->format->levels;
	pvtband->in_width = iband->format->width;
	pvtband->in_height = iband->format->height;

 /*
  * THE BASIC IDEA - 
  *
  * We consider each output pixel as describing an *area* of width 1
  * and height 1.  Pixel (xo,yo) thus refers to the locus of points:
  *
  * LocusOut(xo,yo) = { (x,y) | xo <= x < xo+1, yo <= y < yo + 1 }
  *
  * When we map this area back to the input image, each corner maps
  * according to:
  *
  *	x_in = a * x_out + b * y_out + tx,
  *	y_in = c * x_out + b * y_out + ty.
  *
  * Now let P(xo,yo) be described as the point at relative position
  * (p,q) from (xo,yo) in LocusOut(xo,yo).  In other words, 
  *
  *	P(xo,yo) = (xo+p, yo+q)
  *
  * Let M(p,q;xo,yo) be the mapping in input space of P(xo,yo). Then
  * the x coordinate of M(p,q;xo+1,yo) is:
  *
  *	XCoord[ M(p,q:xo+1,yo) ] = a * (x_o+1+p) + b*yo + tx
  *				 = a * (x_o+p) + b*yo + tx + a*p
  * so
  *
  *	XCoord[ M(p,q:xo+1,yo) ] = XCoord[ M(p,q;xo,yo) ] + a*p
  *
  * similarly, 
  *
  *	YCoord[ M(p,q:xo,yo+1) ] = YCoord[ M(p,q;xo,yo) ] + c*q
  *
  * also,
  *
  *	XCoord[ M(p+1,q:xo,yo) ] = XCoord[ M(p,q;xo,yo) ] + a*p
  *	YCoord[ M(p+1,q:xo,yo) ] = XCoord[ M(p,q;xo,yo) ] + c*p
  *	XCoord[ M(p,q+1:xo,yo) ] = YCoord[ M(p,q;xo,yo) ] + b*q
  *	YCoord[ M(p,q+1:xo,yo) ] = YCoord[ M(p,q;xo,yo) ] + d*q
  *
  * We will use these results to derive a computationally simple
  * antialiasing algorithm.
  *
  * Suppose we are scaling an image down by a factor of four in
  * the X direction:
  *
  *  |  |  |  |**|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
  *  |  |  |  |**|  |  |  |  |**|**|**|**|**|**|**|**|**|**|  |
  *  |  |  |  |**|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
  *
  *       |           |          | ******** | ******** | *****
  *
  *  If we nearest neighbor sample the input image where the 
  *  output pixels map to input locations,  the horizontal line
  *  at the right will show up in the output image, but the vertical
  *  line at left will be completely missed.
  *
  *  The idea of this algorithm is to take the average of all pixels
  *  which are included in the *region* mapped by the output *area*,
  *  as opposed to looking at discrete pixels only.  So we consider
  *  a pixel as representing an output locus
  *
  * 	LocusOut(xo,yo) = { (x,y) | xo <= x < xo+1, yo <= y < yo + 1 }
  *
  *  and the input locus will have corner points:
  *
  *	( XCoord[ M(0,0:xo,yo) ],YCoord[ M(0,0:xo,yo) ] )
  *	( XCoord[ M(1,0:xo,yo) ],YCoord[ M(1,0:xo,yo) ] )
  *	( XCoord[ M(0,1:xo,yo) ],YCoord[ M(0,1:xo,yo) ] )
  *	( XCoord[ M(1,1:xo,yo) ],YCoord[ M(1,1:xo,yo) ] )
  *
  *  If  xi(xo) = XCoord[ M(0,0;xo,yo) ]
  *  and yi(yo) = YCoord[ M(0,0;xo,yo) ],
  *
  *  Then we can rewrite these using the relations above as:
  *
  *  	( xi(xo),     yi(yo) ),  
  *	( xi(xo)+a,   yi(yo)+c ), 
  *  	( xi(xo)+b,   yi(yo)+d),  
  *	( xi(xo)+a+b, yi(yo)+c+d ). 
  * 
  *  We have very little guarantee on what shape this collection
  *  of points will assume,  without agreeing to look for special
  *  cases of (a,b,c,d).  However, we do know that whatever the
  *  locus looks like, there is a "bounding rectangle":
  *
  *	BRect: { (xmin,ymin; xmax,ymax) } 
  *
  *  such that xmin is the greatest lower bound of all x in the input locus,
  *  such that ymin is the greatest lower bound of all y in the input locus,
  *  such that xmax is the least upper bound of all x in the input locus, &
  *  such that ymax is the least upper bound of all y in the input locus.
  *
  *  Furthermore, this bounding rectangle has the very nice property 
  *  of "shift invariance," ie, 
  *
  *  If   { (xmin,ymin; xmax,ymax) } 	is the bounding rectangle for (xo,yo),
  *
  *  Then { (xmin+a*m+b*n,ymin+c*m+d*n; xmax+a*m+b*n,ymax+c*m+d*n) } 
  *					is the bounding rect for (xo+m,yo+n).
  *
  *  Proof.  Let Yo be the ymax coordinate of the bounding rectangle for 
  *  (xo,yo). Let ILo be the input locus of (xo,yo).  Let Yo' be the ymax 
  *  coordinate of (xo+m,yo+n),  and ILo' be the input locus of (xo+m,yo+n).  
  *  We claim Yo' = Yo + c*m+d*n. 
  *
  *  By definition, Yo >= y for all y in ILo, and Yo' >= y for all y in ILo'.  
  *  Suppose Yo' < Yo + c*m+d*n.  Then Yo~ = (Yo' - c*m - d*n) is less than
  *  Yo.  If Yo~ >= y for all y in ILo,  then this contradicts Yo being a
  *  least upper bound.  Therefore there must be some offset (r,s) such 
  *  that Yrs = YCoord[ M(r,s;xo,yo) ] is > Yo~.  But by the translation
  *  rules above, YCoord[ M(r,s; xo+m,yo+n) ] = Yrs + c*m + d*n. Call 
  *  this point Yrs'.  Then:
  *
  *     Yrs' = Yrs + c*m + d*n > Yo~ + c*m + d*n = Yo'
  *
  *  which violates the assumption that Yo' is the least upper bound for
  *  ILo'.   Thus, Yo' >= Yo + c*m + d*n.  It is easy to show that Yo'
  *  greater than Yo + c*m + d*n also leads to a contradiction.  Therefore
  *  we have Yo' identically equal to Yo + c*m + d*n,  and the other 
  *  coordinates (xmin,xmax,ymin) follow by similar reasoning.
  *
  * ----------------------------------------------------------------------
  *
  *  WHY THIS IS USEFUL:
  *
  *  We compute the bounding rectangle for the first pixel on the first
  *  output line. Then it is easy to compute the bounding rectangle for
  *  all other points in the output image.  In particular, we can compute
  *  ymin,ymax values for any line in the output image. We don't bother
  *  decoding any output line until all of the required input image lines
  *  are available.
  *
  *  Once we have the data,  we call a line function that does the real
  *  work.  The line function simply marches through the bounding rect
  *  of each pixel in the output line,  adds up the input image numbers,
  *  and divides by the number of discrete pixels in the bounding rect.
  *  The next bounding rect in the line is easily calculated by the
  *  shift-invariance relation.  Clipping of the bounding rect is used
  *  to avoid integrating nonsense values.  An output pixel is filled
  *  with the constant value only if the *entire* bounding rectangle
  *  is off-image.
  *
  */

/***  Calculate bounding rectangle of first pixel, ylimits of first line ***/

	xmin = xmax = tx;
	ymin = ymax = tot_ymin = tot_ymax = ty;

#if XIE_FULL
	if (gaussian) {
	    pTecGeomGaussianDefPtr tkpvt = (pTecGeomGaussianDefPtr)ped->techPvt;

	    if (tkpvt->radius < 1) tkpvt->radius = 2;

	    /* conservatively pick a bounding box for datamgr calculations */
	    xmin -= ((double) tkpvt->radius + 1.00001);
	    xmax += ((double) tkpvt->radius + 1.99999);
	    ymin -= ((double) tkpvt->radius + 1.00001);
	    ymax += ((double) tkpvt->radius + 1.99999);
	    tot_ymin = ymin + (c < 0.0 ? (width * c) : 0.0);
	    tot_ymax = ymax + (c > 0.0 ? (width * c) : 0.0);
	} else
#endif
	{
	    if (a < 0) xmin += a;
	    else       xmax += a;
	    if (b < 0) xmin += b;
	    else       xmax += b;

	    if (c < 0) { ymin += c; tot_ymin += (width * c); }
	    else       { ymax += c; tot_ymax += (width * c); }
	    if (d < 0) { ymin += d; tot_ymin += d; }
	    else       { ymax += d; tot_ymax += d; }
	}

	pvtband->left_br.xmin = xmin;
	pvtband->left_br.ymin = ymin;
	pvtband->left_br.xmax = xmax;
	pvtband->left_br.ymax = ymax;

        pvtband->first_mlow  = tot_ymin;
        pvtband->first_mhigh = tot_ymax;

        pvtband->first_ilow  = pvtband->first_mlow;
        pvtband->first_ihigh = pvtband->first_mhigh;

/***	Check for some special cases 	***/
	if ((band_flags & (FLG_C_NOT_ZERO | FLG_B_NOT_ZERO)) == 0) {
	    int i, maxpixl  = pvtband->in_width - 1;

#if XIE_FULL
	    pvtband->linefunc = gaussian
		? ga_scl_lines[IndexClass(dataclass)]
		: aa_scl_lines[IndexClass(dataclass)];
#else
	    pvtband->linefunc = aa_scl_lines[IndexClass(dataclass)];
#endif

	    if (!(pvtband->ixmin = (int *) XieMalloc(width * 2*sizeof(int)))) {
		FreeBandData(flo,ped);
		AllocError(flo, ped, return(FALSE));
	    }
	    pvtband->ixmax = pvtband->ixmin + width;
	    /* Valid X coord domain will remain same for entire image */
	    for  ( i=0; i<width; ++i) { 
		register int ixmin,ixmax;

		ixmin = xmin; 		
		ixmax = xmax; 
		xmin += a; 
		xmax += a; 

		if (ixmin < 0)		ixmin = 0;
		if (ixmax > maxpixl)	ixmax = maxpixl;
		if (ixmax > ixmin)	ixmax--;

		pvtband->ixmin[i] = ixmin;
		pvtband->ixmax[i] = ixmax;
	    }
	}

	/* set threshold so we get all needed src lines */
	/* if we need line 256, must ask for 257! */
	threshold = pvtband->first_ihigh + 1;

	/* make sure we get something */
	if (threshold < 1)
	    threshold = 1;

	/* but don't ask for stuff we can't ever get! */
	if (threshold > iband->format->height)
	    threshold = iband->format->height;

	if(!InitBand(flo, ped, iband, iband->format->height,
						     threshold, NO_INPLACE))
	    return(FALSE);
    } else {
	pvtband->flags = FLG_SKIP_BAND;
	BypassSrc(flo,pet,iband);
    }
  }
  return(raw->bandMask ? InitEmitter(flo,ped,NO_DATAMAP,NO_INPLACE) : TRUE);
}                               /* end InitializeGeomAA */
/*------------------------------------------------------------------------
----------------------------- crank some data ----------------------------
------------------------------------------------------------------------*/
static int ActivateGeomAA(flo,ped)
     floDefPtr flo;
     peDefPtr  ped;
{
  peTexPtr	  pet = ped->peTex;
  pGeomDefPtr  pedpvt = (pGeomDefPtr) ped->elemPvt; 
  mpAABandPtr pvtband = (mpAABandPtr) pet->private;
  bandPtr	oband = &(pet->emitter[0]);
  bandPtr	iband = &(pet->receptor[SRCtag].band[0]);
  int    band, nbands = pet->receptor[SRCtag].inFlo->bands;
  int    	width = oband->format->width; /* consider use of pvtband */
  double	    d = pedpvt->coeffs[3];
  register pointer outp;

  for(band = 0; band < nbands; band++, iband++, oband++, pvtband++) {

    if (pvtband->flags & FLG_SKIP_BAND)
	continue;       

    if ((pet->scheduled & (1 << band)) == 0)
	continue;       

    if (pvtband->flags & FLG_BACKWARDS) {

	/* we're going backwards, which is actually *simpler*, 
	* because we don't get ANY data until we have ALL data.
	* Thus, first time through, just map everything we have.
	*/
	if (!pvtband->yOut)  {
	    if (!MapData(flo,pet,iband,0,0,iband->maxGlobal,KEEP)) 
		ImplementationError(flo,ped, return(FALSE));
	    pvtband->lo_src_avail = 0;
	    pvtband->hi_src_avail = iband->maxGlobal-1;
	}

	outp = GetCurrentDst(flo,pet,oband);
	while (outp) {

	    if ((pvtband->first_ihigh < 0) ||
		(pvtband->first_ilow  > pvtband->in_height))

		(*pvtband->fillfunc)(outp,width,pvtband);
	    else 
		(*pvtband->linefunc)(outp,iband->dataMap,width,ped,pvtband);

	    /* now compute highest input line for next oline */
	    pvtband->first_mlow  += d;
	    pvtband->first_mhigh += d;
	    pvtband->first_ilow  = (int) pvtband->first_mlow ;
	    pvtband->first_ihigh = (int) pvtband->first_mhigh;
	    pvtband->yOut++;
	    outp = GetNextDst(flo,pet,oband,TRUE);
	}
	if (oband->final)
	    DisableSrc(flo,pet,iband,FLUSH);

    } else {

	while (!ferrCode(flo)) {		
	    int map_lo;		/* lowest  line mapped by output line */
	    int map_hi;		/* highest line mapped by output line */
	    int last_src_line = pvtband->in_height - 1;
	    int threshold;

	    /* access current output line */
	    outp = GetDst(flo,pet,oband,pvtband->yOut,FLUSH);
	    if (!outp) {
		if (oband->final)
		    DisableSrc(flo, pet, iband, FLUSH);
		else if (iband->current != 0) 
		    FreeData(flo, pet, iband, iband->current);
		goto breakout;
	   }

	    map_lo = pvtband->first_ilow;
	    if (map_lo < 0)
		map_lo = 0;

	    map_hi = pvtband->first_ihigh;
	    if (map_hi > last_src_line)
		map_hi = last_src_line;

	    if (map_hi < 0 || map_lo > last_src_line)
		(*pvtband->fillfunc)(outp,width,pvtband);
	    else {
	        if(!MapData(flo,pet,iband,map_lo,map_lo,(map_hi-map_lo+1),KEEP))
		    break;
		pvtband->lo_src_avail = 0;
		pvtband->hi_src_avail = iband->maxGlobal-1;

		if (map_lo != iband->current) 
		    ImplementationError(flo,ped, return(FALSE));

		/***	Compute output pixels for this line ***/
		(*pvtband->linefunc)(outp,iband->dataMap,width,ped,pvtband);
	    }

	    /* increment to next line. */

	    pvtband->first_mlow  += d;
	    pvtband->first_mhigh += d;
					
	    /* have to be careful about -0.5 rounding to 0, not -1 */
	    if (pvtband->first_ilow < 0) {
		pvtband->first_ilow = (pvtband->first_mlow < 0)
					? -1
					: (int)pvtband->first_mlow;
		pvtband->first_ihigh = (pvtband->first_mhigh < 0)
					? -1
					: (int)pvtband->first_mhigh;
	    } else {
		/* if ilow was positive before, needn't check for negative */
		pvtband->first_ilow  = (int)pvtband->first_mlow;	
		pvtband->first_ihigh = (int)pvtband->first_mhigh;
	    }

	    ++pvtband->yOut;						
	    if (pvtband->first_ilow > last_src_line) {
		/* rest of output image is off the input image */
		/* we will exit after filling output strip */
		while(outp=GetDst(flo,pet,oband,pvtband->yOut,FLUSH)) {
		    (*pvtband->fillfunc)(outp,width,pvtband);
		    pvtband->yOut++;
		}
		if (oband->final) /* out of destination lines */
		    DisableSrc(flo, pet, iband, FLUSH);
		else  
		    goto breakout;
		/* Be nice and let downstream element eat our data */
		/* notice we don't free input data, because then the */
		/* silly scheduler would turn us off */
	    }

	    map_hi = pvtband->first_ihigh;
	    if (map_hi > last_src_line)
		map_hi = last_src_line;

	    threshold = map_hi - iband->current + 1;

	    /* make sure we get something */
	    if (threshold <= 1)
		threshold = 1;

	    /* but don't ask for stuff we can't ever get! */
	    if (threshold > pvtband->in_height)
		threshold = pvtband->in_height;

	    SetBandThreshold(iband, threshold);
	    if (map_hi >= (int) iband->maxGlobal) {
		/* we need to let someone else generate more data */
		break;
	    }
	}
	/* want to make sure we GetSrc at least once before Freeing */
	if (iband->current)
	    FreeData(flo, pet, iband, iband->current);
    }	/* end of backwards/forwards */
breakout: ;
  }	/* end of band loop */
  return(TRUE);
}                               /* end ActivateGeometry */

/*------------------------------------------------------------------------
------------------------ get rid of run-time stuff -----------------------
------------------------------------------------------------------------*/
static int ResetGeomAA(flo,ped)
    floDefPtr flo;
    peDefPtr  ped;
{
    FreeBandData(flo,ped);
    ResetReceptors(ped);
    ResetEmitter(ped);
  
    return(TRUE);
}                               /* end ResetGeomAA */

/*------------------------------------------------------------------------
-------------------------- get rid of this element -----------------------
------------------------------------------------------------------------*/
static int DestroyGeomAA(flo,ped)
    floDefPtr flo;
    peDefPtr  ped;
{
    /* get rid of the peTex structure  */
    ped->peTex = (peTexPtr) XieFree(ped->peTex);

    /* zap this element's entry point vector */
    ped->ddVec.create = (xieIntProc)NULL;
    ped->ddVec.initialize = (xieIntProc)NULL;
    ped->ddVec.activate = (xieIntProc)NULL;
    ped->ddVec.reset = (xieIntProc)NULL;
    ped->ddVec.destroy = (xieIntProc)NULL;

    return(TRUE);
}                               /* end DestroyGeomAA */

/**********************************************************************/
/* fill routines */
static void XXFL_b (OUTP,width,pvtband)
	register pointer OUTP;
	register int width;
	mpAABandPtr pvtband;
{
	register LogInt constant = (LogInt) pvtband->int_constant;
	register LogInt *outp	= (LogInt *) OUTP;
	register int	i;

	if (constant) constant = ~0;
	/*
	** NOTE: Following code assume filling entire line. Which is
	** currently true.  In the future we may need to abide by
	** bit boundaries. Conversely code for bytes and pairs below
	** could be sped up by doing something similar. perhaps we need
	** an action_constant(outp,run,ix,constant) routine similar to
	** action_set().
	*/
	width = (width + 31) >> 5;
	for (i=0; i < width; ++i) *outp++ = constant;
}

#define DO_FL(funcname, iotype, CONST)					\
static void funcname (OUTP,width,pvtband)				\
    register pointer OUTP;						\
    register int width;							\
    mpAABandPtr pvtband;						\
{									\
    register iotype constant = (iotype) pvtband->CONST;			\
    register iotype *outp = (iotype *) OUTP;				\
									\
    for ( ; width > 0; width--) *outp++ = constant;			\
}

DO_FL	(XXFL_R, RealPixel, flt_constant)
DO_FL	(XXFL_B, BytePixel, int_constant)
DO_FL	(XXFL_P, PairPixel, int_constant)
DO_FL	(XXFL_Q, QuadPixel, int_constant)

/**********************************************************************/
/* (x,y) separable routines (eg, scale, mirror_x, mirror_y) 	      */
/**********************************************************************/
/*
 * NOTE - see caveat for GL_b for why the user shouldn't really be
 * asking for antialias in the first place...
 */
static void AASL_b (OUTP,srcimg,width,ped,pvtband)
    register pointer OUTP, *srcimg;
    register int width;
    peDefPtr  ped;
    mpAABandPtr pvtband;
{
    pGeomDefPtr	pedpvt = (pGeomDefPtr)ped->elemPvt; 
    double d = pedpvt->coeffs[3];

    /* These variables describe the limits of the bounding rectangle */

    /* since x and y separable, x limits are precalculated once */
    register int *ixminptr=pvtband->ixmin;
    register int *ixmaxptr=pvtband->ixmax;
    register int ixmin,ixmax;	

    /* since x and y separable, y limits will be set once this line */
    register int iymin,iymax;

    /* some variables which describe available input data (for clipping) */
    register int minline = pvtband->lo_src_avail;
    register int maxline = pvtband->hi_src_avail;


    /* cast of constant, output and input pointers to correct type */
    register LogInt *outp = (LogInt *) OUTP;
    register LogInt *ptrIn;
    register LogInt constant;
    register LogInt value,outval,M;
    register int nfound;

    	constant = pvtband->int_constant ? ~(LogInt) 0 : 0;

	/* round bounding rectangle limits to ints */
	iymin=pvtband->left_br.ymin;
	iymax=pvtband->left_br.ymax;

	/* clip to available src data */
	if (iymin < minline) 	iymin = 0;
	if (iymax > maxline) 	iymax = maxline;
	if (iymax > iymin)	iymax--;

	/* M is a number that encodes the "active" bit */ 
	M=LOGLEFT; 
	
	/* val accumulates the output bits */
#define kludge_based
#ifdef kludge_based
	outval = 0;
#else
	outval = constant;
#endif

	/* loop through all the output pixels on this line */
	while ( width-- > 0 ) { 
	    register int 	ix,iy;

   	    /* make use of precalculated X limits */
	    ixmin = *ixminptr++;
	    ixmax = *ixmaxptr++;

	    nfound = 0;   
	    value = 0;   
	    for (iy=iymin; iy<=iymax; ++iy) {
		ptrIn = (LogInt *) srcimg[iy]; 

		for (ix=ixmin; ix<=ixmax; ++ix) {
		  ++nfound;
#ifdef kludge_based
		  if (LOG_tstbit(ptrIn,ix)) {
		    ++value;
#else
		  if (!LOG_tstbit(ptrIn,ix)) {
		    /* set this pixel black */
		    outval &= ~M;
		    goto next;
#endif
		  }
		}
	    } /* end of iy loop */

/* See GL_b() for an explanation of the kludge below */

#ifdef kludge_based
#define kludge 8/7
    /* note: 4/3 relies on C evaluating left to right */

	    if (!nfound)
		outval |= (constant&M);	/* set to background */
	    else if (value*kludge >= nfound)
		outval |= M;
#else

	    if (!nfound)
		outval &= (constant&M);	/* set to background */
	    else
	        outval |= M;		/* set bit on */
next:

#endif
	    /* shift our active bit over one */
	    LOGRIGHT(M); 
		
	    /* if we hit the end of a word, output what we have */
	    if (!M) { 
		*outp++ = outval; 	/* record output data   */
		M=LOGLEFT; 		/* reset active bit  	*/
		outval = 0; 		/* reset accumulator 	*/
	    }

	} /* end of line loop */

	/* if we didn't write all the pixels out, do so now */
	if (M != LOGLEFT) *outp = outval;

	/* before leaving, update bounding rect for next line */
	pvtband->left_br.ymin += d;
	pvtband->left_br.ymax += d;
}

/* NOTE: too bad paper is white is 255, otherwise you can often avoid the
** divide by "if (value) value /= nfound;"
*/
#define aalinsep_func(funcname, iotype, valtype, CONST)			\
									\
	/* round bounding rectangle limits to ints */			\
	iymin = pvtband->left_br.ymin;					\
	iymax = pvtband->left_br.ymax;					\
									\
	/* clip to available src data */				\
	if (iymin < minline) 	iymin = 0;				\
	if (iymax > maxline) 	iymax = maxline;			\
	if (iymax > iymin)	iymax--;				\
									\
	/* loop through all the output pixels on this line */		\
	for ( i=0; i<width; ++i) { 					\
	    /* loop variables */					\
	    register int ix,iy;						\
									\
	    nfound = 0;     						\
	    value = 0;     						\
	    /* ixmin = pvtband->ixmin[i]; */				\
	    /* ixmax = pvtband->ixmax[i]; */				\
	    ixmin = *ixminptr++;					\
	    ixmax = *ixmaxptr++;					\
									\
	    for (iy=iymin; iy<=iymax; ++iy) {				\
		ptrIn = ixmin + (iotype *) srcimg[iy];  		\
		for (ix=ixmin; ix<=ixmax; ++ix) {			\
		  value += *ptrIn++;					\
		  ++nfound;			  			\
		}				  			\
	    } /* end of iy loop */					\
									\
	    if (nfound) 						\
		value /= nfound;					\
	    else							\
	        value = constant;					\
									\
	    *outp++ = value;						\
									\
	}   /* end of for loop */					\
									\
	/* before leaving, update bounding rect for next line */	\
	pvtband->left_br.ymin += d;					\
	pvtband->left_br.ymax += d;					\

#define DO_AASL(funcname, iotype, valtype, CONST)			\
static void								\
funcname (OUTP,srcimg,width,ped,pvtband)				\
    register pointer OUTP;						\
    register pointer *srcimg;						\
    register int width;							\
    peDefPtr  ped;							\
    mpAABandPtr pvtband;						\
{									\
    pGeomDefPtr	pedpvt = (pGeomDefPtr)ped->elemPvt; 			\
									\
    /* Mapping coefficients */						\
    double d = pedpvt->coeffs[3];					\
									\
    /* These variables describe the limits of the bounding rectangle */	\
    register int *ixminptr = pvtband->ixmin;				\
    register int *ixmaxptr = pvtband->ixmax;				\
    register int ixmin,iymin,ixmax,iymax;				\
									\
    /* variables which describe available input data (for clipping) */	\
    register int minline = pvtband->lo_src_avail;			\
    register int maxline = pvtband->hi_src_avail;			\
									\
    /* cast of constant, output and input pointers to correct type */	\
    register iotype constant = (iotype) pvtband->CONST;			\
    register iotype *outp = (iotype *) OUTP;				\
    register iotype *ptrIn;						\
									\
    register valtype value; 						\
    register int i,nfound;						\
									\
	aalinsep_func(funcname, iotype, valtype, CONST)			\
}

DO_AASL	(AASL_R, RealPixel, RealPixel, flt_constant)
DO_AASL	(AASL_B, BytePixel, QuadPixel, int_constant)
DO_AASL	(AASL_P, PairPixel, QuadPixel, int_constant)
DO_AASL	(AASL_Q, QuadPixel, QuadPixel, int_constant)


/**********************************************************************/
/* general routines (should be able to handle any valid map) */

/* CAVEAT - antialias doesn't really make much sense for bit-bit,
 * because you can't represent any intermediate values.  This means
 * you can choose between line dropouts (bad) or making your image
 * overly bold (also bad).  The algorithm here assumes that if you
 * were worried about being too bold, you would use the default
 * sampling technique (eg, nearest neighbor). So it attempts to
 * eliminate line dropouts at the expense of decreasing fine 
 * image detail.
 */

static void
AAGL_b (OUTP,srcimg,width,ped,pvtband)
    register pointer OUTP, *srcimg;
    register int width;
    peDefPtr  ped;
    mpAABandPtr pvtband;
{
    pGeomDefPtr	pedpvt = (pGeomDefPtr)ped->elemPvt;

    /* Mapping coefficients */
    double a = pedpvt->coeffs[0]; 
    double b = pedpvt->coeffs[1];
    double c = pedpvt->coeffs[2];
    double d = pedpvt->coeffs[3];

    /* These variables describe the limits of the bounding rectangle */
    double xmin = pvtband->left_br.xmin;
    double ymin = pvtband->left_br.ymin;
    double xmax = pvtband->left_br.xmax;
    double ymax = pvtband->left_br.ymax;
    register int ixmin,iymin,ixmax,iymax;	

    /* loop variables for roaming through the bounding rectangle */
    register int ix,iy;

    /* some variables which describe available input data (for clipping) */
    register int maxpixl = pvtband->in_width - 1;
    register int minline = pvtband->lo_src_avail;
    register int maxline = pvtband->hi_src_avail;
    CARD32	 flags	 = pvtband->flags;

    /* cast of constant, output and input pointers to correct type */
    register LogInt *outp = (LogInt *) OUTP;
    register LogInt *ptrIn;
    register LogInt constant;
    register LogInt value,outval,M;
    register int nfound;

    	constant = pvtband->int_constant ? ~(LogInt) 0 : 0;

	/* round bounding rectangle limits to ints */
	ixmin = xmin;
	iymin = ymin;
	ixmax = xmax;
	iymax = ymax; 

	/* clip to available src data */
	if (ixmin < 0) 	 	ixmin = 0;
	if (iymin < minline) 	iymin = 0;
	if (ixmax > maxpixl) 	ixmax = maxpixl;
	if (iymax > maxline) 	iymax = maxline;

	/* M is a number that encodes the "active" bit */ 
	M=LOGLEFT; 
	
	/* val accumulates the output bits */
#define kludge_based_not
#ifdef kludge_based
	outval = 0;
#else
	outval = constant;
#endif

	/* loop through all the output pixels on this line */
	while ( width > 0 ) { 

	    xmin += a; 
	    xmax += a; 
	    nfound = 0;   
	    value = 0;   
	    for (iy=iymin; iy<=iymax; ++iy) {
		ptrIn = (LogInt *) srcimg[iy]; 

		for (ix=ixmin; ix<=ixmax; ++ix) {
		  ++nfound;
#ifdef kludge_based
		  if (LOG_tstbit(ptrIn,ix)) {
		    ++value;
		  }
#else
		  if (!LOG_tstbit(ptrIn,ix)) {
		    /* set this pixel black */
		    outval &= ~M;
		    goto next;
		  }
#endif
		}
	    } /* end of iy loop */

/* on the kludge below:  antialias is weird when you are going from
   1 bit to 1 bit.  A straight generalization of the n-bit algorithm
   would have us find the average value in the bounding rect, and
   then round to 0 or 1.  This corresponds to kludge = 2.  However,
   it may be desirable to choose black if a much smaller number of
   pixels in the bounding rect are off.  For example, we may want to
   have black output if less than 3 out of every four pixels in the
   input bounding rect are on.  (value <= 3/4 nfound) which would
   correspond to value *4/3 < nfound.

   The problem with choosing anything other than 2 is that we are
   treating black and white asymmetrically:   the algorithm will
   perform well for black lines on a white background, and poorly
   for white pixels on a black background.  Granted, one could use
   Point or Constrain to flip, but.... ugh.

   Note that if the bounding rectangle is completely off the input
   image, then we should set 'val' to the background value (constant).
*/

#ifdef kludge_based
#define kludge 8/7
    /* note: 4/3 relies on C evaluating left to right */

	    if (!nfound)
		outval |= (constant&M);	/* set to background */
	    else if (value*kludge >= nfound)
		outval |= M;
#else

	    if (!nfound)
		outval &= (constant&M);	/* set to background */
	    else
	        outval |= M;		/* set bit on */
next:

#endif
	    /* shift our active bit over one */
	    LOGRIGHT(M); 
		
	    /* if we hit the end of a word, output what we have */
	    if (!M) { 
		*outp++ = outval; 	/* record output data   */
		M=LOGLEFT; 		/* reset active bit  	*/
		outval = 0; 		/* reset accumulator 	*/
	    }

	    /* prepare geometry stuff for next loop */
	    width--; 
	    ixmin = xmin;
	    ixmax = xmax;     
	    if (flags & FLG_C_NOT_ZERO) {
		ymin += c; 
		ymax += c; 
		iymin = ymin;     
		iymax = ymax;    
		if (iymin < minline)	iymin = minline;
	       	if (iymax >= maxline)	iymax = maxline;
		if (iymax > iymin)	iymax--;
	    } 
	    if (ixmin < 0)		ixmin = 0;
	    if (ixmax >= maxpixl)	ixmax = maxpixl;
	    if (ixmax > ixmin)	ixmax--;
	} /* end of line loop */

	/* if we didn't write all the pixels out, do so now */
	if (M != LOGLEFT) *outp = outval;

	/* before leaving, update bounding rect for next line */
	if (flags & FLG_B_NOT_ZERO) {
	   pvtband->left_br.xmin += b;
	   pvtband->left_br.xmax += b;
	}
	if (flags & FLG_D_NOT_ZERO) {
	   pvtband->left_br.ymin += d;
	   pvtband->left_br.ymax += d;
	}
}

/* NOTE: too bad paper is white is 255, otherwise you can often avoid the
** divide by "if (value) value /= nfound;"
*/
#define inscrutable_compiler(funcname, iotype, valtype, CONST)		\
	/* round bounding rectangle limits to ints */			\
	ixmin = xmin;		     					\
	iymin = ymin;     						\
	ixmax = xmax;     						\
	iymax = ymax;     						\
									\
	/* clip to available src data */				\
	if (ixmin < 0) 	 	ixmin = 0;				\
	if (iymin < minline) 	iymin = 0;				\
	if (ixmax > maxpixl) 	ixmax = maxpixl;			\
	if (iymax > maxline) 	iymax = maxline;			\
									\
	/* loop through all the output pixels on this line */		\
	while ( width > 0 ) { 						\
									\
	    xmin += a; 							\
	    xmax += a; 							\
	    nfound = 0;     						\
	    value = 0;     						\
	    for (iy=iymin; iy<=iymax; ++iy) {				\
		ptrIn = (iotype *) srcimg[iy];  			\
		for (ix=ixmin; ix<=ixmax; ++ix) {			\
		  value += ptrIn[ix];		  			\
		  ++nfound;			  			\
		}				  			\
	    } /* end of iy loop */					\
									\
	    if (nfound) 						\
		value /= nfound;					\
	    else							\
	        value = constant;					\
									\
	    /* prepare for next loop */					\
	    width--; 							\
	    ixmin = xmin;		     				\
	    ixmax = xmax;     						\
	    if (flags & FLG_C_NOT_ZERO) {				\
		ymin += c; 						\
		ymax += c; 						\
		iymin = ymin;     					\
		iymax = ymax;     					\
		if (iymin < minline)	iymin = minline;		\
	       	if (iymax >= maxline)	iymax = maxline;		\
		if (iymax > iymin)	iymax--;			\
	    } 								\
	    if (ixmin < 0)		ixmin = 0;			\
	    if (ixmax >= maxpixl)	ixmax = maxpixl;		\
	    if (ixmax > ixmin)		ixmax--;			\
	    *outp++ = value;						\
	}/* end of line loop */						\
									\
	/* before leaving, update bounding rect for next line */	\
	if (flags & FLG_B_NOT_ZERO) {					\
	   pvtband->left_br.xmin += b;					\
	   pvtband->left_br.xmax += b;					\
	}								\
	if (flags & FLG_D_NOT_ZERO) {					\
	   pvtband->left_br.ymin += d;					\
	   pvtband->left_br.ymax += d;					\
	}								

#define DO_AAGL(funcname, iotype, valtype, CONST)			\
static void								\
funcname (OUTP,srcimg,width,ped,pvtband)				\
    register pointer OUTP;						\
    register pointer *srcimg;						\
    register int width;							\
    peDefPtr  ped;							\
    mpAABandPtr pvtband;						\
{									\
    pGeomDefPtr	pedpvt = (pGeomDefPtr)ped->elemPvt; 			\
									\
    /* Mapping coefficients */						\
    double a = pedpvt->coeffs[0]; 					\
    double b = pedpvt->coeffs[1];					\
    double c = pedpvt->coeffs[2]; 					\
    double d = pedpvt->coeffs[3];					\
    CARD32 flags = pvtband->flags;					\
									\
    /* These variables describe the limits of the bounding rectangle */	\
    double xmin = pvtband->left_br.xmin;				\
    double ymin = pvtband->left_br.ymin;				\
    double xmax = pvtband->left_br.xmax;				\
    double ymax = pvtband->left_br.ymax;				\
    register int ixmin,iymin,ixmax,iymax;				\
									\
    /* loop variables for roaming through the bounding rectangle */	\
    register int ix,iy;							\
									\
    /* variables which describe available input data (for clipping) */	\
    register int maxpixl = pvtband->in_width - 1;			\
    register int minline = pvtband->lo_src_avail;			\
    register int maxline = pvtband->hi_src_avail;			\
									\
    /* cast of constant, output and input pointers to correct type */	\
    register iotype constant = (iotype) pvtband->CONST;			\
    register iotype *outp = (iotype *) OUTP;				\
    register iotype *ptrIn;						\
									\
    register valtype value;						\
    register int nfound;						\
									\
    inscrutable_compiler(funcname, iotype, valtype, CONST)		\
}

DO_AAGL	(AAGL_R, RealPixel, RealPixel, flt_constant)
DO_AAGL	(AAGL_B, BytePixel, QuadPixel, int_constant)
DO_AAGL	(AAGL_P, PairPixel, QuadPixel, int_constant)
DO_AAGL	(AAGL_Q, QuadPixel, QuadPixel, int_constant)

#if XIE_FULL
/************************************************************************/
/*			Gaussian Routines				*/
/************************************************************************/
/* NOTES:
** this is so slow, we don't try to do a special scale routine.
** should use #include <math.h>?
** should consider using float (e.g. fexp()) but tends to be machine specific.
** should generate pow(2,K) as (1 << floor(k)) + table[fraction(K)].
** (pow(2,K) is half the speed of exp() which defeats the purpose).
** issue of whether to pick points within a radius? or to use a constant
** ...size box of size 2 * radius ?

	(((uv2 = ((ix-xcen)*(ix-xcen)+(iy-ycen)*(iy-ycen))) <= rad2 )	\
		? ((double)(P)) * (simple				\
			?  pow(2.0, sigma * uv2)			\
			: exp(sigma * uv2))				\
		: 0.0 )
*/

extern double exp(), pow();


#define gauss_loop(funcname, iotype, valtype, CONST)			\
									\
	/* loop through all the output pixels on this line */		\
	while ( width-- > 0 ) { 					\
									\
	    iymin = ycen - (radius-1);					\
	    iymax = iymin + (radius+radius-1);				\
	    if (iymin < minline)	iymin = minline;		\
	    if (iymax >= maxline)	iymax = maxline;		\
									\
	    ixmin = xcen - (radius - 1);				\
	    ixmax = ixmin + (radius + radius - 1);			\
	    if (ixmin < 0)		ixmin = 0;			\
	    if (ixmax >= maxpixl)	ixmax = maxpixl;		\
									\
	    nfound = 0;     						\
	    value = 0.;     						\
	    for (iy = iymin; iy <= iymax; ++iy) {			\
		ptrIn = (iotype *) srcimg[iy];  			\
		for (ix = ixmin; ix <= ixmax; ++ix) {			\
		  uv2 = ((ix-xcen)*(ix-xcen)+(iy-ycen)*(iy-ycen));	\
		  value += ((double)(ptrIn[ix])) * (simple		\
				? pow(2.0, sigma * uv2)			\
				: exp(sigma * uv2));			\
		  ++nfound;			  			\
		}				  			\
	    } /* end of iy loop */					\
									\
	    if (nfound) { 						\
		value *= tkpvt->normalize; /* xxx clip */		\
		*outp++ = value < levels ? value : levels;		\
	    } else							\
	        *outp++ = constant;					\
									\
    	    xcen += a;							\
	    ycen += c;							\
	}/* end of line loop */						\
									\
	/* before leaving, update bounding rect for next line */	\
	if (flags & FLG_B_NOT_ZERO) {					\
	   pvtband->left_br.xmin += b;					\
	   pvtband->left_br.xmax += b;					\
	}								\
	if (flags & FLG_D_NOT_ZERO) {					\
	   pvtband->left_br.ymin += d;					\
	   pvtband->left_br.ymax += d;					\
	}								

#define DO_GAGL(funcname, iotype, valtype, CONST)			\
static void								\
funcname (OUTP,srcimg,width,ped,pvtband)				\
    register pointer OUTP;						\
    register pointer *srcimg;						\
    register int width;							\
    peDefPtr  ped;							\
    mpAABandPtr pvtband;						\
{									\
    pGeomDefPtr	pedpvt = (pGeomDefPtr)ped->elemPvt; 			\
									\
    /* Mapping coefficients */						\
    double a = pedpvt->coeffs[0]; 					\
    double b = pedpvt->coeffs[1];					\
    double c = pedpvt->coeffs[2]; 					\
    double d = pedpvt->coeffs[3];					\
    CARD32 flags = pvtband->flags;					\
									\
    register double xcen = pvtband->yOut * b + pedpvt->coeffs[4];	\
    register double ycen = pvtband->yOut * d + pedpvt->coeffs[5];	\
    register int ixmin,iymin,ixmax,iymax;				\
									\
    /* loop variables for roaming through the bounding rectangle */	\
    register int ix,iy;							\
									\
    /* variables which describe available input data (for clipping) */	\
    register int maxpixl = pvtband->in_width - 1;			\
    register int minline = pvtband->lo_src_avail;			\
    register int maxline = pvtband->hi_src_avail;			\
									\
    /* cast of constant, output and input pointers to correct type */	\
    register iotype constant = (iotype) pvtband->CONST;			\
    register iotype *outp = (iotype *) OUTP;				\
    register iotype *ptrIn;						\
									\
    register double value, levels = pvtband->level_clip - 1;		\
    register int nfound;						\
    pTecGeomGaussianDefPtr tkpvt = (pTecGeomGaussianDefPtr)ped->techPvt;\
    int			  simple = tkpvt->simple;			\
    int			  radius = tkpvt->radius;			\
    double		   sigma = (simple ? -1.0 : -0.5) /		\
					(tkpvt->sigma * tkpvt->sigma);	\
    register double	     uv2;					\
									\
    gauss_loop(funcname, iotype, valtype, CONST)			\
}

DO_GAGL	(GAGL_R, RealPixel, RealPixel, flt_constant)
DO_GAGL	(GAGL_B, BytePixel, QuadPixel, int_constant)
DO_GAGL	(GAGL_P, PairPixel, QuadPixel, int_constant)
DO_GAGL	(GAGL_Q, QuadPixel, QuadPixel, int_constant)
#endif

/**********************************************************************/
/* end module mpgeomaa.c */