ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcomb.c
Revision: 1.14
Committed: Thu May 23 13:46:05 1991 UTC (32 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.13: +94 -35 lines
Log Message:
added access to adjacent pixels for filtering, etc.

File Contents

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