ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
Revision: 1.13
Committed: Thu Jan 4 16:52:05 1990 UTC (34 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.12: +122 -6 lines
Log Message:
added fill calculation with rtrace

File Contents

# Content
1 #ifndef lint
2 static char SCCSid[] = "$SunId$ LBL";
3 #endif
4
5 /*
6 * Interpolate and extrapolate pictures with different view parameters.
7 *
8 * Greg Ward 09Dec89
9 */
10
11 #include "standard.h"
12
13 #include "view.h"
14
15 #include "color.h"
16
17 #define pscan(y) (ourpict+(y)*ourview.hresolu)
18 #define zscan(y) (ourzbuf+(y)*ourview.hresolu)
19
20 #define F_FORE 1 /* fill foreground */
21 #define F_BACK 2 /* fill background */
22
23 #define PACKSIZ 42 /* calculation packet size */
24
25 #define RTCOM "rtrace -h -ovl -fff -x %d %s"
26
27 #define ABS(x) ((x)>0?(x):-(x))
28
29 VIEW ourview = STDVIEW(512); /* desired view */
30
31 double zeps = .02; /* allowed z epsilon */
32
33 COLR *ourpict; /* output picture */
34 float *ourzbuf; /* corresponding z-buffer */
35
36 char *progname;
37
38 VIEW theirview = STDVIEW(512); /* input view */
39 int gotview; /* got input view? */
40
41 int fill = F_FORE|F_BACK; /* selected fill algorithm */
42 extern int backfill(), calfill(); /* fill functions */
43 int (*deffill)() = backfill; /* selected fill function */
44 COLR backcolr = BLKCOLR; /* background color */
45 double backz = 0.0; /* background z value */
46
47 double theirs2ours[4][4]; /* transformation matrix */
48 int normdist = 1; /* normalized distance? */
49
50 FILE *psend, *precv; /* pipes to/from fill calculation */
51 int childpid; /* child's process id */
52 int queue[PACKSIZ][2]; /* pending pixels */
53 int queuesiz; /* number of pixels pending */
54
55
56 main(argc, argv) /* interpolate pictures */
57 int argc;
58 char *argv[];
59 {
60 #define check(olen,narg) if (argv[i][olen] || narg >= argc-i) goto badopt
61 extern double atof();
62 int gotvfile = 0;
63 char *zfile = NULL;
64 char *err;
65 int i;
66
67 progname = argv[0];
68
69 for (i = 1; i < argc && argv[i][0] == '-'; i++)
70 switch (argv[i][1]) {
71 case 't': /* threshold */
72 check(2,1);
73 zeps = atof(argv[++i]);
74 break;
75 case 'n': /* dist. normalized? */
76 check(2,0);
77 normdist = !normdist;
78 break;
79 case 'f': /* fill type */
80 switch (argv[i][2]) {
81 case '0': /* none */
82 check(3,0);
83 fill = 0;
84 break;
85 case 'f': /* foreground */
86 check(3,0);
87 fill = F_FORE;
88 break;
89 case 'b': /* background */
90 check(3,0);
91 fill = F_BACK;
92 break;
93 case 'a': /* all */
94 check(3,0);
95 fill = F_FORE|F_BACK;
96 break;
97 case 'c': /* color */
98 check(3,3);
99 deffill = backfill;
100 setcolr(backcolr, atof(argv[i+1]),
101 atof(argv[i+2]), atof(argv[i+3]));
102 i += 3;
103 break;
104 case 'z': /* z value */
105 check(3,1);
106 deffill = backfill;
107 backz = atof(argv[++i]);
108 break;
109 case 'r': /* rtrace */
110 check(3,1);
111 deffill = calfill;
112 calstart(RTCOM, argv[++i]);
113 break;
114 default:
115 goto badopt;
116 }
117 break;
118 case 'z': /* z file */
119 check(2,1);
120 zfile = argv[++i];
121 break;
122 case 'x': /* x resolution */
123 check(2,1);
124 ourview.hresolu = atoi(argv[++i]);
125 break;
126 case 'y': /* y resolution */
127 check(2,1);
128 ourview.vresolu = atoi(argv[++i]);
129 break;
130 case 'v': /* view */
131 switch (argv[i][2]) {
132 case 't': /* type */
133 check(4,0);
134 ourview.type = argv[i][3];
135 break;
136 case 'p': /* point */
137 check(3,3);
138 ourview.vp[0] = atof(argv[++i]);
139 ourview.vp[1] = atof(argv[++i]);
140 ourview.vp[2] = atof(argv[++i]);
141 break;
142 case 'd': /* direction */
143 check(3,3);
144 ourview.vdir[0] = atof(argv[++i]);
145 ourview.vdir[1] = atof(argv[++i]);
146 ourview.vdir[2] = atof(argv[++i]);
147 break;
148 case 'u': /* up */
149 check(3,3);
150 ourview.vup[0] = atof(argv[++i]);
151 ourview.vup[1] = atof(argv[++i]);
152 ourview.vup[2] = atof(argv[++i]);
153 break;
154 case 'h': /* horizontal */
155 check(3,1);
156 ourview.horiz = atof(argv[++i]);
157 break;
158 case 'v': /* vertical */
159 check(3,1);
160 ourview.vert = atof(argv[++i]);
161 break;
162 case 'f': /* file */
163 check(3,1);
164 gotvfile = viewfile(argv[++i], &ourview);
165 if (gotvfile < 0) {
166 perror(argv[i]);
167 exit(1);
168 } else if (gotvfile == 0) {
169 fprintf(stderr, "%s: bad view file\n",
170 argv[i]);
171 exit(1);
172 }
173 break;
174 default:
175 goto badopt;
176 }
177 break;
178 default:
179 badopt:
180 fprintf(stderr, "%s: command line error at '%s'\n",
181 progname, argv[i]);
182 goto userr;
183 }
184 /* check arguments */
185 if ((argc-i)%2)
186 goto userr;
187 /* set view */
188 if (err = setview(&ourview)) {
189 fprintf(stderr, "%s: %s\n", progname, err);
190 exit(1);
191 }
192 /* allocate frame */
193 ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR));
194 ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float));
195 if (ourpict == NULL || ourzbuf == NULL) {
196 perror(progname);
197 exit(1);
198 }
199 /* get input */
200 for ( ; i < argc; i += 2)
201 addpicture(argv[i], argv[i+1]);
202 /* fill in spaces */
203 if (fill&F_BACK)
204 backpicture();
205 else
206 fillpicture();
207 /* close calculation */
208 if (deffill == calfill)
209 caldone();
210 /* add to header */
211 printargs(argc, argv, stdout);
212 if (gotvfile) {
213 printf(VIEWSTR);
214 fprintview(&ourview, stdout);
215 printf("\n");
216 }
217 printf("\n");
218 /* write picture */
219 writepicture();
220 /* write z file */
221 if (zfile != NULL)
222 writedistance(zfile);
223
224 exit(0);
225 userr:
226 fprintf(stderr,
227 "Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n",
228 progname);
229 exit(1);
230 #undef check
231 }
232
233
234 headline(s) /* process header string */
235 char *s;
236 {
237 static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
238 register char **an;
239
240 printf("\t%s", s);
241
242 for (an = altname; *an != NULL; an++)
243 if (!strncmp(*an, s, strlen(*an))) {
244 if (sscanview(&theirview, s+strlen(*an)) == 0)
245 gotview++;
246 break;
247 }
248 }
249
250
251 addpicture(pfile, zspec) /* add picture to output */
252 char *pfile, *zspec;
253 {
254 extern double atof();
255 FILE *pfp, *zfp;
256 char *err;
257 COLR *scanin;
258 float *zin, *zlast;
259 int *plast;
260 int y;
261 /* open picture file */
262 if ((pfp = fopen(pfile, "r")) == NULL) {
263 perror(pfile);
264 exit(1);
265 }
266 /* get header and view */
267 printf("%s:\n", pfile);
268 gotview = 0;
269 getheader(pfp, headline);
270 if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp)
271 != (YMAJOR|YDECR)) {
272 fprintf(stderr, "%s: picture view error\n", pfile);
273 exit(1);
274 }
275 if (err = setview(&theirview)) {
276 fprintf(stderr, "%s: %s\n", pfile, err);
277 exit(1);
278 }
279 /* compute transformation */
280 pixform(theirs2ours, &theirview, &ourview);
281 /* allocate scanlines */
282 scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR));
283 zin = (float *)malloc(theirview.hresolu*sizeof(float));
284 plast = (int *)calloc(theirview.hresolu, sizeof(int));
285 zlast = (float *)calloc(theirview.hresolu, sizeof(float));
286 if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) {
287 perror(progname);
288 exit(1);
289 }
290 /* get z specification or file */
291 if ((zfp = fopen(zspec, "r")) == NULL) {
292 double zvalue;
293 register int x;
294 if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
295 perror(zspec);
296 exit(1);
297 }
298 for (x = 0; x < theirview.hresolu; x++)
299 zin[x] = zvalue;
300 }
301 /* load image */
302 for (y = theirview.vresolu-1; y >= 0; y--) {
303 if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) {
304 fprintf(stderr, "%s: read error\n", pfile);
305 exit(1);
306 }
307 if (zfp != NULL
308 && fread(zin,sizeof(float),theirview.hresolu,zfp)
309 < theirview.hresolu) {
310 fprintf(stderr, "%s: read error\n", zspec);
311 exit(1);
312 }
313 addscanline(y, scanin, zin, plast, zlast);
314 }
315 /* clean up */
316 free((char *)scanin);
317 free((char *)zin);
318 free((char *)plast);
319 free((char *)zlast);
320 fclose(pfp);
321 if (zfp != NULL)
322 fclose(zfp);
323 }
324
325
326 pixform(xfmat, vw1, vw2) /* compute view1 to view2 matrix */
327 register double xfmat[4][4];
328 register VIEW *vw1, *vw2;
329 {
330 double m4t[4][4];
331
332 setident4(xfmat);
333 xfmat[0][0] = vw1->vhinc[0];
334 xfmat[0][1] = vw1->vhinc[1];
335 xfmat[0][2] = vw1->vhinc[2];
336 xfmat[1][0] = vw1->vvinc[0];
337 xfmat[1][1] = vw1->vvinc[1];
338 xfmat[1][2] = vw1->vvinc[2];
339 xfmat[2][0] = vw1->vdir[0];
340 xfmat[2][1] = vw1->vdir[1];
341 xfmat[2][2] = vw1->vdir[2];
342 xfmat[3][0] = vw1->vp[0];
343 xfmat[3][1] = vw1->vp[1];
344 xfmat[3][2] = vw1->vp[2];
345 setident4(m4t);
346 m4t[0][0] = vw2->vhinc[0]/vw2->vhn2;
347 m4t[1][0] = vw2->vhinc[1]/vw2->vhn2;
348 m4t[2][0] = vw2->vhinc[2]/vw2->vhn2;
349 m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2;
350 m4t[0][1] = vw2->vvinc[0]/vw2->vvn2;
351 m4t[1][1] = vw2->vvinc[1]/vw2->vvn2;
352 m4t[2][1] = vw2->vvinc[2]/vw2->vvn2;
353 m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2;
354 m4t[0][2] = vw2->vdir[0];
355 m4t[1][2] = vw2->vdir[1];
356 m4t[2][2] = vw2->vdir[2];
357 m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
358 multmat4(xfmat, xfmat, m4t);
359 }
360
361
362 addscanline(y, pline, zline, lasty, lastyz) /* add scanline to output */
363 int y;
364 COLR *pline;
365 float *zline;
366 int *lasty; /* input/output */
367 float *lastyz; /* input/output */
368 {
369 extern double sqrt();
370 double pos[3];
371 int lastx = 0;
372 double lastxz = 0;
373 double zt;
374 int xpos, ypos;
375 register int x;
376
377 for (x = theirview.hresolu-1; x >= 0; x--) {
378 pos[0] = x - .5*(theirview.hresolu-1);
379 pos[1] = y - .5*(theirview.vresolu-1);
380 pos[2] = zline[x];
381 if (theirview.type == VT_PER) {
382 if (normdist) /* adjust for eye-ray distance */
383 pos[2] /= sqrt( 1.
384 + pos[0]*pos[0]*theirview.vhn2
385 + pos[1]*pos[1]*theirview.vvn2 );
386 pos[0] *= pos[2];
387 pos[1] *= pos[2];
388 }
389 multp3(pos, pos, theirs2ours);
390 if (pos[2] <= 0)
391 continue;
392 if (ourview.type == VT_PER) {
393 pos[0] /= pos[2];
394 pos[1] /= pos[2];
395 }
396 pos[0] += .5*ourview.hresolu;
397 pos[1] += .5*ourview.vresolu;
398 if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu
399 || pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu)
400 continue;
401 /* add pixel to our image */
402 zt = 2.*zeps*zline[x];
403 addpixel(xpos, ypos,
404 (fill&F_FORE && ABS(zline[x]-lastxz) <= zt)
405 ? lastx - xpos : 1,
406 (fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt)
407 ? lasty[x] - ypos : 1,
408 pline[x], pos[2]);
409 lastx = xpos;
410 lasty[x] = ypos;
411 lastxz = lastyz[x] = zline[x];
412 }
413 }
414
415
416 addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */
417 int xstart, ystart;
418 int width, height;
419 COLR pix;
420 double z;
421 {
422 register int x, y;
423 /* make width and height positive */
424 if (width < 0) {
425 width = -width;
426 xstart = xstart-width+1;
427 } else if (width == 0)
428 width = 1;
429 if (height < 0) {
430 height = -height;
431 ystart = ystart-height+1;
432 } else if (height == 0)
433 height = 1;
434 /* fill pixel(s) within rectangle */
435 for (y = ystart; y < ystart+height; y++)
436 for (x = xstart; x < xstart+width; x++)
437 if (zscan(y)[x] <= 0
438 || zscan(y)[x]-z > zeps*zscan(y)[x]) {
439 zscan(y)[x] = z;
440 copycolr(pscan(y)[x], pix);
441 }
442 }
443
444
445 backpicture() /* background fill algorithm */
446 {
447 int *yback, xback;
448 int y;
449 COLR pfill;
450 register int x, i;
451 /* get back buffer */
452 yback = (int *)malloc(ourview.hresolu*sizeof(int));
453 if (yback == NULL) {
454 perror(progname);
455 return;
456 }
457 for (x = 0; x < ourview.hresolu; x++)
458 yback[x] = -2;
459 /*
460 * Xback and yback are the pixel locations of suitable
461 * background values in each direction.
462 * A value of -2 means unassigned, and -1 means
463 * that there is no suitable background in this direction.
464 */
465 /* fill image */
466 for (y = 0; y < ourview.vresolu; y++) {
467 xback = -2;
468 for (x = 0; x < ourview.hresolu; x++)
469 if (zscan(y)[x] <= 0) { /* empty pixel */
470 /*
471 * First, find background from above or below.
472 * (farthest assigned pixel)
473 */
474 if (yback[x] == -2) {
475 for (i = y+1; i < ourview.vresolu; i++)
476 if (zscan(i)[x] > 0)
477 break;
478 if (i < ourview.vresolu
479 && (y <= 0 || zscan(y-1)[x] < zscan(i)[x]))
480 yback[x] = i;
481 else
482 yback[x] = y-1;
483 }
484 /*
485 * Next, find background from left or right.
486 */
487 if (xback == -2) {
488 for (i = x+1; i < ourview.hresolu; i++)
489 if (zscan(y)[i] > 0)
490 break;
491 if (i < ourview.hresolu
492 && (x <= 0 || zscan(y)[x-1] < zscan(y)[i]))
493 xback = i;
494 else
495 xback = x-1;
496 }
497 /*
498 * Check to see if we have no background for
499 * this pixel. If not, use background color.
500 */
501 if (xback < 0 && yback[x] < 0) {
502 (*deffill)(x,y);
503 continue;
504 }
505 /*
506 * Compare, and use the background that is
507 * farther, unless one of them is next to us.
508 */
509 if ( yback[x] < 0
510 || (xback >= 0 && ABS(x-xback) <= 1)
511 || ( ABS(y-yback[x]) > 1
512 && zscan(yback[x])[x]
513 < zscan(y)[xback] ) ) {
514 copycolr(pscan(y)[x],pscan(y)[xback]);
515 zscan(y)[x] = zscan(y)[xback];
516 } else {
517 copycolr(pscan(y)[x],pscan(yback[x])[x]);
518 zscan(y)[x] = zscan(yback[x])[x];
519 }
520 } else { /* full pixel */
521 yback[x] = -2;
522 xback = -2;
523 }
524 }
525 free((char *)yback);
526 }
527
528
529 fillpicture() /* paint in empty pixels with default */
530 {
531 register int x, y;
532
533 for (y = 0; y < ourview.vresolu; y++)
534 for (x = 0; x < ourview.hresolu; x++)
535 if (zscan(y)[x] <= 0)
536 (*deffill)(x,y);
537 }
538
539
540 writepicture() /* write out picture */
541 {
542 int y;
543
544 fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
545 for (y = ourview.vresolu-1; y >= 0; y--)
546 if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) {
547 perror(progname);
548 exit(1);
549 }
550 }
551
552
553 writedistance(fname) /* write out z file */
554 char *fname;
555 {
556 extern double sqrt();
557 int donorm = normdist && ourview.type == VT_PER;
558 FILE *fp;
559 int y;
560 float *zout;
561
562 if ((fp = fopen(fname, "w")) == NULL) {
563 perror(fname);
564 exit(1);
565 }
566 if (donorm
567 && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) {
568 perror(progname);
569 exit(1);
570 }
571 for (y = ourview.vresolu-1; y >= 0; y--) {
572 if (donorm) {
573 double vx, yzn2;
574 register int x;
575 yzn2 = y - .5*(ourview.vresolu-1);
576 yzn2 = 1. + yzn2*yzn2*ourview.vvn2;
577 for (x = 0; x < ourview.hresolu; x++) {
578 vx = x - .5*(ourview.hresolu-1);
579 zout[x] = zscan(y)[x]
580 * sqrt(vx*vx*ourview.vhn2 + yzn2);
581 }
582 } else
583 zout = zscan(y);
584 if (fwrite(zout, sizeof(float), ourview.hresolu, fp)
585 < ourview.hresolu) {
586 perror(fname);
587 exit(1);
588 }
589 }
590 if (donorm)
591 free((char *)zout);
592 fclose(fp);
593 }
594
595
596 isfloat(s) /* see if string is floating number */
597 register char *s;
598 {
599 for ( ; *s; s++)
600 if ((*s < '0' || *s > '9') && *s != '.' && *s != '-'
601 && *s != 'e' && *s != 'E' && *s != '+')
602 return(0);
603 return(1);
604 }
605
606
607 backfill(x, y) /* fill pixel with background */
608 int x, y;
609 {
610 register BYTE *dest = pscan(y)[x];
611
612 copycolr(dest, backcolr);
613 zscan(y)[x] = backz;
614 }
615
616
617 calstart(prog, args) /* start fill calculation */
618 char *prog, *args;
619 {
620 char combuf[512];
621 int p0[2], p1[2];
622
623 sprintf(combuf, prog, PACKSIZ, args);
624 if (pipe(p0) < 0 || pipe(p1) < 0)
625 goto syserr;
626 if ((childpid = vfork()) == 0) { /* fork calculation */
627 close(p0[1]);
628 close(p1[0]);
629 if (p0[0] != 0) {
630 dup2(p0[0], 0);
631 close(p0[0]);
632 }
633 if (p1[1] != 1) {
634 dup2(p1[1], 1);
635 close(p1[1]);
636 }
637 execl("/bin/sh", "sh", "-c", combuf, 0);
638 perror("/bin/sh");
639 _exit(127);
640 }
641 if (childpid < 0)
642 goto syserr;
643 close(p0[0]);
644 close(p1[1]);
645 if ((psend = fdopen(p0[1], "w")) == NULL)
646 goto syserr;
647 if ((precv = fdopen(p1[0], "r")) == NULL)
648 goto syserr;
649 queuesiz = 0;
650 return;
651 syserr:
652 perror(progname);
653 exit(1);
654 }
655
656
657 caldone() /* done with calculation */
658 {
659 int pid;
660
661 fclose(psend);
662 clearqueue();
663 fclose(precv);
664 while ((pid = wait(0)) != -1 && pid != childpid)
665 ;
666 }
667
668
669 calfill(x, y) /* fill with calculated pixel */
670 int x, y;
671 {
672 FVECT orig, dir;
673 float outbuf[6];
674
675 if (queuesiz >= PACKSIZ) { /* flush queue */
676 fflush(psend);
677 clearqueue();
678 }
679 /* send new ray */
680 rayview(orig, dir, &ourview, x+.5, y+.5);
681 outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
682 outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
683 fwrite(outbuf, sizeof(float), 6, psend);
684 /* remember it */
685 queue[queuesiz][0] = x;
686 queue[queuesiz][1] = y;
687 queuesiz++;
688 }
689
690
691 clearqueue() /* get results from queue */
692 {
693 float inbuf[4];
694 register int i;
695
696 for (i = 0; i < queuesiz; i++) {
697 fread(inbuf, sizeof(float), 4, precv);
698 setcolr(pscan(queue[i][1])[queue[i][0]],
699 inbuf[0], inbuf[1], inbuf[2]);
700 zscan(queue[i][1])[queue[i][0]] = inbuf[3];
701 }
702 queuesiz = 0;
703 }