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