| 1 |
greg |
2.1 |
#ifndef lint |
| 2 |
schorsch |
2.9 |
static const char RCSid[] = "$Id: ra_xyze.c,v 2.8 2004/01/02 12:47:01 schorsch Exp $"; |
| 3 |
greg |
2.1 |
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* Program to convert between RADIANCE RGBE and XYZE formats |
| 6 |
greg |
2.6 |
* Added white-balance adjustment 10/01 (GW). |
| 7 |
greg |
2.1 |
*/ |
| 8 |
|
|
|
| 9 |
|
|
#include <stdio.h> |
| 10 |
greg |
2.6 |
#include <string.h> |
| 11 |
greg |
2.1 |
#include <math.h> |
| 12 |
greg |
2.6 |
#include <time.h> |
| 13 |
schorsch |
2.7 |
|
| 14 |
|
|
#include "platform.h" |
| 15 |
greg |
2.1 |
#include "color.h" |
| 16 |
|
|
#include "resolu.h" |
| 17 |
|
|
|
| 18 |
|
|
int rgbinp = -1; /* input is RGBE? */ |
| 19 |
|
|
int rgbout = 0; /* output should be RGBE? */ |
| 20 |
|
|
RGBPRIMS inprims = STDPRIMS; /* input primaries */ |
| 21 |
|
|
RGBPRIMS outprims = STDPRIMS; /* output primaries */ |
| 22 |
|
|
double expcomp = 1.0; /* exposure compensation */ |
| 23 |
|
|
int doflat = -1; /* produce flat file? */ |
| 24 |
|
|
char *progname; |
| 25 |
|
|
|
| 26 |
schorsch |
2.8 |
static gethfunc headline; |
| 27 |
schorsch |
2.9 |
static void quiterr(char *err); |
| 28 |
|
|
static void convert(void); |
| 29 |
|
|
|
| 30 |
greg |
2.1 |
|
| 31 |
schorsch |
2.8 |
|
| 32 |
|
|
static int |
| 33 |
|
|
headline( /* process header line */ |
| 34 |
|
|
char *s, |
| 35 |
|
|
void *p |
| 36 |
|
|
) |
| 37 |
greg |
2.1 |
{ |
| 38 |
|
|
char fmt[32]; |
| 39 |
|
|
|
| 40 |
|
|
if (formatval(fmt, s)) { /* check if format string */ |
| 41 |
|
|
if (!strcmp(fmt,COLRFMT)) |
| 42 |
|
|
rgbinp = 1; |
| 43 |
|
|
else if (!strcmp(fmt,CIEFMT)) |
| 44 |
|
|
rgbinp = 0; |
| 45 |
|
|
else |
| 46 |
|
|
rgbinp = -2; |
| 47 |
gwlarson |
2.5 |
return(0); /* don't echo */ |
| 48 |
greg |
2.1 |
} |
| 49 |
|
|
if (isprims(s)) { /* get input primaries */ |
| 50 |
|
|
primsval(inprims, s); |
| 51 |
gwlarson |
2.5 |
return(0); /* don't echo */ |
| 52 |
greg |
2.1 |
} |
| 53 |
|
|
/* should I grok colcorr also? */ |
| 54 |
gwlarson |
2.5 |
return(fputs(s, stdout)); |
| 55 |
greg |
2.1 |
} |
| 56 |
|
|
|
| 57 |
|
|
|
| 58 |
schorsch |
2.9 |
int |
| 59 |
|
|
main(int argc, char *argv[]) |
| 60 |
greg |
2.1 |
{ |
| 61 |
|
|
int i; |
| 62 |
schorsch |
2.7 |
SET_DEFAULT_BINARY(); |
| 63 |
|
|
SET_FILE_BINARY(stdin); |
| 64 |
|
|
SET_FILE_BINARY(stdout); |
| 65 |
greg |
2.1 |
progname = argv[0]; |
| 66 |
|
|
|
| 67 |
|
|
for (i = 1; i < argc; i++) |
| 68 |
|
|
if (argv[i][0] == '-') |
| 69 |
|
|
switch (argv[i][1]) { |
| 70 |
|
|
case 'c': /* rle-compressed output */ |
| 71 |
|
|
doflat = 0; |
| 72 |
|
|
break; |
| 73 |
|
|
case 'u': /* flat output */ |
| 74 |
|
|
doflat = 1; |
| 75 |
|
|
break; |
| 76 |
|
|
case 'r': /* RGBE output */ |
| 77 |
|
|
rgbout = 1; |
| 78 |
|
|
break; |
| 79 |
|
|
case 'p': /* RGB primaries */ |
| 80 |
greg |
2.2 |
if (i+8 >= argc) |
| 81 |
greg |
2.1 |
goto userr; |
| 82 |
|
|
outprims[RED][CIEX] = atof(argv[++i]); |
| 83 |
|
|
outprims[RED][CIEY] = atof(argv[++i]); |
| 84 |
|
|
outprims[GRN][CIEX] = atof(argv[++i]); |
| 85 |
|
|
outprims[GRN][CIEY] = atof(argv[++i]); |
| 86 |
|
|
outprims[BLU][CIEX] = atof(argv[++i]); |
| 87 |
|
|
outprims[BLU][CIEY] = atof(argv[++i]); |
| 88 |
|
|
outprims[WHT][CIEX] = atof(argv[++i]); |
| 89 |
|
|
outprims[WHT][CIEY] = atof(argv[++i]); |
| 90 |
|
|
break; |
| 91 |
|
|
case 'e': /* exposure compensation */ |
| 92 |
|
|
expcomp = atof(argv[++i]); |
| 93 |
|
|
if (argv[i][0] == '+' || argv[i][0] == '-') |
| 94 |
|
|
expcomp = pow(2., expcomp); |
| 95 |
|
|
break; |
| 96 |
|
|
default: |
| 97 |
|
|
goto userr; |
| 98 |
|
|
} |
| 99 |
|
|
else |
| 100 |
|
|
break; |
| 101 |
|
|
|
| 102 |
|
|
if (doflat < 0) |
| 103 |
|
|
doflat = !rgbout; |
| 104 |
|
|
if (i < argc-2) |
| 105 |
|
|
goto userr; |
| 106 |
|
|
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
| 107 |
|
|
fprintf(stderr, "%s: can't open input \"%s\"\n", |
| 108 |
|
|
progname, argv[i]); |
| 109 |
|
|
exit(1); |
| 110 |
|
|
} |
| 111 |
|
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
| 112 |
|
|
fprintf(stderr, "%s: can't open output \"%s\"\n", |
| 113 |
|
|
progname, argv[i+1]); |
| 114 |
|
|
exit(1); |
| 115 |
|
|
} |
| 116 |
|
|
getheader(stdin, headline, NULL); |
| 117 |
|
|
if (rgbinp == -2) |
| 118 |
|
|
quiterr("unrecognized input file format"); |
| 119 |
|
|
if (rgbinp == -1) |
| 120 |
|
|
rgbinp = !rgbout; |
| 121 |
|
|
printargs(argc, argv, stdout); /* add to header */ |
| 122 |
|
|
convert(); /* convert picture */ |
| 123 |
|
|
exit(0); |
| 124 |
|
|
userr: |
| 125 |
|
|
fprintf(stderr, "Usage: %s [-r][-e exp][-c|-u]", progname); |
| 126 |
|
|
fprintf(stderr, "[-p rx ry gx gy bx by wx wy] [input [output]]\n"); |
| 127 |
|
|
exit(1); |
| 128 |
|
|
} |
| 129 |
|
|
|
| 130 |
|
|
|
| 131 |
schorsch |
2.9 |
static void |
| 132 |
|
|
quiterr( /* print message and exit */ |
| 133 |
|
|
char *err |
| 134 |
|
|
) |
| 135 |
greg |
2.1 |
{ |
| 136 |
|
|
if (err != NULL) { |
| 137 |
|
|
fprintf(stderr, "%s: %s\n", progname, err); |
| 138 |
|
|
exit(1); |
| 139 |
|
|
} |
| 140 |
|
|
exit(0); |
| 141 |
|
|
} |
| 142 |
|
|
|
| 143 |
|
|
|
| 144 |
schorsch |
2.9 |
static void |
| 145 |
|
|
convert(void) /* convert to XYZE or RGBE picture */ |
| 146 |
greg |
2.1 |
{ |
| 147 |
|
|
int order; |
| 148 |
|
|
int xmax, ymax; |
| 149 |
|
|
COLORMAT xfm; |
| 150 |
|
|
register COLOR *scanin; |
| 151 |
|
|
register COLR *scanout; |
| 152 |
gregl |
2.4 |
double ourexp = expcomp; |
| 153 |
greg |
2.1 |
int y; |
| 154 |
|
|
register int x; |
| 155 |
|
|
/* compute transform */ |
| 156 |
|
|
if (rgbout) { |
| 157 |
|
|
if (rgbinp) { /* RGBE -> RGBE */ |
| 158 |
greg |
2.6 |
comprgb2rgbWBmat(xfm, inprims, outprims); |
| 159 |
greg |
2.1 |
} else { /* XYZE -> RGBE */ |
| 160 |
greg |
2.6 |
compxyz2rgbWBmat(xfm, outprims); |
| 161 |
gregl |
2.4 |
ourexp *= WHTEFFICACY; |
| 162 |
greg |
2.1 |
} |
| 163 |
|
|
} else { |
| 164 |
|
|
if (rgbinp) { /* RGBE -> XYZE */ |
| 165 |
greg |
2.6 |
comprgb2xyzWBmat(xfm, inprims); |
| 166 |
gregl |
2.4 |
ourexp /= WHTEFFICACY; |
| 167 |
greg |
2.1 |
} else { /* XYZE -> XYZE */ |
| 168 |
|
|
for (y = 0; y < 3; y++) |
| 169 |
|
|
for (x = 0; x < 3; x++) |
| 170 |
gregl |
2.4 |
xfm[y][x] = x==y ? 1. : 0.; |
| 171 |
greg |
2.1 |
} |
| 172 |
|
|
} |
| 173 |
gregl |
2.4 |
for (y = 0; y < 3; y++) |
| 174 |
|
|
for (x = 0; x < 3; x++) |
| 175 |
|
|
xfm[y][x] *= expcomp; |
| 176 |
greg |
2.1 |
/* get input resolution */ |
| 177 |
|
|
if ((order = fgetresolu(&xmax, &ymax, stdin)) < 0) |
| 178 |
|
|
quiterr("bad picture format"); |
| 179 |
|
|
/* complete output header */ |
| 180 |
gregl |
2.4 |
if (ourexp < 0.99 || ourexp > 1.01) |
| 181 |
|
|
fputexpos(ourexp, stdout); |
| 182 |
greg |
2.1 |
if (rgbout) { |
| 183 |
|
|
fputprims(outprims, stdout); |
| 184 |
|
|
fputformat(COLRFMT, stdout); |
| 185 |
|
|
} else |
| 186 |
|
|
fputformat(CIEFMT, stdout); |
| 187 |
|
|
putc('\n', stdout); |
| 188 |
|
|
fputresolu(order, xmax, ymax, stdout); |
| 189 |
|
|
/* allocate scanline */ |
| 190 |
|
|
scanin = (COLOR *)malloc(xmax*sizeof(COLOR)); |
| 191 |
|
|
if (scanin == NULL) |
| 192 |
|
|
quiterr("out of memory in convert"); |
| 193 |
|
|
scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : NULL; |
| 194 |
|
|
/* convert image */ |
| 195 |
|
|
for (y = 0; y < ymax; y++) { |
| 196 |
|
|
if (freadscan(scanin, xmax, stdin) < 0) |
| 197 |
|
|
quiterr("error reading input picture"); |
| 198 |
greg |
2.3 |
for (x = 0; x < xmax; x++) { |
| 199 |
greg |
2.1 |
colortrans(scanin[x], xfm, scanin[x]); |
| 200 |
greg |
2.3 |
if (rgbout) |
| 201 |
|
|
clipgamut(scanin[x], bright(scanin[x]), |
| 202 |
|
|
CGAMUT_LOWER, cblack, cwhite); |
| 203 |
|
|
} |
| 204 |
greg |
2.1 |
if (scanout != NULL) { |
| 205 |
|
|
for (x = 0; x < xmax; x++) |
| 206 |
|
|
setcolr(scanout[x], colval(scanin[x],RED), |
| 207 |
|
|
colval(scanin[x],GRN), |
| 208 |
|
|
colval(scanin[x],BLU)); |
| 209 |
|
|
fwrite((char *)scanout, sizeof(COLR), xmax, stdout); |
| 210 |
|
|
} else |
| 211 |
|
|
fwritescan(scanin, xmax, stdout); |
| 212 |
|
|
if (ferror(stdout)) |
| 213 |
|
|
quiterr("error writing output picture"); |
| 214 |
|
|
} |
| 215 |
|
|
/* free scanline */ |
| 216 |
greg |
2.6 |
free((void *)scanin); |
| 217 |
greg |
2.1 |
if (scanout != NULL) |
| 218 |
greg |
2.6 |
free((void *)scanout); |
| 219 |
greg |
2.1 |
} |