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.7 by greg, Sat Aug 8 18:47:39 1992 UTC vs.
Revision 2.20 by greg, Tue May 4 09:30:27 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1992 Regents of the University of California */
1 > /* Copyright (c) 1993 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include "standard.h"
12   #include <fcntl.h>
13 +
14 + #ifndef F_SETLKW
15 +
16 + main(argc, argv)
17 + int argc;
18 + char *argv[];
19 + {
20 +        fprintf(stderr, "%s: no NFS lock manager on this machine\n", argv[0]);
21 +        exit(1);
22 + }
23 +
24 + #else
25 +
26   #include <signal.h>
27   #include "color.h"
28   #include "view.h"
29   #include "resolu.h"
30  
31 + #ifndef NFS
32 + #define  NFS                    1
33 + #endif
34 +                                /* set the following to 0 to forgo forking */
35 + #ifndef MAXFORK
36 + #if NFS
37 + #define  MAXFORK                3       /* allotment of duped processes */
38 + #else
39 + #define  MAXFORK                0
40 + #endif
41 + #endif
42 +                                        /* protection from SYSV signals(!) */
43 + #if defined(sgi) || defined(hpux)
44 + #define guard_io()      sighold(SIGALRM)
45 + #define unguard()       sigrelse(SIGALRM)
46 + #endif
47 + #ifndef guard_io
48 + #define guard_io()      0
49 + #define unguard()       0
50 + #endif
51                                  /* rpict command */
52   char  *rpargv[128] = {"rpict", "-S", "1", "-x", "512", "-y", "512", "-pa", "1"};
53   int  rpargc = 9;
54   int  rpd[3];
55   FILE  *torp, *fromrp;
56 < COLR  *scanline;
56 > COLR  *pbuf;
57                                  /* our view parameters */
58   VIEW  ourview = STDVIEW;
59   double  pixaspect = 1.0;
# Line 30 | Line 63 | char  *outfile = NULL;
63   int  outfd;
64   long  scanorig;
65   int  syncfd = -1;               /* lock file descriptor */
66 + int  nforked = 0;
67  
68   char  *progname;
69   int  verbose = 0;
# Line 96 | Line 130 | char  *argv[];
130                          case 'F':               /* syncronization file */
131                                  if (argv[i][2])
132                                          break;
133 <                                if ((syncfd = open(argv[++i], O_RDWR)) < 0) {
133 >                                if ((syncfd = open(argv[++i],
134 >                                                O_RDWR|O_CREAT, 0666)) < 0) {
135                                          fprintf(stderr, "%s: cannot open\n",
136                                                          argv[i]);
137                                          exit(1);
138                                  }
139                                  continue;
140 +                        case 'z':               /* z-file ist verbotten */
141 +                                fprintf(stderr, "%s: -z option not allowed\n",
142 +                                                argv[0]);
143 +                                exit(1);
144                          case 'o':               /* output file */
145                                  if (argv[i][2])
146                                          break;
# Line 117 | Line 156 | char  *argv[];
156          }
157          init(argc, argv);
158          rpiece();
159 <        rval = cleanup();
121 <        exit(rval);
159 >        exit(cleanup(0));
160   }
161  
162  
# Line 158 | Line 196 | char  **av;
196          } else if ((outfd = open(outfile, O_RDWR)) >= 0) {
197                  if ((fp = fdopen(dup(outfd), "r+")) == NULL)
198                          goto filerr;
199 <                getheader(fp, NULL);            /* skip header */
200 <                if (fscnresolu(&hr, &vr, fp) < 0 ||     /* check resolution */
199 >                getheader(fp, NULL, NULL);      /* skip header */
200 >                if (!fscnresolu(&hr, &vr, fp) ||        /* check resolution */
201                                  hr != hres*hmult || vr != vres*vmult) {
202 <                        fprintf(stderr, "%s: picture resolution mismatch\n",
203 <                                        outfile);
202 >                        fprintf(stderr, "%s: resolution mismatch on file \"%s\"\n",
203 >                                        progname, outfile);
204                          exit(1);
205                  }
206          } else {
207 <                fprintf(stderr, "%s: cannot open\n", outfile);
207 >                fprintf(stderr, "%s: cannot open file \"%s\"\n",
208 >                                progname, outfile);
209                  exit(1);
210          }
211          scanorig = ftell(fp);           /* record position of first scanline */
212          if (fclose(fp) == -1)           /* done with stream i/o */
213                  goto filerr;
214 + #if NFS
215 +        sync();                         /* flush NFS buffers */
216 + #endif
217                                          /* start rpict process */
218          if (open_process(rpd, rpargv) <= 0) {
219                  fprintf(stderr, "%s: cannot start %s\n", progname, rpargv[0]);
# Line 183 | Line 225 | char  **av;
225                                  progname, rpargv[0]);
226                  exit(1);
227          }
228 <        if ((scanline = (COLR *)malloc(hres*sizeof(COLR))) == NULL) {
228 >        if ((pbuf = (COLR *)malloc(hres*vres*sizeof(COLR))) == NULL) {
229                  fprintf(stderr, "%s: out of memory\n", progname);
230                  exit(1);
231          }
232          signal(SIGALRM, onalrm);
233          return;
234   filerr:
235 <        fprintf(stderr, "%s: file i/o error\n", outfile);
235 >        fprintf(stderr, "%s: i/o error on file \"%s\"\n", progname, outfile);
236          exit(1);
237   }
238  
# Line 214 | Line 256 | int  *xp, *yp;
256                  lseek(syncfd, 0L, 0);
257                  buf[read(syncfd, buf, sizeof(buf)-1)] = '\0';
258                  if (sscanf(buf, "%*d %*d %d %d", xp, yp) < 2) {
217                        *xp = hmult;
218                        *yp = vmult-1;
219                }
220                if (--(*xp) < 0) {              /* decrement position */
259                          *xp = hmult-1;
260 <                        if (--(*yp) < 0) {      /* all done! */
260 >                        *yp = vmult;
261 >                }
262 >                if (--(*yp) < 0) {              /* decrement position */
263 >                        *yp = vmult-1;
264 >                        if (--(*xp) < 0) {      /* all done! */
265                                  close(syncfd);
266                                  return(0);
267                          }
268                  }
269 <                sprintf(buf, "%d %d\n%d %d\n", hmult, vmult, *xp, *yp);
269 >                sprintf(buf, "%4d %4d\n%4d %4d\n", hmult, vmult, *xp, *yp);
270                  lseek(syncfd, 0L, 0);           /* write new position */
271                  write(syncfd, buf, strlen(buf));
272                  fls.l_type = F_UNLCK;           /* release sync file */
# Line 236 | Line 278 | int  *xp, *yp;
278          if (sscanf(buf, "%d %d", xp, yp) == 2)
279                  return(1);
280          fprintf(stderr, "%s: input format error\n", progname);
281 <        exit(1);
281 >        exit(cleanup(1));
282   }
283  
284  
285   int
286 < cleanup()                       /* close rpict process and clean up */
286 > cleanup(rstat)                  /* close rpict process and clean up */
287 > int  rstat;
288   {
289 <        register int  rpstat;
289 >        int  status;
290  
291 <        free((char *)scanline);
291 >        free((char *)pbuf);
292          fclose(torp);
293          fclose(fromrp);
294 <        rpstat = close_process(rpd);
295 <        if (rpstat == -1)
296 <                rpstat = 0;
297 <        return(rpstat);
294 >        while (wait(&status) != -1)
295 >                if (rstat == 0)
296 >                        rstat = status>>8 & 0xff;
297 >        return(rstat);
298   }
299  
300  
# Line 259 | Line 302 | rpiece()                       /* render picture piece by piece */
302   {
303          VIEW  pview;
304          int  xorg, yorg;
305 <        
305 >                                        /* compute view parameters */
306 >        copystruct(&pview, &ourview);
307 >        switch (ourview.type) {
308 >        case VT_PER:
309 >                pview.horiz = 2.*180./PI*atan(
310 >                                tan(PI/180./2.*ourview.horiz)/hmult );
311 >                pview.vert = 2.*180./PI*atan(
312 >                                tan(PI/180./2.*ourview.vert)/vmult );
313 >                break;
314 >        case VT_PAR:
315 >        case VT_ANG:
316 >                pview.horiz = ourview.horiz / hmult;
317 >                pview.vert = ourview.vert / vmult;
318 >                break;
319 >        case VT_HEM:
320 >                pview.horiz = 2.*180./PI*asin(
321 >                                sin(PI/180./2.*ourview.horiz)/hmult );
322 >                pview.vert = 2.*180./PI*asin(
323 >                                sin(PI/180./2.*ourview.vert)/vmult );
324 >                break;
325 >        default:
326 >                fprintf(stderr, "%s: unknown view type '-vt%c'\n",
327 >                                progname, ourview.type);
328 >                exit(cleanup(1));
329 >        }
330 >                                        /* render each piece */
331          while (nextpiece(&xorg, &yorg)) {
332 <                copystruct(&pview, &ourview);   /* compute view for piece */
333 <                switch (ourview.type) {
266 <                case VT_PER:
267 <                        pview.horiz = 2.*180./PI*atan(
268 <                                        tan(PI/180./2.*ourview.horiz)/hmult );
269 <                        pview.vert = 2.*180./PI*atan(
270 <                                        tan(PI/180./2.*ourview.vert)/vmult );
271 <                        break;
272 <                case VT_PAR:
273 <                case VT_ANG:
274 <                        pview.horiz = ourview.horiz / hmult;
275 <                        pview.vert = ourview.vert / vmult;
276 <                        break;
277 <                case VT_HEM:
278 <                        pview.horiz = 2.*180./PI*asin(
279 <                                        sin(PI/180./2.*ourview.horiz)/hmult );
280 <                        pview.vert = 2.*180./PI*asin(
281 <                                        sin(PI/180./2.*ourview.vert)/vmult );
282 <                        break;
283 <                default:
284 <                        fprintf(stderr, "%s: unknown view type '-vt%c'\n",
285 <                                        progname, ourview.type);
286 <                        exit(1);
287 <                }
288 <                pview.hoff += xorg - 0.5*(hmult-1);
289 <                pview.voff += yorg - 0.5*(vmult-1);
332 >                pview.hoff = ourview.hoff + xorg - 0.5*(hmult-1);
333 >                pview.voff = ourview.voff + yorg - 0.5*(vmult-1);
334                  fputs(VIEWSTR, torp);
335                  fprintview(&pview, torp);
336                  putc('\n', torp);
337 <                fflush(torp);                   /* assign piece to rpict */
337 >                fflush(torp);                   /* assigns piece to rpict */
338                  putpiece(xorg, yorg);           /* place piece in output */
295                if (verbose) {                  /* notify caller */
296                        printf("%d %d done\n", xorg, yorg);
297                        fflush(stdout);
298                }
339          }
340   }
341  
342  
343 + int
344   putpiece(xpos, ypos)            /* get next piece from rpict */
345   int  xpos, ypos;
346   {
347          struct flock  fls;
348 +        int  pid, status;
349          int  hr, vr;
350 <        int  y;
351 <
350 >        register int  y;
351 >                                /* check bounds */
352          if (xpos < 0 | ypos < 0 | xpos >= hmult | ypos >= vmult) {
353                  fprintf(stderr, "%s: requested piece (%d,%d) out of range\n",
354                                  progname, xpos, ypos);
355 <                exit(1);
355 >                exit(cleanup(1));
356          }
357 <        getheader(fromrp, NULL);        /* discard header info. */
358 <        if (fscnresolu(&hr, &vr, fromrp) < 0 || /* check resolution */
359 <                        hr != hres || vr != vres) {
357 >                                /* check header from rpict */
358 >        guard_io();
359 >        getheader(fromrp, NULL, NULL);
360 >        if (!fscnresolu(&hr, &vr, fromrp) || hr != hres | vr != vres) {
361                  fprintf(stderr, "%s: resolution mismatch from %s\n",
362                                  progname, rpargv[0]);
363 <                exit(1);
363 >                exit(cleanup(1));
364          }
365 <        fls.l_whence = 1;
366 <        fls.l_start = 0L;
367 <        fls.l_len = hr*sizeof(COLR);
368 <        for (y = 0; y < vr; y++) {      /* transfer scanlines */
369 <                if (freadcolrs(scanline, hr, fromrp) < 0) {
365 >        unguard();
366 >                                /* load new piece into buffer */
367 >        for (y = 0; y < vr; y++) {
368 >                guard_io();
369 >                if (freadcolrs(pbuf+y*hr, hr, fromrp) < 0) {
370                          fprintf(stderr, "%s: read error from %s\n",
371                                          progname, rpargv[0]);
372 <                        exit(1);
372 >                        exit(cleanup(1));
373                  }
374 <                if ((y == 0 || hmult != 1) && lseek(outfd,
375 < scanorig+((long)((vmult-1-ypos)*vres+y)*hmult*hres+xpos*hres)*sizeof(COLR),
376 <                                0) == -1) {
377 <                        fprintf(stderr, "%s: seek error\n", outfile);
378 <                        exit(1);
374 >                unguard();
375 >        }
376 > #if MAXFORK
377 >                                /* fork so we don't slow rpict down */
378 >        if ((pid = fork()) > 0) {
379 >                if (++nforked >= MAXFORK) {
380 >                        wait(&status);          /* reap a child */
381 >                        if (status)
382 >                                exit(cleanup(status>>8 & 0xff));
383 >                        nforked--;
384                  }
385 <                fls.l_type = F_WRLCK;
386 <                fcntl(outfd, F_SETLKW, &fls);
387 <                if (writebuf(outfd, (char *)scanline, hr*sizeof(COLR)) !=
388 <                                hr*sizeof(COLR)) {
389 <                        fprintf(stderr, "%s: write error\n", outfile);
390 <                        exit(1);
385 >                return(pid);
386 >        }
387 > #else
388 >        pid = -1;               /* no forking */
389 > #endif
390 >        fls.l_start = scanorig +
391 >                ((long)(vmult-1-ypos)*vres*hmult+xpos)*hres*sizeof(COLR);
392 > #if NFS
393 >        fls.l_len = ((long)(vres-1)*hmult+1)*hres*sizeof(COLR);
394 >                                /* lock file section so NFS doesn't mess up */
395 >        fls.l_whence = 0;
396 >        fls.l_type = F_WRLCK;
397 >        fcntl(outfd, F_SETLKW, &fls);
398 > #endif
399 >                                /* write new piece to file */
400 >        if (lseek(outfd, fls.l_start, 0) == -1)
401 >                goto seekerr;
402 >        if (hmult == 1) {
403 >                if (writebuf(outfd, (char *)pbuf,
404 >                                vr*hr*sizeof(COLR)) != vr*hr*sizeof(COLR))
405 >                        goto writerr;
406 >        } else
407 >                for (y = 0; y < vr; y++) {
408 >                        if (writebuf(outfd, (char *)(pbuf+y*hr),
409 >                                        hr*sizeof(COLR)) != hr*sizeof(COLR))
410 >                                goto writerr;
411 >                        if (y < vr-1 && lseek(outfd,
412 >                                        (long)(hmult-1)*hr*sizeof(COLR),
413 >                                        1) == -1)
414 >                                goto seekerr;
415                  }
416 <                fls.l_type = F_UNLCK;
416 >        if (verbose) {                          /* notify caller */
417 >                printf("%d %d done\n", xpos, ypos);
418 >                fflush(stdout);
419 >        }
420 >        if (pid == -1) {        /* didn't fork or fork failed */
421 > #if NFS
422 >                fls.l_type = F_UNLCK;           /* release lock */
423                  fcntl(outfd, F_SETLKW, &fls);
424 + #endif
425 +                return(0);
426          }
427 +        _exit(0);               /* else exit child process (releasing lock) */
428 + seekerr:
429 +        fprintf(stderr, "%s: seek error on file \"%s\"\n", progname, outfile);
430 +        _exit(1);
431 + writerr:
432 +        fprintf(stderr, "%s: write error on file \"%s\"\n", progname, outfile);
433 +        _exit(1);
434   }
435 +
436 + #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines