ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_xyze.c
Revision: 2.7
Committed: Thu Jun 5 19:29:34 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.6: +6 -11 lines
Log Message:
Macros for setting binary file mode. Replacing MSDOS by _WIN32.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: ra_xyze.c,v 2.6 2003/02/22 02:07:28 greg 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
20 int rgbout = 0; /* output should be RGBE? */
21
22 RGBPRIMS inprims = STDPRIMS; /* input primaries */
23
24 RGBPRIMS outprims = STDPRIMS; /* output primaries */
25
26 double expcomp = 1.0; /* exposure compensation */
27
28 int doflat = -1; /* produce flat file? */
29
30 char *progname;
31
32
33 int
34 headline(s) /* process header line */
35 char *s;
36 {
37 char fmt[32];
38
39 if (formatval(fmt, s)) { /* check if format string */
40 if (!strcmp(fmt,COLRFMT))
41 rgbinp = 1;
42 else if (!strcmp(fmt,CIEFMT))
43 rgbinp = 0;
44 else
45 rgbinp = -2;
46 return(0); /* don't echo */
47 }
48 if (isprims(s)) { /* get input primaries */
49 primsval(inprims, s);
50 return(0); /* don't echo */
51 }
52 /* should I grok colcorr also? */
53 return(fputs(s, stdout));
54 }
55
56
57 main(argc, argv)
58 int argc;
59 char *argv[];
60 {
61 int i;
62 SET_DEFAULT_BINARY();
63 SET_FILE_BINARY(stdin);
64 SET_FILE_BINARY(stdout);
65 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 if (i+8 >= argc)
81 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 quiterr(err) /* print message and exit */
132 char *err;
133 {
134 if (err != NULL) {
135 fprintf(stderr, "%s: %s\n", progname, err);
136 exit(1);
137 }
138 exit(0);
139 }
140
141
142 convert() /* convert to XYZE or RGBE picture */
143 {
144 int order;
145 int xmax, ymax;
146 COLORMAT xfm;
147 register COLOR *scanin;
148 register COLR *scanout;
149 double ourexp = expcomp;
150 int y;
151 register int x;
152 /* compute transform */
153 if (rgbout) {
154 if (rgbinp) { /* RGBE -> RGBE */
155 comprgb2rgbWBmat(xfm, inprims, outprims);
156 } else { /* XYZE -> RGBE */
157 compxyz2rgbWBmat(xfm, outprims);
158 ourexp *= WHTEFFICACY;
159 }
160 } else {
161 if (rgbinp) { /* RGBE -> XYZE */
162 comprgb2xyzWBmat(xfm, inprims);
163 ourexp /= WHTEFFICACY;
164 } else { /* XYZE -> XYZE */
165 for (y = 0; y < 3; y++)
166 for (x = 0; x < 3; x++)
167 xfm[y][x] = x==y ? 1. : 0.;
168 }
169 }
170 for (y = 0; y < 3; y++)
171 for (x = 0; x < 3; x++)
172 xfm[y][x] *= expcomp;
173 /* get input resolution */
174 if ((order = fgetresolu(&xmax, &ymax, stdin)) < 0)
175 quiterr("bad picture format");
176 /* complete output header */
177 if (ourexp < 0.99 || ourexp > 1.01)
178 fputexpos(ourexp, stdout);
179 if (rgbout) {
180 fputprims(outprims, stdout);
181 fputformat(COLRFMT, stdout);
182 } else
183 fputformat(CIEFMT, stdout);
184 putc('\n', stdout);
185 fputresolu(order, xmax, ymax, stdout);
186 /* allocate scanline */
187 scanin = (COLOR *)malloc(xmax*sizeof(COLOR));
188 if (scanin == NULL)
189 quiterr("out of memory in convert");
190 scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : NULL;
191 /* convert image */
192 for (y = 0; y < ymax; y++) {
193 if (freadscan(scanin, xmax, stdin) < 0)
194 quiterr("error reading input picture");
195 for (x = 0; x < xmax; x++) {
196 colortrans(scanin[x], xfm, scanin[x]);
197 if (rgbout)
198 clipgamut(scanin[x], bright(scanin[x]),
199 CGAMUT_LOWER, cblack, cwhite);
200 }
201 if (scanout != NULL) {
202 for (x = 0; x < xmax; x++)
203 setcolr(scanout[x], colval(scanin[x],RED),
204 colval(scanin[x],GRN),
205 colval(scanin[x],BLU));
206 fwrite((char *)scanout, sizeof(COLR), xmax, stdout);
207 } else
208 fwritescan(scanin, xmax, stdout);
209 if (ferror(stdout))
210 quiterr("error writing output picture");
211 }
212 /* free scanline */
213 free((void *)scanin);
214 if (scanout != NULL)
215 free((void *)scanout);
216 }