aboutsummaryrefslogtreecommitdiff
path: root/noao/imred/vtel/unwrap.x
blob: a753ddf41d5b438625b363bc4eba1c76f5710159 (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
include <mach.h>
include <imhdr.h>

define	MAXBADLINES	20		# maximum number of bad lines
define	BADTHRESH	1000		# threshold for bad lines
define	FIXWIDTH	20		# Width of average for fixline

# UNWRAP -- Filter an iraf image.  This filter checks for binary wraparound
# in IRAF images.  The algorithm is described in detail in the help page.
# The program accepts templates for both input and output image lists.

procedure t_unwrap()

char	image[SZ_FNAME]			# input image template
char	outimage[SZ_FNAME]		# output image template
int	threshold1			# threshold value for first unwrap
int	threshold2			# threshold value for second unwrap
int	wrapval1			# wrapvalue for first unwrap
int	wrapval2			# wrapvalue for second unwrap
int	cstart				# column to start on
int	step				# number of steps to perform
bool	verbose				# verbose flag

int	i, j 
int	listin, listout
int	length, nlines
int	badlines[MAXBADLINES]
int	diff, nbad
char	tempimage[SZ_FNAME]
pointer	im, imout, lgp, lgp2, lpp, cck, sp

bool	clgetb()
int	imtopenp(), imtlen(), imtgetim(), clgeti()
pointer	immap(), imgl2s(), impl2s()
errchk	immap, imgl2s, impl2s

begin
	# Get parameters from the CL.
	listin = imtopenp ("image")
	listout = imtopenp ("outimage")
	threshold1 = clgeti("threshold1")
	wrapval1 = clgeti("wrapval1")
	threshold2 = clgeti("threshold2")
	wrapval2 = clgeti("wrapval2")
	cstart = clgeti("cstart")
	step = clgeti("step")
	verbose = clgetb("verbose")

	if (verbose) {
	    call printf ("\n\nUNWRAP: ")
	    call printf ("threshold1 = %d\n")
	    call pargi (threshold1)
	    call printf ("\twrapval1   = %d\n")
	    call pargi (wrapval1)
	    call printf ("\tthreshold2 = %d\n")
	    call pargi (threshold2)
	    call printf ("\twrapval2   = %d\n")
	    call pargi (wrapval2)
	    call printf ("\tcstart     = %d\n")
	    call pargi (cstart)
	    call printf ("\tstep       = %d\n\n")
	    call pargi (step)
	    call flush (STDOUT)
	}

	# Check the number of elements.
	if (imtlen (listin) != imtlen (listout)) {
	    call imtclose (listin)
	    call imtclose (listout)
	    call error (1, "Wrong number of elements in the operand lists")
	}

	# Get the next images from the lists.
	while (imtgetim (listin, image, SZ_FNAME) != EOF) {
	    if (imtgetim (listout, outimage, SZ_FNAME) != EOF) {

	        if (verbose) {
		    # Write out about the input file name and output file name.
		    call printf ("\tUnwrapping %s into %s.  ")
		        call pargstr (image)
		        call pargstr (outimage)
		    call flush (STDOUT)
	        }

	        # Open images.
	        iferr {
	            im = immap (image, READ_WRITE, 0)
	        } then {
		    call eprintf ("Cannot open image %s.\n")
		        call pargstr (image)
		    next
	        }

	        call xt_mkimtemp (image, outimage, tempimage, SZ_FNAME)

	        iferr {
	            imout = immap (outimage, NEW_COPY, im)
	        } then {
		    call eprintf ("Cannot open image %s, (already exists?).\n")
		        call pargstr (outimage)
		    next
	        }

	        length = IM_LEN(im,1)
	        nlines = IM_LEN(im,2)

	        # Set up the column check array, then unwrap line by line.
	        call smark (sp)
	        call salloc (cck, nlines, TY_INT)
	        call amovks (0, Memi[cck], nlines)
	        do i = 1, nlines {
	            lgp = imgl2s (im, i)
	            lpp = impl2s (imout, i)
	            call unwrapline (Mems[lgp], Mems[lpp], cck, length,
			threshold1, wrapval1, threshold2, wrapval2, cstart,
			step, i)
	        }

	        # Step 5 is the final step. (fixline)
	        if (step == 5) {
	            # Analyze the column, check for wraps.
	            nbad = 0
	            do i = 2, nlines {
	    	        diff = Memi[cck+i-1] - Memi[cck+i-2]
	    	        if (abs(diff) > BADTHRESH) {
	    	            # Mark this line bad.
	    	            nbad = nbad + 1
	    	            if (nbad > MAXBADLINES)
	    		        break
	    	            badlines[nbad] = i
	    	        }
	            }
	        }

	        # If number bad lines <= than MAXBADLINES, fix em, else, quit.
	        if (nbad <= MAXBADLINES && nbad > 0) {
	            do i = 1, nbad {

	    	        # GET the lines above and below the bad line and PUT the
	    	        # bad line.  Then average the above and below lines and
			# save in the bad line.

	    	        if (badlines[i] != 1 && badlines[i] != nlines) {
	    	            if ((badlines[i+1] - badlines[i]) == 1) {
	                        lgp = imgl2s (imout, badlines[i]-1)
	                        lgp2 = imgl2s (imout, badlines[i]+1)
	                        lpp = impl2s (imout, badlines[i])
	    	                do j = 1, length {
	    		            Mems[lpp+j-1] = int((real(Mems[lgp+j-1]) +
	    		                real(Mems[lgp2+j-1]))/2. + .5)
	    		        }
	    	            } 
	    	        }
	            }
	        }

	        if (verbose) {
	            call printf ("number of bad lines = %d\n")
	                call pargi (nbad)
	            do i = 1, nbad {
	                call printf ("\tbadlines[%d] = %d\n")
	    	        call pargi (i)
	    	        call pargi (badlines[i])
	            }
		    call printf ("\n")
	            call flush (STDOUT)
	        }

	        # Unmap images.
	        call imunmap (im)
	        call imunmap (imout)
	        call xt_delimtemp (outimage, tempimage)
	        call sfree (sp)

	    } # End of if (not EOF)
	}  # End of while loop on input images

	call imtclose (listin)
	call imtclose (listout)
end


# UNWRAPLINE -- Unwrap a line of the image.

procedure unwrapline (line1, line2, cck, numpix, threshold1, wrapval1,
	threshold2, wrapval2, cstart, step, whichline)

short	line1[numpix]			# input line
short	line2[numpix]			# output line
pointer	cck				# pointer to array for column check
int	numpix				# number of pixels per line
int	threshold1			# unwrap threshold for first unwrap
int	wrapval1			# unwrap value for first unwrap
int	threshold2			# unwrap threshold for second unwrap
int	wrapval2			# unwrap value for second unwrap
int	cstart				# column to start on
int	step				# steps to complete
int	whichline			# which line this is we are unwrapping

pointer	tl1, tl2, tl3			# pointers of temporary arrays
pointer	sp				# stack pointer
int	i, diff, sum
short	wrap				# wrap number

begin
	# Mark the stack and allcoate the temporary arrays.
	call smark (sp)
	call salloc (tl1, numpix, TY_SHORT)
	call salloc (tl2, numpix, TY_SHORT)
	call salloc (tl3, numpix, TY_SHORT)

	# Initialize wrap.
	wrap = 0

	# Copy the input line into the output line and the temporary arrays.
	call amovs (line1, line2, numpix)
	call amovs (line1, Mems[tl1], numpix)
	call amovs (line1, Mems[tl2], numpix)
	call amovs (line1, Mems[tl3], numpix)

	# Check the image width, do various things if the image is too small.

	# Too small for anything.
	if (numpix <= 4) {
	    call sfree (sp)
	    return
	}

	# Too small for step 5 (fixline).
	if (numpix <= FIXWIDTH && step == 5)
	    step = 4

	# Unwrap1 (step 1).
	Mems[tl1+cstart-1] = line1[cstart]
	do i = cstart+1, numpix {
	    diff = line1[i] - line1[i-1]
	    if (diff < -threshold1)
		wrap = wrap + 1
	    if (diff > threshold1)
		wrap = wrap - 1

	    Mems[tl1+i-1] = line1[i] + wrap * wrapval1
	}
	if (step == 1) {
	    call amovs (Mems[tl1], line2, numpix)
	    call sfree (sp)
	    return
	}

	# If the user wants it, step 2 (dif).
	do i = cstart, numpix
	    Mems[tl2+i-1] = Mems[tl1+i-1] - Mems[tl1+i-2]

	if (step == 2) {
	    call amovs (Mems[tl2], line2, numpix)
	    call sfree (sp)
	    return
	}

	# If the user wants it, step 3 (unwrap2).
	wrap = 0
	line2[cstart] = Mems[tl2+cstart-1]
	do i = cstart+1, numpix {
	    diff = Mems[tl2+i-1] - Mems[tl2+i-2]
	    if (diff < -threshold2)
	        wrap = wrap + 1
	    if (diff > threshold2)
		wrap = wrap - 1

	    line2[i] = Mems[tl2+i-1] + wrap * wrapval2
	}
	if (step == 3) {
	    call sfree (sp)
	    return
	}

	# If the user wants it, step 4 (reconstruct).
	do i = cstart, numpix
	    line2[i] = line2[i-1] + line2[i]
	
	if (step == 4) {
	    call sfree (sp)
	    return
	}

	# Again, if the user wants it, save data for step 5, (fixline).
	sum = 0
	do i = numpix-FIXWIDTH+1, numpix
	    sum = sum + line2[i]
	Memi[cck+whichline-1] = int(real(sum)/real(FIXWIDTH) + .5)

	call sfree (sp)
end