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.14 by greg, Thu May 23 13:46:05 1991 UTC vs.
Revision 2.8 by greg, Fri Oct 2 16:22:51 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 12 | Line 12 | static char SCCSid[] = "$SunId$ LBL";
12  
13   #include <stdio.h>
14  
15 + #ifdef MSDOS
16 + #include <fcntl.h>
17 + #endif
18 +
19 + #include <math.h>
20 +
21   #include <errno.h>
22  
23   #include "color.h"
24  
25 + #include "resolu.h"
26 +
27   #include "calcomp.h"
28  
29   #define MAXINP          32              /* maximum number of input files */
30   #define WINSIZ          9               /* scanline window size */
31   #define MIDSCN          4               /* current scan position */
32  
25 #define BRT             (-1)            /* special index for brightness */
26
33   struct {
34 <        char    *name;          /* file name */
34 >        char    *name;          /* file or command name */
35          FILE    *fp;            /* stream pointer */
36          COLOR   *scan[WINSIZ];  /* input scanline window */
37          COLOR   coef;           /* coefficient */
38 +        COLOR   expos;          /* recorded exposure */
39   }       input[MAXINP];                  /* input pictures */
40  
41   int     nfiles;                         /* number of input files */
42  
43 < char    *vcolin[3] = {"ri", "gi", "bi"};
44 < char    *vcolout[3] = {"ro", "go", "bo"};
45 < #define vbrtin          "li"
46 < #define vbrtout         "lo"
43 > char    Command[] = "<Command>";
44 > char    vcolin[3][4] = {"ri", "gi", "bi"};
45 > char    vcolout[3][4] = {"ro", "go", "bo"};
46 > char    vbrtin[] = "li";
47 > char    vbrtout[] = "lo";
48 > char    vcolexp[3][4] = {"re", "ge", "be"};
49 > char    vbrtexp[] = "le";
50  
51 < #define vnfiles         "nfiles"
52 < #define vxres           "xres"
53 < #define vyres           "yres"
54 < #define vxpos           "x"
55 < #define vypos           "y"
51 > char    vnfiles[] = "nfiles";
52 > char    vxmax[] = "xmax";
53 > char    vymax[] = "ymax";
54 > char    vxres[] = "xres";
55 > char    vyres[] = "yres";
56 > char    vxpos[] = "x";
57 > char    vypos[] = "y";
58  
59   int     nowarn = 0;                     /* no warning messages? */
60  
61 < int     original = 0;                   /* origninal values? */
61 > int     xmax = 0, ymax = 0;             /* input resolution */
62  
63 < int     xres=0, yres=0;                 /* picture resolution */
63 > int     xscan, yscan;                   /* input position */
64  
65 < int     xpos, ypos;                     /* picture position */
65 > int     xres, yres;                     /* output resolution */
66  
67 + int     xpos, ypos;                     /* output position */
68 +
69   int     wrongformat = 0;
70  
71 + FILE    *popen();
72  
73 < tputs(s)                        /* put out string preceded by a tab */
59 < char    *s;
60 < {
61 <        char    fmt[32];
62 <        double  d;
63 <        COLOR   ctmp;
73 > extern char     *emalloc();
74  
65        if (isformat(s)) {                      /* check format */
66                formatval(fmt, s);
67                wrongformat = strcmp(fmt, COLRFMT);
68        } else if (original && isexpos(s)) {    /* exposure */
69                d = 1.0/exposval(s);
70                scalecolor(input[nfiles].coef, d);
71        } else if (original && iscolcor(s)) {   /* color correction */
72                colcorval(ctmp, s);
73                colval(input[nfiles].coef,RED) /= colval(ctmp,RED);
74                colval(input[nfiles].coef,GRN) /= colval(ctmp,GRN);
75                colval(input[nfiles].coef,BLU) /= colval(ctmp,BLU);
76        } else {                                /* echo unaffected line */
77                putchar('\t');
78                fputs(s, stdout);
79        }
75  
81 }
82
83
76   main(argc, argv)
77   int     argc;
78   char    *argv[];
79   {
80 <        extern double   l_redin(), l_grnin(), l_bluin(), l_brtin(), atof();
80 >        int     original;
81          double  f;
82          int     a, i;
83 <        
84 <        funset(vcolin[RED], 1, '=', l_redin);
85 <        funset(vcolin[GRN], 1, '=', l_grnin);
86 <        funset(vcolin[BLU], 1, '=', l_bluin);
87 <        funset(vbrtin, 1, '=', l_brtin);
88 <        
89 <        for (a = 1; a < argc; a++)
83 > #ifdef MSDOS
84 >        extern int  _fmode;
85 >        _fmode = O_BINARY;
86 >        setmode(fileno(stdin), O_BINARY);
87 >        setmode(fileno(stdout), O_BINARY);
88 > #endif
89 >                                                /* scan options */
90 >        for (a = 1; a < argc; a++) {
91                  if (argv[a][0] == '-')
92                          switch (argv[a][1]) {
100                        case '\0':
101                        case 's':
102                        case 'c':
103                                goto getfiles;
93                          case 'x':
105                                xres = atoi(argv[++a]);
106                                break;
94                          case 'y':
95 <                                yres = atoi(argv[++a]);
96 <                                break;
95 >                                a++;
96 >                                continue;
97                          case 'w':
98                                  nowarn = !nowarn;
99 <                                break;
113 <                        case 'o':
114 <                                original = !original;
115 <                                break;
99 >                                continue;
100                          case 'f':
117                                fcompile(argv[++a]);
118                                break;
101                          case 'e':
102 <                                scompile(argv[++a], NULL, 0);
103 <                                break;
122 <                        default:
123 <                                goto usage;
102 >                                a++;
103 >                                continue;
104                          }
105 <                else
106 <                        break;
107 < getfiles:
108 <        for (nfiles = 0; nfiles < MAXINP; nfiles++)
105 >                break;
106 >        }
107 >                                        /* process files */
108 >        for (nfiles = 0; nfiles < MAXINP; nfiles++) {
109                  setcolor(input[nfiles].coef, 1.0, 1.0, 1.0);
110 +                setcolor(input[nfiles].expos, 1.0, 1.0, 1.0);
111 +        }
112          nfiles = 0;
113 +        original = 0;
114          for ( ; a < argc; a++) {
115                  if (nfiles >= MAXINP) {
116                          eputs(argv[0]);
# Line 140 | Line 123 | getfiles:
123                                  input[nfiles].name = "<stdin>";
124                                  input[nfiles].fp = stdin;
125                                  break;
126 +                        case 'o':
127 +                                original++;
128 +                                continue;
129                          case 's':
130                                  f = atof(argv[++a]);
131                                  scalecolor(input[nfiles].coef, f);
# Line 153 | Line 139 | getfiles:
139                                  goto usage;
140                          }
141                  else {
142 <                        input[nfiles].name = argv[a];
143 <                        input[nfiles].fp = fopen(argv[a], "r");
142 >                        if (argv[a][0] == '!') {
143 >                                input[nfiles].name = Command;
144 >                                input[nfiles].fp = popen(argv[a]+1, "r");
145 >                        } else {
146 >                                input[nfiles].name = argv[a];
147 >                                input[nfiles].fp = fopen(argv[a], "r");
148 >                        }
149                          if (input[nfiles].fp == NULL) {
150                                  perror(argv[a]);
151                                  quit(1);
152                          }
153                  }
154 <                fputs(input[nfiles].name, stdout);
155 <                fputs(":\n", stdout);
156 <                getheader(input[nfiles].fp, tputs, NULL);
157 <                if (wrongformat) {
158 <                        eputs(input[nfiles].name);
159 <                        eputs(": not in Radiance picture format\n");
160 <                        quit(1);
154 >                checkfile();
155 >                if (original) {
156 >                        colval(input[nfiles].coef,RED) /=
157 >                                        colval(input[nfiles].expos,RED);
158 >                        colval(input[nfiles].coef,GRN) /=
159 >                                        colval(input[nfiles].expos,GRN);
160 >                        colval(input[nfiles].coef,BLU) /=
161 >                                        colval(input[nfiles].expos,BLU);
162                  }
171                if (fgetresolu(&xpos, &ypos, input[nfiles].fp) !=
172                                (YMAJOR|YDECR)) {
173                        eputs(input[nfiles].name);
174                        eputs(": bad picture size\n");
175                        quit(1);
176                }
177                if (xres == 0 && yres == 0) {
178                        xres = xpos;
179                        yres = ypos;
180                } else if (xpos != xres || ypos != yres) {
181                        eputs(argv[0]);
182                        eputs(": resolution mismatch\n");
183                        quit(1);
184                }
185                for (i = 0; i < WINSIZ; i++)
186                        input[nfiles].scan[i] =
187                                        (COLOR *)emalloc(xres*sizeof(COLOR));
163                  nfiles++;
164 +                original = 0;
165          }
166 +        init();                         /* set constants */
167 +                                        /* go back and get expressions */
168 +        for (a = 1; a < argc; a++) {
169 +                if (argv[a][0] == '-')
170 +                        switch (argv[a][1]) {
171 +                        case 'x':
172 +                                varset(vxres, ':', eval(argv[++a]));
173 +                                continue;
174 +                        case 'y':
175 +                                varset(vyres, ':', eval(argv[++a]));
176 +                                continue;
177 +                        case 'w':
178 +                                continue;
179 +                        case 'f':
180 +                                fcompile(argv[++a]);
181 +                                continue;
182 +                        case 'e':
183 +                                scompile(argv[++a], NULL, 0);
184 +                                continue;
185 +                        }
186 +                break;
187 +        }
188 +                                                /* set/get output resolution */
189 +        if (!vardefined(vxres))
190 +                varset(vxres, ':', (double)xmax);
191 +        if (!vardefined(vyres))
192 +                varset(vyres, ':', (double)ymax);
193 +        xres = varvalue(vxres) + .5;
194 +        yres = varvalue(vyres) + .5;
195 +        if (xres <= 0 || yres <= 0) {
196 +                eputs(argv[0]);
197 +                eputs(": illegal output resolution\n");
198 +                quit(1);
199 +        }
200 +                                                /* complete header */
201          printargs(argc, argv, stdout);
202          fputformat(COLRFMT, stdout);
203          putchar('\n');
204 <        fputresolu(YMAJOR|YDECR, xres, yres, stdout);
204 >        fprtresolu(xres, yres, stdout);
205 >                                                /* combine pictures */
206          combine();
207          quit(0);
208   usage:
209          eputs("Usage: ");
210          eputs(argv[0]);
211          eputs(
212 < " [-w][-h][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
212 > " [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
213          quit(1);
214   }
215  
216  
217 + tputs(s)                        /* put out string preceded by a tab */
218 + char    *s;
219 + {
220 +        char    fmt[32];
221 +        double  d;
222 +        COLOR   ctmp;
223 +
224 +        if (isformat(s)) {                      /* check format */
225 +                formatval(fmt, s);
226 +                wrongformat = strcmp(fmt, COLRFMT);
227 +                return;         /* don't echo */
228 +        }
229 +        if (isexpos(s)) {                       /* exposure */
230 +                d = exposval(s);
231 +                scalecolor(input[nfiles].expos, d);
232 +        } else if (iscolcor(s)) {               /* color correction */
233 +                colcorval(ctmp, s);
234 +                multcolor(input[nfiles].expos, ctmp);
235 +        }
236 +                                                /* echo line */
237 +        putchar('\t');
238 +        fputs(s, stdout);
239 + }
240 +
241 +
242 + checkfile()                     /* ready a file */
243 + {
244 +        int     xinp, yinp;
245 +        register int    i;
246 +                                        /* process header */
247 +        fputs(input[nfiles].name, stdout);
248 +        fputs(":\n", stdout);
249 +        getheader(input[nfiles].fp, tputs, NULL);
250 +        if (wrongformat) {
251 +                eputs(input[nfiles].name);
252 +                eputs(": not in Radiance picture format\n");
253 +                quit(1);
254 +        }
255 +        if (fgetresolu(&xinp, &yinp, input[nfiles].fp) < 0) {
256 +                eputs(input[nfiles].name);
257 +                eputs(": bad picture size\n");
258 +                quit(1);
259 +        }
260 +        if (xmax == 0 && ymax == 0) {
261 +                xmax = xinp;
262 +                ymax = yinp;
263 +        } else if (xinp != xmax || yinp != ymax) {
264 +                eputs(input[nfiles].name);
265 +                eputs(": resolution mismatch\n");
266 +                quit(1);
267 +        }
268 +                                        /* allocate scanlines */
269 +        for (i = 0; i < WINSIZ; i++)
270 +                input[nfiles].scan[i] = (COLOR *)emalloc(xmax*sizeof(COLOR));
271 + }
272 +
273 +
274 + init()                                  /* perform final setup */
275 + {
276 +        double  l_colin(), l_expos();
277 +        register int    i;
278 +                                                /* define constants */
279 +        varset(vnfiles, ':', (double)nfiles);
280 +        varset(vxmax, ':', (double)xmax);
281 +        varset(vymax, ':', (double)ymax);
282 +                                                /* set functions */
283 +        for (i = 0; i < 3; i++) {
284 +                funset(vcolexp[i], 1, ':', l_expos);
285 +                funset(vcolin[i], 1, '=', l_colin);
286 +        }
287 +        funset(vbrtexp, 1, ':', l_expos);
288 +        funset(vbrtin, 1, '=', l_colin);
289 + }
290 +
291 +
292   combine()                       /* combine pictures */
293   {
294          EPNODE  *coldef[3], *brtdef;
# Line 219 | Line 306 | combine()                      /* combine pictures */
306                  brtdef = eparse(vbrtout);
307          else
308                  brtdef = NULL;
222                                                /* define constants */
223        varset(vnfiles, ':', (double)nfiles);
224        varset(vxres, ':', (double)xres);
225        varset(vyres, ':', (double)yres);
309                                                  /* allocate scanline */
310          scanout = (COLOR *)emalloc(xres*sizeof(COLOR));
311 <                                                /* initialize input */
312 <        initinp();
311 >                                                /* set input position */
312 >        yscan = ymax+MIDSCN;
313                                                  /* combine files */
314          for (ypos = yres-1; ypos >= 0; ypos--) {
315              advance();
316              varset(vypos, '=', (double)ypos);
317              for (xpos = 0; xpos < xres; xpos++) {
318 +                xscan = (long)xpos*xmax/xres;
319                  varset(vxpos, '=', (double)xpos);
320                  eclock++;
321                  if (brtdef != NULL) {
# Line 246 | Line 330 | combine()                      /* combine pictures */
330                          } else {
331                              d = 0.0;
332                              for (i = 0; i < nfiles; i++)
333 <                                d += colval(input[i].scan[MIDSCN][xpos],j);
333 >                                d += colval(input[i].scan[MIDSCN][xscan],j);
334                          }
335                          if (d < 0.0)
336                              d = 0.0;
# Line 263 | Line 347 | combine()                      /* combine pictures */
347   }
348  
349  
350 < initinp()                       /* initilize scan windows */
350 > advance()                       /* read in data for next scanline */
351   {
352 <        for (ypos = yres+(MIDSCN-1); ypos >= yres; ypos--)
353 <                advance();
352 >        int     ytarget;
353 >        register COLOR  *st;
354 >        register int    i, j;
355 >
356 >        for (ytarget = (long)ypos*ymax/yres; yscan > ytarget; yscan--)
357 >                for (i = 0; i < nfiles; i++) {
358 >                        st = input[i].scan[WINSIZ-1];
359 >                        for (j = WINSIZ-1; j > 0; j--)  /* rotate window */
360 >                                input[i].scan[j] = input[i].scan[j-1];
361 >                        input[i].scan[0] = st;
362 >                        if (yscan <= MIDSCN)            /* hit bottom? */
363 >                                continue;
364 >                        if (freadscan(st, xmax, input[i].fp) < 0) {  /* read */
365 >                                eputs(input[i].name);
366 >                                eputs(": read error\n");
367 >                                quit(1);
368 >                        }
369 >                        if (fabs(colval(input[i].coef,RED)-1.0) > 1e-3 ||
370 >                                fabs(colval(input[i].coef,GRN)-1.0) > 1e-3 ||
371 >                                fabs(colval(input[i].coef,BLU)-1.0) > 1e-3)
372 >                                for (j = 0; j < xmax; j++)  /* adjust color */
373 >                                        multcolor(st[j], input[i].coef);
374 >                }
375   }
376  
377  
378 < advance()                       /* read in next scanline */
378 > double
379 > l_expos(nam)                    /* return picture exposure */
380 > register char   *nam;
381   {
382 <        register COLOR  *st;
276 <        register int    i, j;
382 >        register int    fn, n;
383  
384 <        for (i = 0; i < nfiles; i++) {
385 <                st = input[i].scan[WINSIZ-1];
386 <                for (j = WINSIZ-1; j > 0; j--)  /* rotate window */
387 <                        input[i].scan[j] = input[i].scan[j-1];
388 <                input[i].scan[0] = st;
389 <                if (ypos < MIDSCN)              /* hit bottom */
390 <                        continue;
391 <                if (freadscan(st, xres, input[i].fp) < 0) {
392 <                        eputs(input[i].name);
393 <                        eputs(": read error\n");
394 <                        quit(1);
289 <                }
290 <                for (j = 0; j < xres; j++)      /* adjust color */
291 <                        multcolor(st[j], input[i].coef);
292 <        }
384 >        fn = argument(1) - .5;
385 >        if (fn < 0 || fn >= nfiles)
386 >                return(1.0);
387 >        if (nam == vbrtexp)
388 >                return(bright(input[fn].expos));
389 >        n = 3;
390 >        while (n--)
391 >                if (nam == vcolexp[n])
392 >                        return(colval(input[fn].expos,n));
393 >        eputs("Bad call to l_expos()!\n");
394 >        quit(1);
395   }
396  
397  
398   double
399 < colin(ci)                       /* return color value for picture */
400 < register int    ci;
399 > l_colin(nam)                    /* return color value for picture */
400 > register char   *nam;
401   {
402 <        int     n, fn;
403 <        register int    xoff, yoff;
402 >        int     fn;
403 >        register int    n, xoff, yoff;
404          double  d;
405  
406 <        fn = (d = argument(1)) - .5;
407 <        if (d <= -.5 || fn >= nfiles) {
406 >        d = argument(1);
407 >        if (d > -.5 && d < .5)
408 >                return((double)nfiles);
409 >        fn = d - .5;
410 >        if (fn < 0 || fn >= nfiles) {
411                  errno = EDOM;
412                  return(0.0);
413          }
309        if (d < .5)
310                return((double)nfiles);
414          xoff = yoff = 0;
415          n = nargum();
416          if (n >= 2) {
417                  d = argument(2);
418                  if (d < 0.0) {
419                          xoff = d-.5;
420 <                        if (xpos+xoff < 0)
421 <                                xoff = -xpos;
420 >                        if (xscan+xoff < 0)
421 >                                xoff = -xscan;
422                  } else {
423                          xoff = d+.5;
424 <                        if (xpos+xoff >= xres)
425 <                                xoff = xres-1-xpos;
424 >                        if (xscan+xoff >= xmax)
425 >                                xoff = xmax-1-xscan;
426                  }
427          }
428          if (n >= 3) {
# Line 328 | Line 431 | register int   ci;
431                          yoff = d-.5;
432                          if (yoff+MIDSCN < 0)
433                                  yoff = -MIDSCN;
434 <                        if (ypos+yoff < 0)
435 <                                yoff = -ypos;
434 >                        if (yscan+yoff < 0)
435 >                                yoff = -yscan;
436                  } else {
437                          yoff = d+.5;
438                          if (yoff+MIDSCN >= WINSIZ)
439                                  yoff = WINSIZ-1-MIDSCN;
440 <                        if (ypos+yoff >= yres)
441 <                                yoff = yres-1-ypos;
440 >                        if (yscan+yoff >= ymax)
441 >                                yoff = ymax-1-yscan;
442                  }
443          }
444 <        if (ci == BRT)
445 <                return(bright(input[fn].scan[MIDSCN+yoff][xpos+xoff]));
446 <        return(colval(input[fn].scan[MIDSCN+yoff][xpos+xoff],ci));
444 >        if (nam == vbrtin)
445 >                return(bright(input[fn].scan[MIDSCN+yoff][xscan+xoff]));
446 >        n = 3;
447 >        while (n--)
448 >            if (nam == vcolin[n])
449 >                return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n));
450 >        eputs("Bad call to l_colin()!\n");
451 >        quit(1);
452   }
453  
454  
347 double
348 l_redin()                       /* get red color */
349 {
350        return(colin(RED));
351 }
352
353
354 double
355 l_grnin()                       /* get green color */
356 {
357        return(colin(GRN));
358 }
359
360
361 double
362 l_bluin()                       /* get blue color */
363 {
364        return(colin(BLU));
365 }
366
367
368 double
369 l_brtin()                       /* get brightness value */
370 {
371        return(colin(BRT));
372 }
373
374
455   wputs(msg)
456   char    *msg;
457   {
# Line 387 | Line 467 | char   *msg;
467   }
468  
469  
470 < quit(code)
471 < int     code;
470 > quit(code)              /* exit gracefully */
471 > int  code;
472   {
473 +        register int  i;
474 +                                /* close input files */
475 +        for (i = 0; i < nfiles; i++)
476 +                if (input[i].name == Command)
477 +                        pclose(input[i].fp);
478 +                else
479 +                        fclose(input[i].fp);
480          exit(code);
481   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines