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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines