| 5 |
|
* program to convert from RADIANCE RLE to flat format |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
– |
#include <stdio.h> |
| 8 |
|
#include <math.h> |
| 10 |
– |
#include <time.h> |
| 11 |
– |
#include <string.h> |
| 9 |
|
|
| 10 |
|
#include "platform.h" |
| 11 |
< |
#include "rtprocess.h" |
| 11 |
> |
#include "rtio.h" |
| 12 |
> |
#include "paths.h" |
| 13 |
|
#include "color.h" |
| 14 |
|
#include "resolu.h" |
| 15 |
|
|
| 16 |
< |
extern int addhline(); |
| 16 |
> |
#define dumpheader(fp) putbinary(headlines, 1, headlen, fp) |
| 17 |
|
|
| 20 |
– |
#define dumpheader(fp) fwrite(headlines, 1, headlen, fp) |
| 21 |
– |
|
| 18 |
|
int bradj = 0; /* brightness adjustment */ |
| 23 |
– |
|
| 19 |
|
int doflat = 1; /* produce flat file */ |
| 25 |
– |
|
| 20 |
|
int force = 0; /* force file overwrite? */ |
| 27 |
– |
|
| 21 |
|
int findframe = 0; /* find a specific frame? */ |
| 29 |
– |
|
| 22 |
|
int frameno = 0; /* current frame number */ |
| 23 |
|
int fmterr = 0; /* got input format error */ |
| 24 |
< |
char *headlines; /* current header info. */ |
| 25 |
< |
int headlen; /* current header length */ |
| 24 |
> |
char *headlines = NULL; /* current header info. */ |
| 25 |
> |
int headlen1 = 0; /* length of initial frame header */ |
| 26 |
> |
int headlen = 0; /* current header length */ |
| 27 |
> |
char fmt[MAXFMTLEN]; /* input format */ |
| 28 |
|
|
| 29 |
|
char *progname; |
| 30 |
|
|
| 31 |
+ |
static gethfunc addhline; |
| 32 |
+ |
static int transfer(char *ospec); |
| 33 |
+ |
static int loadheader(FILE *fp); |
| 34 |
|
|
| 35 |
< |
main(argc, argv) |
| 36 |
< |
int argc; |
| 37 |
< |
char *argv[]; |
| 35 |
> |
|
| 36 |
> |
int |
| 37 |
> |
main(int argc, char *argv[]) |
| 38 |
|
{ |
| 39 |
|
char *ospec; |
| 40 |
|
int i; |
| 87 |
|
} |
| 88 |
|
|
| 89 |
|
|
| 90 |
< |
transfer(ospec) /* transfer a Radiance picture */ |
| 91 |
< |
char *ospec; |
| 90 |
> |
static int |
| 91 |
> |
transfer( /* transfer a Radiance picture */ |
| 92 |
> |
char *ospec |
| 93 |
> |
) |
| 94 |
|
{ |
| 95 |
< |
char oname[128]; |
| 95 |
> |
char oname[PATH_MAX]; |
| 96 |
|
FILE *fp; |
| 97 |
|
int order; |
| 98 |
|
int xmax, ymax; |
| 109 |
|
if (findframe && findframe < frameno) |
| 110 |
|
return(0); |
| 111 |
|
/* allocate scanline */ |
| 112 |
< |
scanin = (COLR *)tempbuffer(xmax*sizeof(COLR)); |
| 112 |
> |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
| 113 |
|
if (scanin == NULL) { |
| 114 |
|
perror(progname); |
| 115 |
|
exit(1); |
| 116 |
|
} |
| 117 |
|
/* skip frame? */ |
| 118 |
|
if (findframe > frameno) { |
| 119 |
< |
for (y = ymax; y--; ) |
| 119 |
> |
if (NCSAMP > 3) { |
| 120 |
> |
if (fseek(stdin, ymax*xmax*LSCOLR, SEEK_CUR) < 0) { |
| 121 |
> |
perror(progname); |
| 122 |
> |
exit(1); |
| 123 |
> |
} |
| 124 |
> |
} else |
| 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 |
+ |
free(scanin); |
| 133 |
|
return(1); |
| 134 |
|
} |
| 135 |
|
/* open output file/command */ |
| 159 |
|
} |
| 160 |
|
} |
| 161 |
|
SET_FILE_BINARY(fp); |
| 162 |
< |
dumpheader(fp); /* put out header */ |
| 162 |
> |
newheader("RADIANCE", fp); /* put out header */ |
| 163 |
> |
dumpheader(fp); |
| 164 |
|
fputs(progname, fp); |
| 165 |
|
if (bradj) |
| 166 |
|
fprintf(fp, " -e %+d", bradj); |
| 169 |
|
fputc('\n', fp); |
| 170 |
|
if (bradj) |
| 171 |
|
fputexpos(pow(2.0, (double)bradj), fp); |
| 172 |
+ |
if (frameno) |
| 173 |
+ |
fprintf(fp, "FRAME=%d\n", frameno); |
| 174 |
+ |
if (fmt[0]) |
| 175 |
+ |
fputformat(fmt, fp); |
| 176 |
|
fputc('\n', fp); |
| 177 |
|
fputresolu(order, xmax, ymax, fp); |
| 178 |
|
/* transfer picture */ |
| 179 |
|
for (y = ymax; y--; ) { |
| 180 |
< |
if (freadcolrs(scanin, xmax, stdin) < 0) { |
| 180 |
> |
if (fread2colrs(scanin, xmax, stdin, NCSAMP, WLPART) < 0) { |
| 181 |
|
fprintf(stderr, "%s: error reading input picture\n", |
| 182 |
|
progname); |
| 183 |
|
exit(1); |
| 184 |
|
} |
| 185 |
|
if (bradj) |
| 186 |
|
shiftcolrs(scanin, xmax, bradj); |
| 187 |
< |
if (doflat) |
| 188 |
< |
fwrite((char *)scanin, sizeof(COLR), xmax, fp); |
| 189 |
< |
else |
| 179 |
< |
fwritecolrs(scanin, xmax, fp); |
| 180 |
< |
if (ferror(fp)) { |
| 181 |
< |
fprintf(stderr, "%s: error writing output to \"%s\"\n", |
| 182 |
< |
progname, oname); |
| 183 |
< |
exit(1); |
| 184 |
< |
} |
| 187 |
> |
if (doflat ? (putbinary(scanin, sizeof(COLR), xmax, fp) != xmax) : |
| 188 |
> |
(fwritecolrs(scanin, xmax, fp) < 0)) |
| 189 |
> |
goto writerr; |
| 190 |
|
} |
| 191 |
< |
/* clean up */ |
| 192 |
< |
if (oname[0] == '!') |
| 193 |
< |
pclose(fp); |
| 194 |
< |
else if (ospec != NULL) |
| 191 |
> |
free(scanin); /* clean up */ |
| 192 |
> |
if (fflush(fp) == EOF) |
| 193 |
> |
goto writerr; |
| 194 |
> |
if (oname[0] == '!') { |
| 195 |
> |
if (pclose(fp) != 0) |
| 196 |
> |
fprintf(stderr, "%s: warning - bad status from \"%s\"\n", |
| 197 |
> |
progname, oname); |
| 198 |
> |
} else if (ospec != NULL) |
| 199 |
|
fclose(fp); |
| 200 |
|
return(1); |
| 201 |
+ |
writerr: |
| 202 |
+ |
fprintf(stderr, "%s: error writing output to \"%s\"\n", |
| 203 |
+ |
progname, oname); |
| 204 |
+ |
exit(1); |
| 205 |
|
} |
| 206 |
|
|
| 207 |
|
|
| 208 |
< |
int |
| 209 |
< |
addhline(s) /* add a line to our info. header */ |
| 210 |
< |
char *s; |
| 208 |
> |
static int |
| 209 |
> |
addhline( /* add a line to our info. header */ |
| 210 |
> |
char *s, |
| 211 |
> |
void *p |
| 212 |
> |
) |
| 213 |
|
{ |
| 199 |
– |
char fmt[32]; |
| 214 |
|
int n; |
| 215 |
|
|
| 216 |
< |
if (formatval(fmt, s)) |
| 217 |
< |
fmterr += !globmatch(PICFMT, fmt); |
| 218 |
< |
else if (!strncmp(s, "FRAME=", 6)) |
| 216 |
> |
if (isheadid(s)) |
| 217 |
> |
return(0); |
| 218 |
> |
if (!strncmp(s, "FRAME=", 6)) { |
| 219 |
|
frameno = atoi(s+6); |
| 220 |
+ |
return(0); |
| 221 |
+ |
} |
| 222 |
+ |
if (formatval(fmt, s)) { |
| 223 |
+ |
if (!strcmp(fmt, SPECFMT)) |
| 224 |
+ |
strcpy(fmt, COLRFMT); |
| 225 |
+ |
else |
| 226 |
+ |
fmterr += !globmatch(PICFMT, fmt); |
| 227 |
+ |
return(0); |
| 228 |
+ |
} |
| 229 |
+ |
if (isncomp(s)) { |
| 230 |
+ |
NCSAMP = ncompval(s); |
| 231 |
+ |
return(NCSAMP - 3); |
| 232 |
+ |
} |
| 233 |
+ |
if (iswlsplit(s)) { |
| 234 |
+ |
wlsplitval(WLPART, s); |
| 235 |
+ |
return(0); |
| 236 |
+ |
} |
| 237 |
|
n = strlen(s); |
| 238 |
|
if (headlen) |
| 239 |
|
headlines = (char *)realloc((void *)headlines, headlen+n+1); |
| 249 |
|
} |
| 250 |
|
|
| 251 |
|
|
| 252 |
< |
loadheader(fp) /* load an info. header into memory */ |
| 253 |
< |
FILE *fp; |
| 252 |
> |
static int |
| 253 |
> |
loadheader( /* load an info. header into memory */ |
| 254 |
> |
FILE *fp |
| 255 |
> |
) |
| 256 |
|
{ |
| 257 |
|
fmterr = 0; frameno = 0; |
| 258 |
< |
if (headlen) { /* free old header */ |
| 259 |
< |
free(headlines); |
| 260 |
< |
headlen = 0; |
| 261 |
< |
} |
| 258 |
> |
/* revert to initial header length */ |
| 259 |
> |
if (!headlen1) headlen1 = headlen; |
| 260 |
> |
else headlen = headlen1; |
| 261 |
> |
|
| 262 |
|
if (getheader(fp, addhline, NULL) < 0) |
| 263 |
|
return(0); |
| 264 |
|
if (fmterr) |