aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/imgeom/src/shiftlines.x
blob: e0bd6d9a7d1b0297bf6642bc8d0840cf2a447160 (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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<imhdr.h>
include <imset.h>
include <math/iminterp.h>

define	NMARGIN		 3

# SH_LINES -- Shift image lines.
#
# If an integer shift is given then do an efficient integer shift
# without datatype I/O conversions and using array move operators.
# If the shift is non-integer then use image interpolation.

procedure sh_lines (im1, im2, shift, boundary, constant, interpstr)

pointer	im1				# Input image descriptor
pointer	im2				# Output image descriptor
real	shift				# Shift in pixels
int	boundary			# Boundary extension type
real	constant			# Constant boundary extension
char	interpstr[ARB]			# Interpolation type

int	i, nsinc, nincr, ncols, nimcols, nlines, nbpix, nmargin, interpolation
long	v1[IM_MAXDIM], v2[IM_MAXDIM], vout[IM_MAXDIM]
real	dx, deltax, cx
pointer	sp, x, asi, junk, buf1, buf2

bool	fp_equalr()
int	imggsr(), impnlr(), asigeti()

begin
	# Check for out of bounds shifts.
	ncols = IM_LEN(im1, 1)
	if (shift < -ncols || shift > ncols)
	    call error (0, "SHIFTLINES: Shift out of bounds")

	# Compute the shift.
	dx = abs (shift - int (shift))
	if (fp_equalr (dx, 0.0))
	    deltax = 0.0
	else if (shift > 0.0)
	    deltax = 1.0 - dx
	else
	    deltax = dx

	# Initialize the interpolation.
	call asitype (interpstr, interpolation, nsinc, nincr, cx)
	if (interpolation == II_LSINC || interpolation == II_SINC)
	    call asisinit (asi, II_LSINC, nsinc, 1, deltax - nint (deltax),
	        0.0)
	else
	    call asisinit (asi, interpolation, nsinc, 1, cx, 0.0)

	# Set up the image boundary conditions.
	if (interpolation == II_SINC || interpolation == II_LSINC)
	    nmargin = asigeti (asi, II_ASINSINC)
	else
	    nmargin = NMARGIN
	nbpix = int (abs (shift) + 1.0) + nmargin
	call imseti (im1, IM_TYBNDRY, boundary)
	call imseti (im1, IM_NBNDRYPIX, nbpix)
	call imsetr (im1, IM_BNDRYPIXVAL, constant)

	# Allocate space for and set up the interpolation coordinates.
	call smark (sp)
	call salloc (x, 2 * ncols, TY_REAL)
	deltax = deltax + nmargin
	if (interpolation == II_DRIZZLE) {
	    do i = 1, ncols {
	        Memr[x+2*i-2] = i + deltax - 0.5
	        Memr[x+2*i-1] = i + deltax + 0.5
	    }
	} else {
	    do i = 1, ncols
	        Memr[x+i-1] = i + deltax
	}

	# Initialize the input v vectors.
	cx = 1. - nmargin - shift
	if ((cx <= 0.0) && (! fp_equalr (dx, 0.0)))
	    v1[1] = long (cx) - 1
	else
	    v1[1] = long (cx)
	v2[1] = ncols - shift + nmargin + 1
	nimcols = v2[1] - v1[1] + 1
	do i = 2, IM_NDIM(im1) {
	    v1[i] = long (1)
	    v2[i] = long (1)
	}

	# Compute the number of output lines.
	nlines = 1
	do i = 2, IM_NDIM(im1)
	    nlines = nlines * IM_LEN(im1, i)

	# Initialize the output v vector.
	call amovkl (long(1), vout, IM_MAXDIM)

	# Shift the images.
	do i = 1, nlines {

	    # Get the input image data buffer.
	    buf1 = imggsr (im1, v1, v2, IM_NDIM(im1))
	    if (buf1 == EOF)
		call error (0, "SHIFTLINES: Error reading input image\n")

	    # Get the output image data buffer.
	    junk = impnlr (im2, buf2, vout)
	    if (junk == EOF)
		call error (0, "SHIFTLINES: Error writing output image\n")

	    # Evaluate the interpolation at the shifted points.
	    call asifit (asi, Memr[buf1], nimcols)
	    call asivector (asi, Memr[x], Memr[buf2], ncols)

	    # Increment the v vectors.
	    call sh_loop (v1, v2, IM_LEN(im1,1), IM_NDIM(im1))
	}

	call asifree (asi)
	call sfree (sp)
end


# SH_LINESI -- Integer pixel shift.
#
# Shift the pixels in an image by an integer amount.  Perform I/O without
# out type conversion and use array move operators.

procedure sh_linesi (im1, im2, shift, boundary, constant)

pointer	im1				# Input image descriptor
pointer	im2				# Output image descriptor
int	shift				# Integer shift
int	boundary			# Boundary extension type
real	constant			# Constant for boundary extension

int	i, ncols, nlines, junk
long	v1[IM_MAXDIM], v2[IM_MAXDIM], vout[IM_MAXDIM]
pointer	buf1, buf2

int	imggss(), imggsi(), imggsl(), imggsr(), imggsd(), imggsx()
int	impnls(), impnli(), impnll(), impnlr(), impnld(), impnlx()

begin
	# Set the boundary extension parameters.
	call imseti (im1, IM_NBNDRYPIX, abs (shift))
	call imseti (im1, IM_TYBNDRY, boundary)
	call imsetr (im1, IM_BNDRYPIXVAL, constant)

	# Return if shift off image.
	ncols = IM_LEN(im1, 1)
	if (shift < -ncols || shift > ncols)
	    call error (0, "Shiftlinesi: shift out of bounds")

	# Setup start vector for sequential reads and writes.
	v1[1] = max (-ncols + 1, -shift + 1)
	v2[1] = min (2 * ncols, ncols - shift)
	do i = 2, IM_NDIM(im1) {
	    v1[i] = long (1)
	    v2[i] = long (1)
	}
	call amovkl (long(1), vout, IM_MAXDIM)

	# Setup line counter.
	nlines = 1
	do i = 2, IM_NDIM(im1)
	    nlines = nlines * IM_LEN(im1, i)


	# Shift the image using appropriate datatype operators.
	switch (IM_PIXTYPE(im1)) {

	case TY_SHORT:
	    do i = 1, nlines {
	        buf1 = imggss (im1, v1, v2, IM_NDIM(im1))
		if (buf1 == EOF)
		    call error (0, "Shiftlines: error reading input image")
		junk = impnls (im2, buf2, vout)
		if (junk == EOF)
		    call error (0, "Shiftlinesi: error writing out image")
		call amovs (Mems[buf1], Mems[buf2], ncols)
		call sh_loop (v1, v2, IM_LEN(im1,1), IM_NDIM(im1))
	    }

	case TY_INT:
	    do i = 1, nlines {
	        buf1 = imggsi (im1, v1, v2, IM_NDIM(im1))
		if (buf1 == EOF)
		    call error (0, "Shiftlines: error reading input image")
		junk = impnli (im2, buf2, vout)
		if (junk == EOF)
		    call error (0, "Shiftlinesi: error writing out image")
		call amovi (Memi[buf1], Memi[buf2], ncols)
		call sh_loop (v1, v2, IM_LEN(im1,1), IM_NDIM(im1))
	    }

	case TY_LONG:
	    do i = 1, nlines {
	        buf1 = imggsl (im1, v1, v2, IM_NDIM(im1))
		if (buf1 == EOF)
		    call error (0, "Shiftlines: error reading input image")
		junk = impnll (im2, buf2, vout)
		if (junk == EOF)
		    call error (0, "Shiftlinesi: error writing out image")
		call amovl (Meml[buf1], Meml[buf2], ncols)
		call sh_loop (v1, v2, IM_LEN(im1,1), IM_NDIM(im1))
	    }

	case TY_REAL:
	    do i = 1, nlines {
	        buf1 = imggsr (im1, v1, v2, IM_NDIM(im1))
		if (buf1 == EOF)
		    call error (0, "Shiftlines: error reading input image")
		junk = impnlr (im2, buf2, vout)
		if (junk == EOF)
		    call error (0, "Shiftlinesi: error writing out image")
		call amovr (Memr[buf1], Memr[buf2], ncols)
		call sh_loop (v1, v2, IM_LEN(im1,1), IM_NDIM(im1))
	    }

	case TY_DOUBLE:
	    do i = 1, nlines {
	        buf1 = imggsd (im1, v1, v2, IM_NDIM(im1))
		if (buf1 == EOF)
		    call error (0, "Shiftlines: error reading input image")
		junk = impnld (im2, buf2, vout)
		if (junk == EOF)
		    call error (0, "Shiftlinesi: error writing out image")
		call amovd (Memd[buf1], Memd[buf2], ncols)
		call sh_loop (v1, v2, IM_LEN(im1,1), IM_NDIM(im1))
	    }

	case TY_COMPLEX:
	    do i = 1, nlines {
	        buf1 = imggsx (im1, v1, v2, IM_NDIM(im1))
		if (buf1 == EOF)
		    call error (0, "Shiftlines: error reading input image")
		junk = impnlx (im2, buf2, vout)
		if (junk == EOF)
		    call error (0, "Shiftlinesi: error writing out image")
		call amovx (Memx[buf1], Memx[buf2], ncols)
		call sh_loop (v1, v2, IM_LEN(im1,1), IM_NDIM(im1))
	    }

	default:
	    call error (0, "Unknown pixel datatype")
	}
end


# SH_LOOP -- Increment the vector V from VS to VE (nested do loops cannot
# be used because of the variable number of dimensions).  Return LOOP_DONE
# when V exceeds VE.

procedure sh_loop (vs, ve, szdims, ndim)

long	vs[ndim]			# the start vector
long	ve[ndim]			# the end vector
long	szdims[ndim]			# the dimensions vector
int	ndim				# the number of dimensions

int	dim

begin
	for (dim=2;  dim <= ndim;  dim=dim+1) {
	    vs[dim] = vs[dim] + 1
	    ve[dim] = vs[dim]
	    if (vs[dim] - szdims[dim] == 1) {
		if (dim < ndim) {
		    vs[dim] = 1
		    ve[dim] = 1
		} else
		    break
	    } else
		break
	}
end