1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: psum.c,v 2.7 2004/01/02 12:47:01 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* psum.c - program to sum pictures. |
6 |
* |
7 |
* 10/14/86 |
8 |
*/ |
9 |
|
10 |
#include <stdio.h> |
11 |
#include <math.h> |
12 |
|
13 |
#include "color.h" |
14 |
#include "resolu.h" |
15 |
#include "rterror.h" |
16 |
|
17 |
|
18 |
#define MAXFILE 8 |
19 |
|
20 |
int xsiz, ysiz; |
21 |
|
22 |
char *progname; |
23 |
|
24 |
char *fname[MAXFILE]; /* the file names */ |
25 |
FILE *fptr[MAXFILE]; /* the file pointers */ |
26 |
COLOR scale[MAXFILE]; /* scaling factors */ |
27 |
int nfile; /* number of files */ |
28 |
|
29 |
static gethfunc tabputs; |
30 |
static void psum(void); |
31 |
|
32 |
|
33 |
|
34 |
static int |
35 |
tabputs( /* print line preceded by a tab */ |
36 |
char *s, |
37 |
void *p |
38 |
) |
39 |
{ |
40 |
putc('\t', stdout); |
41 |
return(fputs(s, stdout)); |
42 |
} |
43 |
|
44 |
|
45 |
int |
46 |
main( |
47 |
int argc, |
48 |
char *argv[] |
49 |
) |
50 |
{ |
51 |
double d; |
52 |
int xres, yres; |
53 |
int an; |
54 |
|
55 |
progname = argv[0]; |
56 |
|
57 |
nfile = 0; |
58 |
setcolor(scale[0], 1.0, 1.0, 1.0); |
59 |
|
60 |
for (an = 1; an < argc; an++) { |
61 |
if (nfile >= MAXFILE) { |
62 |
fprintf(stderr, "%s: too many files\n", progname); |
63 |
quit(1); |
64 |
} |
65 |
if (argv[an][0] == '-') |
66 |
switch (argv[an][1]) { |
67 |
case 's': |
68 |
d = atof(argv[an+1]); |
69 |
switch (argv[an][2]) { |
70 |
case '\0': |
71 |
scalecolor(scale[nfile], d); |
72 |
break; |
73 |
case 'r': |
74 |
colval(scale[nfile],RED) *= d; |
75 |
break; |
76 |
case 'g': |
77 |
colval(scale[nfile],GRN) *= d; |
78 |
break; |
79 |
case 'b': |
80 |
colval(scale[nfile],BLU) *= d; |
81 |
break; |
82 |
default: |
83 |
goto unkopt; |
84 |
} |
85 |
an++; |
86 |
continue; |
87 |
case '\0': |
88 |
fptr[nfile] = stdin; |
89 |
fname[nfile] = "<stdin>"; |
90 |
break; |
91 |
default:; |
92 |
unkopt: |
93 |
fprintf(stderr, "%s: unknown option: %s\n", |
94 |
progname, argv[an]); |
95 |
quit(1); |
96 |
} |
97 |
else if ((fptr[nfile] = fopen(argv[an], "r")) == NULL) { |
98 |
fprintf(stderr, "%s: can't open file: %s\n", |
99 |
progname, argv[an]); |
100 |
quit(1); |
101 |
} else |
102 |
fname[nfile] = argv[an]; |
103 |
/* get header */ |
104 |
fputs(fname[nfile], stdout); |
105 |
fputs(":\n", stdout); |
106 |
getheader(fptr[nfile], tabputs, NULL); |
107 |
/* get picture size */ |
108 |
if (fgetresolu(&xres, &yres, fptr[nfile]) != (YMAJOR|YDECR)) { |
109 |
fprintf(stderr, "%s: bad picture size\n", progname); |
110 |
quit(1); |
111 |
} else if (nfile == 0) { |
112 |
xsiz = xres; |
113 |
ysiz = yres; |
114 |
} else if (xres != xsiz || yres != ysiz) { |
115 |
fprintf(stderr, "%s: pictures are different sizes\n", |
116 |
progname); |
117 |
quit(1); |
118 |
} |
119 |
nfile++; |
120 |
setcolor(scale[nfile], 1.0, 1.0, 1.0); |
121 |
} |
122 |
/* add new header info. */ |
123 |
printargs(argc, argv, stdout); |
124 |
putchar('\n'); |
125 |
fputresolu(YMAJOR|YDECR, xsiz, ysiz, stdout); |
126 |
|
127 |
psum(); |
128 |
|
129 |
quit(0); |
130 |
return 0; /* pro forma return */ |
131 |
} |
132 |
|
133 |
|
134 |
static void |
135 |
psum(void) /* sum the files */ |
136 |
{ |
137 |
COLOR *scanin, *scanout; |
138 |
int y, i; |
139 |
register int x; |
140 |
|
141 |
scanin = (COLOR *)malloc(xsiz*sizeof(COLOR)); |
142 |
scanout = (COLOR *)malloc(xsiz*sizeof(COLOR)); |
143 |
if (scanin == NULL || scanout == NULL) { |
144 |
fprintf(stderr, "%s: out of memory\n", progname); |
145 |
quit(1); |
146 |
} |
147 |
for (y = ysiz-1; y >= 0; y--) { |
148 |
for (x = 0; x < xsiz; x++) |
149 |
setcolor(scanout[x], 0.0, 0.0, 0.0); |
150 |
for (i = 0; i < nfile; i++) { |
151 |
if (freadscan(scanin, xsiz, fptr[i]) < 0) { |
152 |
fprintf(stderr, "%s: read error on file: %s\n", |
153 |
progname, fname[i]); |
154 |
quit(1); |
155 |
} |
156 |
for (x = 0; x < xsiz; x++) |
157 |
multcolor(scanin[x], scale[i]); |
158 |
for (x = 0; x < xsiz; x++) |
159 |
addcolor(scanout[x], scanin[x]); |
160 |
} |
161 |
if (fwritescan(scanout, xsiz, stdout) < 0) { |
162 |
fprintf(stderr, "%s: write error\n", progname); |
163 |
quit(1); |
164 |
} |
165 |
} |
166 |
free((void *)scanin); |
167 |
free((void *)scanout); |
168 |
} |
169 |
|
170 |
|
171 |
void |
172 |
quit( /* exit gracefully */ |
173 |
int code |
174 |
) |
175 |
{ |
176 |
exit(code); |
177 |
} |