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, 8 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

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.10 static const char RCSid[] = "$Id: ra_xyze.c,v 2.9 2004/03/28 20:33:14 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 greg 2.10 double origexp = -1.0; /* original exposure */
25 greg 2.1 char *progname;
26    
27 schorsch 2.8 static gethfunc headline;
28 schorsch 2.9 static void quiterr(char *err);
29     static void convert(void);
30    
31 greg 2.1
32 schorsch 2.8
33     static int
34     headline( /* process header line */
35     char *s,
36     void *p
37     )
38 greg 2.1 {
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 gwlarson 2.5 return(0); /* don't echo */
49 greg 2.1 }
50 greg 2.10 if (origexp > 0.0 && isexpos(s)) {
51     origexp *= exposval(s);
52     return(0); /* don't echo */
53     }
54 greg 2.1 if (isprims(s)) { /* get input primaries */
55     primsval(inprims, s);
56 gwlarson 2.5 return(0); /* don't echo */
57 greg 2.1 }
58     /* should I grok colcorr also? */
59 gwlarson 2.5 return(fputs(s, stdout));
60 greg 2.1 }
61    
62    
63 schorsch 2.9 int
64     main(int argc, char *argv[])
65 greg 2.1 {
66     int i;
67 schorsch 2.7 SET_DEFAULT_BINARY();
68     SET_FILE_BINARY(stdin);
69     SET_FILE_BINARY(stdout);
70 greg 2.1 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 greg 2.2 if (i+8 >= argc)
86 greg 2.1 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 greg 2.10 case 'o': /* original exposure */
97     origexp = 1.0;
98     break;
99 greg 2.1 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 greg 2.10 fprintf(stderr, "Usage: %s [-r][-o][-e exp][-c|-u]", progname);
134 greg 2.1 fprintf(stderr, "[-p rx ry gx gy bx by wx wy] [input [output]]\n");
135     exit(1);
136     }
137    
138    
139 schorsch 2.9 static void
140     quiterr( /* print message and exit */
141     char *err
142     )
143 greg 2.1 {
144     if (err != NULL) {
145     fprintf(stderr, "%s: %s\n", progname, err);
146     exit(1);
147     }
148     exit(0);
149     }
150    
151    
152 schorsch 2.9 static void
153     convert(void) /* convert to XYZE or RGBE picture */
154 greg 2.1 {
155     int order;
156     int xmax, ymax;
157     COLORMAT xfm;
158     register COLOR *scanin;
159     register COLR *scanout;
160 greg 2.10 double exp2do = expcomp;
161     double exp2report = expcomp;
162 greg 2.1 int y;
163     register int x;
164 greg 2.10 /* recover original? */
165     if (origexp > 0.0)
166     exp2do /= origexp;
167 greg 2.1 /* compute transform */
168     if (rgbout) {
169     if (rgbinp) { /* RGBE -> RGBE */
170 greg 2.6 comprgb2rgbWBmat(xfm, inprims, outprims);
171 greg 2.1 } else { /* XYZE -> RGBE */
172 greg 2.6 compxyz2rgbWBmat(xfm, outprims);
173 greg 2.10 if (origexp > 0.0)
174     exp2do /= WHTEFFICACY;
175     else
176     exp2report *= WHTEFFICACY;
177 greg 2.1 }
178     } else {
179     if (rgbinp) { /* RGBE -> XYZE */
180 greg 2.6 comprgb2xyzWBmat(xfm, inprims);
181 greg 2.10 if (origexp > 0.0)
182     exp2do *= WHTEFFICACY;
183     else
184     exp2report /= WHTEFFICACY;
185 greg 2.1 } else { /* XYZE -> XYZE */
186     for (y = 0; y < 3; y++)
187     for (x = 0; x < 3; x++)
188 gregl 2.4 xfm[y][x] = x==y ? 1. : 0.;
189 greg 2.1 }
190     }
191 gregl 2.4 for (y = 0; y < 3; y++)
192     for (x = 0; x < 3; x++)
193 greg 2.10 xfm[y][x] *= exp2do;
194 greg 2.1 /* get input resolution */
195     if ((order = fgetresolu(&xmax, &ymax, stdin)) < 0)
196     quiterr("bad picture format");
197     /* complete output header */
198 greg 2.10 if ((exp2report < 0.99) | (exp2report > 1.01))
199     fputexpos(exp2report, stdout);
200 greg 2.1 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 greg 2.3 for (x = 0; x < xmax; x++) {
217 greg 2.1 colortrans(scanin[x], xfm, scanin[x]);
218 greg 2.3 if (rgbout)
219     clipgamut(scanin[x], bright(scanin[x]),
220     CGAMUT_LOWER, cblack, cwhite);
221     }
222 greg 2.1 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 greg 2.6 free((void *)scanin);
235 greg 2.1 if (scanout != NULL)
236 greg 2.6 free((void *)scanout);
237 greg 2.1 }