aboutsummaryrefslogtreecommitdiff
path: root/pkg/xtools/xtsums.x
blob: 8d6172f8365bf466177e75f63d6e8aa1099dd788 (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
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
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

# XT_LSUM -- Sum lines
#
# A new sum vector is created when the data pointer is null or if the number
# of columns is changed.  If the previous sum overlaps the requested sum then
# additions and subtractions are performed on the previous sum to minimize
# the number of arithmetic operations.

procedure xt_lsum (im, col1, col2, line1, line2, data)

pointer	im		# IMIO pointer
int	col1, col2	# Column limits of the sum
int	line1, line2	# Line limits
pointer	data		# Data pointer

int	i
int	ncols, nlines, nc, nl, c1, c2, l1, l2
pointer	j

pointer	imgs2r()

begin
	ncols = col2 - col1 + 1
	nlines = line2 - line1 + 1

	if ((data == NULL) || (ncols != nc)) {
	    call mfree (data, TY_REAL)
	    call malloc (data, ncols, TY_REAL)
	    nc = ncols
	    l1 = 0
	    l2 = 0
	}

	if (nlines != nl) {
	    nl = nlines
	    l1 = 0
	    l2 = 0
	}

	# If only one line then don't bother with summing.

	if (nlines == 1) {
	    if ((line1 != l1) || (col1 != c1) || (col2 != c2)) {
	        c1 = col1
	        c2 = col2
	        l1 = line1
	        l2 = line2
		j = imgs2r (im, c1, c2, l1, l2)
	        call amovr (Memr[j], Memr[data], nc)
	    }
	    return
	}

	# If the sum limits are outside the last sum limits then form
	# the sums from scratch.

	if ((line1 > l2) || (line2 < l1) || (col1 != c1) || (col2 != c2)) {
	    c1 = col1
	    c2 = col2
	    l1 = line1
	    l2 = line2
	    call aclrr (Memr[data], nc)
	    do i = l1, l2 {
		j = imgs2r (im, c1, c2, i, i)
		call aaddr (Memr[data], Memr[j], Memr[data], nc)
	    }

	# If the sum limits overlap then add and subtract to compute the
	# new sums from the previous sums.  This minimizes the number of
	# arithmetic operations in common applications.

	} else if (line1 > l1) {
	    do i = l1, line1 - 1 {
		j = imgs2r (im, c1, c2, i, i)
		call asubr (Memr[data], Memr[j], Memr[data], nc)
	    }
	    do i = l2 + 1, line2 {
		j = imgs2r (im, c1, c2, i, i)
		call aaddr (Memr[data], Memr[j], Memr[data], nc)
	    }
	    l1 = line1
	    l2 = line2

	} else {
	    do i = line2 + 1, l2 {
		j = imgs2r (im, c1, c2, i, i)
		call asubr (Memr[data], Memr[j], Memr[data], nc)
	    }
	    do i = line1, l1 - 1 {
		j = imgs2r (im, c1, c2, i, i)
		call aaddr (Memr[data], Memr[j], Memr[data], nc)
	    }
	    l1 = line1
	    l2 = line2
	}
end


# XT_CSUM -- Sum columns
#
# A new sum vector is created when the data pointer is null or if the number
# of lines is changed.  If the previous sum overlaps the requested sum then
# additions and subtractions are performed on the previous sum to minimize
# the number of arithmetic operations.

procedure xt_csum (co, col1, col2, line1, line2, data)

pointer	co		# COIO pointer
int	col1, col2	# Column limits of the sum
int	line1, line2	# Line limits
pointer	data		# Data pointer

int	i
int	ncols, nlines, nc, nl, c1, c2, l1, l2
pointer	j

pointer	cogetr()

begin
	ncols = col2 - col1 + 1
	nlines = line2 - line1 + 1

	if ((data == NULL) || (nlines != nl)) {
	    call mfree (data, TY_REAL)
	    call malloc (data, nlines, TY_REAL)
	    nl = nlines
	    c1 = 0
	    c2 = 0
	}

	if (ncols != nc) {
	    nc = ncols
	    c1 = 0
	    c2 = 0
	}

	# If only one column then don't bother with summing.

	if (ncols == 1) {
	    if ((col1 != c1) || (line1 != l1) || (line2 != l2)) {
	        c1 = col1
	        c2 = col2
	        l1 = line1
	        l2 = line2
		j = cogetr (co, c1, l1, l2)
	        call amovr (Memr[j], Memr[data], nl)
	    }
	    return
	}

	# If the sum limits are outside the last sum limits then form
	# the sums from scratch.

	if ((col1 > c2) || (col2 < c1) || (line1 != l1) || (line2 != l2)) {
	    c1 = col1
	    c2 = col2
	    l1 = line1
	    l2 = line2
	    call aclrr (Memr[data], nlines)
	    do i = c1, c2 {
		j = cogetr (co, i, l1, l2)
		call aaddr (Memr[data], Memr[j], Memr[data], nl)
	    }

	# If the sum limits overlap then add and subtract to compute the
	# new sums from the previous sums.  This minimizes the number of
	# arithmetic operations in common applications.

	} else if (col1 > c1) {
	    do i = c1, col1 - 1 {
		j = cogetr (co, i, l1, l2)
		call asubr (Memr[data], Memr[j], Memr[data], nl)
	    }
	    do i = c2 + 1, col2 {
		j = cogetr (co, i, l1, l2)
		call aaddr (Memr[data], Memr[j], Memr[data], nl)
	    }
	    c1 = col1
	    c2 = col2

	} else {
	    do i = col2 + 1, c2 {
		j = cogetr (co, i, l1, l2)
		call asubr (Memr[data], Memr[j], Memr[data], nl)
	    }
	    do i = col1, c1 - 1 {
		j = cogetr (co, i, l1, l2)
		call aaddr (Memr[data], Memr[j], Memr[data], nl)
	    }
	    c1 = col1
	    c2 = col2
	}
end


# XT_LSUMB -- Sum lines with buffering
#
# A new sum vector is created when the data pointer is null or if the number
# of columns is changed.  If the previous sum overlaps the requested sum then
# additions and subtractions are performed on the previous sum to minimize
# the number of arithmetic operations.  Buffering of previous lines is done.

procedure xt_lsumb (im, col1, col2, line1, line2, data)

pointer	im		# IMIO pointer
int	col1, col2	# Column limits of the sum
int	line1, line2	# Line limits
pointer	data		# Data pointer

int	i
int	ncols, nlines, nc, nl, c1, c2, l1, l2
pointer	j

pointer	imgs2r()

begin
	ncols = col2 - col1 + 1
	nlines = line2 - line1 + 1

	if ((data == NULL) || (ncols != nc)) {
	    call mfree (data, TY_REAL)
	    call malloc (data, (nlines + 1) * ncols, TY_REAL)
	    nc = ncols
	    l1 = 0
	    l2 = 0
	}

	if (nlines != nl) {
	    nl = nlines
	    l1 = 0
	    l2 = 0
	}

	# If only one line then don't bother with summing.

	if (nlines == 1) {
	    if ((line1 != l1) || (col1 != c1) || (col2 != c2)) {
	        c1 = col1
	        c2 = col2
	        l1 = line1
	        l2 = line2
		j = imgs2r (im, c1, c2, l1, l2)
	        call amovr (Memr[j], Memr[data], nc)
	    }
	    return
	}

	# If the sum limits are outside the last sum limits then form
	# the sums from scratch.

	if ((line1 > l2) || (line2 < l1) || (col1 != c1) || (col2 != c2)) {
	    c1 = col1
	    c2 = col2
	    l1 = line1
	    l2 = line2
	    call aclrr (Memr[data], nc)
	    do i = l1, l2 {
		j = data + (mod (i, nl) + 1) * nc
		call amovr (Memr[imgs2r (im, c1, c2, i, i)], Memr[j], nc)
		call aaddr (Memr[data], Memr[j], Memr[data], nc)
	    }

	# If the sum limits overlap then add and subtract to compute the
	# new sums from the previous sums.  This minimizes the number of
	# arithmetic operations in common applications.

	} else if (line1 > l1) {
	    do i = l1, line1 - 1 {
		j = data + (mod (i, nl) + 1) * nc
		call asubr (Memr[data], Memr[j], Memr[data], nc)
	    }
	    do i = l2 + 1, line2 {
		j = data + (mod (i, nl) + 1) * nc
		call amovr (Memr[imgs2r (im, c1, c2, i, i)], Memr[j], nc)
		call aaddr (Memr[data], Memr[j], Memr[data], nc)
	    }
	    l1 = line1
	    l2 = line2

	} else {
	    do i = line2 + 1, l2 {
		j = data + (mod (i, nl) + 1) * nc
		call asubr (Memr[data], Memr[j], Memr[data], nc)
	    }
	    do i = line1, l1 - 1 {
		j = data + (mod (i, nl) + 1) * nc
		call amovr (Memr[imgs2r (im, c1, c2, i, i)], Memr[j], nc)
		call aaddr (Memr[data], Memr[j], Memr[data], nc)
	    }
	    l1 = line1
	    l2 = line2
	}
end


# XT_CSUMB -- Sum columns with buffering
#
# A new sum vector is created when the data pointer is null or if the number
# of lines is changed.  If the previous sum overlaps the requested sum then
# additions and subtractions are performed on the previous sum to minimize
# the number of arithmetic operations.  Buffering is done on the previous cols.

procedure xt_csumb (co, col1, col2, line1, line2, data)

pointer	co		# COIO pointer
int	col1, col2	# Column limits of the sum
int	line1, line2	# Line limits
pointer	data		# Data pointer

int	i
int	ncols, nlines, nc, nl, c1, c2, l1, l2
pointer	j

pointer	cogetr()

begin
	ncols = col2 - col1 + 1
	nlines = line2 - line1 + 1

	if ((data == NULL) || (nlines != nl)) {
	    call mfree (data, TY_REAL)
	    call malloc (data, (ncols + 1) * nlines, TY_REAL)
	    nl = nlines
	    c1 = 0
	    c2 = 0
	}

	if (ncols != nc) {
	    nc = ncols
	    c1 = 0
	    c2 = 0
	}

	# If only one column then don't bother with summing.

	if (ncols == 1) {
	    if ((col1 != c1) || (line1 != l1) || (line2 != l2)) {
	        c1 = col1
	        c2 = col2
	        l1 = line1
	        l2 = line2
		j = cogetr (co, c1, l1, l2)
	        call amovr (Memr[j], Memr[data], nl)
	    }
	    return
	}

	# If the sum limits are outside the last sum limits then form
	# the sums from scratch.

	if ((col1 > c2) || (col2 < c1) || (line1 != l1) || (line2 != l2)) {
	    c1 = col1
	    c2 = col2
	    l1 = line1
	    l2 = line2
	    call aclrr (Memr[data], nlines)
	    do i = c1, c2 {
		j = data + (mod (i, nc) + 1) * nl
		call amovr (Memr[cogetr (co, i, l1, l2)], Memr[j], nl)
		call aaddr (Memr[data], Memr[j], Memr[data], nl)
	    }

	# If the sum limits overlap then add and subtract to compute the
	# new sums from the previous sums.  This minimizes the number of
	# arithmetic operations in common applications.

	} else if (col1 > c1) {
	    do i = c1, col1 - 1 {
		j = data + (mod (i, nc) + 1) * nl
		call asubr (Memr[data], Memr[j], Memr[data], nl)
	    }
	    do i = c2 + 1, col2 {
		j = data + (mod (i, nc) + 1) * nl
		call amovr (Memr[cogetr (co, i, l1, l2)], Memr[j], nl)
		call aaddr (Memr[data], Memr[j], Memr[data], nl)
	    }
	    c1 = col1
	    c2 = col2

	} else {
	    do i = col2 + 1, c2 {
		j = data + (mod (i, nc) + 1) * nl
		call asubr (Memr[data], Memr[j], Memr[data], nl)
	    }
	    do i = col1, c1 - 1 {
		j = data + (mod (i, nc) + 1) * nl
		call amovr (Memr[cogetr (co, i, l1, l2)], Memr[j], nl)
		call aaddr (Memr[data], Memr[j], Memr[data], nl)
	    }
	    c1 = col1
	    c2 = col2
	}
end