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
|
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
include <error.h>
include <imhdr.h>
include <ctype.h>
include <mwset.h>
# T_IMSLICE -- Slice an input image into a list of output images equal in
# length to the length of the dimension to be sliced. The remaining
# dimensions are unchanged. For a 1 dimensionsal image this task is a null
# operation.
procedure t_imslice()
pointer imtlist1 # Input image list
pointer imtlist2 # Output image list
pointer image1 # Input image
pointer image2 # Output image
int sdim # Dimension to be sliced
int verbose # Verbose mode
pointer sp
int list1, list2
bool clgetb()
int imtopen(), imtgetim(), imtlen(), btoi(), clgeti()
errchk sl_slice
begin
call smark (sp)
call salloc (imtlist1, SZ_FNAME, TY_CHAR)
call salloc (imtlist2, SZ_FNAME, TY_CHAR)
call salloc (image1, SZ_FNAME, TY_CHAR)
call salloc (image2, SZ_FNAME, TY_CHAR)
# Get task parameters.
call clgstr ("input", Memc[imtlist1], SZ_FNAME)
call clgstr ("output", Memc[imtlist2], SZ_FNAME)
sdim = clgeti ("slice_dimension")
verbose = btoi (clgetb ("verbose"))
list1 = imtopen (Memc[imtlist1])
list2 = imtopen (Memc[imtlist2])
if (imtlen (list1) != imtlen (list2)) {
call imtclose (list1)
call imtclose (list2)
call error (0, "Number of input and output images not the same.")
}
# Loop over the set of input and output images
while ((imtgetim (list1, Memc[image1], SZ_FNAME) != EOF) &&
(imtgetim (list2, Memc[image2], SZ_FNAME) != EOF))
call sl_imslice (Memc[image1], Memc[image2], sdim, verbose)
call imtclose (list1)
call imtclose (list2)
call sfree (sp)
end
# SL_IMSLICE -- Procedure to slice an n-dimensional image into a set
# of images with one fewer dimensions. A number is appendend to the
# output image name indicating which element of the n-th dimension the
# new image originated from.
procedure sl_imslice (image1, image2, sdim, verbose)
char image1[ARB] # input image
char image2[ARB] # output image
int sdim # slice dimension
int verbose # verbose mode
int i, j, ndim, fdim, ncols, nlout, nimout, pdim
int axno[IM_MAXDIM], axval[IM_MAXDIM]
pointer sp, inname, outname, outsect, im1, im2, buf1, buf2, vim1, vim2
pointer mw, vs, ve
real shifts[IM_MAXDIM]
pointer immap(), mw_openim()
int mw_stati()
int imgnls(), imgnli(), imgnll(), imgnlr(), imgnld(), imgnlx()
int imggss(), imggsi(), imggsl(), imggsr(), imggsd(), imggsx()
int impnls(), impnli(), impnll(), impnlr(), impnld(), impnlx()
bool envgetb()
errchk imgnls(), imgnli(), imgnll(), imgnlr(), imgnld(), imgnlx()
errchk imggss(), imggsi(), imggsl(), imggsr(), imggsd(), imggsx()
errchk impnls(), impnli(), impnll(), impnlr(), impnld(), impnlx()
begin
iferr (im1 = immap (image1, READ_ONLY, 0)) {
call erract (EA_WARN)
return
}
ndim = IM_NDIM(im1)
# Check that sdim is in range.
if (sdim > ndim) {
call printf ("Image %s has fewer than %d dimensions.\n")
call pargstr (image1)
call pargi (sdim)
call imunmap (im1)
return
}
# Cannot slice 1D images.
if (ndim == 1) {
call printf ("Image %s is 1 dimensional.\n")
call pargstr (image1)
call imunmap (im1)
return
}
# Cannot slice an image which is degnerate in slice dimension.
#if (IM_LEN(im1,sdim) == 1) {
#call printf ("Image %s is degenerate in the %d dimension.\n")
#call pargstr (image1)
#call pargi (sdim)
#call imunmap (im1)
#return
#}
call smark (sp)
call salloc (inname, SZ_LINE, TY_CHAR)
call salloc (outname, SZ_FNAME, TY_CHAR)
call salloc (outsect, SZ_LINE, TY_CHAR)
call salloc (vs, IM_MAXDIM, TY_LONG)
call salloc (ve, IM_MAXDIM, TY_LONG)
call salloc (vim1, IM_MAXDIM, TY_LONG)
call salloc (vim2, IM_MAXDIM, TY_LONG)
# Compute the number of output images. and the number of columns
nimout = IM_LEN(im1, sdim)
# Compute the number of lines and columns in the output image.
if (sdim == 1) {
fdim = 2
ncols = IM_LEN(im1,2)
} else {
fdim = 1
ncols = IM_LEN(im1,1)
}
nlout = 1
do i = 1, sdim - 1
nlout = nlout * IM_LEN(im1,i)
do i = sdim + 1, ndim
nlout = nlout * IM_LEN(im1,i)
nlout = nlout / ncols
call amovkl (long(1), Meml[vim1], IM_MAXDIM)
do i = 1, nimout {
# Construct the output image name.
call sprintf (Memc[outname], SZ_FNAME, "%s%03d")
call pargstr (image2)
call pargi (i)
# Open the output image.
iferr (im2 = immap (Memc[outname], NEW_COPY, im1)) {
call erract (EA_WARN)
call imunmap (im1)
call sfree (sp)
return
} else {
IM_NDIM(im2) = ndim - 1
do j = 1, sdim - 1
IM_LEN(im2,j) = IM_LEN(im1,j)
do j = sdim + 1, IM_NDIM(im1)
IM_LEN(im2,j-1) = IM_LEN(im1,j)
}
# Print messages on the screen.
if (verbose == YES) {
call sl_einsection (im1, i, sdim, Memc[inname], SZ_LINE)
call sl_esection (im2, Memc[outsect], SZ_LINE)
call printf ("Copied image %s %s -> %s %s\n")
call pargstr (image1)
call pargstr (Memc[inname])
call pargstr (Memc[outname])
call pargstr (Memc[outsect])
call flush (STDOUT)
}
# Initialize the v vectors for each new image.
if (sdim != ndim) {
do j = 1, ndim {
if (j == sdim) {
Meml[vs+j-1] = i
Meml[ve+j-1] = i
} else if (j == fdim) {
Meml[vs+j-1] = 1
Meml[ve+j-1] = IM_LEN(im1,j)
} else {
Meml[vs+j-1] = 1
Meml[ve+j-1] = 1
}
}
}
# Loop over the appropriate range of lines.
call amovkl (long(1), Meml[vim2], IM_MAXDIM)
switch (IM_PIXTYPE(im1)) {
case TY_SHORT:
if (sdim == ndim) {
do j = 1, nlout {
if (impnls (im2, buf2, Meml[vim2]) == EOF)
call error (0, "Error writing output image.")
if (imgnls (im1, buf1, Meml[vim1]) == EOF)
call error (0, "Error reading input image.")
call amovs (Mems[buf1], Mems[buf2], ncols)
}
} else {
do j = 1, nlout {
if (impnls (im2, buf2, Meml[vim2]) == EOF)
call error (0, "Error writing output image.")
buf1 = imggss (im1, Meml[vs], Meml[ve], IM_NDIM(im1))
if (buf1 == EOF)
call error (0, "Error reading input image.")
call amovs (Mems[buf1], Mems[buf2], ncols)
call sl_loop (Meml[vs], Meml[ve], IM_LEN(im1,1), fdim,
sdim, ndim)
}
}
case TY_USHORT, TY_INT:
if (sdim == ndim) {
do j = 1, nlout {
if (impnli (im2, buf2, Meml[vim2]) == EOF)
call error (0, "Error writing output image.")
if (imgnli (im1, buf1, Meml[vim1]) == EOF)
call error (0, "Error reading input image.")
call amovi (Memi[buf1], Memi[buf2], ncols)
}
} else {
do j = 1, nlout {
if (impnli (im2, buf2, Meml[vim2]) == EOF)
call error (0, "Error writing output image.")
buf1= imggsi (im1, Meml[vs], Meml[ve], IM_NDIM(im1))
if (buf1 == EOF)
call error (0, "Error reading input image.")
call amovi (Memi[buf1], Memi[buf2], ncols)
call sl_loop (Meml[vs], Meml[ve], IM_LEN(im1,1), fdim,
sdim, ndim)
}
}
case TY_LONG:
if (sdim == ndim) {
do j = 1, nlout {
if (impnll (im2, buf2, Meml[vim2]) == EOF)
call error (0, "Error writing output image.")
if (imgnll (im1, buf1, Meml[vim1]) == EOF)
call error (0, "Error reading input image.")
call amovl (Meml[buf1], Meml[buf2], ncols)
}
} else {
do j = 1, nlout {
if (impnll (im2, buf2, Meml[vim2]) == EOF)
call error (0, "Error writing output image.")
buf1 = imggsl (im1, Meml[vs], Meml[ve], IM_NDIM(im1))
if (buf1 == EOF)
call error (0, "Error reading input image.")
call amovl (Meml[buf1], Meml[buf2], ncols)
call sl_loop (Meml[vs], Meml[ve], IM_LEN(im1,1), fdim,
sdim, ndim)
}
}
case TY_REAL:
if (sdim == ndim) {
do j = 1, nlout {
if (impnlr (im2, buf2, Meml[vim2]) == EOF)
call error (0, "Error writing output image.")
if (imgnlr (im1, buf1, Meml[vim1]) == EOF)
call error (0, "Error reading input image.")
call amovr (Memr[buf1], Memr[buf2], ncols)
}
} else {
do j = 1, nlout {
if (impnlr (im2, buf2, Meml[vim2]) == EOF)
call error (0, "Error writing output image.")
buf1 = imggsr (im1, Meml[vs], Meml[ve], IM_NDIM(im1))
if (buf1 == EOF)
call error (0, "Error reading input image.")
call amovr (Memr[buf1], Memr[buf2], ncols)
call sl_loop (Meml[vs], Meml[ve], IM_LEN(im1,1),
fdim, sdim, ndim)
}
}
case TY_DOUBLE:
if (sdim == ndim) {
do j = 1, nlout {
if (impnld (im2, buf2, Meml[vim2]) == EOF)
call error (0, "Error writing output image.")
if (imgnld (im1, buf1, Meml[vim1]) == EOF)
call error (0, "Error reading input image.")
call amovd (Memd[buf1], Memd[buf2], ncols)
}
} else {
do j = 1, nlout {
if (impnld (im2, buf2, Meml[vim2]) == EOF)
call error (0, "Error writing output image.")
buf1 = imggsd (im1, Meml[vs], Meml[ve], IM_NDIM(im1))
if (buf1 == EOF)
call error (0, "Error reading input image.")
call amovd (Memd[buf1], Memd[buf2], ncols)
call sl_loop (Meml[vs], Meml[ve], IM_LEN(im1,1), fdim,
sdim, ndim)
}
}
case TY_COMPLEX:
if (sdim == ndim) {
do j = 1, nlout {
if (impnlx (im2, buf2, Meml[vim2]) == EOF)
call error (0, "Error writing output image.")
if (imgnlx (im1, buf1, Meml[vim1]) == EOF)
call error (0, "Error reading input image.")
call amovx (Memx[buf1], Memx[buf2], ncols)
}
} else {
do j = 1, nlout {
if (impnlx (im2, buf2, Meml[vim2]) == EOF)
call error (0, "Error writing output image.")
buf1 = imggsx (im1, Meml[vs], Meml[ve], IM_NDIM(im1))
if (buf1 == EOF)
call error (0, "Error reading input image.")
call amovx (Memx[buf1], Memx[buf2], ncols)
call sl_loop (Meml[vs], Meml[ve], IM_LEN(im1,1), fdim,
sdim, ndim)
}
}
}
# Update the wcs.
if (! envgetb ("nowcs")) {
# Open and shift the wcs.
mw = mw_openim (im1)
call aclrr (shifts, ndim)
shifts[sdim] = -(i - 1)
call mw_shift (mw, shifts, (2 ** ndim - 1))
# Get and reset the axis map.
pdim = mw_stati (mw, MW_NPHYSDIM)
call mw_gaxmap (mw, axno, axval, pdim)
do j = 1, pdim {
if (axno[j] < sdim) {
next
} else if (axno[j] > sdim) {
axno[j] = axno[j] - 1
} else {
axno[j] = 0
axval[j] = i - 1
}
}
call mw_saxmap (mw, axno, axval, pdim)
call mw_savim (mw, im2)
call mw_close (mw)
}
call imunmap (im2)
}
call imunmap (im1)
call sfree (sp)
end
# SL_LOOP -- Increment the vector V from VS to VE (nested do loops cannot
# be used because of the variable number of dimensions).
procedure sl_loop (vs, ve, ldim, fdim, sdim, ndim)
long vs[ndim] # vector of starting points
long ve[ndim] # vector of ending points
long ldim[ndim] # vector of dimension lengths
int fdim # first dimension
int sdim # slice dimension
int ndim # number of dimensions
int dim
begin
for (dim = fdim+1; dim <= ndim; dim = dim + 1) {
if (dim == sdim)
next
vs[dim] = vs[dim] + 1
ve[dim] = vs[dim]
if (vs[dim] - ldim[dim] == 1) {
if (dim < ndim) {
vs[dim] = 1
ve[dim] = 1
} else
break
} else
break
}
end
# SL_EINSECTION -- Encode the dimensions of an image where the element of
# the slice dimension is fixed in section notation.
procedure sl_einsection (im, el, sdim, section, maxch)
pointer im # pointer to the image
int el # element of last dimension
int sdim # slice dimension
char section[ARB] # output section
int maxch # maximum number of characters in output section
int i, op
int ltoc(), gstrcat()
begin
op = 1
section[1] = '['
op = op + 1
# Encode dimensions up to the slice dimension.
for (i = 1; i <= sdim - 1 && op <= maxch; i = i + 1) {
op = op + ltoc (long(1), section[op], maxch)
op = op + gstrcat (":", section[op], maxch)
op = op + ltoc (IM_LEN(im,i), section[op], maxch)
op = op + gstrcat (",", section[op], maxch)
}
# Encode the slice dimension.
op = op + ltoc (el, section[op], maxch)
op = op + gstrcat (",", section[op], maxch)
# Encode dimensions above the slice dimension.
for (i = sdim + 1; i <= IM_NDIM(im); i = i + 1) {
op = op + ltoc (long(1), section[op], maxch)
op = op + gstrcat (":", section[op], maxch)
op = op + ltoc (IM_LEN(im,i), section[op], maxch)
op = op + gstrcat (",", section[op], maxch)
}
section[op-1] = ']'
section[op] = EOS
end
# SL_ESECTION -- Encode the dimensions of an image in section notation.
procedure sl_esection (im, section, maxch)
pointer im # pointer to the image
char section[ARB] # output section
int maxch # maximum number of characters in output section
int i, op
int ltoc(), gstrcat()
begin
op = 1
section[1] = '['
op = op + 1
for (i = 1; i <= IM_NDIM(im); i = i + 1) {
op = op + ltoc (long(1), section[op], maxch)
op = op + gstrcat (":", section[op], maxch)
op = op + ltoc (IM_LEN(im,i), section[op], maxch)
op = op + gstrcat (",", section[op], maxch)
}
section[op-1] = ']'
section[op] = EOS
end
|