ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_xyze.c
Revision: 2.5
Committed: Tue Oct 27 09:08:27 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 2.4: +4 -3 lines
Log Message:
changed getheader() to listen to return value of passed function

File Contents

# Content
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #endif
6
7 /*
8 * Program to convert between RADIANCE RGBE and XYZE formats
9 */
10
11 #include <stdio.h>
12 #include <math.h>
13 #include "color.h"
14 #include "resolu.h"
15
16 #ifdef MSDOS
17 #include <fcntl.h>
18 #endif
19
20 extern char *malloc(), *strcpy();
21
22 int rgbinp = -1; /* input is RGBE? */
23
24 int rgbout = 0; /* output should be RGBE? */
25
26 RGBPRIMS inprims = STDPRIMS; /* input primaries */
27
28 RGBPRIMS outprims = STDPRIMS; /* output primaries */
29
30 double expcomp = 1.0; /* exposure compensation */
31
32 int doflat = -1; /* produce flat file? */
33
34 char *progname;
35
36
37 int
38 headline(s) /* process header line */
39 char *s;
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 #ifdef MSDOS
67 extern int _fmode;
68 _fmode = O_BINARY;
69 setmode(fileno(stdin), O_BINARY);
70 setmode(fileno(stdout), O_BINARY);
71 #endif
72 progname = argv[0];
73
74 for (i = 1; i < argc; i++)
75 if (argv[i][0] == '-')
76 switch (argv[i][1]) {
77 case 'c': /* rle-compressed output */
78 doflat = 0;
79 break;
80 case 'u': /* flat output */
81 doflat = 1;
82 break;
83 case 'r': /* RGBE output */
84 rgbout = 1;
85 break;
86 case 'p': /* RGB primaries */
87 if (i+8 >= argc)
88 goto userr;
89 outprims[RED][CIEX] = atof(argv[++i]);
90 outprims[RED][CIEY] = atof(argv[++i]);
91 outprims[GRN][CIEX] = atof(argv[++i]);
92 outprims[GRN][CIEY] = atof(argv[++i]);
93 outprims[BLU][CIEX] = atof(argv[++i]);
94 outprims[BLU][CIEY] = atof(argv[++i]);
95 outprims[WHT][CIEX] = atof(argv[++i]);
96 outprims[WHT][CIEY] = atof(argv[++i]);
97 break;
98 case 'e': /* exposure compensation */
99 expcomp = atof(argv[++i]);
100 if (argv[i][0] == '+' || argv[i][0] == '-')
101 expcomp = pow(2., expcomp);
102 break;
103 default:
104 goto userr;
105 }
106 else
107 break;
108
109 if (doflat < 0)
110 doflat = !rgbout;
111 if (i < argc-2)
112 goto userr;
113 if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
114 fprintf(stderr, "%s: can't open input \"%s\"\n",
115 progname, argv[i]);
116 exit(1);
117 }
118 if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
119 fprintf(stderr, "%s: can't open output \"%s\"\n",
120 progname, argv[i+1]);
121 exit(1);
122 }
123 getheader(stdin, headline, NULL);
124 if (rgbinp == -2)
125 quiterr("unrecognized input file format");
126 if (rgbinp == -1)
127 rgbinp = !rgbout;
128 printargs(argc, argv, stdout); /* add to header */
129 convert(); /* convert picture */
130 exit(0);
131 userr:
132 fprintf(stderr, "Usage: %s [-r][-e exp][-c|-u]", progname);
133 fprintf(stderr, "[-p rx ry gx gy bx by wx wy] [input [output]]\n");
134 exit(1);
135 }
136
137
138 quiterr(err) /* print message and exit */
139 char *err;
140 {
141 if (err != NULL) {
142 fprintf(stderr, "%s: %s\n", progname, err);
143 exit(1);
144 }
145 exit(0);
146 }
147
148
149 convert() /* convert to XYZE or RGBE picture */
150 {
151 int order;
152 int xmax, ymax;
153 COLORMAT xfm;
154 register COLOR *scanin;
155 register COLR *scanout;
156 double ourexp = expcomp;
157 int y;
158 register int x;
159 /* compute transform */
160 if (rgbout) {
161 if (rgbinp) { /* RGBE -> RGBE */
162 comprgb2rgbmat(xfm, inprims, outprims);
163 } else { /* XYZE -> RGBE */
164 compxyz2rgbmat(xfm, outprims);
165 ourexp *= WHTEFFICACY;
166 }
167 } else {
168 if (rgbinp) { /* RGBE -> XYZE */
169 comprgb2xyzmat(xfm, inprims);
170 ourexp /= WHTEFFICACY;
171 } else { /* XYZE -> XYZE */
172 for (y = 0; y < 3; y++)
173 for (x = 0; x < 3; x++)
174 xfm[y][x] = x==y ? 1. : 0.;
175 }
176 }
177 for (y = 0; y < 3; y++)
178 for (x = 0; x < 3; x++)
179 xfm[y][x] *= expcomp;
180 /* get input resolution */
181 if ((order = fgetresolu(&xmax, &ymax, stdin)) < 0)
182 quiterr("bad picture format");
183 /* complete output header */
184 if (ourexp < 0.99 || ourexp > 1.01)
185 fputexpos(ourexp, stdout);
186 if (rgbout) {
187 fputprims(outprims, stdout);
188 fputformat(COLRFMT, stdout);
189 } else
190 fputformat(CIEFMT, stdout);
191 putc('\n', stdout);
192 fputresolu(order, xmax, ymax, stdout);
193 /* allocate scanline */
194 scanin = (COLOR *)malloc(xmax*sizeof(COLOR));
195 if (scanin == NULL)
196 quiterr("out of memory in convert");
197 scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : NULL;
198 /* convert image */
199 for (y = 0; y < ymax; y++) {
200 if (freadscan(scanin, xmax, stdin) < 0)
201 quiterr("error reading input picture");
202 for (x = 0; x < xmax; x++) {
203 colortrans(scanin[x], xfm, scanin[x]);
204 if (rgbout)
205 clipgamut(scanin[x], bright(scanin[x]),
206 CGAMUT_LOWER, cblack, cwhite);
207 }
208 if (scanout != NULL) {
209 for (x = 0; x < xmax; x++)
210 setcolr(scanout[x], colval(scanin[x],RED),
211 colval(scanin[x],GRN),
212 colval(scanin[x],BLU));
213 fwrite((char *)scanout, sizeof(COLR), xmax, stdout);
214 } else
215 fwritescan(scanin, xmax, stdout);
216 if (ferror(stdout))
217 quiterr("error writing output picture");
218 }
219 /* free scanline */
220 free((char *)scanin);
221 if (scanout != NULL)
222 free((char *)scanout);
223 }