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.12 by greg, Tue Apr 23 15:49:53 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  
27   struct {
28 <        char    *name;          /* file name */
28 >        char    *name;          /* file or command name */
29          FILE    *fp;            /* stream pointer */
30 <        COLOR   *scan;          /* input scanline */
30 >        COLOR   *scan[WINSIZ];  /* input scanline window */
31          COLOR   coef;           /* coefficient */
32 +        COLOR   expos;          /* recorded exposure */
33   }       input[MAXINP];                  /* input pictures */
34  
35   int     nfiles;                         /* number of input files */
36  
37 < char    *vcolin[3] = {"ri", "gi", "bi"};
38 < char    *vcolout[3] = {"ro", "go", "bo"};
39 < #define vbrtin          "li"
40 < #define vbrtout         "lo"
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  
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  
54 tputs(s)                        /* put out string preceded by a tab */
55 char    *s;
56 {
57        char    fmt[32];
58        double  d;
59        COLOR   ctmp;
67  
61        if (isformat(s)) {                      /* check format */
62                formatval(fmt, s);
63                wrongformat = strcmp(fmt, COLRFMT);
64        } else if (original && isexpos(s)) {    /* exposure */
65                d = 1.0/exposval(s);
66                scalecolor(input[nfiles].coef, d);
67        } else if (original && iscolcor(s)) {   /* color correction */
68                colcorval(ctmp, s);
69                colval(input[nfiles].coef,RED) /= colval(ctmp,RED);
70                colval(input[nfiles].coef,GRN) /= colval(ctmp,GRN);
71                colval(input[nfiles].coef,BLU) /= colval(ctmp,BLU);
72        } else {                                /* echo unaffected line */
73                putchar('\t');
74                fputs(s, stdout);
75        }
76
77 }
78
79
68   main(argc, argv)
69   int     argc;
70   char    *argv[];
71   {
72 <        extern double   l_redin(), l_grnin(), l_bluin(), l_brtin(), atof();
72 >        int     original;
73          double  f;
74 <        int     a;
75 <        
76 <        funset(vcolin[RED], 1, '=', l_redin);
89 <        funset(vcolin[GRN], 1, '=', l_grnin);
90 <        funset(vcolin[BLU], 1, '=', l_bluin);
91 <        funset(vbrtin, 1, '=', l_brtin);
92 <        
93 <        for (a = 1; a < argc; a++)
74 >        int     a, i;
75 >                                                /* scan options */
76 >        for (a = 1; a < argc; a++) {
77                  if (argv[a][0] == '-')
78                          switch (argv[a][1]) {
96                        case '\0':
97                        case 's':
98                        case 'c':
99                                goto getfiles;
79                          case 'x':
101                                xres = atoi(argv[++a]);
102                                break;
80                          case 'y':
81 <                                yres = atoi(argv[++a]);
82 <                                break;
81 >                                a++;
82 >                                continue;
83                          case 'w':
84                                  nowarn = !nowarn;
85 <                                break;
109 <                        case 'o':
110 <                                original = !original;
111 <                                break;
85 >                                continue;
86                          case 'f':
113                                fcompile(argv[++a]);
114                                break;
87                          case 'e':
88 <                                scompile(argv[++a], NULL, 0);
89 <                                break;
118 <                        default:
119 <                                goto usage;
88 >                                a++;
89 >                                continue;
90                          }
91 <                else
92 <                        break;
93 < getfiles:
94 <        for (nfiles = 0; nfiles < MAXINP; nfiles++)
91 >                break;
92 >        }
93 >                                        /* process files */
94 >        for (nfiles = 0; nfiles < MAXINP; nfiles++) {
95                  setcolor(input[nfiles].coef, 1.0, 1.0, 1.0);
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 136 | Line 109 | getfiles:
109                                  input[nfiles].name = "<stdin>";
110                                  input[nfiles].fp = stdin;
111                                  break;
112 +                        case 'o':
113 +                                original++;
114 +                                continue;
115                          case 's':
116                                  f = atof(argv[++a]);
117                                  scalecolor(input[nfiles].coef, f);
# Line 149 | Line 125 | getfiles:
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 <                fputs(input[nfiles].name, stdout);
141 <                fputs(":\n", stdout);
142 <                getheader(input[nfiles].fp, tputs, NULL);
143 <                if (wrongformat) {
144 <                        eputs(input[nfiles].name);
145 <                        eputs(": not in Radiance picture format\n");
146 <                        quit(1);
140 >                checkfile();
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                  }
167                if (fgetresolu(&xpos, &ypos, input[nfiles].fp) !=
168                                (YMAJOR|YDECR)) {
169                        eputs(input[nfiles].name);
170                        eputs(": bad picture size\n");
171                        quit(1);
172                }
173                if (xres == 0 && yres == 0) {
174                        xres = xpos;
175                        yres = ypos;
176                } else if (xpos != xres || ypos != yres) {
177                        eputs(argv[0]);
178                        eputs(": resolution mismatch\n");
179                        quit(1);
180                }
181                input[nfiles].scan = (COLOR *)emalloc(xres*sizeof(COLOR));
149                  nfiles++;
150 +                original = 0;
151          }
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 +                                varset(vyres, ':', eval(argv[++a]));
162 +                                continue;
163 +                        case 'w':
164 +                                continue;
165 +                        case 'f':
166 +                                fcompile(argv[++a]);
167 +                                continue;
168 +                        case 'e':
169 +                                scompile(argv[++a], NULL, 0);
170 +                                continue;
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);
194   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  
202  
203 + tputs(s)                        /* put out string preceded by a tab */
204 + char    *s;
205 + {
206 +        char    fmt[32];
207 +        double  d;
208 +        COLOR   ctmp;
209 +
210 +        if (isformat(s)) {                      /* check format */
211 +                formatval(fmt, s);
212 +                wrongformat = strcmp(fmt, COLRFMT);
213 +                return;         /* don't echo */
214 +        }
215 +        if (isexpos(s)) {                       /* exposure */
216 +                d = exposval(s);
217 +                scalecolor(input[nfiles].expos, d);
218 +        } else if (iscolcor(s)) {               /* color correction */
219 +                colcorval(ctmp, s);
220 +                multcolor(input[nfiles].expos, ctmp);
221 +        }
222 +                                                /* echo line */
223 +        putchar('\t');
224 +        fputs(s, stdout);
225 + }
226 +
227 +
228 + checkfile()                     /* ready a file */
229 + {
230 +        int     xinp, yinp;
231 +        register int    i;
232 +                                        /* process header */
233 +        fputs(input[nfiles].name, stdout);
234 +        fputs(":\n", stdout);
235 +        getheader(input[nfiles].fp, tputs, NULL);
236 +        if (wrongformat) {
237 +                eputs(input[nfiles].name);
238 +                eputs(": not in Radiance picture format\n");
239 +                quit(1);
240 +        }
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 (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(xmax*sizeof(COLOR));
257 + }
258 +
259 +
260 + init()                                  /* perform final setup */
261 + {
262 +        double  l_colin(), l_expos();
263 +        register int    i;
264 +                                                /* define constants */
265 +        varset(vnfiles, ':', (double)nfiles);
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);
271 +                funset(vcolin[i], 1, '=', l_colin);
272 +        }
273 +        funset(vbrtexp, 1, ':', l_expos);
274 +        funset(vbrtin, 1, '=', l_colin);
275 + }
276 +
277 +
278   combine()                       /* combine pictures */
279   {
280          EPNODE  *coldef[3], *brtdef;
# Line 213 | Line 292 | combine()                      /* combine pictures */
292                  brtdef = eparse(vbrtout);
293          else
294                  brtdef = NULL;
216                                                /* predefine variables */
217        varset(vnfiles, '=', (double)nfiles);
218        varset(vxres, '=', (double)xres);
219        varset(vyres, '=', (double)yres);
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 <            for (i = 0; i < nfiles; i++)
225 <                    if (freadscan(input[i].scan, xres, input[i].fp) < 0) {
226 <                            eputs(input[i].name);
227 <                            eputs(": read error\n");
228 <                            quit(1);
229 <                    }
301 >            advance();
302              varset(vypos, '=', (double)ypos);
303              for (xpos = 0; xpos < xres; xpos++) {
304 <                for (i = 0; i < nfiles; i++)
233 <                        multcolor(input[i].scan[xpos],input[i].coef);
304 >                xscan = (long)xpos*xmax/xres;
305                  varset(vxpos, '=', (double)xpos);
306                  eclock++;
307                  if (brtdef != NULL) {
# Line 241 | Line 312 | combine()                      /* combine pictures */
312                  } else {
313                      for (j = 0; j < 3; j++) {
314                          if (coldef[j] != NULL) {
315 <                                colval(scanout[xpos],j) = evalue(coldef[j]);
315 >                                d = evalue(coldef[j]);
316                          } else {
317 <                            colval(scanout[xpos],j) = 0.0;
317 >                            d = 0.0;
318                              for (i = 0; i < nfiles; i++)
319 <                                colval(scanout[xpos],j) +=
249 <                                        colval(input[i].scan[xpos],j);
319 >                                d += colval(input[i].scan[MIDSCN][xscan],j);
320                          }
321 <                        if (colval(scanout[xpos],j) < 0.0)
322 <                            colval(scanout[xpos],j) = 0.0;
321 >                        if (d < 0.0)
322 >                            d = 0.0;
323 >                        colval(scanout[xpos],j) = d;
324                      }
325                  }
326              }
# Line 262 | Line 333 | combine()                      /* combine pictures */
333   }
334  
335  
336 < double
266 < colin(fn, ci)                   /* return color value for picture */
267 < register int    fn, ci;
336 > advance()                       /* read in data for next scanline */
337   {
338 <        if (fn == 0)
339 <                return((double)nfiles);
340 <        if (fn < 1 || fn > nfiles) {
341 <                errno = EDOM;
273 <                return(0.0);
274 <        }
275 <        return(colval(input[fn-1].scan[xpos],ci));
276 < }
338 >        extern double  fabs();
339 >        int     ytarget;
340 >        register COLOR  *st;
341 >        register int    i, j;
342  
343 <
344 < double
345 < l_redin()                       /* get red color */
346 < {
347 <        return(colin((int)(argument(1)+.5), RED));
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 >                }
362   }
363  
364  
365   double
366 < l_grnin()                       /* get green color */
366 > l_expos(nam)                    /* return picture exposure */
367 > register char   *nam;
368   {
369 <        return(colin((int)(argument(1)+.5), GRN));
290 < }
369 >        register int    fn, n;
370  
371 <
372 < double
373 < l_bluin()                       /* get blue color */
374 < {
375 <        return(colin((int)(argument(1)+.5), BLU));
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;
377 >        while (n--)
378 >                if (nam == vcolexp[n])
379 >                        return(colval(input[fn].expos,n));
380 >        eputs("Bad call to l_expos()!\n");
381 >        quit(1);
382   }
383  
384  
385   double
386 < l_brtin()                       /* get brightness value */
386 > l_colin(nam)                    /* return color value for picture */
387 > register char   *nam;
388   {
389 <        register int    fn;
389 >        int     fn;
390 >        register int    n, xoff, yoff;
391 >        double  d;
392  
393 <        fn = argument(1)+.5;
394 <        if (fn == 0)
393 >        d = argument(1);
394 >        if (d > -.5 && d < .5)
395                  return((double)nfiles);
396 <        if (fn < 1 || fn > nfiles) {
396 >        fn = d - .5;
397 >        if (fn < 0 || fn >= nfiles) {
398                  errno = EDOM;
399                  return(0.0);
400          }
401 <        return(bright(input[fn-1].scan[xpos]));
401 >        xoff = yoff = 0;
402 >        n = nargum();
403 >        if (n >= 2) {
404 >                d = argument(2);
405 >                if (d < 0.0) {
406 >                        xoff = d-.5;
407 >                        if (xscan+xoff < 0)
408 >                                xoff = -xscan;
409 >                } else {
410 >                        xoff = d+.5;
411 >                        if (xscan+xoff >= xmax)
412 >                                xoff = xmax-1-xscan;
413 >                }
414 >        }
415 >        if (n >= 3) {
416 >                d = argument(3);
417 >                if (d < 0.0) {
418 >                        yoff = d-.5;
419 >                        if (yoff+MIDSCN < 0)
420 >                                yoff = -MIDSCN;
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 (yscan+yoff >= ymax)
428 >                                yoff = ymax-1-yscan;
429 >                }
430 >        }
431 >        if (nam == vbrtin)
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][xscan+xoff],n));
437 >        eputs("Bad call to l_colin()!\n");
438 >        quit(1);
439   }
440  
441  
# Line 328 | 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