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.17 by greg, Fri May 24 17:14:49 1991 UTC vs.
Revision 2.6 by greg, Tue Sep 8 10:35:05 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1991 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 16 | Line 16 | static char SCCSid[] = "$SunId$ LBL";
16  
17   #include "color.h"
18  
19 + #include  "resolu.h"
20 +
21   #include "calcomp.h"
22  
23   #define MAXINP          32              /* maximum number of input files */
24   #define WINSIZ          9               /* scanline window size */
25   #define MIDSCN          4               /* current scan position */
26  
25 #define BRT             (-1)            /* special index for brightness */
26
27   struct {
28 <        char    *name;          /* file name */
28 >        char    *name;          /* file or command name */
29          FILE    *fp;            /* stream pointer */
30          COLOR   *scan[WINSIZ];  /* input scanline window */
31          COLOR   coef;           /* coefficient */
# Line 34 | Line 34 | struct {
34  
35   int     nfiles;                         /* number of input files */
36  
37 < char    *vcolin[3] = {"ri", "gi", "bi"};
38 < char    *vcolout[3] = {"ro", "go", "bo"};
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] = {"re", "ge", "be"};
42 > char    vcolexp[3][4] = {"re", "ge", "be"};
43   char    vbrtexp[] = "le";
44  
45 < #define vnfiles         "nfiles"
46 < #define vxres           "xres"
47 < #define vyres           "yres"
48 < #define vxpos           "x"
49 < #define vypos           "y"
45 > char    vnfiles[] = "nfiles";
46 > char    vxmax[] = "xmax";
47 > char    vymax[] = "ymax";
48 > char    vxres[] = "xres";
49 > char    vyres[] = "yres";
50 > char    vxpos[] = "x";
51 > char    vypos[] = "y";
52  
53   int     nowarn = 0;                     /* no warning messages? */
54  
55 < int     original = 0;                   /* origninal values? */
55 > int     xmax = 0, ymax = 0;             /* input resolution */
56  
57 < int     xres=0, yres=0;                 /* picture resolution */
57 > int     xscan, yscan;                   /* input position */
58  
59 < int     xpos, ypos;                     /* picture position */
59 > int     xres, yres;                     /* output resolution */
60  
61 + int     xpos, ypos;                     /* output position */
62 +
63   int     wrongformat = 0;
64  
65 + FILE    *popen();
66  
67 +
68   main(argc, argv)
69   int     argc;
70   char    *argv[];
71   {
72 +        int     original;
73          double  f;
74          int     a, i;
75                                                  /* scan options */
# Line 69 | Line 77 | char   *argv[];
77                  if (argv[a][0] == '-')
78                          switch (argv[a][1]) {
79                          case 'x':
72                                xres = atoi(argv[++a]);
73                                continue;
80                          case 'y':
81 <                                yres = atoi(argv[++a]);
81 >                                a++;
82                                  continue;
83                          case 'w':
84                                  nowarn = !nowarn;
# Line 90 | Line 96 | char   *argv[];
96                  setcolor(input[nfiles].expos, 1.0, 1.0, 1.0);
97          }
98          nfiles = 0;
99 +        original = 0;
100          for ( ; a < argc; a++) {
101                  if (nfiles >= MAXINP) {
102                          eputs(argv[0]);
# Line 104 | Line 111 | char   *argv[];
111                                  break;
112                          case 'o':
113                                  original++;
114 <                                break;
114 >                                continue;
115                          case 's':
116                                  f = atof(argv[++a]);
117                                  scalecolor(input[nfiles].coef, f);
# Line 118 | Line 125 | char   *argv[];
125                                  goto usage;
126                          }
127                  else {
128 <                        input[nfiles].name = argv[a];
129 <                        input[nfiles].fp = fopen(argv[a], "r");
128 >                        if (argv[a][0] == '!') {
129 >                                input[nfiles].name = Command;
130 >                                input[nfiles].fp = popen(argv[a]+1, "r");
131 >                        } else {
132 >                                input[nfiles].name = argv[a];
133 >                                input[nfiles].fp = fopen(argv[a], "r");
134 >                        }
135                          if (input[nfiles].fp == NULL) {
136                                  perror(argv[a]);
137                                  quit(1);
138                          }
139                  }
140                  checkfile();
141 <                original = 0;
141 >                if (original) {
142 >                        colval(input[nfiles].coef,RED) /=
143 >                                        colval(input[nfiles].expos,RED);
144 >                        colval(input[nfiles].coef,GRN) /=
145 >                                        colval(input[nfiles].expos,GRN);
146 >                        colval(input[nfiles].coef,BLU) /=
147 >                                        colval(input[nfiles].expos,BLU);
148 >                }
149                  nfiles++;
150 +                original = 0;
151          }
152 <        init();                         /* set constant expressions */
152 >        init();                         /* set constants */
153                                          /* go back and get expressions */
154          for (a = 1; a < argc; a++) {
155                  if (argv[a][0] == '-')
156                          switch (argv[a][1]) {
157                          case 'x':
158 +                                varset(vxres, ':', eval(argv[++a]));
159 +                                continue;
160                          case 'y':
161 <                                a++;
161 >                                varset(vyres, ':', eval(argv[++a]));
162                                  continue;
163                          case 'w':
164                                  continue;
# Line 149 | Line 171 | char   *argv[];
171                          }
172                  break;
173          }
174 +                                                /* set/get output resolution */
175 +        if (!vardefined(vxres))
176 +                varset(vxres, ':', (double)xmax);
177 +        if (!vardefined(vyres))
178 +                varset(vyres, ':', (double)ymax);
179 +        xres = varvalue(vxres) + .5;
180 +        yres = varvalue(vyres) + .5;
181 +        if (xres <= 0 || yres <= 0) {
182 +                eputs(argv[0]);
183 +                eputs(": illegal output resolution\n");
184 +                quit(1);
185 +        }
186                                                  /* complete header */
187          printargs(argc, argv, stdout);
188          fputformat(COLRFMT, stdout);
189          putchar('\n');
190 <        fputresolu(YMAJOR|YDECR, xres, yres, stdout);
190 >        fprtresolu(xres, yres, stdout);
191                                                  /* combine pictures */
192          combine();
193          quit(0);
# Line 161 | Line 195 | usage:
195          eputs("Usage: ");
196          eputs(argv[0]);
197          eputs(
198 < " [-w][-h][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
198 > " [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
199          quit(1);
200   }
201  
# Line 181 | Line 215 | char   *s;
215          if (isexpos(s)) {                       /* exposure */
216                  d = exposval(s);
217                  scalecolor(input[nfiles].expos, d);
184                if (original)
185                        scalecolor(input[nfiles].coef, 1.0/d);
218          } else if (iscolcor(s)) {               /* color correction */
219                  colcorval(ctmp, s);
220                  multcolor(input[nfiles].expos, ctmp);
189                if (original) {
190                        colval(input[nfiles].coef,RED) /= colval(ctmp,RED);
191                        colval(input[nfiles].coef,GRN) /= colval(ctmp,GRN);
192                        colval(input[nfiles].coef,BLU) /= colval(ctmp,BLU);
193                }
221          }
222                                                  /* echo line */
223          putchar('\t');
# Line 200 | Line 227 | char   *s;
227  
228   checkfile()                     /* ready a file */
229   {
230 +        int     xinp, yinp;
231          register int    i;
232                                          /* process header */
233          fputs(input[nfiles].name, stdout);
# Line 210 | Line 238 | checkfile()                    /* ready a file */
238                  eputs(": not in Radiance picture format\n");
239                  quit(1);
240          }
241 <        if (fgetresolu(&xpos, &ypos, input[nfiles].fp) != (YMAJOR|YDECR)) {
241 >        if (fgetresolu(&xinp, &yinp, input[nfiles].fp) < 0) {
242                  eputs(input[nfiles].name);
243                  eputs(": bad picture size\n");
244                  quit(1);
245          }
246 <        if (xres == 0 && yres == 0) {
247 <                xres = xpos;
248 <                yres = ypos;
249 <        } else if (xpos != xres || ypos != yres) {
246 >        if (xmax == 0 && ymax == 0) {
247 >                xmax = xinp;
248 >                ymax = yinp;
249 >        } else if (xinp != xmax || yinp != ymax) {
250                  eputs(input[nfiles].name);
251                  eputs(": resolution mismatch\n");
252                  quit(1);
253          }
254                                          /* allocate scanlines */
255          for (i = 0; i < WINSIZ; i++)
256 <                input[nfiles].scan[i] = (COLOR *)emalloc(xres*sizeof(COLOR));
256 >                input[nfiles].scan[i] = (COLOR *)emalloc(xmax*sizeof(COLOR));
257   }
258  
259  
260 < init()                          /* perform final setup */
260 > init()                                  /* perform final setup */
261   {
262          double  l_colin(), l_expos();
263          register int    i;
236                                                /* prime input */
237        for (ypos = yres+(MIDSCN-1); ypos >= yres; ypos--)
238                advance();
264                                                  /* define constants */
265          varset(vnfiles, ':', (double)nfiles);
266 <        varset(vxres, ':', (double)xres);
267 <        varset(vyres, ':', (double)yres);
266 >        varset(vxmax, ':', (double)xmax);
267 >        varset(vymax, ':', (double)ymax);
268                                                  /* set functions */
269          for (i = 0; i < 3; i++) {
270                  funset(vcolexp[i], 1, ':', l_expos);
# Line 269 | Line 294 | combine()                      /* combine pictures */
294                  brtdef = NULL;
295                                                  /* allocate scanline */
296          scanout = (COLOR *)emalloc(xres*sizeof(COLOR));
297 +                                                /* set input position */
298 +        yscan = ymax+MIDSCN;
299                                                  /* combine files */
300          for (ypos = yres-1; ypos >= 0; ypos--) {
301              advance();
302              varset(vypos, '=', (double)ypos);
303              for (xpos = 0; xpos < xres; xpos++) {
304 +                xscan = (long)xpos*xmax/xres;
305                  varset(vxpos, '=', (double)xpos);
306                  eclock++;
307                  if (brtdef != NULL) {
# Line 288 | Line 316 | combine()                      /* combine pictures */
316                          } else {
317                              d = 0.0;
318                              for (i = 0; i < nfiles; i++)
319 <                                d += colval(input[i].scan[MIDSCN][xpos],j);
319 >                                d += colval(input[i].scan[MIDSCN][xscan],j);
320                          }
321                          if (d < 0.0)
322                              d = 0.0;
# Line 305 | Line 333 | combine()                      /* combine pictures */
333   }
334  
335  
336 < advance()                       /* read in next scanline */
336 > advance()                       /* read in data for next scanline */
337   {
338 +        extern double  fabs();
339 +        int     ytarget;
340          register COLOR  *st;
341          register int    i, j;
342  
343 <        for (i = 0; i < nfiles; i++) {
344 <                st = input[i].scan[WINSIZ-1];
345 <                for (j = WINSIZ-1; j > 0; j--)  /* rotate window */
346 <                        input[i].scan[j] = input[i].scan[j-1];
347 <                input[i].scan[0] = st;
348 <                if (ypos < MIDSCN)              /* hit bottom */
349 <                        continue;
350 <                if (freadscan(st, xres, input[i].fp) < 0) {
351 <                        eputs(input[i].name);
352 <                        eputs(": read error\n");
353 <                        quit(1);
343 >        for (ytarget = (long)ypos*ymax/yres; yscan > ytarget; yscan--)
344 >                for (i = 0; i < nfiles; i++) {
345 >                        st = input[i].scan[WINSIZ-1];
346 >                        for (j = WINSIZ-1; j > 0; j--)  /* rotate window */
347 >                                input[i].scan[j] = input[i].scan[j-1];
348 >                        input[i].scan[0] = st;
349 >                        if (yscan <= MIDSCN)            /* hit bottom? */
350 >                                continue;
351 >                        if (freadscan(st, xmax, input[i].fp) < 0) {  /* read */
352 >                                eputs(input[i].name);
353 >                                eputs(": read error\n");
354 >                                quit(1);
355 >                        }
356 >                        if (fabs(colval(input[i].coef,RED)-1.0) > 1e-3 ||
357 >                                fabs(colval(input[i].coef,GRN)-1.0) > 1e-3 ||
358 >                                fabs(colval(input[i].coef,BLU)-1.0) > 1e-3)
359 >                                for (j = 0; j < xmax; j++)  /* adjust color */
360 >                                        multcolor(st[j], input[i].coef);
361                  }
325                for (j = 0; j < xres; j++)      /* adjust color */
326                        multcolor(st[j], input[i].coef);
327        }
362   }
363  
364  
# Line 333 | Line 367 | l_expos(nam)                   /* return picture exposure */
367   register char   *nam;
368   {
369          register int    fn, n;
336        double  d;
370  
371 <        d = argument(1);
372 <        if (d > -.5 && d < .5)
373 <                return((double)nfiles);
341 <        fn = d - .5;
342 <        if (fn < 0 || fn >= nfiles) {
343 <                errno = EDOM;
344 <                return(0.0);
345 <        }
371 >        fn = argument(1) - .5;
372 >        if (fn < 0 || fn >= nfiles)
373 >                return(1.0);
374          if (nam == vbrtexp)
375                  return(bright(input[fn].expos));
376          n = 3;
# Line 376 | Line 404 | register char  *nam;
404                  d = argument(2);
405                  if (d < 0.0) {
406                          xoff = d-.5;
407 <                        if (xpos+xoff < 0)
408 <                                xoff = -xpos;
407 >                        if (xscan+xoff < 0)
408 >                                xoff = -xscan;
409                  } else {
410                          xoff = d+.5;
411 <                        if (xpos+xoff >= xres)
412 <                                xoff = xres-1-xpos;
411 >                        if (xscan+xoff >= xmax)
412 >                                xoff = xmax-1-xscan;
413                  }
414          }
415          if (n >= 3) {
# Line 390 | Line 418 | register char  *nam;
418                          yoff = d-.5;
419                          if (yoff+MIDSCN < 0)
420                                  yoff = -MIDSCN;
421 <                        if (ypos+yoff < 0)
422 <                                yoff = -ypos;
421 >                        if (yscan+yoff < 0)
422 >                                yoff = -yscan;
423                  } else {
424                          yoff = d+.5;
425                          if (yoff+MIDSCN >= WINSIZ)
426                                  yoff = WINSIZ-1-MIDSCN;
427 <                        if (ypos+yoff >= yres)
428 <                                yoff = yres-1-ypos;
427 >                        if (yscan+yoff >= ymax)
428 >                                yoff = ymax-1-yscan;
429                  }
430          }
431          if (nam == vbrtin)
432 <                return(bright(input[fn].scan[MIDSCN+yoff][xpos+xoff]));
432 >                return(bright(input[fn].scan[MIDSCN+yoff][xscan+xoff]));
433          n = 3;
434          while (n--)
435              if (nam == vcolin[n])
436 <                return(colval(input[fn].scan[MIDSCN+yoff][xpos+xoff],n));
436 >                return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n));
437          eputs("Bad call to l_colin()!\n");
438          quit(1);
439   }
# Line 426 | Line 454 | char   *msg;
454   }
455  
456  
457 < quit(code)
458 < int     code;
457 > quit(code)              /* exit gracefully */
458 > int  code;
459   {
460 +        register int  i;
461 +                                /* close input files */
462 +        for (i = 0; i < nfiles; i++)
463 +                if (input[i].name == Command)
464 +                        pclose(input[i].fp);
465 +                else
466 +                        fclose(input[i].fp);
467          exit(code);
468   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines