aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/imfit/src/imsurfit.x
blob: 9f655f52e7fd95c0653d974c03dd4769a0e8f657 (plain) (blame)
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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include <mach.h>
include <imhdr.h>
include <math/surfit.h>
include "imsurfit.h"

# IMSURFIT -- Procedure to fit a surface to a single image including
# optional pixel rejection.

procedure imsurfit (imin, imout, imfit, gl)

pointer	imin	# pointer to the input image
pointer	imout	# pointer to the output image
pointer	imfit	# pointer to the imsurfit parameters
pointer	gl	# pointer to the good regions list

pointer	sf, rl
errchk	isfree, prl_free
errchk	all_pixels, good_pixels, good_median, all_medians, do_reject
errchk	set_outimage

begin
	sf = NULL
	rl = NULL

	# Accumulate and solve the surface.
	if (gl == NULL) {
	    if (MEDIAN(imfit) == NO)
	        call all_pixels (imin, imfit, sf)
	    else
		call all_medians (imin, imfit, sf)
	} else {
	    if (MEDIAN(imfit) == NO)
	        call good_pixels (imin, imfit, gl, sf)
	    else
		call good_medians (imin, imfit, gl, sf)
	}

	# Perform the reject cycle.
	if (REJECT(imfit) == YES || TYPE_OUTPUT(imfit) == CLEAN)
	    call do_reject (imin, imfit, gl, sf, rl)

	# Evaluate surface for appropriate output type.
	call set_outimage (imin, imout, imfit, sf, rl)

	# Cleanup.
	call prl_free (rl)
	call isfree (sf)

	rl = NULL
	sf = NULL
end


# ALL_PIXELS -- Accumulate surface when there are no bad regions
# and no median processing.

procedure all_pixels (im, imfit, sf)

pointer	im	# pointer to the input image
pointer	imfit	# pointer to the imsurfit structure
pointer	sf	# pointer to the surface descriptor

int	i, lp, ncols, nlines, ier
long	v[IM_MAXDIM]
pointer	sp, cols, lines, wgt, lbuf
int	imgnlr()
errchk	smark, salloc, sfree, imgnlr
errchk isinit, islfit, islrefit, issolve

begin
	ncols = IM_LEN(im, 1)
	nlines = IM_LEN(im,2)

	# Initialize the surface fit.
	call isinit (sf, SURFACE_TYPE(imfit), XORDER(imfit), YORDER(imfit),
	    CROSS_TERMS(imfit), ncols, nlines) 

	# Allocate working space for fitting.
	call smark (sp)
	call salloc (cols, ncols, TY_INT)
	call salloc (lines, nlines, TY_INT)
	call salloc (wgt, ncols, TY_REAL)

	# Initialize the x and weight buffers.
	do i = 1, ncols
	    Memi[cols - 1 + i] = i
	 call amovkr (1.0, Memr[wgt], ncols)

	# Loop over image lines.
	lp = 0
	call amovkl (long (1), v, IM_MAXDIM)
	do i = 1, nlines {

	    # Read in the image line.
	    if (imgnlr (im, lbuf, v) == EOF)
		call error (0, "Error reading image")

	    # Fit each image line.
	    if (i == 1)
	        call islfit (sf, Memi[cols], i, Memr[lbuf], Memr[wgt],
		ncols, SF_USER, ier)
	    else
		call islrefit (sf, Memi[cols], i, Memr[lbuf], Memr[wgt])

	    # Handle fitting errors.
	    switch (ier) {
	    case NO_DEG_FREEDOM:
		call eprintf ("Warning: Too few columns to fit line: %d\n")
		    call pargi (i)
	    case SINGULAR:
		call eprintf ("Warning: Solution singular for line: %d\n")
		    call pargi (i)
		Memi[lines + lp] = i
		lp = lp + 1
	    default:
		Memi[lines + lp] = i
		lp = lp + 1
	    }

	}

	# Solve the surface.
	call issolve (sf, Memi[lines], lp, ier)

	# Handle fitting errors.
	switch (ier) {
	case NO_DEG_FREEDOM:
	    call error (0, "ALL_PIXELS: Cannot fit surface.")
	case SINGULAR:
	    call eprintf ("Warning: Solution singular for surface.\n")
	default:
	    # everything OK
	}

	# Free space.
	call sfree (sp)
end


# GOOD_PIXELS -- Get surface when good regions are defined and median
# processing is off.

procedure good_pixels (im, imfit, gl, sf)

pointer	im	# input image
pointer	imfit	# pointer to imsurfit header structure
pointer	gl	# pointer to good region list
pointer	sf	# pointer to the surface descriptor

int	lp, lineno, prevlineno, ncols, nlines, npts, nranges, ier, ijunk
int	max_nranges
pointer	sp, colsfit, lines, buf, fbuf, wgt, ranges
int	prl_nextlineno(), prl_eqlines(), prl_get_ranges(), is_expand_ranges()
int	is_choose_rangesr()
pointer	imgl2r()

errchk	smark, salloc, sfree, imgl2r
errchk	isinit, islfit, islrefit, issolve
errchk	prl_nextlineno, prl_eqlines, prl_get_ranges
errchk	is_choose_rangesr

begin
	ncols = IM_LEN(im,1)
	nlines = IM_LEN(im,2)
	max_nranges = ncols

	# Initialize the surface fit.
	call isinit (sf, SURFACE_TYPE(imfit), XORDER(imfit), YORDER(imfit),
	    CROSS_TERMS(imfit), ncols, nlines)

	# Allocate temporary space for fitting.
	call smark (sp)
	call salloc (colsfit, ncols, TY_INT)
	call salloc (lines, nlines, TY_INT)
	call salloc (fbuf, ncols, TY_REAL)
	call salloc (wgt, ncols, TY_REAL)
	call salloc (ranges, 3 * max_nranges + 1, TY_INT)
	call amovkr (1., Memr[wgt], ncols)

	# Intialize counters and pointers.
	lp = 0
	lineno = 0
	prevlineno = 0

	# Loop over those lines to be fit.
	while (prl_nextlineno (gl, lineno) != EOF) {

	    # Read in the image line.
	    buf = imgl2r (im, lineno)
	    if (buf == EOF)
		call error (0, "GOOD_PIXELS: Error reading image.")

	    # Get the ranges for that image line.
	    nranges = prl_get_ranges (gl, lineno, Memi[ranges], max_nranges)
	    if (nranges == 0)
		next

	    # If ranges are not equal to previous line fit else refit.
	    if (lp == 0 || prl_eqlines (gl, lineno, prevlineno) == NO) {
		npts = is_expand_ranges (Memi[ranges], Memi[colsfit], ncols)
		ijunk = is_choose_rangesr (Memi[colsfit], Memr[buf], Memr[fbuf],
		    npts, 1, ncols)
		call islfit (sf, Memi[colsfit], lineno, Memr[fbuf], Memr[wgt],
		    npts, SF_USER, ier)
	    } else {
		ijunk = is_choose_rangesr (Memi[colsfit], Memr[buf], Memr[fbuf],
		    npts, 1, ncols)
		call islrefit (sf, Memi[colsfit], lineno, Memr[fbuf], Memr[wgt])
	    }

	    # Handle fitting errors.
	    switch (ier) {
	    case NO_DEG_FREEDOM:
		call eprintf ("Warning: Too few columns to fit line: %d\n")
		    call pargi (lineno)
	    case SINGULAR:
		call eprintf ("Warning: Solution singular for line: %d\n")
		    call pargi (lineno)
		Memi[lines+lp] = lineno
		lp = lp + 1
	    default:
		Memi[lines+lp] = lineno
		lp = lp + 1
	    }

	    prevlineno = lineno
	}

	# Solve the surface.
	call issolve (sf, Memi[lines], lp, ier)

	# Handle fitting errors.
	switch (ier) {
	case NO_DEG_FREEDOM:
	    call error (0, "GOOD_PIXELS: Cannot fit surface.")
	case SINGULAR:
	    call eprintf ("Warning: Solution singular for surface.\n")
	default:
	    # everything OK
	}

	# Free space.
	call sfree (sp)
end


# ALL_MEDIANS -- Get surface when median processor on and all
# pixels good.

procedure  all_medians (im, imfit, sf)

pointer	im	# input image
pointer	imfit	# pointer to the imsurfit header structure
pointer	sf	# pointer to the surface descriptor

int	i, lp, cp, op, lineno, x1, x2, y1, y2, ier
int	nimcols, nimlines, ncols, nlines, npts
pointer	sp, cols, lines, wgt, z, med, sbuf, lbuf, buf

pointer	imgs2r()
real	asokr()
errchk	salloc, sfree, smark
errchk	isinit, islfit, islrefit, issolve

begin
	# Determine the number of lines and columns for a median processed
	# image.
	nimcols = IM_LEN(im,1)
	if (mod (int (IM_LEN(im,1)), XMEDIAN(imfit)) != 0)
	    ncols = IM_LEN(im,1) / XMEDIAN(imfit) + 1
	else
	    ncols = IM_LEN(im,1) / XMEDIAN(imfit)
	nimlines = IM_LEN(im,2)
	if (mod (int (IM_LEN(im,2)), YMEDIAN(imfit)) != 0)
	    nlines = IM_LEN(im,2) / YMEDIAN(imfit) + 1
	else
	    nlines = IM_LEN(im,2) / YMEDIAN(imfit)

	# Initialize the surface fitting.
	call isinit (sf, SURFACE_TYPE(imfit), XORDER(imfit), YORDER(imfit),
	   CROSS_TERMS(imfit), ncols, nlines)

	# Allocate workin memory.
	call smark (sp)
	call salloc (cols, ncols, TY_INT)
	call salloc (wgt, ncols, TY_REAL)
	call salloc (lines, nlines, TY_INT)
	call salloc (z, ncols, TY_REAL)
	call salloc (med, XMEDIAN(imfit) * YMEDIAN(imfit), TY_REAL)

	# Intialize the x and weight arrays.
	do i = 1, ncols
	    Memi[cols - 1 + i] = i
	call amovkr (1.0, Memr[wgt], ncols)

	# Loop over image sections.
	lp = 0
	lineno = 1
	for (y1 = 1; y1 <= nimlines; y1 = y1 + YMEDIAN(imfit)) {

	    # Get image section.
	    y2 = min (y1 + YMEDIAN(imfit) - 1, nimlines)
	    sbuf = imgs2r (im, 1, nimcols, y1, y2)
	    if (sbuf == EOF)
		call error (0, "Error reading image section.")

	    # Loop over median boxes.
	    cp = 0
	    for (x1 = 1; x1 <= nimcols; x1 = x1 + XMEDIAN(imfit)) {

		x2 = min (x1 + XMEDIAN(imfit) - 1, nimcols)
		npts = x2 - x1 + 1
		lbuf = sbuf - 1 + x1

		# Loop over lines in the median box.
		op = 0
		buf = lbuf
		for (i = 1; i <= y2 - y1 + 1; i = i + 1) {
		   call amovr (Memr[buf], Memr[med+op], npts)
		   op = op + npts
		   buf = buf + nimcols
		}

		# Calculate the median.
		Memr[z+cp] = asokr (Memr[med], op, (op + 1) / 2)
		cp = cp + 1

	    }

	    # Fit each image "line".
	    if (y1 == 1)
		call islfit (sf, Memi[cols], lineno, Memr[z], Memr[wgt],
		     ncols, SF_USER, ier)
	    else
		call islrefit (sf, Memi[cols], lineno, Memr[z], Memr[wgt])

	    # Handle fitting errors.
	    switch (ier) {
	    case NO_DEG_FREEDOM:
		call eprintf ("Warning: Too few columns to fit line: %d\n")
		    call pargi (lineno)
	    case SINGULAR:
		call eprintf ("Warning: Solution singular for line: %d\n")
		    call pargi (lineno)
		Memi[lines + lp] = lineno
		lp = lp + 1
	    default:
		Memi[lines + lp] = lineno
		lp = lp + 1
	    }

	    lineno = lineno + 1
	}

	# Solve th surface.
	call issolve (sf, Memi[lines], lp, ier)

	# Handle fitting errors.
	switch (ier) {
	case NO_DEG_FREEDOM:
	    call error (0, "ALL_MEDIANS: Cannot fit surface.")
	case SINGULAR:
	    call eprintf ("Warning: Solution singular for surface.\n")
	default:
	    # everything OK
	}

	# Free space
	call sfree (sp)
end


# GOOD_MEDIANS -- Procedure to fetch medians when the good regions
# list is defined.

procedure good_medians (im, imfit, gl, sf)

pointer	im	# input image
pointer	imfit	# pointer to surface descriptor structure
pointer	gl	# pointer to good regions list
pointer	sf	# pointer the surface descriptor

int	i, cp, lp, x1, x2, y1, y2, ier, ntemp
int	nimcols, nimlines, ncols, nlines, nranges, nbox, nxpts
int	lineno, current_line, lines_per_box, max_nranges
pointer	sp, colsfit, cols, lines, wgt, npts, lbuf, med, mbuf, z, ranges

int	prl_get_ranges(), prl_nextlineno(), is_expand_ranges()
int	is_choose_rangesr()
pointer	imgl2r()
real	asokr()
errchk	smark, salloc, sfree, imgl2r
errchk	isinit, islfit, issolve
errchk	prl_get_ranges, prl_nextlineno, is_choose_rangesr()

begin
	# Determine the number of lines and columns for a median processed
	# image.
	nimcols = IM_LEN(im,1)
	if (mod (int (IM_LEN(im,1)), XMEDIAN(imfit)) != 0)
	    ncols = IM_LEN(im,1) / XMEDIAN(imfit) + 1
	else
	    ncols = IM_LEN(im,1) / XMEDIAN(imfit)
	nimlines = IM_LEN(im,2)
	if (mod (int (IM_LEN(im,2)), YMEDIAN(imfit)) != 0)
	    nlines = IM_LEN(im,2) / YMEDIAN(imfit) + 1
	else
	    nlines = IM_LEN(im,2) / YMEDIAN(imfit)
	nbox = XMEDIAN(imfit) * YMEDIAN(imfit)
	max_nranges = nimcols

	# Initialize the surface fitting.
	call isinit (sf, SURFACE_TYPE(imfit), XORDER(imfit), YORDER(imfit),
	    CROSS_TERMS(imfit), ncols, nlines)

	# Allocate working memory.
	call smark (sp)
	call salloc (colsfit, nimcols, TY_INT)
	call salloc (cols, ncols, TY_INT)
	call salloc (npts, ncols, TY_INT)
	call salloc (lines, nlines, TY_INT)
	call salloc (wgt, ncols, TY_REAL)
	call salloc (med, nbox * ncols, TY_REAL)
	call salloc (z, ncols, TY_REAL)
	call salloc (ranges, 3 * max_nranges + 1, TY_INT)
	call amovkr (1., Memr[wgt], ncols)

	# Loop over median boxes in y.
	lp = 0
	lineno = 0
	for (y1 = 1; y1 <= nimlines; y1 = y1 + YMEDIAN(imfit)) {

	    lineno = lineno + 1
	    current_line = y1 - 1
	    y2 = min (y1 + YMEDIAN(imfit) - 1, nimlines)

	    # If lines not in range, next image section.
	    lines_per_box = 0
	    while (prl_nextlineno (gl, current_line) != EOF) {
		if (current_line > y2)
		    break
		lines_per_box = lines_per_box + 1
	    }
	    if (lines_per_box < (YMEDIAN(imfit) * (MEDIAN_PERCENT(imfit)/100.)))
		next

	    # Loop over the image lines.
	    call aclri (Memi[npts], ncols)
	    do i = y1, y2 {

		# Get image line, and check the good regions list.
		lbuf = imgl2r (im, i)
		nranges = prl_get_ranges (gl, i, Memi[ranges], max_nranges)
		if (nranges == 0)
		    next
		nxpts = is_expand_ranges (Memi[ranges], Memi[colsfit], nimcols)

		# Loop over the median boxes in x.
		cp= 0
		mbuf = med
		for (x1 = 1; x1 <= nimcols; x1 = x1 + XMEDIAN(imfit)) {
		    x2 = min (x1 + XMEDIAN(imfit) - 1, nimcols)
		    ntemp = is_choose_rangesr (Memi[colsfit], Memr[lbuf],
		        Memr[mbuf+Memi[npts+cp]], nxpts, x1, x2)
		    Memi[npts+cp] = Memi[npts+cp] + ntemp
		    mbuf = mbuf + nbox
		    cp = cp + 1
		}
	    }

	    # Calculate the medians.
	    nxpts = 0
	    mbuf = med
	    do i = 1, ncols {
		if (Memi[npts+i-1] > ((MEDIAN_PERCENT(imfit) / 100.) * nbox)) {
		    Memr[z+nxpts] = asokr (Memr[mbuf], Memi[npts+i-1],
			(Memi[npts+i-1] + 1) / 2)
		    Memi[cols+nxpts] = i
		    nxpts = nxpts + 1
		}
		mbuf = mbuf + nbox
	    }

	    # Fit the line.
	    call islfit (sf, Memi[cols], lineno, Memr[z], Memr[wgt], nxpts,
		SF_USER, ier)

	    # Handle fitting errors.
	    switch (ier) {
	    case NO_DEG_FREEDOM:
		call eprintf ("Warning: Too few columns to fit line: %d\n")
		    call pargi (lineno)
	    case SINGULAR:
		call eprintf ("Warning: Solution singular for line: %d\n")
		    call pargi (lineno)
		Memi[lines+lp] = lineno
		lp = lp + 1
	    default:
		Memi[lines+lp] = lineno
		lp = lp + 1
	    }
	}

	# Solve the surface.
	call issolve (sf, Memi[lines], lp, ier)

	# Handle fitting errors.
	switch (ier) {
	case NO_DEG_FREEDOM:
	    call error (0, "GOOD_MEDIANS: Cannot fit surface.")
	case SINGULAR:
	    call eprintf ("Warning: Solution singular for surface.")
	default:
	    # everyting OK
	}

	# Free space.
	call sfree (sp)
end


# SET_OUTIMAGE -- Procedure to write an output image of the desired type.

procedure set_outimage (imin, imout, imfit, sf, rl)

pointer	imin	# input image
pointer	imout	# output image
pointer	imfit	# pointer to the imsurfut header structure
pointer	sf	# pointer to the surface descriptor
pointer	rl	# pointer to the rejected pixel list regions list

int	i, k, ncols, nlines, max_nranges
long	u[IM_MAXDIM], v[IM_MAXDIM]
real	b1x, b2x, b1y, b2y
pointer	sp, x, y, inbuf, outbuf, ranges

int	impnlr(), imgnlr()
real	ims_divzero()
extern	ims_divzero
errchk	malloc, mfree, imgnlr, impnlr

begin
	ncols = IM_LEN(imin,1)
	nlines = IM_LEN(imin,2)
	max_nranges = ncols

	# Calculate transformation constants from real coordinates to
	# median coordinates if median processing specified. 
	if (MEDIAN(imfit) == YES) {
	    b1x = (1. + XMEDIAN(imfit)) / (2. * XMEDIAN(imfit))
	    b2x = (2. * ncols + XMEDIAN(imfit) - 1.) / (2. * XMEDIAN(imfit))
	    b1y = (1. + YMEDIAN(imfit)) / (2. * YMEDIAN(imfit))
	    b2y = (2. * nlines + YMEDIAN(imfit) - 1.) / (2. * YMEDIAN(imfit))
	}

	# Allocate space for x coordinates, initialize to image coordinates
	# and transform to median coordinates.
	call smark (sp)
	call salloc (x, ncols, TY_REAL)
	call salloc (y, ncols, TY_REAL)
	call salloc (ranges, 3 * max_nranges + 1, TY_INT)

	# Intialize the x array.
	do i = 1, ncols
	    Memr[x - 1 + i] = i
	if (MEDIAN(imfit) == YES)
	    call amapr (Memr[x], Memr[x], ncols, 1., real (ncols), b1x, b2x)

	# loop over the images lines
	call amovkl (long (1), v, IM_MAXDIM)
	call amovkl (long (1), u, IM_MAXDIM)
	do i = 1, nlines {

	    # Get input and output image buffers.
	    if (TYPE_OUTPUT(imfit) != FIT) {
		if (imgnlr (imin, inbuf, v) == EOF)
		    call error (0, "Error reading input image.")
	    }
	    if (impnlr (imout, outbuf, u) == EOF)
		call error (0, "Error writing output image.")

	    # Intialize y coordinates to image coordinates, and
	    # transform to median coordinates.
	    if (MEDIAN(imfit) == YES) {
	        Memr[y] = real (i)
		call amapr (Memr[y], Memr[y], 1, 1., real (nlines),
			    b1y, b2y)
		call amovkr (Memr[y], Memr[y+1], (ncols-1))
	    } else
		call amovkr (real (i), Memr[y], ncols)

	    # Write output image.
	    switch (TYPE_OUTPUT(imfit)) {
	    case FIT:
	        call isvector (sf, Memr[x], Memr[y], Memr[outbuf], ncols)
	    case CLEAN:
		call clean_line (Memr[x], Memr[y], Memr[inbuf], ncols, nlines,
		   rl, sf, i, NGROW(imfit)) 
		call amovr (Memr[inbuf], Memr[outbuf], ncols)
	    case RESID:
	        call isvector (sf, Memr[x], Memr[y], Memr[outbuf], ncols)
		call asubr (Memr[inbuf], Memr[outbuf], Memr[outbuf], ncols)
	    case RESP:
	        call isvector (sf, Memr[x], Memr[y], Memr[outbuf], ncols)
		if (IS_INDEF(DIV_MIN(imfit))) {
		    iferr (call adivr (Memr[inbuf], Memr[outbuf], Memr[outbuf],
		        ncols))
			call advzr (Memr[inbuf], Memr[outbuf], Memr[outbuf],
			    ncols, ims_divzero)
		} else {
		    do k = 1, ncols {
			if (Memr[outbuf-1+k] < DIV_MIN(imfit))
			    Memr[outbuf-1+k] = 1.
			else
			    Memr[outbuf-1+k] = Memr[inbuf-1+k] /
			    Memr[outbuf-1+k]
		    }
		}
	    default:
		call error (0, "SET_OUTIMAGE: Unknown output type.")
	    }
	}

	# Free space
	call sfree (sp)
end


# CLEAN_LINE -- Procedure to set weights of rejected points to zero

procedure clean_line (x, y, z, ncols, nlines, rl, sf, line, ngrow)

real	x[ARB]		# array of weights set to 1
real	y		# y value of line
real	z[ARB]		# line of data
int	ncols		# number of image columns
int	nlines		# number of image lines
pointer	rl		# pointer to reject pixel list
pointer	sf		# surface fitting
int	line		# line number
int	ngrow		# radius for region growing

int	cp, j, k, nranges, dist, yreg_min, yreg_max, xreg_min, xreg_max
pointer	sp, branges
real	r2
int	prl_get_ranges(), is_next_number()
real	iseval()

begin
	call smark (sp)
	call salloc (branges, 3 * ncols + 1, TY_INT)

	r2 = ngrow ** 2
	yreg_min = max (1, line - ngrow)
	yreg_max = min (nlines, line + ngrow)

	do j = yreg_min, yreg_max {
	    nranges = prl_get_ranges (rl, j, Memi[branges], ncols)
	    if (nranges == 0)
		next
	    dist = int (sqrt (r2 - (j - line) ** 2))
	    cp = 0
	    while (is_next_number (Memi[branges], cp) != EOF) {
		xreg_min = max (1, cp - dist)
		xreg_max = min (ncols, cp + dist)
		do k = xreg_min, xreg_max
		    z[k] = iseval (sf, x[k], y)
		cp = xreg_max
	    }
	}

	call sfree (sp)
end


# DO_REJECT -- Procedure to detect rejected pixels in an image.

procedure do_reject (im, imfit, gl, sf, rl)

pointer		im	# pointer to in put image
pointer		imfit	# pointer to image fitting structure
pointer		gl	# pointer to good regions list
pointer		sf	# pointer to surface descriptor
pointer		rl	# pointer to rejected pixel list

int	niter, nrejects
real	sigma
int	detect_rejects()
real	get_sigma()
errchk  prl_init, detect_rejects, get_sigma, refit_surface

begin
	# Initialize rejected pixel list.
	call prl_init (rl, int(IM_LEN(im,1)), int(IM_LEN(im,2)))

	# Do an iterative rejection cycle on the image.
	niter = 0
	repeat {

	    # Get the sigma of the fit.
	    sigma = get_sigma (im, gl, sf, rl)

	    # Detect rejected pixels.
	    nrejects = detect_rejects (im, imfit, gl, sf, rl, sigma)

	    # If no rejected pixels quit, else refit surface.
	    if (nrejects == 0 || NITER(imfit) == 0)
		break
	    call refit_surface (im, imfit, gl, sf, rl)

	    niter = niter + 1

	} until (niter == NITER(imfit))
end


# REFIT_SURFACE -- Procedure tp refit the surface.

procedure refit_surface (im, imfit, gl, sf, rl)

pointer		im	# pointer to image
pointer		imfit	# pointer to surface fitting structure
pointer		gl	# pointer to good regions list
pointer		sf	# pointer to surface descriptor
pointer		rl	# pointer to rejected pixels list

int	i, ijunk, lp, ier, max_nranges
int	ncols, nlines, npts, nfree, nrejects, nranges, ncoeff
pointer	sp, cols, colsfit, lines, buf, fbuf, wgt, granges

int	prl_get_ranges(), grow_regions(), is_expand_ranges()
int	is_choose_rangesr()
pointer	imgl2r()
errchk	smark, salloc, sfree, imgl2r
errchk	iscoeff, islfit, issolve
errchk	prl_get_ranges, grow_regions
errchk	is_choose_rangesr

begin
	ncols = IM_LEN(im,1)
	nlines = IM_LEN(im,2)
	max_nranges = ncols

	# Allocate up temporary storage.
	call smark (sp)
	call salloc (cols, ncols, TY_INT)
	call salloc (colsfit, ncols, TY_INT)
	call salloc (lines, nlines, TY_INT)
	call salloc (fbuf, ncols, TY_REAL)
	call salloc (wgt, ncols, TY_REAL)
	call salloc (granges, 3 * max_nranges + 1, TY_INT)

	# Initialize columns.
	do i = 1, ncols
	    Memi[cols+i-1] = i
	call amovi (Memi[cols], Memi[colsfit], ncols)

	# Get number of coefficients.
	switch (SURFACE_TYPE(imfit)) {
	case SF_LEGENDRE, SF_CHEBYSHEV:
	    ncoeff = XORDER(imfit)
	case SF_SPLINE3:
	    ncoeff = XORDER(imfit) + 3
	case SF_SPLINE1:
	    ncoeff = XORDER(imfit) + 1
	}

	# Refit affected lines and solve for surface.
	lp = 0
	do i = 1, nlines {

	    # Determine whether image line is good.
	    if (gl != NULL) {
		nranges = prl_get_ranges (gl, i, Memi[granges], max_nranges)
		if (nranges == 0)
		    next
	    }

	    # Define rejected points with region growing.
	    call amovkr (1., Memr[wgt], ncols)
	    nrejects = grow_regions (Memr[wgt], ncols, nlines, rl, i,
	        NGROW(imfit))

	    # Get number of data points.
	    if (gl == NULL)
		npts = ncols
	    else
		npts = is_expand_ranges (Memi[granges], Memi[colsfit], ncols)
	    nfree = npts - nrejects

	    # If no rejected pixels skip to next line.
	    if (nrejects == 0) {
		if (nfree >= ncoeff ) {
		    Memi[lines+lp] = i
		    lp = lp + 1
		}
		next
	    }

	    # Read in image line.
	    buf = imgl2r (im, i)
	    if (buf == EOF)
		call error (0, "REFIT_SURFACE: Error reading image.")

	    # Select the data.
	    if (gl == NULL) {
		npts = ncols
	        if (nfree >= ncoeff)
	            call islfit (sf, Memi[colsfit], i, Memr[buf], Memr[wgt],
		        npts, SF_USER, ier)
	        else
		    ier = NO_DEG_FREEDOM
	    } else {
		ijunk = is_choose_rangesr (Memi[colsfit], Memr[buf],
		    Memr[fbuf], npts, 1, ncols)
		ijunk = is_choose_rangesr (Memi[colsfit], Memr[wgt], Memr[wgt],
		    npts, 1, ncols)
	        if (nfree >= ncoeff)
	            call islfit (sf, Memi[colsfit], i, Memr[fbuf], Memr[wgt],
		        npts, SF_USER, ier)
	        else
		    ier = NO_DEG_FREEDOM
	    }

	    # Evaluate fitting errors.
	    switch (ier) {
	    case NO_DEG_FREEDOM:
		call eprintf ("REFIT_SURFACE: Too few points to fit line: %d\n")
		    call pargi (i)
	    case SINGULAR:
		call eprintf ("REFIT_SURFACE: Solution singular for line: %d\n")
		    call pargi (i)
		Memi[lines+lp] = i
		lp = lp + 1
	    default:
		Memi[lines+lp] = i
		lp = lp + 1
	    }
	}

	# Resolve surface.
	call issolve (sf, Memi[lines], lp, ier)

	# Evaluate fitting errors for surface.
	switch (ier) {
	case NO_DEG_FREEDOM:
	    call error (0, "REFIT_SURFACE: Too few points to fit surface\n")
	case SINGULAR:
	    call eprintf ("REFIT_SURFACE: Solution singular for surface\n")
	default:
	    # everything OK
	}

	call sfree (sp)

end


# GROW_REGIONS -- Procedure to set weights of rejected points to zero.

int procedure grow_regions (wgt, ncols, nlines, rl, line, ngrow)

real	wgt[ARB]	# array of weights set to 1
int	ncols		# number of image columnspoints
int	nlines		# number of images lines
pointer	rl		# pointer to reject pixel list
int	line		# line number
int	ngrow		# radius for region growing

int	cp, j, k, nrejects, nranges, max_nranges
int	dist, yreg_min, yreg_max, xreg_min, xreg_max
pointer	sp, branges
real	r2
int	prl_get_ranges(), is_next_number()
errchk	smark, salloc, sfree
errchk	prl_get_ranges, is_next_number 

begin
	max_nranges = ncols

	call smark (sp)
	call salloc (branges, 3 * max_nranges + 1, TY_INT)

	r2 = ngrow ** 2
	nrejects = 0
	yreg_min = max (1, line - ngrow)
	yreg_max = min (nlines, line + ngrow)

	do j = yreg_min, yreg_max {
	    nranges = prl_get_ranges (rl, j, Memi[branges], max_nranges)
	    if (nranges == 0)
		next
	    dist = int (sqrt (r2 - (j - line) ** 2))
	    cp = 0
	    while (is_next_number (Memi[branges], cp) != EOF) {
		xreg_min = max (1, cp - dist)
		xreg_max = min (ncols, cp + dist)
		do k = xreg_min, xreg_max {
		    if (wgt[k] > 0.) {
			wgt[k] = 0.
			nrejects = nrejects + 1
		    }
		}
		cp = xreg_max
	    }
	}

	call sfree (sp)
	return (nrejects)
end


# GET_SIGMA -- Procedure to calculate the sigma of the surface fit

real procedure get_sigma (im, gl, sf, rl)

pointer		im	# pointer to image
pointer		gl	# pointer to good pixel list
pointer		sf	# pointer to surface deascriptor
pointer		rl	# pointer to rejected pixel list

int	i, ijunk, cp, nranges, npts, ntpts, ncols, nlines, max_nranges
pointer	sp, colsfit, x, xfit, y, zfit, buf, fbuf, wgt, granges, branges
real	sum, sigma
int	prl_get_ranges(), is_next_number(), is_expand_ranges()
int	is_choose_rangesr()
pointer	imgl2r()
real	asumr(), awssqr()
errchk	smark, salloc, sfree, imgl2r

begin
	ncols = IM_LEN(im,1)
	nlines = IM_LEN(im,2)
	max_nranges = ncols

	# Allocate working space.
	call smark (sp)
	call salloc (colsfit, ncols, TY_INT)
	call salloc (x, ncols, TY_REAL)
	call salloc (xfit, ncols, TY_REAL)
	call salloc (y, ncols, TY_REAL)
	call salloc (fbuf, ncols, TY_REAL)
	call salloc (zfit, ncols, TY_REAL)
	call salloc (wgt, ncols, TY_REAL)
	call salloc (granges, 3 * max_nranges + 1, TY_INT)
	call salloc (branges, 3 * max_nranges + 1, TY_INT)

	# Intialize the x array.
	do i = 1, ncols
	    Memr[x+i-1] = i
	call amovr (Memr[x], Memr[xfit], ncols)

	sum = 0.
	sigma = 0.
	ntpts = 0

	# Loop over the image.
	do i = 1, nlines {

	    # Check that line is in range.
	    if (gl != NULL) {
		nranges = prl_get_ranges (gl, i, Memi[granges], max_nranges)
		if (nranges == 0)
		    next
		npts = is_expand_ranges (Memi[granges], Memi[colsfit], ncols)
	    }
	    
	    # Read in image.
	    buf = imgl2r (im, i)
	    if (buf == EOF)
		call error (0, "GET_SIGMA: Error reading image.")

	    # Select appropriate data and fit.
	    call amovkr (real (i), Memr[y], ncols)
	    if (gl == NULL) {
		npts = ncols
	        call isvector (sf, Memr[xfit], Memr[y], Memr[zfit], npts)
	        call asubr (Memr[buf], Memr[zfit], Memr[zfit], npts)
	    } else {
		ijunk = is_choose_rangesr (Memi[colsfit], Memr[x], Memr[xfit],
		    npts, 1, ncols)
		ijunk = is_choose_rangesr (Memi[colsfit], Memr[buf], Memr[fbuf],
		    npts, 1, ncols)
	        call isvector (sf, Memr[xfit], Memr[y], Memr[zfit], npts)
	        call asubr (Memr[fbuf], Memr[zfit], Memr[zfit], npts)
	    }

	    # Get ranges of rejected pixels for the line and set weights.
	    call amovkr (1., Memr[wgt], ncols)
	    nranges = prl_get_ranges (rl, i, Memi[branges], max_nranges)
	    if (nranges > 0) {
		cp = 0
		while (is_next_number (Memi[branges], cp) != EOF)
		    Memr[wgt+cp-1] = 0.
		if (gl != NULL)
		    ijunk = is_choose_rangesr (Memi[colsfit], Memr[wgt],
		        Memr[wgt], npts, 1, ncols)
	    }

	    # Calculate sigma.
	    sigma = sigma + awssqr (Memr[zfit], Memr[wgt], npts)
	    ntpts = ntpts + asumr (Memr[wgt], npts)
	}

	call sfree (sp)

	return (sqrt (sigma / (ntpts - 1)))
end


# DETECT_REJECTS - Procedure to detect rejected pixels.

int procedure detect_rejects (im, imfit, gl, sf, rl, sigma)

pointer		im	# pointer to image
pointer		imfit	# pointer to surface fitting structure
pointer		gl	# pointer to good pixel list
pointer		sf	# pointer to surface descriptor
pointer		rl	# pointer to rejected pixel list
real		sigma	# standard deviation of fit

int	i, j, ijunk, cp, ncols, nlines, npts, nranges, nlrejects, ntrejects
int	norejects, max_nranges
pointer	sp, granges, branges, x, xfit, cols, colsfit, y, zfit, buf, fbuf
pointer	wgt, list
real	upper, lower

int	prl_get_ranges(), is_next_number(), is_make_ranges(), is_expand_ranges()
int	is_choose_rangesr()
pointer	imgl2r()

begin
	ncols = IM_LEN(im,1)
	nlines = IM_LEN(im,2)
	max_nranges = ncols

	# Allocate temporary space.
	call smark (sp)
	call salloc (x, ncols, TY_REAL)
	call salloc (xfit, ncols, TY_REAL)
	call salloc (cols, ncols, TY_INT)
	call salloc (colsfit, ncols, TY_INT)
	call salloc (y, ncols, TY_REAL)
	call salloc (fbuf, ncols, TY_REAL)
	call salloc (zfit, ncols, TY_REAL)
	call salloc (wgt, ncols, TY_REAL)
	call salloc (granges, 3 * max_nranges + 1, TY_INT)
	call salloc (branges, 3 * max_nranges + 1, TY_INT)
	call salloc (list, ncols, TY_INT)

	# Intialize x and column values.
	do i = 1, ncols {
	    Memi[cols+i-1] = i
	    Memr[x+i-1] = i
	}
	call amovr (Memr[x], Memr[xfit], ncols)
	call amovi (Memi[cols], Memi[colsfit], ncols)

	ntrejects = 0
	if (LOWER(imfit) <= 0.0)
	    lower = -MAX_REAL
	else
	    lower = -sigma * LOWER(imfit)
	if (UPPER(imfit) <= 0.0)
	    upper = MAX_REAL
	else
	    upper = sigma * UPPER(imfit)

	# Loop over the image.
	do i = 1, nlines {

	    # Get ranges if appropriate.
	    if (gl != NULL) {
		nranges = prl_get_ranges (gl, i, Memi[granges], max_nranges)
		if (nranges == 0)
		    next
		npts = is_expand_ranges (Memi[granges], Memi[colsfit], ncols) 
	    }

	    # Read in image.
	    buf = imgl2r (im, i)
	    if (buf == EOF)
		call error (0, "GET_SIGMA: Error reading image.")

	    # Select appropriate data and fit.
	    call amovkr (real (i), Memr[y], ncols)
	    if (gl == NULL) {
		npts = ncols
	        call isvector (sf, Memr[xfit], Memr[y], Memr[zfit], npts)
	        call asubr (Memr[buf], Memr[zfit], Memr[zfit], npts)
	    } else {
		ijunk = is_choose_rangesr (Memi[colsfit], Memr[x], Memr[xfit],
		    npts, 1, ncols)
		ijunk = is_choose_rangesr (Memi[colsfit], Memr[buf], Memr[fbuf],
		    npts, 1, ncols)
	        call isvector (sf, Memr[xfit], Memr[y], Memr[zfit], npts)
	        call asubr (Memr[fbuf], Memr[zfit], Memr[zfit], npts)
	    }

	    # Get ranges of rejected pixels for the line and set weights.
	    call amovkr (1., Memr[wgt], ncols)
	    nranges = prl_get_ranges (rl, i, Memi[branges], max_nranges)
	    norejects = 0
	    if (nranges > 0) {
		cp = 0
		while (is_next_number (Memi[branges], cp) != EOF) {
		    Memi[list+norejects] = cp
		    norejects = norejects + 1
		    Memr[wgt+cp-1] = 0.
		}
		if (gl != NULL)
		    ijunk = is_choose_rangesr (Memi[colsfit], Memr[wgt],
		        Memr[wgt], npts, 1, ncols)
	    }

	    # Detect deviant pixels.
	    nlrejects = 0
	    do j = 1, npts {
		if ((Memr[zfit+j-1] < lower || Memr[zfit+j-1] > upper) &&
		    Memr[wgt+j-1] != 0.) {
		    Memi[list+norejects+nlrejects] = Memi[colsfit+j-1]
		    nlrejects = nlrejects + 1
		}
	    }

	    # Add to rejected pixel list.
	    if (nlrejects > 0) {
		call asrti (Memi[list], Memi[list], norejects + nlrejects)
		nranges = is_make_ranges (Memi[list], norejects + nlrejects,
		    Memi[granges], max_nranges)
		call prl_put_ranges (rl, i, i, Memi[granges])
	    }

	    ntrejects = ntrejects + nlrejects
	}

	call sfree (sp)
	return (ntrejects)
end


# AWSSQR -- Procedure to calculate the weighted sum of the squares

real procedure awssqr (a, w, npts)

real	a[npts]		# array of data
real	w[npts]		# array of points
int	npts		# number of data points

int	i
real	sum

begin
	sum = 0.
	do i = 1, npts
	    sum = sum + w[i] * a[i] ** 2

	return (sum)
end


# IMS_DIVZER0 -- Return 1. on a divide by zero

real	procedure ims_divzero (x)

real	x

begin
	return (1.)
end