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

Comparing ray/src/px/pcomb.c (file contents):
Revision 1.21 by greg, Wed Aug 14 15:00:34 1991 UTC vs.
Revision 2.48 by greg, Sat Jan 13 00:35:55 2018 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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   *  Combine picture files according to calcomp functions.
6   *
7   *      1/4/89
8   */
9  
10 < #include <stdio.h>
11 <
12 < #include <errno.h>
16 <
10 > #include "platform.h"
11 > #include "standard.h"
12 > #include "paths.h"
13   #include "color.h"
18
14   #include "calcomp.h"
15 + #include "view.h"
16  
17 < #define MAXINP          32              /* maximum number of input files */
18 < #define WINSIZ          9               /* scanline window size */
19 < #define MIDSCN          4               /* current scan position */
17 > #define MAXINP          1024            /* maximum number of input files */
18 > #define WINSIZ          127             /* scanline window size */
19 > #define MIDSCN          ((WINSIZ-1)/2+1)
20  
21   struct {
22          char    *name;          /* file or command name */
23          FILE    *fp;            /* stream pointer */
24 +        VIEW    vw;             /* view for picture */
25 +        RESOLU  rs;             /* image resolution and orientation */
26 +        float   pa;             /* pixel aspect ratio */
27          COLOR   *scan[WINSIZ];  /* input scanline window */
28          COLOR   coef;           /* coefficient */
29          COLOR   expos;          /* recorded exposure */
# Line 32 | Line 31 | struct {
31  
32   int     nfiles;                         /* number of input files */
33  
34 + char    ourfmt[LPICFMT+1] = PICFMT;     /* input picture format */
35 +
36 + char    StandardInput[] = "<stdin>";
37 + char    Command[] = "<Command>";
38   char    vcolin[3][4] = {"ri", "gi", "bi"};
39   char    vcolout[3][4] = {"ro", "go", "bo"};
40   char    vbrtin[] = "li";
41   char    vbrtout[] = "lo";
42   char    vcolexp[3][4] = {"re", "ge", "be"};
43   char    vbrtexp[] = "le";
44 + char    vpixaspect[] = "pa";
45  
46 + char    vray[7][4] = {"Ox", "Oy", "Oz", "Dx", "Dy", "Dz", "T"};
47 +
48 + char    vpsize[] = "S";
49 +
50   char    vnfiles[] = "nfiles";
51 + char    vwhteff[] = "WE";
52   char    vxmax[] = "xmax";
53   char    vymax[] = "ymax";
54   char    vxres[] = "xres";
# Line 57 | Line 66 | int    xres, yres;                     /* output resolution */
66  
67   int     xpos, ypos;                     /* output position */
68  
69 + char    *progname;                      /* global argv[0] */
70 +
71 + int     echoheader = 1;
72   int     wrongformat = 0;
73 + int     gotview;
74  
62 FILE    *popen();
75  
76 + static gethfunc headline;
77 + static void checkfile(void);
78 + static double rgb_bright(COLOR  clr);
79 + static double xyz_bright(COLOR  clr);
80 + static void init(void);
81 + static void combine(void);
82 + static void advance(void);
83 + static double l_expos(char      *nam);
84 + static double l_pixaspect(char *nm);
85 + static double l_colin(char      *nam);
86 + static double l_ray(char        *nam);
87 + static double l_psize(char *nm);
88  
89 < main(argc, argv)
90 < int     argc;
91 < char    *argv[];
89 >
90 > int
91 > main(
92 >        int     argc,
93 >        char    *argv[]
94 > )
95   {
96          int     original;
97          double  f;
98 <        int     a, i;
98 >        int     a;
99 >
100 >        SET_DEFAULT_BINARY();
101 >        SET_FILE_BINARY(stdin);
102 >        SET_FILE_BINARY(stdout);
103 >        progname = argv[0];
104 >        esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
105 >        esupport &= ~(E_OUTCHAN|E_INCHAN);
106                                                  /* scan options */
107          for (a = 1; a < argc; a++) {
108                  if (argv[a][0] == '-')
# Line 80 | Line 114 | char   *argv[];
114                          case 'w':
115                                  nowarn = !nowarn;
116                                  continue;
117 +                        case 'h':
118 +                                echoheader = !echoheader;
119 +                                continue;
120                          case 'f':
121                          case 'e':
122                                  a++;
# Line 87 | Line 124 | char   *argv[];
124                          }
125                  break;
126          }
127 +        newheader("RADIANCE", stdout);  /* start header */
128 +        fputnow(stdout);
129                                          /* process files */
130          for (nfiles = 0; nfiles < MAXINP; nfiles++) {
131                  setcolor(input[nfiles].coef, 1.0, 1.0, 1.0);
132                  setcolor(input[nfiles].expos, 1.0, 1.0, 1.0);
133 +                input[nfiles].vw = stdview;
134 +                input[nfiles].pa = 1.0;
135          }
136          nfiles = 0;
137          original = 0;
# Line 103 | Line 144 | char   *argv[];
144                  if (argv[a][0] == '-')
145                          switch (argv[a][1]) {
146                          case '\0':
147 <                                input[nfiles].name = "<stdin>";
147 >                                input[nfiles].name = StandardInput;
148                                  input[nfiles].fp = stdin;
149                                  break;
150                          case 'o':
151                                  original++;
152 <                                break;
152 >                                continue;
153                          case 's':
154                                  f = atof(argv[++a]);
155                                  scalecolor(input[nfiles].coef, f);
# Line 122 | Line 163 | char   *argv[];
163                                  goto usage;
164                          }
165                  else {
166 <                        input[nfiles].name = argv[a];
167 <                        input[nfiles].fp = argv[a][0]=='!' ?
168 <                                        popen(argv[a]+1, "r") :
169 <                                        fopen(argv[a], "r");
166 >                        if (argv[a][0] == '!') {
167 >                                input[nfiles].name = Command;
168 >                                input[nfiles].fp = popen(argv[a]+1, "r");
169 >                        } else {
170 >                                input[nfiles].name = argv[a];
171 >                                input[nfiles].fp = fopen(argv[a], "r");
172 >                        }
173                          if (input[nfiles].fp == NULL) {
174                                  perror(argv[a]);
175                                  quit(1);
# Line 139 | Line 183 | char   *argv[];
183                                          colval(input[nfiles].expos,GRN);
184                          colval(input[nfiles].coef,BLU) /=
185                                          colval(input[nfiles].expos,BLU);
186 +                        setcolor(input[nfiles].expos, 1.0, 1.0, 1.0);
187                  }
188                  nfiles++;
189                  original = 0;
# Line 156 | Line 201 | char   *argv[];
201                                  continue;
202                          case 'w':
203                                  continue;
204 +                        case 'h':
205 +                                continue;
206                          case 'f':
207                                  fcompile(argv[++a]);
208                                  continue;
# Line 165 | Line 212 | char   *argv[];
212                          }
213                  break;
214          }
215 <                                                /* set/get output resolution */
169 <        if (!vardefined(vxres))
170 <                varset(vxres, ':', (double)xmax);
171 <        if (!vardefined(vyres))
172 <                varset(vyres, ':', (double)ymax);
215 >                                                /* get output resolution */
216          xres = varvalue(vxres) + .5;
217          yres = varvalue(vyres) + .5;
218          if (xres <= 0 || yres <= 0) {
# Line 179 | Line 222 | char   *argv[];
222          }
223                                                  /* complete header */
224          printargs(argc, argv, stdout);
225 <        fputformat(COLRFMT, stdout);
225 >        if (strcmp(ourfmt, PICFMT))
226 >                fputformat(ourfmt, stdout);     /* print format if known */
227          putchar('\n');
228 <        fputresolu(YMAJOR|YDECR, xres, yres, stdout);
228 >        fprtresolu(xres, yres, stdout);
229                                                  /* combine pictures */
230          combine();
231          quit(0);
# Line 189 | Line 233 | usage:
233          eputs("Usage: ");
234          eputs(argv[0]);
235          eputs(
236 < " [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
236 > " [-w][-h][-x xr][-y yr][-e expr][-f file] [ [-o][-s f][-c r g b] hdr ..]\n");
237          quit(1);
238 +        return 1; /* pro forma return */
239   }
240  
241  
242 < tputs(s)                        /* put out string preceded by a tab */
243 < char    *s;
242 > static int
243 > headline(                       /* check header line & echo if requested */
244 >        char    *s,
245 >        void    *p
246 > )
247   {
248          char    fmt[32];
249          double  d;
250          COLOR   ctmp;
251  
252 <        if (isformat(s)) {                      /* check format */
253 <                formatval(fmt, s);
254 <                wrongformat = strcmp(fmt, COLRFMT);
255 <                return;         /* don't echo */
252 >        if (isheadid(s))                        /* header id */
253 >                return(0);      /* don't echo */
254 >        if (formatval(fmt, s)) {                /* check format */
255 >                if (globmatch(ourfmt, fmt)) {
256 >                        wrongformat = 0;
257 >                        strcpy(ourfmt, fmt);
258 >                } else
259 >                        wrongformat = globmatch(PICFMT, fmt) ? 1 : -1;
260 >                return(0);      /* don't echo */
261          }
262          if (isexpos(s)) {                       /* exposure */
263                  d = exposval(s);
# Line 212 | Line 265 | char   *s;
265          } else if (iscolcor(s)) {               /* color correction */
266                  colcorval(ctmp, s);
267                  multcolor(input[nfiles].expos, ctmp);
268 +        } else if (isaspect(s))
269 +                input[nfiles].pa *= aspectval(s);
270 +        else if (isview(s) && sscanview(&input[nfiles].vw, s) > 0)
271 +                gotview++;
272 +
273 +        if (echoheader) {                       /* echo line */
274 +                putchar('\t');
275 +                return(fputs(s, stdout));
276          }
277 <                                                /* echo line */
217 <        putchar('\t');
218 <        fputs(s, stdout);
277 >        return(0);
278   }
279  
280  
281 < checkfile()                     /* ready a file */
281 > static void
282 > checkfile(void)                 /* ready a file */
283   {
284 <        int     xinp, yinp;
225 <        register int    i;
284 >        int     i;
285                                          /* process header */
286 <        fputs(input[nfiles].name, stdout);
287 <        fputs(":\n", stdout);
288 <        getheader(input[nfiles].fp, tputs, NULL);
289 <        if (wrongformat) {
286 >        gotview = 0;
287 >        if (echoheader) {
288 >                fputs(input[nfiles].name, stdout);
289 >                fputs(":\n", stdout);
290 >        }
291 >        getheader(input[nfiles].fp, headline, NULL);
292 >        if (wrongformat < 0) {
293                  eputs(input[nfiles].name);
294 <                eputs(": not in Radiance picture format\n");
294 >                eputs(": not a Radiance picture\n");
295                  quit(1);
296          }
297 <        if (fgetresolu(&xinp, &yinp, input[nfiles].fp) != (YMAJOR|YDECR)) {
297 >        if (wrongformat > 0) {
298 >                wputs(input[nfiles].name);
299 >                wputs(": warning -- incompatible picture format\n");
300 >        }
301 >        if (!gotview || setview(&input[nfiles].vw) != NULL)
302 >                input[nfiles].vw.type = 0;
303 >        if (!fgetsresolu(&input[nfiles].rs, input[nfiles].fp)) {
304                  eputs(input[nfiles].name);
305                  eputs(": bad picture size\n");
306                  quit(1);
307          }
308          if (xmax == 0 && ymax == 0) {
309 <                xmax = xinp;
310 <                ymax = yinp;
311 <        } else if (xinp != xmax || yinp != ymax) {
309 >                xmax = scanlen(&input[nfiles].rs);
310 >                ymax = numscans(&input[nfiles].rs);
311 >        } else if (scanlen(&input[nfiles].rs) != xmax ||
312 >                        numscans(&input[nfiles].rs) != ymax) {
313                  eputs(input[nfiles].name);
314                  eputs(": resolution mismatch\n");
315                  quit(1);
# Line 251 | Line 320 | checkfile()                    /* ready a file */
320   }
321  
322  
323 < init()                                  /* perform final setup */
323 > static double
324 > rgb_bright(
325 >        COLOR  clr
326 > )
327   {
328 <        double  l_colin(), l_expos();
329 <        register int    i;
328 >        return(bright(clr));
329 > }
330 >
331 >
332 > static double
333 > xyz_bright(
334 >        COLOR  clr
335 > )
336 > {
337 >        return(clr[CIEY]);
338 > }
339 >
340 >
341 > double  (*ourbright)() = rgb_bright;
342 >
343 >
344 > static void
345 > init(void)                                      /* perform final setup */
346 > {
347 >        int     i;
348                                                  /* define constants */
349 +        varset("PI", ':', PI);
350          varset(vnfiles, ':', (double)nfiles);
351          varset(vxmax, ':', (double)xmax);
352          varset(vymax, ':', (double)ymax);
# Line 266 | Line 357 | init()                                 /* perform final setup */
357          }
358          funset(vbrtexp, 1, ':', l_expos);
359          funset(vbrtin, 1, '=', l_colin);
360 +        funset(vpixaspect, 1, ':', l_pixaspect);
361 +        for (i = 0; i < 7; i++)
362 +                funset(vray[i], 1, '=', l_ray);
363 +        funset(vpsize, 1, '=', l_psize);
364 +                                                /* set brightness function */
365 +        if (!strcmp(ourfmt, CIEFMT)) {
366 +                varset(vwhteff, ':', 1.0);
367 +                ourbright = xyz_bright;
368 +        } else
369 +                varset(vwhteff, ':', WHTEFFICACY);
370 +                                                /* these may be overridden */
371 +        varset(vxres, ':', (double)xmax);
372 +        varset(vyres, ':', (double)ymax);
373   }
374  
375  
376 < combine()                       /* combine pictures */
376 > static void
377 > combine(void)                   /* combine pictures */
378   {
379          EPNODE  *coldef[3], *brtdef;
380          COLOR   *scanout;
381          double  d;
382 <        register int    i, j;
382 >        int     i, j;
383                                                  /* check defined variables */
384          for (j = 0; j < 3; j++) {
385                  if (vardefined(vcolout[j]))
# Line 295 | Line 400 | combine()                      /* combine pictures */
400              advance();
401              varset(vypos, '=', (double)ypos);
402              for (xpos = 0; xpos < xres; xpos++) {
403 <                xscan = (long)xpos*xmax/xres;
403 >                xscan = (xpos+.5)*xmax/xres;
404                  varset(vxpos, '=', (double)xpos);
405                  eclock++;
406                  if (brtdef != NULL) {
# Line 323 | Line 428 | combine()                      /* combine pictures */
428                      quit(1);
429              }
430          }
431 <        efree(scanout);
431 >        efree((char *)scanout);
432   }
433  
434  
435 < advance()                       /* read in data for next scanline */
435 > static void
436 > advance(void)                   /* read in data for next scanline */
437   {
438          int     ytarget;
439 <        register COLOR  *st;
440 <        register int    i, j;
439 >        COLOR   *st;
440 >        int     i, j;
441  
442 <        for (ytarget = (long)ypos*ymax/yres; yscan > ytarget; yscan--)
442 >        for (ytarget = (ypos+.5)*ymax/yres; yscan > ytarget; yscan--)
443                  for (i = 0; i < nfiles; i++) {
444                          st = input[i].scan[WINSIZ-1];
445                          for (j = WINSIZ-1; j > 0; j--)  /* rotate window */
# Line 341 | Line 447 | advance()                      /* read in data for next scanline */
447                          input[i].scan[0] = st;
448                          if (yscan <= MIDSCN)            /* hit bottom? */
449                                  continue;
450 <                        if (freadscan(st, xmax, input[i].fp) < 0) {     /* read */
450 >                        if (freadscan(st, xmax, input[i].fp) < 0) {  /* read */
451                                  eputs(input[i].name);
452                                  eputs(": read error\n");
453                                  quit(1);
# Line 352 | Line 458 | advance()                      /* read in data for next scanline */
458   }
459  
460  
461 < double
462 < l_expos(nam)                    /* return picture exposure */
463 < register char   *nam;
461 > static double
462 > l_expos(                        /* return picture exposure */
463 >        char    *nam
464 > )
465   {
466 <        register int    fn, n;
466 >        int     fn, n;
467 >        double  d;
468  
469 <        fn = argument(1) - .5;
470 <        if (fn < 0 || fn >= nfiles)
471 <                return(1.0);
469 >        d = argument(1);
470 >        if (d <= -0.5 || d >= nfiles+0.5) {
471 >                errno = EDOM;
472 >                return(0.0);
473 >        }
474 >        if (d < 0.5)
475 >                return((double)nfiles);
476 >        fn = d - 0.5;
477          if (nam == vbrtexp)
478 <                return(bright(input[fn].expos));
478 >                return((*ourbright)(input[fn].expos));
479          n = 3;
480          while (n--)
481                  if (nam == vcolexp[n])
482                          return(colval(input[fn].expos,n));
483          eputs("Bad call to l_expos()!\n");
484          quit(1);
485 +        return 1; /* pro forma return */
486   }
487  
488  
489 < double
490 < l_colin(nam)                    /* return color value for picture */
377 < register char   *nam;
489 > static double
490 > l_pixaspect(char *nm)           /* return pixel aspect ratio */
491   {
492          int     fn;
380        register int    n, xoff, yoff;
493          double  d;
494  
495          d = argument(1);
496 <        if (d > -.5 && d < .5)
496 >        if (d <= -0.5 || d >= nfiles+0.5) {
497 >                errno = EDOM;
498 >                return(0.0);
499 >        }
500 >        if (d < 0.5)
501                  return((double)nfiles);
502 <        fn = d - .5;
503 <        if (fn < 0 || fn >= nfiles) {
502 >        fn = d - 0.5;
503 >        return(input[fn].pa);
504 > }
505 >
506 >
507 > static double
508 > l_colin(                        /* return color value for picture */
509 >        char    *nam
510 > )
511 > {
512 >        int     fn;
513 >        int     n, xoff, yoff;
514 >        double  d;
515 >
516 >        d = argument(1);
517 >        if (d <= -0.5 || d >= nfiles+0.5) {
518                  errno = EDOM;
519                  return(0.0);
520          }
521 +        if (d < 0.5)
522 +                return((double)nfiles);
523 +        fn = d - 0.5;
524          xoff = yoff = 0;
525          n = nargum();
526          if (n >= 2) {
# Line 419 | Line 552 | register char  *nam;
552                  }
553          }
554          if (nam == vbrtin)
555 <                return(bright(input[fn].scan[MIDSCN+yoff][xscan+xoff]));
555 >                return((*ourbright)(input[fn].scan[MIDSCN+yoff][xscan+xoff]));
556          n = 3;
557          while (n--)
558              if (nam == vcolin[n])
559                  return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n));
560          eputs("Bad call to l_colin()!\n");
561          quit(1);
562 +        return 1; /* pro forma return */
563   }
564  
565  
566 < wputs(msg)
567 < char    *msg;
566 > static double
567 > l_ray(          /* return ray origin or direction */
568 >        char    *nam
569 > )
570   {
571 +        static unsigned long    ltick[MAXINP];
572 +        static FVECT    lorg[MAXINP], ldir[MAXINP];
573 +        static double   ldist[MAXINP];
574 +        RREAL   loc[2];
575 +        double  d;
576 +        int     fn;
577 +        int     i;
578 +
579 +        d = argument(1);
580 +        if (d <= -0.5 || d >= nfiles+0.5) {
581 +                errno = EDOM;
582 +                return(0.0);
583 +        }
584 +        if (d < 0.5)
585 +                return((double)nfiles);
586 +        fn = d - 0.5;
587 +        if (ltick[fn] != eclock) {              /* need to compute? */
588 +                lorg[fn][0] = lorg[fn][1] = lorg[fn][2] = 0.0;
589 +                ldir[fn][0] = ldir[fn][1] = ldir[fn][2] = 0.0;
590 +                ldist[fn] = -1.0;
591 +                if (input[fn].vw.type == 0)
592 +                        errno = EDOM;
593 +                else {
594 +                        pix2loc(loc, &input[fn].rs, xscan, ymax-1-yscan);
595 +                        ldist[fn] = viewray(lorg[fn], ldir[fn],
596 +                                        &input[fn].vw, loc[0], loc[1]);
597 +                }
598 +                ltick[fn] = eclock;
599 +        }
600 +        if (nam == vray[i=6])
601 +                return(ldist[fn]);
602 +        while (i--)
603 +                if (nam == vray[i])
604 +                        return(i < 3 ? lorg[fn][i] : ldir[fn][i-3]);
605 +        eputs("Bad call to l_ray()!\n");
606 +        quit(1);
607 +        return 1; /* pro forma return */
608 + }
609 +
610 +
611 + static double
612 + l_psize(char *nm)               /* compute pixel size in steradians */
613 + {
614 +        static unsigned long    ltick[MAXINP];
615 +        static double   psize[MAXINP];
616 +        FVECT   dir0, org, dirx, diry;
617 +        RREAL   locx[2], locy[2];
618 +        double  d;
619 +        int     fn;
620 +        int     i;
621 +
622 +        d = argument(1);
623 +        if (d <= -0.5 || d >= nfiles+0.5) {
624 +                errno = EDOM;
625 +                return(0.0);
626 +        }
627 +        if (d < 0.5)
628 +                return((double)nfiles);
629 +        fn = d - 0.5;
630 +        if (ltick[fn] != eclock) {              /* need to compute? */
631 +                psize[fn] = 0.0;
632 +                if (input[fn].vw.type == 0)
633 +                        errno = EDOM;
634 +                else if (input[fn].vw.type != VT_PAR &&
635 +                                funvalue(vray[6], 1, &d) >= -FTINY) {
636 +                        for (i = 0; i < 3; i++)
637 +                                dir0[i] = funvalue(vray[3+i], 1, &d);
638 +                        pix2loc(locx, &input[fn].rs, xscan+1, ymax-1-yscan);
639 +                        pix2loc(locy, &input[fn].rs, xscan, ymax-yscan);
640 +                        if (viewray(org, dirx, &input[fn].vw,
641 +                                        locx[0], locx[1]) >= -FTINY &&
642 +                                        viewray(org, diry, &input[fn].vw,
643 +                                        locy[0], locy[1]) >= -FTINY) {
644 +                                                /* approximate solid angle */
645 +                                for (i = 0; i < 3; i++) {
646 +                                        dirx[i] -= dir0[i];
647 +                                        diry[i] -= dir0[i];
648 +                                }
649 +                                fcross(dir0, dirx, diry);
650 +                                psize[fn] = VLEN(dir0);
651 +                        }
652 +                }
653 +                ltick[fn] = eclock;
654 +        }
655 +        return(psize[fn]);
656 + }
657 +
658 +
659 + extern void
660 + wputs(char      *msg)
661 + {
662          if (!nowarn)
663                  eputs(msg);
664   }
665  
666  
667 < eputs(msg)
668 < char    *msg;
667 > extern void
668 > eputs(char *msg)
669   {
670          fputs(msg, stderr);
671   }
672  
673  
674 < quit(code)
675 < int     code;
674 > extern void
675 > quit(int code)          /* exit gracefully */
676   {
677 +        int  i;
678 +                                /* close input files */
679 +        for (i = 0; i < nfiles; i++)
680 +                if (input[i].name == Command)
681 +                        pclose(input[i].fp);
682 +                else
683 +                        fclose(input[i].fp);
684          exit(code);
685   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines