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.15 by greg, Fri Jan 5 11:34:27 1990 UTC vs.
Revision 1.27 by greg, Tue Mar 6 15:45:31 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 < VIEW    theirview = STDVIEW(512);       /* input view */
50 < int     gotview;                        /* got input view? */
42 <
43 < 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;            /* input view */
59 + int     gotview;                        /* got input view? */
60 + int     thresolu, tvresolu;             /* input resolution */
61 + double  theirexp;                       /* input picture exposure */
62   double  theirs2ours[4][4];              /* transformation matrix */
50 int     normdist = 1;                   /* normalized distance? */
63  
64   int     childpid = -1;                  /* id of fill process */
65   FILE    *psend, *precv;                 /* pipes to/from fill calculation */
# Line 64 | Line 76 | char   *argv[];
76          int     gotvfile = 0;
77          char    *zfile = NULL;
78          char    *err;
79 <        int     i;
79 >        int     i, rval;
80  
81          progname = argv[0];
82  
83 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
83 >        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
84 >                rval = getviewopt(&ourview, argc-i, argv+i);
85 >                if (rval >= 0) {
86 >                        i += rval;
87 >                        continue;
88 >                }
89                  switch (argv[i][1]) {
90                  case 't':                               /* threshold */
91                          check(2,1);
# Line 82 | Line 99 | char   *argv[];
99                          switch (argv[i][2]) {
100                          case '0':                               /* none */
101                                  check(3,0);
102 <                                fill = 0;
102 >                                fillo = 0;
103                                  break;
104                          case 'f':                               /* foreground */
105                                  check(3,0);
106 <                                fill = F_FORE;
106 >                                fillo = F_FORE;
107                                  break;
108                          case 'b':                               /* background */
109                                  check(3,0);
110 <                                fill = F_BACK;
110 >                                fillo = F_BACK;
111                                  break;
112                          case 'a':                               /* all */
113                                  check(3,0);
114 <                                fill = F_FORE|F_BACK;
114 >                                fillo = F_FORE|F_BACK;
115                                  break;
116 +                        case 's':                               /* sample */
117 +                                check(3,1);
118 +                                fillsamp = atoi(argv[++i]);
119 +                                break;
120                          case 'c':                               /* color */
121                                  check(3,3);
122 <                                deffill = backfill;
122 >                                fillfunc = backfill;
123                                  setcolr(backcolr, atof(argv[i+1]),
124                                          atof(argv[i+2]), atof(argv[i+3]));
125                                  i += 3;
126                                  break;
127                          case 'z':                               /* z value */
128                                  check(3,1);
129 <                                deffill = backfill;
129 >                                fillfunc = backfill;
130                                  backz = atof(argv[++i]);
131                                  break;
132                          case 'r':                               /* rtrace */
133                                  check(3,1);
134 <                                deffill = rcalfill;
134 >                                fillfunc = rcalfill;
135                                  calstart(RTCOM, argv[++i]);
136                                  break;
137                          default:
# Line 123 | Line 144 | char   *argv[];
144                          break;
145                  case 'x':                               /* x resolution */
146                          check(2,1);
147 <                        ourview.hresolu = atoi(argv[++i]);
147 >                        hresolu = atoi(argv[++i]);
148                          break;
149                  case 'y':                               /* y resolution */
150                          check(2,1);
151 <                        ourview.vresolu = atoi(argv[++i]);
151 >                        vresolu = atoi(argv[++i]);
152                          break;
153 <                case 'v':                               /* view */
154 <                        switch (argv[i][2]) {
155 <                        case 't':                               /* type */
156 <                                check(4,0);
157 <                                ourview.type = argv[i][3];
158 <                                break;
138 <                        case 'p':                               /* point */
139 <                                check(3,3);
140 <                                ourview.vp[0] = atof(argv[++i]);
141 <                                ourview.vp[1] = atof(argv[++i]);
142 <                                ourview.vp[2] = atof(argv[++i]);
143 <                                break;
144 <                        case 'd':                               /* direction */
145 <                                check(3,3);
146 <                                ourview.vdir[0] = atof(argv[++i]);
147 <                                ourview.vdir[1] = atof(argv[++i]);
148 <                                ourview.vdir[2] = atof(argv[++i]);
149 <                                break;
150 <                        case 'u':                               /* up */
151 <                                check(3,3);
152 <                                ourview.vup[0] = atof(argv[++i]);
153 <                                ourview.vup[1] = atof(argv[++i]);
154 <                                ourview.vup[2] = atof(argv[++i]);
155 <                                break;
156 <                        case 'h':                               /* horizontal */
157 <                                check(3,1);
158 <                                ourview.horiz = atof(argv[++i]);
159 <                                break;
160 <                        case 'v':                               /* vertical */
161 <                                check(3,1);
162 <                                ourview.vert = atof(argv[++i]);
163 <                                break;
164 <                        case 'f':                               /* file */
165 <                                check(3,1);
166 <                                gotvfile = viewfile(argv[++i], &ourview);
167 <                                if (gotvfile < 0) {
168 <                                        perror(argv[i]);
169 <                                        exit(1);
170 <                                } else if (gotvfile == 0) {
171 <                                        fprintf(stderr, "%s: bad view file\n",
172 <                                                        argv[i]);
173 <                                        exit(1);
174 <                                }
175 <                                break;
176 <                        default:
153 >                case 'p':                               /* pixel aspect */
154 >                        check(2,1);
155 >                        pixaspect = atof(argv[++i]);
156 >                        break;
157 >                case 'v':                               /* view file */
158 >                        if (argv[i][2] != 'f')
159                                  goto badopt;
160 +                        check(3,1);
161 +                        gotvfile = viewfile(argv[++i], &ourview, 0, 0);
162 +                        if (gotvfile < 0) {
163 +                                perror(argv[i]);
164 +                                exit(1);
165 +                        } else if (gotvfile == 0) {
166 +                                fprintf(stderr, "%s: bad view file\n",
167 +                                                argv[i]);
168 +                                exit(1);
169                          }
170                          break;
171                  default:
# Line 183 | Line 174 | char   *argv[];
174                                          progname, argv[i]);
175                          goto userr;
176                  }
177 +        }
178                                                  /* check arguments */
179          if ((argc-i)%2)
180                  goto userr;
# Line 191 | Line 183 | char   *argv[];
183                  fprintf(stderr, "%s: %s\n", progname, err);
184                  exit(1);
185          }
186 +        normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
187                                                  /* allocate frame */
188 <        ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR));
189 <        ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float));
188 >        ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR));
189 >        ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float));
190          if (ourpict == NULL || ourzbuf == NULL)
191                  syserror();
192                                                          /* get input */
193          for ( ; i < argc; i += 2)
194                  addpicture(argv[i], argv[i+1]);
195                                                          /* fill in spaces */
196 <        if (fill&F_BACK)
197 <                backpicture();
196 >        if (fillo&F_BACK)
197 >                backpicture(fillfunc, fillsamp);
198          else
199 <                fillpicture();
199 >                fillpicture(fillfunc);
200                                                          /* close calculation */
201          caldone();
202                                                          /* add to header */
203          printargs(argc, argv, stdout);
204          if (gotvfile) {
205 <                printf(VIEWSTR);
205 >                fputs(VIEWSTR, stdout);
206                  fprintview(&ourview, stdout);
207 <                printf("\n");
207 >                putc('\n', stdout);
208          }
209 <        printf("\n");
209 >        if (pixaspect < .99 || pixaspect > 1.01)
210 >                fputaspect(pixaspect, stdout);
211 >        if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
212 >                fputexpos(ourexp, stdout);
213 >        putc('\n', stdout);
214                                                          /* write picture */
215          writepicture();
216                                                          /* write z file */
# Line 223 | Line 220 | char   *argv[];
220          exit(0);
221   userr:
222          fprintf(stderr,
223 <        "Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n",
223 >        "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n",
224                          progname);
225          exit(1);
226   #undef check
# Line 233 | Line 230 | userr:
230   headline(s)                             /* process header string */
231   char    *s;
232   {
233 <        static char     *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
233 >        static char     *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL};
234          register char   **an;
235  
236 <        printf("\t%s", s);
236 >        putc('\t', stdout);
237 >        fputs(s, stdout);
238  
239 +        if (isexpos(s)) {
240 +                theirexp *= exposval(s);
241 +                return;
242 +        }
243          for (an = altname; *an != NULL; an++)
244                  if (!strncmp(*an, s, strlen(*an))) {
245 <                        if (sscanview(&theirview, s+strlen(*an)) == 0)
245 >                        if (sscanview(&theirview, s+strlen(*an)) > 0)
246                                  gotview++;
247                          break;
248                  }
# Line 251 | Line 253 | addpicture(pfile, zspec)               /* add picture to output */
253   char    *pfile, *zspec;
254   {
255          extern double   atof();
256 <        FILE    *pfp, *zfp;
256 >        FILE    *pfp;
257 >        int     zfd;
258          char    *err;
259          COLR    *scanin;
260          float   *zin;
# Line 262 | Line 265 | char   *pfile, *zspec;
265                  perror(pfile);
266                  exit(1);
267          }
268 <                                        /* get header and view */
269 <        printf("%s:\n", pfile);
268 >                                        /* get header with exposure and view */
269 >        theirexp = 1.0;
270          gotview = 0;
271 +        printf("%s:\n", pfile);
272          getheader(pfp, headline);
273 <        if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp)
273 >        if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp)
274                          != (YMAJOR|YDECR)) {
275                  fprintf(stderr, "%s: picture view error\n", pfile);
276                  exit(1);
277          }
278 +        if (ourexp <= 0)
279 +                ourexp = theirexp;
280 +        else if (ABS(theirexp-ourexp) > .01*ourexp)
281 +                fprintf(stderr, "%s: different exposure (warning)\n", pfile);
282          if (err = setview(&theirview)) {
283                  fprintf(stderr, "%s: %s\n", pfile, err);
284                  exit(1);
# Line 278 | Line 286 | char   *pfile, *zspec;
286                                          /* compute transformation */
287          pixform(theirs2ours, &theirview, &ourview);
288                                          /* allocate scanlines */
289 <        scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR));
290 <        zin = (float *)malloc(theirview.hresolu*sizeof(float));
291 <        plast = (struct position *)calloc(theirview.hresolu,
284 <                                                sizeof(struct position));
289 >        scanin = (COLR *)malloc(thresolu*sizeof(COLR));
290 >        zin = (float *)malloc(thresolu*sizeof(float));
291 >        plast = (struct position *)calloc(thresolu, sizeof(struct position));
292          if (scanin == NULL || zin == NULL || plast == NULL)
293                  syserror();
294                                          /* get z specification or file */
295 <        if ((zfp = fopen(zspec, "r")) == NULL) {
295 >        if ((zfd = open(zspec, O_RDONLY)) == -1) {
296                  double  zvalue;
297                  register int    x;
298                  if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
299                          perror(zspec);
300                          exit(1);
301                  }
302 <                for (x = 0; x < theirview.hresolu; x++)
302 >                for (x = 0; x < thresolu; x++)
303                          zin[x] = zvalue;
304          }
305                                          /* load image */
306 <        for (y = theirview.vresolu-1; y >= 0; y--) {
307 <                if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) {
306 >        for (y = tvresolu-1; y >= 0; y--) {
307 >                if (freadcolrs(scanin, thresolu, pfp) < 0) {
308                          fprintf(stderr, "%s: read error\n", pfile);
309                          exit(1);
310                  }
311 <                if (zfp != NULL
312 <                        && fread(zin,sizeof(float),theirview.hresolu,zfp)
306 <                                < theirview.hresolu) {
311 >                if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float))
312 >                                < thresolu*sizeof(float)) {
313                          fprintf(stderr, "%s: read error\n", zspec);
314                          exit(1);
315                  }
# Line 314 | Line 320 | char   *pfile, *zspec;
320          free((char *)zin);
321          free((char *)plast);
322          fclose(pfp);
323 <        if (zfp != NULL)
324 <                fclose(zfp);
323 >        if (zfd != -1)
324 >                close(zfd);
325   }
326  
327  
# Line 326 | Line 332 | register VIEW  *vw1, *vw2;
332          double  m4t[4][4];
333  
334          setident4(xfmat);
335 <        xfmat[0][0] = vw1->vhinc[0];
336 <        xfmat[0][1] = vw1->vhinc[1];
337 <        xfmat[0][2] = vw1->vhinc[2];
338 <        xfmat[1][0] = vw1->vvinc[0];
339 <        xfmat[1][1] = vw1->vvinc[1];
340 <        xfmat[1][2] = vw1->vvinc[2];
335 >        xfmat[0][0] = vw1->hvec[0];
336 >        xfmat[0][1] = vw1->hvec[1];
337 >        xfmat[0][2] = vw1->hvec[2];
338 >        xfmat[1][0] = vw1->vvec[0];
339 >        xfmat[1][1] = vw1->vvec[1];
340 >        xfmat[1][2] = vw1->vvec[2];
341          xfmat[2][0] = vw1->vdir[0];
342          xfmat[2][1] = vw1->vdir[1];
343          xfmat[2][2] = vw1->vdir[2];
# Line 339 | Line 345 | register VIEW  *vw1, *vw2;
345          xfmat[3][1] = vw1->vp[1];
346          xfmat[3][2] = vw1->vp[2];
347          setident4(m4t);
348 <        m4t[0][0] = vw2->vhinc[0]/vw2->vhn2;
349 <        m4t[1][0] = vw2->vhinc[1]/vw2->vhn2;
350 <        m4t[2][0] = vw2->vhinc[2]/vw2->vhn2;
351 <        m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2;
352 <        m4t[0][1] = vw2->vvinc[0]/vw2->vvn2;
353 <        m4t[1][1] = vw2->vvinc[1]/vw2->vvn2;
354 <        m4t[2][1] = vw2->vvinc[2]/vw2->vvn2;
355 <        m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2;
348 >        m4t[0][0] = vw2->hvec[0]/vw2->hn2;
349 >        m4t[1][0] = vw2->hvec[1]/vw2->hn2;
350 >        m4t[2][0] = vw2->hvec[2]/vw2->hn2;
351 >        m4t[3][0] = -DOT(vw2->vp,vw2->hvec)/vw2->hn2;
352 >        m4t[0][1] = vw2->vvec[0]/vw2->vn2;
353 >        m4t[1][1] = vw2->vvec[1]/vw2->vn2;
354 >        m4t[2][1] = vw2->vvec[2]/vw2->vn2;
355 >        m4t[3][1] = -DOT(vw2->vp,vw2->vvec)/vw2->vn2;
356          m4t[0][2] = vw2->vdir[0];
357          m4t[1][2] = vw2->vdir[1];
358          m4t[2][2] = vw2->vdir[2];
# Line 366 | Line 372 | struct position        *lasty;         /* input/output */
372          struct position lastx, newpos;
373          register int    x;
374  
375 <        for (x = theirview.hresolu-1; x >= 0; x--) {
376 <                pos[0] = x - .5*(theirview.hresolu-1);
377 <                pos[1] = y - .5*(theirview.vresolu-1);
375 >        lastx.z = 0;
376 >        for (x = thresolu-1; x >= 0; x--) {
377 >                pos[0] = (x+.5)/thresolu + theirview.hoff - .5;
378 >                pos[1] = (y+.5)/tvresolu + theirview.voff - .5;
379                  pos[2] = zline[x];
380                  if (theirview.type == VT_PER) {
381                          if (normdist)   /* adjust for eye-ray distance */
382                                  pos[2] /= sqrt( 1.
383 <                                        + pos[0]*pos[0]*theirview.vhn2
384 <                                        + pos[1]*pos[1]*theirview.vvn2 );
383 >                                        + pos[0]*pos[0]*theirview.hn2
384 >                                        + pos[1]*pos[1]*theirview.vn2 );
385                          pos[0] *= pos[2];
386                          pos[1] *= pos[2];
387                  }
# Line 387 | Line 394 | struct position        *lasty;         /* input/output */
394                          pos[0] /= pos[2];
395                          pos[1] /= pos[2];
396                  }
397 <                pos[0] += .5*ourview.hresolu;
398 <                pos[1] += .5*ourview.vresolu;
399 <                newpos.x = pos[0];
400 <                newpos.y = pos[1];
397 >                pos[0] += .5 - ourview.hoff;
398 >                pos[1] += .5 - ourview.voff;
399 >                newpos.x = pos[0] * hresolu;
400 >                newpos.y = pos[1] * vresolu;
401                  newpos.z = zline[x];
402                                          /* add pixel to our image */
403 <                if (pos[0] >= 0 && newpos.x < ourview.hresolu
404 <                                && pos[1] >= 0 && newpos.y < ourview.vresolu) {
403 >                if (pos[0] >= 0 && newpos.x < hresolu
404 >                                && pos[1] >= 0 && newpos.y < vresolu) {
405                          addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
406                          lasty[x].x = lastx.x = newpos.x;
407                          lasty[x].y = lastx.y = newpos.y;
# Line 418 | Line 425 | double z;
425          register int    x, y;                   /* final position */
426  
427                                          /* compute vector p0p1 */
428 <        if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) {
428 >        if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
429                  s1x = p1->x - p0->x;
430                  s1y = p1->y - p0->y;
431                  l1 = ABS(s1x);
# Line 429 | Line 436 | double z;
436                  p1isy = -1;
437          }
438                                          /* compute vector p0p2 */
439 <        if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) {
439 >        if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
440                  s2x = p2->x - p0->x;
441                  s2y = p2->y - p0->y;
442                  if (p1isy == 1)
# Line 458 | Line 465 | double z;
465   }
466  
467  
468 < backpicture()                           /* background fill algorithm */
468 > backpicture(fill, samp)                 /* background fill algorithm */
469 > int     (*fill)();
470 > int     samp;
471   {
472          int     *yback, xback;
473          int     y;
474          COLR    pfill;
475          register int    x, i;
476                                                          /* get back buffer */
477 <        yback = (int *)malloc(ourview.hresolu*sizeof(int));
477 >        yback = (int *)malloc(hresolu*sizeof(int));
478          if (yback == NULL)
479                  syserror();
480 <        for (x = 0; x < ourview.hresolu; x++)
480 >        for (x = 0; x < hresolu; x++)
481                  yback[x] = -2;
482          /*
483           * Xback and yback are the pixel locations of suitable
# Line 477 | Line 486 | backpicture()                          /* background fill algorithm */
486           * that there is no suitable background in this direction.
487           */
488                                                          /* fill image */
489 <        for (y = 0; y < ourview.vresolu; y++) {
489 >        for (y = 0; y < vresolu; y++) {
490                  xback = -2;
491 <                for (x = 0; x < ourview.hresolu; x++)
491 >                for (x = 0; x < hresolu; x++)
492                          if (zscan(y)[x] <= 0) {         /* empty pixel */
493                                  /*
494                                   * First, find background from above or below.
495                                   * (farthest assigned pixel)
496                                   */
497                                  if (yback[x] == -2) {
498 <                                        for (i = y+1; i < ourview.vresolu; i++)
498 >                                        for (i = y+1; i < vresolu; i++)
499                                                  if (zscan(i)[x] > 0)
500                                                          break;
501 <                                        if (i < ourview.vresolu
501 >                                        if (i < vresolu
502                                  && (y <= 0 || zscan(y-1)[x] < zscan(i)[x]))
503                                                  yback[x] = i;
504                                          else
# Line 499 | Line 508 | backpicture()                          /* background fill algorithm */
508                                   * Next, find background from left or right.
509                                   */
510                                  if (xback == -2) {
511 <                                        for (i = x+1; i < ourview.hresolu; i++)
511 >                                        for (i = x+1; i < hresolu; i++)
512                                                  if (zscan(y)[i] > 0)
513                                                          break;
514 <                                        if (i < ourview.hresolu
514 >                                        if (i < hresolu
515                                  && (x <= 0 || zscan(y)[x-1] < zscan(y)[i]))
516                                                  xback = i;
517                                          else
518                                                  xback = x-1;
519                                  }
520                                  /*
521 <                                 * Check to see if we have no background for
522 <                                 * this pixel.  If not, use background color.
521 >                                 * If we have no background for this pixel,
522 >                                 * use the given fill function.
523                                   */
524 <                                if (xback < 0 && yback[x] < 0) {
525 <                                        (*deffill)(x,y);
517 <                                        continue;
518 <                                }
524 >                                if (xback < 0 && yback[x] < 0)
525 >                                        goto fillit;
526                                  /*
527                                   * Compare, and use the background that is
528                                   * farther, unless one of them is next to us.
529 +                                 * If the background is too distant, call
530 +                                 * the fill function.
531                                   */
532                                  if ( yback[x] < 0
533                                          || (xback >= 0 && ABS(x-xback) <= 1)
534                                          || ( ABS(y-yback[x]) > 1
535                                                  && zscan(yback[x])[x]
536                                                  < zscan(y)[xback] ) ) {
537 +                                        if (samp > 0 && ABS(x-xback) >= samp)
538 +                                                goto fillit;
539                                          copycolr(pscan(y)[x],pscan(y)[xback]);
540                                          zscan(y)[x] = zscan(y)[xback];
541                                  } else {
542 +                                        if (samp > 0 && ABS(y-yback[x]) > samp)
543 +                                                goto fillit;
544                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
545                                          zscan(y)[x] = zscan(yback[x])[x];
546                                  }
547 +                                continue;
548 +                        fillit:
549 +                                (*fill)(x,y);
550 +                                if (fill == rcalfill) {         /* use it */
551 +                                        clearqueue();
552 +                                        xback = x;
553 +                                        yback[x] = y;
554 +                                }
555                          } else {                                /* full pixel */
556                                  yback[x] = -2;
557                                  xback = -2;
# Line 540 | Line 561 | backpicture()                          /* background fill algorithm */
561   }
562  
563  
564 < fillpicture()                           /* paint in empty pixels with default */
564 > fillpicture(fill)               /* paint in empty pixels using fill */
565 > int     (*fill)();
566   {
567          register int    x, y;
568  
569 <        for (y = 0; y < ourview.vresolu; y++)
570 <                for (x = 0; x < ourview.hresolu; x++)
569 >        for (y = 0; y < vresolu; y++)
570 >                for (x = 0; x < hresolu; x++)
571                          if (zscan(y)[x] <= 0)
572 <                                (*deffill)(x,y);
572 >                                (*fill)(x,y);
573   }
574  
575  
# Line 555 | Line 577 | writepicture()                         /* write out picture */
577   {
578          int     y;
579  
580 <        fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
581 <        for (y = ourview.vresolu-1; y >= 0; y--)
582 <                if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0)
580 >        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
581 >        for (y = vresolu-1; y >= 0; y--)
582 >                if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
583                          syserror();
584   }
585  
# Line 567 | Line 589 | char   *fname;
589   {
590          extern double   sqrt();
591          int     donorm = normdist && ourview.type == VT_PER;
592 <        FILE    *fp;
592 >        int     fd;
593          int     y;
594          float   *zout;
595  
596 <        if ((fp = fopen(fname, "w")) == NULL) {
596 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
597                  perror(fname);
598                  exit(1);
599          }
600          if (donorm
601 <        && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL)
601 >        && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
602                  syserror();
603 <        for (y = ourview.vresolu-1; y >= 0; y--) {
603 >        for (y = vresolu-1; y >= 0; y--) {
604                  if (donorm) {
605                          double  vx, yzn2;
606                          register int    x;
607 <                        yzn2 = y - .5*(ourview.vresolu-1);
608 <                        yzn2 = 1. + yzn2*yzn2*ourview.vvn2;
609 <                        for (x = 0; x < ourview.hresolu; x++) {
610 <                                vx = x - .5*(ourview.hresolu-1);
607 >                        yzn2 = y - .5*(vresolu-1);
608 >                        yzn2 = 1. + yzn2*yzn2*ourview.vn2;
609 >                        for (x = 0; x < hresolu; x++) {
610 >                                vx = x - .5*(hresolu-1);
611                                  zout[x] = zscan(y)[x]
612 <                                        * sqrt(vx*vx*ourview.vhn2 + yzn2);
612 >                                        * sqrt(vx*vx*ourview.hn2 + yzn2);
613                          }
614                  } else
615                          zout = zscan(y);
616 <                if (fwrite(zout, sizeof(float), ourview.hresolu, fp)
617 <                                < ourview.hresolu) {
616 >                if (write(fd, (char *)zout, hresolu*sizeof(float))
617 >                                < hresolu*sizeof(float)) {
618                          perror(fname);
619                          exit(1);
620                  }
621          }
622          if (donorm)
623                  free((char *)zout);
624 <        fclose(fp);
624 >        close(fd);
625   }
626  
627  
# Line 634 | Line 656 | char   *prog, *args;
656                  fprintf(stderr, "%s: too many calculations\n", progname);
657                  exit(1);
658          }
659 <        sprintf(combuf, prog, PACKSIZ, args);
659 >        sprintf(combuf, prog, args);
660          if (pipe(p0) < 0 || pipe(p1) < 0)
661                  syserror();
662          if ((childpid = vfork()) == 0) {        /* fork calculation */
# Line 670 | Line 692 | caldone()                              /* done with calculation */
692  
693          if (childpid == -1)
694                  return;
673        if (fclose(psend) == EOF)
674                syserror();
695          clearqueue();
696 +        fclose(psend);
697          fclose(precv);
698          while ((pid = wait(0)) != -1 && pid != childpid)
699                  ;
# Line 683 | Line 704 | caldone()                              /* done with calculation */
704   rcalfill(x, y)                          /* fill with ray-calculated pixel */
705   int     x, y;
706   {
707 <        FVECT   orig, dir;
687 <        float   outbuf[6];
688 <
689 <        if (queuesiz >= PACKSIZ) {      /* flush queue */
690 <                if (fflush(psend) == EOF)
691 <                        syserror();
707 >        if (queuesiz >= PACKSIZ)        /* flush queue if needed */
708                  clearqueue();
709 <        }
694 <                                        /* send new ray */
695 <        rayview(orig, dir, &ourview, x+.5, y+.5);
696 <        outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
697 <        outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
698 <        if (fwrite(outbuf, sizeof(float), 6, psend) < 6)
699 <                syserror();
700 <                                        /* remember it */
709 >                                        /* add position to queue */
710          queue[queuesiz][0] = x;
711          queue[queuesiz][1] = y;
712          queuesiz++;
713   }
714  
715  
716 < clearqueue()                            /* get results from queue */
716 > clearqueue()                            /* process queue */
717   {
718 <        float   inbuf[4];
718 >        FVECT   orig, dir;
719 >        float   fbuf[6];
720          register int    i;
721  
722          for (i = 0; i < queuesiz; i++) {
723 <                if (fread(inbuf, sizeof(float), 4, precv) < 4) {
723 >                viewray(orig, dir, &ourview,
724 >                                (queue[i][0]+.5)/hresolu,
725 >                                (queue[i][1]+.5)/vresolu);
726 >                fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2];
727 >                fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
728 >                fwrite((char *)fbuf, sizeof(float), 6, psend);
729 >        }
730 >                                        /* flush output and get results */
731 >        fbuf[3] = fbuf[4] = fbuf[5] = 0.0;              /* mark */
732 >        fwrite((char *)fbuf, sizeof(float), 6, psend);
733 >        if (fflush(psend) == EOF)
734 >                syserror();
735 >        for (i = 0; i < queuesiz; i++) {
736 >                if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) {
737                          fprintf(stderr, "%s: read error in clearqueue\n",
738                                          progname);
739                          exit(1);
740                  }
741 +                if (ourexp > 0 && ourexp != 1.0) {
742 +                        fbuf[0] *= ourexp;
743 +                        fbuf[1] *= ourexp;
744 +                        fbuf[2] *= ourexp;
745 +                }
746                  setcolr(pscan(queue[i][1])[queue[i][0]],
747 <                                inbuf[0], inbuf[1], inbuf[2]);
748 <                zscan(queue[i][1])[queue[i][0]] = inbuf[3];
747 >                                fbuf[0], fbuf[1], fbuf[2]);
748 >                zscan(queue[i][1])[queue[i][0]] = fbuf[3];
749          }
750          queuesiz = 0;
751   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines