| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: ra_rgbe.c,v 2.25 2025/04/22 14:51:29 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* program to convert from RADIANCE RLE to flat format
|
| 6 |
*/
|
| 7 |
|
| 8 |
#include <math.h>
|
| 9 |
|
| 10 |
#include "platform.h"
|
| 11 |
#include "rtio.h"
|
| 12 |
#include "color.h"
|
| 13 |
#include "resolu.h"
|
| 14 |
|
| 15 |
#define dumpheader(fp) putbinary(headlines, 1, headlen, fp)
|
| 16 |
|
| 17 |
int bradj = 0; /* brightness adjustment */
|
| 18 |
int doflat = 1; /* produce flat file */
|
| 19 |
int force = 0; /* force file overwrite? */
|
| 20 |
int findframe = 0; /* find a specific frame? */
|
| 21 |
int frameno = 0; /* current frame number */
|
| 22 |
int fmterr = 0; /* got input format error */
|
| 23 |
char *headlines = NULL; /* current header info. */
|
| 24 |
int headlen1 = 0; /* length of initial frame header */
|
| 25 |
int headlen = 0; /* current header length */
|
| 26 |
char fmt[MAXFMTLEN]; /* input format */
|
| 27 |
|
| 28 |
char *progname;
|
| 29 |
|
| 30 |
static gethfunc addhline;
|
| 31 |
static int transfer(char *ospec);
|
| 32 |
static int loadheader(FILE *fp);
|
| 33 |
|
| 34 |
|
| 35 |
int
|
| 36 |
main(int argc, char *argv[])
|
| 37 |
{
|
| 38 |
char *ospec;
|
| 39 |
int i;
|
| 40 |
|
| 41 |
progname = argv[0];
|
| 42 |
|
| 43 |
for (i = 1; i < argc; i++)
|
| 44 |
if (argv[i][0] == '-')
|
| 45 |
switch (argv[i][1]) {
|
| 46 |
case 'r':
|
| 47 |
doflat = !doflat;
|
| 48 |
break;
|
| 49 |
case 'e':
|
| 50 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
|
| 51 |
goto userr;
|
| 52 |
bradj = atoi(argv[++i]);
|
| 53 |
break;
|
| 54 |
case 'n':
|
| 55 |
findframe = atoi(argv[++i]);
|
| 56 |
break;
|
| 57 |
case 'f':
|
| 58 |
force++;
|
| 59 |
break;
|
| 60 |
case '\0':
|
| 61 |
goto gotfile;
|
| 62 |
default:
|
| 63 |
goto userr;
|
| 64 |
}
|
| 65 |
else
|
| 66 |
break;
|
| 67 |
gotfile:
|
| 68 |
if (i < argc-2)
|
| 69 |
goto userr;
|
| 70 |
if (i <= argc-1 && strcmp(argv[i], "-") &&
|
| 71 |
freopen(argv[i], "r", stdin) == NULL) {
|
| 72 |
fprintf(stderr, "%s: can't open input \"%s\"\n",
|
| 73 |
progname, argv[i]);
|
| 74 |
exit(1);
|
| 75 |
}
|
| 76 |
SET_FILE_BINARY(stdin);
|
| 77 |
ospec = i==argc-2 ? argv[i+1] : (char *)NULL;
|
| 78 |
while (transfer(ospec))
|
| 79 |
;
|
| 80 |
exit(0);
|
| 81 |
userr:
|
| 82 |
fprintf(stderr,
|
| 83 |
"Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n",
|
| 84 |
progname);
|
| 85 |
exit(1);
|
| 86 |
}
|
| 87 |
|
| 88 |
|
| 89 |
static int
|
| 90 |
transfer( /* transfer a Radiance picture */
|
| 91 |
char *ospec
|
| 92 |
)
|
| 93 |
{
|
| 94 |
char oname[PATH_MAX];
|
| 95 |
FILE *fp;
|
| 96 |
int order;
|
| 97 |
int xmax, ymax;
|
| 98 |
COLR *scanin;
|
| 99 |
int y;
|
| 100 |
/* get header info. */
|
| 101 |
if (!(y = loadheader(stdin)))
|
| 102 |
return(0);
|
| 103 |
if (y < 0 || (order = fgetresolu(&xmax, &ymax, stdin)) < 0) {
|
| 104 |
fprintf(stderr, "%s: bad input format\n", progname);
|
| 105 |
exit(1);
|
| 106 |
}
|
| 107 |
/* did we pass the target frame? */
|
| 108 |
if (findframe && findframe < frameno)
|
| 109 |
return(0);
|
| 110 |
/* allocate scanline */
|
| 111 |
scanin = (COLR *)malloc(xmax*sizeof(COLR));
|
| 112 |
if (scanin == NULL) {
|
| 113 |
perror(progname);
|
| 114 |
exit(1);
|
| 115 |
}
|
| 116 |
/* skip frame? */
|
| 117 |
if (findframe > frameno) {
|
| 118 |
if (NCSAMP > 3) {
|
| 119 |
if (fseek(stdin, ymax*xmax*LSCOLR, SEEK_CUR) < 0) {
|
| 120 |
perror(progname);
|
| 121 |
exit(1);
|
| 122 |
}
|
| 123 |
} else
|
| 124 |
for (y = ymax; y--; )
|
| 125 |
if (freadcolrs(scanin, xmax, stdin) < 0) {
|
| 126 |
fprintf(stderr,
|
| 127 |
"%s: error reading input picture\n",
|
| 128 |
progname);
|
| 129 |
exit(1);
|
| 130 |
}
|
| 131 |
free(scanin);
|
| 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 |
SET_FILE_BINARY(fp);
|
| 161 |
newheader("RADIANCE", fp); /* put out header */
|
| 162 |
dumpheader(fp);
|
| 163 |
fputs(progname, fp);
|
| 164 |
if (bradj)
|
| 165 |
fprintf(fp, " -e %+d", bradj);
|
| 166 |
if (!doflat)
|
| 167 |
fputs(" -r", fp);
|
| 168 |
fputc('\n', fp);
|
| 169 |
if (bradj)
|
| 170 |
fputexpos(pow(2.0, (double)bradj), fp);
|
| 171 |
if (frameno)
|
| 172 |
fprintf(fp, "FRAME=%d\n", frameno);
|
| 173 |
if (fmt[0])
|
| 174 |
fputformat(fmt, fp);
|
| 175 |
fputc('\n', fp);
|
| 176 |
fputresolu(order, xmax, ymax, fp);
|
| 177 |
/* transfer picture */
|
| 178 |
for (y = ymax; y--; ) {
|
| 179 |
if (fread2colrs(scanin, xmax, stdin, NCSAMP, WLPART) < 0) {
|
| 180 |
fprintf(stderr, "%s: error reading input picture\n",
|
| 181 |
progname);
|
| 182 |
exit(1);
|
| 183 |
}
|
| 184 |
if (bradj)
|
| 185 |
shiftcolrs(scanin, xmax, bradj);
|
| 186 |
if (doflat ? (putbinary(scanin, sizeof(COLR), xmax, fp) != xmax) :
|
| 187 |
(fwritecolrs(scanin, xmax, fp) < 0))
|
| 188 |
goto writerr;
|
| 189 |
}
|
| 190 |
free(scanin); /* clean up */
|
| 191 |
if (fflush(fp) == EOF)
|
| 192 |
goto writerr;
|
| 193 |
if (oname[0] == '!') {
|
| 194 |
if (pclose(fp) != 0)
|
| 195 |
fprintf(stderr, "%s: warning - bad status from \"%s\"\n",
|
| 196 |
progname, oname);
|
| 197 |
} else if (ospec != NULL)
|
| 198 |
fclose(fp);
|
| 199 |
return(1);
|
| 200 |
writerr:
|
| 201 |
fprintf(stderr, "%s: error writing output to \"%s\"\n",
|
| 202 |
progname, oname);
|
| 203 |
exit(1);
|
| 204 |
}
|
| 205 |
|
| 206 |
|
| 207 |
static int
|
| 208 |
addhline( /* add a line to our info. header */
|
| 209 |
char *s,
|
| 210 |
void *p
|
| 211 |
)
|
| 212 |
{
|
| 213 |
int n;
|
| 214 |
|
| 215 |
if (isheadid(s))
|
| 216 |
return(0);
|
| 217 |
if (!strncmp(s, "FRAME=", 6)) {
|
| 218 |
frameno = atoi(s+6);
|
| 219 |
return(0);
|
| 220 |
}
|
| 221 |
if (formatval(fmt, s)) {
|
| 222 |
if (!strcmp(fmt, SPECFMT))
|
| 223 |
strcpy(fmt, COLRFMT);
|
| 224 |
else
|
| 225 |
fmterr += !globmatch(PICFMT, fmt);
|
| 226 |
return(0);
|
| 227 |
}
|
| 228 |
if (isncomp(s)) {
|
| 229 |
NCSAMP = ncompval(s);
|
| 230 |
return(NCSAMP - 3);
|
| 231 |
}
|
| 232 |
if (iswlsplit(s)) {
|
| 233 |
wlsplitval(WLPART, s);
|
| 234 |
return(0);
|
| 235 |
}
|
| 236 |
n = strlen(s);
|
| 237 |
if (headlen)
|
| 238 |
headlines = (char *)realloc((void *)headlines, headlen+n+1);
|
| 239 |
else
|
| 240 |
headlines = (char *)malloc(n+1);
|
| 241 |
if (headlines == NULL) {
|
| 242 |
perror(progname);
|
| 243 |
exit(1);
|
| 244 |
}
|
| 245 |
strcpy(headlines+headlen, s);
|
| 246 |
headlen += n;
|
| 247 |
return(0);
|
| 248 |
}
|
| 249 |
|
| 250 |
|
| 251 |
static int
|
| 252 |
loadheader( /* load an info. header into memory */
|
| 253 |
FILE *fp
|
| 254 |
)
|
| 255 |
{
|
| 256 |
fmterr = 0; frameno = 0;
|
| 257 |
/* revert to initial header length */
|
| 258 |
if (!headlen1) headlen1 = headlen;
|
| 259 |
else headlen = headlen1;
|
| 260 |
|
| 261 |
if (getheader(fp, addhline, NULL) < 0)
|
| 262 |
return(0);
|
| 263 |
if (fmterr)
|
| 264 |
return(-1);
|
| 265 |
return(1);
|
| 266 |
}
|