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.25 by greg, Tue Mar 6 11:14:36 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)
18 < #define zscan(y)        (ourzbuf+(y)*ourview.hresolu)
19 > #include "random.h"
20  
21 + #ifndef BSD
22 + #define vfork           fork
23 + #endif
24 +
25 + #define pscan(y)        (ourpict+(y)*hresolu)
26 + #define zscan(y)        (ourzbuf+(y)*hresolu)
27 +
28 + #define PMASK           0xfffffff       /* probability mask */
29 + #define sampval(x)      (long)((x)*(PMASK+1)+.5)
30 + #define samp(p)         ((p) > (random()&PMASK))
31 +
32   #define F_FORE          1               /* fill foreground */
33   #define F_BACK          2               /* fill background */
34  
35   #define PACKSIZ         42              /* calculation packet size */
36  
37 < #define RTCOM           "rtrace -h -ovl -fff -x %d %s"
37 > #define RTCOM           "rtrace -h -ovl -fff %s"
38  
39   #define ABS(x)          ((x)>0?(x):-(x))
40  
41   struct position {int x,y; float z;};
42  
43 < VIEW    ourview = STDVIEW(512);         /* desired view */
43 > VIEW    ourview = STDVIEW;              /* desired view */
44 > int     hresolu = 512;                  /* horizontal resolution */
45 > int     vresolu = 512;                  /* vertical resolution */
46 > double  pixaspect = 1.0;                /* pixel aspect ratio */
47  
48   double  zeps = .02;                     /* allowed z epsilon */
49  
# Line 37 | Line 52 | float  *ourzbuf;                       /* corresponding z-buffer */
52  
53   char    *progname;
54  
55 < VIEW    theirview = STDVIEW(512);       /* input view */
56 < int     gotview;                        /* got input view? */
42 <
43 < int     fill = F_FORE|F_BACK;           /* selected fill algorithm */
55 > int     fillo = F_FORE|F_BACK;          /* selected fill options */
56 > long    sampprob = 0;                   /* sample probability */
57   extern int      backfill(), rcalfill(); /* fill functions */
58 < int     (*deffill)() = backfill;        /* selected fill function */
58 > int     (*fillfunc)() = backfill;       /* selected fill function */
59   COLR    backcolr = BLKCOLR;             /* background color */
60   double  backz = 0.0;                    /* background z value */
61 + int     normdist = 1;                   /* normalized distance? */
62 + double  ourexp = -1;                    /* output picture exposure */
63  
64 + VIEW    theirview = STDVIEW;            /* input view */
65 + int     gotview;                        /* got input view? */
66 + int     thresolu, tvresolu;             /* input resolution */
67 + double  theirexp;                       /* input picture exposure */
68   double  theirs2ours[4][4];              /* transformation matrix */
50 int     normdist = 1;                   /* normalized distance? */
69  
70   int     childpid = -1;                  /* id of fill process */
71   FILE    *psend, *precv;                 /* pipes to/from fill calculation */
# Line 64 | Line 82 | char   *argv[];
82          int     gotvfile = 0;
83          char    *zfile = NULL;
84          char    *err;
85 <        int     i;
85 >        int     i, rval;
86  
87          progname = argv[0];
88  
89 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
89 >        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
90 >                rval = getviewopt(&ourview, argc-i, argv+i);
91 >                if (rval >= 0) {
92 >                        i += rval;
93 >                        continue;
94 >                }
95                  switch (argv[i][1]) {
96                  case 't':                               /* threshold */
97                          check(2,1);
# Line 82 | Line 105 | char   *argv[];
105                          switch (argv[i][2]) {
106                          case '0':                               /* none */
107                                  check(3,0);
108 <                                fill = 0;
108 >                                fillo = 0;
109                                  break;
110                          case 'f':                               /* foreground */
111                                  check(3,0);
112 <                                fill = F_FORE;
112 >                                fillo = F_FORE;
113                                  break;
114                          case 'b':                               /* background */
115                                  check(3,0);
116 <                                fill = F_BACK;
116 >                                fillo = F_BACK;
117                                  break;
118                          case 'a':                               /* all */
119                                  check(3,0);
120 <                                fill = F_FORE|F_BACK;
120 >                                fillo = F_FORE|F_BACK;
121                                  break;
122 +                        case 's':                               /* sample */
123 +                                check(3,1);
124 +                                sampprob = sampval(atof(argv[++i]));
125 +                                break;
126                          case 'c':                               /* color */
127                                  check(3,3);
128 <                                deffill = backfill;
128 >                                fillfunc = backfill;
129                                  setcolr(backcolr, atof(argv[i+1]),
130                                          atof(argv[i+2]), atof(argv[i+3]));
131                                  i += 3;
132                                  break;
133                          case 'z':                               /* z value */
134                                  check(3,1);
135 <                                deffill = backfill;
135 >                                fillfunc = backfill;
136                                  backz = atof(argv[++i]);
137                                  break;
138                          case 'r':                               /* rtrace */
139                                  check(3,1);
140 <                                deffill = rcalfill;
140 >                                fillfunc = rcalfill;
141                                  calstart(RTCOM, argv[++i]);
142                                  break;
143                          default:
# Line 123 | Line 150 | char   *argv[];
150                          break;
151                  case 'x':                               /* x resolution */
152                          check(2,1);
153 <                        ourview.hresolu = atoi(argv[++i]);
153 >                        hresolu = atoi(argv[++i]);
154                          break;
155                  case 'y':                               /* y resolution */
156                          check(2,1);
157 <                        ourview.vresolu = atoi(argv[++i]);
157 >                        vresolu = atoi(argv[++i]);
158                          break;
159 <                case 'v':                               /* view */
160 <                        switch (argv[i][2]) {
161 <                        case 't':                               /* type */
162 <                                check(4,0);
163 <                                ourview.type = argv[i][3];
164 <                                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:
159 >                case 'p':                               /* pixel aspect */
160 >                        check(2,1);
161 >                        pixaspect = atof(argv[++i]);
162 >                        break;
163 >                case 'v':                               /* view file */
164 >                        if (argv[i][2] != 'f')
165                                  goto badopt;
166 +                        check(3,1);
167 +                        gotvfile = viewfile(argv[++i], &ourview, 0, 0);
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:
# Line 183 | Line 180 | char   *argv[];
180                                          progname, argv[i]);
181                          goto userr;
182                  }
183 +        }
184                                                  /* check arguments */
185          if ((argc-i)%2)
186                  goto userr;
# Line 191 | Line 189 | char   *argv[];
189                  fprintf(stderr, "%s: %s\n", progname, err);
190                  exit(1);
191          }
192 +        normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
193                                                  /* allocate frame */
194 <        ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR));
195 <        ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float));
194 >        ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR));
195 >        ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float));
196          if (ourpict == NULL || ourzbuf == NULL)
197                  syserror();
198                                                          /* get input */
199          for ( ; i < argc; i += 2)
200                  addpicture(argv[i], argv[i+1]);
201                                                          /* fill in spaces */
202 <        if (fill&F_BACK)
203 <                backpicture();
202 >        if (fillo&F_BACK)
203 >                backpicture(fillfunc, sampprob);
204          else
205 <                fillpicture();
205 >                fillpicture(fillfunc);
206                                                          /* close calculation */
207          caldone();
208                                                          /* add to header */
209          printargs(argc, argv, stdout);
210          if (gotvfile) {
211 <                printf(VIEWSTR);
211 >                fputs(VIEWSTR, stdout);
212                  fprintview(&ourview, stdout);
213 <                printf("\n");
213 >                putc('\n', stdout);
214          }
215 <        printf("\n");
215 >        if (pixaspect < .99 || pixaspect > 1.01)
216 >                fputaspect(pixaspect, stdout);
217 >        if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
218 >                fputexpos(ourexp, stdout);
219 >        putc('\n', stdout);
220                                                          /* write picture */
221          writepicture();
222                                                          /* write z file */
# Line 223 | Line 226 | char   *argv[];
226          exit(0);
227   userr:
228          fprintf(stderr,
229 <        "Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n",
229 >        "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n",
230                          progname);
231          exit(1);
232   #undef check
# Line 233 | Line 236 | userr:
236   headline(s)                             /* process header string */
237   char    *s;
238   {
239 <        static char     *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
239 >        static char     *altname[] = {VIEWSTR,"rpict","rview","pinterp",NULL};
240          register char   **an;
241  
242 <        printf("\t%s", s);
242 >        putc('\t', stdout);
243 >        fputs(s, stdout);
244  
245 +        if (isexpos(s)) {
246 +                theirexp *= exposval(s);
247 +                return;
248 +        }
249          for (an = altname; *an != NULL; an++)
250                  if (!strncmp(*an, s, strlen(*an))) {
251 <                        if (sscanview(&theirview, s+strlen(*an)) == 0)
251 >                        if (sscanview(&theirview, s+strlen(*an)) > 0)
252                                  gotview++;
253                          break;
254                  }
# Line 251 | Line 259 | addpicture(pfile, zspec)               /* add picture to output */
259   char    *pfile, *zspec;
260   {
261          extern double   atof();
262 <        FILE    *pfp, *zfp;
262 >        FILE    *pfp;
263 >        int     zfd;
264          char    *err;
265          COLR    *scanin;
266          float   *zin;
# Line 262 | Line 271 | char   *pfile, *zspec;
271                  perror(pfile);
272                  exit(1);
273          }
274 <                                        /* get header and view */
275 <        printf("%s:\n", pfile);
274 >                                        /* get header with exposure and view */
275 >        theirexp = 1.0;
276          gotview = 0;
277 +        printf("%s:\n", pfile);
278          getheader(pfp, headline);
279 <        if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp)
279 >        if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp)
280                          != (YMAJOR|YDECR)) {
281                  fprintf(stderr, "%s: picture view error\n", pfile);
282                  exit(1);
283          }
284 +        if (ourexp <= 0)
285 +                ourexp = theirexp;
286 +        else if (ABS(theirexp-ourexp) > .01*ourexp)
287 +                fprintf(stderr, "%s: different exposure (warning)\n", pfile);
288          if (err = setview(&theirview)) {
289                  fprintf(stderr, "%s: %s\n", pfile, err);
290                  exit(1);
# Line 278 | Line 292 | char   *pfile, *zspec;
292                                          /* compute transformation */
293          pixform(theirs2ours, &theirview, &ourview);
294                                          /* allocate scanlines */
295 <        scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR));
296 <        zin = (float *)malloc(theirview.hresolu*sizeof(float));
297 <        plast = (struct position *)calloc(theirview.hresolu,
284 <                                                sizeof(struct position));
295 >        scanin = (COLR *)malloc(thresolu*sizeof(COLR));
296 >        zin = (float *)malloc(thresolu*sizeof(float));
297 >        plast = (struct position *)calloc(thresolu, sizeof(struct position));
298          if (scanin == NULL || zin == NULL || plast == NULL)
299                  syserror();
300                                          /* get z specification or file */
301 <        if ((zfp = fopen(zspec, "r")) == NULL) {
301 >        if ((zfd = open(zspec, O_RDONLY)) == -1) {
302                  double  zvalue;
303                  register int    x;
304                  if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
305                          perror(zspec);
306                          exit(1);
307                  }
308 <                for (x = 0; x < theirview.hresolu; x++)
308 >                for (x = 0; x < thresolu; x++)
309                          zin[x] = zvalue;
310          }
311                                          /* load image */
312 <        for (y = theirview.vresolu-1; y >= 0; y--) {
313 <                if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) {
312 >        for (y = tvresolu-1; y >= 0; y--) {
313 >                if (freadcolrs(scanin, thresolu, pfp) < 0) {
314                          fprintf(stderr, "%s: read error\n", pfile);
315                          exit(1);
316                  }
317 <                if (zfp != NULL
318 <                        && fread(zin,sizeof(float),theirview.hresolu,zfp)
306 <                                < theirview.hresolu) {
317 >                if (zfd != -1 && read(zfd,(char *)zin,thresolu*sizeof(float))
318 >                                < thresolu*sizeof(float)) {
319                          fprintf(stderr, "%s: read error\n", zspec);
320                          exit(1);
321                  }
# Line 314 | Line 326 | char   *pfile, *zspec;
326          free((char *)zin);
327          free((char *)plast);
328          fclose(pfp);
329 <        if (zfp != NULL)
330 <                fclose(zfp);
329 >        if (zfd != -1)
330 >                close(zfd);
331   }
332  
333  
# Line 326 | Line 338 | register VIEW  *vw1, *vw2;
338          double  m4t[4][4];
339  
340          setident4(xfmat);
341 <        xfmat[0][0] = vw1->vhinc[0];
342 <        xfmat[0][1] = vw1->vhinc[1];
343 <        xfmat[0][2] = vw1->vhinc[2];
344 <        xfmat[1][0] = vw1->vvinc[0];
345 <        xfmat[1][1] = vw1->vvinc[1];
346 <        xfmat[1][2] = vw1->vvinc[2];
341 >        xfmat[0][0] = vw1->hvec[0];
342 >        xfmat[0][1] = vw1->hvec[1];
343 >        xfmat[0][2] = vw1->hvec[2];
344 >        xfmat[1][0] = vw1->vvec[0];
345 >        xfmat[1][1] = vw1->vvec[1];
346 >        xfmat[1][2] = vw1->vvec[2];
347          xfmat[2][0] = vw1->vdir[0];
348          xfmat[2][1] = vw1->vdir[1];
349          xfmat[2][2] = vw1->vdir[2];
# Line 339 | Line 351 | register VIEW  *vw1, *vw2;
351          xfmat[3][1] = vw1->vp[1];
352          xfmat[3][2] = vw1->vp[2];
353          setident4(m4t);
354 <        m4t[0][0] = vw2->vhinc[0]/vw2->vhn2;
355 <        m4t[1][0] = vw2->vhinc[1]/vw2->vhn2;
356 <        m4t[2][0] = vw2->vhinc[2]/vw2->vhn2;
357 <        m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2;
358 <        m4t[0][1] = vw2->vvinc[0]/vw2->vvn2;
359 <        m4t[1][1] = vw2->vvinc[1]/vw2->vvn2;
360 <        m4t[2][1] = vw2->vvinc[2]/vw2->vvn2;
361 <        m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2;
354 >        m4t[0][0] = vw2->hvec[0]/vw2->hn2;
355 >        m4t[1][0] = vw2->hvec[1]/vw2->hn2;
356 >        m4t[2][0] = vw2->hvec[2]/vw2->hn2;
357 >        m4t[3][0] = -DOT(vw2->vp,vw2->hvec)/vw2->hn2;
358 >        m4t[0][1] = vw2->vvec[0]/vw2->vn2;
359 >        m4t[1][1] = vw2->vvec[1]/vw2->vn2;
360 >        m4t[2][1] = vw2->vvec[2]/vw2->vn2;
361 >        m4t[3][1] = -DOT(vw2->vp,vw2->vvec)/vw2->vn2;
362          m4t[0][2] = vw2->vdir[0];
363          m4t[1][2] = vw2->vdir[1];
364          m4t[2][2] = vw2->vdir[2];
# Line 366 | Line 378 | struct position        *lasty;         /* input/output */
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 + theirview.hoff - .5;
384 >                pos[1] = (y+.5)/tvresolu + theirview.voff - .5;
385                  pos[2] = zline[x];
386                  if (theirview.type == VT_PER) {
387                          if (normdist)   /* adjust for eye-ray distance */
388                                  pos[2] /= sqrt( 1.
389 <                                        + pos[0]*pos[0]*theirview.vhn2
390 <                                        + pos[1]*pos[1]*theirview.vvn2 );
389 >                                        + pos[0]*pos[0]*theirview.hn2
390 >                                        + pos[1]*pos[1]*theirview.vn2 );
391                          pos[0] *= pos[2];
392                          pos[1] *= pos[2];
393                  }
# Line 387 | Line 400 | struct position        *lasty;         /* input/output */
400                          pos[0] /= pos[2];
401                          pos[1] /= pos[2];
402                  }
403 <                pos[0] += .5*ourview.hresolu;
404 <                pos[1] += .5*ourview.vresolu;
405 <                newpos.x = pos[0];
406 <                newpos.y = pos[1];
403 >                pos[0] += .5 - ourview.hoff;
404 >                pos[1] += .5 - ourview.voff;
405 >                newpos.x = pos[0] * hresolu;
406 >                newpos.y = pos[1] * vresolu;
407                  newpos.z = zline[x];
408                                          /* add pixel to our image */
409 <                if (pos[0] >= 0 && newpos.x < ourview.hresolu
410 <                                && pos[1] >= 0 && newpos.y < ourview.vresolu) {
409 >                if (pos[0] >= 0 && newpos.x < hresolu
410 >                                && pos[1] >= 0 && newpos.y < vresolu) {
411                          addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
412                          lasty[x].x = lastx.x = newpos.x;
413                          lasty[x].y = lastx.y = newpos.y;
# Line 418 | Line 431 | double z;
431          register int    x, y;                   /* final position */
432  
433                                          /* compute vector p0p1 */
434 <        if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) {
434 >        if (fillo&F_FORE && ABS(p1->z-p0->z) <= zt) {
435                  s1x = p1->x - p0->x;
436                  s1y = p1->y - p0->y;
437                  l1 = ABS(s1x);
# Line 429 | Line 442 | double z;
442                  p1isy = -1;
443          }
444                                          /* compute vector p0p2 */
445 <        if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) {
445 >        if (fillo&F_FORE && ABS(p2->z-p0->z) <= zt) {
446                  s2x = p2->x - p0->x;
447                  s2y = p2->y - p0->y;
448                  if (p1isy == 1)
# Line 458 | Line 471 | double z;
471   }
472  
473  
474 < backpicture()                           /* background fill algorithm */
474 > backpicture(fill, prob)                 /* background fill algorithm */
475 > int     (*fill)();
476 > long    prob;
477   {
478          int     *yback, xback;
479          int     y;
480          COLR    pfill;
481          register int    x, i;
482                                                          /* get back buffer */
483 <        yback = (int *)malloc(ourview.hresolu*sizeof(int));
483 >        yback = (int *)malloc(hresolu*sizeof(int));
484          if (yback == NULL)
485                  syserror();
486 <        for (x = 0; x < ourview.hresolu; x++)
486 >        for (x = 0; x < hresolu; x++)
487                  yback[x] = -2;
488          /*
489           * Xback and yback are the pixel locations of suitable
# Line 477 | Line 492 | backpicture()                          /* background fill algorithm */
492           * that there is no suitable background in this direction.
493           */
494                                                          /* fill image */
495 <        for (y = 0; y < ourview.vresolu; y++) {
495 >        for (y = 0; y < vresolu; y++) {
496                  xback = -2;
497 <                for (x = 0; x < ourview.hresolu; x++)
497 >                for (x = 0; x < hresolu; x++)
498                          if (zscan(y)[x] <= 0) {         /* empty pixel */
499                                  /*
500                                   * First, find background from above or below.
501                                   * (farthest assigned pixel)
502                                   */
503                                  if (yback[x] == -2) {
504 <                                        for (i = y+1; i < ourview.vresolu; i++)
504 >                                        for (i = y+1; i < vresolu; i++)
505                                                  if (zscan(i)[x] > 0)
506                                                          break;
507 <                                        if (i < ourview.vresolu
507 >                                        if (i < vresolu
508                                  && (y <= 0 || zscan(y-1)[x] < zscan(i)[x]))
509                                                  yback[x] = i;
510                                          else
# Line 499 | Line 514 | backpicture()                          /* background fill algorithm */
514                                   * Next, find background from left or right.
515                                   */
516                                  if (xback == -2) {
517 <                                        for (i = x+1; i < ourview.hresolu; i++)
517 >                                        for (i = x+1; i < hresolu; i++)
518                                                  if (zscan(y)[i] > 0)
519                                                          break;
520 <                                        if (i < ourview.hresolu
520 >                                        if (i < hresolu
521                                  && (x <= 0 || zscan(y)[x-1] < zscan(y)[i]))
522                                                  xback = i;
523                                          else
# Line 510 | Line 525 | backpicture()                          /* background fill algorithm */
525                                  }
526                                  /*
527                                   * Check to see if we have no background for
528 <                                 * this pixel.  If not, use background color.
528 >                                 * this pixel.  If not, or sampling was
529 >                                 * requested, use the given fill function.
530                                   */
531 <                                if (xback < 0 && yback[x] < 0) {
532 <                                        (*deffill)(x,y);
531 >                                if ((xback < 0 && yback[x] < 0) || samp(prob)) {
532 >                                        (*fill)(x,y);
533                                          continue;
534                                  }
535                                  /*
# Line 540 | Line 556 | backpicture()                          /* background fill algorithm */
556   }
557  
558  
559 < fillpicture()                           /* paint in empty pixels with default */
559 > fillpicture(fill)               /* paint in empty pixels using fill */
560 > int     (*fill)();
561   {
562          register int    x, y;
563  
564 <        for (y = 0; y < ourview.vresolu; y++)
565 <                for (x = 0; x < ourview.hresolu; x++)
564 >        for (y = 0; y < vresolu; y++)
565 >                for (x = 0; x < hresolu; x++)
566                          if (zscan(y)[x] <= 0)
567 <                                (*deffill)(x,y);
567 >                                (*fill)(x,y);
568   }
569  
570  
# Line 555 | Line 572 | writepicture()                         /* write out picture */
572   {
573          int     y;
574  
575 <        fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
576 <        for (y = ourview.vresolu-1; y >= 0; y--)
577 <                if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0)
575 >        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
576 >        for (y = vresolu-1; y >= 0; y--)
577 >                if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
578                          syserror();
579   }
580  
# Line 567 | Line 584 | char   *fname;
584   {
585          extern double   sqrt();
586          int     donorm = normdist && ourview.type == VT_PER;
587 <        FILE    *fp;
587 >        int     fd;
588          int     y;
589          float   *zout;
590  
591 <        if ((fp = fopen(fname, "w")) == NULL) {
591 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
592                  perror(fname);
593                  exit(1);
594          }
595          if (donorm
596 <        && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL)
596 >        && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
597                  syserror();
598 <        for (y = ourview.vresolu-1; y >= 0; y--) {
598 >        for (y = vresolu-1; y >= 0; y--) {
599                  if (donorm) {
600                          double  vx, yzn2;
601                          register int    x;
602 <                        yzn2 = y - .5*(ourview.vresolu-1);
603 <                        yzn2 = 1. + yzn2*yzn2*ourview.vvn2;
604 <                        for (x = 0; x < ourview.hresolu; x++) {
605 <                                vx = x - .5*(ourview.hresolu-1);
602 >                        yzn2 = y - .5*(vresolu-1);
603 >                        yzn2 = 1. + yzn2*yzn2*ourview.vn2;
604 >                        for (x = 0; x < hresolu; x++) {
605 >                                vx = x - .5*(hresolu-1);
606                                  zout[x] = zscan(y)[x]
607 <                                        * sqrt(vx*vx*ourview.vhn2 + yzn2);
607 >                                        * sqrt(vx*vx*ourview.hn2 + yzn2);
608                          }
609                  } else
610                          zout = zscan(y);
611 <                if (fwrite(zout, sizeof(float), ourview.hresolu, fp)
612 <                                < ourview.hresolu) {
611 >                if (write(fd, (char *)zout, hresolu*sizeof(float))
612 >                                < hresolu*sizeof(float)) {
613                          perror(fname);
614                          exit(1);
615                  }
616          }
617          if (donorm)
618                  free((char *)zout);
619 <        fclose(fp);
619 >        close(fd);
620   }
621  
622  
# Line 634 | Line 651 | char   *prog, *args;
651                  fprintf(stderr, "%s: too many calculations\n", progname);
652                  exit(1);
653          }
654 <        sprintf(combuf, prog, PACKSIZ, args);
654 >        sprintf(combuf, prog, args);
655          if (pipe(p0) < 0 || pipe(p1) < 0)
656                  syserror();
657          if ((childpid = vfork()) == 0) {        /* fork calculation */
# Line 670 | Line 687 | caldone()                              /* done with calculation */
687  
688          if (childpid == -1)
689                  return;
673        if (fclose(psend) == EOF)
674                syserror();
690          clearqueue();
691 +        fclose(psend);
692          fclose(precv);
693          while ((pid = wait(0)) != -1 && pid != childpid)
694                  ;
# Line 683 | Line 699 | caldone()                              /* done with calculation */
699   rcalfill(x, y)                          /* fill with ray-calculated pixel */
700   int     x, y;
701   {
702 <        FVECT   orig, dir;
687 <        float   outbuf[6];
688 <
689 <        if (queuesiz >= PACKSIZ) {      /* flush queue */
690 <                if (fflush(psend) == EOF)
691 <                        syserror();
702 >        if (queuesiz >= PACKSIZ)        /* flush queue if needed */
703                  clearqueue();
704 <        }
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 */
704 >                                        /* add position to queue */
705          queue[queuesiz][0] = x;
706          queue[queuesiz][1] = y;
707          queuesiz++;
708   }
709  
710  
711 < clearqueue()                            /* get results from queue */
711 > clearqueue()                            /* process queue */
712   {
713 <        float   inbuf[4];
713 >        FVECT   orig, dir;
714 >        float   fbuf[6];
715          register int    i;
716  
717          for (i = 0; i < queuesiz; i++) {
718 <                if (fread(inbuf, sizeof(float), 4, precv) < 4) {
718 >                viewray(orig, dir, &ourview,
719 >                                (queue[i][0]+.5)/hresolu,
720 >                                (queue[i][1]+.5)/vresolu);
721 >                fbuf[0] = orig[0]; fbuf[1] = orig[1]; fbuf[2] = orig[2];
722 >                fbuf[3] = dir[0]; fbuf[4] = dir[1]; fbuf[5] = dir[2];
723 >                fwrite((char *)fbuf, sizeof(float), 6, psend);
724 >        }
725 >                                        /* flush output and get results */
726 >        fbuf[3] = fbuf[4] = fbuf[5] = 0.0;              /* mark */
727 >        fwrite((char *)fbuf, sizeof(float), 6, psend);
728 >        if (fflush(psend) == EOF)
729 >                syserror();
730 >        for (i = 0; i < queuesiz; i++) {
731 >                if (fread((char *)fbuf, sizeof(float), 4, precv) < 4) {
732                          fprintf(stderr, "%s: read error in clearqueue\n",
733                                          progname);
734                          exit(1);
735                  }
736 +                if (ourexp > 0 && ourexp != 1.0) {
737 +                        fbuf[0] *= ourexp;
738 +                        fbuf[1] *= ourexp;
739 +                        fbuf[2] *= ourexp;
740 +                }
741                  setcolr(pscan(queue[i][1])[queue[i][0]],
742 <                                inbuf[0], inbuf[1], inbuf[2]);
743 <                zscan(queue[i][1])[queue[i][0]] = inbuf[3];
742 >                                fbuf[0], fbuf[1], fbuf[2]);
743 >                zscan(queue[i][1])[queue[i][0]] = fbuf[3];
744          }
745          queuesiz = 0;
746   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines