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