ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_xyze.c
Revision: 2.6
Committed: Sat Feb 22 02:07:28 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.5: +9 -11 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.6 static const char RCSid[] = "$Id$";
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 greg 2.1 #include "color.h"
14     #include "resolu.h"
15    
16     #ifdef MSDOS
17     #include <fcntl.h>
18     #endif
19    
20     int rgbinp = -1; /* input is RGBE? */
21    
22     int rgbout = 0; /* output should be RGBE? */
23    
24     RGBPRIMS inprims = STDPRIMS; /* input primaries */
25    
26     RGBPRIMS outprims = STDPRIMS; /* output primaries */
27    
28     double expcomp = 1.0; /* exposure compensation */
29    
30     int doflat = -1; /* produce flat file? */
31    
32     char *progname;
33    
34    
35 gwlarson 2.5 int
36 greg 2.1 headline(s) /* process header line */
37     char *s;
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 gwlarson 2.5 return(0); /* don't echo */
49 greg 2.1 }
50     if (isprims(s)) { /* get input primaries */
51     primsval(inprims, s);
52 gwlarson 2.5 return(0); /* don't echo */
53 greg 2.1 }
54     /* should I grok colcorr also? */
55 gwlarson 2.5 return(fputs(s, stdout));
56 greg 2.1 }
57    
58    
59     main(argc, argv)
60     int argc;
61     char *argv[];
62     {
63     int i;
64     #ifdef MSDOS
65     extern int _fmode;
66     _fmode = O_BINARY;
67     setmode(fileno(stdin), O_BINARY);
68     setmode(fileno(stdout), O_BINARY);
69     #endif
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 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     case 'e': /* exposure compensation */
97     expcomp = atof(argv[++i]);
98     if (argv[i][0] == '+' || argv[i][0] == '-')
99     expcomp = pow(2., expcomp);
100     break;
101     default:
102     goto userr;
103     }
104     else
105     break;
106    
107     if (doflat < 0)
108     doflat = !rgbout;
109     if (i < argc-2)
110     goto userr;
111     if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
112     fprintf(stderr, "%s: can't open input \"%s\"\n",
113     progname, argv[i]);
114     exit(1);
115     }
116     if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
117     fprintf(stderr, "%s: can't open output \"%s\"\n",
118     progname, argv[i+1]);
119     exit(1);
120     }
121     getheader(stdin, headline, NULL);
122     if (rgbinp == -2)
123     quiterr("unrecognized input file format");
124     if (rgbinp == -1)
125     rgbinp = !rgbout;
126     printargs(argc, argv, stdout); /* add to header */
127     convert(); /* convert picture */
128     exit(0);
129     userr:
130     fprintf(stderr, "Usage: %s [-r][-e exp][-c|-u]", progname);
131     fprintf(stderr, "[-p rx ry gx gy bx by wx wy] [input [output]]\n");
132     exit(1);
133     }
134    
135    
136     quiterr(err) /* print message and exit */
137     char *err;
138     {
139     if (err != NULL) {
140     fprintf(stderr, "%s: %s\n", progname, err);
141     exit(1);
142     }
143     exit(0);
144     }
145    
146    
147     convert() /* convert to XYZE or RGBE picture */
148     {
149     int order;
150     int xmax, ymax;
151     COLORMAT xfm;
152     register COLOR *scanin;
153     register COLR *scanout;
154 gregl 2.4 double ourexp = expcomp;
155 greg 2.1 int y;
156     register int x;
157     /* compute transform */
158     if (rgbout) {
159     if (rgbinp) { /* RGBE -> RGBE */
160 greg 2.6 comprgb2rgbWBmat(xfm, inprims, outprims);
161 greg 2.1 } else { /* XYZE -> RGBE */
162 greg 2.6 compxyz2rgbWBmat(xfm, outprims);
163 gregl 2.4 ourexp *= WHTEFFICACY;
164 greg 2.1 }
165     } else {
166     if (rgbinp) { /* RGBE -> XYZE */
167 greg 2.6 comprgb2xyzWBmat(xfm, inprims);
168 gregl 2.4 ourexp /= WHTEFFICACY;
169 greg 2.1 } else { /* XYZE -> XYZE */
170     for (y = 0; y < 3; y++)
171     for (x = 0; x < 3; x++)
172 gregl 2.4 xfm[y][x] = x==y ? 1. : 0.;
173 greg 2.1 }
174     }
175 gregl 2.4 for (y = 0; y < 3; y++)
176     for (x = 0; x < 3; x++)
177     xfm[y][x] *= expcomp;
178 greg 2.1 /* get input resolution */
179     if ((order = fgetresolu(&xmax, &ymax, stdin)) < 0)
180     quiterr("bad picture format");
181     /* complete output header */
182 gregl 2.4 if (ourexp < 0.99 || ourexp > 1.01)
183     fputexpos(ourexp, stdout);
184 greg 2.1 if (rgbout) {
185     fputprims(outprims, stdout);
186     fputformat(COLRFMT, stdout);
187     } else
188     fputformat(CIEFMT, stdout);
189     putc('\n', stdout);
190     fputresolu(order, xmax, ymax, stdout);
191     /* allocate scanline */
192     scanin = (COLOR *)malloc(xmax*sizeof(COLOR));
193     if (scanin == NULL)
194     quiterr("out of memory in convert");
195     scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : NULL;
196     /* convert image */
197     for (y = 0; y < ymax; y++) {
198     if (freadscan(scanin, xmax, stdin) < 0)
199     quiterr("error reading input picture");
200 greg 2.3 for (x = 0; x < xmax; x++) {
201 greg 2.1 colortrans(scanin[x], xfm, scanin[x]);
202 greg 2.3 if (rgbout)
203     clipgamut(scanin[x], bright(scanin[x]),
204     CGAMUT_LOWER, cblack, cwhite);
205     }
206 greg 2.1 if (scanout != NULL) {
207     for (x = 0; x < xmax; x++)
208     setcolr(scanout[x], colval(scanin[x],RED),
209     colval(scanin[x],GRN),
210     colval(scanin[x],BLU));
211     fwrite((char *)scanout, sizeof(COLR), xmax, stdout);
212     } else
213     fwritescan(scanin, xmax, stdout);
214     if (ferror(stdout))
215     quiterr("error writing output picture");
216     }
217     /* free scanline */
218 greg 2.6 free((void *)scanin);
219 greg 2.1 if (scanout != NULL)
220 greg 2.6 free((void *)scanout);
221 greg 2.1 }