aboutsummaryrefslogtreecommitdiff
path: root/noao/onedspec/odcombine/srcwt/icimstack.x
blob: d56286949b74b12bf10de17ac993012d2ad69278 (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<error.h>
include	<imhdr.h>


# IC_IMSTACK -- Stack images into a single image of higher dimension.

procedure ic_imstack (list, output, mask)

int	list		#I List of images
char	output[ARB]	#I Name of output image
char	mask[ARB]	#I Name of output mask

int	i, j, npix
long	line_in[IM_MAXDIM], line_out[IM_MAXDIM], line_outbpm[IM_MAXDIM]
pointer	sp, input, bpmname, key, in, out, inbpm, outbpm, buf_in, buf_out, ptr

int	imtgetim(), imtlen(), errget()
int	imgnls(), imgnli(), imgnll(), imgnlr(), imgnld(), imgnlx()
int	impnls(), impnli(), impnll(), impnlr(), impnld(), impnlx()
pointer	immap(), pm_newmask()
errchk	immap
errchk	imgnls, imgnli, imgnll, imgnlr, imgnld, imgnlx
errchk	impnls, impnli, impnll, impnlr, impnld, impnlx

begin
	call smark (sp)
	call salloc (input, SZ_FNAME, TY_CHAR)
	call salloc (bpmname, SZ_FNAME, TY_CHAR)
	call salloc (key, SZ_FNAME, TY_CHAR)

	iferr {
	    # Add each input image to the output image.
	    out = NULL; outbpm = NULL
	    i = 0
	    while (imtgetim (list, Memc[input], SZ_FNAME) != EOF) {

		i = i + 1
		in = NULL; inbpm = NULL
		ptr = immap (Memc[input], READ_ONLY, 0)
		in = ptr

		# For the first input image map the output image as a copy
		# and increment the dimension.  Set the output line counter.

		if (i == 1) {
		    ptr = immap (output, NEW_COPY, in)
		    out = ptr
		    IM_NDIM(out) = IM_NDIM(out) + 1
		    IM_LEN(out, IM_NDIM(out)) = imtlen (list)
		    npix = IM_LEN(out, 1)
		    call amovkl (long(1), line_out, IM_MAXDIM)

		    if (mask[1] != EOS) {
			ptr = immap (mask, NEW_COPY, in)
			outbpm = ptr
			IM_NDIM(outbpm) = IM_NDIM(outbpm) + 1
			IM_LEN(outbpm, IM_NDIM(outbpm)) = imtlen (list)
			call amovkl (long(1), line_outbpm, IM_MAXDIM)
		    }
		}

		# Check next input image for consistency with the output image.
		if (IM_NDIM(in) != IM_NDIM(out) - 1)
		    call error (0, "Input images not consistent")
		do j = 1, IM_NDIM(in) {
		    if (IM_LEN(in, j) != IM_LEN(out, j))
			call error (0, "Input images not consistent")
		}

		call sprintf (Memc[key], SZ_FNAME, "stck%04d")
		    call pargi (i)
		call imastr (out, Memc[key], Memc[input])

		# Copy the input lines from the image to the next lines of
		# the output image.  Switch on the output data type to optimize
		# IMIO.

		call amovkl (long(1), line_in, IM_MAXDIM)
		switch (IM_PIXTYPE (out)) {
		case TY_SHORT:
		    while (imgnls (in, buf_in, line_in) != EOF) {
			if (impnls (out, buf_out, line_out) == EOF)
			    call error (0, "Error writing output image")
			call amovs (Mems[buf_in], Mems[buf_out], npix)
		    }
		case TY_INT:
		    while (imgnli (in, buf_in, line_in) != EOF) {
			if (impnli (out, buf_out, line_out) == EOF)
			    call error (0, "Error writing output image")
			call amovi (Memi[buf_in], Memi[buf_out], npix)
		    }
		case TY_USHORT, TY_LONG:
		    while (imgnll (in, buf_in, line_in) != EOF) {
			if (impnll (out, buf_out, line_out) == EOF)
			    call error (0, "Error writing output image")
			call amovl (Meml[buf_in], Meml[buf_out], npix)
		    }
		case TY_REAL:
		    while (imgnlr (in, buf_in, line_in) != EOF) {
			if (impnlr (out, buf_out, line_out) == EOF)
			    call error (0, "Error writing output image")
			call amovr (Memr[buf_in], Memr[buf_out], npix)
		    }
		case TY_DOUBLE:
		    while (imgnld (in, buf_in, line_in) != EOF) {
			if (impnld (out, buf_out, line_out) == EOF)
			    call error (0, "Error writing output image")
			call amovd (Memd[buf_in], Memd[buf_out], npix)
		    }
		case TY_COMPLEX:
		    while (imgnlx (in, buf_in, line_in) != EOF) {
			if (impnlx (out, buf_out, line_out) == EOF)
			    call error (0, "Error writing output image")
			call amovx (Memx[buf_in], Memx[buf_out], npix)
		    }
		default:
		    while (imgnlr (in, buf_in, line_in) != EOF) {
			if (impnlr (out, buf_out, line_out) == EOF)
			    call error (0, "Error writing output image")
			call amovr (Memr[buf_in], Memr[buf_out], npix)
		    }
		}

		# Copy mask.
		if (mask[1] != EOS) {
		    iferr (call imgstr (in, "bpm", Memc[bpmname], SZ_FNAME)) {
			Memc[bpmname] = EOS
			ptr = pm_newmask (in, 27)
		    } else
			ptr = immap (Memc[bpmname], READ_ONLY, 0)
		    inbpm = ptr

		    if (IM_NDIM(inbpm) != IM_NDIM(outbpm) - 1)
			call error (0, "Input images not consistent")
		    do j = 1, IM_NDIM(inbpm) {
			if (IM_LEN(inbpm, j) != IM_LEN(outbpm, j))
			    call error (0, "Masks not consistent")
		    }

		    call amovkl (long(1), line_in, IM_MAXDIM)
		    while (imgnli (inbpm, buf_in, line_in) != EOF) {
			if (impnli (outbpm, buf_out, line_outbpm) == EOF)
			    call error (0, "Error writing output mask")
			call amovi (Memi[buf_in], Memi[buf_out], npix)
		    }

		    call sprintf (Memc[key], SZ_FNAME, "bpm%04d")
			call pargi (i)
		    call imastr (out, Memc[key], Memc[bpmname])

		    call imunmap (inbpm)
		}

		call imunmap (in)
	    }
	} then {
	    i = errget (Memc[key], SZ_FNAME)
	    call erract (EA_WARN)
	    if (outbpm != NULL) {
		call imunmap (outbpm)
		iferr (call imdelete (mask))
		    ;
	    }
	    if (out != NULL) {
		call imunmap (out)
		iferr (call imdelete (output))
		    ;
	    }
	    if (inbpm != NULL)
		call imunmap (inbpm)
	    if (in != NULL)
		call imunmap (in)
	    call sfree (sp)
	    call error (i, "Can't make temporary stack images")
	}

	# Finish up.
	if (outbpm != NULL) {
	    call imunmap (outbpm)
	    call imastr (out, "bpm", mask)
	}
	call imunmap (out)
	call sfree (sp)
end