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