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.24 by greg, Fri Aug 6 12:58:43 1993 UTC vs.
Revision 2.44 by greg, Tue Jun 8 19:48:31 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Generate sections of a picture.
6   */
7  
8 +
9 + #include <stdio.h>
10 + #include <signal.h>
11 + #include <sys/types.h>
12 + #include <sys/wait.h>
13 +
14 + #include "platform.h"
15   #include "standard.h"
16 < #include <fcntl.h>
16 > #include "color.h"
17 > #include "view.h"
18 > #include "rtprocess.h"
19  
20   #ifndef F_SETLKW
21  
22 < main(argc, argv)
23 < int argc;
24 < char *argv[];
22 > int
23 > main(
24 >        int argc,
25 >        char *argv[]
26 > )
27   {
28          fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]);
29          exit(1);
# Line 23 | Line 31 | char *argv[];
31  
32   #else
33  
26 #include <signal.h>
27 #include "color.h"
28 #include "view.h"
29 #include "resolu.h"
30
34   #ifndef NFS
35   #define  NFS                    1
36   #endif
# Line 40 | Line 43 | char *argv[];
43   #endif
44   #endif
45                                          /* protection from SYSV signals(!) */
46 < #if defined(sgi) || defined(hpux)
46 > #if defined(sgi)
47   #define guard_io()      sighold(SIGALRM)
48   #define unguard()       sigrelse(SIGALRM)
49   #endif
50   #ifndef guard_io
51 < #define guard_io()      0
52 < #define unguard()       0
51 > #define guard_io()      
52 > #define unguard()      
53   #endif
54 +
55 + extern char  *strerror();
56 +
57                                  /* rpict command */
58   char  *rpargv[128] = {"rpict", "-S", "1"};
59   int  rpargc = 3;
54 int  rpd[3];
60   FILE  *torp, *fromrp;
61   COLR  *pbuf;
62                                  /* our view parameters */
# Line 62 | Line 67 | int  hres = 1024, vres = 1024, hmult = 4, vmult = 4;
67   char  *outfile = NULL;
68   int  outfd;
69   long  scanorig;
70 < int  syncfd = -1;               /* lock file descriptor */
70 > FILE  *syncfp = NULL;           /* synchronization file pointer */
71 > int  synclst = F_UNLCK;         /* synchronization file lock status */
72   int  nforked = 0;
73  
74 + #define  sflock(t)      if ((t)!=synclst) dolock(fileno(syncfp),synclst=t)
75 +
76   char  *progname;
77   int  verbose = 0;
78 + unsigned  timelim = 0;
79 + int  rvrlim = -1;
80  
71 extern long  lseek(), ftell();
72
81   int  gotalrm = 0;
82 < int  onalrm() { gotalrm++; }
82 > void  onalrm(int i) { gotalrm++; }
83  
84 + static void dolock(int  fd, int  ltyp);
85 + static void init(int  ac, char  **av);
86 + static int nextpiece(int        *xp, int        *yp);
87 + static int rvrpiece(int *xp, int        *yp);
88 + static int cleanup(int  rstat);
89 + static void rpiece(void);
90 + static int putpiece(int xpos, int       ypos);
91 + static void filerr(char  *t);
92  
93 < main(argc, argv)
94 < int  argc;
95 < char  *argv[];
93 >
94 > int
95 > main(
96 >        int  argc,
97 >        char  *argv[]
98 > )
99   {
100          register int  i, rval;
101          
102          progname = argv[0];
103          for (i = 1; i < argc; i++) {
104                                                  /* expand arguments */
105 <                while (rval = expandarg(&argc, &argv, i))
106 <                        if (rval < 0) {
107 <                                fprintf(stderr, "%s: cannot expand '%s'",
108 <                                                argv[0], argv[i]);
109 <                                exit(1);
110 <                        }
105 >                while ((rval = expandarg(&argc, &argv, i)) > 0)
106 >                        ;
107 >                if (rval < 0) {
108 >                        fprintf(stderr, "%s: cannot expand '%s'",
109 >                                        argv[0], argv[i]);
110 >                        exit(1);
111 >                }
112                  if (argv[i][0] == '-')
113                          switch (argv[i][1]) {
114                          case 'v':
# Line 115 | Line 135 | char  *argv[];
135                          case 'p':               /* pixel aspect ratio? */
136                                  if (argv[i][2] != 'a' || argv[i][3])
137                                          break;
138 <                                pixaspect = atof(argv[i+1]);
138 >                                pixaspect = atof(argv[++i]);
139                                  continue;
140 +                        case 'T':               /* time limit (hours) */
141 +                                if (argv[i][2])
142 +                                        break;
143 +                                timelim = atof(argv[++i])*3600. + .5;
144 +                                break;
145                          case 'x':               /* overall x resolution */
146                                  if (argv[i][2])
147                                          break;
# Line 137 | Line 162 | char  *argv[];
162                                          break;
163                                  vmult = atoi(argv[++i]);
164                                  continue;
165 +                        case 'R':               /* recover */
166 +                                if (argv[i][2])
167 +                                        break;
168 +                                rvrlim = 0;
169 +                        /* fall through */
170                          case 'F':               /* syncronization file */
171                                  if (argv[i][2])
172                                          break;
173 <                                if ((syncfd = open(argv[++i],
174 <                                                O_RDWR|O_CREAT, 0666)) < 0) {
173 >                                if ((syncfp =
174 >                fdopen(open(argv[++i],O_RDWR|O_CREAT,0666),"r+")) == NULL) {
175                                          fprintf(stderr, "%s: cannot open\n",
176                                                          argv[i]);
177                                          exit(1);
# Line 174 | Line 204 | char  *argv[];
204   }
205  
206  
207 < init(ac, av)                    /* set up output file and start rpict */
208 < int  ac;
209 < char  **av;
207 > static void
208 > dolock(         /* lock or unlock a file */
209 >        int  fd,
210 >        int  ltyp
211 > )
212   {
213 +        static struct flock  fls;       /* static so initialized to zeroes */
214 +
215 +        fls.l_type = ltyp;
216 +        if (fcntl(fd, F_SETLKW, &fls) < 0) {
217 +                fprintf(stderr, "%s: cannot lock/unlock file: %s\n",
218 +                                progname, strerror(errno));
219 +                exit(1);
220 +        }
221 + }
222 +
223 +
224 + static void
225 + init(                   /* set up output file and start rpict */
226 +        int  ac,
227 +        char  **av
228 + )
229 + {
230          static char  hrbuf[16], vrbuf[16];
231          extern char  VersionID[];
232          char  *err;
233          FILE  *fp;
234          int  hr, vr;
235 +        SUBPROC  rpd; /* since we don't close_process(), this can be local */
236                                          /* set up view */
237          if ((err = setview(&ourview)) != NULL) {
238                  fprintf(stderr, "%s: %s\n", progname, err);
239                  exit(1);
240          }
241 <        if (syncfd != -1) {
242 <                char  buf[32];
243 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
244 <                sscanf(buf, "%d %d", &hmult, &vmult);
241 >        if (syncfp != NULL) {
242 >                sflock(F_RDLCK);
243 >                fscanf(syncfp, "%d %d", &hmult, &vmult);
244 >                sflock(F_UNLCK);
245          }
246                                          /* compute piece size */
247          hres /= hmult;
# Line 206 | Line 256 | char  **av;
256          rpargv[rpargc] = NULL;
257                                          /* open output file */
258          if ((outfd = open(outfile, O_WRONLY|O_CREAT|O_EXCL, 0666)) >= 0) {
259 +                dolock(outfd, F_WRLCK);
260                  if ((fp = fdopen(dup(outfd), "w")) == NULL)
261                          goto filerr;
262 <                printargs(ac, av, fp);          /* write header */
262 >                newheader("RADIANCE", fp);      /* create header */
263 >                printargs(ac, av, fp);
264                  fprintf(fp, "SOFTWARE= %s\n", VersionID);
265                  fputs(VIEWSTR, fp);
266                  fprintview(&ourview, fp);
# Line 219 | Line 271 | char  **av;
271                  putc('\n', fp);
272                  fprtresolu(hres*hmult, vres*vmult, fp);
273          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
274 +                dolock(outfd, F_RDLCK);
275                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
276                          goto filerr;
277                  getheader(fp, NULL, NULL);      /* skip header */
# Line 236 | Line 289 | char  **av;
289          scanorig = ftell(fp);           /* record position of first scanline */
290          if (fclose(fp) == -1)           /* done with stream i/o */
291                  goto filerr;
292 < #if NFS
240 <        sync();                         /* flush NFS buffers */
241 < #endif
292 >        dolock(outfd, F_UNLCK);
293                                          /* start rpict process */
294 <        if (open_process(rpd, rpargv) <= 0) {
294 >        if (open_process(&rpd, rpargv) <= 0) {
295                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
296                  exit(1);
297          }
298 <        if ((fromrp = fdopen(rpd[0], "r")) == NULL ||
299 <                        (torp = fdopen(rpd[1], "w")) == NULL) {
298 >        if ((fromrp = fdopen(rpd.r, "r")) == NULL ||
299 >                        (torp = fdopen(rpd.w, "w")) == NULL) {
300                  fprintf(stderr, "%s: cannot open stream to %s\n",
301                                  progname, rpargv[0]);
302                  exit(1);
# Line 255 | Line 306 | char  **av;
306                  exit(1);
307          }
308          signal(SIGALRM, onalrm);
309 +        if (timelim)
310 +                alarm(timelim);
311          return;
312   filerr:
313          fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
# Line 262 | Line 315 | filerr:
315   }
316  
317  
318 < int
319 < nextpiece(xp, yp)               /* get next piece assignment */
320 < int  *xp, *yp;
318 > static int
319 > nextpiece(              /* get next piece assignment */
320 >        int     *xp,
321 >        int     *yp
322 > )
323   {
269        struct flock  fls;
270        char  buf[64];
271
324          if (gotalrm)                    /* someone wants us to quit */
325                  return(0);
326 <        if (syncfd != -1) {             /* use sync file */
327 <                fls.l_type = F_WRLCK;           /* gain exclusive access */
328 <                fls.l_whence = 0;
329 <                fls.l_start = 0L;
330 <                fls.l_len = 0L;
331 <                fcntl(syncfd, F_SETLKW, &fls);
332 <                lseek(syncfd, 0L, 0);
333 <                buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
334 <                if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
326 >        if (syncfp != NULL) {           /* use sync file */
327 >                /*
328 >                 * So we don't necessarily have to lock and unlock the file
329 >                 * multiple times (very slow), we establish an exclusive
330 >                 * lock at the beginning on our synchronization file and
331 >                 * maintain it in the subroutine rvrpiece().
332 >                 */
333 >                sflock(F_WRLCK);
334 >                fseek(syncfp, 0L, 0);           /* read position */
335 >                if (fscanf(syncfp, "%*d %*d %d %d", xp, yp) < 2) {
336                          *xp = hmult-1;
337                          *yp = vmult;
338                  }
339 +                if (rvrlim == 0)                /* initialize recovery limit */
340 +                        rvrlim = *xp*vmult + *yp;
341 +                if (rvrpiece(xp, yp)) {         /* do stragglers first */
342 +                        sflock(F_UNLCK);
343 +                        return(1);
344 +                }
345                  if (--(*yp) < 0) {              /* decrement position */
346                          *yp = vmult-1;
347 <                        if (--(*xp) < 0) {      /* all done! */
348 <                                close(syncfd);
347 >                        if (--(*xp) < 0) {      /* all done */
348 >                                sflock(F_UNLCK);
349                                  return(0);
350                          }
351                  }
352 <                sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp);
353 <                lseek(syncfd, 0L, 0);           /* write new position */
354 <                write(syncfd, buf, strlen(buf));
355 <                fls.l_type = F_UNLCK;           /* release sync file */
297 <                fcntl(syncfd, F_SETLKW, &fls);
352 >                fseek(syncfp, 0L, 0);           /* write new position */
353 >                fprintf(syncfp, "%4d %4d\n%4d %4d\n\n", hmult, vmult, *xp, *yp);
354 >                fflush(syncfp);
355 >                sflock(F_UNLCK);                /* release sync file */
356                  return(1);
357          }
358 <        if (fgets(buf, sizeof(buf), stdin) == NULL)     /* use stdin */
301 <                return(0);
302 <        if (sscanf(buf, "%d %d", xp, yp) == 2)
303 <                return(1);
304 <        fprintf(stderr, "%s: input format error\n", progname);
305 <        exit(cleanup(1));
358 >        return(scanf("%d %d", xp, yp) == 2);    /* use stdin */
359   }
360  
361  
362 < int
363 < cleanup(rstat)                  /* close rpict process and clean up */
364 < int  rstat;
362 > static int
363 > rvrpiece(               /* check for recoverable pieces */
364 >        register int    *xp,
365 >        register int    *yp
366 > )
367   {
368 +        static char  *pdone = NULL;     /* which pieces are done */
369 +        static long  readpos = -1;      /* how far we've read */
370 +        register int  i;
371 +        /*
372 +         * This routine is called by nextpiece() with an
373 +         * exclusive lock on syncfp and the file pointer at the
374 +         * appropriate position to read in the finished pieces.
375 +         */
376 +        if (rvrlim < 0)
377 +                return(0);              /* only check if asked */
378 +        if (pdone == NULL)              /* first call */
379 +                pdone = calloc(hmult*vmult, sizeof(char));
380 +        if (pdone == NULL) {
381 +                fprintf(stderr, "%s: out of memory\n", progname);
382 +                exit(1);
383 +        }
384 +        if (readpos != -1)              /* mark what's been done */
385 +                fseek(syncfp, readpos, 0);
386 +        while (fscanf(syncfp, "%d %d", xp, yp) == 2)
387 +                pdone[*xp*vmult+*yp] = 1;
388 +        if (!feof(syncfp)) {
389 +                fprintf(stderr, "%s: format error in sync file\n", progname);
390 +                exit(1);
391 +        }
392 +        readpos = ftell(syncfp);
393 +        i = hmult*vmult;                /* find an unaccounted for piece */
394 +        while (i-- > rvrlim)
395 +                if (!pdone[i]) {
396 +                        *xp = i / vmult;
397 +                        *yp = i % vmult;
398 +                        pdone[i] = 1;   /* consider it done */
399 +                        return(1);
400 +                }
401 +        rvrlim = -1;                    /* nothing left to recover */
402 +        free(pdone);
403 +        pdone = NULL;
404 +        return(0);
405 + }
406 +
407 +
408 + static int
409 + cleanup(                        /* close rpict process and clean up */
410 +        int  rstat
411 + )
412 + {
413          int  status;
414  
415          bfree((char *)pbuf, hres*vres*sizeof(COLR));
# Line 322 | Line 422 | int  rstat;
422   }
423  
424  
425 < rpiece()                        /* render picture piece by piece */
425 > static void
426 > rpiece(void)                    /* render picture piece by piece */
427   {
428          VIEW  pview;
429          int  xorg, yorg;
430                                          /* compute view parameters */
431 <        copystruct(&pview, &ourview);
431 >        pview = ourview;
432          switch (ourview.type) {
433          case VT_PER:
434                  pview.horiz = 2.*180./PI*atan(
# Line 340 | Line 441 | rpiece()                       /* render picture piece by piece */
441                  pview.horiz = ourview.horiz / hmult;
442                  pview.vert = ourview.vert / vmult;
443                  break;
444 +        case VT_CYL:
445 +                pview.horiz = ourview.horiz / hmult;
446 +                pview.vert = 2.*180./PI*atan(
447 +                                tan(PI/180./2.*ourview.vert)/vmult );
448 +                break;
449          case VT_HEM:
450                  pview.horiz = 2.*180./PI*asin(
451                                  sin(PI/180./2.*ourview.horiz)/hmult );
# Line 353 | Line 459 | rpiece()                       /* render picture piece by piece */
459          }
460                                          /* render each piece */
461          while (nextpiece(&xorg, &yorg)) {
462 <                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
463 <                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
462 >                pview.hoff = ourview.hoff*hmult + xorg - 0.5*(hmult-1);
463 >                pview.voff = ourview.voff*vmult + yorg - 0.5*(vmult-1);
464                  fputs(VIEWSTR, torp);
465                  fprintview(&pview, torp);
466                  putc('\n', torp);
# Line 364 | Line 470 | rpiece()                       /* render picture piece by piece */
470   }
471  
472  
473 < int
474 < putpiece(xpos, ypos)            /* get next piece from rpict */
475 < int  xpos, ypos;
473 > static int
474 > putpiece(               /* get next piece from rpict */
475 > int     xpos,
476 > int     ypos
477 > )
478   {
479          struct flock  fls;
480          int  pid, status;
481          int  hr, vr;
482          register int  y;
483                                  /* check bounds */
484 <        if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
484 >        if ((xpos < 0) | (ypos < 0) | (xpos >= hmult) | (ypos >= vmult)) {
485                  fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
486                                  progname, xpos, ypos);
487                  exit(cleanup(1));
# Line 381 | Line 489 | int  xpos, ypos;
489                                  /* check header from rpict */
490          guard_io();
491          getheader(fromrp, NULL, NULL);
492 <        if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
492 >        if (!fscnresolu(&hr, &vr, fromrp) || (hr != hres) | (vr != vres)) {
493                  fprintf(stderr, "%s: resolution mismatch from %s\n",
494                                  progname, rpargv[0]);
495                  exit(cleanup(1));
# Line 422 | Line 530 | int  xpos, ypos;
530                                  /* lock file section so NFS doesn't mess up */
531          fls.l_whence = 0;
532          fls.l_type = F_WRLCK;
533 <        fcntl(outfd, F_SETLKW, &fls);
533 >        if (fcntl(outfd, F_SETLKW, &fls) < 0)
534 >                filerr("lock");
535   #endif
536                                  /* write new piece to file */
537 <        if (lseek(outfd, fls.l_start, 0) == -1)
538 <                goto seekerr;
537 >        if (lseek(outfd, (off_t)fls.l_start, SEEK_SET) < 0)
538 >                filerr("seek");
539          if (hmult == 1) {
540                  if (writebuf(outfd, (char *)pbuf,
541                                  vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
542 <                        goto writerr;
542 >                        filerr("write");
543          } else
544                  for (y = 0; y < vr; y++) {
545                          if (writebuf(outfd, (char *)(pbuf+y*hr),
546                                          hr*sizeof(COLR)) != hr*sizeof(COLR))
547 <                                goto writerr;
547 >                                filerr("write");
548                          if (y < vr-1 && lseek(outfd,
549 <                                        (long)(hmult-1)*hr*sizeof(COLR),
550 <                                        1) == -1)
551 <                                goto seekerr;
549 >                                        (off_t)(hmult-1)*hr*sizeof(COLR),
550 >                                        SEEK_CUR) < 0)
551 >                                filerr("seek");
552                  }
553 + #if NFS
554 +        fls.l_type = F_UNLCK;           /* release lock */
555 +        if (fcntl(outfd, F_SETLKW, &fls) < 0)
556 +                filerr("lock");
557 + #endif
558          if (verbose) {                          /* notify caller */
559                  printf("%d %d done\n", xpos, ypos);
560                  fflush(stdout);
561          }
562 <        if (pid == -1) {        /* didn't fork or fork failed */
563 < #if NFS
564 <                fls.l_type = F_UNLCK;           /* release lock */
565 <                fcntl(outfd, F_SETLKW, &fls);
566 < #endif
567 <                return(0);
562 >        if (syncfp != NULL) {                   /* record what's been done */
563 >                sflock(F_WRLCK);
564 >                fseek(syncfp, 0L, 2);           /* append index */
565 >                fprintf(syncfp, "%4d %4d\n", xpos, ypos);
566 >                fflush(syncfp);
567 >                                /*** Unlock not necessary, since
568 >                sflock(F_UNLCK);        _exit() or nextpiece() is next ***/
569          }
570 <        _exit(0);               /* else exit child process (releasing lock) */
571 < seekerr:
572 <        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
573 <        _exit(1);
574 < writerr:
575 <        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
570 >        if (pid == -1)          /* didn't fork or fork failed */
571 >                return(0);
572 >        _exit(0);               /* else exit child process (releasing locks) */
573 > }
574 >
575 >
576 > static void
577 > filerr(                 /* report file error and exit */
578 >        char  *t
579 > )
580 > {
581 >        fprintf(stderr, "%s: %s error on file \"%s\": %s\n",
582 >                        progname, t, outfile, strerror(errno));
583          _exit(1);
584   }
585  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines