ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
Revision: 1.12
Committed: Thu Jan 4 14:25:56 1990 UTC (34 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.11: +20 -8 lines
Log Message:
added (*deffill)() in anticipation of computed fill

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