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 1.19 by greg, Thu May 30 09:47:14 1991 UTC

# Line 19 | Line 19 | static char SCCSid[] = "$SunId$ LBL";
19   #include "calcomp.h"
20  
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 */
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"
39 > char    vbrtin[] = "li";
40 > char    vbrtout[] = "lo";
41 > char    *vcolexp[3] = {"re", "ge", "be"};
42 > char    vbrtexp[] = "le";
43  
44 < #define vnfiles         "nfiles"
45 < #define vxres           "xres"
46 < #define vyres           "yres"
47 < #define vxpos           "x"
48 < #define vypos           "y"
44 > char    vnfiles[] = "nfiles";
45 > char    vxmax[] = "xmax";
46 > char    vymax[] = "ymax";
47 > char    vxres[] = "xres";
48 > char    vyres[] = "yres";
49 > char    vxpos[] = "x";
50 > char    vypos[] = "y";
51  
52   int     nowarn = 0;                     /* no warning messages? */
53  
54 < int     original = 0;                   /* origninal values? */
54 > int     xmax = 0, ymax = 0;             /* input resolution */
55  
56 < int     xres=0, yres=0;                 /* picture resolution */
56 > int     xscan, yscan;                   /* input position */
57  
58 < int     xpos, ypos;                     /* picture position */
58 > int     xres, yres;                     /* output resolution */
59  
60 + int     xpos, ypos;                     /* output position */
61 +
62   int     wrongformat = 0;
63  
64 + FILE    *popen();
65  
54 tputs(s)                        /* put out string preceded by a tab */
55 char    *s;
56 {
57        char    fmt[32];
58        double  d;
59        COLOR   ctmp;
66  
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
67   main(argc, argv)
68   int     argc;
69   char    *argv[];
70   {
71 <        extern double   l_redin(), l_grnin(), l_bluin(), l_brtin(), atof();
71 >        char    buf[128];
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 +                                break;
115                          case 's':
116                                  f = atof(argv[++a]);
117                                  scalecolor(input[nfiles].coef, f);
# Line 150 | Line 126 | getfiles:
126                          }
127                  else {
128                          input[nfiles].name = argv[a];
129 <                        input[nfiles].fp = fopen(argv[a], "r");
129 >                        input[nfiles].fp = argv[a][0]=='!' ?
130 >                                        popen(argv[a]+1, "r") :
131 >                                        fopen(argv[a], "r");
132                          if (input[nfiles].fp == NULL) {
133                                  perror(argv[a]);
134                                  quit(1);
135                          }
136                  }
137 <                fputs(input[nfiles].name, stdout);
138 <                fputs(":\n", stdout);
139 <                getheader(input[nfiles].fp, tputs, NULL);
140 <                if (wrongformat) {
141 <                        eputs(input[nfiles].name);
142 <                        eputs(": not in Radiance picture format\n");
143 <                        quit(1);
137 >                checkfile();
138 >                if (original) {
139 >                        colval(input[nfiles].coef,RED) /=
140 >                                        colval(input[nfiles].expos,RED);
141 >                        colval(input[nfiles].coef,GRN) /=
142 >                                        colval(input[nfiles].expos,GRN);
143 >                        colval(input[nfiles].coef,BLU) /=
144 >                                        colval(input[nfiles].expos,BLU);
145                  }
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));
146                  nfiles++;
147 +                original = 0;
148          }
149 +        init();                         /* set constants */
150 +                                        /* go back and get expressions */
151 +        for (a = 1; a < argc; a++) {
152 +                if (argv[a][0] == '-')
153 +                        switch (argv[a][1]) {
154 +                        case 'x':
155 +                                strcpy(buf, vxres);
156 +                                strcat(buf, ":");
157 +                                strcat(buf, argv[++a]);
158 +                                scompile(buf, NULL, 0);
159 +                                continue;
160 +                        case 'y':
161 +                                strcpy(buf, vyres);
162 +                                strcat(buf, ":");
163 +                                strcat(buf, argv[++a]);
164 +                                scompile(buf, NULL, 0);
165 +                                continue;
166 +                        case 'w':
167 +                                continue;
168 +                        case 'f':
169 +                                fcompile(argv[++a]);
170 +                                continue;
171 +                        case 'e':
172 +                                scompile(argv[++a], NULL, 0);
173 +                                continue;
174 +                        }
175 +                break;
176 +        }
177 +                                                /* set/get output resolution */
178 +        if (!vardefined(vxres))
179 +                varset(vxres, ':', (double)xmax);
180 +        if (!vardefined(vyres))
181 +                varset(vyres, ':', (double)ymax);
182 +        xres = varvalue(vxres) + .5;
183 +        yres = varvalue(vyres) + .5;
184 +        if (xres <= 0 || yres <= 0) {
185 +                eputs(argv[0]);
186 +                eputs(": illegal output resolution\n");
187 +                quit(1);
188 +        }
189 +                                                /* complete header */
190          printargs(argc, argv, stdout);
191          fputformat(COLRFMT, stdout);
192          putchar('\n');
193          fputresolu(YMAJOR|YDECR, xres, yres, stdout);
194 +                                                /* combine pictures */
195          combine();
196          quit(0);
197   usage:
# Line 196 | Line 203 | usage:
203   }
204  
205  
206 + tputs(s)                        /* put out string preceded by a tab */
207 + char    *s;
208 + {
209 +        char    fmt[32];
210 +        double  d;
211 +        COLOR   ctmp;
212 +
213 +        if (isformat(s)) {                      /* check format */
214 +                formatval(fmt, s);
215 +                wrongformat = strcmp(fmt, COLRFMT);
216 +                return;         /* don't echo */
217 +        }
218 +        if (isexpos(s)) {                       /* exposure */
219 +                d = exposval(s);
220 +                scalecolor(input[nfiles].expos, d);
221 +        } else if (iscolcor(s)) {               /* color correction */
222 +                colcorval(ctmp, s);
223 +                multcolor(input[nfiles].expos, ctmp);
224 +        }
225 +                                                /* echo line */
226 +        putchar('\t');
227 +        fputs(s, stdout);
228 + }
229 +
230 +
231 + checkfile()                     /* ready a file */
232 + {
233 +        int     xinp, yinp;
234 +        register int    i;
235 +                                        /* process header */
236 +        fputs(input[nfiles].name, stdout);
237 +        fputs(":\n", stdout);
238 +        getheader(input[nfiles].fp, tputs, NULL);
239 +        if (wrongformat) {
240 +                eputs(input[nfiles].name);
241 +                eputs(": not in Radiance picture format\n");
242 +                quit(1);
243 +        }
244 +        if (fgetresolu(&xinp, &yinp, input[nfiles].fp) != (YMAJOR|YDECR)) {
245 +                eputs(input[nfiles].name);
246 +                eputs(": bad picture size\n");
247 +                quit(1);
248 +        }
249 +        if (xmax == 0 && ymax == 0) {
250 +                xmax = xinp;
251 +                ymax = yinp;
252 +        } else if (xinp != xmax || yinp != ymax) {
253 +                eputs(input[nfiles].name);
254 +                eputs(": resolution mismatch\n");
255 +                quit(1);
256 +        }
257 +                                        /* allocate scanlines */
258 +        for (i = 0; i < WINSIZ; i++)
259 +                input[nfiles].scan[i] = (COLOR *)emalloc(xmax*sizeof(COLOR));
260 + }
261 +
262 +
263 + init()                                  /* perform final setup */
264 + {
265 +        double  l_colin(), l_expos();
266 +        register int    i;
267 +                                                /* define constants */
268 +        varset(vnfiles, ':', (double)nfiles);
269 +        varset(vxmax, ':', (double)xmax);
270 +        varset(vymax, ':', (double)ymax);
271 +                                                /* set functions */
272 +        for (i = 0; i < 3; i++) {
273 +                funset(vcolexp[i], 1, ':', l_expos);
274 +                funset(vcolin[i], 1, '=', l_colin);
275 +        }
276 +        funset(vbrtexp, 1, ':', l_expos);
277 +        funset(vbrtin, 1, '=', l_colin);
278 + }
279 +
280 +
281   combine()                       /* combine pictures */
282   {
283          EPNODE  *coldef[3], *brtdef;
# Line 213 | Line 295 | combine()                      /* combine pictures */
295                  brtdef = eparse(vbrtout);
296          else
297                  brtdef = NULL;
216                                                /* predefine variables */
217        varset(vnfiles, '=', (double)nfiles);
218        varset(vxres, '=', (double)xres);
219        varset(vyres, '=', (double)yres);
298                                                  /* allocate scanline */
299          scanout = (COLOR *)emalloc(xres*sizeof(COLOR));
300 +                                                /* set input position */
301 +        yscan = ymax+MIDSCN;
302                                                  /* combine files */
303          for (ypos = yres-1; ypos >= 0; ypos--) {
304 <            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 <                    }
304 >            advance();
305              varset(vypos, '=', (double)ypos);
306              for (xpos = 0; xpos < xres; xpos++) {
307 <                for (i = 0; i < nfiles; i++)
233 <                        multcolor(input[i].scan[xpos],input[i].coef);
307 >                xscan = (long)xpos*xmax/xres;
308                  varset(vxpos, '=', (double)xpos);
309                  eclock++;
310                  if (brtdef != NULL) {
# Line 241 | Line 315 | combine()                      /* combine pictures */
315                  } else {
316                      for (j = 0; j < 3; j++) {
317                          if (coldef[j] != NULL) {
318 <                                colval(scanout[xpos],j) = evalue(coldef[j]);
318 >                                d = evalue(coldef[j]);
319                          } else {
320 <                            colval(scanout[xpos],j) = 0.0;
320 >                            d = 0.0;
321                              for (i = 0; i < nfiles; i++)
322 <                                colval(scanout[xpos],j) +=
249 <                                        colval(input[i].scan[xpos],j);
322 >                                d += colval(input[i].scan[MIDSCN][xscan],j);
323                          }
324 <                        if (colval(scanout[xpos],j) < 0.0)
325 <                            colval(scanout[xpos],j) = 0.0;
324 >                        if (d < 0.0)
325 >                            d = 0.0;
326 >                        colval(scanout[xpos],j) = d;
327                      }
328                  }
329              }
# Line 262 | Line 336 | combine()                      /* combine pictures */
336   }
337  
338  
339 < double
266 < colin(fn, ci)                   /* return color value for picture */
267 < register int    fn, ci;
339 > advance()                       /* read in data for next scanline */
340   {
341 <        if (fn == 0)
342 <                return((double)nfiles);
343 <        if (fn < 1 || fn > nfiles) {
272 <                errno = EDOM;
273 <                return(0.0);
274 <        }
275 <        return(colval(input[fn-1].scan[xpos],ci));
276 < }
341 >        int     ytarget;
342 >        register COLOR  *st;
343 >        register int    i, j;
344  
345 <
346 < double
347 < l_redin()                       /* get red color */
348 < {
349 <        return(colin((int)(argument(1)+.5), RED));
345 >        for (ytarget = (long)ypos*ymax/yres; yscan > ytarget; yscan--)
346 >                for (i = 0; i < nfiles; i++) {
347 >                        st = input[i].scan[WINSIZ-1];
348 >                        for (j = WINSIZ-1; j > 0; j--)  /* rotate window */
349 >                                input[i].scan[j] = input[i].scan[j-1];
350 >                        input[i].scan[0] = st;
351 >                        if (yscan <= MIDSCN)            /* hit bottom? */
352 >                                continue;
353 >                        if (freadscan(st, xmax, input[i].fp) < 0) {     /* read */
354 >                                eputs(input[i].name);
355 >                                eputs(": read error\n");
356 >                                quit(1);
357 >                        }
358 >                        for (j = 0; j < xmax; j++)      /* adjust color */
359 >                                multcolor(st[j], input[i].coef);
360 >                }
361   }
362  
363  
364   double
365 < l_grnin()                       /* get green color */
365 > l_expos(nam)                    /* return picture exposure */
366 > register char   *nam;
367   {
368 <        return(colin((int)(argument(1)+.5), GRN));
290 < }
368 >        register int    fn, n;
369  
370 <
371 < double
372 < l_bluin()                       /* get blue color */
373 < {
374 <        return(colin((int)(argument(1)+.5), BLU));
370 >        fn = argument(1) - .5;
371 >        if (fn < 0 || fn >= nfiles)
372 >                return(1.0);
373 >        if (nam == vbrtexp)
374 >                return(bright(input[fn].expos));
375 >        n = 3;
376 >        while (n--)
377 >                if (nam == vcolexp[n])
378 >                        return(colval(input[fn].expos,n));
379 >        eputs("Bad call to l_expos()!\n");
380 >        quit(1);
381   }
382  
383  
384   double
385 < l_brtin()                       /* get brightness value */
385 > l_colin(nam)                    /* return color value for picture */
386 > register char   *nam;
387   {
388 <        register int    fn;
388 >        int     fn;
389 >        register int    n, xoff, yoff;
390 >        double  d;
391  
392 <        fn = argument(1)+.5;
393 <        if (fn == 0)
392 >        d = argument(1);
393 >        if (d > -.5 && d < .5)
394                  return((double)nfiles);
395 <        if (fn < 1 || fn > nfiles) {
395 >        fn = d - .5;
396 >        if (fn < 0 || fn >= nfiles) {
397                  errno = EDOM;
398                  return(0.0);
399          }
400 <        return(bright(input[fn-1].scan[xpos]));
400 >        xoff = yoff = 0;
401 >        n = nargum();
402 >        if (n >= 2) {
403 >                d = argument(2);
404 >                if (d < 0.0) {
405 >                        xoff = d-.5;
406 >                        if (xscan+xoff < 0)
407 >                                xoff = -xscan;
408 >                } else {
409 >                        xoff = d+.5;
410 >                        if (xscan+xoff >= xmax)
411 >                                xoff = xmax-1-xscan;
412 >                }
413 >        }
414 >        if (n >= 3) {
415 >                d = argument(3);
416 >                if (d < 0.0) {
417 >                        yoff = d-.5;
418 >                        if (yoff+MIDSCN < 0)
419 >                                yoff = -MIDSCN;
420 >                        if (yscan+yoff < 0)
421 >                                yoff = -yscan;
422 >                } else {
423 >                        yoff = d+.5;
424 >                        if (yoff+MIDSCN >= WINSIZ)
425 >                                yoff = WINSIZ-1-MIDSCN;
426 >                        if (yscan+yoff >= ymax)
427 >                                yoff = ymax-1-yscan;
428 >                }
429 >        }
430 >        if (nam == vbrtin)
431 >                return(bright(input[fn].scan[MIDSCN+yoff][xscan+xoff]));
432 >        n = 3;
433 >        while (n--)
434 >            if (nam == vcolin[n])
435 >                return(colval(input[fn].scan[MIDSCN+yoff][xscan+xoff],n));
436 >        eputs("Bad call to l_colin()!\n");
437 >        quit(1);
438   }
439  
440  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines