aboutsummaryrefslogtreecommitdiff
path: root/pkg/xtools/xtsample.x
blob: e8184e1d39112a0a4fce859b317ca94da92306f5 (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
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
include	<imhdr.h>


# XT_SAMPLE -- Get sample of pixels.
#
# This routine returns a sample of unmasked pixels from an N-dim image.
# The input is the image pointer, the mask pointer (which may be NULL),
# the array to be filled, the maximum number of sample plixels, and the
# minimum number of lines to sample.  The return value is the actual number
# of pixels which will be less than or equal to the specified maximum number.
# 
# The intent of this routine is to sample fairly uniformly but efficiently.
# If nlines is zero the total number of pixels, in raster order, is divided
# into uniform steps.  But this may end up reading many lines each for a
# few pixels.  To be more efficient if nlines is greater than zero then as
# many pixels per line as possible are read to sample at least the requested
# number of lines.


int procedure xt_samples (im, bpm, sample, nsample, nlines)

pointer	im			#I Image pointer
pointer	bpm			#I Bad pixel pointer
short	sample[nsample]		#I Work array
int	nsample			#I Maximum number of sample pixels
int	nlines			#I Minimum number of lines to sample
int	nreturn			#I Number of pixels returned

long	v[IM_MAXDIM], vbuf[IM_MAXDIM]
int	i, ip, n, ndim, npix, nc
real	p, c, pstep, cstep
pointer	buf, bpmbuf

int	imgnls()

begin
	# Determine the number of pixels in the data, the number
	# to make up nsample pixels, and the pixel step.

	ndim = IM_NDIM(im)
	nc = IM_LEN(im,1)
	npix = 1
	do i = 1, ndim
	    npix = npix * IM_LEN(im,i)
	pstep = real(npix) / min (npix, nsample)

	# To insure a minimum number of lines and efficient use of
	# pixels in a line, set the column step.

	if (nlines == 0)
	    cstep = pstep
	else
	    cstep = nc / min (min(npix,nsample)/nlines, nc)

	# Step through the pixels.
	call amovkl (long(1), v, IM_MAXDIM)
	nreturn = 0
	for (p=(pstep-0.01)/2; p<npix && nreturn<nsample;) {

	    # Convert pixel number to image vector coordinates.
	    n = npix; ip = nint(p)
	    do i = ndim, 1, -1 {
	        n = n / IM_LEN(im,i)
	        v[i] = 1 + ip / n
		ip = mod (ip, n)
	    }

	    # Sample the pixels in the line.
	    if (nlines == 0)
		c = v[1]
	    else
		c = (cstep - 0.01) / 2

	    if (bpm == NULL) {
		v[1] = 1
		if (imgnls (im, buf, v) == EOF)
		    break
		for (; c<nc && nreturn<nsample; c=c+cstep) {
		    ip = nint (c)
		    nreturn = nreturn + 1
		    sample[nreturn] = Mems[buf+ip]
		    p = p + pstep
		}
	    } else {
		v[1] = 1
	        call amovl (v, vbuf, IM_MAXDIM)
		if (imgnls (bpm, bpmbuf, vbuf) == EOF)
		    break
		if (imgnls (im, buf, v) == EOF)
		    break
		for (; c<nc && nreturn<nsample; c=c+cstep) {
		    ip = nint (c)
		    if (Mems[bpmbuf+ip] == 0) {
			nreturn = nreturn + 1
			sample[nreturn] = Mems[buf+ip]
		    }
		    p = p + pstep
		}
	    }
	}

	return (nreturn)
end

int procedure xt_samplei (im, bpm, sample, nsample, nlines)

pointer	im			#I Image pointer
pointer	bpm			#I Bad pixel pointer
int	sample[nsample]		#I Work array
int	nsample			#I Maximum number of sample pixels
int	nlines			#I Minimum number of lines to sample
int	nreturn			#I Number of pixels returned

long	v[IM_MAXDIM], vbuf[IM_MAXDIM]
int	i, ip, n, ndim, npix, nc
real	p, c, pstep, cstep
pointer	buf, bpmbuf

int	imgnls()
int	imgnli()

begin
	# Determine the number of pixels in the data, the number
	# to make up nsample pixels, and the pixel step.

	ndim = IM_NDIM(im)
	nc = IM_LEN(im,1)
	npix = 1
	do i = 1, ndim
	    npix = npix * IM_LEN(im,i)
	pstep = real(npix) / min (npix, nsample)

	# To insure a minimum number of lines and efficient use of
	# pixels in a line, set the column step.

	if (nlines == 0)
	    cstep = pstep
	else
	    cstep = nc / min (min(npix,nsample)/nlines, nc)

	# Step through the pixels.
	call amovkl (long(1), v, IM_MAXDIM)
	nreturn = 0
	for (p=(pstep-0.01)/2; p<npix && nreturn<nsample;) {

	    # Convert pixel number to image vector coordinates.
	    n = npix; ip = nint(p)
	    do i = ndim, 1, -1 {
	        n = n / IM_LEN(im,i)
	        v[i] = 1 + ip / n
		ip = mod (ip, n)
	    }

	    # Sample the pixels in the line.
	    if (nlines == 0)
		c = v[1]
	    else
		c = (cstep - 0.01) / 2

	    if (bpm == NULL) {
		v[1] = 1
		if (imgnli (im, buf, v) == EOF)
		    break
		for (; c<nc && nreturn<nsample; c=c+cstep) {
		    ip = nint (c)
		    nreturn = nreturn + 1
		    sample[nreturn] = Memi[buf+ip]
		    p = p + pstep
		}
	    } else {
		v[1] = 1
	        call amovl (v, vbuf, IM_MAXDIM)
		if (imgnls (bpm, bpmbuf, vbuf) == EOF)
		    break
		if (imgnli (im, buf, v) == EOF)
		    break
		for (; c<nc && nreturn<nsample; c=c+cstep) {
		    ip = nint (c)
		    if (Mems[bpmbuf+ip] == 0) {
			nreturn = nreturn + 1
			sample[nreturn] = Memi[buf+ip]
		    }
		    p = p + pstep
		}
	    }
	}

	return (nreturn)
end

int procedure xt_sampler (im, bpm, sample, nsample, nlines)

pointer	im			#I Image pointer
pointer	bpm			#I Bad pixel pointer
real	sample[nsample]		#I Work array
int	nsample			#I Maximum number of sample pixels
int	nlines			#I Minimum number of lines to sample
int	nreturn			#I Number of pixels returned

long	v[IM_MAXDIM], vbuf[IM_MAXDIM]
int	i, ip, n, ndim, npix, nc
real	p, c, pstep, cstep
pointer	buf, bpmbuf

int	imgnls()
int	imgnlr()

begin
	# Determine the number of pixels in the data, the number
	# to make up nsample pixels, and the pixel step.

	ndim = IM_NDIM(im)
	nc = IM_LEN(im,1)
	npix = 1
	do i = 1, ndim
	    npix = npix * IM_LEN(im,i)
	pstep = real(npix) / min (npix, nsample)

	# To insure a minimum number of lines and efficient use of
	# pixels in a line, set the column step.

	if (nlines == 0)
	    cstep = pstep
	else
	    cstep = nc / min (min(npix,nsample)/nlines, nc)

	# Step through the pixels.
	call amovkl (long(1), v, IM_MAXDIM)
	nreturn = 0
	for (p=(pstep-0.01)/2; p<npix && nreturn<nsample;) {

	    # Convert pixel number to image vector coordinates.
	    n = npix; ip = nint(p)
	    do i = ndim, 1, -1 {
	        n = n / IM_LEN(im,i)
	        v[i] = 1 + ip / n
		ip = mod (ip, n)
	    }

	    # Sample the pixels in the line.
	    if (nlines == 0)
		c = v[1]
	    else
		c = (cstep - 0.01) / 2

	    if (bpm == NULL) {
		v[1] = 1
		if (imgnlr (im, buf, v) == EOF)
		    break
		for (; c<nc && nreturn<nsample; c=c+cstep) {
		    ip = nint (c)
		    nreturn = nreturn + 1
		    sample[nreturn] = Memr[buf+ip]
		    p = p + pstep
		}
	    } else {
		v[1] = 1
	        call amovl (v, vbuf, IM_MAXDIM)
		if (imgnls (bpm, bpmbuf, vbuf) == EOF)
		    break
		if (imgnlr (im, buf, v) == EOF)
		    break
		for (; c<nc && nreturn<nsample; c=c+cstep) {
		    ip = nint (c)
		    if (Mems[bpmbuf+ip] == 0) {
			nreturn = nreturn + 1
			sample[nreturn] = Memr[buf+ip]
		    }
		    p = p + pstep
		}
	    }
	}

	return (nreturn)
end

int procedure xt_sampled (im, bpm, sample, nsample, nlines)

pointer	im			#I Image pointer
pointer	bpm			#I Bad pixel pointer
double	sample[nsample]		#I Work array
int	nsample			#I Maximum number of sample pixels
int	nlines			#I Minimum number of lines to sample
int	nreturn			#I Number of pixels returned

long	v[IM_MAXDIM], vbuf[IM_MAXDIM]
int	i, ip, n, ndim, npix, nc
real	p, c, pstep, cstep
pointer	buf, bpmbuf

int	imgnls()
int	imgnld()

begin
	# Determine the number of pixels in the data, the number
	# to make up nsample pixels, and the pixel step.

	ndim = IM_NDIM(im)
	nc = IM_LEN(im,1)
	npix = 1
	do i = 1, ndim
	    npix = npix * IM_LEN(im,i)
	pstep = real(npix) / min (npix, nsample)

	# To insure a minimum number of lines and efficient use of
	# pixels in a line, set the column step.

	if (nlines == 0)
	    cstep = pstep
	else
	    cstep = nc / min (min(npix,nsample)/nlines, nc)

	# Step through the pixels.
	call amovkl (long(1), v, IM_MAXDIM)
	nreturn = 0
	for (p=(pstep-0.01)/2; p<npix && nreturn<nsample;) {

	    # Convert pixel number to image vector coordinates.
	    n = npix; ip = nint(p)
	    do i = ndim, 1, -1 {
	        n = n / IM_LEN(im,i)
	        v[i] = 1 + ip / n
		ip = mod (ip, n)
	    }

	    # Sample the pixels in the line.
	    if (nlines == 0)
		c = v[1]
	    else
		c = (cstep - 0.01) / 2

	    if (bpm == NULL) {
		v[1] = 1
		if (imgnld (im, buf, v) == EOF)
		    break
		for (; c<nc && nreturn<nsample; c=c+cstep) {
		    ip = nint (c)
		    nreturn = nreturn + 1
		    sample[nreturn] = Memd[buf+ip]
		    p = p + pstep
		}
	    } else {
		v[1] = 1
	        call amovl (v, vbuf, IM_MAXDIM)
		if (imgnls (bpm, bpmbuf, vbuf) == EOF)
		    break
		if (imgnld (im, buf, v) == EOF)
		    break
		for (; c<nc && nreturn<nsample; c=c+cstep) {
		    ip = nint (c)
		    if (Mems[bpmbuf+ip] == 0) {
			nreturn = nreturn + 1
			sample[nreturn] = Memd[buf+ip]
		    }
		    p = p + pstep
		}
	    }
	}

	return (nreturn)
end