ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_xyze.c
Revision: 2.13
Committed: Sat Dec 28 18:05:14 2019 UTC (4 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, rad5R3, HEAD
Changes since 2.12: +1 -4 lines
Log Message:
Removed redundant include files

File Contents

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