aboutsummaryrefslogtreecommitdiff
path: root/pkg/images/tv/imedit/epmask.x
blob: 12fd8fc9af495445f80e52498d507d52dccd8787 (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
include	<mach.h>
include	"epix.h"
 
# EP_MASK -- Make a mask array with 1=aperture and 2=background annulus.
#
# Exclude values outside a specified range.
 
procedure ep_mask (ep, mask, ap, xa, ya, xb, yb)
 
pointer	ep			# EPIX pointer
pointer	mask			# Mask pointer
int	ap			# Aperture type
int	xa, ya, xb, yb		# Aperture
 
int	xc, yc, i, j
real	rad, r, a, b, c, d, minv, maxv
int	x1a, x1b, x1c, x2a, x2b, x2c, y1a, y1b, y1c, y2a, y2b, y2c
pointer	sp, line, ptr1, ptr2
 
begin
	rad = max (0.5, EP_RADIUS(ep))

	switch (ap) {
	case APCIRCULAR:
	    xc = nint ((xa + xb) / 2.)
	    yc = nint ((ya + yb) / 2.)
 
	    a = rad ** 2
	    b = (rad + EP_BUFFER(ep)) ** 2
	    c = (rad + EP_BUFFER(ep) + EP_WIDTH(ep)) ** 2
 
	    ptr1 = mask
	    do j = EP_Y1(ep), EP_Y2(ep) {
	        d = (j - yc) ** 2
	        do i = EP_X1(ep), EP_X2(ep) {
		    r = d + (i - xc) ** 2
		    if (r <= a)
		        Memi[ptr1] = 1
		    else if (r >= b && r <= c)
		        Memi[ptr1] = 2
		    else
		        Memi[ptr1] = 0
		    ptr1 = ptr1 + 1
	        }
	    }
	case APCDIAG:
	    a = rad
	    b = rad + EP_BUFFER(ep)
	    c = rad + EP_BUFFER(ep) + EP_WIDTH(ep)
 
	    if (yb - ya != 0)
	        d = real (xb - xa) / (yb - ya)
	    else
		d = 1.
 
	    ptr1 = mask
	    do j = EP_Y1(ep), EP_Y2(ep) {
		xc = xa + d * (j - ya)
		do i = EP_X1(ep), EP_X2(ep) {
		    r = abs (i - xc)
		    if (r <= a)
			Memi[ptr1] = 1
		    else if (r >= b && r <= c)
			Memi[ptr1] = 2
		    else
			Memi[ptr1] = 0
		    ptr1 = ptr1 + 1
		}
	    }
	case APLDIAG:
	    a = rad
	    b = rad + EP_BUFFER(ep)
	    c = rad + EP_BUFFER(ep) + EP_WIDTH(ep)
 
	    if (xb - xa != 0)
	        d = real (yb - ya) / (xb - xa)
	    else
		d = 1.
 
	    ptr1 = mask
	    do j = EP_Y1(ep), EP_Y2(ep) {
		do i = EP_X1(ep), EP_X2(ep) {
		    yc = ya + d * (i - xa)
		    r = abs (j - yc)
		    if (r <= a)
			Memi[ptr1] = 1
		    else if (r >= b && r <= c)
			Memi[ptr1] = 2
		    else
			Memi[ptr1] = 0
		    ptr1 = ptr1 + 1
		}
	    }
	default:
	    call smark (sp)
	    call salloc (line, EP_NX(ep), TY_INT)
 
	    x1a = max (EP_X1(ep), min (xa, xb))
	    x1b = max (EP_X1(ep), int (x1a - EP_BUFFER(ep)))
	    x1c = max (EP_X1(ep), int (x1a - EP_BUFFER(ep) - EP_WIDTH(ep)))
	    x2a = min (EP_X2(ep), max (xa, xb))
	    x2b = min (EP_X2(ep), int (x2a + EP_BUFFER(ep)))
	    x2c = min (EP_X2(ep), int (x2a + EP_BUFFER(ep) + EP_WIDTH(ep)))
 
	    y1a = max (EP_Y1(ep), min (ya, yb))
	    y1b = max (EP_Y1(ep), int (y1a - EP_BUFFER(ep)))
	    y1c = max (EP_Y1(ep), int (y1a - EP_BUFFER(ep) - EP_WIDTH(ep)))
	    y2a = min (EP_Y2(ep), max (ya, yb))
	    y2b = min (EP_Y2(ep), int (y2a + EP_BUFFER(ep)))
	    y2c = min (EP_Y2(ep), int (y2a + EP_BUFFER(ep) + EP_WIDTH(ep)))
 
	    ptr1 = line - EP_X1(ep)
	    ptr2 = mask - EP_Y1(ep) * EP_NX(ep)
 
	    for (i=EP_X1(ep); i<x1c; i=i+1)
	        Memi[ptr1+i] = 0
	    for (; i<x1b; i=i+1)
	        Memi[ptr1+i] = 2
	    for (; i<x1a; i=i+1)
	        Memi[ptr1+i] = 0
	    for (; i<=x2a; i=i+1)
	        Memi[ptr1+i] = 1
	    for (; i<=x2b; i=i+1)
	        Memi[ptr1+i] = 0
	    for (; i<=x2c; i=i+1)
	        Memi[ptr1+i] = 2
	    for (; i<=EP_X2(ep); i=i+1)
	        Memi[ptr1+i] = 0
	    do i = y1a, y2a
	        call amovi (Memi[line], Memi[ptr2+i*EP_NX(ep)], EP_NX(ep))
 
	    for (i=x1a; i<=x2a; i=i+1)
	        Memi[ptr1+i] = 0
	    for (i=y1b; i<y1a; i=i+1)
	        call amovi (Memi[line], Memi[ptr2+i*EP_NX(ep)], EP_NX(ep))
	    for (i=y2a+1; i<=y2b; i=i+1)
	        call amovi (Memi[line], Memi[ptr2+i*EP_NX(ep)], EP_NX(ep))
 
	    for (i=x1b; i<=x2b; i=i+1)
	        Memi[ptr1+i] = 2
	    for (i=y1c; i<y1b; i=i+1)
	        call amovi (Memi[line], Memi[ptr2+i*EP_NX(ep)], EP_NX(ep))
	    for (i=y2b+1; i<=y2c; i=i+1)
	        call amovi (Memi[line], Memi[ptr2+i*EP_NX(ep)], EP_NX(ep))
 
	    for (i=EP_Y1(ep); i<y1c; i=i+1)
	        call aclri (Memi[ptr2+i*EP_NX(ep)], EP_NX(ep))
	    for (i=y2c+1; i<=EP_Y2(ep); i=i+1)
	        call aclri (Memi[ptr2+i*EP_NX(ep)], EP_NX(ep))
 
	    call sfree (sp)
	}

	# Exclude data values.
	ptr2 = EP_OUTDATA(ep)
	if (ptr2 == NULL ||
	    (IS_INDEFR(EP_MINVALUE(ep)) && IS_INDEFR(EP_MAXVALUE(ep))))
	    return

	minv = EP_MINVALUE(ep)
	maxv = EP_MAXVALUE(ep)
	if (IS_INDEFR(minv))
	    minv = -MAX_REAL
	if (IS_INDEFR(maxv))
	    maxv = MAX_REAL
	ptr1 = mask
	do j = EP_Y1(ep), EP_Y2(ep) {
	    do i = EP_X1(ep), EP_X2(ep) {
	        if (Memi[ptr1] != 0) {
		    if (Memr[ptr2] < minv || Memr[ptr2] > maxv)
		        Memi[ptr1] = 0
		}
		ptr1 = ptr1 + 1
		ptr2 = ptr2 + 1
	    }
	}
end