1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
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 |
#define MAXINP 32 /* maximum number of input files */ |
22 |
|
23 |
struct { |
24 |
char *name; /* file name */ |
25 |
FILE *fp; /* stream pointer */ |
26 |
COLOR *scan; /* input scanline */ |
27 |
COLOR coef; /* coefficient */ |
28 |
} input[MAXINP]; /* input pictures */ |
29 |
|
30 |
int nfiles; /* number of input files */ |
31 |
|
32 |
char *vcolin[3] = {"ri", "gi", "bi"}; |
33 |
char *vcolout[3] = {"ro", "go", "bo"}; |
34 |
#define vbrtin "li" |
35 |
#define vbrtout "lo" |
36 |
|
37 |
#define vnfiles "nfiles" |
38 |
#define vxres "xres" |
39 |
#define vyres "yres" |
40 |
#define vxpos "x" |
41 |
#define vypos "y" |
42 |
|
43 |
int nowarn = 0; /* no warning messages? */ |
44 |
|
45 |
int xres=0, yres=0; /* picture resolution */ |
46 |
|
47 |
int xpos, ypos; /* picture position */ |
48 |
|
49 |
|
50 |
tputs(s) /* put out string preceded by a tab */ |
51 |
char *s; |
52 |
{ |
53 |
putchar('\t'); |
54 |
fputs(s, stdout); |
55 |
} |
56 |
|
57 |
|
58 |
main(argc, argv) |
59 |
int argc; |
60 |
char *argv[]; |
61 |
{ |
62 |
extern double l_redin(), l_grnin(), l_bluin(), l_brtin(), atof(); |
63 |
double f; |
64 |
int a; |
65 |
|
66 |
funset(vcolin[RED], 1, l_redin); |
67 |
funset(vcolin[GRN], 1, l_grnin); |
68 |
funset(vcolin[BLU], 1, l_bluin); |
69 |
funset(vbrtin, 1, l_brtin); |
70 |
|
71 |
for (a = 1; a < argc; a++) |
72 |
if (argv[a][0] == '-') |
73 |
switch (argv[a][1]) { |
74 |
case '\0': |
75 |
case 's': |
76 |
case 'c': |
77 |
goto getfiles; |
78 |
case 'x': |
79 |
xres = atoi(argv[++a]); |
80 |
break; |
81 |
case 'y': |
82 |
yres = atoi(argv[++a]); |
83 |
break; |
84 |
case 'w': |
85 |
nowarn = !nowarn; |
86 |
break; |
87 |
case 'f': |
88 |
fcompile(argv[++a]); |
89 |
break; |
90 |
case 'e': |
91 |
scompile(argv[++a], NULL, 0); |
92 |
break; |
93 |
default: |
94 |
goto usage; |
95 |
} |
96 |
else |
97 |
break; |
98 |
getfiles: |
99 |
for (nfiles = 0; nfiles < MAXINP; nfiles++) |
100 |
setcolor(input[nfiles].coef, 1.0, 1.0, 1.0); |
101 |
nfiles = 0; |
102 |
for ( ; a < argc; a++) { |
103 |
if (nfiles >= MAXINP) { |
104 |
eputs(argv[0]); |
105 |
eputs(": too many picture files\n"); |
106 |
quit(1); |
107 |
} |
108 |
if (argv[a][0] == '-') |
109 |
switch (argv[a][1]) { |
110 |
case '\0': |
111 |
input[nfiles].name = "<stdin>"; |
112 |
input[nfiles].fp = stdin; |
113 |
break; |
114 |
case 's': |
115 |
f = atof(argv[++a]); |
116 |
scalecolor(input[nfiles].coef, f); |
117 |
continue; |
118 |
case 'c': |
119 |
colval(input[nfiles].coef,RED)*=atof(argv[++a]); |
120 |
colval(input[nfiles].coef,GRN)*=atof(argv[++a]); |
121 |
colval(input[nfiles].coef,BLU)*=atof(argv[++a]); |
122 |
continue; |
123 |
default: |
124 |
goto usage; |
125 |
} |
126 |
else { |
127 |
input[nfiles].name = argv[a]; |
128 |
input[nfiles].fp = fopen(argv[a], "r"); |
129 |
if (input[nfiles].fp == NULL) { |
130 |
perror(argv[a]); |
131 |
quit(1); |
132 |
} |
133 |
} |
134 |
fputs(input[nfiles].name, stdout); |
135 |
fputs(":\n", stdout); |
136 |
getheader(input[nfiles].fp, tputs); |
137 |
if (fgetresolu(&xpos, &ypos, input[nfiles].fp) != |
138 |
(YMAJOR|YDECR)) { |
139 |
eputs(input[nfiles].name); |
140 |
eputs(": bad picture size\n"); |
141 |
quit(1); |
142 |
} |
143 |
if (xres == 0 && yres == 0) { |
144 |
xres = xpos; |
145 |
yres = ypos; |
146 |
} else if (xpos != xres || ypos != yres) { |
147 |
eputs(argv[0]); |
148 |
eputs(": resolution mismatch\n"); |
149 |
quit(1); |
150 |
} |
151 |
input[nfiles].scan = (COLOR *)emalloc(xres*sizeof(COLOR)); |
152 |
nfiles++; |
153 |
} |
154 |
printargs(argc, argv, stdout); |
155 |
putchar('\n'); |
156 |
fputresolu(YMAJOR|YDECR, xres, yres, stdout); |
157 |
combine(); |
158 |
quit(0); |
159 |
usage: |
160 |
eputs("Usage: "); |
161 |
eputs(argv[0]); |
162 |
eputs(" [-w][-x xr][-y yr][-e expr][-f file] [ [-s f][-c r g b] picture ..]\n"); |
163 |
quit(1); |
164 |
} |
165 |
|
166 |
|
167 |
combine() /* combine pictures */ |
168 |
{ |
169 |
EPNODE *coldef[3], *brtdef; |
170 |
COLOR *scanout; |
171 |
double d; |
172 |
register int i, j; |
173 |
/* check defined variables */ |
174 |
for (j = 0; j < 3; j++) { |
175 |
if (vardefined(vcolout[j])) |
176 |
coldef[j] = eparse(vcolout[j]); |
177 |
else |
178 |
coldef[j] = NULL; |
179 |
} |
180 |
if (vardefined(vbrtout)) |
181 |
brtdef = eparse(vbrtout); |
182 |
else |
183 |
brtdef = NULL; |
184 |
/* predefine variables */ |
185 |
varset(vnfiles, (double)nfiles); |
186 |
varset(vxres, (double)xres); |
187 |
varset(vyres, (double)yres); |
188 |
/* allocate scanline */ |
189 |
scanout = (COLOR *)emalloc(xres*sizeof(COLOR)); |
190 |
/* combine files */ |
191 |
for (ypos = yres-1; ypos >= 0; ypos--) { |
192 |
for (i = 0; i < nfiles; i++) |
193 |
if (freadscan(input[i].scan, xres, input[i].fp) < 0) { |
194 |
eputs(input[i].name); |
195 |
eputs(": read error\n"); |
196 |
quit(1); |
197 |
} |
198 |
varset(vypos, (double)ypos); |
199 |
for (xpos = 0; xpos < xres; xpos++) { |
200 |
varset(vxpos, (double)xpos); |
201 |
eclock++; |
202 |
if (brtdef != NULL) { |
203 |
d = evalue(brtdef); |
204 |
if (d < 0.0) |
205 |
d = 0.0; |
206 |
setcolor(scanout[xpos], d, d, d); |
207 |
} else { |
208 |
for (j = 0; j < 3; j++) { |
209 |
if (coldef[j] != NULL) { |
210 |
colval(scanout[xpos],j) = evalue(coldef[j]); |
211 |
} else { |
212 |
colval(scanout[xpos],j) = 0.0; |
213 |
for (i = 0; i < nfiles; i++) |
214 |
colval(scanout[xpos],j) += |
215 |
colval(input[i].coef,j) * |
216 |
colval(input[i].scan[xpos],j); |
217 |
} |
218 |
if (colval(scanout[xpos],j) < 0.0) |
219 |
colval(scanout[xpos],j) = 0.0; |
220 |
} |
221 |
} |
222 |
} |
223 |
if (fwritescan(scanout, xres, stdout) < 0) { |
224 |
perror("write error"); |
225 |
quit(1); |
226 |
} |
227 |
} |
228 |
efree(scanout); |
229 |
} |
230 |
|
231 |
|
232 |
double |
233 |
colin(fn, ci) /* return color value for picture */ |
234 |
register int fn, ci; |
235 |
{ |
236 |
if (fn == 0) |
237 |
return((double)nfiles); |
238 |
if (fn < 1 || fn > nfiles) { |
239 |
errno = EDOM; |
240 |
return(0.0); |
241 |
} |
242 |
return(colval(input[fn-1].scan[xpos],ci)); |
243 |
} |
244 |
|
245 |
|
246 |
double |
247 |
l_redin() /* get red color */ |
248 |
{ |
249 |
return(colin((int)(argument(1)+.5), RED)); |
250 |
} |
251 |
|
252 |
|
253 |
double |
254 |
l_grnin() /* get green color */ |
255 |
{ |
256 |
return(colin((int)(argument(1)+.5), GRN)); |
257 |
} |
258 |
|
259 |
|
260 |
double |
261 |
l_bluin() /* get blue color */ |
262 |
{ |
263 |
return(colin((int)(argument(1)+.5), BLU)); |
264 |
} |
265 |
|
266 |
|
267 |
double |
268 |
l_brtin() /* get brightness value */ |
269 |
{ |
270 |
register int fn; |
271 |
|
272 |
fn = argument(1)+.5; |
273 |
if (fn == 0) |
274 |
return((double)nfiles); |
275 |
if (fn < 1 || fn > nfiles) { |
276 |
errno = EDOM; |
277 |
return(0.0); |
278 |
} |
279 |
return(bright(input[fn-1].scan[xpos])); |
280 |
} |
281 |
|
282 |
|
283 |
wputs(msg) |
284 |
char *msg; |
285 |
{ |
286 |
if (!nowarn) |
287 |
eputs(msg); |
288 |
} |
289 |
|
290 |
|
291 |
eputs(msg) |
292 |
char *msg; |
293 |
{ |
294 |
fputs(msg, stderr); |
295 |
} |
296 |
|
297 |
|
298 |
quit(code) |
299 |
int code; |
300 |
{ |
301 |
exit(code); |
302 |
} |