ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_xyze.c
Revision: 2.8
Committed: Fri Jan 2 12:47:01 2004 UTC (20 years, 3 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.7: +8 -4 lines
Log Message:
Fixed typing/prototype of getheader() and its callback.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: ra_xyze.c,v 2.7 2003/06/05 19:29:34 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
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 static gethfunc headline;
33
34
35 static int
36 headline( /* process header line */
37 char *s,
38 void *p
39 )
40 {
41 char fmt[32];
42
43 if (formatval(fmt, s)) { /* check if format string */
44 if (!strcmp(fmt,COLRFMT))
45 rgbinp = 1;
46 else if (!strcmp(fmt,CIEFMT))
47 rgbinp = 0;
48 else
49 rgbinp = -2;
50 return(0); /* don't echo */
51 }
52 if (isprims(s)) { /* get input primaries */
53 primsval(inprims, s);
54 return(0); /* don't echo */
55 }
56 /* should I grok colcorr also? */
57 return(fputs(s, stdout));
58 }
59
60
61 main(argc, argv)
62 int argc;
63 char *argv[];
64 {
65 int i;
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++)
72 if (argv[i][0] == '-')
73 switch (argv[i][1]) {
74 case 'c': /* rle-compressed output */
75 doflat = 0;
76 break;
77 case 'u': /* flat output */
78 doflat = 1;
79 break;
80 case 'r': /* RGBE output */
81 rgbout = 1;
82 break;
83 case 'p': /* RGB primaries */
84 if (i+8 >= argc)
85 goto userr;
86 outprims[RED][CIEX] = atof(argv[++i]);
87 outprims[RED][CIEY] = atof(argv[++i]);
88 outprims[GRN][CIEX] = atof(argv[++i]);
89 outprims[GRN][CIEY] = atof(argv[++i]);
90 outprims[BLU][CIEX] = atof(argv[++i]);
91 outprims[BLU][CIEY] = atof(argv[++i]);
92 outprims[WHT][CIEX] = atof(argv[++i]);
93 outprims[WHT][CIEY] = atof(argv[++i]);
94 break;
95 case 'e': /* exposure compensation */
96 expcomp = atof(argv[++i]);
97 if (argv[i][0] == '+' || argv[i][0] == '-')
98 expcomp = pow(2., expcomp);
99 break;
100 default:
101 goto userr;
102 }
103 else
104 break;
105
106 if (doflat < 0)
107 doflat = !rgbout;
108 if (i < argc-2)
109 goto userr;
110 if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
111 fprintf(stderr, "%s: can't open input \"%s\"\n",
112 progname, argv[i]);
113 exit(1);
114 }
115 if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
116 fprintf(stderr, "%s: can't open output \"%s\"\n",
117 progname, argv[i+1]);
118 exit(1);
119 }
120 getheader(stdin, headline, NULL);
121 if (rgbinp == -2)
122 quiterr("unrecognized input file format");
123 if (rgbinp == -1)
124 rgbinp = !rgbout;
125 printargs(argc, argv, stdout); /* add to header */
126 convert(); /* convert picture */
127 exit(0);
128 userr:
129 fprintf(stderr, "Usage: %s [-r][-e exp][-c|-u]", progname);
130 fprintf(stderr, "[-p rx ry gx gy bx by wx wy] [input [output]]\n");
131 exit(1);
132 }
133
134
135 quiterr(err) /* print message and exit */
136 char *err;
137 {
138 if (err != NULL) {
139 fprintf(stderr, "%s: %s\n", progname, err);
140 exit(1);
141 }
142 exit(0);
143 }
144
145
146 convert() /* convert to XYZE or RGBE picture */
147 {
148 int order;
149 int xmax, ymax;
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) {
158 if (rgbinp) { /* RGBE -> RGBE */
159 comprgb2rgbWBmat(xfm, inprims, outprims);
160 } else { /* XYZE -> RGBE */
161 compxyz2rgbWBmat(xfm, outprims);
162 ourexp *= WHTEFFICACY;
163 }
164 } else {
165 if (rgbinp) { /* RGBE -> XYZE */
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 ? 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);
186 } else
187 fputformat(CIEFMT, stdout);
188 putc('\n', stdout);
189 fputresolu(order, xmax, ymax, stdout);
190 /* allocate scanline */
191 scanin = (COLOR *)malloc(xmax*sizeof(COLOR));
192 if (scanin == NULL)
193 quiterr("out of memory in convert");
194 scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : NULL;
195 /* convert image */
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++) {
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),
208 colval(scanin[x],GRN),
209 colval(scanin[x],BLU));
210 fwrite((char *)scanout, sizeof(COLR), xmax, stdout);
211 } else
212 fwritescan(scanin, xmax, stdout);
213 if (ferror(stdout))
214 quiterr("error writing output picture");
215 }
216 /* free scanline */
217 free((void *)scanin);
218 if (scanout != NULL)
219 free((void *)scanout);
220 }