ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
Revision: 1.15
Committed: Fri Jan 5 11:34:27 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.14: +78 -67 lines
Log Message:
changed addpixel() to fill parallelogram

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