ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pinterp.c
(Generate patch)

Comparing ray/src/px/pinterp.c (file contents):
Revision 1.6 by greg, Thu Dec 28 15:09:28 1989 UTC vs.
Revision 1.12 by greg, Thu Jan 4 14:25:56 1990 UTC

# Line 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
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 = 0.001;                   /* allowed z epsilon */
27 > double  zeps = .02;                     /* allowed z epsilon */
28  
29   COLR    *ourpict;                       /* output picture */
30   float   *ourzbuf;                       /* corresponding z-buffer */
# Line 31 | Line 34 | char   *progname;
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 */
# Line 39 | Line 48 | 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  
# Line 51 | Line 62 | char   *argv[];
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]);
# Line 109 | Line 157 | char   *argv[];
157                          break;
158                  default:
159                  badopt:
160 <                        fprintf(stderr, "%s: unknown option '%s'\n",
160 >                        fprintf(stderr, "%s: command line error at '%s'\n",
161                                          progname, argv[i]);
162 <                        exit(1);
162 >                        goto userr;
163                  }
164                                                  /* check arguments */
165 <        if (argc-i < 2 || (argc-i)%2) {
166 <                fprintf(stderr, "Usage: %s [view args] pfile zfile ..\n",
119 <                                progname);
120 <                exit(1);
121 <        }
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 *)calloc(ourview.hresolu*ourview.vresolu,sizeof(COLR));
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);
# Line 135 | Line 180 | char   *argv[];
180          for ( ; i < argc; i += 2)
181                  addpicture(argv[i], argv[i+1]);
182                                                          /* fill in spaces */
183 <        fillpicture();
183 >        if (fill&F_BACK)
184 >                backpicture();
185 >        else
186 >                fillpicture();
187                                                          /* add to header */
188          printargs(argc, argv, stdout);
189          if (gotvfile) {
# Line 144 | Line 192 | char   *argv[];
192                  printf("\n");
193          }
194          printf("\n");
195 <                                                        /* write output */
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  
# Line 169 | Line 225 | char   *s;
225   }
226  
227  
228 < addpicture(pfile, zfile)                /* add picture to output */
229 < char    *pfile, *zfile;
228 > addpicture(pfile, zspec)                /* add picture to output */
229 > char    *pfile, *zspec;
230   {
231 +        extern double   atof();
232          FILE    *pfp, *zfp;
176        COLR    *scanin;
177        float   *zin;
233          char    *err;
234 <        int     xres, yres;
234 >        COLR    *scanin;
235 >        float   *zin, *zlast;
236 >        int     *plast;
237          int     y;
238 <                                        /* open input files */
238 >                                        /* open picture file */
239          if ((pfp = fopen(pfile, "r")) == NULL) {
240                  perror(pfile);
241                  exit(1);
242          }
186        if ((zfp = fopen(zfile, "r")) == NULL) {
187                perror(zfile);
188                exit(1);
189        }
243                                          /* get header and view */
244          printf("%s:\n", pfile);
245          gotview = 0;
246          getheader(pfp, headline);
247 <        if (!gotview || fgetresolu(&xres, &yres, pfp) != (YMAJOR|YDECR)) {
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          }
198        theirview.hresolu = xres;
199        theirview.vresolu = yres;
252          if (err = setview(&theirview)) {
253                  fprintf(stderr, "%s: %s\n", pfile, err);
254                  exit(1);
# Line 204 | Line 256 | char   *pfile, *zfile;
256                                          /* compute transformation */
257          pixform(theirs2ours, &theirview, &ourview);
258                                          /* allocate scanlines */
259 <        scanin = (COLR *)malloc(xres*sizeof(COLR));
260 <        zin = (float *)malloc(xres*sizeof(float));
261 <        if (scanin == NULL || zin == NULL) {
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 = yres-1; y >= 0; y--) {
280 <                if (freadcolrs(scanin, xres, pfp) < 0) {
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 (fread(zin, sizeof(float), xres, zfp) < xres) {
285 <                        fprintf(stderr, "%s: read error\n", zfile);
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);
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 <        fclose(zfp);
298 >        if (zfp != NULL)
299 >                fclose(zfp);
300   }
301  
302  
# Line 266 | Line 336 | register VIEW  *vw1, *vw2;
336   }
337  
338  
339 < addscanline(y, pline, zline)            /* add scanline to output */
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;
277        register int    xpos, ypos;
353  
354 <        for (x = 0; x < theirview.hresolu; x++) {
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 <                        pos[2] /= sqrt( 1.
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.0)
367 >                if (pos[2] <= 0)
368                          continue;
369                  if (ourview.type == VT_PER) {
370                          pos[0] /= pos[2];
# Line 296 | Line 372 | float  *zline;
372                  }
373                  pos[0] += .5*ourview.hresolu;
374                  pos[1] += .5*ourview.vresolu;
375 <                if (pos[0] < 0 || pos[0] > ourview.hresolu
376 <                                || pos[1] < 0 || pos[1] > ourview.vresolu)
375 >                if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu
376 >                        || pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu)
377                          continue;
378 <                                        /* check current value at pos */
379 <                xpos = pos[0];
380 <                ypos = pos[1];
381 <                if (zscan(ypos)[xpos] <= 0.0
382 <                                || zscan(ypos)[xpos] - pos[2]
383 <                                        > zeps*zscan(ypos)[xpos]) {
384 <                        zscan(ypos)[xpos] = pos[2];
385 <                        copycolr(pscan(ypos)[xpos], pline[x]);
386 <                }
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 < fillpicture()                           /* fill in empty spaces */
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;
# Line 326 | Line 433 | fillpicture()                          /* fill in empty spaces */
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.0) {       /* empty pixel */
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.0)
453 >                                                if (zscan(i)[x] > 0)
454                                                          break;
455                                          if (i < ourview.vresolu
456                                  && (y <= 0 || zscan(y-1)[x] < zscan(i)[x]))
# Line 341 | Line 458 | fillpicture()                          /* fill in empty spaces */
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; x < ourview.hresolu; i++)
466 <                                                if (zscan(y)[i] > 0.0)
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]))
# Line 351 | Line 471 | fillpicture()                          /* fill in empty spaces */
471                                          else
472                                                  xback = x-1;
473                                  }
474 <                                if (xback < 0 && yback[x] < 0)
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 <                                if (yback[x] < 0 || ABS(x-xback) <= 1
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] < zscan(y)[xback] ))
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]);
# Line 368 | Line 500 | fillpicture()                          /* fill in empty spaces */
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;
# Line 378 | Line 521 | writepicture()                         /* write out picture */
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines