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.20 by greg, Thu May 30 10:14:39 1991 UTC vs.
Revision 2.32 by schorsch, Sun Mar 28 20:33:14 2004 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>
13 <
10 > #include "platform.h"
11 > #include "rtprocess.h"
12 > #include "rterror.h"
13 > #include "rtmisc.h"
14   #include "color.h"
18
15   #include "calcomp.h"
16 + #include "view.h"
17  
18   #define MAXINP          32              /* maximum number of input files */
19 < #define WINSIZ          9               /* scanline window size */
20 < #define MIDSCN          4               /* current scan position */
19 > #define WINSIZ          64              /* scanline window size */
20 > #define MIDSCN          ((WINSIZ-1)/2+1)
21  
25 #define BRT             (-1)            /* special index for brightness */
26
22   struct {
23          char    *name;          /* file or command name */
24          FILE    *fp;            /* stream pointer */
25 +        VIEW    vw;             /* view for picture */
26 +        RESOLU  rs;             /* image resolution and orientation */
27 +        float   pa;             /* pixel aspect ratio */
28          COLOR   *scan[WINSIZ];  /* input scanline window */
29          COLOR   coef;           /* coefficient */
30          COLOR   expos;          /* recorded exposure */
# Line 34 | Line 32 | struct {
32  
33   int     nfiles;                         /* number of input files */
34  
35 < char    *vcolin[3] = {"ri", "gi", "bi"};
36 < char    *vcolout[3] = {"ro", "go", "bo"};
35 > char    ourfmt[LPICFMT+1] = PICFMT;     /* input picture format */
36 >
37 > char    StandardInput[] = "<stdin>";
38 > char    Command[] = "<Command>";
39 > char    vcolin[3][4] = {"ri", "gi", "bi"};
40 > char    vcolout[3][4] = {"ro", "go", "bo"};
41   char    vbrtin[] = "li";
42   char    vbrtout[] = "lo";
43 < char    *vcolexp[3] = {"re", "ge", "be"};
43 > char    vcolexp[3][4] = {"re", "ge", "be"};
44   char    vbrtexp[] = "le";
45 + char    vpixaspect[] = "pa";
46  
47 + char    vray[7][4] = {"Ox", "Oy", "Oz", "Dx", "Dy", "Dz", "T"};
48 +
49 + char    vpsize[] = "S";
50 +
51   char    vnfiles[] = "nfiles";
52 + char    vwhteff[] = "WE";
53   char    vxmax[] = "xmax";
54   char    vymax[] = "ymax";
55   char    vxres[] = "xres";
# Line 59 | Line 67 | int    xres, yres;                     /* output resolution */
67  
68   int     xpos, ypos;                     /* output position */
69  
70 + char    *progname;                      /* global argv[0] */
71 +
72   int     wrongformat = 0;
73 + int     gotview;
74  
64 FILE    *popen();
75  
76 + static gethfunc tabputs;
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   {
71        char    buf[128];
96          int     original;
97          double  f;
98 <        int     a, i;
98 >        int     a;
99 >        SET_DEFAULT_BINARY();
100 >        SET_FILE_BINARY(stdin);
101 >        SET_FILE_BINARY(stdout);
102 >        progname = argv[0];
103                                                  /* scan options */
104          for (a = 1; a < argc; a++) {
105                  if (argv[a][0] == '-')
# Line 90 | Line 118 | char   *argv[];
118                          }
119                  break;
120          }
121 +        newheader("RADIANCE", stdout);  /* start header */
122 +        fputnow(stdout);
123                                          /* process files */
124          for (nfiles = 0; nfiles < MAXINP; nfiles++) {
125                  setcolor(input[nfiles].coef, 1.0, 1.0, 1.0);
126                  setcolor(input[nfiles].expos, 1.0, 1.0, 1.0);
127 +                input[nfiles].vw = stdview;
128 +                input[nfiles].pa = 1.0;
129          }
130          nfiles = 0;
131          original = 0;
# Line 106 | Line 138 | char   *argv[];
138                  if (argv[a][0] == '-')
139                          switch (argv[a][1]) {
140                          case '\0':
141 <                                input[nfiles].name = "<stdin>";
141 >                                input[nfiles].name = StandardInput;
142                                  input[nfiles].fp = stdin;
143                                  break;
144                          case 'o':
145                                  original++;
146 <                                break;
146 >                                continue;
147                          case 's':
148                                  f = atof(argv[++a]);
149                                  scalecolor(input[nfiles].coef, f);
# Line 125 | Line 157 | char   *argv[];
157                                  goto usage;
158                          }
159                  else {
160 <                        input[nfiles].name = argv[a];
161 <                        input[nfiles].fp = argv[a][0]=='!' ?
162 <                                        popen(argv[a]+1, "r") :
163 <                                        fopen(argv[a], "r");
160 >                        if (argv[a][0] == '!') {
161 >                                input[nfiles].name = Command;
162 >                                input[nfiles].fp = popen(argv[a]+1, "r");
163 >                        } else {
164 >                                input[nfiles].name = argv[a];
165 >                                input[nfiles].fp = fopen(argv[a], "r");
166 >                        }
167                          if (input[nfiles].fp == NULL) {
168                                  perror(argv[a]);
169                                  quit(1);
# Line 142 | Line 177 | char   *argv[];
177                                          colval(input[nfiles].expos,GRN);
178                          colval(input[nfiles].coef,BLU) /=
179                                          colval(input[nfiles].expos,BLU);
180 +                        setcolor(input[nfiles].expos, 1.0, 1.0, 1.0);
181                  }
182                  nfiles++;
183                  original = 0;
# Line 152 | Line 188 | char   *argv[];
188                  if (argv[a][0] == '-')
189                          switch (argv[a][1]) {
190                          case 'x':
191 <                                strcpy(buf, vxres);
156 <                                strcat(buf, ":");
157 <                                strcat(buf, argv[++a]);
158 <                                scompile(buf, NULL, 0);
191 >                                varset(vxres, ':', eval(argv[++a]));
192                                  continue;
193                          case 'y':
194 <                                strcpy(buf, vyres);
162 <                                strcat(buf, ":");
163 <                                strcat(buf, argv[++a]);
164 <                                scompile(buf, NULL, 0);
194 >                                varset(vyres, ':', eval(argv[++a]));
195                                  continue;
196                          case 'w':
197                                  continue;
# Line 188 | Line 218 | char   *argv[];
218          }
219                                                  /* complete header */
220          printargs(argc, argv, stdout);
221 <        fputformat(COLRFMT, stdout);
221 >        if (strcmp(ourfmt, PICFMT))
222 >                fputformat(ourfmt, stdout);     /* print format if known */
223          putchar('\n');
224 <        fputresolu(YMAJOR|YDECR, xres, yres, stdout);
224 >        fprtresolu(xres, yres, stdout);
225                                                  /* combine pictures */
226          combine();
227          quit(0);
# Line 198 | Line 229 | usage:
229          eputs("Usage: ");
230          eputs(argv[0]);
231          eputs(
232 < " [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
232 > " [-w][-x xr][-y yr][-e expr][-f file] [ [-o][-s f][-c r g b] pic ..]\n");
233          quit(1);
234 +        return 1; /* pro forma return */
235   }
236  
237  
238 < tputs(s)                        /* put out string preceded by a tab */
239 < char    *s;
238 > static int
239 > tabputs(                        /* put out string preceded by a tab */
240 >        char    *s,
241 >        void    *p
242 > )
243   {
244          char    fmt[32];
245          double  d;
246          COLOR   ctmp;
247  
248 <        if (isformat(s)) {                      /* check format */
249 <                formatval(fmt, s);
250 <                wrongformat = strcmp(fmt, COLRFMT);
251 <                return;         /* don't echo */
248 >        if (isheadid(s))                        /* header id */
249 >                return(0);      /* don't echo */
250 >        if (formatval(fmt, s)) {                /* check format */
251 >                if (globmatch(ourfmt, fmt)) {
252 >                        wrongformat = 0;
253 >                        strcpy(ourfmt, fmt);
254 >                } else
255 >                        wrongformat = globmatch(PICFMT, fmt) ? 1 : -1;
256 >                return(0);      /* don't echo */
257          }
258          if (isexpos(s)) {                       /* exposure */
259                  d = exposval(s);
# Line 221 | Line 261 | char   *s;
261          } else if (iscolcor(s)) {               /* color correction */
262                  colcorval(ctmp, s);
263                  multcolor(input[nfiles].expos, ctmp);
264 <        }
264 >        } else if (isaspect(s))
265 >                input[nfiles].pa *= aspectval(s);
266 >        else if (isview(s) && sscanview(&input[nfiles].vw, s) > 0)
267 >                gotview++;
268                                                  /* echo line */
269          putchar('\t');
270 <        fputs(s, stdout);
270 >        return(fputs(s, stdout));
271   }
272  
273  
274 < checkfile()                     /* ready a file */
274 > static void
275 > checkfile(void)                 /* ready a file */
276   {
233        int     xinp, yinp;
277          register int    i;
278                                          /* process header */
279 +        gotview = 0;
280          fputs(input[nfiles].name, stdout);
281          fputs(":\n", stdout);
282 <        getheader(input[nfiles].fp, tputs, NULL);
283 <        if (wrongformat) {
282 >        getheader(input[nfiles].fp, tabputs, NULL);
283 >        if (wrongformat < 0) {
284                  eputs(input[nfiles].name);
285 <                eputs(": not in Radiance picture format\n");
285 >                eputs(": not a Radiance picture\n");
286                  quit(1);
287          }
288 <        if (fgetresolu(&xinp, &yinp, input[nfiles].fp) != (YMAJOR|YDECR)) {
288 >        if (wrongformat > 0) {
289 >                wputs(input[nfiles].name);
290 >                wputs(": warning -- incompatible picture format\n");
291 >        }
292 >        if (!gotview || setview(&input[nfiles].vw) != NULL)
293 >                input[nfiles].vw.type = 0;
294 >        if (!fgetsresolu(&input[nfiles].rs, input[nfiles].fp)) {
295                  eputs(input[nfiles].name);
296                  eputs(": bad picture size\n");
297                  quit(1);
298          }
299          if (xmax == 0 && ymax == 0) {
300 <                xmax = xinp;
301 <                ymax = yinp;
302 <        } else if (xinp != xmax || yinp != ymax) {
300 >                xmax = scanlen(&input[nfiles].rs);
301 >                ymax = numscans(&input[nfiles].rs);
302 >        } else if (scanlen(&input[nfiles].rs) != xmax ||
303 >                        numscans(&input[nfiles].rs) != ymax) {
304                  eputs(input[nfiles].name);
305                  eputs(": resolution mismatch\n");
306                  quit(1);
# Line 260 | Line 311 | checkfile()                    /* ready a file */
311   }
312  
313  
314 < init()                                  /* perform final setup */
314 > static double
315 > rgb_bright(
316 >        COLOR  clr
317 > )
318   {
319 <        double  l_colin(), l_expos();
319 >        return(bright(clr));
320 > }
321 >
322 >
323 > static double
324 > xyz_bright(
325 >        COLOR  clr
326 > )
327 > {
328 >        return(clr[CIEY]);
329 > }
330 >
331 >
332 > double  (*ourbright)() = rgb_bright;
333 >
334 >
335 > static void
336 > init(void)                                      /* perform final setup */
337 > {
338 >        double  l_colin(char *), l_expos(char *), l_pixaspect(char *),
339 >                        l_ray(char *), l_psize(char *);
340          register int    i;
341                                                  /* define constants */
342 +        varset("PI", ':', PI);
343          varset(vnfiles, ':', (double)nfiles);
344          varset(vxmax, ':', (double)xmax);
345          varset(vymax, ':', (double)ymax);
# Line 275 | Line 350 | init()                                 /* perform final setup */
350          }
351          funset(vbrtexp, 1, ':', l_expos);
352          funset(vbrtin, 1, '=', l_colin);
353 +        funset(vpixaspect, 1, ':', l_pixaspect);
354 +        for (i = 0; i < 7; i++)
355 +                funset(vray[i], 1, '=', l_ray);
356 +        funset(vpsize, 1, '=', l_psize);
357 +                                                /* set brightness function */
358 +        if (!strcmp(ourfmt, CIEFMT)) {
359 +                varset(vwhteff, ':', 1.0);
360 +                ourbright = xyz_bright;
361 +        } else
362 +                varset(vwhteff, ':', WHTEFFICACY);
363   }
364  
365  
366 < combine()                       /* combine pictures */
366 > static void
367 > combine(void)                   /* combine pictures */
368   {
369          EPNODE  *coldef[3], *brtdef;
370          COLOR   *scanout;
# Line 332 | Line 418 | combine()                      /* combine pictures */
418                      quit(1);
419              }
420          }
421 <        efree(scanout);
421 >        efree((char *)scanout);
422   }
423  
424  
425 < advance()                       /* read in data for next scanline */
425 > static void
426 > advance(void)                   /* read in data for next scanline */
427   {
428          int     ytarget;
429          register COLOR  *st;
# Line 350 | Line 437 | advance()                      /* read in data for next scanline */
437                          input[i].scan[0] = st;
438                          if (yscan <= MIDSCN)            /* hit bottom? */
439                                  continue;
440 <                        if (freadscan(st, xmax, input[i].fp) < 0) {     /* read */
440 >                        if (freadscan(st, xmax, input[i].fp) < 0) {  /* read */
441                                  eputs(input[i].name);
442                                  eputs(": read error\n");
443                                  quit(1);
444                          }
445 <                        for (j = 0; j < xmax; j++)      /* adjust color */
446 <                                multcolor(st[j], input[i].coef);
445 >                        if (fabs(colval(input[i].coef,RED)-1.0) > 1e-3 ||
446 >                                fabs(colval(input[i].coef,GRN)-1.0) > 1e-3 ||
447 >                                fabs(colval(input[i].coef,BLU)-1.0) > 1e-3)
448 >                                for (j = 0; j < xmax; j++)  /* adjust color */
449 >                                        multcolor(st[j], input[i].coef);
450                  }
451   }
452  
453  
454 < double
455 < l_expos(nam)                    /* return picture exposure */
456 < register char   *nam;
454 > static double
455 > l_expos(                        /* return picture exposure */
456 >        register char   *nam
457 > )
458   {
459          register int    fn, n;
460  
# Line 371 | Line 462 | register char  *nam;
462          if (fn < 0 || fn >= nfiles)
463                  return(1.0);
464          if (nam == vbrtexp)
465 <                return(bright(input[fn].expos));
465 >                return((*ourbright)(input[fn].expos));
466          n = 3;
467          while (n--)
468                  if (nam == vcolexp[n])
469                          return(colval(input[fn].expos,n));
470          eputs("Bad call to l_expos()!\n");
471          quit(1);
472 +        return 1; /* pro forma return */
473   }
474  
475  
476 < double
477 < l_colin(nam)                    /* return color value for picture */
386 < register char   *nam;
476 > static double
477 > l_pixaspect(char *nm)           /* return pixel aspect ratio */
478   {
479 +        register int    fn;
480 +
481 +        fn = argument(1) - .5;
482 +        if (fn < 0 || fn >= nfiles)
483 +                return(1.0);
484 +        return(input[fn].pa);
485 + }
486 +
487 +
488 + static double
489 + l_colin(                        /* return color value for picture */
490 +        register char   *nam
491 + )
492 + {
493          int     fn;
494          register int    n, xoff, yoff;
495          double  d;
496  
497 <        d = argument(1);
393 <        if (d > -.5 && d < .5)
394 <                return((double)nfiles);
395 <        fn = d - .5;
497 >        fn = argument(1) - .5;
498          if (fn < 0 || fn >= nfiles) {
499                  errno = EDOM;
500                  return(0.0);
# Line 428 | Line 530 | register char  *nam;
530                  }
531          }
532          if (nam == vbrtin)
533 <                return(bright(input[fn].scan[MIDSCN+yoff][xscan+xoff]));
533 >                return((*ourbright)(input[fn].scan[MIDSCN+yoff][xscan+xoff]));
534          n = 3;
535          while (n--)
536              if (nam == vcolin[n])
537                  return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n));
538          eputs("Bad call to l_colin()!\n");
539          quit(1);
540 +        return 1; /* pro forma return */
541   }
542  
543  
544 < wputs(msg)
545 < char    *msg;
544 > static double
545 > l_ray(          /* return ray origin or direction */
546 >        register char   *nam
547 > )
548   {
549 +        static unsigned long    ltick[MAXINP];
550 +        static FVECT    lorg[MAXINP], ldir[MAXINP];
551 +        static double   ldist[MAXINP];
552 +        RREAL   loc[2];
553 +        int     fn;
554 +        register int    i;
555 +
556 +        fn = argument(1) - .5;
557 +        if (fn < 0 || fn >= nfiles) {
558 +                errno = EDOM;
559 +                return(0.0);
560 +        }
561 +        if (ltick[fn] != eclock) {              /* need to compute? */
562 +                lorg[fn][0] = lorg[fn][1] = lorg[fn][2] = 0.0;
563 +                ldir[fn][0] = ldir[fn][1] = ldir[fn][2] = 0.0;
564 +                ldist[fn] = -1.0;
565 +                if (input[fn].vw.type == 0)
566 +                        errno = EDOM;
567 +                else {
568 +                        pix2loc(loc, &input[fn].rs, xscan, ymax-1-yscan);
569 +                        ldist[fn] = viewray(lorg[fn], ldir[fn],
570 +                                        &input[fn].vw, loc[0], loc[1]);
571 +                }
572 +                ltick[fn] = eclock;
573 +        }
574 +        if (nam == vray[i=6])
575 +                return(ldist[fn]);
576 +        while (i--)
577 +                if (nam == vray[i])
578 +                        return(i < 3 ? lorg[fn][i] : ldir[fn][i-3]);
579 +        eputs("Bad call to l_ray()!\n");
580 +        quit(1);
581 +        return 1; /* pro forma return */
582 + }
583 +
584 +
585 + static double
586 + l_psize(char *nm)               /* compute pixel size in steradians */
587 + {
588 +        static unsigned long    ltick[MAXINP];
589 +        static double   psize[MAXINP];
590 +        FVECT   dir0, org, dirx, diry;
591 +        RREAL   locx[2], locy[2];
592 +        double  d;
593 +        int     fn;
594 +        register int    i;
595 +
596 +        d = argument(1);
597 +        if (d < .5 || d >= nfiles+.5) {
598 +                errno = EDOM;
599 +                return(0.0);
600 +        }
601 +        fn = d - .5;
602 +        if (ltick[fn] != eclock) {              /* need to compute? */
603 +                psize[fn] = 0.0;
604 +                if (input[fn].vw.type == 0)
605 +                        errno = EDOM;
606 +                else if (input[fn].vw.type != VT_PAR &&
607 +                                funvalue(vray[6], 1, &d) >= 0) {
608 +                        for (i = 0; i < 3; i++)
609 +                                dir0[i] = funvalue(vray[3+i], 1, &d);
610 +                        pix2loc(locx, &input[fn].rs, xscan+1, ymax-1-yscan);
611 +                        pix2loc(locy, &input[fn].rs, xscan, ymax-yscan);
612 +                        if (viewray(org, dirx, &input[fn].vw,
613 +                                        locx[0], locx[1]) >= 0 &&
614 +                                        viewray(org, diry, &input[fn].vw,
615 +                                        locy[0], locy[1]) >= 0) {
616 +                                                /* approximate solid angle */
617 +                                for (i = 0; i < 3; i++) {
618 +                                        dirx[i] -= dir0[i];
619 +                                        diry[i] -= dir0[i];
620 +                                }
621 +                                fcross(dir0, dirx, diry);
622 +                                psize[fn] = sqrt(DOT(dir0,dir0));
623 +                        }
624 +                }
625 +                ltick[fn] = eclock;
626 +        }
627 +        return(psize[fn]);
628 + }
629 +
630 +
631 + extern void
632 + wputs(char      *msg)
633 + {
634          if (!nowarn)
635                  eputs(msg);
636   }
637  
638  
639 < eputs(msg)
640 < char    *msg;
639 > extern void
640 > eputs(char *msg)
641   {
642          fputs(msg, stderr);
643   }
644  
645  
646 < quit(code)
647 < int     code;
646 > extern void
647 > quit(int code)          /* exit gracefully */
648   {
649 +        register int  i;
650 +                                /* close input files */
651 +        for (i = 0; i < nfiles; i++)
652 +                if (input[i].name == Command)
653 +                        pclose(input[i].fp);
654 +                else
655 +                        fclose(input[i].fp);
656          exit(code);
657   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines