ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_xyze.c
Revision: 2.4
Committed: Tue Jul 22 10:17:53 1997 UTC (26 years, 9 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 2.3: +11 -14 lines
Log Message:
made it so EXPOSURE is changed rather than actual pixel values

File Contents

# User Rev Content
1 gregl 2.4 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2 greg 2.1
3     #ifndef lint
4 gregl 2.4 static char SCCSid[] = "$SunId$ SGI";
5 greg 2.1 #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     headline(s) /* process header line */
38     char *s;
39     {
40     char fmt[32];
41    
42     if (formatval(fmt, s)) { /* check if format string */
43     if (!strcmp(fmt,COLRFMT))
44     rgbinp = 1;
45     else if (!strcmp(fmt,CIEFMT))
46     rgbinp = 0;
47     else
48     rgbinp = -2;
49     return; /* don't echo */
50     }
51     if (isprims(s)) { /* get input primaries */
52     primsval(inprims, s);
53     return; /* don't echo */
54     }
55     /* should I grok colcorr also? */
56     fputs(s, stdout);
57     }
58    
59    
60     main(argc, argv)
61     int argc;
62     char *argv[];
63     {
64     int i;
65     #ifdef MSDOS
66     extern int _fmode;
67     _fmode = O_BINARY;
68     setmode(fileno(stdin), O_BINARY);
69     setmode(fileno(stdout), O_BINARY);
70     #endif
71     progname = argv[0];
72    
73     for (i = 1; i < argc; i++)
74     if (argv[i][0] == '-')
75     switch (argv[i][1]) {
76     case 'c': /* rle-compressed output */
77     doflat = 0;
78     break;
79     case 'u': /* flat output */
80     doflat = 1;
81     break;
82     case 'r': /* RGBE output */
83     rgbout = 1;
84     break;
85     case 'p': /* RGB primaries */
86 greg 2.2 if (i+8 >= argc)
87 greg 2.1 goto userr;
88     outprims[RED][CIEX] = atof(argv[++i]);
89     outprims[RED][CIEY] = atof(argv[++i]);
90     outprims[GRN][CIEX] = atof(argv[++i]);
91     outprims[GRN][CIEY] = atof(argv[++i]);
92     outprims[BLU][CIEX] = atof(argv[++i]);
93     outprims[BLU][CIEY] = atof(argv[++i]);
94     outprims[WHT][CIEX] = atof(argv[++i]);
95     outprims[WHT][CIEY] = atof(argv[++i]);
96     break;
97     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     fprintf(stderr, "Usage: %s [-r][-e exp][-c|-u]", progname);
132     fprintf(stderr, "[-p rx ry gx gy bx by wx wy] [input [output]]\n");
133     exit(1);
134     }
135    
136    
137     quiterr(err) /* print message and exit */
138     char *err;
139     {
140     if (err != NULL) {
141     fprintf(stderr, "%s: %s\n", progname, err);
142     exit(1);
143     }
144     exit(0);
145     }
146    
147    
148     convert() /* convert to XYZE or RGBE picture */
149     {
150     int order;
151     int xmax, ymax;
152     COLORMAT xfm;
153     register COLOR *scanin;
154     register COLR *scanout;
155 gregl 2.4 double ourexp = expcomp;
156 greg 2.1 int y;
157     register int x;
158     /* compute transform */
159     if (rgbout) {
160     if (rgbinp) { /* RGBE -> RGBE */
161     comprgb2rgbmat(xfm, inprims, outprims);
162     } else { /* XYZE -> RGBE */
163     compxyz2rgbmat(xfm, outprims);
164 gregl 2.4 ourexp *= WHTEFFICACY;
165 greg 2.1 }
166     } else {
167     if (rgbinp) { /* RGBE -> XYZE */
168     comprgb2xyzmat(xfm, inprims);
169 gregl 2.4 ourexp /= WHTEFFICACY;
170 greg 2.1 } else { /* XYZE -> XYZE */
171     for (y = 0; y < 3; y++)
172     for (x = 0; x < 3; x++)
173 gregl 2.4 xfm[y][x] = x==y ? 1. : 0.;
174 greg 2.1 }
175     }
176 gregl 2.4 for (y = 0; y < 3; y++)
177     for (x = 0; x < 3; x++)
178     xfm[y][x] *= expcomp;
179 greg 2.1 /* get input resolution */
180     if ((order = fgetresolu(&xmax, &ymax, stdin)) < 0)
181     quiterr("bad picture format");
182     /* complete output header */
183 gregl 2.4 if (ourexp < 0.99 || ourexp > 1.01)
184     fputexpos(ourexp, stdout);
185 greg 2.1 if (rgbout) {
186     fputprims(outprims, stdout);
187     fputformat(COLRFMT, stdout);
188     } else
189     fputformat(CIEFMT, stdout);
190     putc('\n', stdout);
191     fputresolu(order, xmax, ymax, stdout);
192     /* allocate scanline */
193     scanin = (COLOR *)malloc(xmax*sizeof(COLOR));
194     if (scanin == NULL)
195     quiterr("out of memory in convert");
196     scanout = doflat ? (COLR *)malloc(xmax*sizeof(COLR)) : NULL;
197     /* convert image */
198     for (y = 0; y < ymax; y++) {
199     if (freadscan(scanin, xmax, stdin) < 0)
200     quiterr("error reading input picture");
201 greg 2.3 for (x = 0; x < xmax; x++) {
202 greg 2.1 colortrans(scanin[x], xfm, scanin[x]);
203 greg 2.3 if (rgbout)
204     clipgamut(scanin[x], bright(scanin[x]),
205     CGAMUT_LOWER, cblack, cwhite);
206     }
207 greg 2.1 if (scanout != NULL) {
208     for (x = 0; x < xmax; x++)
209     setcolr(scanout[x], colval(scanin[x],RED),
210     colval(scanin[x],GRN),
211     colval(scanin[x],BLU));
212     fwrite((char *)scanout, sizeof(COLR), xmax, stdout);
213     } else
214     fwritescan(scanin, xmax, stdout);
215     if (ferror(stdout))
216     quiterr("error writing output picture");
217     }
218     /* free scanline */
219     free((char *)scanin);
220     if (scanout != NULL)
221     free((char *)scanout);
222     }