ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rpiece.c
(Generate patch)

Comparing ray/src/util/rpiece.c (file contents):
Revision 2.5 by greg, Fri Aug 7 11:05:30 1992 UTC vs.
Revision 2.14 by greg, Wed Dec 2 17:07:00 1992 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include "standard.h"
12   #include <fcntl.h>
13 + #include <signal.h>
14   #include "color.h"
15   #include "view.h"
16   #include "resolu.h"
17  
18 + #ifndef NFS
19 + #define  NFS                    1
20 + #endif
21 +                                /* set the following to 0 to forgo forking */
22 + #ifndef MAXFORK
23 + #if NFS
24 + #define  MAXFORK                3       /* allotment of duped processes */
25 + #else
26 + #define  MAXFORK                0
27 + #endif
28 + #endif
29 +
30                                  /* rpict command */
31   char  *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"};
32   int  rpargc = 9;
33   int  rpd[3];
34   FILE  *torp, *fromrp;
35 < COLR  *scanline;
35 > COLR  *pbuf;
36                                  /* our view parameters */
37   VIEW  ourview = STDVIEW;
38   double  pixaspect = 1.0;
# Line 29 | Line 42 | char  *outfile = NULL;
42   int  outfd;
43   long  scanorig;
44   int  syncfd = -1;               /* lock file descriptor */
45 + int  nforked = 0;
46  
47   char  *progname;
48   int  verbose = 0;
49  
50   extern long  lseek(), ftell();
51  
52 + int  gotalrm = 0;
53 + int  onalrm() { gotalrm++; }
54  
55 +
56   main(argc, argv)
57   int  argc;
58   char  *argv[];
# Line 92 | Line 109 | char  *argv[];
109                          case 'F':               /* syncronization file */
110                                  if (argv[i][2])
111                                          break;
112 <                                if ((syncfd = open(argv[++i], O_RDWR)) < 0) {
112 >                                if ((syncfd = open(argv[++i],
113 >                                                O_RDWR|O_CREAT, 0666)) < 0) {
114                                          fprintf(stderr, "%s: cannot open\n",
115                                                          argv[i]);
116                                          exit(1);
# Line 113 | Line 131 | char  *argv[];
131          }
132          init(argc, argv);
133          rpiece();
134 <        rval = cleanup();
134 >        rval = cleanup(0);
135          exit(rval);
136   }
137  
# Line 154 | Line 172 | char  **av;
172          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
173                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
174                          goto filerr;
175 <                getheader(fp, NULL);            /* skip header */
175 >                getheader(fp, NULL, NULL);      /* skip header */
176                  if (fscnresolu(&hr, &vr, fp) < 0 ||     /* check resolution */
177                                  hr != hres*hmult || vr != vres*vmult) {
178 <                        fprintf(stderr, "%s: picture resolution mismatch\n",
179 <                                        outfile);
178 >                        fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
179 >                                        progname, outfile);
180                          exit(1);
181                  }
182          } else {
183 <                fprintf(stderr, "%s: cannot open\n", outfile);
183 >                fprintf(stderr, "%s: cannot open file \"%s\"\n",
184 >                                progname, outfile);
185                  exit(1);
186          }
187          scanorig = ftell(fp);           /* record position of first scanline */
188          if (fclose(fp) == -1)           /* done with stream i/o */
189                  goto filerr;
190 <        sync();                         /* avoid NFS buffering */
190 > #if NFS
191 >        sync();                         /* flush NFS buffers */
192 > #endif
193                                          /* start rpict process */
194          if (open_process(rpd, rpargv) <= 0) {
195                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
# Line 180 | Line 201 | char  **av;
201                                  progname, rpargv[0]);
202                  exit(1);
203          }
204 <        if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) {
204 >        if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) {
205                  fprintf(stderr, "%s: out of memory\n", progname);
206                  exit(1);
207          }
208 +        signal(SIGALRM, onalrm);
209          return;
210   filerr:
211 <        fprintf(stderr, "%s: file i/o error\n", outfile);
211 >        fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
212          exit(1);
213   }
214  
# Line 199 | Line 221 | int  *xp, *yp;
221          struct flock  fls;
222          char  buf[64];
223  
224 <        if (syncfd != -1) {             /* using sync file */
224 >        if (gotalrm)                    /* someone wants us to quit */
225 >                return(0);
226 >        if (syncfd != -1) {             /* use sync file */
227                  fls.l_type = F_WRLCK;           /* gain exclusive access */
228                  fls.l_whence = 0;
229                  fls.l_start = 0L;
# Line 208 | Line 232 | int  *xp, *yp;
232                  lseek(syncfd, 0L, 0);
233                  buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
234                  if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
211                        *xp = hmult;
212                        *yp = vmult-1;
213                }
214                if (--(*xp) < 0) {              /* decrement position */
235                          *xp = hmult-1;
236 <                        if (--(*yp) < 0) {      /* all done! */
236 >                        *yp = vmult;
237 >                }
238 >                if (--(*yp) < 0) {              /* decrement position */
239 >                        *yp = vmult-1;
240 >                        if (--(*xp) < 0) {      /* all done! */
241                                  close(syncfd);
242                                  return(0);
243                          }
# Line 225 | Line 249 | int  *xp, *yp;
249                  fcntl(syncfd, F_SETLKW, &fls);
250                  return(1);
251          }
252 <        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* using stdin */
252 >        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* use stdin */
253                  return(0);
254          if (sscanf(buf, "%d %d", xp, yp) == 2)
255                  return(1);
256          fprintf(stderr, "%s: input format error\n", progname);
257 <        exit(1);
257 >        exit(cleanup(1));
258   }
259  
260  
261   int
262 < cleanup()                       /* close rpict process and clean up */
262 > cleanup(rstat)                  /* close rpict process and clean up */
263 > int  rstat;
264   {
265 <        register int  rpstat;
265 >        int  status;
266  
267 <        free((char *)scanline);
267 >        free((char *)pbuf);
268          fclose(torp);
269          fclose(fromrp);
270 <        rpstat = close_process(rpd);
271 <        if (rpstat == -1)
272 <                rpstat = 0;
273 <        return(rpstat);
270 >        while (wait(&status) != -1)
271 >                if (rstat == 0)
272 >                        rstat = status>>8 & 0xff;
273 >        return(rstat);
274   }
275  
276  
# Line 253 | Line 278 | rpiece()                       /* render picture piece by piece */
278   {
279          VIEW  pview;
280          int  xorg, yorg;
281 <        
281 >                                        /* compute view parameters */
282 >        copystruct(&pview, &ourview);
283 >        switch (ourview.type) {
284 >        case VT_PER:
285 >                pview.horiz = 2.*180./PI*atan(
286 >                                tan(PI/180./2.*ourview.horiz)/hmult );
287 >                pview.vert = 2.*180./PI*atan(
288 >                                tan(PI/180./2.*ourview.vert)/vmult );
289 >                break;
290 >        case VT_PAR:
291 >        case VT_ANG:
292 >                pview.horiz = ourview.horiz / hmult;
293 >                pview.vert = ourview.vert / vmult;
294 >                break;
295 >        case VT_HEM:
296 >                pview.horiz = 2.*180./PI*asin(
297 >                                sin(PI/180./2.*ourview.horiz)/hmult );
298 >                pview.vert = 2.*180./PI*asin(
299 >                                sin(PI/180./2.*ourview.vert)/vmult );
300 >                break;
301 >        default:
302 >                fprintf(stderr, "%s: unknown view type '-vt%c'\n",
303 >                                progname, ourview.type);
304 >                exit(cleanup(1));
305 >        }
306 >                                        /* render each piece */
307          while (nextpiece(&xorg, &yorg)) {
308 <                copystruct(&pview, &ourview);   /* compute view for piece */
309 <                switch (ourview.type) {
260 <                case VT_PER:
261 <                        pview.horiz = 2.*180./PI*atan(
262 <                                        tan(PI/180./2.*ourview.horiz)/hmult );
263 <                        pview.vert = 2.*180./PI*atan(
264 <                                        tan(PI/180./2.*ourview.vert)/vmult );
265 <                        break;
266 <                case VT_PAR:
267 <                case VT_ANG:
268 <                        pview.horiz = ourview.horiz / hmult;
269 <                        pview.vert = ourview.vert / vmult;
270 <                        break;
271 <                case VT_HEM:
272 <                        pview.horiz = 2.*180./PI*asin(
273 <                                        sin(PI/180./2.*ourview.horiz)/hmult );
274 <                        pview.vert = 2.*180./PI*asin(
275 <                                        sin(PI/180./2.*ourview.vert)/vmult );
276 <                        break;
277 <                default:
278 <                        fprintf(stderr, "%s: unknown view type '-vt%c'\n",
279 <                                        progname, ourview.type);
280 <                        exit(1);
281 <                }
282 <                pview.hoff += xorg - 0.5*(hmult-1);
283 <                pview.voff += yorg - 0.5*(vmult-1);
308 >                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
309 >                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
310                  fputs(VIEWSTR, torp);
311                  fprintview(&pview, torp);
312                  putc('\n', torp);
313 <                fflush(torp);                   /* assign piece to rpict */
313 >                fflush(torp);                   /* assigns piece to rpict */
314                  putpiece(xorg, yorg);           /* place piece in output */
315                  if (verbose) {                  /* notify caller */
316                          printf("%d %d done\n", xorg, yorg);
# Line 294 | Line 320 | rpiece()                       /* render picture piece by piece */
320   }
321  
322  
323 + int
324   putpiece(xpos, ypos)            /* get next piece from rpict */
325   int  xpos, ypos;
326   {
327 +        struct flock  fls;
328 +        int  pid, status;
329          int  hr, vr;
330 <        int  y;
331 <
330 >        register int  y;
331 >                                /* check bounds */
332          if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
333                  fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
334                                  progname, xpos, ypos);
335 <                exit(1);
335 >                exit(cleanup(1));
336          }
337 <        getheader(fromrp, NULL);        /* discard header info. */
338 <        if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */
339 <                        hr != hres || vr != vres) {
337 >                                /* check header from rpict */
338 >        getheader(fromrp, NULL, NULL);
339 >        if (fscnresolu(&hr, &vr, fromrp) < 0 || hr != hres | vr != vres) {
340                  fprintf(stderr, "%s: resolution mismatch from %s\n",
341                                  progname, rpargv[0]);
342 <                exit(1);
342 >                exit(cleanup(1));
343          }
344 <        for (y = 0; y < vr; y++) {      /* transfer scanlines */
345 <                if (freadcolrs(scanline, hr, fromrp) < 0) {
344 >                                /* load new piece into buffer */
345 >        for (y = 0; y < vr; y++)
346 >                if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
347                          fprintf(stderr, "%s: read error from %s\n",
348                                          progname, rpargv[0]);
349 <                        exit(1);
349 >                        exit(cleanup(1));
350                  }
351 <                if ((y == 0 || hmult != 1) && lseek(outfd,
352 < scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR),
353 <                                0) == -1) {
354 <                        fprintf(stderr, "%s: seek error\n", outfile);
355 <                        exit(1);
351 > #if MAXFORK
352 >                                /* fork so we don't slow rpict down */
353 >        if ((pid = fork()) > 0) {
354 >                if (++nforked >= MAXFORK) {
355 >                        wait(&status);          /* reap a child */
356 >                        if (status)
357 >                                exit(cleanup(status>>8 & 0xff));
358 >                        nforked--;
359                  }
360 <                if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) !=
361 <                                hr*sizeof(COLR)) {
362 <                        fprintf(stderr, "%s: write error\n", outfile);
363 <                        exit(1);
360 >                return(pid);
361 >        }
362 > #else
363 >        pid = -1;               /* no forking */
364 > #endif
365 > #if NFS
366 >                                /* lock file section so NFS doesn't mess up */
367 >        fls.l_whence = 0;
368 >        fls.l_len = (long)vres*hmult*hres*sizeof(COLR);
369 >        fls.l_start = scanorig + (vmult-1-ypos)*fls.l_len;
370 >        fls.l_type = F_WRLCK;
371 >        fcntl(outfd, F_SETLKW, &fls);
372 > #endif
373 >                                /* write new piece to file */
374 >        if (lseek(outfd, fls.l_start+(long)xpos*hres*sizeof(COLR), 0) == -1)
375 >                goto seekerr;
376 >        if (hmult == 1) {
377 >                if (writebuf(outfd, (char *)pbuf,
378 >                                vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
379 >                        goto writerr;
380 >        } else
381 >                for (y = 0; y < vr; y++) {
382 >                        if (writebuf(outfd, (char *)(pbuf+y*hr),
383 >                                        hr*sizeof(COLR)) != hr*sizeof(COLR))
384 >                                goto writerr;
385 >                        if (y < vr-1 && lseek(outfd,
386 >                                        (long)(hmult-1)*hr*sizeof(COLR),
387 >                                        1) == -1)
388 >                                goto seekerr;
389                  }
390 +        if (pid == -1) {        /* fork failed */
391 + #if NFS
392 +                fls.l_type = F_UNLCK;           /* release lock */
393 +                fcntl(outfd, F_SETLKW, &fls);
394 + #endif
395 +                return(0);
396          }
397 +        _exit(0);               /* else exit child process (releasing lock) */
398 + seekerr:
399 +        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
400 +        _exit(1);
401 + writerr:
402 +        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
403 +        _exit(1);
404   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines