26 |
|
|
27 |
|
#define ABS(x) ((x)>0?(x):-(x)) |
28 |
|
|
29 |
+ |
struct position {int x,y; float z;}; |
30 |
+ |
|
31 |
|
VIEW ourview = STDVIEW(512); /* desired view */ |
32 |
|
|
33 |
|
double zeps = .02; /* allowed z epsilon */ |
37 |
|
|
38 |
|
char *progname; |
39 |
|
|
38 |
– |
VIEW theirview = STDVIEW(512); /* input view */ |
39 |
– |
int gotview; /* got input view? */ |
40 |
– |
|
40 |
|
int fill = F_FORE|F_BACK; /* selected fill algorithm */ |
41 |
< |
extern int backfill(), calfill(); /* fill functions */ |
41 |
> |
extern int backfill(), rcalfill(); /* fill functions */ |
42 |
|
int (*deffill)() = backfill; /* selected fill function */ |
43 |
|
COLR backcolr = BLKCOLR; /* background color */ |
44 |
|
double backz = 0.0; /* background z value */ |
45 |
+ |
int normdist = 1; /* normalized distance? */ |
46 |
+ |
double ourexp = -1; /* output picture exposure */ |
47 |
|
|
48 |
+ |
VIEW theirview = STDVIEW(512); /* input view */ |
49 |
+ |
int gotview; /* got input view? */ |
50 |
|
double theirs2ours[4][4]; /* transformation matrix */ |
51 |
< |
int normdist = 1; /* normalized distance? */ |
51 |
> |
double theirexp; /* input picture exposure */ |
52 |
|
|
53 |
+ |
int childpid = -1; /* id of fill process */ |
54 |
|
FILE *psend, *precv; /* pipes to/from fill calculation */ |
51 |
– |
int childpid; /* child's process id */ |
55 |
|
int queue[PACKSIZ][2]; /* pending pixels */ |
56 |
|
int queuesiz; /* number of pixels pending */ |
57 |
|
|
111 |
|
break; |
112 |
|
case 'r': /* rtrace */ |
113 |
|
check(3,1); |
114 |
< |
deffill = calfill; |
114 |
> |
deffill = rcalfill; |
115 |
|
calstart(RTCOM, argv[++i]); |
116 |
|
break; |
117 |
|
default: |
195 |
|
/* allocate frame */ |
196 |
|
ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR)); |
197 |
|
ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float)); |
198 |
< |
if (ourpict == NULL || ourzbuf == NULL) { |
199 |
< |
perror(progname); |
197 |
< |
exit(1); |
198 |
< |
} |
198 |
> |
if (ourpict == NULL || ourzbuf == NULL) |
199 |
> |
syserror(); |
200 |
|
/* get input */ |
201 |
|
for ( ; i < argc; i += 2) |
202 |
|
addpicture(argv[i], argv[i+1]); |
206 |
|
else |
207 |
|
fillpicture(); |
208 |
|
/* close calculation */ |
209 |
< |
if (deffill == calfill) |
209 |
< |
caldone(); |
209 |
> |
caldone(); |
210 |
|
/* add to header */ |
211 |
|
printargs(argc, argv, stdout); |
212 |
|
if (gotvfile) { |
214 |
|
fprintview(&ourview, stdout); |
215 |
|
printf("\n"); |
216 |
|
} |
217 |
+ |
if (ourexp > 0 && ourexp != 1.0) |
218 |
+ |
fputexpos(ourexp, stdout); |
219 |
|
printf("\n"); |
220 |
|
/* write picture */ |
221 |
|
writepicture(); |
241 |
|
|
242 |
|
printf("\t%s", s); |
243 |
|
|
244 |
+ |
if (isexpos(s)) { |
245 |
+ |
theirexp *= exposval(s); |
246 |
+ |
return; |
247 |
+ |
} |
248 |
|
for (an = altname; *an != NULL; an++) |
249 |
|
if (!strncmp(*an, s, strlen(*an))) { |
250 |
|
if (sscanview(&theirview, s+strlen(*an)) == 0) |
261 |
|
FILE *pfp, *zfp; |
262 |
|
char *err; |
263 |
|
COLR *scanin; |
264 |
< |
float *zin, *zlast; |
265 |
< |
int *plast; |
264 |
> |
float *zin; |
265 |
> |
struct position *plast; |
266 |
|
int y; |
267 |
|
/* open picture file */ |
268 |
|
if ((pfp = fopen(pfile, "r")) == NULL) { |
269 |
|
perror(pfile); |
270 |
|
exit(1); |
271 |
|
} |
272 |
< |
/* get header and view */ |
273 |
< |
printf("%s:\n", pfile); |
272 |
> |
/* get header with exposure and view */ |
273 |
> |
theirexp = 1.0; |
274 |
|
gotview = 0; |
275 |
+ |
printf("%s:\n", pfile); |
276 |
|
getheader(pfp, headline); |
277 |
|
if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp) |
278 |
|
!= (YMAJOR|YDECR)) { |
279 |
|
fprintf(stderr, "%s: picture view error\n", pfile); |
280 |
|
exit(1); |
281 |
|
} |
282 |
+ |
if (ourexp <= 0) |
283 |
+ |
ourexp = theirexp; |
284 |
+ |
else if (ABS(theirexp-ourexp) > .01*ourexp) |
285 |
+ |
fprintf(stderr, "%s: different exposure (warning)\n", pfile); |
286 |
|
if (err = setview(&theirview)) { |
287 |
|
fprintf(stderr, "%s: %s\n", pfile, err); |
288 |
|
exit(1); |
292 |
|
/* allocate scanlines */ |
293 |
|
scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR)); |
294 |
|
zin = (float *)malloc(theirview.hresolu*sizeof(float)); |
295 |
< |
plast = (int *)calloc(theirview.hresolu, sizeof(int)); |
296 |
< |
zlast = (float *)calloc(theirview.hresolu, sizeof(float)); |
297 |
< |
if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) { |
298 |
< |
perror(progname); |
288 |
< |
exit(1); |
289 |
< |
} |
295 |
> |
plast = (struct position *)calloc(theirview.hresolu, |
296 |
> |
sizeof(struct position)); |
297 |
> |
if (scanin == NULL || zin == NULL || plast == NULL) |
298 |
> |
syserror(); |
299 |
|
/* get z specification or file */ |
300 |
|
if ((zfp = fopen(zspec, "r")) == NULL) { |
301 |
|
double zvalue; |
319 |
|
fprintf(stderr, "%s: read error\n", zspec); |
320 |
|
exit(1); |
321 |
|
} |
322 |
< |
addscanline(y, scanin, zin, plast, zlast); |
322 |
> |
addscanline(y, scanin, zin, plast); |
323 |
|
} |
324 |
|
/* clean up */ |
325 |
|
free((char *)scanin); |
326 |
|
free((char *)zin); |
327 |
|
free((char *)plast); |
319 |
– |
free((char *)zlast); |
328 |
|
fclose(pfp); |
329 |
|
if (zfp != NULL) |
330 |
|
fclose(zfp); |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
< |
addscanline(y, pline, zline, lasty, lastyz) /* add scanline to output */ |
370 |
> |
addscanline(y, pline, zline, lasty) /* add scanline to output */ |
371 |
|
int y; |
372 |
|
COLR *pline; |
373 |
|
float *zline; |
374 |
< |
int *lasty; /* input/output */ |
367 |
< |
float *lastyz; /* input/output */ |
374 |
> |
struct position *lasty; /* input/output */ |
375 |
|
{ |
376 |
|
extern double sqrt(); |
377 |
|
double pos[3]; |
378 |
< |
int lastx = 0; |
372 |
< |
double lastxz = 0; |
373 |
< |
double zt; |
374 |
< |
int xpos, ypos; |
378 |
> |
struct position lastx, newpos; |
379 |
|
register int x; |
380 |
|
|
381 |
|
for (x = theirview.hresolu-1; x >= 0; x--) { |
391 |
|
pos[1] *= pos[2]; |
392 |
|
} |
393 |
|
multp3(pos, pos, theirs2ours); |
394 |
< |
if (pos[2] <= 0) |
394 |
> |
if (pos[2] <= 0) { |
395 |
> |
lasty[x].z = lastx.z = 0; /* mark invalid */ |
396 |
|
continue; |
397 |
+ |
} |
398 |
|
if (ourview.type == VT_PER) { |
399 |
|
pos[0] /= pos[2]; |
400 |
|
pos[1] /= pos[2]; |
401 |
|
} |
402 |
|
pos[0] += .5*ourview.hresolu; |
403 |
|
pos[1] += .5*ourview.vresolu; |
404 |
< |
if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu |
405 |
< |
|| pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu) |
406 |
< |
continue; |
404 |
> |
newpos.x = pos[0]; |
405 |
> |
newpos.y = pos[1]; |
406 |
> |
newpos.z = zline[x]; |
407 |
|
/* add pixel to our image */ |
408 |
< |
zt = 2.*zeps*zline[x]; |
409 |
< |
addpixel(xpos, ypos, |
410 |
< |
(fill&F_FORE && ABS(zline[x]-lastxz) <= zt) |
411 |
< |
? lastx - xpos : 1, |
412 |
< |
(fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt) |
413 |
< |
? lasty[x] - ypos : 1, |
414 |
< |
pline[x], pos[2]); |
415 |
< |
lastx = xpos; |
410 |
< |
lasty[x] = ypos; |
411 |
< |
lastxz = lastyz[x] = zline[x]; |
408 |
> |
if (pos[0] >= 0 && newpos.x < ourview.hresolu |
409 |
> |
&& pos[1] >= 0 && newpos.y < ourview.vresolu) { |
410 |
> |
addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]); |
411 |
> |
lasty[x].x = lastx.x = newpos.x; |
412 |
> |
lasty[x].y = lastx.y = newpos.y; |
413 |
> |
lasty[x].z = lastx.z = newpos.z; |
414 |
> |
} else |
415 |
> |
lasty[x].z = lastx.z = 0; /* mark invalid */ |
416 |
|
} |
417 |
|
} |
418 |
|
|
419 |
|
|
420 |
< |
addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */ |
421 |
< |
int xstart, ystart; |
418 |
< |
int width, height; |
420 |
> |
addpixel(p0, p1, p2, pix, z) /* fill in pixel parallelogram */ |
421 |
> |
struct position *p0, *p1, *p2; |
422 |
|
COLR pix; |
423 |
|
double z; |
424 |
|
{ |
425 |
< |
register int x, y; |
426 |
< |
/* make width and height positive */ |
427 |
< |
if (width < 0) { |
428 |
< |
width = -width; |
429 |
< |
xstart = xstart-width+1; |
430 |
< |
} else if (width == 0) |
431 |
< |
width = 1; |
432 |
< |
if (height < 0) { |
433 |
< |
height = -height; |
434 |
< |
ystart = ystart-height+1; |
435 |
< |
} else if (height == 0) |
436 |
< |
height = 1; |
437 |
< |
/* fill pixel(s) within rectangle */ |
438 |
< |
for (y = ystart; y < ystart+height; y++) |
439 |
< |
for (x = xstart; x < xstart+width; x++) |
440 |
< |
if (zscan(y)[x] <= 0 |
441 |
< |
|| zscan(y)[x]-z > zeps*zscan(y)[x]) { |
425 |
> |
double zt = 2.*zeps*p0->z; /* threshold */ |
426 |
> |
int s1x, s1y, s2x, s2y; /* step sizes */ |
427 |
> |
int l1, l2, c1, c2; /* side lengths and counters */ |
428 |
> |
int p1isy; /* p0p1 along y? */ |
429 |
> |
int x1, y1; /* p1 position */ |
430 |
> |
register int x, y; /* final position */ |
431 |
> |
|
432 |
> |
/* compute vector p0p1 */ |
433 |
> |
if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) { |
434 |
> |
s1x = p1->x - p0->x; |
435 |
> |
s1y = p1->y - p0->y; |
436 |
> |
l1 = ABS(s1x); |
437 |
> |
if (p1isy = (ABS(s1y) > l1)) |
438 |
> |
l1 = ABS(s1y); |
439 |
> |
} else { |
440 |
> |
l1 = s1x = s1y = 1; |
441 |
> |
p1isy = -1; |
442 |
> |
} |
443 |
> |
/* compute vector p0p2 */ |
444 |
> |
if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) { |
445 |
> |
s2x = p2->x - p0->x; |
446 |
> |
s2y = p2->y - p0->y; |
447 |
> |
if (p1isy == 1) |
448 |
> |
l2 = ABS(s2x); |
449 |
> |
else { |
450 |
> |
l2 = ABS(s2y); |
451 |
> |
if (p1isy != 0 && ABS(s2x) > l2) |
452 |
> |
l2 = ABS(s2x); |
453 |
> |
} |
454 |
> |
} else |
455 |
> |
l2 = s2x = s2y = 1; |
456 |
> |
/* fill the parallelogram */ |
457 |
> |
for (c1 = l1; c1-- > 0; ) { |
458 |
> |
x1 = p0->x + c1*s1x/l1; |
459 |
> |
y1 = p0->y + c1*s1y/l1; |
460 |
> |
for (c2 = l2; c2-- > 0; ) { |
461 |
> |
x = x1 + c2*s2x/l2; |
462 |
> |
y = y1 + c2*s2y/l2; |
463 |
> |
if (zscan(y)[x] <= 0 || zscan(y)[x]-z |
464 |
> |
> zeps*zscan(y)[x]) { |
465 |
|
zscan(y)[x] = z; |
466 |
|
copycolr(pscan(y)[x], pix); |
467 |
|
} |
468 |
+ |
} |
469 |
+ |
} |
470 |
|
} |
471 |
|
|
472 |
|
|
478 |
|
register int x, i; |
479 |
|
/* get back buffer */ |
480 |
|
yback = (int *)malloc(ourview.hresolu*sizeof(int)); |
481 |
< |
if (yback == NULL) { |
482 |
< |
perror(progname); |
455 |
< |
return; |
456 |
< |
} |
481 |
> |
if (yback == NULL) |
482 |
> |
syserror(); |
483 |
|
for (x = 0; x < ourview.hresolu; x++) |
484 |
|
yback[x] = -2; |
485 |
|
/* |
569 |
|
|
570 |
|
fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout); |
571 |
|
for (y = ourview.vresolu-1; y >= 0; y--) |
572 |
< |
if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) { |
573 |
< |
perror(progname); |
548 |
< |
exit(1); |
549 |
< |
} |
572 |
> |
if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) |
573 |
> |
syserror(); |
574 |
|
} |
575 |
|
|
576 |
|
|
588 |
|
exit(1); |
589 |
|
} |
590 |
|
if (donorm |
591 |
< |
&& (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) { |
592 |
< |
perror(progname); |
569 |
< |
exit(1); |
570 |
< |
} |
591 |
> |
&& (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) |
592 |
> |
syserror(); |
593 |
|
for (y = ourview.vresolu-1; y >= 0; y--) { |
594 |
|
if (donorm) { |
595 |
|
double vx, yzn2; |
642 |
|
char combuf[512]; |
643 |
|
int p0[2], p1[2]; |
644 |
|
|
645 |
+ |
if (childpid != -1) { |
646 |
+ |
fprintf(stderr, "%s: too many calculations\n", progname); |
647 |
+ |
exit(1); |
648 |
+ |
} |
649 |
|
sprintf(combuf, prog, PACKSIZ, args); |
650 |
|
if (pipe(p0) < 0 || pipe(p1) < 0) |
651 |
< |
goto syserr; |
651 |
> |
syserror(); |
652 |
|
if ((childpid = vfork()) == 0) { /* fork calculation */ |
653 |
|
close(p0[1]); |
654 |
|
close(p1[0]); |
664 |
|
perror("/bin/sh"); |
665 |
|
_exit(127); |
666 |
|
} |
667 |
< |
if (childpid < 0) |
668 |
< |
goto syserr; |
667 |
> |
if (childpid == -1) |
668 |
> |
syserror(); |
669 |
|
close(p0[0]); |
670 |
|
close(p1[1]); |
671 |
|
if ((psend = fdopen(p0[1], "w")) == NULL) |
672 |
< |
goto syserr; |
672 |
> |
syserror(); |
673 |
|
if ((precv = fdopen(p1[0], "r")) == NULL) |
674 |
< |
goto syserr; |
674 |
> |
syserror(); |
675 |
|
queuesiz = 0; |
650 |
– |
return; |
651 |
– |
syserr: |
652 |
– |
perror(progname); |
653 |
– |
exit(1); |
676 |
|
} |
677 |
|
|
678 |
|
|
680 |
|
{ |
681 |
|
int pid; |
682 |
|
|
683 |
< |
fclose(psend); |
683 |
> |
if (childpid == -1) |
684 |
> |
return; |
685 |
> |
if (fclose(psend) == EOF) |
686 |
> |
syserror(); |
687 |
|
clearqueue(); |
688 |
|
fclose(precv); |
689 |
|
while ((pid = wait(0)) != -1 && pid != childpid) |
690 |
|
; |
691 |
+ |
childpid = -1; |
692 |
|
} |
693 |
|
|
694 |
|
|
695 |
< |
calfill(x, y) /* fill with calculated pixel */ |
695 |
> |
rcalfill(x, y) /* fill with ray-calculated pixel */ |
696 |
|
int x, y; |
697 |
|
{ |
698 |
|
FVECT orig, dir; |
699 |
|
float outbuf[6]; |
700 |
|
|
701 |
|
if (queuesiz >= PACKSIZ) { /* flush queue */ |
702 |
< |
fflush(psend); |
702 |
> |
if (fflush(psend) == EOF) |
703 |
> |
syserror(); |
704 |
|
clearqueue(); |
705 |
|
} |
706 |
|
/* send new ray */ |
707 |
|
rayview(orig, dir, &ourview, x+.5, y+.5); |
708 |
|
outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2]; |
709 |
|
outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2]; |
710 |
< |
fwrite(outbuf, sizeof(float), 6, psend); |
710 |
> |
if (fwrite(outbuf, sizeof(float), 6, psend) < 6) |
711 |
> |
syserror(); |
712 |
|
/* remember it */ |
713 |
|
queue[queuesiz][0] = x; |
714 |
|
queue[queuesiz][1] = y; |
722 |
|
register int i; |
723 |
|
|
724 |
|
for (i = 0; i < queuesiz; i++) { |
725 |
< |
fread(inbuf, sizeof(float), 4, precv); |
725 |
> |
if (fread(inbuf, sizeof(float), 4, precv) < 4) { |
726 |
> |
fprintf(stderr, "%s: read error in clearqueue\n", |
727 |
> |
progname); |
728 |
> |
exit(1); |
729 |
> |
} |
730 |
> |
if (ourexp > 0 && ourexp != 1.0) { |
731 |
> |
inbuf[0] *= ourexp; |
732 |
> |
inbuf[1] *= ourexp; |
733 |
> |
inbuf[2] *= ourexp; |
734 |
> |
} |
735 |
|
setcolr(pscan(queue[i][1])[queue[i][0]], |
736 |
|
inbuf[0], inbuf[1], inbuf[2]); |
737 |
|
zscan(queue[i][1])[queue[i][0]] = inbuf[3]; |
738 |
|
} |
739 |
|
queuesiz = 0; |
740 |
+ |
} |
741 |
+ |
|
742 |
+ |
|
743 |
+ |
syserror() /* report error and exit */ |
744 |
+ |
{ |
745 |
+ |
perror(progname); |
746 |
+ |
exit(1); |
747 |
|
} |