-
Notifications
You must be signed in to change notification settings - Fork 2
/
aa.c
2157 lines (2118 loc) · 105 KB
/
aa.c
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
/****************************************************************************
*
* Copyright(c) 2002-2009, John Forkosh Associates, Inc. All rights reserved.
* http://www.forkosh.com mailto: [email protected]
* --------------------------------------------------------------------------
* This file is part of mimeTeX, which is free software. You may redistribute
* and/or modify it under the terms of the GNU General Public License,
* version 3 or later, as published by the Free Software Foundation.
* MimeTeX is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY, not even the implied warranty of MERCHANTABILITY.
* See the GNU General Public License for specific details.
* By using mimeTeX, you warrant that you have read, understood and
* agreed to these terms and conditions, and that you possess the legal
* right and ability to enter into this agreement and to use mimeTeX
* in accordance with it.
* Your mimetex.zip distribution file should contain the file COPYING,
* an ascii text copy of the GNU General Public License, version 3.
* If not, point your browser to http://www.gnu.org/licenses/
* or write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
****************************************************************************/
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "mimetex_priv.h"
/* ==========================================================================
* Function: aagridnum ( rp, irow, icol )
* Purpose: calculates gridnum, 0-511 (see Notes below),
* for 3x3 grid centered at irow,icol
* --------------------------------------------------------------------------
* Arguments: rp (I) raster * to raster containing
* bitmap image (to be anti-aliased)
* irow (I) int containing row, 0...height-1,
* at center of 3x3 grid
* icol (I) int containing col, 0...width-1,
* at center of 3x3 grid
* --------------------------------------------------------------------------
* Returns: ( int ) 0-511 grid number, or -1=any error
* --------------------------------------------------------------------------
* Notes: o Input gridnum is a 9-bit int, 0-511, coding a 3x3 pixel grid
* whose bit positions (and corresponding values) in gridnum are
* 876 256 128 64
* 504 = 32 1 16
* 321 8 4 2
* Thus, for example (*=pixel set/black, -=pixel not set/white),
* *-- *-- -** (note that 209 is the
* -*- = 259 *-- = 302 -** = 209 inverse, set<-->unset,
* --* *** --- of 302)
* o A set pixel is considered black, an unset pixel considered
* white.
* ======================================================================= */
/* --- entry point --- */
static int aagridnum(mimetex_ctx *mctx, raster *rp, int irow, int icol)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* local rp->pixmap ptr */
pixbyte *bitmap = rp->pixmap;
int width = rp->width, height = rp->height, /* width, height of raster */
imap = icol + irow * width; /* pixel index = icol + irow*width */
int bitval = 0, /* value of rp bit at irow,icol */
nnbitval = 0, nebitval = 0, eebitval = 0, sebitval = 0, /*adjacent vals*/
ssbitval = 0, swbitval = 0, wwbitval = 0, nwbitval = 0, /*compass pt names*/
gridnum = (-1); /* grid# 0-511 for above 9 bits */
/* ------------------------------------------------------------
check input
------------------------------------------------------------ */
if (irow < 0 || irow >= height /* irow out-of-bounds */
/* icol out-of-bounds */
|| icol < 0 || icol >= width) goto end_of_job;
/* ------------------------------------------------------------
get the 9 bits comprising the 3x3 grid centered at irow,icol
------------------------------------------------------------ */
/* --- get center bit --- */
/* value of rp input bit at imap */
bitval = getlongbit(bitmap, imap);
/* --- get 8 surrounding bits --- */
if (irow > 0) /* nn (north) bit available */
/* nn bit value */
nnbitval = getlongbit(bitmap, imap - width);
if (irow < height - 1) /* ss (south) bit available */
/* ss bit value */
ssbitval = getlongbit(bitmap, imap + width);
if (icol > 0) { /* ww (west) bit available */
/* ww bit value */
wwbitval = getlongbit(bitmap, imap - 1);
if (irow > 0) /* nw bit available */
/* nw bit value */
nwbitval = getlongbit(bitmap, imap - width - 1);
if (irow < height - 1) /* sw bit available */
swbitval = getlongbit(bitmap, imap + width - 1);
} /* sw bit value */
if (icol < width - 1) { /* ee (east) bit available */
/* ee bit value */
eebitval = getlongbit(bitmap, imap + 1);
if (irow > 0) /* ne bit available */
/* ne bit value */
nebitval = getlongbit(bitmap, imap - width + 1);
if (irow < height - 1) /* se bit available */
sebitval = getlongbit(bitmap, imap + width + 1);
} /* se bit value */
/* --- set gridnum --- */
/* clear all bits */
gridnum = 0;
/* set1bit(gridnum,0); */
if (bitval) gridnum = 1;
/* set1bit(gridnum,8); */
if (nwbitval) gridnum += 256;
/* set1bit(gridnum,7); */
if (nnbitval) gridnum += 128;
/* set1bit(gridnum,6); */
if (nebitval) gridnum += 64;
/* set1bit(gridnum,5); */
if (wwbitval) gridnum += 32;
/* set1bit(gridnum,4); */
if (eebitval) gridnum += 16;
/* set1bit(gridnum,3); */
if (swbitval) gridnum += 8;
/* set1bit(gridnum,2); */
if (ssbitval) gridnum += 4;
/* set1bit(gridnum,1); */
if (sebitval) gridnum += 2;
/* ------------------------------------------------------------
Back to caller with gridnum coding 3x3 grid centered at irow,icol
------------------------------------------------------------ */
end_of_job:
return (gridnum);
} /* --- end-of-function aagridnum() --- */
/* ==========================================================================
* Function: aapatternnum ( gridnum )
* Purpose: Looks up the pattern number 1...51
* corresponding to the 3x3 pixel grid coded by gridnum 0=no
* pixels set (white) to 511=all pixels set (black).
* --------------------------------------------------------------------------
* Arguments: gridnum (I) int containing 0-511 coding a 3x3 pixel grid
* (see Notes below)
* --------------------------------------------------------------------------
* Returns: ( int ) 1 to 51, or -1=error
* --------------------------------------------------------------------------
* Notes: o Input gridnum is a 9-bit int, 0-511, coding a 3x3 pixel grid
* whose bit positions (and corresponding values) in gridnum are
* 876 256 128 64
* 504 = 32 1 16
* 321 8 4 2
* Thus, for example (*=pixel set/black, -=pixel not set/white),
* *-- *-- -** (note that 209 is the
* -*- = 259 *-- = 302 -** = 209 inverse, set<-->unset,
* --* *** --- of 302)
* o A set pixel is considered black, an unset pixel considered
* white.
* o Ignoring whether the center pixel is set or unset, and
* taking rotation, reflection and inversion (set<-->unset)
* symmetries into account, there are 32 unique pixel patterns.
* If inversions are listed separately, there are 51 patterns.
* o Here are the 51 unique patterns, with ? always denoting the
* undetermined center pixel. At the upper-left corner of each
* pattern is the "pattern index number" assigned to it in this
* function. At the upper-right is the pattern's multiplicity,
* i.e., the number of different patterns obtained by rotations
* and reflection of the illustrated one. Inverse patters are
* illustrated immediately beneath the original (the first three
* four-pixel patterns have identical inverses).
* -------------------------------------------------------------
* No pixels set:
* #1 1 (in this case, 1 signifies that rotation
* --- and reflection give no different grids)
* -?-
* ---
* Inverse, all eight pixels set
* #2 1 (the inverse multiplicity is always the same)
* ***
* *?*
* ***
* -------------------------------------------------------------
* One pixel set:
* #3 4 #4 4
* *-- -*-
* -?- -?-
* --- ---
* Inverse, seven pixels set:
* #5 4 #6 4
* -** *-*
* *?* *?*
* *** ***
* -------------------------------------------------------------
* Two pixels set:
* #7 8 #8 4 #9 8 10 2 11 4 12 2
* **- *-* *-- *-- -*- -*-
* -?- -?- -?* -?- -?* -?-
* --- --- --- --* --- -*-
* Inverse, six pixels set:
* #13 8 14 4 15 8 16 2 17 4 18 2
* --* -*- -** -** *-* *-*
* *?* *?* *?- *?* *?- *?*
* *** *** *** **- *** *-*
* -------------------------------------------------------------
* Three pixels set:
* #19 4 20 8 21 8 22 8 23 8 24 4 25 4 26 4 27 4 28 4
* *** **- **- **- **- **- *-* *-* -*- -*-
* -?- -?* -?- -?- -?- *?- -?- -?- -?* -?*
* --- --- --* -*- *-- --- --* -*- -*- *--
* Inverse, five pixels set:
* #29 4 30 8 31 8 32 8 33 8 34 4 35 4 36 4 37 4 38 4
* --- --* --* --* --* --* -*- -*- *-* *-*
* *?* *?- *?* *?* *?* -?* *?* *?* *?- *?-
* *** *** **- *-* -** *** **- *-* *-* -**
* -------------------------------------------------------------
* Four pixels set (including inverses):
* #39 8 40 4 41 8 42 8 43 4 44 4 45 8 46 1
* *** **- **- *** *** **- **- *-*
* -?* -?- -?* -?- -?- -?* -?* -?-
* --- -** *-- --* -*- --* -*- *-*
*
* #47 8 48 4 49 4 50 8 51 1
* --- --- --* --* -*-
* *?* *?* *?- *?- *?*
* **- *-* **- *-* -*-
* ======================================================================= */
/* --- entry point --- */
static int aapatternnum(mimetex_ctx *mctx, int gridnum)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/*pattern#, 1-51, for input gridnum*/
int pattern = (-1);
/* ---
* pattern number corresponding to input gridnum/2 code
* ( gridnum/2 strips off gridnum's low bit because it's
* the same pattern whether or not center pixel is set )
* --- */
static int patternnum[] = {
1, 3, 4, 7, 3, 8, 7, 19, 4, 7, 11, 24, 9, 23, 20, 39, /* 0- 15 */
4, 9, 11, 20, 7, 23, 24, 39, 12, 22, 27, 47, 22, 48, 47, 29, /* 16- 31 */
3, 8, 9, 23, 10, 25, 21, 42, 7, 19, 20, 39, 21, 42, 44, 34, /* 32- 47 */
9, 26, 28, 41, 21, 50, 49, 30, 22, 43, 45, 33, 40, 32, 31, 13, /* 48- 63 */
4, 9, 12, 22, 9, 26, 22, 43, 11, 20, 27, 47, 28, 41, 45, 33, /* 64- 79 */
11, 28, 27, 45, 20, 41, 47, 33, 27, 45, 51, 35, 45, 36, 35, 14, /* 80- 95 */
7, 23, 22, 48, 21, 50, 40, 32, 24, 39, 47, 29, 49, 30, 31, 13, /* 96-111 */
20, 41, 45, 36, 44, 38, 31, 15, 47, 33, 35, 14, 31, 15, 16, 5, /* 112-127 */
3, 10, 9, 21, 8, 25, 23, 42, 9, 21, 28, 49, 26, 50, 41, 30, /* 128-143 */
7, 21, 20, 44, 19, 42, 39, 34, 22, 40, 45, 31, 43, 32, 33, 13, /* 144-159 */
8, 25, 26, 50, 25, 46, 50, 37, 23, 42, 41, 30, 50, 37, 38, 17, /* 160-175 */
23, 50, 41, 38, 42, 37, 30, 17, 48, 32, 36, 15, 32, 18, 15, 6, /* 176-191 */
7, 21, 22, 40, 23, 50, 48, 32, 20, 44, 45, 31, 41, 38, 36, 15, /* 192-207 */
24, 49, 47, 31, 39, 30, 29, 13, 47, 31, 35, 16, 33, 15, 14, 5, /* 208-223 */
19, 42, 43, 32, 42, 37, 32, 18, 39, 34, 33, 13, 30, 17, 15, 6, /* 224-239 */
39, 30, 33, 15, 34, 17, 13, 6, 29, 13, 14, 5, 13, 6, 5, 2, /* 240-255 */
-1
}; /* --- end-of-patternnum[] --- */
/* ------------------------------------------------------------
look up pattern number for gridnum
------------------------------------------------------------ */
/* --- first check input --- */
/* gridnum out of bounds */
if (gridnum < 0 || gridnum > 511) goto end_of_job;
/* --- look up pattern number, 1-51, corresponding to input gridnum --- */
pattern = patternnum[gridnum/2]; /* /2 strips off gridnum's low bit */
/* some internal error */
if (pattern < 1 || pattern > 51) pattern = (-1);
end_of_job:
/* back to caller with pattern# */
return (pattern);
} /* --- end-of-function aapatternnum() --- */
/* ==========================================================================
* Function: aafollowline ( rp, irow, icol, direction )
* Purpose: starting with pixel at irow,icol, moves in
* specified direction looking for a "turn"
* --------------------------------------------------------------------------
* Arguments: rp (I) raster * to raster containing pixel image
* irow (I) int containing row, 0...height-1,
* of first pixel
* icol (I) int containing col, 0...width-1,
* of first pixel
* direction (I) int containing +1 to follow line up/north
* (decreasing irow), -1 to follow line
* down/south (increasing irow), +2 to follow
* line right/east (increasing icol),
* -2 to follow line left/west (decreasing icol)
* --------------------------------------------------------------------------
* Returns: ( int ) #rows or #cols traversed prior to turn,
* or 0 if no turn detected (or for any error).
* Sign is + if turn direction is right/east or
* up/north, or is - if left/west or down/south.
* --------------------------------------------------------------------------
* Notes: o Here are some examples illustrating turn detection in
* +2 (right/east) direction. Turns in other directions
* are detected similarly/symmetrically. * denotes black
* bits (usually fg), - denotes white bits (usually bg),
* and ? denotes "don't care" bit (won't affect outcome).
* Arrow --> points to start pixel denoted by irow,icol.
*
* *??? -??? turn=0 (no turn) is returned
* -->*??? or -->-??? because the start pixel isn't
* *??? -??? on an edge to begin with
*
* ---- **-- turn=0 returned because the
* -->***- or -->***- line ends abruptly without
* ---- ---- turning (even the second case)
*
* ---* ---* turn=0 returned because the
* -->***- or -->**** line forms a Y or T rather
* ---* ---* than turning
*
* ***- **** turn=+3 returned
* -->***- or -->***- (outside corner)
* ---- ----
*
* ***** ****- turn=-4 returned
* -->***** or -->****- (inside corner)
* ----* ----*
*
* ----* ----* turn=+4 returned
* -->****- or -->***** (outside or inside corner)
* ----- -----
* ======================================================================= */
/* --- entry point --- */
static int aafollowline(mimetex_ctx *mctx, raster *rp, int irow, int icol, int direction)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* local rp->pixmap ptr */
pixbyte *bitmap = rp->pixmap;
/* width, height of raster */
int width = rp->width, height = rp->height;
int drow = 0, dcol = 0, /* delta row,col to follow line */
jrow = irow, jcol = icol; /* current row,col following line */
int bitval = 1, /* value of rp bit at irow,icol */
fgval = 1, bgval = 0, /* "fg" is whatever bitval is */
bitminus = 0, bitplus = 0; /* value of left/down, right/up bit*/
/*isline signals one-pixel wide line*/
int isline = 1, isedge = 0;
int turn = 0, /* detected turn back to caller */
maxturn = mctx->maxfollow; /* don't follow more than max pixels*/
/* ------------------------------------------------------------
Initialization
------------------------------------------------------------ */
/* --- check input --- */
if (irow < 0 || irow >= height /* irow out-of-bounds */
/* icol out-of-bounds */
|| icol < 0 || icol >= width) goto end_of_job;
/* --- starting bit -- see if we're following a fg (usual), or bg line --- */
/* starting pixel (bg or fg)*/
bitval = getlongbit(bitmap, (icol + irow * width));
fgval = bitval;
/* define "fg" as whatever bitval is*/
bgval = (1 - bitval);
/* --- set drow,dcol corresponding to desired direction --- */
switch (direction) { /* determine drow,dcol for direction*/
default:
/* unrecognized direction arg */
goto end_of_job;
case 1:
drow = (-1);
break;
/* follow line up/north */
case -1:
drow = 1;
break;
/* down/south */
case 2:
dcol = 1;
break;
/* right/east */
case -2:
dcol = (-1);
break;
} /* left/west */
/* --- set bitminus and bitplus --- */
if (drow == 0) { /* we're following line right/left */
if (irow < height) /* there's a pixel below current */
/* get it */
bitminus = getlongbit(bitmap, (icol + (irow + 1) * width));
if (irow > 0) /* there's a pixel above current */
bitplus = getlongbit(bitmap, (icol + (irow - 1) * width));
} /* get it */
if (dcol == 0) { /* we're following line up/down */
if (icol < width) /* there's a pixel to the right */
/* get it */
bitplus = getlongbit(bitmap, (icol + 1 + irow * width));
if (icol > 0) /* there's a pixel to the left */
bitminus = getlongbit(bitmap, (icol - 1 + irow * width));
} /* get it */
/* --- check for lack of line to follow --- */
if (bitval == bitplus /* starting pixel same as above */
&& bitval == bitminus) /* and below (or right and left) */
/* so there's no line to follow */
goto end_of_job;
/* --- set isline and isedge (already initted for isline) --- */
if (bitval == bitplus) { /* starting pixel same as above */
/* so we're at an edge below */
isedge = (-1);
isline = 0;
}
if (bitval == bitminus) { /* starting pixel same as below */
/* so we're at an edge above */
isedge = 1;
isline = 0;
}
/* ------------------------------------------------------------
follow line
------------------------------------------------------------ */
while (1) { /* until turn found (or max) */
/* --- local allocations and declarations --- */
int dbitval = 0, /* value of bit at jrow,jcol */
dbitminus = 0, dbitplus = 0; /* value of left/down, right/up bit*/
/* --- bump pixel count and indexes; check for max or end-of-raster --- */
/* bump #pixels followed */
turn++;
jrow += drow;
/* indexes of next pixel to check */
jcol += dcol;
if (turn > maxturn /* already followed max #pixels */
|| jrow < 0 || jrow >= height /* or jrow past end-of-raster */
|| jcol < 0 || jcol >= width) { /* or jcol past end-of-raster */
/* so quit without finding a turn */
turn = 0;
goto end_of_job;
}
/* --- set current bit (dbitval) --- */
/*value of jrow,jcol bit*/
dbitval = getlongbit(bitmap, (jcol + jrow * width));
/* --- set dbitminus and dbitplus --- */
if (drow == 0) { /* we're following line right/left */
if (irow < height) /* there's a pixel below current */
/* get it */
dbitminus = getlongbit(bitmap, (jcol + (irow + 1) * width));
if (irow > 0) /* there's a pixel above current */
dbitplus = getlongbit(bitmap, (jcol + (irow - 1) * width));
} /* get it */
if (dcol == 0) { /* we're following line up/down */
if (icol < width) /* there's a pixel to the right */
/* get it */
dbitplus = getlongbit(bitmap, (icol + 1 + jrow * width));
if (icol > 0) /* there's a pixel to the left */
dbitminus = getlongbit(bitmap, (icol - 1 + jrow * width));
} /* get it */
/* --- first check for abrupt end-of-line, or for T or Y --- */
if (isline != 0) /* abrupt end or T,Y must be a line*/
if ((bgval == dbitval /* end-of-line if pixel flips to bg*/
&& bgval == dbitplus /* and bg same as above pixel */
&& bgval == dbitminus) /* and below (or right and left) */
|| (fgval == dbitplus /* T or Y if fg same as above pixel*/
&& fgval == dbitminus)) { /* and below (or right and left) */
/* so we're at a T or Y */
turn = 0;
goto end_of_job;
}
/* --- check for turning line --- */
if (isline != 0) { /* turning line must be a line */
if (fgval == dbitminus) { /* turning down */
/* so return negative turn */
turn = -turn;
goto end_of_job;
} else if (fgval == dbitplus) /* turning up */
goto end_of_job;
} /* so return positive turn */
/* --- check for inside corner at edge --- */
if (isedge != 0) { /* inside corner must be a edge */
if (isedge < 0 && fgval == bitminus) { /* corner below */
/* so return negative turn */
turn = -turn;
goto end_of_job;
}
if (isedge > 0 && fgval == bitplus) /* corner above */
goto end_of_job;
} /* so return positive turn */
/* --- check for abrupt end at edge --- */
if (isedge != 0 /* abrupt edge end must be an edge */
&& fgval == dbitval) /* and line must not end */
if ((isedge < 0 && bgval == bitplus) /* abrupt end above */
|| (isedge > 0 && bgval == bitminus)) { /* or abrupt end below */
/* so edge ended abruptly */
turn = 0;
goto end_of_job;
}
/* --- check for outside corner at edge --- */
if (isedge != 0 /* outside corner must be a edge */
&& bgval == dbitval) { /* and line must end */
/* outside turn down from edge above*/
if (isedge > 0) turn = -turn;
goto end_of_job;
}
} /* --- end-of-while(1) --- */
/* ------------------------------------------------------------
Back to caller with #rows or #cols traversed, and direction of detected turn
------------------------------------------------------------ */
end_of_job:
if (mctx->msglevel >= 99 && mctx->msgfp != NULL) /* debugging/diagnostic output */
fprintf(mctx->msgfp, "aafollowline> irow,icol,direction=%d,%d,%d, turn=%d\n",
irow, icol, direction, turn);
return (turn);
} /* --- end-of-function aafollowline() --- */
/* ==========================================================================
* Function: aapattern1124 ( rp, irow, icol, gridnum, grayscale )
* Purpose: calculates anti-aliased value for pixel at irow,icol,
* whose surrounding 3x3 pixel grid is coded by gridnum
* (which must correspond to pattern #11 or #24).
* --------------------------------------------------------------------------
* Arguments: rp (I) raster * to raster whose bitmap
* is to be anti-aliased
* irow (I) int containing row, 0...height-1,
* of pixel to be antialiased
* icol (I) int containing col, 0...width-1,
* of pixel to be antialiased
* gridnum (I) int containing 0...511 corresponding to
* 3x3 pixel grid surrounding irow,icol
* grayscale (I) int containing number of grayscales
* to be calculated, 0...grayscale-1
* (should typically be given as 256)
* --------------------------------------------------------------------------
* Returns: ( int ) 0...grayscale-1 for success, -1=any error
* --------------------------------------------------------------------------
* Notes: o Handles the eight gridnum's
* (gridnum/2 shown to eliminate irrelevant low-order bit)
* --- --- -*- -*-
* --* = 10 *-- = 18 --* = 72 *-- = 80 (pattern$11)
* -*- -*- --- ---
*
* --- --- -** **-
* --* = 11 *-- = 22 --* = 104 *-- = 208 (pattern$24)
* -** **- --- ---
* o For black * center pixel, using grid#10 as an example,
* pixel stays --- antialiased ---*
* black if -*** if part of -**
* part of a -*- a diagonal -*-
* corner, eg, * line, eg, *
* ======================================================================= */
/* --- entry point --- */
static int aapattern1124(mimetex_ctx *mctx, raster *rp, int irow, int icol,
int gridnum, int grayscale)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* antialiased value returned */
int aaval = (-1);
/* true if pixel at irow,icol black*/
int iscenter = gridnum & 1;
/* init for pattern#24 default */
int patternum = 24;
/* local rp->pixmap ptr */
pixbyte *bitmap = rp->pixmap;
/* width, height of raster */
int width = rp->width, height = rp->height;
/* corner or diagonal row,col */
int jrow = irow, jcol = icol;
int vertcornval = 0, horzcornval = 0, /* vertical, horizontal corner bits*/
topdiagval = 0, botdiagval = 0, /* upper,lower diagonal pixel bits */
cornval = 0, diagval = 0; /* vert+horzcorn, top+botdiag */
int hdirection = 99, vdirection = 99, /* horz,vert corner direction */
hturn = 99, vturn = 99; /* follow corner till turns */
/* ------------------------------------------------------------
Check corner and diagonal pixels
------------------------------------------------------------ */
/* true to turn off pattern1124 */
if (0) goto end_of_job;
switch (gridnum / 2) {
/* check pattern#11,24 corner, diag*/
default:
/* not a pattern#11,24 gridnum */
goto end_of_job;
case 10:
patternum = 11;
case 11:
hdirection = 2;
/* directions to follow corner */
vdirection = -1;
if ((jrow = irow + 2) < height) { /* vert corner below center pixel */
vertcornval = getlongbit(bitmap, (icol + jrow * width));
if ((icol - 1) >= 0) /* lower diag left of center */
botdiagval = getlongbit(bitmap, ((icol - 1) + jrow * width));
}
if ((jcol = icol + 2) < width) { /* horz corner right of center */
horzcornval = getlongbit(bitmap, (jcol + irow * width));
if ((irow - 1) >= 0) /* upper diag above center */
topdiagval = getlongbit(bitmap, (jcol + (irow - 1) * width));
}
break;
case 18:
patternum = 11;
case 22:
hdirection = -2;
/* directions to follow corner */
vdirection = -1;
if ((jrow = irow + 2) < height) { /* vert corner below center pixel */
vertcornval = getlongbit(bitmap, (icol + jrow * width));
if ((icol + 1) < width) /* lower diag right of center */
botdiagval = getlongbit(bitmap, ((icol + 1) + jrow * width));
}
if ((jcol = icol - 2) >= 0) { /* horz corner left of center */
horzcornval = getlongbit(bitmap, (jcol + irow * width));
if ((irow - 1) >= 0) /* upper diag above center */
topdiagval = getlongbit(bitmap, (jcol + (irow - 1) * width));
}
break;
case 72:
patternum = 11;
case 104:
hdirection = 2;
/* directions to follow corner */
vdirection = 1;
if ((jrow = irow - 2) >= 0) { /* vert corner above center pixel */
vertcornval = getlongbit(bitmap, (icol + jrow * width));
if ((icol - 1) >= 0) /* upper diag left of center */
topdiagval = getlongbit(bitmap, ((icol - 1) + jrow * width));
}
if ((jcol = icol + 2) < width) { /* horz corner right of center */
horzcornval = getlongbit(bitmap, (jcol + irow * width));
if ((irow + 1) < height) /* lower diag below center */
botdiagval = getlongbit(bitmap, (jcol + (irow + 1) * width));
}
break;
case 80:
patternum = 11;
case 208:
hdirection = -2;
/* directions to follow corner */
vdirection = 1;
if ((jrow = irow - 2) >= 0) { /* vert corner above center pixel */
vertcornval = getlongbit(bitmap, (icol + jrow * width));
if ((icol + 1) < width) /* upper diag right of center */
topdiagval = getlongbit(bitmap, ((icol + 1) + jrow * width));
}
if ((jcol = icol - 2) >= 0) { /* horz corner left of center */
horzcornval = getlongbit(bitmap, (jcol + irow * width));
if ((irow + 1) < height) /* lower diag below center */
botdiagval = getlongbit(bitmap, (jcol + (irow + 1) * width));
}
break;
} /* --- end-of-switch(gridnum/2) --- */
/* 0=no corner bits, 1, 2=both */
cornval = vertcornval + horzcornval;
/* 0=no diag bits, 1, 2=both */
diagval = topdiagval + botdiagval;
/* ------------------------------------------------------------
Handle white center
------------------------------------------------------------ */
if (1 && !iscenter) {
aaval = (patternum == 11 ? 51 : 64);
goto end_of_job;
}
/* ------------------------------------------------------------
Handle black center
------------------------------------------------------------ */
if (diagval > 1) aaval = (patternum == 24 ? 255 : 191);
else {
hturn = aafollowline(mctx, rp, irow, icol, hdirection);
vturn = aafollowline(mctx, rp, irow, icol, vdirection);
if (vturn*hdirection < 0 && hturn*vdirection < 0)
aaval = (patternum == 24 ? 255 : 191);
else aaval = grayscale - 1;
} /* actual corner */
/* ------------------------------------------------------------
Back to caller with grayscale antialiased value for pixel at irow,icol
------------------------------------------------------------ */
end_of_job:
if (aaval >= 0) /* have antialiasing result */
if (mctx->msglevel >= 99 && mctx->msgfp != NULL) fprintf(mctx->msgfp, /* diagnostic output */
"aapattern1124> irow,icol,grid#/2=%d,%d,%d, top,botdiag=%d,%d, "
"vert,horzcorn=%d,%d, v,hdir=%d,%d, v,hturn=%d,%d, aaval=%d\n",
irow, icol, gridnum / 2, topdiagval, botdiagval, vertcornval, horzcornval,
vdirection, hdirection, vturn, hturn, aaval);
/* back with antialiased value */
return (aaval);
} /* --- end-of-function aapattern1124() --- */
/* ==========================================================================
* Function: aapattern19 ( rp, irow, icol, gridnum, grayscale )
* Purpose: calculates anti-aliased value for pixel at irow,icol,
* whose surrounding 3x3 pixel grid is coded by gridnum
* (which must correspond to pattern #19).
* --------------------------------------------------------------------------
* Arguments: rp (I) raster * to raster whose bitmap
* is to be anti-aliased
* irow (I) int containing row, 0...height-1,
* of pixel to be antialiased
* icol (I) int containing col, 0...width-1,
* of pixel to be antialiased
* gridnum (I) int containing 0...511 corresponding to
* 3x3 pixel grid surrounding irow,icol
* grayscale (I) int containing number of grayscales
* to be calculated, 0...grayscale-1
* (should typically be given as 256)
* --------------------------------------------------------------------------
* Returns: ( int ) 0...grayscale-1 for success, -1=any error
* --------------------------------------------------------------------------
* Notes: o Handles the four gridnum's
* (gridnum/2 shown to eliminate irrelevant low-order bit)
* --- --* *-- ***
* --- = 7 --* = 41 *-- = 148 --- = 224
* *** --* *-- ---
* ======================================================================= */
/* --- entry point --- */
static int aapattern19(mimetex_ctx *mctx, raster *rp, int irow, int icol,
int gridnum, int grayscale)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* antialiased value returned */
int aaval = (-1);
/* true if pixel at irow,icol black*/
int iscenter = gridnum & 1;
int orientation = 1, /* 1=vertical, 2=horizontal */
jrow = irow, jcol = icol; /* middle pixel of *** line */
/* follow *** line till it turns */
int turn1 = 0, turn2 = 0;
/* ------------------------------------------------------------
Initialization and calculation of antialiased value
------------------------------------------------------------ */
/* --- check input -- */
/* we only antialias white pixels */
if (iscenter) goto end_of_job;
/* --- set params --- */
switch (gridnum / 2) { /* check pattern#19 orientation */
default:
/* not a pattern#19 gridnum */
goto end_of_job;
case 7:
orientation = 2;
jrow++;
break;
case 41:
orientation = 1;
jcol++;
break;
case 148:
orientation = 1;
jcol--;
break;
case 224:
orientation = 2;
jrow--;
break;
} /* --- end-of-switch(gridnum/2) --- */
/* --- get turns in both directions --- */
if ((turn1 = aafollowline(mctx, rp, jrow, jcol, orientation)) == 0) goto end_of_job;
if ((turn2 = aafollowline(mctx, rp, jrow, jcol, -orientation)) == 0) goto end_of_job;
/* both turns in same direction */
if (turn1*turn2 >= 0) goto end_of_job;
/* --- weight pixel --- */
aaval = grayscale / (3 + min2(abs(turn1), abs(turn2)));
/* ------------------------------------------------------------
Back to caller with grayscale antialiased value for pixel at irow,icol
------------------------------------------------------------ */
end_of_job:
if (aaval >= 0) /* have antialiasing result */
if (mctx->msglevel >= 99 && mctx->msgfp != NULL) fprintf(mctx->msgfp, /* diagnostic output */
"aapattern19> irow,icol,grid#/2=%d,%d,%d, turn+%d,%d=%d,%d, aaval=%d\n",
irow, icol, gridnum / 2, orientation, -orientation, turn1, turn2, aaval);
/* back with antialiased value */
return (aaval);
} /* --- end-of-function aapattern19() --- */
/* ==========================================================================
* Function: aapattern20 ( rp, irow, icol, gridnum, grayscale )
* Purpose: calculates anti-aliased value for pixel at irow,icol,
* whose surrounding 3x3 pixel grid is coded by gridnum
* (which must correspond to pattern #20).
* --------------------------------------------------------------------------
* Arguments: rp (I) raster * to raster whose bitmap
* is to be anti-aliased
* irow (I) int containing row, 0...height-1,
* of pixel to be antialiased
* icol (I) int containing col, 0...width-1,
* of pixel to be antialiased
* gridnum (I) int containing 0...511 corresponding to
* 3x3 pixel grid surrounding irow,icol
* grayscale (I) int containing number of grayscales
* to be calculated, 0...grayscale-1
* (should typically be given as 256)
* --------------------------------------------------------------------------
* Returns: ( int ) 0...grayscale-1 for success, -1=any error
* --------------------------------------------------------------------------
* Notes: o Handles the eight gridnum's
* (gridnum/2 shown to eliminate irrelevant low-order bit)
* --- --- --* -*-
* --* = 14 *-- = 19 --* = 42 --* = 73
* **- -** -*- --*
*
* -*- -** *-- **-
* *-- = 84 *-- = 112 *-- = 146 --* = 200
* *-- --- -*- ---
* ======================================================================= */
/* --- entry point --- */
static int aapattern20(mimetex_ctx *mctx, raster *rp, int irow, int icol,
int gridnum, int grayscale)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* antialiased value returned */
int aaval = (-1);
/* true if pixel at irow,icol black*/
int iscenter = gridnum & 1;
int direction = 1, /* direction to follow ** line */
jrow1 = irow, jcol1 = icol, /* coords of * */
jrow2 = irow, jcol2 = icol; /* coords of adjacent ** pixel */
/* follow *,** lines till turns */
int turn1 = 0, turn2 = 0;
/* ------------------------------------------------------------
Initialization and calculation of antialiased value
------------------------------------------------------------ */
/* --- check input -- */
/* don't want this one */
if (1) goto end_of_job;
/* we only antialias white pixels */
if (iscenter) goto end_of_job;
/* --- set params --- */
switch (gridnum / 2) { /* check pattern#20 orientation */
default:
/* not a pattern#20 gridnum */
goto end_of_job;
case 14:
direction = -2;
jcol1++;
jrow2++;
break;
case 19:
direction = 2;
jcol1--;
jrow2++;
break;
case 42:
direction = 1;
jrow1++;
jcol2++;
break;
case 73:
direction = -1;
jrow1--;
jcol2++;
break;
case 84:
direction = -1;
jrow1--;
jcol2--;
break;
case 112:
direction = 2;
jcol1--;
jrow2--;
break;
case 146:
direction = 1;
jrow1++;
jcol2--;
break;
case 200:
direction = -2;
jcol1++;
jrow2--;
break;
} /* --- end-of-switch(gridnum/2) --- */
/* --- get turns in both directions --- */
if ((turn1 = aafollowline(mctx, rp, jrow1, jcol1, -direction)) == 0) goto end_of_job;
if ((turn2 = aafollowline(mctx, rp, jrow2, jcol2, direction)) == 0) goto end_of_job;
/* both turns in same direction */
if (turn1*turn2 >= 0) goto end_of_job;
/* --- weight pixel --- */
aaval = grayscale / (3 + min2(abs(turn1), abs(turn2)));
/* ------------------------------------------------------------
Back to caller with grayscale antialiased value for pixel at irow,icol
------------------------------------------------------------ */
end_of_job:
if (aaval >= 0) /* have antialiasing result */
if (mctx->msglevel >= 99 && mctx->msgfp != NULL) fprintf(mctx->msgfp, /* diagnostic output */
"aapattern20> irow,icol,grid#/2=%d,%d,%d, turn%d,%d=%d,%d, aaval=%d\n",
irow, icol, gridnum / 2, -direction, direction, turn1, turn2, aaval);
/* back with antialiased value */
return (aaval);
} /* --- end-of-function aapattern20() --- */
/* ==========================================================================
* Function: aapattern39 ( rp, irow, icol, gridnum, grayscale )
* Purpose: calculates anti-aliased value for pixel at irow,icol,
* whose surrounding 3x3 pixel grid is coded by gridnum
* (which must correspond to pattern #39).
* --------------------------------------------------------------------------
* Arguments: rp (I) raster * to raster whose bitmap
* is to be anti-aliased
* irow (I) int containing row, 0...height-1,
* of pixel to be antialiased
* icol (I) int containing col, 0...width-1,
* of pixel to be antialiased
* gridnum (I) int containing 0...511 corresponding to
* 3x3 pixel grid surrounding irow,icol
* grayscale (I) int containing number of grayscales
* to be calculated, 0...grayscale-1
* (should typically be given as 256)
* --------------------------------------------------------------------------
* Returns: ( int ) 0...grayscale-1 for success, -1=any error
* --------------------------------------------------------------------------
* Notes: o Handles the eight gridnum's
* (gridnum/2 shown to eliminate irrelevant low-order bit)
* --- --- --* -**
* --* = 15 *-- = 23 --* = 43 --* = 105
* *** *** -** --*
*
* **- *** *-- ***
* *-- = 212 *-- = 240 *-- = 150 --* = 232
* *-- --- **- ---
* ======================================================================= */
/* --- entry point --- */
static int aapattern39(mimetex_ctx *mctx, raster *rp, int irow, int icol,
int gridnum, int grayscale)
{
/* ------------------------------------------------------------
Allocations and Declarations
------------------------------------------------------------ */
/* antialiased value returned */
int aaval = (-1);
/* true if pixel at irow,icol black*/
int iscenter = gridnum & 1;
int direction = 1, /* direction to follow ** line */
jrow1 = irow, jcol1 = icol, /* coords of * */
jrow2 = irow, jcol2 = icol; /* coords of adjacent ** pixel */
/* follow *,** lines till turns */
int turn1 = 0, turn2 = 0;
/* ------------------------------------------------------------
Initialization and calculation of antialiased value
------------------------------------------------------------ */
/* --- check input -- */
/* we only antialias white pixels */
if (iscenter) goto end_of_job;
/* --- set params --- */
switch (gridnum / 2) { /* check pattern#39 orientation */
default:
/* not a pattern#39 gridnum */
goto end_of_job;
case 15:
direction = -2;
jcol1++;
jrow2++;
break;
case 23:
direction = 2;
jcol1--;
jrow2++;
break;
case 43:
direction = 1;
jrow1++;
jcol2++;
break;
case 105:
direction = -1;
jrow1--;
jcol2++;
break;
case 212:
direction = -1;
jrow1--;
jcol2--;
break;
case 240:
direction = 2;
jcol1--;
jrow2--;
break;
case 150:
direction = 1;
jrow1++;
jcol2--;
break;
case 232:
direction = -2;
jcol1++;
jrow2--;
break;
} /* --- end-of-switch(gridnum/2) --- */
/* --- get turns directions (tunr1==1 signals inside corner) --- */
if ((turn1 = aafollowline(mctx, rp, jrow1, jcol1, -direction)) == 1) {
aaval = 0;
goto end_of_job;
}
/* stop here for now */
if (1) goto end_of_job;
if ((turn2 = aafollowline(mctx, rp, jrow2, jcol2, direction)) == 0) goto end_of_job;
/* both turns in same direction */
if (turn1*turn2 >= 0) goto end_of_job;
/* --- weight pixel --- */
aaval = grayscale / (3 + min2(abs(turn1), abs(turn2)));
/* ------------------------------------------------------------