| 1 |
– |
/* Copyright (c) 1999 Silicon Graphics, Inc. */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
|
* Rendering routines for rhpict. |
| 6 |
|
*/ |
| 19 |
|
#define MAXRAD 64 /* maximum kernel radius */ |
| 20 |
|
#endif |
| 21 |
|
#ifndef NNEIGH |
| 22 |
< |
#define NNEIGH 7 /* find this many neighbors */ |
| 22 |
> |
#define NNEIGH 5 /* find this many neighbors */ |
| 23 |
|
#endif |
| 24 |
|
|
| 25 |
|
#define NINF 16382 |
| 35 |
|
#define CLR4(f,i) FL4OP(f,i,&=~) |
| 36 |
|
#define TGL4(f,i) FL4OP(f,i,^=) |
| 37 |
|
#define FL4NELS(n) (((n)+0x1f)>>5) |
| 38 |
< |
#define CLR4ALL(f,n) bzero((char *)(f),FL4NELS(n)*sizeof(int4)) |
| 38 |
> |
#define CLR4ALL(f,n) bzero((char *)(f),FL4NELS(n)*sizeof(int32)) |
| 39 |
|
#endif |
| 40 |
|
|
| 41 |
< |
static int4 *pixFlags; /* pixel occupancy flags */ |
| 41 |
> |
static int32 *pixFlags; /* pixel occupancy flags */ |
| 42 |
|
static float pixWeight[MAXRAD2]; /* pixel weighting function */ |
| 43 |
|
static short isqrttab[MAXRAD2]; /* integer square root table */ |
| 44 |
|
|
| 45 |
< |
#define isqrt(i2) ((int)isqrttab[(int)(i2)]) |
| 45 |
> |
#define isqrt(i2) (isqrttab[i2]) |
| 46 |
|
|
| 47 |
|
extern VIEW myview; /* current output view */ |
| 48 |
|
extern COLOR *mypixel; /* pixels being rendered */ |
| 61 |
|
double d, prox; |
| 62 |
|
COLOR col; |
| 63 |
|
int n; |
| 64 |
< |
register int4 p; |
| 64 |
> |
register int32 p; |
| 65 |
|
|
| 66 |
|
if (!hdbcoord(gc, hb->h, hb->b)) |
| 67 |
|
error(CONSISTENCY, "bad beam in render_beam"); |
| 109 |
|
|
| 110 |
|
|
| 111 |
|
int |
| 112 |
< |
kill_occl(h, v, nl, n) /* check for occlusion errors */ |
| 112 |
> |
kill_occl(h, v, nl, nd, n) /* check for occlusion errors */ |
| 113 |
|
int h, v; |
| 114 |
|
register short nl[NNEIGH][2]; |
| 115 |
+ |
int nd[NNEIGH]; |
| 116 |
|
int n; |
| 117 |
|
{ |
| 118 |
|
short forequad[2][2]; |
| 119 |
|
int d; |
| 120 |
< |
register int4 i, p; |
| 120 |
> |
register int i; |
| 121 |
> |
register int32 p; |
| 122 |
|
|
| 123 |
|
if (n <= 0) { |
| 124 |
|
#ifdef DEBUG |
| 129 |
|
p = v*hres + h; |
| 130 |
|
forequad[0][0] = forequad[0][1] = forequad[1][0] = forequad[1][1] = 0; |
| 131 |
|
for (i = n; i--; ) { |
| 132 |
< |
d = (h-nl[i][0])*(h-nl[i][0]) + (v-nl[i][1])*(v-nl[i][1]); |
| 134 |
< |
d = isqrt(d); |
| 132 |
> |
d = isqrt(nd[i]); |
| 133 |
|
if (mydepth[nl[i][1]*hres+nl[i][0]]*(1.+DEPS*d) < mydepth[p]) |
| 134 |
|
forequad[nl[i][0]<h][nl[i][1]<v] = 1; |
| 135 |
|
} |
| 142 |
|
|
| 143 |
|
|
| 144 |
|
int |
| 145 |
< |
smooth_samp(h, v, nl, n) /* grow sample point smoothly */ |
| 145 |
> |
smooth_samp(h, v, nl, nd, n) /* grow sample point smoothly */ |
| 146 |
|
int h, v; |
| 147 |
|
register short nl[NNEIGH][2]; |
| 148 |
+ |
int nd[NNEIGH]; |
| 149 |
|
int n; |
| 150 |
|
{ |
| 151 |
|
int dis[NNEIGH], ndis; |
| 152 |
|
COLOR mykern[MAXRAD2]; |
| 153 |
< |
int4 maxr2; |
| 153 |
> |
int maxr2; |
| 154 |
|
double d; |
| 155 |
< |
register int4 p, r2; |
| 155 |
> |
register int32 p; |
| 156 |
> |
register int r2; |
| 157 |
|
int i, r, maxr, h2, v2; |
| 158 |
|
|
| 159 |
|
if (n <= 0) |
| 160 |
|
return(1); |
| 161 |
|
p = v*hres + h; /* build kernel values */ |
| 162 |
< |
maxr2 = (h-nl[n-1][0])*(h-nl[n-1][0]) + (v-nl[n-1][1])*(v-nl[n-1][1]); |
| 162 |
> |
maxr2 = nd[n-1]; |
| 163 |
|
DCHECK(maxr2>=MAXRAD2, CONSISTENCY, "out of range neighbor"); |
| 164 |
|
maxr = isqrt(maxr2); |
| 165 |
|
for (v2 = 1; v2 <= maxr; v2++) |
| 171 |
|
} |
| 172 |
|
ndis = 0; /* find discontinuities */ |
| 173 |
|
for (i = n; i--; ) { |
| 174 |
< |
r2 = (h-nl[i][0])*(h-nl[i][0]) + (v-nl[i][1])*(v-nl[i][1]); |
| 175 |
< |
r = isqrt(r2); |
| 174 |
> |
r = isqrt(nd[i]); |
| 175 |
|
d = mydepth[nl[i][1]*hres+nl[i][0]] / mydepth[p]; |
| 176 |
|
d = d>=1. ? d-1. : 1.-d; |
| 177 |
|
if (d > r*DEPS || bigdiff(mypixel[p], |
| 204 |
|
|
| 205 |
|
|
| 206 |
|
int |
| 207 |
< |
random_samp(h, v, nl, n, rf) /* grow sample point noisily */ |
| 207 |
> |
random_samp(h, v, nl, nd, n, rf) /* gather samples randomly */ |
| 208 |
|
int h, v; |
| 209 |
|
register short nl[NNEIGH][2]; |
| 210 |
+ |
int nd[NNEIGH]; |
| 211 |
|
int n; |
| 212 |
|
double *rf; |
| 213 |
|
{ |
| 214 |
< |
float nwt[NNEIGH]; |
| 215 |
< |
int4 maxr2; |
| 216 |
< |
register int4 p, r2; |
| 217 |
< |
register int i; |
| 218 |
< |
int maxr, h2, v2; |
| 219 |
< |
COLOR ctmp; |
| 214 |
> |
float rnt[NNEIGH]; |
| 215 |
> |
double rvar; |
| 216 |
> |
register int32 p, pn; |
| 217 |
> |
register int ni; |
| 218 |
|
|
| 219 |
|
if (n <= 0) |
| 220 |
|
return(1); |
| 221 |
< |
p = v*hres + h; /* compute kernel radius */ |
| 222 |
< |
maxr2 = (h-nl[n-1][0])*(h-nl[n-1][0]) + (v-nl[n-1][1])*(v-nl[n-1][1]); |
| 223 |
< |
DCHECK(maxr2>=MAXRAD2, CONSISTENCY, "out of range neighbor"); |
| 224 |
< |
maxr = isqrt(maxr2); |
| 225 |
< |
/* compute neighbor weights */ |
| 226 |
< |
for (i = n; i--; ) { |
| 227 |
< |
r2 = (nl[i][0]-h)*(nl[i][0]-h) + (nl[i][1]-v)*(nl[i][1]-v); |
| 228 |
< |
nwt[i] = pixWeight[r2]; |
| 221 |
> |
p = v*hres + h; |
| 222 |
> |
if (*rf <= FTINY) /* straight Voronoi regions */ |
| 223 |
> |
ni = 0; |
| 224 |
> |
else { /* weighted choice */ |
| 225 |
> |
DCHECK(nd[n-1]>=MAXRAD2, CONSISTENCY, "out of range neighbor"); |
| 226 |
> |
rnt[0] = pixWeight[nd[0]]; |
| 227 |
> |
for (ni = 1; ni < n; ni++) |
| 228 |
> |
rnt[ni] = rnt[ni-1] + pixWeight[nd[ni]]; |
| 229 |
> |
rvar = rnt[n-1]*pow(frandom(), 1. / *rf); |
| 230 |
> |
for (ni = 0; rvar > rnt[ni]+FTINY; ni++) |
| 231 |
> |
; |
| 232 |
|
} |
| 233 |
< |
/* sample kernel */ |
| 234 |
< |
for (v2 = v-maxr; v2 <= v+maxr; v2++) { |
| 235 |
< |
if (v2 < 0) v2 = 0; |
| 235 |
< |
else if (v2 >= vres) break; |
| 236 |
< |
for (h2 = h-maxr; h2 <= h+maxr; h2++) { |
| 237 |
< |
if (h2 < 0) h2 = 0; |
| 238 |
< |
else if (h2 >= hres) break; |
| 239 |
< |
r2 = (h2-h)*(h2-h) + (v2-v)*(v2-v); |
| 240 |
< |
if (r2 > maxr2) continue; |
| 241 |
< |
if (CHK4(pixFlags, v2*hres+h2)) |
| 242 |
< |
continue; /* occupied */ |
| 243 |
< |
if (frandom() < *rf) { /* pick neighbor instead */ |
| 244 |
< |
i = random() % n; |
| 245 |
< |
r2 = nl[i][1]*hres + nl[i][0]; |
| 246 |
< |
copycolor(ctmp, mypixel[r2]); |
| 247 |
< |
scalecolor(ctmp, nwt[i]); |
| 248 |
< |
addcolor(mypixel[v2*hres+h2], ctmp); |
| 249 |
< |
myweight[v2*hres+h2] += nwt[i] * myweight[r2]; |
| 250 |
< |
continue; |
| 251 |
< |
} |
| 252 |
< |
copycolor(ctmp, mypixel[p]); |
| 253 |
< |
scalecolor(ctmp, pixWeight[r2]); |
| 254 |
< |
addcolor(mypixel[v2*hres+h2], ctmp); |
| 255 |
< |
myweight[v2*hres+h2] += pixWeight[r2] * myweight[p]; |
| 256 |
< |
} |
| 257 |
< |
} |
| 233 |
> |
pn = nl[ni][1]*hres + nl[ni][0]; |
| 234 |
> |
addcolor(mypixel[p], mypixel[pn]); |
| 235 |
> |
myweight[p] += myweight[pn]; |
| 236 |
|
return(1); |
| 237 |
|
} |
| 238 |
|
|
| 243 |
|
if (pixWeight[0] <= FTINY) |
| 244 |
|
init_wfunc(); /* initialize weighting function */ |
| 245 |
|
reset_flags(); /* set occupancy flags */ |
| 246 |
< |
meet_neighbors(kill_occl,NULL); /* eliminate occlusion errors */ |
| 246 |
> |
meet_neighbors(1,kill_occl,NULL); /* identify occlusion errors */ |
| 247 |
|
reset_flags(); /* reset occupancy flags */ |
| 248 |
|
if (ransamp >= 0.) /* spread samples over image */ |
| 249 |
< |
meet_neighbors(random_samp,&ransamp); |
| 249 |
> |
meet_neighbors(0,random_samp,&ransamp); |
| 250 |
|
else |
| 251 |
< |
meet_neighbors(smooth_samp,NULL); |
| 252 |
< |
free((char *)pixFlags); /* free pixel flags */ |
| 251 |
> |
meet_neighbors(1,smooth_samp,NULL); |
| 252 |
> |
free((void *)pixFlags); /* free pixel flags */ |
| 253 |
|
pixFlags = NULL; |
| 254 |
|
} |
| 255 |
|
|
| 256 |
|
|
| 257 |
|
reset_flags() /* allocate/set/reset occupancy flags */ |
| 258 |
|
{ |
| 259 |
< |
register int4 p; |
| 259 |
> |
register int32 p; |
| 260 |
|
|
| 261 |
|
if (pixFlags == NULL) { |
| 262 |
< |
pixFlags = (int4 *)calloc(FL4NELS(hres*vres), sizeof(int4)); |
| 262 |
> |
pixFlags = (int32 *)calloc(FL4NELS(hres*vres), sizeof(int32)); |
| 263 |
|
CHECK(pixFlags==NULL, SYSTEM, "out of memory in reset_flags"); |
| 264 |
|
} else |
| 265 |
|
CLR4ALL(pixFlags, hres*vres); |
| 271 |
|
|
| 272 |
|
init_wfunc() /* initialize weighting function */ |
| 273 |
|
{ |
| 274 |
< |
register int4 r2; |
| 274 |
> |
register int r2; |
| 275 |
|
register double d; |
| 276 |
|
|
| 277 |
|
for (r2 = MAXRAD2; --r2; ) { |
| 285 |
|
|
| 286 |
|
|
| 287 |
|
int |
| 288 |
< |
findneigh(nl, h, v, rnl) /* find NNEIGH neighbors for pixel */ |
| 288 |
> |
findneigh(nl, nd, h, v, rnl) /* find NNEIGH neighbors for pixel */ |
| 289 |
|
short nl[NNEIGH][2]; |
| 290 |
+ |
int nd[NNEIGH]; |
| 291 |
|
int h, v; |
| 292 |
|
register short (*rnl)[NNEIGH]; |
| 293 |
|
{ |
| 294 |
|
int nn = 0; |
| 295 |
< |
int4 d, nd[NNEIGH]; |
| 317 |
< |
int n, hoff; |
| 295 |
> |
int d, n, hoff; |
| 296 |
|
register int h2, n2; |
| 297 |
|
|
| 298 |
|
nd[NNEIGH-1] = MAXRAD2; |
| 299 |
< |
for (hoff = 1; hoff < hres; hoff = (hoff<0) - hoff) { |
| 299 |
> |
for (hoff = 0; hoff < hres; hoff = (hoff<=0) - hoff) { |
| 300 |
|
h2 = h + hoff; |
| 301 |
|
if (h2 < 0 | h2 >= hres) |
| 302 |
|
continue; |
| 304 |
|
break; |
| 305 |
|
for (n = 0; n < NNEIGH && rnl[h2][n] < NINF; n++) { |
| 306 |
|
d = (h2-h)*(h2-h) + (v-rnl[h2][n])*(v-rnl[h2][n]); |
| 307 |
< |
if (d >= nd[NNEIGH-1]) |
| 307 |
> |
if (d == 0 | d >= nd[NNEIGH-1]) |
| 308 |
|
continue; |
| 309 |
|
if (nn < NNEIGH) /* insert neighbor */ |
| 310 |
|
nn++; |
| 325 |
|
} |
| 326 |
|
|
| 327 |
|
|
| 328 |
< |
meet_neighbors(nf, dp) /* run through samples and their neighbors */ |
| 328 |
> |
meet_neighbors(occ, nf, dp) /* run through samples and their neighbors */ |
| 329 |
> |
int occ; |
| 330 |
|
int (*nf)(); |
| 331 |
|
char *dp; |
| 332 |
|
{ |
| 333 |
|
short ln[NNEIGH][2]; |
| 334 |
+ |
int nd[NNEIGH]; |
| 335 |
|
int h, v, n, v2; |
| 336 |
|
register short (*rnl)[NNEIGH]; |
| 337 |
|
/* initialize bottom row list */ |
| 350 |
|
v = 0; /* do each row */ |
| 351 |
|
for ( ; ; ) { |
| 352 |
|
for (h = 0; h < hres; h++) { |
| 353 |
< |
if (!CHK4(pixFlags, v*hres+h)) |
| 354 |
< |
continue; /* no one home */ |
| 355 |
< |
n = findneigh(ln, h, v, rnl); |
| 356 |
< |
(*nf)(h, v, ln, n, dp); /* call on neighbors */ |
| 353 |
> |
if (!CHK4(pixFlags, v*hres+h) != !occ) |
| 354 |
> |
continue; /* occupancy mismatch */ |
| 355 |
> |
/* find neighbors */ |
| 356 |
> |
n = findneigh(ln, nd, h, v, rnl); |
| 357 |
> |
/* call on neighbors */ |
| 358 |
> |
(*nf)(h, v, ln, nd, n, dp); |
| 359 |
|
} |
| 360 |
|
if (++v >= vres) /* reinitialize row list */ |
| 361 |
|
break; |
| 370 |
|
} |
| 371 |
|
} |
| 372 |
|
} |
| 373 |
< |
free((char *)rnl); /* free row list */ |
| 373 |
> |
free((void *)rnl); /* free row list */ |
| 374 |
|
} |