aboutsummaryrefslogtreecommitdiff
path: root/noao/nproto/ir/irmatch2d.x
blob: 6e1bcd50c852e02ad53c31c78575643446275830 (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
include <imhdr.h>
include "iralign.h"

# IR_M2MATCH -- Compute the intensity matching parameters.

procedure ir_m2match (ir, im, ranges, ic1, ic2, il1, il2, deltax, deltay,
	deltai)

pointer	ir			# pointer to the ir structure
pointer	im			# pointer to the input image
int	ranges[ARB]		# ranges of data to align
int	ic1[ARB]		# array of input begin columns
int	ic2[ARB]		# array of input end columns
int	il1[ARB]		# array of input begin lines
int	il2[ARB]		# array of input end lines
real	deltax[ARB]		# array of x shifts
real	deltay[ARB]		# array of y shifts
real	deltai[ARB]		# array of i shifts

begin
	# Initialize the intensity subraster.
	call ir_vecinit (deltai, IR_NXSUB(ir) * IR_NYSUB(ir), ranges)
	if (ranges[1] == NULL)
	    return

	# Match the intensities in the direction of observation.
	call ir_omatch (ir, im, ic1, ic2, il1, il2, deltax, deltay, deltai)

	# Match the intensities in the other direction.
	call ir_nmatch (ir, im, ic1, ic2, il1, il2, deltax, deltay, deltai)
end


# IR_OMATCH -- Procedure to match images in the direction of observation
# direction. 

procedure ir_omatch (ir, im, ic1, ic2, il1, il2, deltax, deltay, deltai)

pointer	ir			# pointer to the ir structure
pointer	im			# pointer to the input image
int	ic1[ARB]		# beginning column limits
int	ic2[ARB]		# ending column limits
int	il1[ARB]		# beginning line limits
int	il2[ARB]		# ending line limits
real	deltax[ARB]		# array of x shifts
real	deltay[ARB]		# array of y shifts
real	deltai[ARB]		# array of intensity shifts

int	num, nimages, nrasters
int	pc1, pc2, pl1, pl2, c1, c2, l1, l2
int	pideltax, pideltay, ideltax, ideltay
int	oc1, oc2, ol1, ol2, clim1, clim2, llim1, llim2
pointer	buf
real	pmedian, median, dif

int	ir_overlap()
pointer	imgs2r()
real	amedr()

begin
	# Compute the do loop parameters.
	nimages = IR_NXSUB(ir) * IR_NYSUB(ir)
	if (IR_ORDER(ir) == IR_ROW)
	    nrasters = IR_NXSUB(ir)
	else
	    nrasters = IR_NYSUB(ir)

	# Loop over the subrasters to be matched.
	for (num = 1; num <= nimages; num = num + 1) {

	    if (mod (num, nrasters) == 1) {

		# Get the position and shift for the first subraster in
		# the column.
		pideltax = nint (deltax[num])
		pideltay = nint (deltay[num])
		pc1 = ic1[num]
		pc2 = ic2[num]
		pl1 = il1[num]
		pl2 = il2[num]
		num = num + 1
		dif = 0.0

		# Get the the position and shift for the next subraster in
		# the column.to be
		ideltax = nint (deltax[num])
		ideltay = nint (deltay[num])
		c1 = ic1[num]
		c2 = ic2[num]
		l1 = il1[num]
		l2 = il2[num]

	    } else {

		# Reset the coordinates of the previous subraster.
		pc1 = c1
		pc2 = c2
		pl1 = l1
		pl2 = l2
		pideltax = ideltax
		pideltay = ideltay

		# Get the positions and shifts of the next subraster.
		ideltax = nint (deltax[num])
		ideltay = nint (deltay[num])
		c1 = ic1[num]
		c2 = ic2[num]
		l1 = il1[num]
		l2 = il2[num]

	    }

	    # Compute the overlap region.
	    if (ir_overlap (pc1 + pideltax, pc2 + pideltax, pl1 + pideltay,
		pl2 + pideltay, c1 + ideltax, c2 + ideltax, l1 + ideltay,
		l2 + ideltay, oc1, oc2, ol1, ol2) == YES) {

		clim1 = max (pc1, min (oc1 - pideltax, pc2))
		clim2 = min (pc2, max (oc2 - pideltax, pc1))
		llim1 = max (pl1, min (ol1 - pideltay, pl2))
		llim2 = min (pl2, max (ol2 - pideltay, pl1))
		buf = imgs2r (im, clim1, clim2, llim1, llim2)
		pmedian = amedr (Memr[buf], (clim2 - clim1 + 1) * (llim2 -
		    llim1 + 1))

		clim1 = max (c1, min (oc1 - ideltax, c2))
		clim2 = min (c2, max (oc2 - ideltax, c1))
		llim1 = max (l1, min (ol1 - ideltay, l2))
		llim2 = min (l2, max (ol2 - ideltay, l1))
		buf = imgs2r (im, clim1, clim2, llim1, llim2)
		median = amedr (Memr[buf], (clim2 - clim1 + 1) * (llim2 -
		    llim1 + 1))

		dif = dif + median - pmedian
		if (! IS_INDEFR (deltai[num]))
		    deltai[num] = deltai[num] -  dif
	    }
	}
end


# IR_NMATCH -- Procedure to match images in the other direction. 

procedure ir_nmatch (ir, im, ic1, ic2, il1, il2, deltax, deltay, deltai)

pointer	ir		# pointer to the ir structure
pointer	im		# pointer to the input image
int	ic1[ARB]	# array of beginning columns
int	ic2[ARB]	# array of ending columns
int	il1[ARB]	# array of beginning lines
int	il2[ARB]	# array of ending lines
real	deltax[ARB]	# array of x shifts
real	deltay[ARB]	# array of y shifts
real	deltai[ARB]	# array of intensity shifts

int	num, nrasters, fac, nimages, count
int	pc1, pc2, pl1, pl2, c1, c2, l1, l2
int	pideltax, pideltay, ideltax, ideltay
int	oc1, oc2, ol1, ol2, clim1, clim2, llim1, llim2
pointer	buf
real	pmedian, median, pdif, dif, tdif

int	ir_overlap()
pointer	imgs2r()
real	amedr()

begin
	# Compute the do loop parameters.
	nimages = IR_NXSUB(ir) * IR_NYSUB(ir)
	if (IR_ORDER(ir) == IR_ROW)
	    nrasters = IR_NXSUB(ir)
	else
	    nrasters = IR_NYSUB(ir)
	fac = 2 * nrasters

	# Loop over the subrasters to be matched.
	num = 1
	count = 1
	repeat {

	    # Get the position and shift for the first subraster.
	    if (num <= nrasters) {

		pideltax = nint (deltax[num])
		pideltay = nint (deltay[num])
		pc1 = ic1[num]
		pc2 = ic2[num]
		pl1 = il1[num]
		pl2 = il2[num]
		if (IS_INDEFR(deltai[num]))
		    pdif = 0.0
		else
		    pdif = deltai[num]
		tdif = 0.0
		if (IR_RASTER(ir) == YES) {
		    num = fac - num + 1
		    fac = fac + fac
		} else
		    num = num + nrasters

		# Get the the position and shift for the next.
		ideltax = nint (deltax[num])
		ideltay = nint (deltay[num])
		c1 = ic1[num]
		c2 = ic2[num]
		l1 = il1[num]
		l2 = il2[num]
		if (IS_INDEFR(deltai[num]))
		    dif = 0.0
		else
		    dif = deltai[num]

	    } else {

		# Reset the coordinates of the previous subraster.
		pc1 = c1
		pc2 = c2
		pl1 = l1
		pl2 = l2
		pideltax = ideltax
		pideltay = ideltay
		pdif = dif

		# Get the positions and shifts of the subraster to be adjusted.
		ideltax = nint (deltax[num])
		ideltay = nint (deltay[num])
		c1 = ic1[num]
		c2 = ic2[num]
		l1 = il1[num]
		l2 = il2[num]
		if (IS_INDEFR(deltai[num]))
		    dif = 0.0
		else
		    dif = deltai[num]

	    }

	    # Compute the overlap region.
	    if (ir_overlap (pc1 + pideltax, pc2 + pideltax, pl1 + pideltay,
		pl2 + pideltay, c1 + ideltax, c2 + ideltax, l1 + ideltay,
		l2 + ideltay, oc1, oc2, ol1, ol2) == YES) {

		clim1 = max (pc1, oc1 - pideltax)
		clim2 = min (pc2, oc2 - pideltax)
		llim1 = max (pl1, ol1 - pideltay)
		llim2 = min (pl2, ol2 - pideltay)
		buf = imgs2r (im, clim1, clim2, llim1, llim2)
		pmedian = amedr (Memr[buf], (clim2 - clim1 + 1) * (llim2 -
		    llim1 + 1))

		clim1 = max (c1, oc1 - ideltax)
		clim2 = min (c2, oc2 - ideltax)
		llim1 = max (l1, ol1 - ideltay)
		llim2 = min (l2, ol2 - ideltay)
		buf = imgs2r (im, clim1, clim2, llim1, llim2)
		median = amedr (Memr[buf], (clim2 - clim1 + 1) * (llim2 -
		    llim1 + 1))

		tdif = tdif + median + dif - pmedian - pdif
		if (! IS_INDEFR (deltai[num]))
		    deltai[num] = deltai[num] - tdif
	    }

	    if (IR_RASTER(ir) == YES) {
		num = fac - num + 1
		fac = fac + fac
	    } else
	        num = num + nrasters
	    if (num > nimages) {
		count = count + 1
		num = count
		fac = 2 * nrasters
	    }

	} until (count > nrasters)
end