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.16 by greg, Sat Jan 6 22:27:10 1990 UTC vs.
Revision 1.30 by greg, Mon Oct 15 11:01:48 1990 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include "standard.h"
12  
13 + #include <fcntl.h>
14 +
15   #include "view.h"
16  
17   #include "color.h"
18  
19 < #define pscan(y)        (ourpict+(y)*ourview.hresolu)
20 < #define zscan(y)        (ourzbuf+(y)*ourview.hresolu)
19 > #ifndef BSD
20 > #define vfork           fork
21 > #endif
22  
23 + #define pscan(y)        (ourpict+(y)*hresolu)
24 + #define zscan(y)        (ourzbuf+(y)*hresolu)
25 +
26   #define F_FORE          1               /* fill foreground */
27   #define F_BACK          2               /* fill background */
28  
29   #define PACKSIZ         42              /* calculation packet size */
30  
31 < #define RTCOM           "rtrace -h -ovl -fff -x %d %s"
31 > #define RTCOM           "rtrace -h -ovl -fff %s"
32  
33   #define ABS(x)          ((x)>0?(x):-(x))
34  
35   struct position {int x,y; float z;};
36  
37 < VIEW    ourview = STDVIEW(512);         /* desired view */
37 > VIEW    ourview = STDVIEW;              /* desired view */
38 > int     hresolu = 512;                  /* horizontal resolution */
39 > int     vresolu = 512;                  /* vertical resolution */
40 > double  pixaspect = 1.0;                /* pixel aspect ratio */
41  
42   double  zeps = .02;                     /* allowed z epsilon */
43  
# Line 37 | Line 46 | float  *ourzbuf;                       /* corresponding z-buffer */
46  
47   char    *progname;
48  
49 < int     fill = F_FORE|F_BACK;           /* selected fill algorithm */
49 > int     fillo = F_FORE|F_BACK;          /* selected fill options */
50 > int     fillsamp = 0;                   /* sample separation (0 == inf) */
51   extern int      backfill(), rcalfill(); /* fill functions */
52 < int     (*deffill)() = backfill;        /* selected fill function */
52 > int     (*fillfunc)() = backfill;       /* selected fill function */
53   COLR    backcolr = BLKCOLR;             /* background color */
54   double  backz = 0.0;                    /* background z value */
55   int     normdist = 1;                   /* normalized distance? */
56   double  ourexp = -1;                    /* output picture exposure */
57  
58 < VIEW    theirview = STDVIEW(512);       /* input view */
58 > VIEW    theirview = STDVIEW;            /* input view */
59   int     gotview;                        /* got input view? */
60 < double  theirs2ours[4][4];              /* transformation matrix */
60 > int     thresolu, tvresolu;             /* input resolution */
61   double  theirexp;                       /* input picture exposure */
62 + double  theirs2ours[4][4];              /* transformation matrix */
63 + int     hasmatrix = 0;                  /* has transformation matrix */
64  
65   int     childpid = -1;                  /* id of fill process */
66   FILE    *psend, *precv;                 /* pipes to/from fill calculation */
# Line 65 | Line 77 | char   *argv[];
77          int     gotvfile = 0;
78          char    *zfile = NULL;
79          char    *err;
80 <        int     i;
80 >        int     i, rval;
81  
82          progname = argv[0];
83  
84 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
84 >        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
85 >                rval = getviewopt(&ourview, argc-i, argv+i);
86 >                if (rval >= 0) {
87 >                        i += rval;
88 >                        continue;
89 >                }
90                  switch (argv[i][1]) {
91                  case 't':                               /* threshold */
92                          check(2,1);
# Line 83 | Line 100 | char   *argv[];
100                          switch (argv[i][2]) {
101                          case '0':                               /* none */
102                                  check(3,0);
103 <                                fill = 0;
103 >                                fillo = 0;
104                                  break;
105                          case 'f':                               /* foreground */
106                                  check(3,0);
107 <                                fill = F_FORE;
107 >                                fillo = F_FORE;
108                                  break;
109                          case 'b':                               /* background */
110                                  check(3,0);
111 <                                fill = F_BACK;
111 >                                fillo = F_BACK;
112                                  break;
113                          case 'a':                               /* all */
114                                  check(3,0);
115 <                                fill = F_FORE|F_BACK;
115 >                                fillo = F_FORE|F_BACK;
116                                  break;
117 +                        case 's':                               /* sample */
118 +                                check(3,1);
119 +                                fillsamp = atoi(argv[++i]);
120 +                                break;
121                          case 'c':                               /* color */
122                                  check(3,3);
123 <                                deffill = backfill;
123 >                                fillfunc = backfill;
124                                  setcolr(backcolr, atof(argv[i+1]),
125                                          atof(argv[i+2]), atof(argv[i+3]));
126                                  i += 3;
127                                  break;
128                          case 'z':                               /* z value */
129                                  check(3,1);
130 <                                deffill = backfill;
130 >                                fillfunc = backfill;
131                                  backz = atof(argv[++i]);
132                                  break;
133                          case 'r':                               /* rtrace */
134                                  check(3,1);
135 <                                deffill = rcalfill;
135 >                                fillfunc = rcalfill;
136                                  calstart(RTCOM, argv[++i]);
137                                  break;
138                          default:
# Line 124 | Line 145 | char   *argv[];
145                          break;
146                  case 'x':                               /* x resolution */
147                          check(2,1);
148 <                        ourview.hresolu = atoi(argv[++i]);
148 >                        hresolu = atoi(argv[++i]);
149                          break;
150                  case 'y':                               /* y resolution */
151                          check(2,1);
152 <                        ourview.vresolu = atoi(argv[++i]);
152 >                        vresolu = atoi(argv[++i]);
153                          break;
154 <                case 'v':                               /* view */
155 <                        switch (argv[i][2]) {
156 <                        case 't':                               /* type */
157 <                                check(4,0);
158 <                                ourview.type = argv[i][3];
159 <                                break;
139 <                        case 'p':                               /* point */
140 <                                check(3,3);
141 <                                ourview.vp[0] = atof(argv[++i]);
142 <                                ourview.vp[1] = atof(argv[++i]);
143 <                                ourview.vp[2] = atof(argv[++i]);
144 <                                break;
145 <                        case 'd':                               /* direction */
146 <                                check(3,3);
147 <                                ourview.vdir[0] = atof(argv[++i]);
148 <                                ourview.vdir[1] = atof(argv[++i]);
149 <                                ourview.vdir[2] = atof(argv[++i]);
150 <                                break;
151 <                        case 'u':                               /* up */
152 <                                check(3,3);
153 <                                ourview.vup[0] = atof(argv[++i]);
154 <                                ourview.vup[1] = atof(argv[++i]);
155 <                                ourview.vup[2] = atof(argv[++i]);
156 <                                break;
157 <                        case 'h':                               /* horizontal */
158 <                                check(3,1);
159 <                                ourview.horiz = atof(argv[++i]);
160 <                                break;
161 <                        case 'v':                               /* vertical */
162 <                                check(3,1);
163 <                                ourview.vert = atof(argv[++i]);
164 <                                break;
165 <                        case 'f':                               /* file */
166 <                                check(3,1);
167 <                                gotvfile = viewfile(argv[++i], &ourview);
168 <                                if (gotvfile < 0) {
169 <                                        perror(argv[i]);
170 <                                        exit(1);
171 <                                } else if (gotvfile == 0) {
172 <                                        fprintf(stderr, "%s: bad view file\n",
173 <                                                        argv[i]);
174 <                                        exit(1);
175 <                                }
176 <                                break;
177 <                        default:
154 >                case 'p':                               /* pixel aspect */
155 >                        check(2,1);
156 >                        pixaspect = atof(argv[++i]);
157 >                        break;
158 >                case 'v':                               /* view file */
159 >                        if (argv[i][2] != 'f')
160                                  goto badopt;
161 +                        check(3,1);
162 +                        gotvfile = viewfile(argv[++i], &ourview, 0, 0);
163 +                        if (gotvfile < 0) {
164 +                                perror(argv[i]);
165 +                                exit(1);
166 +                        } else if (gotvfile == 0) {
167 +                                fprintf(stderr, "%s: bad view file\n",
168 +                                                argv[i]);
169 +                                exit(1);
170                          }
171                          break;
172                  default:
# Line 184 | Line 175 | char   *argv[];
175                                          progname, argv[i]);
176                          goto userr;
177                  }
178 +        }
179                                                  /* check arguments */
180          if ((argc-i)%2)
181                  goto userr;
# Line 192 | Line 184 | char   *argv[];
184                  fprintf(stderr, "%s: %s\n", progname, err);
185                  exit(1);
186          }
187 +        normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
188                                                  /* allocate frame */
189 <        ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR));
190 <        ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float));
189 >        ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR));
190 >        ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float));
191          if (ourpict == NULL || ourzbuf == NULL)
192                  syserror();
193                                                          /* get input */
194          for ( ; i < argc; i += 2)
195                  addpicture(argv[i], argv[i+1]);
196                                                          /* fill in spaces */
197 <        if (fill&F_BACK)
198 <                backpicture();
197 >        if (fillo&F_BACK)
198 >                backpicture(fillfunc, fillsamp);
199          else
200 <                fillpicture();
200 >                fillpicture(fillfunc);
201                                                          /* close calculation */
202          caldone();
203                                                          /* add to header */
204          printargs(argc, argv, stdout);
205          if (gotvfile) {
206 <                printf(VIEWSTR);
206 >                fputs(VIEWSTR, stdout);
207                  fprintview(&ourview, stdout);
208 <                printf("\n");
208 >                putc('\n', stdout);
209          }
210 <        if (ourexp > 0 && ourexp != 1.0)
210 >        if (pixaspect < .99 || pixaspect > 1.01)
211 >                fputaspect(pixaspect, stdout);
212 >        if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
213                  fputexpos(ourexp, stdout);
214 <        printf("\n");
214 >        putc('\n', stdout);
215                                                          /* write picture */
216          writepicture();
217                                                          /* write z file */
# Line 226 | Line 221 | char   *argv[];
221          exit(0);
222   userr:
223          fprintf(stderr,
224 <        "Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n",
224 >        "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n",
225                          progname);
226          exit(1);
227   #undef check
# Line 236 | Line 231 | userr:
231   headline(s)                             /* process header string */
232   char    *s;
233   {
234 <        static char     *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
234 >        static char     *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL};
235          register char   **an;
236  
237 <        printf("\t%s", s);
237 >        putc('\t', stdout);
238 >        fputs(s, stdout);
239  
240          if (isexpos(s)) {
241                  theirexp *= exposval(s);
# Line 247 | Line 243 | char   *s;
243          }
244          for (an = altname; *an != NULL; an++)
245                  if (!strncmp(*an, s, strlen(*an))) {
246 <                        if (sscanview(&theirview, s+strlen(*an)) == 0)
246 >                        if (sscanview(&theirview, s+strlen(*an)) > 0)
247                                  gotview++;
248                          break;
249                  }
# Line 258 | Line 254 | addpicture(pfile, zspec)               /* add picture to output */
254   char    *pfile, *zspec;
255   {
256          extern double   atof();
257 <        FILE    *pfp, *zfp;
257 >        FILE    *pfp;
258 >        int     zfd;
259          char    *err;
260          COLR    *scanin;
261          float   *zin;
# Line 274 | Line 271 | char   *pfile, *zspec;
271          gotview = 0;
272          printf("%s:\n", pfile);
273          getheader(pfp, headline);
274 <        if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp)
274 >        if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp)
275                          != (YMAJOR|YDECR)) {
276                  fprintf(stderr, "%s: picture view error\n", pfile);
277                  exit(1);
# Line 288 | Line 285 | char   *pfile, *zspec;
285                  exit(1);
286          }
287                                          /* compute transformation */
288 <        pixform(theirs2ours, &theirview, &ourview);
288 >        hasmatrix = pixform(theirs2ours, &theirview, &ourview);
289                                          /* allocate scanlines */
290 <        scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR));
291 <        zin = (float *)malloc(theirview.hresolu*sizeof(float));
292 <        plast = (struct position *)calloc(theirview.hresolu,
296 <                                                sizeof(struct position));
290 >        scanin = (COLR *)malloc(thresolu*sizeof(COLR));
291 >        zin = (float *)malloc(thresolu*sizeof(float));
292 >        plast = (struct position *)calloc(thresolu, sizeof(struct position));
293          if (scanin == NULL || zin == NULL || plast == NULL)
294                  syserror();
295                                          /* get z specification or file */
296 <        if ((zfp = fopen(zspec, "r")) == NULL) {
296 >        if ((zfd = open(zspec, O_RDONLY)) == -1) {
297                  double  zvalue;
298                  register int    x;
299                  if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
300                          perror(zspec);
301                          exit(1);
302                  }
303 <                for (x = 0; x < theirview.hresolu; x++)
303 >                for (x = 0; x < thresolu; x++)
304                          zin[x] = zvalue;
305          }
306                                          /* load image */
307 <        for (y = theirview.vresolu-1; y >= 0; y--) {
308 <                if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) {
307 >        for (y = tvresolu-1; y >= 0; y--) {
308 >                if (freadcolrs(scanin, thresolu, pfp) < 0) {
309                          fprintf(stderr, "%s: read error\n", pfile);
310                          exit(1);
311                  }
312 <                if (zfp != NULL
313 <                        && fread(zin,sizeof(float),theirview.hresolu,zfp)
318 <                                < theirview.hresolu) {
312 >                if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float))
313 >                                < thresolu*sizeof(float)) {
314                          fprintf(stderr, "%s: read error\n", zspec);
315                          exit(1);
316                  }
# Line 326 | Line 321 | char   *pfile, *zspec;
321          free((char *)zin);
322          free((char *)plast);
323          fclose(pfp);
324 <        if (zfp != NULL)
325 <                fclose(zfp);
324 >        if (zfd != -1)
325 >                close(zfd);
326   }
327  
328  
# Line 337 | Line 332 | register VIEW  *vw1, *vw2;
332   {
333          double  m4t[4][4];
334  
335 +        if (vw1->type != VT_PER && vw1->type != VT_PAR)
336 +                return(0);
337 +        if (vw2->type != VT_PER && vw2->type != VT_PAR)
338 +                return(0);
339          setident4(xfmat);
340 <        xfmat[0][0] = vw1->vhinc[0];
341 <        xfmat[0][1] = vw1->vhinc[1];
342 <        xfmat[0][2] = vw1->vhinc[2];
343 <        xfmat[1][0] = vw1->vvinc[0];
344 <        xfmat[1][1] = vw1->vvinc[1];
345 <        xfmat[1][2] = vw1->vvinc[2];
340 >        xfmat[0][0] = vw1->hvec[0];
341 >        xfmat[0][1] = vw1->hvec[1];
342 >        xfmat[0][2] = vw1->hvec[2];
343 >        xfmat[1][0] = vw1->vvec[0];
344 >        xfmat[1][1] = vw1->vvec[1];
345 >        xfmat[1][2] = vw1->vvec[2];
346          xfmat[2][0] = vw1->vdir[0];
347          xfmat[2][1] = vw1->vdir[1];
348          xfmat[2][2] = vw1->vdir[2];
# Line 351 | Line 350 | register VIEW  *vw1, *vw2;
350          xfmat[3][1] = vw1->vp[1];
351          xfmat[3][2] = vw1->vp[2];
352          setident4(m4t);
353 <        m4t[0][0] = vw2->vhinc[0]/vw2->vhn2;
354 <        m4t[1][0] = vw2->vhinc[1]/vw2->vhn2;
355 <        m4t[2][0] = vw2->vhinc[2]/vw2->vhn2;
356 <        m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2;
357 <        m4t[0][1] = vw2->vvinc[0]/vw2->vvn2;
358 <        m4t[1][1] = vw2->vvinc[1]/vw2->vvn2;
359 <        m4t[2][1] = vw2->vvinc[2]/vw2->vvn2;
360 <        m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2;
353 >        m4t[0][0] = vw2->hvec[0]/vw2->hn2;
354 >        m4t[1][0] = vw2->hvec[1]/vw2->hn2;
355 >        m4t[2][0] = vw2->hvec[2]/vw2->hn2;
356 >        m4t[3][0] = -DOT(vw2->vp,vw2->hvec)/vw2->hn2;
357 >        m4t[0][1] = vw2->vvec[0]/vw2->vn2;
358 >        m4t[1][1] = vw2->vvec[1]/vw2->vn2;
359 >        m4t[2][1] = vw2->vvec[2]/vw2->vn2;
360 >        m4t[3][1] = -DOT(vw2->vp,vw2->vvec)/vw2->vn2;
361          m4t[0][2] = vw2->vdir[0];
362          m4t[1][2] = vw2->vdir[1];
363          m4t[2][2] = vw2->vdir[2];
364          m4t[3][2] = -DOT(vw2->vp,vw2->vdir);
365          multmat4(xfmat, xfmat, m4t);
366 +        return(1);
367   }
368  
369  
# Line 374 | Line 374 | float  *zline;
374   struct position *lasty;         /* input/output */
375   {
376          extern double   sqrt();
377 <        double  pos[3];
377 >        FVECT   pos;
378          struct position lastx, newpos;
379          register int    x;
380  
381 <        for (x = theirview.hresolu-1; x >= 0; x--) {
382 <                pos[0] = x - .5*(theirview.hresolu-1);
383 <                pos[1] = y - .5*(theirview.vresolu-1);
381 >        lastx.z = 0;
382 >        for (x = thresolu-1; x >= 0; x--) {
383 >                pos[0] = (x+.5)/thresolu;
384 >                pos[1] = (y+.5)/tvresolu;
385                  pos[2] = zline[x];
386 <                if (theirview.type == VT_PER) {
386 <                        if (normdist)   /* adjust for eye-ray distance */
387 <                                pos[2] /= sqrt( 1.
388 <                                        + pos[0]*pos[0]*theirview.vhn2
389 <                                        + pos[1]*pos[1]*theirview.vvn2 );
390 <                        pos[0] *= pos[2];
391 <                        pos[1] *= pos[2];
392 <                }
393 <                multp3(pos, pos, theirs2ours);
394 <                if (pos[2] <= 0) {
386 >                if (movepixel(pos) < 0) {
387                          lasty[x].z = lastx.z = 0;       /* mark invalid */
388                          continue;
389                  }
390 <                if (ourview.type == VT_PER) {
391 <                        pos[0] /= pos[2];
400 <                        pos[1] /= pos[2];
401 <                }
402 <                pos[0] += .5*ourview.hresolu;
403 <                pos[1] += .5*ourview.vresolu;
404 <                newpos.x = pos[0];
405 <                newpos.y = pos[1];
390 >                newpos.x = pos[0] * hresolu;
391 >                newpos.y = pos[1] * vresolu;
392                  newpos.z = zline[x];
393                                          /* add pixel to our image */
394 <                if (pos[0] >= 0 && newpos.x < ourview.hresolu
395 <                                && pos[1] >= 0 && newpos.y < ourview.vresolu) {
394 >                if (pos[0] >= 0 && newpos.x < hresolu
395 >                                && pos[1] >= 0 && newpos.y < vresolu) {
396                          addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
397                          lasty[x].x = lastx.x = newpos.x;
398                          lasty[x].y = lastx.y = newpos.y;
# Line 430 | Line 416 | double z;
416          register int    x, y;                   /* final position */
417  
418                                          /* compute vector p0p1 */
419 <        if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) {
419 >        if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
420                  s1x = p1->x - p0->x;
421                  s1y = p1->y - p0->y;
422                  l1 = ABS(s1x);
# Line 441 | Line 427 | double z;
427                  p1isy = -1;
428          }
429                                          /* compute vector p0p2 */
430 <        if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) {
430 >        if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
431                  s2x = p2->x - p0->x;
432                  s2y = p2->y - p0->y;
433                  if (p1isy == 1)
# Line 459 | Line 445 | double z;
445                  y1 = p0->y + c1*s1y/l1;
446                  for (c2 = l2; c2-- > 0; ) {
447                          x = x1 + c2*s2x/l2;
448 +                        if (x < 0 || x >= hresolu)
449 +                                continue;
450                          y = y1 + c2*s2y/l2;
451 +                        if (y < 0 || y >= vresolu)
452 +                                continue;
453                          if (zscan(y)[x] <= 0 || zscan(y)[x]-z
454                                                  > zeps*zscan(y)[x]) {
455                                  zscan(y)[x] = z;
# Line 470 | Line 460 | double z;
460   }
461  
462  
463 < backpicture()                           /* background fill algorithm */
463 > movepixel(pos)                          /* reposition image point */
464 > FVECT   pos;
465   {
466 +        FVECT   pt, direc;
467 +        
468 +        if (hasmatrix) {
469 +                pos[0] += theirview.hoff - .5;
470 +                pos[1] += theirview.voff - .5;
471 +                if (theirview.type == VT_PER) {
472 +                        if (normdist)   /* adjust for eye-ray distance */
473 +                                pos[2] /= sqrt( 1.
474 +                                        + pos[0]*pos[0]*theirview.hn2
475 +                                        + pos[1]*pos[1]*theirview.vn2 );
476 +                        pos[0] *= pos[2];
477 +                        pos[1] *= pos[2];
478 +                }
479 +                multp3(pos, pos, theirs2ours);
480 +                if (pos[2] <= 0)
481 +                        return(-1);
482 +                if (ourview.type == VT_PER) {
483 +                        pos[0] /= pos[2];
484 +                        pos[1] /= pos[2];
485 +                }
486 +                pos[0] += .5 - ourview.hoff;
487 +                pos[1] += .5 - ourview.voff;
488 +                return(0);
489 +        }
490 +        if (viewray(pt, direc, &theirview, pos[0], pos[1]) < 0)
491 +                return(-1);
492 +        pt[0] += direc[0]*pos[2];
493 +        pt[1] += direc[1]*pos[2];
494 +        pt[2] += direc[2]*pos[2];
495 +        viewpixel(&pos[0], &pos[1], &pos[2], &ourview, pt);
496 +        if (pos[2] <= 0)
497 +                return(-1);
498 +        return(0);
499 + }
500 +
501 +
502 + backpicture(fill, samp)                 /* background fill algorithm */
503 + int     (*fill)();
504 + int     samp;
505 + {
506          int     *yback, xback;
507          int     y;
508          COLR    pfill;
509          register int    x, i;
510                                                          /* get back buffer */
511 <        yback = (int *)malloc(ourview.hresolu*sizeof(int));
511 >        yback = (int *)malloc(hresolu*sizeof(int));
512          if (yback == NULL)
513                  syserror();
514 <        for (x = 0; x < ourview.hresolu; x++)
514 >        for (x = 0; x < hresolu; x++)
515                  yback[x] = -2;
516          /*
517           * Xback and yback are the pixel locations of suitable
# Line 489 | Line 520 | backpicture()                          /* background fill algorithm */
520           * that there is no suitable background in this direction.
521           */
522                                                          /* fill image */
523 <        for (y = 0; y < ourview.vresolu; y++) {
523 >        for (y = 0; y < vresolu; y++) {
524                  xback = -2;
525 <                for (x = 0; x < ourview.hresolu; x++)
525 >                for (x = 0; x < hresolu; x++)
526                          if (zscan(y)[x] <= 0) {         /* empty pixel */
527                                  /*
528                                   * First, find background from above or below.
529                                   * (farthest assigned pixel)
530                                   */
531                                  if (yback[x] == -2) {
532 <                                        for (i = y+1; i < ourview.vresolu; i++)
532 >                                        for (i = y+1; i < vresolu; i++)
533                                                  if (zscan(i)[x] > 0)
534                                                          break;
535 <                                        if (i < ourview.vresolu
535 >                                        if (i < vresolu
536                                  && (y <= 0 || zscan(y-1)[x] < zscan(i)[x]))
537                                                  yback[x] = i;
538                                          else
# Line 511 | Line 542 | backpicture()                          /* background fill algorithm */
542                                   * Next, find background from left or right.
543                                   */
544                                  if (xback == -2) {
545 <                                        for (i = x+1; i < ourview.hresolu; i++)
545 >                                        for (i = x+1; i < hresolu; i++)
546                                                  if (zscan(y)[i] > 0)
547                                                          break;
548 <                                        if (i < ourview.hresolu
548 >                                        if (i < hresolu
549                                  && (x <= 0 || zscan(y)[x-1] < zscan(y)[i]))
550                                                  xback = i;
551                                          else
552                                                  xback = x-1;
553                                  }
554                                  /*
555 <                                 * Check to see if we have no background for
556 <                                 * this pixel.  If not, use background color.
555 >                                 * If we have no background for this pixel,
556 >                                 * use the given fill function.
557                                   */
558 <                                if (xback < 0 && yback[x] < 0) {
559 <                                        (*deffill)(x,y);
529 <                                        continue;
530 <                                }
558 >                                if (xback < 0 && yback[x] < 0)
559 >                                        goto fillit;
560                                  /*
561                                   * Compare, and use the background that is
562                                   * farther, unless one of them is next to us.
563 +                                 * If the background is too distant, call
564 +                                 * the fill function.
565                                   */
566                                  if ( yback[x] < 0
567                                          || (xback >= 0 && ABS(x-xback) <= 1)
568                                          || ( ABS(y-yback[x]) > 1
569                                                  && zscan(yback[x])[x]
570                                                  < zscan(y)[xback] ) ) {
571 +                                        if (samp > 0 && ABS(x-xback) >= samp)
572 +                                                goto fillit;
573                                          copycolr(pscan(y)[x],pscan(y)[xback]);
574                                          zscan(y)[x] = zscan(y)[xback];
575                                  } else {
576 +                                        if (samp > 0 && ABS(y-yback[x]) > samp)
577 +                                                goto fillit;
578                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
579                                          zscan(y)[x] = zscan(yback[x])[x];
580                                  }
581 +                                continue;
582 +                        fillit:
583 +                                (*fill)(x,y);
584 +                                if (fill == rcalfill) {         /* use it */
585 +                                        clearqueue();
586 +                                        xback = x;
587 +                                        yback[x] = y;
588 +                                }
589                          } else {                                /* full pixel */
590                                  yback[x] = -2;
591                                  xback = -2;
# Line 552 | Line 595 | backpicture()                          /* background fill algorithm */
595   }
596  
597  
598 < fillpicture()                           /* paint in empty pixels with default */
598 > fillpicture(fill)               /* paint in empty pixels using fill */
599 > int     (*fill)();
600   {
601          register int    x, y;
602  
603 <        for (y = 0; y < ourview.vresolu; y++)
604 <                for (x = 0; x < ourview.hresolu; x++)
603 >        for (y = 0; y < vresolu; y++)
604 >                for (x = 0; x < hresolu; x++)
605                          if (zscan(y)[x] <= 0)
606 <                                (*deffill)(x,y);
606 >                                (*fill)(x,y);
607   }
608  
609  
# Line 567 | Line 611 | writepicture()                         /* write out picture */
611   {
612          int     y;
613  
614 <        fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
615 <        for (y = ourview.vresolu-1; y >= 0; y--)
616 <                if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0)
614 >        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
615 >        for (y = vresolu-1; y >= 0; y--)
616 >                if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
617                          syserror();
618   }
619  
# Line 579 | Line 623 | char   *fname;
623   {
624          extern double   sqrt();
625          int     donorm = normdist && ourview.type == VT_PER;
626 <        FILE    *fp;
626 >        int     fd;
627          int     y;
628          float   *zout;
629  
630 <        if ((fp = fopen(fname, "w")) == NULL) {
630 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
631                  perror(fname);
632                  exit(1);
633          }
634          if (donorm
635 <        && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL)
635 >        && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
636                  syserror();
637 <        for (y = ourview.vresolu-1; y >= 0; y--) {
637 >        for (y = vresolu-1; y >= 0; y--) {
638                  if (donorm) {
639                          double  vx, yzn2;
640                          register int    x;
641 <                        yzn2 = y - .5*(ourview.vresolu-1);
642 <                        yzn2 = 1. + yzn2*yzn2*ourview.vvn2;
643 <                        for (x = 0; x < ourview.hresolu; x++) {
644 <                                vx = x - .5*(ourview.hresolu-1);
641 >                        yzn2 = (y+.5)/vresolu + ourview.voff - .5;
642 >                        yzn2 = 1. + yzn2*yzn2*ourview.vn2;
643 >                        for (x = 0; x < hresolu; x++) {
644 >                                vx = (x+.5)/hresolu + ourview.hoff - .5;
645                                  zout[x] = zscan(y)[x]
646 <                                        * sqrt(vx*vx*ourview.vhn2 + yzn2);
646 >                                        * sqrt(vx*vx*ourview.hn2 + yzn2);
647                          }
648                  } else
649                          zout = zscan(y);
650 <                if (fwrite(zout, sizeof(float), ourview.hresolu, fp)
651 <                                < ourview.hresolu) {
650 >                if (write(fd, (char *)zout, hresolu*sizeof(float))
651 >                                < hresolu*sizeof(float)) {
652                          perror(fname);
653                          exit(1);
654                  }
655          }
656          if (donorm)
657                  free((char *)zout);
658 <        fclose(fp);
658 >        close(fd);
659   }
660  
661  
# Line 646 | Line 690 | char   *prog, *args;
690                  fprintf(stderr, "%s: too many calculations\n", progname);
691                  exit(1);
692          }
693 <        sprintf(combuf, prog, PACKSIZ, args);
693 >        sprintf(combuf, prog, args);
694          if (pipe(p0) < 0 || pipe(p1) < 0)
695                  syserror();
696          if ((childpid = vfork()) == 0) {        /* fork calculation */
# Line 682 | Line 726 | caldone()                              /* done with calculation */
726  
727          if (childpid == -1)
728                  return;
685        if (fclose(psend) == EOF)
686                syserror();
729          clearqueue();
730 +        fclose(psend);
731          fclose(precv);
732          while ((pid = wait(0)) != -1 && pid != childpid)
733                  ;
# Line 695 | Line 738 | caldone()                              /* done with calculation */
738   rcalfill(x, y)                          /* fill with ray-calculated pixel */
739   int     x, y;
740   {
741 <        FVECT   orig, dir;
699 <        float   outbuf[6];
700 <
701 <        if (queuesiz >= PACKSIZ) {      /* flush queue */
702 <                if (fflush(psend) == EOF)
703 <                        syserror();
741 >        if (queuesiz >= PACKSIZ)        /* flush queue if needed */
742                  clearqueue();
743 <        }
706 <                                        /* send new ray */
707 <        rayview(orig, dir, &ourview, x+.5, y+.5);
708 <        outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
709 <        outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
710 <        if (fwrite(outbuf, sizeof(float), 6, psend) < 6)
711 <                syserror();
712 <                                        /* remember it */
743 >                                        /* add position to queue */
744          queue[queuesiz][0] = x;
745          queue[queuesiz][1] = y;
746          queuesiz++;
747   }
748  
749  
750 < clearqueue()                            /* get results from queue */
750 > clearqueue()                            /* process queue */
751   {
752 <        float   inbuf[4];
752 >        FVECT   orig, dir;
753 >        float   fbuf[6];
754          register int    i;
755  
756          for (i = 0; i < queuesiz; i++) {
757 <                if (fread(inbuf, sizeof(float), 4, precv) < 4) {
757 >                viewray(orig, dir, &ourview,
758 >                                (queue[i][0]+.5)/hresolu,
759 >                                (queue[i][1]+.5)/vresolu);
760 >                fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2];
761 >                fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
762 >                fwrite((char *)fbuf, sizeof(float), 6, psend);
763 >        }
764 >                                        /* flush output and get results */
765 >        fbuf[3] = fbuf[4] = fbuf[5] = 0.0;              /* mark */
766 >        fwrite((char *)fbuf, sizeof(float), 6, psend);
767 >        if (fflush(psend) == EOF)
768 >                syserror();
769 >        for (i = 0; i < queuesiz; i++) {
770 >                if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) {
771                          fprintf(stderr, "%s: read error in clearqueue\n",
772                                          progname);
773                          exit(1);
774                  }
775                  if (ourexp > 0 && ourexp != 1.0) {
776 <                        inbuf[0] *= ourexp;
777 <                        inbuf[1] *= ourexp;
778 <                        inbuf[2] *= ourexp;
776 >                        fbuf[0] *= ourexp;
777 >                        fbuf[1] *= ourexp;
778 >                        fbuf[2] *= ourexp;
779                  }
780                  setcolr(pscan(queue[i][1])[queue[i][0]],
781 <                                inbuf[0], inbuf[1], inbuf[2]);
782 <                zscan(queue[i][1])[queue[i][0]] = inbuf[3];
781 >                                fbuf[0], fbuf[1], fbuf[2]);
782 >                zscan(queue[i][1])[queue[i][0]] = fbuf[3];
783          }
784          queuesiz = 0;
785   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines