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