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.12 by greg, Thu Jan 4 14:25:56 1990 UTC vs.
Revision 1.20 by greg, Tue Jan 16 16:10:57 1990 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include "standard.h"
12  
13 + #include <sys/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 > #define pscan(y)        (ourpict+(y)*hresolu)
20 > #define zscan(y)        (ourzbuf+(y)*hresolu)
21  
22 < #define F_FORE  1                       /* fill foreground */
23 < #define F_BACK  2                       /* fill background */
22 > #define F_FORE          1               /* fill foreground */
23 > #define F_BACK          2               /* fill background */
24  
25 + #define PACKSIZ         42              /* calculation packet size */
26 +
27 + #define RTCOM           "rtrace -h -ovl -fff -x %d %s"
28 +
29   #define ABS(x)          ((x)>0?(x):-(x))
30  
31 < VIEW    ourview = STDVIEW(512);         /* desired view */
31 > struct position {int x,y; float z;};
32  
33 + VIEW    ourview = STDVIEW;              /* desired view */
34 + int     hresolu = 512;                  /* horizontal resolution */
35 + int     vresolu = 512;                  /* vertical resolution */
36 + double  pixaspect = 1.0;                /* pixel aspect ratio */
37 +
38   double  zeps = .02;                     /* allowed z epsilon */
39  
40   COLR    *ourpict;                       /* output picture */
# Line 31 | Line 42 | float  *ourzbuf;                       /* corresponding z-buffer */
42  
43   char    *progname;
44  
34 VIEW    theirview = STDVIEW(512);       /* input view */
35 int     gotview;                        /* got input view? */
36
45   int     fill = F_FORE|F_BACK;           /* selected fill algorithm */
46 < extern int      backfill();             /* fill functions */
46 > extern int      backfill(), rcalfill(); /* fill functions */
47   int     (*deffill)() = backfill;        /* selected fill function */
48   COLR    backcolr = BLKCOLR;             /* background color */
49 + double  backz = 0.0;                    /* background z value */
50 + int     normdist = 1;                   /* normalized distance? */
51 + double  ourexp = -1;                    /* output picture exposure */
52  
53 + VIEW    theirview = STDVIEW;            /* input view */
54 + int     gotview;                        /* got input view? */
55 + int     thresolu, tvresolu;             /* input resolution */
56 + double  theirexp;                       /* input picture exposure */
57   double  theirs2ours[4][4];              /* transformation matrix */
43 int     normdist = 1;                   /* normalized distance? */
58  
59 + int     childpid = -1;                  /* id of fill process */
60 + FILE    *psend, *precv;                 /* pipes to/from fill calculation */
61 + int     queue[PACKSIZ][2];              /* pending pixels */
62 + int     queuesiz;                       /* number of pixels pending */
63  
64 +
65   main(argc, argv)                        /* interpolate pictures */
66   int     argc;
67   char    *argv[];
# Line 52 | Line 71 | char   *argv[];
71          int     gotvfile = 0;
72          char    *zfile = NULL;
73          char    *err;
74 <        int     i;
74 >        int     i, rval;
75  
76          progname = argv[0];
77  
78 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
78 >        for (i = 1; i < argc && argv[i][0] == '-'; i++) {
79 >                rval = getviewopt(&ourview, argc-i, argv+i);
80 >                if (rval >= 0) {
81 >                        i += rval;
82 >                        continue;
83 >                }
84                  switch (argv[i][1]) {
85                  case 't':                               /* threshold */
86                          check(2,1);
# Line 91 | Line 115 | char   *argv[];
115                                          atof(argv[i+2]), atof(argv[i+3]));
116                                  i += 3;
117                                  break;
118 +                        case 'z':                               /* z value */
119 +                                check(3,1);
120 +                                deffill = backfill;
121 +                                backz = atof(argv[++i]);
122 +                                break;
123 +                        case 'r':                               /* rtrace */
124 +                                check(3,1);
125 +                                deffill = rcalfill;
126 +                                calstart(RTCOM, argv[++i]);
127 +                                break;
128                          default:
129                                  goto badopt;
130                          }
# Line 101 | Line 135 | char   *argv[];
135                          break;
136                  case 'x':                               /* x resolution */
137                          check(2,1);
138 <                        ourview.hresolu = atoi(argv[++i]);
138 >                        hresolu = atoi(argv[++i]);
139                          break;
140                  case 'y':                               /* y resolution */
141                          check(2,1);
142 <                        ourview.vresolu = atoi(argv[++i]);
142 >                        vresolu = atoi(argv[++i]);
143                          break;
144 <                case 'v':                               /* view */
145 <                        switch (argv[i][2]) {
146 <                        case 't':                               /* type */
147 <                                check(4,0);
148 <                                ourview.type = argv[i][3];
149 <                                break;
116 <                        case 'p':                               /* point */
117 <                                check(3,3);
118 <                                ourview.vp[0] = atof(argv[++i]);
119 <                                ourview.vp[1] = atof(argv[++i]);
120 <                                ourview.vp[2] = atof(argv[++i]);
121 <                                break;
122 <                        case 'd':                               /* direction */
123 <                                check(3,3);
124 <                                ourview.vdir[0] = atof(argv[++i]);
125 <                                ourview.vdir[1] = atof(argv[++i]);
126 <                                ourview.vdir[2] = atof(argv[++i]);
127 <                                break;
128 <                        case 'u':                               /* up */
129 <                                check(3,3);
130 <                                ourview.vup[0] = atof(argv[++i]);
131 <                                ourview.vup[1] = atof(argv[++i]);
132 <                                ourview.vup[2] = atof(argv[++i]);
133 <                                break;
134 <                        case 'h':                               /* horizontal */
135 <                                check(3,1);
136 <                                ourview.horiz = atof(argv[++i]);
137 <                                break;
138 <                        case 'v':                               /* vertical */
139 <                                check(3,1);
140 <                                ourview.vert = atof(argv[++i]);
141 <                                break;
142 <                        case 'f':                               /* file */
143 <                                check(3,1);
144 <                                gotvfile = viewfile(argv[++i], &ourview);
145 <                                if (gotvfile < 0) {
146 <                                        perror(argv[i]);
147 <                                        exit(1);
148 <                                } else if (gotvfile == 0) {
149 <                                        fprintf(stderr, "%s: bad view file\n",
150 <                                                        argv[i]);
151 <                                        exit(1);
152 <                                }
153 <                                break;
154 <                        default:
144 >                case 'p':                               /* pixel aspect */
145 >                        check(2,1);
146 >                        pixaspect = atof(argv[++i]);
147 >                        break;
148 >                case 'v':                               /* view file */
149 >                        if (argv[i][2] != 'f')
150                                  goto badopt;
151 +                        check(3,1);
152 +                        gotvfile = viewfile(argv[++i], &ourview, 0, 0);
153 +                        if (gotvfile < 0) {
154 +                                perror(argv[i]);
155 +                                exit(1);
156 +                        } else if (gotvfile == 0) {
157 +                                fprintf(stderr, "%s: bad view file\n",
158 +                                                argv[i]);
159 +                                exit(1);
160                          }
161                          break;
162                  default:
# Line 161 | Line 165 | char   *argv[];
165                                          progname, argv[i]);
166                          goto userr;
167                  }
168 +        }
169                                                  /* check arguments */
170 <        if (argc-i < 2 || (argc-i)%2)
170 >        if ((argc-i)%2)
171                  goto userr;
172                                                  /* set view */
173          if (err = setview(&ourview)) {
174                  fprintf(stderr, "%s: %s\n", progname, err);
175                  exit(1);
176          }
177 +        normaspect(viewaspect(&ourview), &pixaspect, &hresolu, &vresolu);
178                                                  /* allocate frame */
179 <        ourpict = (COLR *)malloc(ourview.hresolu*ourview.vresolu*sizeof(COLR));
180 <        ourzbuf = (float *)calloc(ourview.hresolu*ourview.vresolu,sizeof(float));
181 <        if (ourpict == NULL || ourzbuf == NULL) {
182 <                perror(progname);
177 <                exit(1);
178 <        }
179 >        ourpict = (COLR *)malloc(hresolu*vresolu*sizeof(COLR));
180 >        ourzbuf = (float *)calloc(hresolu*vresolu,sizeof(float));
181 >        if (ourpict == NULL || ourzbuf == NULL)
182 >                syserror();
183                                                          /* get input */
184          for ( ; i < argc; i += 2)
185                  addpicture(argv[i], argv[i+1]);
# Line 184 | Line 188 | char   *argv[];
188                  backpicture();
189          else
190                  fillpicture();
191 +                                                        /* close calculation */
192 +        caldone();
193                                                          /* add to header */
194          printargs(argc, argv, stdout);
195          if (gotvfile) {
# Line 191 | Line 197 | char   *argv[];
197                  fprintview(&ourview, stdout);
198                  printf("\n");
199          }
200 +        if (pixaspect < .99 || pixaspect > 1.01)
201 +                fputaspect(pixaspect, stdout);
202 +        if (ourexp > 0 && (ourexp < .995 || ourexp > 1.005))
203 +                fputexpos(ourexp, stdout);
204          printf("\n");
205                                                          /* write picture */
206          writepicture();
# Line 201 | Line 211 | char   *argv[];
211          exit(0);
212   userr:
213          fprintf(stderr,
214 <        "Usage: %s [view opts][-t zthresh][-z zout][-fT][-n] pfile zspec ..\n",
214 >        "Usage: %s [view opts][-t eps][-z zout][-fT][-n] pfile zspec ..\n",
215                          progname);
216          exit(1);
217   #undef check
# Line 216 | Line 226 | char   *s;
226  
227          printf("\t%s", s);
228  
229 +        if (isexpos(s)) {
230 +                theirexp *= exposval(s);
231 +                return;
232 +        }
233          for (an = altname; *an != NULL; an++)
234                  if (!strncmp(*an, s, strlen(*an))) {
235 <                        if (sscanview(&theirview, s+strlen(*an)) == 0)
235 >                        if (sscanview(&theirview, s+strlen(*an)) > 0)
236                                  gotview++;
237                          break;
238                  }
# Line 229 | Line 243 | addpicture(pfile, zspec)               /* add picture to output */
243   char    *pfile, *zspec;
244   {
245          extern double   atof();
246 <        FILE    *pfp, *zfp;
246 >        FILE    *pfp;
247 >        int     zfd;
248          char    *err;
249          COLR    *scanin;
250 <        float   *zin, *zlast;
251 <        int     *plast;
250 >        float   *zin;
251 >        struct position *plast;
252          int     y;
253                                          /* open picture file */
254          if ((pfp = fopen(pfile, "r")) == NULL) {
255                  perror(pfile);
256                  exit(1);
257          }
258 <                                        /* get header and view */
259 <        printf("%s:\n", pfile);
258 >                                        /* get header with exposure and view */
259 >        theirexp = 1.0;
260          gotview = 0;
261 +        printf("%s:\n", pfile);
262          getheader(pfp, headline);
263 <        if (!gotview || fgetresolu(&theirview.hresolu, &theirview.vresolu, pfp)
263 >        if (!gotview || fgetresolu(&thresolu, &tvresolu, pfp)
264                          != (YMAJOR|YDECR)) {
265                  fprintf(stderr, "%s: picture view error\n", pfile);
266                  exit(1);
267          }
268 +        if (ourexp <= 0)
269 +                ourexp = theirexp;
270 +        else if (ABS(theirexp-ourexp) > .01*ourexp)
271 +                fprintf(stderr, "%s: different exposure (warning)\n", pfile);
272          if (err = setview(&theirview)) {
273                  fprintf(stderr, "%s: %s\n", pfile, err);
274                  exit(1);
# Line 256 | Line 276 | char   *pfile, *zspec;
276                                          /* compute transformation */
277          pixform(theirs2ours, &theirview, &ourview);
278                                          /* allocate scanlines */
279 <        scanin = (COLR *)malloc(theirview.hresolu*sizeof(COLR));
280 <        zin = (float *)malloc(theirview.hresolu*sizeof(float));
281 <        plast = (int *)calloc(theirview.hresolu, sizeof(int));
282 <        zlast = (float *)calloc(theirview.hresolu, sizeof(float));
283 <        if (scanin == NULL || zin == NULL || plast == NULL || zlast == NULL) {
264 <                perror(progname);
265 <                exit(1);
266 <        }
279 >        scanin = (COLR *)malloc(thresolu*sizeof(COLR));
280 >        zin = (float *)malloc(thresolu*sizeof(float));
281 >        plast = (struct position *)calloc(thresolu, sizeof(struct position));
282 >        if (scanin == NULL || zin == NULL || plast == NULL)
283 >                syserror();
284                                          /* get z specification or file */
285 <        if ((zfp = fopen(zspec, "r")) == NULL) {
285 >        if ((zfd = open(zspec, O_RDONLY)) == -1) {
286                  double  zvalue;
287                  register int    x;
288                  if (!isfloat(zspec) || (zvalue = atof(zspec)) <= 0.0) {
289                          perror(zspec);
290                          exit(1);
291                  }
292 <                for (x = 0; x < theirview.hresolu; x++)
292 >                for (x = 0; x < thresolu; x++)
293                          zin[x] = zvalue;
294          }
295                                          /* load image */
296 <        for (y = theirview.vresolu-1; y >= 0; y--) {
297 <                if (freadcolrs(scanin, theirview.hresolu, pfp) < 0) {
296 >        for (y = tvresolu-1; y >= 0; y--) {
297 >                if (freadcolrs(scanin, thresolu, pfp) < 0) {
298                          fprintf(stderr, "%s: read error\n", pfile);
299                          exit(1);
300                  }
301 <                if (zfp != NULL
302 <                        && fread(zin,sizeof(float),theirview.hresolu,zfp)
286 <                                < theirview.hresolu) {
301 >                if (zfd != -1 && read(zfd,zin,thresolu*sizeof(float))
302 >                                < thresolu*sizeof(float)) {
303                          fprintf(stderr, "%s: read error\n", zspec);
304                          exit(1);
305                  }
306 <                addscanline(y, scanin, zin, plast, zlast);
306 >                addscanline(y, scanin, zin, plast);
307          }
308                                          /* clean up */
309          free((char *)scanin);
310          free((char *)zin);
311          free((char *)plast);
296        free((char *)zlast);
312          fclose(pfp);
313 <        if (zfp != NULL)
314 <                fclose(zfp);
313 >        if (zfd != -1)
314 >                close(zfd);
315   }
316  
317  
# Line 307 | Line 322 | register VIEW  *vw1, *vw2;
322          double  m4t[4][4];
323  
324          setident4(xfmat);
325 <        xfmat[0][0] = vw1->vhinc[0];
326 <        xfmat[0][1] = vw1->vhinc[1];
327 <        xfmat[0][2] = vw1->vhinc[2];
328 <        xfmat[1][0] = vw1->vvinc[0];
329 <        xfmat[1][1] = vw1->vvinc[1];
330 <        xfmat[1][2] = vw1->vvinc[2];
325 >        xfmat[0][0] = vw1->hvec[0];
326 >        xfmat[0][1] = vw1->hvec[1];
327 >        xfmat[0][2] = vw1->hvec[2];
328 >        xfmat[1][0] = vw1->vvec[0];
329 >        xfmat[1][1] = vw1->vvec[1];
330 >        xfmat[1][2] = vw1->vvec[2];
331          xfmat[2][0] = vw1->vdir[0];
332          xfmat[2][1] = vw1->vdir[1];
333          xfmat[2][2] = vw1->vdir[2];
# Line 320 | Line 335 | register VIEW  *vw1, *vw2;
335          xfmat[3][1] = vw1->vp[1];
336          xfmat[3][2] = vw1->vp[2];
337          setident4(m4t);
338 <        m4t[0][0] = vw2->vhinc[0]/vw2->vhn2;
339 <        m4t[1][0] = vw2->vhinc[1]/vw2->vhn2;
340 <        m4t[2][0] = vw2->vhinc[2]/vw2->vhn2;
341 <        m4t[3][0] = -DOT(vw2->vp,vw2->vhinc)/vw2->vhn2;
342 <        m4t[0][1] = vw2->vvinc[0]/vw2->vvn2;
343 <        m4t[1][1] = vw2->vvinc[1]/vw2->vvn2;
344 <        m4t[2][1] = vw2->vvinc[2]/vw2->vvn2;
345 <        m4t[3][1] = -DOT(vw2->vp,vw2->vvinc)/vw2->vvn2;
338 >        m4t[0][0] = vw2->hvec[0]/vw2->hn2;
339 >        m4t[1][0] = vw2->hvec[1]/vw2->hn2;
340 >        m4t[2][0] = vw2->hvec[2]/vw2->hn2;
341 >        m4t[3][0] = -DOT(vw2->vp,vw2->hvec)/vw2->hn2;
342 >        m4t[0][1] = vw2->vvec[0]/vw2->vn2;
343 >        m4t[1][1] = vw2->vvec[1]/vw2->vn2;
344 >        m4t[2][1] = vw2->vvec[2]/vw2->vn2;
345 >        m4t[3][1] = -DOT(vw2->vp,vw2->vvec)/vw2->vn2;
346          m4t[0][2] = vw2->vdir[0];
347          m4t[1][2] = vw2->vdir[1];
348          m4t[2][2] = vw2->vdir[2];
# Line 336 | Line 351 | register VIEW  *vw1, *vw2;
351   }
352  
353  
354 < addscanline(y, pline, zline, lasty, lastyz)     /* add scanline to output */
354 > addscanline(y, pline, zline, lasty)     /* add scanline to output */
355   int     y;
356   COLR    *pline;
357   float   *zline;
358 < int     *lasty;                 /* input/output */
344 < float   *lastyz;                /* input/output */
358 > struct position *lasty;         /* input/output */
359   {
360          extern double   sqrt();
361          double  pos[3];
362 <        int     lastx = 0;
349 <        double  lastxz = 0;
350 <        double  zt;
351 <        int     xpos, ypos;
362 >        struct position lastx, newpos;
363          register int    x;
364  
365 <        for (x = theirview.hresolu-1; x >= 0; x--) {
366 <                pos[0] = x - .5*(theirview.hresolu-1);
367 <                pos[1] = y - .5*(theirview.vresolu-1);
365 >        lastx.z = 0;
366 >        for (x = thresolu-1; x >= 0; x--) {
367 >                pos[0] = (x+.5)/thresolu + theirview.hoff - .5;
368 >                pos[1] = (y+.5)/tvresolu + theirview.voff - .5;
369                  pos[2] = zline[x];
370                  if (theirview.type == VT_PER) {
371                          if (normdist)   /* adjust for eye-ray distance */
372                                  pos[2] /= sqrt( 1.
373 <                                        + pos[0]*pos[0]*theirview.vhn2
374 <                                        + pos[1]*pos[1]*theirview.vvn2 );
373 >                                        + pos[0]*pos[0]*theirview.hn2
374 >                                        + pos[1]*pos[1]*theirview.vn2 );
375                          pos[0] *= pos[2];
376                          pos[1] *= pos[2];
377                  }
378                  multp3(pos, pos, theirs2ours);
379 <                if (pos[2] <= 0)
379 >                if (pos[2] <= 0) {
380 >                        lasty[x].z = lastx.z = 0;       /* mark invalid */
381                          continue;
382 +                }
383                  if (ourview.type == VT_PER) {
384                          pos[0] /= pos[2];
385                          pos[1] /= pos[2];
386                  }
387 <                pos[0] += .5*ourview.hresolu;
388 <                pos[1] += .5*ourview.vresolu;
389 <                if (pos[0] < 0 || (xpos = pos[0]) >= ourview.hresolu
390 <                        || pos[1] < 0 || (ypos = pos[1]) >= ourview.vresolu)
391 <                        continue;
387 >                pos[0] += .5 - ourview.hoff;
388 >                pos[1] += .5 - ourview.voff;
389 >                newpos.x = pos[0] * hresolu;
390 >                newpos.y = pos[1] * vresolu;
391 >                newpos.z = zline[x];
392                                          /* add pixel to our image */
393 <                zt = 2.*zeps*zline[x];
394 <                addpixel(xpos, ypos,
395 <                        (fill&F_FORE && ABS(zline[x]-lastxz) <= zt)
396 <                                ? lastx - xpos : 1,
397 <                        (fill&F_FORE && ABS(zline[x]-lastyz[x]) <= zt)
398 <                                ? lasty[x] - ypos : 1,
399 <                        pline[x], pos[2]);
400 <                lastx = xpos;
387 <                lasty[x] = ypos;
388 <                lastxz = lastyz[x] = zline[x];
393 >                if (pos[0] >= 0 && newpos.x < hresolu
394 >                                && pos[1] >= 0 && newpos.y < vresolu) {
395 >                        addpixel(&newpos, &lastx, &lasty[x], pline[x], pos[2]);
396 >                        lasty[x].x = lastx.x = newpos.x;
397 >                        lasty[x].y = lastx.y = newpos.y;
398 >                        lasty[x].z = lastx.z = newpos.z;
399 >                } else
400 >                        lasty[x].z = lastx.z = 0;       /* mark invalid */
401          }
402   }
403  
404  
405 < addpixel(xstart, ystart, width, height, pix, z) /* fill in area for pixel */
406 < int     xstart, ystart;
395 < int     width, height;
405 > addpixel(p0, p1, p2, pix, z)            /* fill in pixel parallelogram */
406 > struct position *p0, *p1, *p2;
407   COLR    pix;
408   double  z;
409   {
410 <        register int    x, y;
411 <                                        /* make width and height positive */
412 <        if (width < 0) {
413 <                width = -width;
414 <                xstart = xstart-width+1;
415 <        } else if (width == 0)
416 <                width = 1;
417 <        if (height < 0) {
418 <                height = -height;
419 <                ystart = ystart-height+1;
420 <        } else if (height == 0)
421 <                height = 1;
422 <                                        /* fill pixel(s) within rectangle */
423 <        for (y = ystart; y < ystart+height; y++)
424 <                for (x = xstart; x < xstart+width; x++)
425 <                        if (zscan(y)[x] <= 0
426 <                                        || zscan(y)[x]-z > zeps*zscan(y)[x]) {
410 >        double  zt = 2.*zeps*p0->z;             /* threshold */
411 >        int     s1x, s1y, s2x, s2y;             /* step sizes */
412 >        int     l1, l2, c1, c2;                 /* side lengths and counters */
413 >        int     p1isy;                          /* p0p1 along y? */
414 >        int     x1, y1;                         /* p1 position */
415 >        register int    x, y;                   /* final position */
416 >
417 >                                        /* compute vector p0p1 */
418 >        if (fill&F_FORE && ABS(p1->z-p0->z) <= zt) {
419 >                s1x = p1->x - p0->x;
420 >                s1y = p1->y - p0->y;
421 >                l1 = ABS(s1x);
422 >                if (p1isy = (ABS(s1y) > l1))
423 >                        l1 = ABS(s1y);
424 >        } else {
425 >                l1 = s1x = s1y = 1;
426 >                p1isy = -1;
427 >        }
428 >                                        /* compute vector p0p2 */
429 >        if (fill&F_FORE && ABS(p2->z-p0->z) <= zt) {
430 >                s2x = p2->x - p0->x;
431 >                s2y = p2->y - p0->y;
432 >                if (p1isy == 1)
433 >                        l2 = ABS(s2x);
434 >                else {
435 >                        l2 = ABS(s2y);
436 >                        if (p1isy != 0 && ABS(s2x) > l2)
437 >                                l2 = ABS(s2x);
438 >                }
439 >        } else
440 >                l2 = s2x = s2y = 1;
441 >                                        /* fill the parallelogram */
442 >        for (c1 = l1; c1-- > 0; ) {
443 >                x1 = p0->x + c1*s1x/l1;
444 >                y1 = p0->y + c1*s1y/l1;
445 >                for (c2 = l2; c2-- > 0; ) {
446 >                        x = x1 + c2*s2x/l2;
447 >                        y = y1 + c2*s2y/l2;
448 >                        if (zscan(y)[x] <= 0 || zscan(y)[x]-z
449 >                                                > zeps*zscan(y)[x]) {
450                                  zscan(y)[x] = z;
451                                  copycolr(pscan(y)[x], pix);
452                          }
453 +                }
454 +        }
455   }
456  
457  
# Line 426 | Line 462 | backpicture()                          /* background fill algorithm */
462          COLR    pfill;
463          register int    x, i;
464                                                          /* get back buffer */
465 <        yback = (int *)malloc(ourview.hresolu*sizeof(int));
466 <        if (yback == NULL) {
467 <                perror(progname);
468 <                return;
433 <        }
434 <        for (x = 0; x < ourview.hresolu; x++)
465 >        yback = (int *)malloc(hresolu*sizeof(int));
466 >        if (yback == NULL)
467 >                syserror();
468 >        for (x = 0; x < hresolu; x++)
469                  yback[x] = -2;
470          /*
471           * Xback and yback are the pixel locations of suitable
# Line 440 | Line 474 | backpicture()                          /* background fill algorithm */
474           * that there is no suitable background in this direction.
475           */
476                                                          /* fill image */
477 <        for (y = 0; y < ourview.vresolu; y++) {
477 >        for (y = 0; y < vresolu; y++) {
478                  xback = -2;
479 <                for (x = 0; x < ourview.hresolu; x++)
479 >                for (x = 0; x < hresolu; x++)
480                          if (zscan(y)[x] <= 0) {         /* empty pixel */
481                                  /*
482                                   * First, find background from above or below.
483                                   * (farthest assigned pixel)
484                                   */
485                                  if (yback[x] == -2) {
486 <                                        for (i = y+1; i < ourview.vresolu; i++)
486 >                                        for (i = y+1; i < vresolu; i++)
487                                                  if (zscan(i)[x] > 0)
488                                                          break;
489 <                                        if (i < ourview.vresolu
489 >                                        if (i < vresolu
490                                  && (y <= 0 || zscan(y-1)[x] < zscan(i)[x]))
491                                                  yback[x] = i;
492                                          else
# Line 462 | Line 496 | backpicture()                          /* background fill algorithm */
496                                   * Next, find background from left or right.
497                                   */
498                                  if (xback == -2) {
499 <                                        for (i = x+1; i < ourview.hresolu; i++)
499 >                                        for (i = x+1; i < hresolu; i++)
500                                                  if (zscan(y)[i] > 0)
501                                                          break;
502 <                                        if (i < ourview.hresolu
502 >                                        if (i < hresolu
503                                  && (x <= 0 || zscan(y)[x-1] < zscan(y)[i]))
504                                                  xback = i;
505                                          else
# Line 487 | Line 521 | backpicture()                          /* background fill algorithm */
521                                          || (xback >= 0 && ABS(x-xback) <= 1)
522                                          || ( ABS(y-yback[x]) > 1
523                                                  && zscan(yback[x])[x]
524 <                                                < zscan(y)[xback] ) )
524 >                                                < zscan(y)[xback] ) ) {
525                                          copycolr(pscan(y)[x],pscan(y)[xback]);
526 <                                else
526 >                                        zscan(y)[x] = zscan(y)[xback];
527 >                                } else {
528                                          copycolr(pscan(y)[x],pscan(yback[x])[x]);
529 +                                        zscan(y)[x] = zscan(yback[x])[x];
530 +                                }
531                          } else {                                /* full pixel */
532                                  yback[x] = -2;
533                                  xback = -2;
# Line 504 | Line 541 | fillpicture()                          /* paint in empty pixels with default
541   {
542          register int    x, y;
543  
544 <        for (y = 0; y < ourview.vresolu; y++)
545 <                for (x = 0; x < ourview.hresolu; x++)
544 >        for (y = 0; y < vresolu; y++)
545 >                for (x = 0; x < hresolu; x++)
546                          if (zscan(y)[x] <= 0)
547                                  (*deffill)(x,y);
548   }
# Line 515 | Line 552 | writepicture()                         /* write out picture */
552   {
553          int     y;
554  
555 <        fputresolu(YMAJOR|YDECR, ourview.hresolu, ourview.vresolu, stdout);
556 <        for (y = ourview.vresolu-1; y >= 0; y--)
557 <                if (fwritecolrs(pscan(y), ourview.hresolu, stdout) < 0) {
558 <                        perror(progname);
522 <                        exit(1);
523 <                }
555 >        fputresolu(YMAJOR|YDECR, hresolu, vresolu, stdout);
556 >        for (y = vresolu-1; y >= 0; y--)
557 >                if (fwritecolrs(pscan(y), hresolu, stdout) < 0)
558 >                        syserror();
559   }
560  
561  
# Line 529 | Line 564 | char   *fname;
564   {
565          extern double   sqrt();
566          int     donorm = normdist && ourview.type == VT_PER;
567 <        FILE    *fp;
567 >        int     fd;
568          int     y;
569          float   *zout;
570  
571 <        if ((fp = fopen(fname, "w")) == NULL) {
571 >        if ((fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
572                  perror(fname);
573                  exit(1);
574          }
575          if (donorm
576 <        && (zout = (float *)malloc(ourview.hresolu*sizeof(float))) == NULL) {
577 <                perror(progname);
578 <                exit(1);
544 <        }
545 <        for (y = ourview.vresolu-1; y >= 0; y--) {
576 >        && (zout = (float *)malloc(hresolu*sizeof(float))) == NULL)
577 >                syserror();
578 >        for (y = vresolu-1; y >= 0; y--) {
579                  if (donorm) {
580                          double  vx, yzn2;
581                          register int    x;
582 <                        yzn2 = y - .5*(ourview.vresolu-1);
583 <                        yzn2 = 1. + yzn2*yzn2*ourview.vvn2;
584 <                        for (x = 0; x < ourview.hresolu; x++) {
585 <                                vx = x - .5*(ourview.hresolu-1);
582 >                        yzn2 = y - .5*(vresolu-1);
583 >                        yzn2 = 1. + yzn2*yzn2*ourview.vn2;
584 >                        for (x = 0; x < hresolu; x++) {
585 >                                vx = x - .5*(hresolu-1);
586                                  zout[x] = zscan(y)[x]
587 <                                        * sqrt(vx*vx*ourview.vhn2 + yzn2);
587 >                                        * sqrt(vx*vx*ourview.hn2 + yzn2);
588                          }
589                  } else
590                          zout = zscan(y);
591 <                if (fwrite(zout, sizeof(float), ourview.hresolu, fp)
592 <                                < ourview.hresolu) {
591 >                if (write(fd, zout, hresolu*sizeof(float))
592 >                                < hresolu*sizeof(float)) {
593                          perror(fname);
594                          exit(1);
595                  }
596          }
597          if (donorm)
598                  free((char *)zout);
599 <        fclose(fp);
599 >        close(fd);
600   }
601  
602  
# Line 584 | Line 617 | int    x, y;
617          register BYTE   *dest = pscan(y)[x];
618  
619          copycolr(dest, backcolr);
620 +        zscan(y)[x] = backz;
621 + }
622 +
623 +
624 + calstart(prog, args)                    /* start fill calculation */
625 + char    *prog, *args;
626 + {
627 +        char    combuf[512];
628 +        int     p0[2], p1[2];
629 +
630 +        if (childpid != -1) {
631 +                fprintf(stderr, "%s: too many calculations\n", progname);
632 +                exit(1);
633 +        }
634 +        sprintf(combuf, prog, PACKSIZ, args);
635 +        if (pipe(p0) < 0 || pipe(p1) < 0)
636 +                syserror();
637 +        if ((childpid = vfork()) == 0) {        /* fork calculation */
638 +                close(p0[1]);
639 +                close(p1[0]);
640 +                if (p0[0] != 0) {
641 +                        dup2(p0[0], 0);
642 +                        close(p0[0]);
643 +                }
644 +                if (p1[1] != 1) {
645 +                        dup2(p1[1], 1);
646 +                        close(p1[1]);
647 +                }
648 +                execl("/bin/sh", "sh", "-c", combuf, 0);
649 +                perror("/bin/sh");
650 +                _exit(127);
651 +        }
652 +        if (childpid == -1)
653 +                syserror();
654 +        close(p0[0]);
655 +        close(p1[1]);
656 +        if ((psend = fdopen(p0[1], "w")) == NULL)
657 +                syserror();
658 +        if ((precv = fdopen(p1[0], "r")) == NULL)
659 +                syserror();
660 +        queuesiz = 0;
661 + }
662 +
663 +
664 + caldone()                               /* done with calculation */
665 + {
666 +        int     pid;
667 +
668 +        if (childpid == -1)
669 +                return;
670 +        if (fclose(psend) == EOF)
671 +                syserror();
672 +        clearqueue();
673 +        fclose(precv);
674 +        while ((pid = wait(0)) != -1 && pid != childpid)
675 +                ;
676 +        childpid = -1;
677 + }
678 +
679 +
680 + rcalfill(x, y)                          /* fill with ray-calculated pixel */
681 + int     x, y;
682 + {
683 +        FVECT   orig, dir;
684 +        float   outbuf[6];
685 +
686 +        if (queuesiz >= PACKSIZ) {      /* flush queue */
687 +                if (fflush(psend) == EOF)
688 +                        syserror();
689 +                clearqueue();
690 +        }
691 +                                        /* send new ray */
692 +        viewray(orig, dir, &ourview, (x+.5)/hresolu, (y+.5)/vresolu);
693 +        outbuf[0] = orig[0]; outbuf[1] = orig[1]; outbuf[2] = orig[2];
694 +        outbuf[3] = dir[0]; outbuf[4] = dir[1]; outbuf[5] = dir[2];
695 +        if (fwrite(outbuf, sizeof(float), 6, psend) < 6)
696 +                syserror();
697 +                                        /* remember it */
698 +        queue[queuesiz][0] = x;
699 +        queue[queuesiz][1] = y;
700 +        queuesiz++;
701 + }
702 +
703 +
704 + clearqueue()                            /* get results from queue */
705 + {
706 +        float   inbuf[4];
707 +        register int    i;
708 +
709 +        for (i = 0; i < queuesiz; i++) {
710 +                if (fread(inbuf, sizeof(float), 4, precv) < 4) {
711 +                        fprintf(stderr, "%s: read error in clearqueue\n",
712 +                                        progname);
713 +                        exit(1);
714 +                }
715 +                if (ourexp > 0 && ourexp != 1.0) {
716 +                        inbuf[0] *= ourexp;
717 +                        inbuf[1] *= ourexp;
718 +                        inbuf[2] *= ourexp;
719 +                }
720 +                setcolr(pscan(queue[i][1])[queue[i][0]],
721 +                                inbuf[0], inbuf[1], inbuf[2]);
722 +                zscan(queue[i][1])[queue[i][0]] = inbuf[3];
723 +        }
724 +        queuesiz = 0;
725 + }
726 +
727 +
728 + syserror()                      /* report error and exit */
729 + {
730 +        perror(progname);
731 +        exit(1);
732   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines