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