aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/immatch/src/linmatch/rglregions.x
blob: 16f01b1519cdf32e6e2e86c8817a49912dd0deec (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
include <ctype.h>
include <fset.h>
include <imhdr.h>
include "linmatch.h"

# RG_LREGIONS -- Decode the input regions description. If the regions string
# is NULL then the regions list is empty. The regions are specified in section
# notation, grid notation, coordinate notation or are read
# from a file.

int procedure rg_lregions (list, im, ls, rp, reread)

int	list			#I pointer to the regions file list
pointer	im			#I pointer to the reference image
pointer	ls			#I pointer to the linscale structure
int	rp			#I region pointer
int	reread			#I reread the current file

char	fname[SZ_FNAME]
int	max_nregions, nregions, fd
pointer	sp, regions
int	rg_lstati(), rg_lgrid(), rg_lgregions(), rg_lsregions()
int	rg_lrsections(), rg_lrcoords(), fntgfnb(), open()
data	fname[1] /EOS/
errchk	fntgfnb(), seek(),  open(), close()

begin
	call smark (sp)
	call salloc (regions, SZ_LINE, TY_CHAR)

	call rg_lstats (ls, REGIONS, Memc[regions], SZ_LINE)
	max_nregions = rg_lstati (ls, MAXNREGIONS)

	if (rp < 1 || rp > max_nregions || Memc[regions] == EOS) {
	    nregions = 0
	} else if (rg_lgrid (im, ls, rp, max_nregions) > 0) {
	    nregions = rg_lstati (ls, NREGIONS)
	} else if (rg_lgregions (im, ls, rp, max_nregions) > 0) {
	    nregions = rg_lstati (ls, NREGIONS)
	} else if (rg_lsregions (im, ls, rp, max_nregions) > 0) {
	    nregions = rg_lstati (ls, NREGIONS)
	} else if (list != NULL) {
	    if (reread == NO) {
	        iferr {
		    if (fntgfnb (list, fname, SZ_FNAME) != EOF) {
	                fd = open (fname, READ_ONLY, TEXT_FILE)
	                nregions= rg_lrsections (fd, im, ls, rp, max_nregions)
		        if (nregions <= 0) {
			    call seek (fd, BOF)
	                    nregions= rg_lrcoords (fd, im, ls, rp, max_nregions)
		        }
	                call close (fd)
		    } else
		        nregions = 0
	        } then
	            nregions = 0
	    } else if (fname[1] != EOS) {
		iferr {
	            fd = open (fname, READ_ONLY, TEXT_FILE)
	            nregions= rg_lrsections (fd, im, ls, rp, max_nregions)
		    if (nregions <= 0) {
			call seek (fd, BOF)
	                nregions= rg_lrcoords (fd, im, ls, rp, max_nregions)
		    }
	            call close (fd)
		} then
		    nregions = 0
	    }
	} else
	    nregions = 0

	call sfree (sp)

	return (nregions)
end


# RG_LGRID - Decode the regions from a grid specification.

int procedure rg_lgrid (im, ls, rp, max_nregions)

pointer im                      #I pointer to the reference image
pointer ls                      #I pointer to the linscale structure
int     rp                      #I index of the current region
int     max_nregions            #I the maximum number of regions

int     i, istart, iend, j, jstart, jend, ncols, nlines, nxsample, nysample
int     nxcols, nylines, nregions
pointer sp, region, section
int     rg_lstati(), nscan(), strcmp()
pointer rg_lstatp()

begin
        # Allocate working space.
        call smark (sp)
        call salloc (region, SZ_LINE, TY_CHAR)
        call salloc (section, SZ_LINE, TY_CHAR)

        # Allocate the arrays to hold the regions information,
        call rg_lrealloc (ls, max_nregions)

        # Initialize.
        call rg_lstats (ls, REGIONS, Memc[region], SZ_LINE)
        ncols = IM_LEN(im,1)
        nlines = IM_LEN(im,2)
        nregions = min (rp - 1, rg_lstati (ls, NREGIONS))

        # Decode the grid specification.
        call sscan (Memc[region])
            call gargwrd (Memc[section], SZ_LINE)
            call gargi (nxsample)
            call gargi (nysample)
        if ((nscan() != 3) || (strcmp (Memc[section], "grid") != 0)) {
            call sfree (sp)
            return (nregions)
        }

        # Decode the regions.
        if ((nxsample * nysample) > max_nregions) {
            nxsample = nint (sqrt (real (max_nregions) * real (ncols) /
                real (nlines)))
            nysample = real (max_nregions) / real (nxsample)
        }
        nxcols = ncols / nxsample
        nylines = nlines / nysample
        jstart = 1 + (nlines - nysample * nylines) / 2
        jend = jstart + (nysample - 1) * nylines
        do j = jstart, jend, nylines {
            istart = 1 + (ncols - nxsample * nxcols) / 2
            iend = istart + (nxsample - 1) * nxcols
            do i = istart, iend, nxcols {
                Memi[rg_lstatp(ls,RC1)+nregions] = i
                Memi[rg_lstatp(ls,RC2)+nregions] = i + nxcols - 1
                Memi[rg_lstatp(ls,RL1)+nregions] = j
                Memi[rg_lstatp(ls,RL2)+nregions] = j + nylines - 1
                Memi[rg_lstatp(ls,RXSTEP)+nregions] = 1
                Memi[rg_lstatp(ls,RYSTEP)+nregions] = 1
		Memr[rg_lstatp(ls,RMEAN)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RMEDIAN)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RMODE)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RSIGMA)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RSKY)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RSKYERR)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RMAG)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RMAGERR)+nregions] = INDEFR
		Memi[rg_lstatp(ls,RNPTS)+nregions] = INDEFI
		Memr[rg_lstatp(ls,IMEAN)+nregions] = INDEFR
		Memr[rg_lstatp(ls,IMEDIAN)+nregions] = INDEFR
		Memr[rg_lstatp(ls,IMODE)+nregions] = INDEFR
		Memr[rg_lstatp(ls,ISIGMA)+nregions] = INDEFR
		Memr[rg_lstatp(ls,ISKY)+nregions] = INDEFR
		Memr[rg_lstatp(ls,ISKYERR)+nregions] = INDEFR
		Memr[rg_lstatp(ls,IMAG)+nregions] = INDEFR
		Memr[rg_lstatp(ls,IMAGERR)+nregions] = INDEFR
		Memi[rg_lstatp(ls,INPTS)+nregions] = INDEFI
		Memr[rg_lstatp(ls,RBSCALE)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RBSCALEERR)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RBZERO)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RBZEROERR)+nregions] = INDEFR
		Memi[rg_lstatp(ls,RDELETE)+nregions] = LS_NO
		Memr[rg_lstatp(ls,RCHI)+nregions] = INDEFR
                nregions = nregions + 1
            }
        }

        call rg_lseti (ls, NREGIONS, nregions)
        if (nregions > 0)
            call rg_lrealloc (ls, nregions)
        else
            call rg_lrfree (ls)
        call sfree (sp)

        return (nregions)
end


# RG_LGREGIONS -- Compute the column and line limits givenan x and y
# coordinate and a default size.

int procedure rg_lgregions (im, ls, rp, max_nregions)

pointer im                      #I pointer to the image
pointer ls                      #I pointer to the linscale structure
int     rp                      #I pointer to the current region
int     max_nregions            #I maximum number of regions

char	comma
int     ncols, nlines, nregions, onscan()
int     x1, x2, y1, y2
pointer sp, region
real    x, y, xc, yc
int     rg_lstati(), nscan()
pointer rg_lstatp()

begin
        # Allocate working space.
        call smark (sp)
        call salloc (region, SZ_LINE, TY_CHAR)

        # Allocate the arrays to hold the regions information.
        call rg_lrealloc (ls, max_nregions)

        # Get the constants.
        ncols = IM_LEN(im,1)
        nlines = IM_LEN(im,2)

        # Decode the center.
        call rg_lstats (ls, REGIONS, Memc[region], SZ_LINE)
        nregions = min (rp - 1, rg_lstati (ls, NREGIONS))
	onscan = 0
        call sscan (Memc[region])
            call gargr (x)
            call gargr (y)
	    call gargc (comma)

	# Compute the data region.
        while ((nscan() == onscan + 3) && (nregions < max_nregions)) {

	    # Check for the comma.
	    if (comma != ',')
		break

            # Compute a more accurate center.
            #if (rg_lstati (ls, CENTER) == YES) {
                #call rg_lcntr (im, x, y, DEF_CRADIUS, xc, yc)
            #} else {
                xc = x
                yc = y
            #}

            # Compute the data section.
            x1 = xc - rg_lstati (ls, DNX) / 2
            x2 = x1 + rg_lstati (ls, DNX) - 1
            if (IM_NDIM(im) == 1) {
                y1 = 1
                y2 = 1
            } else {
                y1 = yc - rg_lstati (ls, DNY) / 2
                y2 = y1 + rg_lstati (ls, DNY) - 1
            }

            # Make sure that the region is on the image.
            if (x1 >= 1 && x2 <= IM_LEN(im,1) && y1 >= 1 &&
                y2 <= IM_LEN(im,2)) {
                Memi[rg_lstatp(ls,RC1)+nregions] = x1
                Memi[rg_lstatp(ls,RC2)+nregions] = x2
                Memi[rg_lstatp(ls,RL1)+nregions] = y1
                Memi[rg_lstatp(ls,RL2)+nregions] = y2
                Memi[rg_lstatp(ls,RXSTEP)+nregions] = 1
                Memi[rg_lstatp(ls,RYSTEP)+nregions] = 1
		Memr[rg_lstatp(ls,RMEAN)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RMEDIAN)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RMODE)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RSIGMA)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RSKY)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RSKYERR)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RMAG)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RMAGERR)+nregions] = INDEFR
		Memi[rg_lstatp(ls,RNPTS)+nregions] = INDEFI
		Memr[rg_lstatp(ls,IMEAN)+nregions] = INDEFR
		Memr[rg_lstatp(ls,IMEDIAN)+nregions] = INDEFR
		Memr[rg_lstatp(ls,IMODE)+nregions] = INDEFR
		Memr[rg_lstatp(ls,ISIGMA)+nregions] = INDEFR
		Memr[rg_lstatp(ls,ISKY)+nregions] = INDEFR
		Memr[rg_lstatp(ls,ISKYERR)+nregions] = INDEFR
		Memr[rg_lstatp(ls,IMAG)+nregions] = INDEFR
		Memr[rg_lstatp(ls,IMAGERR)+nregions] = INDEFR
		Memi[rg_lstatp(ls,INPTS)+nregions] = INDEFI
		Memr[rg_lstatp(ls,RBSCALE)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RBSCALEERR)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RBZERO)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RBZEROERR)+nregions] = INDEFR
		Memi[rg_lstatp(ls,RDELETE)+nregions] = LS_NO
		Memr[rg_lstatp(ls,RCHI)+nregions] = INDEFR
                nregions = nregions + 1
            }

	    onscan = nscan()
            call gargr (x)
            call gargr (y)
	    call gargc (comma)
        }

        # Reallocate the correct amount of space.
        call rg_lseti (ls, NREGIONS, nregions)
        if (nregions > 0)
            call rg_lrealloc (ls, nregions)
        else
            call rg_lrfree (ls)

        call sfree (sp)

        return (nregions)
end


# RG_LMKREGIONS -- Procedure to mark the sections on the image display.
# Sections are marked by pointing the image display cursor to the
# lower left and upper rights corners of the desired sections respectively.

int procedure rg_lmkregions (fd, im, ls, rp, max_nregions, regions, maxch)

int	fd			#I pointer to the output text file
pointer	im			#I pointer to the image
pointer	ls			#I pointer to the intensity scaling structure
int	rp			#I pointer to current region
int	max_nregions		#I maximum number of regions
char	regions[ARB]		#O the output regions string
int	maxch			#I the maximum size of the output string

int	nregions, op, wcs, key
pointer	sp, cmd
real	xll, yll, xur, yur
int	rg_lstati(), clgcur(), gstrcpy()
pointer	rg_lstatp()

begin
	# Allocate working space.
	call smark (sp)
	call salloc (cmd, SZ_LINE, TY_CHAR)

	# Allocate the arrays to hold the regions information,
	call rg_lrealloc (ls, max_nregions)

	# Initialize.
	nregions = min (rp-1, rg_lstati (ls, NREGIONS))
	op = 1
	regions[1] = EOS

	while (nregions < max_nregions) {

	    call printf ("Mark lower left corner of region %d [q to quit].\n")
		call pargi (nregions + 1)
	    if (clgcur ("icommands", xll, yll, wcs, key, Memc[cmd],
	        SZ_LINE) == EOF)
		break
	    if (key == 'q')
		break

	    call printf ("Mark upper right corner of region %d [q to quit].\n")
		call pargi (nregions + 1)
	    if (clgcur ("icommands", xur, yur, wcs, key, Memc[cmd],
	        SZ_LINE) == EOF)
		break
	    if (key == 'q')
		break

	    # Make sure that the region is on the image.
	    if (xll < 1.0 || xur > IM_LEN(im,1) || yll < 1.0 || yur >
		IM_LEN(im,2))
		next

	    Memi[rg_lstatp(ls,RC1)+nregions] = nint(xll)
	    Memi[rg_lstatp(ls,RC2)+nregions] = nint(xur)
	    Memi[rg_lstatp(ls,RXSTEP)+nregions] = 1
	    Memi[rg_lstatp(ls,RL1)+nregions] = nint(yll)
	    Memi[rg_lstatp(ls,RL2)+nregions] = nint(yur)
	    Memi[rg_lstatp(ls,RYSTEP)+nregions] = 1

	    Memr[rg_lstatp(ls,RMEAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMEDIAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMODE)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RSIGMA)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RSKY)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RSKYERR)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMAG)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMAGERR)+nregions] = INDEFR
	    Memi[rg_lstatp(ls,RNPTS)+nregions] = INDEFI

	    Memr[rg_lstatp(ls,IMEAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMEDIAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMODE)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,ISIGMA)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,ISKY)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,ISKYERR)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMAG)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMAGERR)+nregions] = INDEFR
	    Memi[rg_lstatp(ls,INPTS)+nregions] = INDEFI

	    Memr[rg_lstatp(ls,RBSCALE)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RBSCALEERR)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RBZERO)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RBZEROERR)+nregions] = INDEFR
	    Memi[rg_lstatp(ls,RDELETE)+nregions] = LS_NO
	    Memr[rg_lstatp(ls,RCHI)+nregions] = INDEFR
	    nregions = nregions + 1

	    # Write the regions string.
	    call sprintf (Memc[cmd], SZ_LINE, "[%d:%d,%d:%d] ")
		call pargi (nint(xll))
		call pargi (nint(xur))
		call pargi (nint(yll))
		call pargi (nint(yur))
	    op = op + gstrcpy (Memc[cmd], regions[op], maxch - op + 1)

	    # Write the output record.
	    if (fd != NULL) {
		call fprintf (fd, "[%d:%d,%d:%d]\n")
		    call pargi (nint(xll)) 
		    call pargi (nint(xur)) 
		    call pargi (nint(yll)) 
		    call pargi (nint(yur)) 
	    }
	}
	call printf ("\n")

	# Reallocate the correct amount of space.
	call rg_lsets (ls, REGIONS, regions)
	call rg_lseti (ls, NREGIONS, nregions)

	if (nregions > 0)
	    call rg_lrealloc (ls, nregions)
	else 
	    call rg_lrfree (ls)

	call sfree (sp)

	return (nregions)
end


# RG_LMKXY -- Create a list of objects by selecting objects with
# the image display cursor.

int procedure rg_lmkxy (fd, im, ls, rp, max_nregions)

int     fd                      #I the output coordinates file descriptor
pointer im                      #I pointer to the image
pointer	ls                      #I pointer to the psf matching structure
int     rp                      #I pointer to current region
int     max_nregions            #I maximum number of regions

int     nregions, wcs, key, x1, x2, y1, y2
pointer sp, region, cmd
real    xc, yc
int     clgcur(), rg_lstati()
pointer rg_lstatp()

begin
        # Allocate working space.
        call smark (sp)
        call salloc (region, SZ_FNAME, TY_CHAR)
        call salloc (cmd, SZ_LINE, TY_CHAR)

        # Allocate the arrays to hold the regions information,
        call rg_lrealloc (ls, max_nregions)

        nregions = min (rp-1, rg_lstati (ls, NREGIONS))
        while (nregions < max_nregions) {

            # Identify the object.
            call printf ("Mark object %d [any key=mark,q=quit]:\n")
                call pargi (nregions + 1)
            if (clgcur ("icommands", xc, yc, wcs, key, Memc[cmd],
		SZ_LINE) == EOF)
                break
            if (key == 'q')
                break

            # Compute the data section.
            x1 = xc - rg_lstati (ls, DNX) / 2
            x2 = x1 + rg_lstati (ls, DNX) - 1
            y1 = yc - rg_lstati (ls, DNY) / 2
            y2 = y1 + rg_lstati (ls, DNY) - 1

            # Make sure that the region is on the image.
            if (x1 < 1 || x2 > IM_LEN(im,1) || y1 < 1 || y2 >
                IM_LEN(im,2))
                next

            if (fd != NULL) {
                call fprintf (fd, "%0.3f  %0.3f\n")
                    call pargr (xc)
                    call pargr (yc)
            }

	    Memi[rg_lstatp(ls,RC1)+nregions] = x1
	    Memi[rg_lstatp(ls,RC2)+nregions] = x2
	    Memi[rg_lstatp(ls,RXSTEP)+nregions] = 1
	    Memi[rg_lstatp(ls,RL1)+nregions] = y1
	    Memi[rg_lstatp(ls,RL2)+nregions] = y2
	    Memi[rg_lstatp(ls,RYSTEP)+nregions] = 1

	    Memr[rg_lstatp(ls,RMEAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMEDIAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMODE)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RSIGMA)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RSKY)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RSKYERR)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMAG)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMAGERR)+nregions] = INDEFR
	    Memi[rg_lstatp(ls,RNPTS)+nregions] = INDEFI

	    Memr[rg_lstatp(ls,IMEAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMEDIAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMODE)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,ISIGMA)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,ISKY)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,ISKYERR)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMAG)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMAGERR)+nregions] = INDEFR
	    Memi[rg_lstatp(ls,INPTS)+nregions] = INDEFI

	    Memr[rg_lstatp(ls,RBSCALE)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RBSCALEERR)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RBZERO)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RBZEROERR)+nregions] = INDEFR
	    Memi[rg_lstatp(ls,RDELETE)+nregions] = LS_NO
	    Memr[rg_lstatp(ls,RCHI)+nregions] = INDEFR

            nregions = nregions + 1

        }

	# Reallocate the correct amount of space.
        call rg_lseti (ls, NREGIONS, nregions)
        if (nregions > 0) {
            call rg_lrealloc (ls, nregions)
            if (fd != NULL) {
                call fstats (fd, F_FILENAME, Memc[region], SZ_FNAME)
                call rg_lsets (ls, REGIONS, Memc[region])
            } else
                call rg_lsets (ls, REGIONS, "")
        } else {
            call rg_lrfree (ls)
            call rg_lsets (ls, REGIONS, "")
        }

        call sfree (sp)
        return (nregions)
end


# RG_LRSECTIONS -- Read the sections from a file.

int procedure rg_lrsections (fd, im, ls, rp, max_nregions)

int	fd			#I the regions file descriptor
pointer	im			#I pointer to the image
pointer	ls			#I pointer to the linscale structure
int	rp			#I pointer to current region
int	max_nregions		#I the  maximum number of regions

int	stat, nregions, ncols, nlines, x1, y1, x2, y2, xstep, ystep
pointer	sp, section, line
int	rg_lstati(), getline(), rg_lgsections()
pointer	rg_lstatp()

begin
	# Allocate working space.
	call smark (sp)
	call salloc (line, SZ_LINE, TY_CHAR)
	call salloc (section, SZ_LINE, TY_CHAR)

	# Allocate the arrays to hold the regions information,
	call rg_lrealloc (ls, max_nregions)

	# Get the constants.
	ncols = IM_LEN(im,1)
	nlines = IM_LEN(im,2)

	# Decode the regions string.
	nregions = min (rp - 1, rg_lstati (ls, NREGIONS))
	while (getline (fd, Memc[line]) != EOF &&  nregions < max_nregions) {

	    call sscan (Memc[line])
		call gargwrd (Memc[section], SZ_LINE)

	    while (Memc[section] != EOS && nregions < max_nregions) {
		stat = rg_lgsections (Memc[section], x1, x2, xstep, y1, y2,
		    ystep, ncols, nlines)

		# Check for even dimensioned regions.
		if (stat == OK) {
		    if (mod (x2 - x1 + 1, 2) == 2) {
			x2 = x2 + 1
			if (x2 > ncols)
			    x2 = x2 - 2
			if (x2 < 1)
			    stat = ERR
		    }
		    if (mod (y2 - y1 + 1, 2) == 2) {
			y2 = y2 + 1
			if (y2 > nlines)
			    y2 = y2 - 2
			if (y2 < 1)
			    stat = ERR
		    }
		} else
		    stat = ERR

		# Add the new region to the list.
		if (stat == OK) {
		    Memi[rg_lstatp(ls,RC1)+nregions] = x1
		    Memi[rg_lstatp(ls,RC2)+nregions] = x2
		    Memi[rg_lstatp(ls,RL1)+nregions] = y1
		    Memi[rg_lstatp(ls,RL2)+nregions] = y2
		    Memi[rg_lstatp(ls,RXSTEP)+nregions] = xstep
		    Memi[rg_lstatp(ls,RYSTEP)+nregions] = ystep
		    Memr[rg_lstatp(ls,RMEAN)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,RMEDIAN)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,RMODE)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,RSIGMA)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,RSKY)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,RSKYERR)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,RMAG)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,RMAGERR)+nregions] = INDEFR
		    Memi[rg_lstatp(ls,RNPTS)+nregions] = INDEFI
		    Memr[rg_lstatp(ls,IMEAN)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,IMEDIAN)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,IMODE)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,ISIGMA)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,ISKY)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,ISKYERR)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,IMAG)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,IMAGERR)+nregions] = INDEFR
		    Memi[rg_lstatp(ls,INPTS)+nregions] = INDEFI
		    Memr[rg_lstatp(ls,RBSCALE)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,RBSCALEERR)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,RBZERO)+nregions] = INDEFR
		    Memr[rg_lstatp(ls,RBZEROERR)+nregions] = INDEFR
		    Memi[rg_lstatp(ls,RDELETE)+nregions] = LS_NO
		    Memr[rg_lstatp(ls,RCHI)+nregions] = INDEFR
		    nregions = nregions + 1
		}

		call gargwrd (Memc[section], SZ_LINE)
	    }
	}

	call rg_lseti (ls, NREGIONS, nregions)
	if (nregions > 0)
	    call rg_lrealloc (ls, nregions)
	else
	    call rg_lrfree (ls)

	call sfree (sp)
	return (nregions)
end


# RG_LRCOORDS -- Read the coordinates from a file.

int procedure rg_lrcoords (fd, im, ls, rp, max_nregions)

int	fd			#I the regions file descriptor
pointer	im			#I pointer to the image
pointer	ls			#I pointer to the linscale structure
int	rp			#I pointer to current region
int	max_nregions		#I the  maximum number of regions

int	ncols, nlines, nregions, x1, x2, y1, y2
pointer	sp, line
real	x, y, xc, yc
int	rg_lstati(), getline(), nscan()
pointer	rg_lstatp()

begin
	# Allocate working space.
	call smark (sp)
	call salloc (line, SZ_LINE, TY_CHAR)

	# Allocate the arrays to hold the regions information,
	call rg_lrealloc (ls, max_nregions)

	# Get the constants.
	ncols = IM_LEN(im,1)
	nlines = IM_LEN(im,2)

	# Decode the regions string.
	nregions = min (rp - 1, rg_lstati (ls, NREGIONS))
	while (getline (fd, Memc[line]) != EOF &&  nregions < max_nregions) {

	    call sscan (Memc[line])
		call gargr (x)
		call gargr (y)
	    if (nscan() != 2)
		next

            # Compute a more accurate center.
            #if (rg_lstati (ls, CENTER) == YES) {
                #call rg_lcntr (im, x, y, DEF_CRADIUS, xc, yc)
            #} else {
                xc = x
                yc = y
            #}

            # Compute the data section.
            x1 = xc - rg_lstati (ls, DNX) / 2
            x2 = x1 + rg_lstati (ls, DNX) - 1
            if (IM_NDIM(im) == 1) {
                y1 = 1
                y2 = 1
            } else {
                y1 = yc - rg_lstati (ls, DNY) / 2
                y2 = y1 + rg_lstati (ls, DNY) - 1
            }

            # Make sure that the region is on the image.
            if (x1 >= 1 && x2 <= IM_LEN(im,1) && y1 >= 1 && y2 <=
	        IM_LEN(im,2)) {
                Memi[rg_lstatp(ls,RC1)+nregions] = x1
                Memi[rg_lstatp(ls,RC2)+nregions] = x2
                Memi[rg_lstatp(ls,RL1)+nregions] = y1
                Memi[rg_lstatp(ls,RL2)+nregions] = y2
                Memi[rg_lstatp(ls,RXSTEP)+nregions] = 1
                Memi[rg_lstatp(ls,RYSTEP)+nregions] = 1
		Memr[rg_lstatp(ls,RMEAN)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RMEDIAN)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RMODE)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RSIGMA)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RSKY)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RSKYERR)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RMAG)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RMAGERR)+nregions] = INDEFR
		Memi[rg_lstatp(ls,RNPTS)+nregions] = INDEFI
		Memr[rg_lstatp(ls,IMEAN)+nregions] = INDEFR
		Memr[rg_lstatp(ls,IMEDIAN)+nregions] = INDEFR
		Memr[rg_lstatp(ls,IMODE)+nregions] = INDEFR
		Memr[rg_lstatp(ls,ISIGMA)+nregions] = INDEFR
		Memr[rg_lstatp(ls,ISKY)+nregions] = INDEFR
		Memr[rg_lstatp(ls,ISKYERR)+nregions] = INDEFR
		Memr[rg_lstatp(ls,IMAG)+nregions] = INDEFR
		Memr[rg_lstatp(ls,IMAGERR)+nregions] = INDEFR
		Memi[rg_lstatp(ls,INPTS)+nregions] = INDEFI
		Memr[rg_lstatp(ls,RBSCALE)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RBSCALEERR)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RBZERO)+nregions] = INDEFR
		Memr[rg_lstatp(ls,RBZEROERR)+nregions] = INDEFR
		Memi[rg_lstatp(ls,RDELETE)+nregions] = LS_NO
		Memr[rg_lstatp(ls,RCHI)+nregions] = INDEFR
                nregions = nregions + 1
            }
	}

	call rg_lseti (ls, NREGIONS, nregions)
	if (nregions > 0)
	    call rg_lrealloc (ls, nregions)
	else
	    call rg_lrfree (ls)

	call sfree (sp)
	return (nregions)
end


# RG_LRPHOT -- Read the photometry from a file.

int procedure rg_lrphot (fd, ls, rp, max_nregions, refimage)

int	fd			#I the regions file descriptor
pointer	ls			#I pointer to the linscale structure
int	rp			#I pointer to current region
int	max_nregions		#I the  maximum number of regions
int	refimage		#I is the photometry for the reference image

int	nregions, maxnr
pointer	sp, line
real	sky, skyerr, mag, magerr
int	rg_lstati(), getline(), nscan()
pointer	rg_lstatp()

begin
	# Allocate working space.
	call smark (sp)
	call salloc (line, SZ_LINE, TY_CHAR)

	# Allocate the space to hold the arrays.
	if (refimage == YES) {
	    call rg_lrealloc (ls, max_nregions)
	    nregions = min (rp - 1, rg_lstati (ls, NREGIONS))
	    maxnr = max_nregions
	} else {
	    nregions = 0
	    maxnr = rg_lstati(ls, NREGIONS)
	}

	while (getline (fd, Memc[line]) != EOF &&  nregions < maxnr) {

	    call sscan (Memc[line])
		call gargr (sky)
		call gargr (skyerr)
		call gargr (mag)
		call gargr (magerr)
	    if (nscan() != 4)
		next

            Memi[rg_lstatp(ls,RC1)+nregions] = INDEFI
            Memi[rg_lstatp(ls,RC2)+nregions] = INDEFI
            Memi[rg_lstatp(ls,RL1)+nregions] = INDEFI
            Memi[rg_lstatp(ls,RL2)+nregions] = INDEFI
            Memi[rg_lstatp(ls,RXSTEP)+nregions] = INDEFI
            Memi[rg_lstatp(ls,RYSTEP)+nregions] = INDEFI

	    Memr[rg_lstatp(ls,RMEAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMEDIAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMODE)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RSIGMA)+nregions] = INDEFR
	    Memi[rg_lstatp(ls,RNPTS)+nregions] = INDEFI
	    if (refimage == YES) {
	        Memr[rg_lstatp(ls,RSKY)+nregions] = sky
	        Memr[rg_lstatp(ls,RSKYERR)+nregions] = skyerr
	        Memr[rg_lstatp(ls,RMAG)+nregions] = mag
	        Memr[rg_lstatp(ls,RMAGERR)+nregions] = magerr
	        Memr[rg_lstatp(ls,ISKY)+nregions] = INDEFR
	        Memr[rg_lstatp(ls,ISKYERR)+nregions] = INDEFR
	        Memr[rg_lstatp(ls,IMAG)+nregions] = INDEFR
	        Memr[rg_lstatp(ls,IMAGERR)+nregions] = INDEFR
	    }

	    Memr[rg_lstatp(ls,IMEAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMEDIAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMODE)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,ISIGMA)+nregions] = INDEFR
	    Memi[rg_lstatp(ls,INPTS)+nregions] = INDEFI
	    if (refimage == NO) {
	        Memr[rg_lstatp(ls,ISKY)+nregions] = sky
	        Memr[rg_lstatp(ls,ISKYERR)+nregions] = skyerr
	        Memr[rg_lstatp(ls,IMAG)+nregions] = mag
	        Memr[rg_lstatp(ls,IMAGERR)+nregions] = magerr
	    }

	    Memr[rg_lstatp(ls,RBSCALE)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RBSCALEERR)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RBZERO)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RBZEROERR)+nregions] = INDEFR
	    Memi[rg_lstatp(ls,RDELETE)+nregions] = LS_NO
	    Memr[rg_lstatp(ls,RCHI)+nregions] = INDEFR
            nregions = nregions + 1
	}

	if (refimage == YES) {
	    call rg_lseti (ls, NREGIONS, nregions)
	    if (nregions > 0)
	        call rg_lrealloc (ls, nregions)
	    else
	        call rg_lrfree (ls)
	} else if (nregions < rg_lstati (ls,NREGIONS)) {
	    call rg_lseti (ls, NREGIONS, nregions)
	}

	call sfree (sp)
	return (nregions)
end


# RG_LSREGIONS -- Procedure to compute the column and line limits given
# an image section. If the section is the null string then the region list
# is empty.

int procedure rg_lsregions (im, ls, rp, max_nregions)

pointer	im			#I pointer to the image
pointer	ls			#I pointer to the linscale structure
int	rp			#I pointer to the current region
int	max_nregions		#I maximum number of regions

int	ncols, nlines, nregions
int	x1, x2, y1, y2, xstep, ystep
pointer	sp, section, region
int	rg_lstati(), rg_lgsections()
pointer	rg_lstatp()

begin
	# Allocate working space.
	call smark (sp)
	call salloc (region, SZ_LINE, TY_CHAR)
	call salloc (section, SZ_LINE, TY_CHAR)
	call rg_lstats (ls, REGIONS, Memc[region], SZ_LINE)

	# Allocate the arrays to hold the regions information.
	call rg_lrealloc (ls, max_nregions)

	# Get the constants.
	ncols = IM_LEN(im,1)
	nlines = IM_LEN(im,2)

	if (Memc[region] != EOS) {

	    call sscan (Memc[region])
	        call gargwrd (Memc[section], SZ_LINE)

	    nregions = min (rp - 1, rg_lstati (ls, NREGIONS))
	    while (Memc[section] != EOS && nregions < max_nregions) {

		# Check for even dimensioned regions.
		if (rg_lgsections (Memc[section], x1, x2, xstep, y1, y2, ystep,
		    ncols, nlines) == OK) {
		    Memi[rg_lstatp(ls,RC1)+nregions] = x1
		    Memi[rg_lstatp(ls,RC2)+nregions] = x2
		    Memi[rg_lstatp(ls,RL1)+nregions] = y1
		    Memi[rg_lstatp(ls,RL2)+nregions] = y2
		    Memi[rg_lstatp(ls,RXSTEP)+nregions] = xstep
		    Memi[rg_lstatp(ls,RYSTEP)+nregions] = ystep
	    	    Memr[rg_lstatp(ls,RMEAN)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,RMEDIAN)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,RMODE)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,RSIGMA)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,RSKY)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,RSKYERR)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,RMAG)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,RMAGERR)+nregions] = INDEFR
	    	    Memi[rg_lstatp(ls,RNPTS)+nregions] = INDEFI
	    	    Memr[rg_lstatp(ls,IMEAN)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,IMEDIAN)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,IMODE)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,ISIGMA)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,ISKY)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,ISKYERR)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,IMAG)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,IMAGERR)+nregions] = INDEFR
	    	    Memi[rg_lstatp(ls,INPTS)+nregions] = INDEFI
	    	    Memr[rg_lstatp(ls,RBSCALE)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,RBSCALEERR)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,RBZERO)+nregions] = INDEFR
	    	    Memr[rg_lstatp(ls,RBZEROERR)+nregions] = INDEFR
	    	    Memi[rg_lstatp(ls,RDELETE)+nregions] = LS_NO
	    	    Memr[rg_lstatp(ls,RCHI)+nregions] = INDEFR
		    nregions = nregions + 1
		}
	        call gargwrd (Memc[section], SZ_LINE)
	    }

	} else {
	    Memi[rg_lstatp(ls,RC1)+nregions] = 1
	    Memi[rg_lstatp(ls,RC2)+nregions] = ncols
	    Memi[rg_lstatp(ls,RL1)+nregions] = 1
	    Memi[rg_lstatp(ls,RL2)+nregions] = nlines
	    Memi[rg_lstatp(ls,RXSTEP)+nregions] = 1
	    Memi[rg_lstatp(ls,RYSTEP)+nregions] = 1
	    Memr[rg_lstatp(ls,RMEAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMEDIAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMODE)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RSIGMA)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RSKY)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RSKYERR)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMAG)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RMAGERR)+nregions] = INDEFR
	    Memi[rg_lstatp(ls,RNPTS)+nregions] = INDEFI
	    Memr[rg_lstatp(ls,IMEAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMEDIAN)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMODE)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,ISIGMA)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,ISKY)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,ISKYERR)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMAG)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,IMAGERR)+nregions] = INDEFR
	    Memi[rg_lstatp(ls,INPTS)+nregions] = INDEFI
	    Memr[rg_lstatp(ls,RBSCALE)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RBSCALEERR)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RBZERO)+nregions] = INDEFR
	    Memr[rg_lstatp(ls,RBZEROERR)+nregions] = INDEFR
	    Memi[rg_lstatp(ls,RDELETE)+nregions] = LS_NO
	    Memr[rg_lstatp(ls,RCHI)+nregions] = INDEFR
	    nregions = 1
	}


	# Reallocate the correct amount of space.
	call rg_lseti (ls, NREGIONS, nregions)
	if (nregions > 0)
	    call rg_lrealloc (ls, nregions)
	else
	    call rg_lrfree (ls)

	call sfree (sp)
	return (nregions)
end


# RG_LGSECTIONS -- Decode an image section into column and line limits
# and a step size. Sections which describe the whole image are decoded into
# a block ncols * nlines long.

int procedure rg_lgsections (section, x1, x2, xstep, y1, y2, ystep, ncols,
        nlines)

char    section[ARB]            #I the input section string
int     x1, x2                  #O the output column section limits
int     xstep                   #O the output column step size
int     y1, y2                  #O the output line section limits
int     ystep                   #O the output line step size
int     ncols, nlines           #I the maximum number of lines and columns

int     ip
int     rg_lgdim()

begin
        ip = 1
        if (rg_lgdim (section, ip, x1, x2, xstep, ncols) == ERR)
            return (ERR)
        if (rg_lgdim (section, ip, y1, y2, ystep, nlines) == ERR)
            return (ERR)

        return (OK)
end


# RG_LGDIM -- Decode a single subscript expression to produce the
# range of values for that subscript (X1:X2), and the sampling step size, STEP.
# Note that X1 may be less than, greater than, or equal to X2, and STEP may
# be a positive or negative nonzero integer.  Various shorthand notations are
# permitted, as is embedded whitespace.

int procedure rg_lgdim (section, ip, x1, x2, step, limit)

char    section[ARB]            #I the input image section
int     ip                      #I/O pointer to the position in section string
int     x1                      #O first limit of dimension
int     x2                      #O second limit of dimension
int     step                    #O step size of dimension
int     limit                   #I maximum size of dimension

int     temp
int     ctoi()

begin
        x1 = 1
        x2 = limit
        step = 1

        while (IS_WHITE(section[ip]))
            ip = ip + 1

        if (section[ip] =='[')
            ip = ip + 1

        while (IS_WHITE(section[ip]))
            ip = ip + 1


 	# Get X1, X2.
        if (ctoi (section, ip, temp) > 0) {                     # [x1
            x1 = max (1, min (temp, limit))
            if (section[ip] == ':') {
                ip = ip + 1
                if (ctoi (section, ip, temp) == 0)              # [x1:x2
                    return (ERR)
                x2 = max (1, min (temp, limit))
            } else
                x2 = x1

        } else if (section[ip] == '-') {
            x1 = limit
            x2 = 1
            ip = ip + 1
            if (section[ip] == '*')
                ip = ip + 1

        } else if (section[ip] == '*')                          # [*
            ip = ip + 1

        while (IS_WHITE(section[ip]))
            ip = ip + 1

        # Get sample step size, if give.
        if (section[ip] == ':') {                               # ..:step
            ip = ip + 1
            if (ctoi (section, ip, step) == 0)
                return (ERR)
            else if (step == 0)
                return (ERR)
        }

 	# Allow notation such as "-*:5", (or even "-:5") where the step
        # is obviously supposed to be negative.

        if (x1 > x2 && step > 0)
            step = -step

        while (IS_WHITE(section[ip]))
            ip = ip + 1

        if (section[ip] == ',') {
            ip = ip + 1
            return (OK)
        } else if (section[ip] == ']')
            return (OK)
        else
            return (ERR)
end