| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id$";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* ra_bn.c - program to convert between RADIANCE and barneyscan picture format.
|
| 6 |
*
|
| 7 |
* 10/12/88
|
| 8 |
*/
|
| 9 |
|
| 10 |
#include <stdio.h>
|
| 11 |
|
| 12 |
#ifdef MSDOS
|
| 13 |
#include <fcntl.h>
|
| 14 |
#endif
|
| 15 |
|
| 16 |
#include <time.h>
|
| 17 |
|
| 18 |
#include <math.h>
|
| 19 |
|
| 20 |
#include "color.h"
|
| 21 |
|
| 22 |
#include "resolu.h"
|
| 23 |
|
| 24 |
double gamcor = 2.0; /* gamma correction */
|
| 25 |
|
| 26 |
int bradj = 0; /* brightness adjustment */
|
| 27 |
|
| 28 |
char *progname;
|
| 29 |
|
| 30 |
char errmsg[128];
|
| 31 |
|
| 32 |
FILE *rafp, *bnfp[3];
|
| 33 |
|
| 34 |
int xmax, ymax;
|
| 35 |
|
| 36 |
|
| 37 |
main(argc, argv)
|
| 38 |
int argc;
|
| 39 |
char *argv[];
|
| 40 |
{
|
| 41 |
int reverse = 0;
|
| 42 |
int i;
|
| 43 |
#ifdef MSDOS
|
| 44 |
extern int _fmode;
|
| 45 |
_fmode = O_BINARY;
|
| 46 |
setmode(fileno(stdin), O_BINARY);
|
| 47 |
setmode(fileno(stdout), O_BINARY);
|
| 48 |
#endif
|
| 49 |
progname = argv[0];
|
| 50 |
|
| 51 |
for (i = 1; i < argc; i++)
|
| 52 |
if (argv[i][0] == '-')
|
| 53 |
switch (argv[i][1]) {
|
| 54 |
case 'g':
|
| 55 |
gamcor = atof(argv[++i]);
|
| 56 |
break;
|
| 57 |
case 'r':
|
| 58 |
reverse = !reverse;
|
| 59 |
break;
|
| 60 |
case 'e':
|
| 61 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
|
| 62 |
goto userr;
|
| 63 |
bradj = atoi(argv[++i]);
|
| 64 |
break;
|
| 65 |
default:
|
| 66 |
goto userr;
|
| 67 |
}
|
| 68 |
else
|
| 69 |
break;
|
| 70 |
/* set gamma correction */
|
| 71 |
setcolrgam(gamcor);
|
| 72 |
|
| 73 |
if (reverse) {
|
| 74 |
if (i > argc-1 || i < argc-2)
|
| 75 |
goto userr;
|
| 76 |
if (openbarney(argv[i], "r") < 0) {
|
| 77 |
sprintf(errmsg, "cannot open input \"%s\"", argv[i]);
|
| 78 |
quiterr(errmsg);
|
| 79 |
}
|
| 80 |
if (i == argc-1 || !strcmp(argv[i+1], "-"))
|
| 81 |
rafp = stdout;
|
| 82 |
else if ((rafp = fopen(argv[i+1], "w")) == NULL) {
|
| 83 |
sprintf(errmsg, "cannot open output \"%s\"",
|
| 84 |
argv[i+1]);
|
| 85 |
quiterr(errmsg);
|
| 86 |
}
|
| 87 |
/* put header */
|
| 88 |
newheader("RADIANCE", stdout);
|
| 89 |
printargs(i, argv, rafp);
|
| 90 |
fputformat(COLRFMT, rafp);
|
| 91 |
putc('\n', rafp);
|
| 92 |
fprtresolu(xmax, ymax, rafp);
|
| 93 |
/* convert file */
|
| 94 |
bn2ra();
|
| 95 |
} else {
|
| 96 |
if (i != argc-2)
|
| 97 |
goto userr;
|
| 98 |
if (!strcmp(argv[i], "-"))
|
| 99 |
rafp = stdin;
|
| 100 |
else if ((rafp = fopen(argv[i], "r")) == NULL) {
|
| 101 |
sprintf(errmsg, "cannot open input \"%s\"",
|
| 102 |
argv[i]);
|
| 103 |
quiterr(errmsg);
|
| 104 |
}
|
| 105 |
/* get header */
|
| 106 |
if (checkheader(rafp, COLRFMT, NULL) < 0 ||
|
| 107 |
fgetresolu(&xmax, &ymax, rafp) < 0)
|
| 108 |
quiterr("bad RADIANCE format");
|
| 109 |
if (openbarney(argv[i+1], "w") < 0) {
|
| 110 |
sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]);
|
| 111 |
quiterr(errmsg);
|
| 112 |
}
|
| 113 |
/* convert file */
|
| 114 |
ra2bn();
|
| 115 |
}
|
| 116 |
quiterr(NULL);
|
| 117 |
userr:
|
| 118 |
fprintf(stderr, "Usage: %s [-g gamma][-e +/-stops] {input|-} output\n",
|
| 119 |
progname);
|
| 120 |
fprintf(stderr, " or: %s -r [-g gamma][-e +/-stops] input [output|-]\n",
|
| 121 |
progname);
|
| 122 |
exit(1);
|
| 123 |
}
|
| 124 |
|
| 125 |
|
| 126 |
quiterr(err) /* print message and exit */
|
| 127 |
char *err;
|
| 128 |
{
|
| 129 |
if (err != NULL) {
|
| 130 |
fprintf(stderr, "%s: %s\n", progname, err);
|
| 131 |
exit(1);
|
| 132 |
}
|
| 133 |
exit(0);
|
| 134 |
}
|
| 135 |
|
| 136 |
|
| 137 |
openbarney(fname, mode) /* open barneyscan files */
|
| 138 |
char *fname;
|
| 139 |
char *mode;
|
| 140 |
{
|
| 141 |
static char suffix[3][4] = {"red", "grn", "blu"};
|
| 142 |
int i;
|
| 143 |
char file[128];
|
| 144 |
|
| 145 |
for (i = 0; i < 3; i++) {
|
| 146 |
sprintf(file, "%s.%s", fname, suffix[i]);
|
| 147 |
if ((bnfp[i] = fopen(file, mode)) == NULL)
|
| 148 |
return(-1);
|
| 149 |
}
|
| 150 |
if (mode[0] == 'r') { /* get header */
|
| 151 |
xmax = getint(bnfp[0]);
|
| 152 |
ymax = getint(bnfp[0]);
|
| 153 |
for (i = 1; i < 3; i++)
|
| 154 |
if (getint(bnfp[i]) != xmax || getint(bnfp[i]) != ymax)
|
| 155 |
return(-1);
|
| 156 |
} else { /* put header */
|
| 157 |
for (i = 0; i < 3; i++) {
|
| 158 |
putint(xmax, bnfp[i]);
|
| 159 |
putint(ymax, bnfp[i]);
|
| 160 |
}
|
| 161 |
}
|
| 162 |
return(0);
|
| 163 |
}
|
| 164 |
|
| 165 |
|
| 166 |
int
|
| 167 |
getint(fp) /* get short int from barneyscan file */
|
| 168 |
register FILE *fp;
|
| 169 |
{
|
| 170 |
register short val;
|
| 171 |
|
| 172 |
val = getc(fp);
|
| 173 |
val |= getc(fp) << 8;
|
| 174 |
|
| 175 |
return(val);
|
| 176 |
}
|
| 177 |
|
| 178 |
|
| 179 |
putint(val, fp) /* put short int to barneyscan file */
|
| 180 |
register int val;
|
| 181 |
register FILE *fp;
|
| 182 |
{
|
| 183 |
putc(val&0xff, fp);
|
| 184 |
putc((val >> 8)&0xff, fp);
|
| 185 |
}
|
| 186 |
|
| 187 |
|
| 188 |
ra2bn() /* convert radiance to barneyscan */
|
| 189 |
{
|
| 190 |
register int i;
|
| 191 |
register COLR *inl;
|
| 192 |
int j;
|
| 193 |
|
| 194 |
if ((inl = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
|
| 195 |
quiterr("out of memory");
|
| 196 |
for (j = 0; j < ymax; j++) {
|
| 197 |
if (freadcolrs(inl, xmax, rafp) < 0)
|
| 198 |
quiterr("error reading RADIANCE file");
|
| 199 |
if (bradj)
|
| 200 |
shiftcolrs(inl, xmax, bradj);
|
| 201 |
colrs_gambs(inl, xmax);
|
| 202 |
for (i = 0; i < xmax; i++) {
|
| 203 |
putc(inl[i][RED], bnfp[0]);
|
| 204 |
putc(inl[i][GRN], bnfp[1]);
|
| 205 |
putc(inl[i][BLU], bnfp[2]);
|
| 206 |
}
|
| 207 |
if (ferror(bnfp[0]) || ferror(bnfp[1]) || ferror(bnfp[2]))
|
| 208 |
quiterr("error writing Barney files");
|
| 209 |
}
|
| 210 |
free((void *)inl);
|
| 211 |
}
|
| 212 |
|
| 213 |
|
| 214 |
bn2ra() /* convert barneyscan to radiance */
|
| 215 |
{
|
| 216 |
register int i;
|
| 217 |
register COLR *outline;
|
| 218 |
int j;
|
| 219 |
|
| 220 |
if ((outline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
|
| 221 |
quiterr("out of memory");
|
| 222 |
for (j = 0; j < ymax; j++) {
|
| 223 |
for (i = 0; i < xmax; i++) {
|
| 224 |
outline[i][RED] = getc(bnfp[0]);
|
| 225 |
outline[i][GRN] = getc(bnfp[1]);
|
| 226 |
outline[i][BLU] = getc(bnfp[2]);
|
| 227 |
}
|
| 228 |
if (feof(bnfp[0]) || feof(bnfp[1]) || feof(bnfp[2]))
|
| 229 |
quiterr("error reading barney file");
|
| 230 |
gambs_colrs(outline, xmax);
|
| 231 |
if (bradj)
|
| 232 |
shiftcolrs(outline, xmax, bradj);
|
| 233 |
if (fwritecolrs(outline, xmax, rafp) < 0)
|
| 234 |
quiterr("error writing RADIANCE file");
|
| 235 |
}
|
| 236 |
free((void *)outline);
|
| 237 |
}
|