1 |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* sm_samp.c |
9 |
*/ |
10 |
#include "standard.h" |
11 |
#include "sm_flag.h" |
12 |
#include "rhd_sample.h" |
13 |
|
14 |
SAMP rsL; |
15 |
int4 *samp_flag=NULL; |
16 |
|
17 |
|
18 |
/* Each sample has a world coord point, and direction, brightness,chrominance, |
19 |
and RGB triples |
20 |
*/ |
21 |
#ifndef HP_VERSION |
22 |
#define TMSIZE sizeof(TMbright) |
23 |
#else |
24 |
#define TMSIZE 0 |
25 |
#endif |
26 |
#define SAMPSIZ (3*sizeof(float)+sizeof(int4)+ 6*sizeof(BYTE) + TMSIZE) |
27 |
|
28 |
/* Extra points just need direction and world space point */ |
29 |
#define POINTSIZ (3*sizeof(float)) |
30 |
|
31 |
/* Clear the flags for all samples */ |
32 |
sClear_all_flags(s) |
33 |
SAMP *s; |
34 |
{ |
35 |
if(samp_flag) |
36 |
bzero((char *)samp_flag,FLAG_BYTES(S_MAX_BASE_PT(s))); |
37 |
} |
38 |
|
39 |
sInit(s) |
40 |
SAMP *s; |
41 |
{ |
42 |
S_REPLACE_SAMP(s) = 0; |
43 |
S_NUM_SAMP(s) = 0; |
44 |
S_TONE_MAP(s) = 0; |
45 |
S_FREE_SAMP(s) = -1; |
46 |
sClear_base_points(s); |
47 |
|
48 |
} |
49 |
|
50 |
sFree(s) |
51 |
SAMP *s; |
52 |
{ |
53 |
if(S_BASE(s)) |
54 |
{ |
55 |
free(S_BASE(s)); |
56 |
S_BASE(s)=NULL; |
57 |
} |
58 |
if(samp_flag) |
59 |
{ |
60 |
free(samp_flag); |
61 |
samp_flag = NULL; |
62 |
} |
63 |
} |
64 |
/* Initialize/clear global smL sample list for at least n samples */ |
65 |
SAMP |
66 |
*sAlloc(nptr,extra_points) |
67 |
int *nptr,extra_points; |
68 |
{ |
69 |
unsigned nbytes; |
70 |
register unsigned i; |
71 |
SAMP *s; |
72 |
int n; |
73 |
|
74 |
s = &rsL; |
75 |
/* round space up to nearest power of 2 */ |
76 |
nbytes = (*nptr)*SAMPSIZ + extra_points*POINTSIZ + 8; |
77 |
for (i = 1024; nbytes > i; i <<= 1) |
78 |
; |
79 |
/* get number of samples that fit in block */ |
80 |
n = (i - 8 - extra_points*POINTSIZ) / SAMPSIZ; |
81 |
|
82 |
S_BASE(s) = (char *)malloc(n*SAMPSIZ + extra_points*POINTSIZ); |
83 |
if (!S_BASE(s)) |
84 |
error(SYSTEM,"sAlloc(): Unable to allocate memory"); |
85 |
|
86 |
/* assign larger alignment types earlier */ |
87 |
S_W_PT(s) = (float (*)[3])S_BASE(s); |
88 |
S_W_DIR(s) = (int4 *)(S_W_PT(s) + n + extra_points); |
89 |
S_BRT(s) = (TMbright *)(S_W_DIR(s) + n); |
90 |
S_CHR(s) = (BYTE (*)[3])(S_BRT(s) + n); |
91 |
S_RGB(s) = (BYTE (*)[3])(S_CHR(s) + n); |
92 |
S_MAX_SAMP(s) = n; |
93 |
S_MAX_BASE_PT(s) = n + extra_points; |
94 |
|
95 |
/* Allocate memory for a per/sample bit flag */ |
96 |
if(!(samp_flag = (int4 *)malloc(FLAG_BYTES(n+extra_points)))) |
97 |
error(SYSTEM,"sAlloc(): Unable to allocate flag memory"); |
98 |
|
99 |
sInit(s); |
100 |
sClear_all_flags(s); |
101 |
|
102 |
*nptr = n; |
103 |
|
104 |
return(s); |
105 |
} |
106 |
|
107 |
|
108 |
/* Add a base point to the sample structure: only the world point |
109 |
is added: These points are not displayed-they are used to form the |
110 |
initial mesh |
111 |
*/ |
112 |
int |
113 |
sAdd_base_point(s,v) |
114 |
SAMP *s; |
115 |
FVECT v; |
116 |
{ |
117 |
int id; |
118 |
|
119 |
/* Get pointer to next available point */ |
120 |
id = S_NEXT_BASE_PT(s); |
121 |
if(id >= S_MAX_BASE_PT(s)) |
122 |
return(-1); |
123 |
|
124 |
/* Update free aux pointer */ |
125 |
S_NEXT_BASE_PT(s)++; |
126 |
|
127 |
/* Copy vector into point */ |
128 |
VCOPY(S_NTH_W_PT(s,id),v); |
129 |
return(id); |
130 |
} |
131 |
|
132 |
/* Copy the values of sample n_id into sample id: Note must not call with |
133 |
Base point n_id |
134 |
*/ |
135 |
int |
136 |
sCopy_samp(s,n_id,id) |
137 |
SAMP *s; |
138 |
int n_id,id; |
139 |
{ |
140 |
|
141 |
#ifdef DEBUG |
142 |
if(id <0||id >= S_MAX_SAMP(s)||n_id <0||n_id >= S_MAX_SAMP(s)) |
143 |
{ |
144 |
eputs("smReset_sample():invalid sample id\n"); |
145 |
return(0); |
146 |
} |
147 |
#endif |
148 |
if(n_id == id) |
149 |
return(1); |
150 |
/* Copy vector into point */ |
151 |
VCOPY(S_NTH_W_PT(s,n_id),S_NTH_W_PT(s,id)); |
152 |
S_NTH_W_DIR(s,n_id) = S_NTH_W_DIR(s,id); |
153 |
|
154 |
S_NTH_BRT(s,n_id) = S_NTH_BRT(s,id); |
155 |
S_NTH_CHR(s,n_id)[0] = S_NTH_CHR(s,id)[0]; |
156 |
S_NTH_CHR(s,n_id)[1] = S_NTH_CHR(s,id)[1]; |
157 |
S_NTH_CHR(s,n_id)[2] = S_NTH_CHR(s,id)[2]; |
158 |
|
159 |
return(1); |
160 |
} |
161 |
|
162 |
|
163 |
/* Initialize the sample 'id' to contain world point 'p', direction 'd' and |
164 |
color 'c'. If 'p' is NULL, a direction only is represented in 'd'. In |
165 |
this case, the world point is set to the projection of the direction on |
166 |
the view sphere, and the direction value is set to -1 |
167 |
*/ |
168 |
void |
169 |
sInit_samp(s,id,c,d,p,o_id) |
170 |
SAMP *s; |
171 |
int id; |
172 |
COLR c; |
173 |
FVECT d,p; |
174 |
int o_id; |
175 |
{ |
176 |
|
177 |
if(o_id != INVALID) |
178 |
sCopy_samp(s,id,o_id); |
179 |
else |
180 |
{ |
181 |
/* Copy vector into point */ |
182 |
VCOPY(S_NTH_W_PT(s,id),p); |
183 |
if(d) |
184 |
S_NTH_W_DIR(s,id) = encodedir(d); |
185 |
else |
186 |
S_NTH_W_DIR(s,id) = -1; |
187 |
|
188 |
/* calculate the brightness and chrominance,RGB will be set by |
189 |
tonemapping |
190 |
*/ |
191 |
#ifndef TEST_DRIVER |
192 |
tmCvColrs(&S_NTH_BRT(s,id),S_NTH_CHR(s,id),c,1); |
193 |
#else |
194 |
if(c) |
195 |
{ |
196 |
S_NTH_RGB(s,id)[0] = c[0]; |
197 |
S_NTH_RGB(s,id)[1] = c[1]; |
198 |
S_NTH_RGB(s,id)[2] = c[2]; |
199 |
} |
200 |
else |
201 |
{ |
202 |
S_NTH_RGB(s,id)[0] = 100; |
203 |
S_NTH_RGB(s,id)[1] = 0; |
204 |
S_NTH_RGB(s,id)[2] = 0; |
205 |
} |
206 |
#endif |
207 |
} |
208 |
/* Set ACTIVE bit upon creation */ |
209 |
S_SET_FLAG(id); |
210 |
#ifndef TEST_DRIVER |
211 |
if(id < S_TONE_MAP(s)) |
212 |
tmMapPixels(S_NTH_RGB(s,id),&S_NTH_BRT(s,id),S_NTH_CHR(s,id),1); |
213 |
#endif |
214 |
} |
215 |
|
216 |
|
217 |
/* Allocate a new sample. If an existing sample was replaced: set flag */ |
218 |
int |
219 |
sAlloc_samp(s,replaced) |
220 |
SAMP *s; |
221 |
int *replaced; |
222 |
|
223 |
{ |
224 |
int id; |
225 |
|
226 |
if(replaced) |
227 |
*replaced = 0; |
228 |
|
229 |
/* First check if there are any freed sample available */ |
230 |
if((id = S_FREE_SAMP(s)) != INVALID) |
231 |
{ |
232 |
S_FREE_SAMP(s) = S_NTH_NEXT(s,id); |
233 |
return(id); |
234 |
} |
235 |
/* If havn't reached end of sample array:return next sample */ |
236 |
if(S_NUM_SAMP(s) < S_MAX_SAMP(s)) |
237 |
id = S_NUM_SAMP(s)++; |
238 |
else |
239 |
{ |
240 |
/* CLOCKED LRU replacement policy: Search through samples starting |
241 |
with S_REPLACE_SAMP for a sample that does not have its active |
242 |
bit set. Clear bit along the way |
243 |
*/ |
244 |
id = S_REPLACE_SAMP(s); |
245 |
while(S_IS_FLAG(id)) |
246 |
{ |
247 |
S_CLR_FLAG(id); |
248 |
id = (id +1)% S_MAX_SAMP(s); |
249 |
} |
250 |
S_REPLACE_SAMP(s) = (id +1)% S_MAX_SAMP(s); |
251 |
if(replaced) |
252 |
*replaced = 1; |
253 |
} |
254 |
return(id); |
255 |
} |
256 |
|
257 |
/* Set the sample flag to be unused- so will get replaced on next LRU |
258 |
iteration |
259 |
*/ |
260 |
int |
261 |
sDelete_samp(s,id) |
262 |
SAMP *s; |
263 |
int id; |
264 |
{ |
265 |
#ifdef DEBUG |
266 |
/* Fist check for a valid id */ |
267 |
if(id >= S_MAX_SAMP(s) || id < 0) |
268 |
return(0); |
269 |
#endif |
270 |
/* Add sample to the free list */ |
271 |
sUnalloc_samp(s,id); |
272 |
|
273 |
S_CLR_FLAG(id); |
274 |
return(1); |
275 |
} |
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|