| 20 |
|
|
| 21 |
|
#include <ctype.h> |
| 22 |
|
|
| 23 |
< |
VIEW ourview = STDVIEW(470); /* viewing parameters */ |
| 23 |
> |
VIEW ourview = STDVIEW; /* viewing parameters */ |
| 24 |
> |
int hresolu, vresolu; /* image resolution */ |
| 25 |
|
|
| 26 |
|
int psample = 8; /* pixel sample size */ |
| 27 |
|
double maxdiff = .15; /* max. sample difference */ |
| 30 |
|
|
| 31 |
|
double dstrsrc = 0.0; /* square source distribution */ |
| 32 |
|
double shadthresh = .1; /* shadow threshold */ |
| 33 |
+ |
double shadcert = .25; /* shadow certainty */ |
| 34 |
|
|
| 35 |
|
int maxdepth = 4; /* maximum recursion depth */ |
| 36 |
|
double minweight = 1e-2; /* minimum ray weight */ |
| 37 |
|
|
| 38 |
|
COLOR ambval = BLKCOLOR; /* ambient value */ |
| 39 |
|
double ambacc = 0.2; /* ambient accuracy */ |
| 40 |
< |
int ambres = 64; /* ambient resolution */ |
| 40 |
> |
int ambres = 8; /* ambient resolution */ |
| 41 |
|
int ambdiv = 32; /* ambient divisions */ |
| 42 |
|
int ambssamp = 0; /* ambient super-samples */ |
| 43 |
|
int ambounce = 0; /* ambient bounces */ |
| 45 |
|
int ambincl = -1; /* include == 1, exclude == 0 */ |
| 46 |
|
|
| 47 |
|
int greyscale = 0; /* map colors to brightness? */ |
| 48 |
< |
char *devname = "X"; /* output device name */ |
| 48 |
> |
char *devname = dev_default; /* output device name */ |
| 49 |
|
|
| 50 |
|
struct driver *dev = NULL; /* driver functions */ |
| 51 |
|
|
| 55 |
|
RECT pframe; /* current frame boundaries */ |
| 56 |
|
int pdepth; /* image depth in current frame */ |
| 57 |
|
|
| 58 |
+ |
static char *reserve_mem = NULL; /* pre-allocated reserve memory */ |
| 59 |
+ |
|
| 60 |
+ |
#define RESERVE_AMT 8192 /* amount of memory to reserve */ |
| 61 |
+ |
|
| 62 |
|
#define CTRL(c) ('c'-'@') |
| 63 |
|
|
| 64 |
|
|
| 73 |
|
devopen(dname) /* open device driver */ |
| 74 |
|
char *dname; |
| 75 |
|
{ |
| 76 |
< |
extern char *progname; |
| 77 |
< |
char *devargv[3]; |
| 76 |
> |
extern char *progname, *octname; |
| 77 |
> |
char *id; |
| 78 |
|
register int i; |
| 79 |
+ |
|
| 80 |
+ |
id = octname!=NULL ? octname : progname; |
| 81 |
|
/* check device table */ |
| 82 |
|
for (i = 0; devtable[i].name; i++) |
| 83 |
|
if (!strcmp(dname, devtable[i].name)) |
| 84 |
< |
if ((dev = (*devtable[i].init)(progname)) == NULL) { |
| 84 |
> |
if ((dev = (*devtable[i].init)(dname, id)) == NULL) { |
| 85 |
|
sprintf(errmsg, "cannot initialize %s", dname); |
| 86 |
|
error(USER, errmsg); |
| 87 |
|
} else |
| 88 |
|
return; |
| 89 |
|
/* not there, try exec */ |
| 90 |
< |
devargv[0] = dname; |
| 83 |
< |
devargv[1] = progname; |
| 84 |
< |
devargv[2] = NULL; |
| 85 |
< |
if ((dev = comm_init(devargv)) == NULL) { |
| 90 |
> |
if ((dev = comm_init(dname, id)) == NULL) { |
| 91 |
|
sprintf(errmsg, "cannot start device \"%s\"", dname); |
| 92 |
|
error(USER, errmsg); |
| 93 |
|
} |
| 116 |
|
char buf[32]; |
| 117 |
|
|
| 118 |
|
devopen(devname); /* open device */ |
| 119 |
< |
newimage(); /* set up image */ |
| 119 |
> |
newimage(); /* start image (calls fillreserves) */ |
| 120 |
|
|
| 121 |
|
for ( ; ; ) { /* quit in command() */ |
| 122 |
< |
while (ourview.hresolu <= 1<<pdepth && |
| 118 |
< |
ourview.vresolu <= 1<<pdepth) |
| 122 |
> |
while (hresolu <= 1<<pdepth && vresolu <= 1<<pdepth) |
| 123 |
|
command("done: "); |
| 124 |
< |
|
| 125 |
< |
if (ourview.hresolu <= psample<<pdepth && |
| 126 |
< |
ourview.vresolu <= psample<<pdepth) { |
| 124 |
> |
while (reserve_mem == NULL) |
| 125 |
> |
command("out of memory: "); |
| 126 |
> |
errno = 0; |
| 127 |
> |
if (hresolu <= psample<<pdepth && vresolu <= psample<<pdepth) { |
| 128 |
|
sprintf(buf, "%d sampling...\n", 1<<pdepth); |
| 129 |
|
(*dev->comout)(buf); |
| 130 |
|
rsample(); |
| 131 |
|
} else { |
| 132 |
|
sprintf(buf, "%d refining...\n", 1<<pdepth); |
| 133 |
|
(*dev->comout)(buf); |
| 134 |
< |
refine(&ptrunk, 0, 0, ourview.hresolu, |
| 130 |
< |
ourview.vresolu, pdepth+1); |
| 134 |
> |
refine(&ptrunk, 0, 0, hresolu, vresolu, pdepth+1); |
| 135 |
|
} |
| 136 |
< |
if (dev->inpready) |
| 136 |
> |
if (errno == ENOMEM) /* ran out of memory */ |
| 137 |
> |
freereserves(); |
| 138 |
> |
else if (dev->inpready) /* noticed some input */ |
| 139 |
|
command(": "); |
| 140 |
< |
else |
| 140 |
> |
else /* finished this depth */ |
| 141 |
|
pdepth++; |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
|
| 146 |
+ |
fillreserves() /* fill memory reserves */ |
| 147 |
+ |
{ |
| 148 |
+ |
if (reserve_mem != NULL) |
| 149 |
+ |
return; |
| 150 |
+ |
reserve_mem = malloc(RESERVE_AMT); |
| 151 |
+ |
} |
| 152 |
+ |
|
| 153 |
+ |
|
| 154 |
+ |
freereserves() /* free memory reserves */ |
| 155 |
+ |
{ |
| 156 |
+ |
if (reserve_mem == NULL) |
| 157 |
+ |
return; |
| 158 |
+ |
free(reserve_mem); |
| 159 |
+ |
reserve_mem = NULL; |
| 160 |
+ |
} |
| 161 |
+ |
|
| 162 |
+ |
|
| 163 |
|
command(prompt) /* get/execute command */ |
| 164 |
|
char *prompt; |
| 165 |
|
{ |
| 168 |
|
char inpbuf[256]; |
| 169 |
|
char *args; |
| 170 |
|
again: |
| 171 |
< |
(*dev->comout)(prompt); /* get command + arguments */ |
| 149 |
< |
(*dev->comin)(inpbuf); |
| 171 |
> |
(*dev->comin)(inpbuf, prompt); /* get command + arguments */ |
| 172 |
|
for (args = inpbuf; *args && *args != ' '; args++) |
| 173 |
|
; |
| 174 |
|
if (*args) *args++ = '\0'; |
| 220 |
|
goto commerr; |
| 221 |
|
getmove(args); |
| 222 |
|
break; |
| 223 |
< |
case 'r': /* rotate camera */ |
| 224 |
< |
if (badcom("rotate")) |
| 225 |
< |
goto commerr; |
| 223 |
> |
case 'r': /* rotate/repaint */ |
| 224 |
> |
if (badcom("rotate")) { |
| 225 |
> |
if (badcom("repaint")) |
| 226 |
> |
goto commerr; |
| 227 |
> |
getrepaint(args); |
| 228 |
> |
break; |
| 229 |
> |
} |
| 230 |
|
getrotate(args); |
| 231 |
|
break; |
| 232 |
|
case 'p': /* pivot view */ |
| 288 |
|
* difference, we subsample the super-pixels. The testing process |
| 289 |
|
* includes initialization of the next row. |
| 290 |
|
*/ |
| 291 |
< |
xsiz = (((pframe.r-pframe.l)<<pdepth)+ourview.hresolu-1) / |
| 292 |
< |
ourview.hresolu; |
| 267 |
< |
ysiz = (((pframe.u-pframe.d)<<pdepth)+ourview.vresolu-1) / |
| 268 |
< |
ourview.vresolu; |
| 291 |
> |
xsiz = (((pframe.r-pframe.l)<<pdepth)+hresolu-1) / hresolu; |
| 292 |
> |
ysiz = (((pframe.u-pframe.d)<<pdepth)+vresolu-1) / vresolu; |
| 293 |
|
rl = (RECT *)malloc(xsiz*sizeof(RECT)); |
| 294 |
+ |
if (rl == NULL) |
| 295 |
+ |
return; |
| 296 |
|
pl = (PNODE **)malloc(xsiz*sizeof(PNODE *)); |
| 297 |
< |
if (rl == NULL || pl == NULL) |
| 298 |
< |
error(SYSTEM, "out of memory in rsample"); |
| 297 |
> |
if (pl == NULL) |
| 298 |
> |
return; |
| 299 |
|
/* |
| 300 |
|
* Initialize the bottom row. |
| 301 |
|
*/ |
| 302 |
|
rl[0].l = rl[0].d = 0; |
| 303 |
< |
rl[0].r = ourview.hresolu; rl[0].u = ourview.vresolu; |
| 303 |
> |
rl[0].r = hresolu; rl[0].u = vresolu; |
| 304 |
|
pl[0] = findrect(pframe.l, pframe.d, &ptrunk, rl, pdepth); |
| 305 |
|
for (x = 1; x < xsiz; x++) { |
| 306 |
|
rl[x].l = rl[x].d = 0; |
| 307 |
< |
rl[x].r = ourview.hresolu; rl[x].u = ourview.vresolu; |
| 308 |
< |
pl[x] = findrect(pframe.l+((x*ourview.hresolu)>>pdepth), |
| 307 |
> |
rl[x].r = hresolu; rl[x].u = vresolu; |
| 308 |
> |
pl[x] = findrect(pframe.l+((x*hresolu)>>pdepth), |
| 309 |
|
pframe.d, &ptrunk, rl+x, pdepth); |
| 310 |
|
} |
| 311 |
|
/* sample the image */ |
| 333 |
|
* Find super-pixel at this position in next row. |
| 334 |
|
*/ |
| 335 |
|
r.l = r.d = 0; |
| 336 |
< |
r.r = ourview.hresolu; r.u = ourview.vresolu; |
| 337 |
< |
p = findrect(pframe.l+((x*ourview.hresolu)>>pdepth), |
| 338 |
< |
pframe.d+(((y+1)*ourview.vresolu)>>pdepth), |
| 336 |
> |
r.r = hresolu; r.u = vresolu; |
| 337 |
> |
p = findrect(pframe.l+((x*hresolu)>>pdepth), |
| 338 |
> |
pframe.d+(((y+1)*vresolu)>>pdepth), |
| 339 |
|
&ptrunk, &r, pdepth); |
| 340 |
|
/* |
| 341 |
|
* Test super-pixel in next row. |
| 382 |
|
if (p->kid == NULL) { /* subdivide */ |
| 383 |
|
|
| 384 |
|
if ((p->kid = newptree()) == NULL) |
| 385 |
< |
error(SYSTEM, "out of memory in refine"); |
| 385 |
> |
return(growth); |
| 386 |
|
/* |
| 387 |
|
* The following paint order can leave a black pixel |
| 388 |
|
* when redraw() is called in (*dev->paintr)(). |