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.9 by greg, Wed Jan 3 12:52:30 1990 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 */
# 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;
233          char    *err;
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;
# Line 205 | Line 258 | char   *pfile, *zfile;
258                                          /* allocate scanlines */
259          scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR));
260          zin = (float *)malloc(theirview.hresolu*sizeof(float));
208        zlast = (float *)calloc(theirview.hresolu, sizeof(float));
261          plast = (int *)calloc(theirview.hresolu, sizeof(int));
262 <        if (scanin == NULL || zin == NULL || zlast == NULL || plast == NULL) {
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 (fread(zin, sizeof(float), theirview.hresolu, zfp)
284 >                if (zfp != NULL
285 >                        && fread(zin,sizeof(float),theirview.hresolu,zfp)
286                                  < theirview.hresolu) {
287 <                        fprintf(stderr, "%s: read error\n", zfile);
287 >                        fprintf(stderr, "%s: read error\n", zspec);
288                          exit(1);
289                  }
290                  addscanline(y, scanin, zin, plast, zlast);
# Line 230 | Line 295 | char   *pfile, *zfile;
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 277 | Line 343 | float  *zline;
343   int     *lasty;                 /* input/output */
344   float   *lastyz;                /* input/output */
345   {
346 <        extern double   sqrt(), fabs();
346 >        extern double   sqrt();
347          double  pos[3];
348          int     lastx = 0;
349          double  lastxz = 0;
# Line 290 | Line 356 | float  *lastyz;                /* input/output */
356                  pos[1] = y - .5*(theirview.vresolu-1);
357                  pos[2] = zline[x];
358                  if (theirview.type == VT_PER) {
359 <                        /*
360 <                         * The following (single) statement can go
295 <                         * if z is along the view direction rather
296 <                         * than an eye ray.
297 <                         */
298 <                        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];
# Line 316 | Line 378 | float  *lastyz;                /* input/output */
378                                          /* add pixel to our image */
379                  zt = 2.*zeps*zline[x];
380                  addpixel(xpos, ypos,
381 <                        (fabs(zline[x]-lastxz) <= zt) ? lastx - xpos : 1,
382 <                        (fabs(zline[x]-lastyz[x]) <= zt) ? lasty[x] - ypos : 1,
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;
# Line 355 | Line 419 | double z;
419   }
420  
421  
422 < fillpicture()                           /* fill in empty spaces */
422 > backpicture()                           /* background fill algorithm */
423   {
424          int     *yback, xback;
425          int     y;
# Line 398 | Line 462 | fillpicture()                          /* fill in empty spaces */
462                                   * Next, find background from left or right.
463                                   */
464                                  if (xback == -2) {
465 <                                        for (i = x+1; x < ourview.hresolu; i++)
465 >                                        for (i = x+1; i < ourview.hresolu; i++)
466                                                  if (zscan(y)[i] > 0)
467                                                          break;
468                                          if (i < ourview.hresolu
# Line 407 | Line 471 | fillpicture()                          /* fill in empty spaces */
471                                          else
472                                                  xback = x-1;
473                                  }
410                                if (xback < 0 && yback[x] < 0)
411                                        continue;       /* no background */
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 || ABS(x-xback) <= 1
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 428 | 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 438 | 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