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.5 by greg, Tue Sep 12 13:04:22 1989 UTC vs.
Revision 1.16 by greg, Fri May 24 15:47:53 1991 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1989 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 18 | Line 18 | static char SCCSid[] = "$SunId$ LBL";
18  
19   #include "calcomp.h"
20  
21 < #define MAXINP          16              /* maximum number of input files */
21 > #define MAXINP          32              /* maximum number of input files */
22 > #define WINSIZ          9               /* scanline window size */
23 > #define MIDSCN          4               /* current scan position */
24  
25 + #define BRT             (-1)            /* special index for brightness */
26 +
27   struct {
28          char    *name;          /* file name */
29          FILE    *fp;            /* stream pointer */
30 <        COLOR   *scan;          /* input scanline */
30 >        COLOR   *scan[WINSIZ];  /* input scanline window */
31          COLOR   coef;           /* coefficient */
32   }       input[MAXINP];                  /* input pictures */
33  
# Line 31 | Line 35 | int    nfiles;                         /* number of input files */
35  
36   char    *vcolin[3] = {"ri", "gi", "bi"};
37   char    *vcolout[3] = {"ro", "go", "bo"};
38 + char    vbrtin[] = "li";
39 + char    vbrtout[] = "lo";
40 + char    *vcolcoef[3] = {"rc", "gc", "bc"};
41 + char    vbrtcoef[] = "lc";
42  
43 + #define vnfiles         "nfiles"
44 + #define vxres           "xres"
45 + #define vyres           "yres"
46   #define vxpos           "x"
47   #define vypos           "y"
48  
49   int     nowarn = 0;                     /* no warning messages? */
50  
51 + int     original = 0;                   /* origninal values? */
52 +
53   int     xres=0, yres=0;                 /* picture resolution */
54  
55   int     xpos, ypos;                     /* picture position */
56  
57 + int     wrongformat = 0;
58  
45 tputs(s)                        /* put out string preceded by a tab */
46 char    *s;
47 {
48        putchar('\t');
49        fputs(s, stdout);
50 }
59  
52
60   main(argc, argv)
61   int     argc;
62   char    *argv[];
63   {
57        extern double   l_redin(), l_grnin(), l_bluin(), atof();
64          double  f;
65 <        int     a;
66 <        
67 <        funset(vcolin[RED], 1, l_redin);
62 <        funset(vcolin[GRN], 1, l_grnin);
63 <        funset(vcolin[BLU], 1, l_bluin);
64 <        
65 <        for (a = 1; a < argc; a++)
65 >        int     a, i;
66 >                                                /* scan options */
67 >        for (a = 1; a < argc; a++) {
68                  if (argv[a][0] == '-')
69                          switch (argv[a][1]) {
68                        case '\0':
69                        case 's':
70                        case 'c':
71                                goto getfiles;
70                          case 'x':
71                                  xres = atoi(argv[++a]);
72 <                                break;
72 >                                continue;
73                          case 'y':
74                                  yres = atoi(argv[++a]);
75 <                                break;
75 >                                continue;
76                          case 'w':
77                                  nowarn = !nowarn;
78 <                                break;
78 >                                continue;
79 >                        case 'o':
80 >                                original = !original;
81 >                                continue;
82                          case 'f':
82                                fcompile(argv[++a]);
83                                break;
83                          case 'e':
84 <                                scompile(NULL, argv[++a]);
85 <                                break;
87 <                        default:
88 <                                goto usage;
84 >                                a++;
85 >                                continue;
86                          }
87 <                else
88 <                        break;
89 < getfiles:
87 >                break;
88 >        }
89 >                                        /* process files */
90          for (nfiles = 0; nfiles < MAXINP; nfiles++)
91                  setcolor(input[nfiles].coef, 1.0, 1.0, 1.0);
92          nfiles = 0;
# Line 121 | Line 118 | getfiles:
118                          input[nfiles].name = argv[a];
119                          input[nfiles].fp = fopen(argv[a], "r");
120                          if (input[nfiles].fp == NULL) {
121 <                                eputs(argv[a]);
125 <                                eputs(": cannot open\n");
121 >                                perror(argv[a]);
122                                  quit(1);
123                          }
124                  }
125 <                fputs(input[nfiles].name, stdout);
130 <                fputs(":\n", stdout);
131 <                getheader(input[nfiles].fp, tputs);
132 <                if (fgetresolu(&xpos, &ypos, input[nfiles].fp) !=
133 <                                (YMAJOR|YDECR)) {
134 <                        eputs(input[nfiles].name);
135 <                        eputs(": bad picture size\n");
136 <                        quit(1);
137 <                }
138 <                if (xres == 0 && yres == 0) {
139 <                        xres = xpos;
140 <                        yres = ypos;
141 <                } else if (xpos != xres || ypos != yres) {
142 <                        eputs(argv[0]);
143 <                        eputs(": resolution mismatch\n");
144 <                        quit(1);
145 <                }
146 <                input[nfiles].scan = (COLOR *)emalloc(xres*sizeof(COLOR));
125 >                checkfile();
126                  nfiles++;
127          }
128 +        init();                         /* set constant expressions */
129 +                                        /* go back and get expressions */
130 +        for (a = 1; a < argc; a++) {
131 +                if (argv[a][0] == '-')
132 +                        switch (argv[a][1]) {
133 +                        case 'x':
134 +                        case 'y':
135 +                                a++;
136 +                                continue;
137 +                        case 'w':
138 +                        case 'o':
139 +                                continue;
140 +                        case 'f':
141 +                                fcompile(argv[++a]);
142 +                                continue;
143 +                        case 'e':
144 +                                scompile(argv[++a], NULL, 0);
145 +                                continue;
146 +                        }
147 +                break;
148 +        }
149 +                                                /* complete header */
150          printargs(argc, argv, stdout);
151 +        fputformat(COLRFMT, stdout);
152          putchar('\n');
153          fputresolu(YMAJOR|YDECR, xres, yres, stdout);
154 +                                                /* combine pictures */
155          combine();
156          quit(0);
157   usage:
158          eputs("Usage: ");
159          eputs(argv[0]);
160 < eputs(" [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] picture ..]\n");
160 >        eputs(
161 > " [-w][-h][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] pic ..]\n");
162          quit(1);
163   }
164  
165  
166 + tputs(s)                        /* put out string preceded by a tab */
167 + char    *s;
168 + {
169 +        char    fmt[32];
170 +        double  d;
171 +        COLOR   ctmp;
172 +
173 +        if (isformat(s)) {                      /* check format */
174 +                formatval(fmt, s);
175 +                wrongformat = strcmp(fmt, COLRFMT);
176 +        } else if (original && isexpos(s)) {    /* exposure */
177 +                d = 1.0/exposval(s);
178 +                scalecolor(input[nfiles].coef, d);
179 +        } else if (original && iscolcor(s)) {   /* color correction */
180 +                colcorval(ctmp, s);
181 +                colval(input[nfiles].coef,RED) /= colval(ctmp,RED);
182 +                colval(input[nfiles].coef,GRN) /= colval(ctmp,GRN);
183 +                colval(input[nfiles].coef,BLU) /= colval(ctmp,BLU);
184 +        } else {                                /* echo unaffected line */
185 +                putchar('\t');
186 +                fputs(s, stdout);
187 +        }
188 + }
189 +
190 +
191 + checkfile()                     /* ready a file */
192 + {
193 +        register int    i;
194 +                                        /* process header */
195 +        fputs(input[nfiles].name, stdout);
196 +        fputs(":\n", stdout);
197 +        getheader(input[nfiles].fp, tputs, NULL);
198 +        if (wrongformat) {
199 +                eputs(input[nfiles].name);
200 +                eputs(": not in Radiance picture format\n");
201 +                quit(1);
202 +        }
203 +        if (fgetresolu(&xpos, &ypos, input[nfiles].fp) != (YMAJOR|YDECR)) {
204 +                eputs(input[nfiles].name);
205 +                eputs(": bad picture size\n");
206 +                quit(1);
207 +        }
208 +        if (xres == 0 && yres == 0) {
209 +                xres = xpos;
210 +                yres = ypos;
211 +        } else if (xpos != xres || ypos != yres) {
212 +                eputs(input[nfiles].name);
213 +                eputs(": resolution mismatch\n");
214 +                quit(1);
215 +        }
216 +                                        /* allocate scanlines */
217 +        for (i = 0; i < WINSIZ; i++)
218 +                input[nfiles].scan[i] = (COLOR *)emalloc(xres*sizeof(COLOR));
219 + }
220 +
221 +
222 + init()                          /* perform final setup */
223 + {
224 +        double  l_colin(), l_coef();
225 +        register int    i;
226 +                                                /* prime input */
227 +        for (ypos = yres+(MIDSCN-1); ypos >= yres; ypos--)
228 +                advance();
229 +                                                /* define constants */
230 +        varset(vnfiles, ':', (double)nfiles);
231 +        varset(vxres, ':', (double)xres);
232 +        varset(vyres, ':', (double)yres);
233 +                                                /* set functions */
234 +        for (i = 0; i < 3; i++) {
235 +                funset(vcolcoef[i], 1, ':', l_coef);
236 +                funset(vcolin[i], 1, '=', l_colin);
237 +        }
238 +        funset(vbrtcoef, 1, ':', l_coef);
239 +        funset(vbrtin, 1, '=', l_colin);
240 + }
241 +
242 +
243   combine()                       /* combine pictures */
244   {
245 <        EPNODE  *coldef[3];
245 >        EPNODE  *coldef[3], *brtdef;
246          COLOR   *scanout;
247 +        double  d;
248          register int    i, j;
249                                                  /* check defined variables */
250          for (j = 0; j < 3; j++) {
# Line 171 | Line 253 | combine()                      /* combine pictures */
253                  else
254                          coldef[j] = NULL;
255          }
256 +        if (vardefined(vbrtout))
257 +                brtdef = eparse(vbrtout);
258 +        else
259 +                brtdef = NULL;
260                                                  /* allocate scanline */
261          scanout = (COLOR *)emalloc(xres*sizeof(COLOR));
262                                                  /* combine files */
263          for (ypos = yres-1; ypos >= 0; ypos--) {
264 <                for (i = 0; i < nfiles; i++)
265 <                        if (freadscan(input[i].scan, xres, input[i].fp) < 0) {
266 <                                eputs(input[i].name);
267 <                                eputs(": read error\n");
268 <                                quit(1);
264 >            advance();
265 >            varset(vypos, '=', (double)ypos);
266 >            for (xpos = 0; xpos < xres; xpos++) {
267 >                varset(vxpos, '=', (double)xpos);
268 >                eclock++;
269 >                if (brtdef != NULL) {
270 >                    d = evalue(brtdef);
271 >                    if (d < 0.0)
272 >                        d = 0.0;
273 >                    setcolor(scanout[xpos], d, d, d);
274 >                } else {
275 >                    for (j = 0; j < 3; j++) {
276 >                        if (coldef[j] != NULL) {
277 >                                d = evalue(coldef[j]);
278 >                        } else {
279 >                            d = 0.0;
280 >                            for (i = 0; i < nfiles; i++)
281 >                                d += colval(input[i].scan[MIDSCN][xpos],j);
282                          }
283 <                varset(vypos, (double)ypos);
284 <                for (xpos = 0; xpos < xres; xpos++) {
285 <                        varset(vxpos, (double)xpos);
286 <                        eclock++;
188 <                        for (j = 0; j < 3; j++) {
189 <                                if (coldef[j] != NULL) {
190 <                                        colval(scanout[xpos],j) =
191 <                                                evalue(coldef[j]);
192 <                                } else {
193 <                                        colval(scanout[xpos],j) = 0.0;
194 <                                        for (i = 0; i < nfiles; i++)
195 <                                                colval(scanout[xpos],j) +=
196 <                                                colval(input[i].coef,j) *
197 <                                                colval(input[i].scan[xpos],j);
198 <                                }
199 <                                if (colval(scanout[xpos],j) < 0.0)
200 <                                        colval(scanout[xpos],j) = 0.0;
201 <                        }
283 >                        if (d < 0.0)
284 >                            d = 0.0;
285 >                        colval(scanout[xpos],j) = d;
286 >                    }
287                  }
288 <                if (fwritescan(scanout, xres, stdout) < 0) {
289 <                        eputs("write error\n");
290 <                        quit(1);
291 <                }
288 >            }
289 >            if (fwritescan(scanout, xres, stdout) < 0) {
290 >                    perror("write error");
291 >                    quit(1);
292 >            }
293          }
294          efree(scanout);
295   }
296  
297  
298 < double
213 < colin(fn, ci)                   /* return color value for picture */
214 < register int    fn, ci;
298 > advance()                       /* read in next scanline */
299   {
300 <        if (fn == 0)
301 <                return((double)nfiles);
302 <        if (fn < 1 || fn > nfiles) {
303 <                errno = EDOM;
304 <                return(0.0);
300 >        register COLOR  *st;
301 >        register int    i, j;
302 >
303 >        for (i = 0; i < nfiles; i++) {
304 >                st = input[i].scan[WINSIZ-1];
305 >                for (j = WINSIZ-1; j > 0; j--)  /* rotate window */
306 >                        input[i].scan[j] = input[i].scan[j-1];
307 >                input[i].scan[0] = st;
308 >                if (ypos < MIDSCN)              /* hit bottom */
309 >                        continue;
310 >                if (freadscan(st, xres, input[i].fp) < 0) {
311 >                        eputs(input[i].name);
312 >                        eputs(": read error\n");
313 >                        quit(1);
314 >                }
315 >                for (j = 0; j < xres; j++)      /* adjust color */
316 >                        multcolor(st[j], input[i].coef);
317          }
222        return(colval(input[fn-1].scan[xpos],ci));
318   }
319  
320  
321   double
322 < l_redin()                       /* get red color */
322 > l_coef(nam)                     /* return picture coefficients */
323 > register char   *nam;
324   {
325 <        return(colin((int)(argument(1)+.5), RED));
326 < }
325 >        register int    fn, n;
326 >        double  d;
327  
328 <
329 < double
330 < l_grnin()                       /* get green color */
331 < {
332 <        return(colin((int)(argument(1)+.5), GRN));
328 >        d = argument(1);
329 >        if (d > -.5 && d < .5)
330 >                return((double)nfiles);
331 >        fn = d - .5;
332 >        if (fn < 0 || fn >= nfiles) {
333 >                errno = EDOM;
334 >                return(0.0);
335 >        }
336 >        if (nam == vbrtcoef)
337 >                return(bright(input[fn].coef));
338 >        n = 3;
339 >        while (n--)
340 >                if (nam == vcolcoef[n])
341 >                        return(colval(input[fn].coef,n));
342 >        eputs("Bad call to l_coef()!\n");
343 >        quit(1);
344   }
345  
346  
347   double
348 < l_bluin()                       /* get blue color */
348 > l_colin(nam)                    /* return color value for picture */
349 > register char   *nam;
350   {
351 <        return(colin((int)(argument(1)+.5), BLU));
351 >        int     fn;
352 >        register int    n, xoff, yoff;
353 >        double  d;
354 >
355 >        d = argument(1);
356 >        if (d > -.5 && d < .5)
357 >                return((double)nfiles);
358 >        fn = d - .5;
359 >        if (fn < 0 || fn >= nfiles) {
360 >                errno = EDOM;
361 >                return(0.0);
362 >        }
363 >        xoff = yoff = 0;
364 >        n = nargum();
365 >        if (n >= 2) {
366 >                d = argument(2);
367 >                if (d < 0.0) {
368 >                        xoff = d-.5;
369 >                        if (xpos+xoff < 0)
370 >                                xoff = -xpos;
371 >                } else {
372 >                        xoff = d+.5;
373 >                        if (xpos+xoff >= xres)
374 >                                xoff = xres-1-xpos;
375 >                }
376 >        }
377 >        if (n >= 3) {
378 >                d = argument(3);
379 >                if (d < 0.0) {
380 >                        yoff = d-.5;
381 >                        if (yoff+MIDSCN < 0)
382 >                                yoff = -MIDSCN;
383 >                        if (ypos+yoff < 0)
384 >                                yoff = -ypos;
385 >                } else {
386 >                        yoff = d+.5;
387 >                        if (yoff+MIDSCN >= WINSIZ)
388 >                                yoff = WINSIZ-1-MIDSCN;
389 >                        if (ypos+yoff >= yres)
390 >                                yoff = yres-1-ypos;
391 >                }
392 >        }
393 >        if (nam == vbrtin)
394 >                return(bright(input[fn].scan[MIDSCN+yoff][xpos+xoff]));
395 >        n = 3;
396 >        while (n--)
397 >            if (nam == vcolin[n])
398 >                return(colval(input[fn].scan[MIDSCN+yoff][xpos+xoff],n));
399 >        eputs("Bad call to l_colin()!\n");
400 >        quit(1);
401   }
402  
403  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines