aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/immatch/src/xregister/rgxregions.x
blob: ed682f61d5792f002f7e2febd442a2525bba6fd6 (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
include <fset.h>
include <ctype.h>
include <imhdr.h>
include "xregister.h"

# RG_XREGIONS -- Decode the image sections into regions. If the sections string
# is NULL then the regions list is initially empty and depending on the mode
# of the task, XREGISTER will or will not complain.Otherwise the image
# sections specified in the sections string or file are decoded into a
# regions list.

int procedure rg_xregions (list, im, xc, rp)

int	list			#I pointer to the regions list
pointer	im			#I pointer to the reference image
pointer	xc			#I pointer to the cross-correlation structure
int	rp			#I index of the current region

int	fd, nregions
pointer	sp, fname, regions
int	rg_xgrid(), rg_xgregions(), rg_xrregions(), rg_xstati(), fntgfnb()
int	open()
errchk	fntgfnb(), open(), close()

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

	call rg_xstats (xc, REGIONS, Memc[regions], SZ_LINE)
	if (rp < 1 || rp > MAX_NREGIONS || Memc[regions] == EOS) {
	    nregions = 0
	} else if (rg_xgrid (im, xc, rp, MAX_NREGIONS) > 0) {
	    nregions = rg_xstati (xc, NREGIONS)
	} else if (rg_xgregions (im, xc, rp, MAX_NREGIONS) > 0) {
	    nregions = rg_xstati (xc, NREGIONS)
	} else if (list != NULL) {
	    iferr {
		if (fntgfnb (list, Memc[fname], SZ_FNAME) != EOF) {
	            fd = open (Memc[fname], READ_ONLY, TEXT_FILE)
	            nregions= rg_xrregions (fd, im, xc, rp, MAX_NREGIONS)
	            call close (fd)
		}
	    } then
		nregions = 0
	} else
	    nregions = 0

	call sfree (sp)

	return (nregions)
end


# RG_XMKREGIONS -- Create a list of regions by marking image sections
# on the image display.

int procedure rg_xmkregions (im, xc, rp, max_nregions, regions, maxch)

pointer	im			#I pointer to the reference image
pointer	xc			#I pointer to the cross-correlation structure
int	rp			#I index of the current region
int	max_nregions		#I the maximum number of regions
char	regions[ARB]		#O the output regions string
int	maxch			#I maximum size of the output regions string

int	op, nregions, wcs, key
pointer	sp, region, section, cmd
real	xll, yll, xur, yur
int	rg_xstati(), clgcur(), gstrcpy()
pointer	rg_xstatp()

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

	# Allocate the arrays to hold the regions information,
	call rg_xrealloc (xc, max_nregions)

	# Initialize.
	nregions = min (rp-1, rg_xstati (xc, NREGIONS))
	op = 1

	# Mark the sections on the display.
	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

	    if (xll < 1.0 || xur > IM_LEN(im,1) || yll < 1.0 || yur >
		IM_LEN(im,2))
		break

	    Memi[rg_xstatp(xc,RC1)+nregions] = nint (xll)
	    Memi[rg_xstatp(xc,RC2)+nregions] = nint (xur)
	    Memi[rg_xstatp(xc,RL1)+nregions] = nint (yll)
	    Memi[rg_xstatp(xc,RL2)+nregions] = nint (yur)
	    Memr[rg_xstatp(xc,RZERO)+nregions] = INDEFR
	    Memr[rg_xstatp(xc,RXSLOPE)+nregions] = INDEFR
	    Memr[rg_xstatp(xc,RYSLOPE)+nregions] = INDEFR
	    Memr[rg_xstatp(xc,XSHIFTS)+nregions] = INDEFR
	    Memr[rg_xstatp(xc,YSHIFTS)+nregions] = INDEFR
	    nregions = nregions + 1

	    # Write the first 9 regions into 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)
	}
	call printf ("\n")

	# Reallocate the correct amount of space.
	call rg_xseti (xc, NREGIONS, nregions)
	if (nregions > 0)
	    call rg_xrealloc (xc, nregions)
	else 
	    call rg_xrfree (xc)

	call sfree (sp)

	return (nregions)
end


# RG_XGRID - Decode the regions from a grid specification.

int procedure rg_xgrid (im, xc, rp, max_nregions)

pointer	im			#I pointer to the reference image
pointer	xc			#I pointer to the cross-correlation 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_xstati(), nscan(), strcmp()
pointer	rg_xstatp()

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_xrealloc (xc, max_nregions)

	# Initialize.
	call rg_xstats (xc, REGIONS, Memc[region], SZ_LINE)
	ncols = IM_LEN(im,1)
	nlines = IM_LEN(im,2)
	nregions = min (rp - 1, rg_xstati (xc, 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_xstatp(xc,RC1)+nregions] = i 
		Memi[rg_xstatp(xc,RC2)+nregions] = i + nxcols - 1
		Memi[rg_xstatp(xc,RL1)+nregions] = j
		Memi[rg_xstatp(xc,RL2)+nregions] = j + nylines - 1
		Memr[rg_xstatp(xc,RZERO)+nregions] = INDEFR
		Memr[rg_xstatp(xc,RXSLOPE)+nregions] = INDEFR
		Memr[rg_xstatp(xc,RYSLOPE)+nregions] = INDEFR
		Memr[rg_xstatp(xc,XSHIFTS)+nregions] = INDEFR
		Memr[rg_xstatp(xc,YSHIFTS)+nregions] = INDEFR
		nregions = nregions + 1
	    }
	}

	call rg_xseti (xc, NREGIONS, nregions)
	if (nregions > 0)
	    call rg_xrealloc (xc, nregions)
	else
	    call rg_xrfree (xc)
	call sfree (sp)

	return (nregions)
end


# RG_XRREGIONS -- Read and decode the regions from a file.

int procedure rg_xrregions (fd, im, xc, rp, max_nregions)

int	fd			#I regions file descriptor
pointer	im			#I pointer to the reference image
pointer	xc			#I pointer to the cross-correlation structure
int	rp			#I index of the current region
int	max_nregions		#I the maximum number of regions

int	ncols, nlines, nregions, x1, y1, x2, y2, step
pointer	sp, line, section
int	rg_xstati(), getline(), rg_xgsections()
pointer	rg_xstatp()

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_xrealloc (xc, max_nregions)

	# Initialize.
	ncols = IM_LEN(im,1)
	nlines = IM_LEN(im,2)
	nregions = min (rp - 1, rg_xstati (xc, NREGIONS))

	# Decode the regions string.
	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)) {
		if (rg_xgsections (Memc[section], x1, x2, step, y1, y2, step,
		    ncols, nlines) == OK) {
		    Memi[rg_xstatp(xc,RC1)+nregions] = x1
		    Memi[rg_xstatp(xc,RC2)+nregions] = x2
		    Memi[rg_xstatp(xc,RL1)+nregions] = y1
		    Memi[rg_xstatp(xc,RL2)+nregions] = y2
		    Memr[rg_xstatp(xc,RZERO)+nregions] = INDEFR
		    Memr[rg_xstatp(xc,RXSLOPE)+nregions] = INDEFR
		    Memr[rg_xstatp(xc,RYSLOPE)+nregions] = INDEFR
		    Memr[rg_xstatp(xc,XSHIFTS)+nregions] = INDEFR
		    Memr[rg_xstatp(xc,YSHIFTS)+nregions] = INDEFR
		    nregions = nregions + 1
		}
		call gargwrd (Memc[section], SZ_LINE)
	    }
	}

	# Reallocate the correct amount of space.
	call rg_xseti (xc, NREGIONS, nregions)
	if (nregions > 0)
	    call rg_xrealloc (xc, nregions)
	else
	    call rg_xrfree (xc)

	call sfree (sp)

	return (nregions)
end


# RG_XGREGIONS -- Decode a list of regions from a string containing
# a list of sections.

int procedure rg_xgregions (im, xc, rp, max_nregions)

pointer	im			#I pointer to the reference image
pointer	xc			#I pointer to cross-correlation structure
int	rp			#I the index of the current region
int	max_nregions		#I the maximum number of regions

int	ncols, nlines, nregions, x1, x2, y1, y2, step
pointer	sp, section, region
int	rg_xstati(), rg_xgsections()
pointer	rg_xstatp()

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_xrealloc (xc, max_nregions)

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

	# Decode the sections
	call sscan (Memc[region])
	    call gargwrd (Memc[section], SZ_LINE)
	while ((Memc[section] != EOS) && (nregions < max_nregions)) {
	    if (rg_xgsections (Memc[section], x1, x2, step, y1, y2, step,
		ncols, nlines) == OK) {
		Memi[rg_xstatp(xc,RC1)+nregions] = x1
		Memi[rg_xstatp(xc,RC2)+nregions] = x2
		Memi[rg_xstatp(xc,RL1)+nregions] = y1
		Memi[rg_xstatp(xc,RL2)+nregions] = y2
		Memr[rg_xstatp(xc,RZERO)+nregions] = INDEFR
		Memr[rg_xstatp(xc,RXSLOPE)+nregions] = INDEFR
		Memr[rg_xstatp(xc,RYSLOPE)+nregions] = INDEFR
		Memr[rg_xstatp(xc,XSHIFTS)+nregions] = INDEFR
		Memr[rg_xstatp(xc,YSHIFTS)+nregions] = INDEFR
		nregions = nregions + 1
	    }
	    call gargwrd (Memc[section], SZ_LINE)
	}


	# Reallocate the correct amount of space.
	call rg_xseti (xc, NREGIONS, nregions)
	if (nregions > 0)
	    call rg_xrealloc (xc, nregions)
	else
	    call rg_xrfree (xc)

	call sfree (sp)

	return (nregions)
end


# RG_XGSECTIONS -- 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_xgsections (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_xgdim()

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

	return (OK)
end


# RG_XGDIM -- 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_xgdim (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