| 9 |
|
*/ |
| 10 |
|
|
| 11 |
|
#include <stdio.h> |
| 12 |
< |
|
| 12 |
> |
#include <math.h> |
| 13 |
|
#include "color.h" |
| 14 |
|
#include "resolu.h" |
| 15 |
|
|
| 16 |
+ |
#ifdef MSDOS |
| 17 |
+ |
#include <fcntl.h> |
| 18 |
+ |
#endif |
| 19 |
+ |
|
| 20 |
+ |
extern char *malloc(), *realloc(), *tempbuffer(); |
| 21 |
+ |
extern int addhline(); |
| 22 |
+ |
|
| 23 |
+ |
#define dumpheader(fp) fwrite(headlines, 1, headlen, fp) |
| 24 |
+ |
|
| 25 |
|
int bradj = 0; /* brightness adjustment */ |
| 26 |
|
|
| 27 |
|
int doflat = 1; /* produce flat file */ |
| 28 |
|
|
| 29 |
+ |
int force = 0; /* force file overwrite? */ |
| 30 |
+ |
|
| 31 |
+ |
int findframe = 0; /* find a specific frame? */ |
| 32 |
+ |
|
| 33 |
+ |
int frameno = 0; /* current frame number */ |
| 34 |
+ |
int fmterr = 0; /* got input format error */ |
| 35 |
+ |
char *headlines; /* current header info. */ |
| 36 |
+ |
int headlen; /* current header length */ |
| 37 |
+ |
|
| 38 |
|
char *progname; |
| 39 |
|
|
| 40 |
|
|
| 42 |
|
int argc; |
| 43 |
|
char *argv[]; |
| 44 |
|
{ |
| 45 |
+ |
char *ospec; |
| 46 |
|
int i; |
| 47 |
< |
|
| 47 |
> |
|
| 48 |
|
progname = argv[0]; |
| 49 |
|
|
| 50 |
|
for (i = 1; i < argc; i++) |
| 58 |
|
goto userr; |
| 59 |
|
bradj = atoi(argv[++i]); |
| 60 |
|
break; |
| 61 |
+ |
case 'n': |
| 62 |
+ |
findframe = atoi(argv[++i]); |
| 63 |
+ |
break; |
| 64 |
+ |
case 'f': |
| 65 |
+ |
force++; |
| 66 |
+ |
break; |
| 67 |
+ |
case '\0': |
| 68 |
+ |
goto gotfile; |
| 69 |
|
default: |
| 70 |
|
goto userr; |
| 71 |
|
} |
| 72 |
|
else |
| 73 |
|
break; |
| 74 |
< |
|
| 74 |
> |
gotfile: |
| 75 |
|
if (i < argc-2) |
| 76 |
|
goto userr; |
| 77 |
< |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
| 77 |
> |
if (i <= argc-1 && strcmp(argv[i], "-") && |
| 78 |
> |
freopen(argv[i], "r", stdin) == NULL) { |
| 79 |
|
fprintf(stderr, "%s: can't open input \"%s\"\n", |
| 80 |
|
progname, argv[i]); |
| 81 |
|
exit(1); |
| 82 |
|
} |
| 83 |
< |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
| 84 |
< |
fprintf(stderr, "can't open output \"%s\"\n", |
| 85 |
< |
progname, argv[i+1]); |
| 86 |
< |
exit(1); |
| 87 |
< |
} |
| 88 |
< |
transfer(); |
| 83 |
> |
#ifdef MSDOS |
| 84 |
> |
setmode(fileno(stdin), O_BINARY); |
| 85 |
> |
#endif |
| 86 |
> |
ospec = i==argc-2 ? argv[i+1] : (char *)NULL; |
| 87 |
> |
while (transfer(ospec)) |
| 88 |
> |
; |
| 89 |
|
exit(0); |
| 90 |
|
userr: |
| 91 |
< |
fprintf(stderr, "Usage: %s [-r][-e +/-stops] [input [output]]\n", |
| 91 |
> |
fprintf(stderr, |
| 92 |
> |
"Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n", |
| 93 |
|
progname); |
| 94 |
|
exit(1); |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
|
| 98 |
< |
quiterr(err) /* print message and exit */ |
| 99 |
< |
char *err; |
| 98 |
> |
transfer(ospec) /* transfer a Radiance picture */ |
| 99 |
> |
char *ospec; |
| 100 |
|
{ |
| 101 |
< |
if (err != NULL) { |
| 102 |
< |
fprintf(stderr, "%s: %s\n", progname, err); |
| 74 |
< |
exit(1); |
| 75 |
< |
} |
| 76 |
< |
exit(0); |
| 77 |
< |
} |
| 78 |
< |
|
| 79 |
< |
|
| 80 |
< |
transfer() /* transfer Radiance picture */ |
| 81 |
< |
{ |
| 82 |
< |
extern double pow(); |
| 101 |
> |
char oname[128]; |
| 102 |
> |
FILE *fp; |
| 103 |
|
int order; |
| 104 |
< |
int xmax, ymax; |
| 104 |
> |
int xmax, ymax; |
| 105 |
|
COLR *scanin; |
| 86 |
– |
register int x; |
| 106 |
|
int y; |
| 107 |
< |
/* get header info. */ |
| 108 |
< |
if (checkheader(stdin, COLRFMT, stdout) < 0 || |
| 109 |
< |
(order = fgetresolu(&xmax, &ymax, stdin)) < 0) |
| 110 |
< |
quiterr("bad picture format"); |
| 111 |
< |
fputs(progname, stdout); |
| 112 |
< |
if (bradj) |
| 94 |
< |
printf(" -e %+d", bradj); |
| 95 |
< |
if (doflat) |
| 96 |
< |
fputs("\n", stdout); |
| 97 |
< |
else { |
| 98 |
< |
fputs(" -r\n", stdout); |
| 99 |
< |
fputformat(COLRFMT, stdout); |
| 107 |
> |
/* get header info. */ |
| 108 |
> |
if (!(y = loadheader(stdin))) |
| 109 |
> |
return(0); |
| 110 |
> |
if (y < 0 || (order = fgetresolu(&xmax, &ymax, stdin)) < 0) { |
| 111 |
> |
fprintf(stderr, "%s: bad input format\n", progname); |
| 112 |
> |
exit(1); |
| 113 |
|
} |
| 114 |
+ |
/* did we pass the target frame? */ |
| 115 |
+ |
if (findframe && findframe < frameno) |
| 116 |
+ |
return(0); |
| 117 |
+ |
/* allocate scanline */ |
| 118 |
+ |
scanin = (COLR *)tempbuffer(xmax*sizeof(COLR)); |
| 119 |
+ |
if (scanin == NULL) { |
| 120 |
+ |
perror(progname); |
| 121 |
+ |
exit(1); |
| 122 |
+ |
} |
| 123 |
+ |
/* skip frame? */ |
| 124 |
+ |
if (findframe > frameno) { |
| 125 |
+ |
for (y = ymax; y--; ) |
| 126 |
+ |
if (freadcolrs(scanin, xmax, stdin) < 0) { |
| 127 |
+ |
fprintf(stderr, |
| 128 |
+ |
"%s: error reading input picture\n", |
| 129 |
+ |
progname); |
| 130 |
+ |
exit(1); |
| 131 |
+ |
} |
| 132 |
+ |
return(1); |
| 133 |
+ |
} |
| 134 |
+ |
/* open output file/command */ |
| 135 |
+ |
if (ospec == NULL) { |
| 136 |
+ |
strcpy(oname, "<stdout>"); |
| 137 |
+ |
fp = stdout; |
| 138 |
+ |
} else { |
| 139 |
+ |
sprintf(oname, ospec, frameno); |
| 140 |
+ |
if (oname[0] == '!') { |
| 141 |
+ |
if ((fp = popen(oname+1, "w")) == NULL) { |
| 142 |
+ |
fprintf(stderr, "%s: cannot start \"%s\"\n", |
| 143 |
+ |
progname, oname); |
| 144 |
+ |
exit(1); |
| 145 |
+ |
} |
| 146 |
+ |
} else { |
| 147 |
+ |
if (!force && access(oname, 0) >= 0) { |
| 148 |
+ |
fprintf(stderr, |
| 149 |
+ |
"%s: output file \"%s\" exists\n", |
| 150 |
+ |
progname, oname); |
| 151 |
+ |
exit(1); |
| 152 |
+ |
} |
| 153 |
+ |
if ((fp = fopen(oname, "w")) == NULL) { |
| 154 |
+ |
fprintf(stderr, "%s: ", progname); |
| 155 |
+ |
perror(oname); |
| 156 |
+ |
exit(1); |
| 157 |
+ |
} |
| 158 |
+ |
} |
| 159 |
+ |
} |
| 160 |
+ |
#ifdef MSDOS |
| 161 |
+ |
setmode(fileno(fp), O_BINARY); |
| 162 |
+ |
#endif |
| 163 |
+ |
dumpheader(fp); /* put out header */ |
| 164 |
+ |
fputs(progname, fp); |
| 165 |
|
if (bradj) |
| 166 |
< |
fputexpos(pow(2.0, (double)bradj), stdout); |
| 167 |
< |
fputs("\n", stdout); |
| 168 |
< |
fputresolu(order, xmax, ymax, stdout); |
| 169 |
< |
/* allocate scanline */ |
| 170 |
< |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
| 171 |
< |
if (scanin == NULL) |
| 172 |
< |
quiterr("out of memory in transfer"); |
| 173 |
< |
/* convert image */ |
| 174 |
< |
for (y = ymax-1; y >= 0; y--) { |
| 175 |
< |
if (freadcolrs(scanin, xmax, stdin) < 0) |
| 176 |
< |
quiterr("error reading input picture"); |
| 166 |
> |
fprintf(fp, " -e %+d", bradj); |
| 167 |
> |
if (!doflat) |
| 168 |
> |
fputs(" -r", fp); |
| 169 |
> |
fputc('\n', fp); |
| 170 |
> |
if (bradj) |
| 171 |
> |
fputexpos(pow(2.0, (double)bradj), fp); |
| 172 |
> |
fputc('\n', fp); |
| 173 |
> |
fputresolu(order, xmax, ymax, fp); |
| 174 |
> |
/* transfer picture */ |
| 175 |
> |
for (y = ymax; y--; ) { |
| 176 |
> |
if (freadcolrs(scanin, xmax, stdin) < 0) { |
| 177 |
> |
fprintf(stderr, "%s: error reading input picture\n", |
| 178 |
> |
progname); |
| 179 |
> |
exit(1); |
| 180 |
> |
} |
| 181 |
|
if (bradj) |
| 182 |
|
shiftcolrs(scanin, xmax, bradj); |
| 183 |
|
if (doflat) |
| 184 |
< |
fwrite((char *)scanin, sizeof(COLR), xmax, stdout); |
| 184 |
> |
fwrite((char *)scanin, sizeof(COLR), xmax, fp); |
| 185 |
|
else |
| 186 |
< |
fwritecolrs(scanin, xmax, stdout); |
| 187 |
< |
if (ferror(stdout)) |
| 188 |
< |
quiterr("error writing rasterfile"); |
| 186 |
> |
fwritecolrs(scanin, xmax, fp); |
| 187 |
> |
if (ferror(fp)) { |
| 188 |
> |
fprintf(stderr, "%s: error writing output to \"%s\"\n", |
| 189 |
> |
progname, oname); |
| 190 |
> |
exit(1); |
| 191 |
> |
} |
| 192 |
|
} |
| 193 |
< |
/* free scanline */ |
| 194 |
< |
free((char *)scanin); |
| 193 |
> |
/* clean up */ |
| 194 |
> |
if (oname[0] == '!') |
| 195 |
> |
pclose(fp); |
| 196 |
> |
else if (ospec != NULL) |
| 197 |
> |
fclose(fp); |
| 198 |
> |
return(1); |
| 199 |
> |
} |
| 200 |
> |
|
| 201 |
> |
|
| 202 |
> |
int |
| 203 |
> |
addhline(s) /* add a line to our info. header */ |
| 204 |
> |
char *s; |
| 205 |
> |
{ |
| 206 |
> |
char fmt[32]; |
| 207 |
> |
int n; |
| 208 |
> |
|
| 209 |
> |
if (formatval(fmt, s)) |
| 210 |
> |
fmterr += !globmatch(PICFMT, fmt); |
| 211 |
> |
else if (!strncmp(s, "FRAME=", 6)) |
| 212 |
> |
frameno = atoi(s+6); |
| 213 |
> |
n = strlen(s); |
| 214 |
> |
if (headlen) |
| 215 |
> |
headlines = (char *)realloc(headlines, headlen+n+1); |
| 216 |
> |
else |
| 217 |
> |
headlines = (char *)malloc(n+1); |
| 218 |
> |
if (headlines == NULL) { |
| 219 |
> |
perror(progname); |
| 220 |
> |
exit(1); |
| 221 |
> |
} |
| 222 |
> |
strcpy(headlines+headlen, s); |
| 223 |
> |
headlen += n; |
| 224 |
> |
return(0); |
| 225 |
> |
} |
| 226 |
> |
|
| 227 |
> |
|
| 228 |
> |
loadheader(fp) /* load an info. header into memory */ |
| 229 |
> |
FILE *fp; |
| 230 |
> |
{ |
| 231 |
> |
fmterr = 0; frameno = 0; |
| 232 |
> |
if (headlen) { /* free old header */ |
| 233 |
> |
free(headlines); |
| 234 |
> |
headlen = 0; |
| 235 |
> |
} |
| 236 |
> |
if (getheader(fp, addhline, NULL) < 0) |
| 237 |
> |
return(0); |
| 238 |
> |
if (fmterr) |
| 239 |
> |
return(-1); |
| 240 |
> |
return(1); |
| 241 |
|
} |