ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_xyze.c
Revision: 2.10
Committed: Tue Aug 22 21:38:22 2006 UTC (17 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad4R2, rad4R1, rad4R0, rad3R8, rad3R9, rad4R2P1
Changes since 2.9: +26 -8 lines
Log Message:
Added -o option to ra_xyze for original pixel values.

File Contents

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