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