1 |
– |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Program to convert between RADIANCE RGBE and XYZE formats |
6 |
+ |
* Added white-balance adjustment 10/01 (GW). |
7 |
|
*/ |
8 |
|
|
9 |
|
#include <stdio.h> |
10 |
+ |
#include <string.h> |
11 |
|
#include <math.h> |
12 |
+ |
#include <time.h> |
13 |
+ |
|
14 |
+ |
#include "platform.h" |
15 |
|
#include "color.h" |
16 |
|
#include "resolu.h" |
17 |
|
|
16 |
– |
#ifdef MSDOS |
17 |
– |
#include <fcntl.h> |
18 |
– |
#endif |
19 |
– |
|
20 |
– |
extern char *malloc(), *strcpy(); |
21 |
– |
|
18 |
|
int rgbinp = -1; /* input is RGBE? */ |
19 |
|
|
20 |
|
int rgbout = 0; /* output should be RGBE? */ |
29 |
|
|
30 |
|
char *progname; |
31 |
|
|
32 |
+ |
static gethfunc headline; |
33 |
|
|
34 |
< |
headline(s) /* process header line */ |
35 |
< |
char *s; |
34 |
> |
|
35 |
> |
static int |
36 |
> |
headline( /* process header line */ |
37 |
> |
char *s, |
38 |
> |
void *p |
39 |
> |
) |
40 |
|
{ |
41 |
|
char fmt[32]; |
42 |
|
|
47 |
|
rgbinp = 0; |
48 |
|
else |
49 |
|
rgbinp = -2; |
50 |
< |
return; /* don't echo */ |
50 |
> |
return(0); /* don't echo */ |
51 |
|
} |
52 |
|
if (isprims(s)) { /* get input primaries */ |
53 |
|
primsval(inprims, s); |
54 |
< |
return; /* don't echo */ |
54 |
> |
return(0); /* don't echo */ |
55 |
|
} |
56 |
|
/* should I grok colcorr also? */ |
57 |
< |
fputs(s, stdout); |
57 |
> |
return(fputs(s, stdout)); |
58 |
|
} |
59 |
|
|
60 |
|
|
63 |
|
char *argv[]; |
64 |
|
{ |
65 |
|
int i; |
66 |
< |
#ifdef MSDOS |
67 |
< |
extern int _fmode; |
68 |
< |
_fmode = O_BINARY; |
68 |
< |
setmode(fileno(stdin), O_BINARY); |
69 |
< |
setmode(fileno(stdout), O_BINARY); |
70 |
< |
#endif |
66 |
> |
SET_DEFAULT_BINARY(); |
67 |
> |
SET_FILE_BINARY(stdin); |
68 |
> |
SET_FILE_BINARY(stdout); |
69 |
|
progname = argv[0]; |
70 |
|
|
71 |
|
for (i = 1; i < argc; i++) |
81 |
|
rgbout = 1; |
82 |
|
break; |
83 |
|
case 'p': /* RGB primaries */ |
84 |
< |
if (i+9 >= argc) |
84 |
> |
if (i+8 >= argc) |
85 |
|
goto userr; |
86 |
|
outprims[RED][CIEX] = atof(argv[++i]); |
87 |
|
outprims[RED][CIEY] = atof(argv[++i]); |
123 |
|
if (rgbinp == -1) |
124 |
|
rgbinp = !rgbout; |
125 |
|
printargs(argc, argv, stdout); /* add to header */ |
128 |
– |
if (expcomp < 0.99 || expcomp > 1.01) |
129 |
– |
fputexpos(expcomp, stdout); |
126 |
|
convert(); /* convert picture */ |
127 |
|
exit(0); |
128 |
|
userr: |
150 |
|
COLORMAT xfm; |
151 |
|
register COLOR *scanin; |
152 |
|
register COLR *scanout; |
153 |
+ |
double ourexp = expcomp; |
154 |
|
int y; |
155 |
|
register int x; |
156 |
|
/* compute transform */ |
157 |
|
if (rgbout) { |
161 |
– |
double mult; |
158 |
|
if (rgbinp) { /* RGBE -> RGBE */ |
159 |
< |
comprgb2rgbmat(xfm, inprims, outprims); |
164 |
< |
mult = expcomp; |
159 |
> |
comprgb2rgbWBmat(xfm, inprims, outprims); |
160 |
|
} else { /* XYZE -> RGBE */ |
161 |
< |
compxyz2rgbmat(xfm, outprims); |
162 |
< |
mult = expcomp/WHTEFFICACY; |
161 |
> |
compxyz2rgbWBmat(xfm, outprims); |
162 |
> |
ourexp *= WHTEFFICACY; |
163 |
|
} |
169 |
– |
for (y = 0; y < 3; y++) |
170 |
– |
for (x = 0; x < 3; x++) |
171 |
– |
xfm[y][x] *= mult; |
164 |
|
} else { |
165 |
|
if (rgbinp) { /* RGBE -> XYZE */ |
166 |
< |
comprgb2xyzmat(xfm, inprims); |
167 |
< |
for (y = 0; y < 3; y++) |
176 |
< |
for (x = 0; x < 3; x++) |
177 |
< |
xfm[y][x] *= WHTEFFICACY*expcomp; |
166 |
> |
comprgb2xyzWBmat(xfm, inprims); |
167 |
> |
ourexp /= WHTEFFICACY; |
168 |
|
} else { /* XYZE -> XYZE */ |
169 |
|
for (y = 0; y < 3; y++) |
170 |
|
for (x = 0; x < 3; x++) |
171 |
< |
xfm[y][x] = x==y ? expcomp : 0.; |
171 |
> |
xfm[y][x] = x==y ? 1. : 0.; |
172 |
|
} |
173 |
|
} |
174 |
+ |
for (y = 0; y < 3; y++) |
175 |
+ |
for (x = 0; x < 3; x++) |
176 |
+ |
xfm[y][x] *= expcomp; |
177 |
|
/* get input resolution */ |
178 |
|
if ((order = fgetresolu(&xmax, &ymax, stdin)) < 0) |
179 |
|
quiterr("bad picture format"); |
180 |
|
/* complete output header */ |
181 |
+ |
if (ourexp < 0.99 || ourexp > 1.01) |
182 |
+ |
fputexpos(ourexp, stdout); |
183 |
|
if (rgbout) { |
184 |
|
fputprims(outprims, stdout); |
185 |
|
fputformat(COLRFMT, stdout); |
196 |
|
for (y = 0; y < ymax; y++) { |
197 |
|
if (freadscan(scanin, xmax, stdin) < 0) |
198 |
|
quiterr("error reading input picture"); |
199 |
< |
for (x = 0; x < xmax; x++) |
199 |
> |
for (x = 0; x < xmax; x++) { |
200 |
|
colortrans(scanin[x], xfm, scanin[x]); |
201 |
+ |
if (rgbout) |
202 |
+ |
clipgamut(scanin[x], bright(scanin[x]), |
203 |
+ |
CGAMUT_LOWER, cblack, cwhite); |
204 |
+ |
} |
205 |
|
if (scanout != NULL) { |
206 |
|
for (x = 0; x < xmax; x++) |
207 |
|
setcolr(scanout[x], colval(scanin[x],RED), |
214 |
|
quiterr("error writing output picture"); |
215 |
|
} |
216 |
|
/* free scanline */ |
217 |
< |
free((char *)scanin); |
217 |
> |
free((void *)scanin); |
218 |
|
if (scanout != NULL) |
219 |
< |
free((char *)scanout); |
219 |
> |
free((void *)scanout); |
220 |
|
} |