ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_rgbe.c
Revision: 2.13
Committed: Thu Jul 3 22:41:44 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.12: +2 -1 lines
Log Message:
Reduced compile problems on Windows.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.13 static const char RCSid[] = "$Id: ra_rgbe.c,v 2.12 2003/06/05 19:29:34 schorsch Exp $";
3 greg 1.1 #endif
4     /*
5     * program to convert from RADIANCE RLE to flat format
6     */
7    
8     #include <stdio.h>
9 greg 2.4 #include <math.h>
10 greg 2.10 #include <time.h>
11 schorsch 2.13 #include <string.h>
12 schorsch 2.12
13     #include "platform.h"
14 greg 2.4 #include "color.h"
15     #include "resolu.h"
16    
17 gregl 2.8 extern int addhline();
18 greg 2.3
19 gregl 2.8 #define dumpheader(fp) fwrite(headlines, 1, headlen, fp)
20    
21 greg 1.1 int bradj = 0; /* brightness adjustment */
22    
23     int doflat = 1; /* produce flat file */
24    
25 gregl 2.8 int force = 0; /* force file overwrite? */
26    
27     int findframe = 0; /* find a specific frame? */
28    
29     int frameno = 0; /* current frame number */
30     int fmterr = 0; /* got input format error */
31     char *headlines; /* current header info. */
32     int headlen; /* current header length */
33    
34 greg 1.1 char *progname;
35    
36    
37     main(argc, argv)
38     int argc;
39     char *argv[];
40     {
41 gregl 2.8 char *ospec;
42 greg 1.1 int i;
43 gregl 2.7
44 greg 1.1 progname = argv[0];
45    
46     for (i = 1; i < argc; i++)
47     if (argv[i][0] == '-')
48     switch (argv[i][1]) {
49     case 'r':
50     doflat = !doflat;
51     break;
52     case 'e':
53     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
54     goto userr;
55     bradj = atoi(argv[++i]);
56     break;
57 gregl 2.8 case 'n':
58     findframe = atoi(argv[++i]);
59     break;
60     case 'f':
61     force++;
62     break;
63     case '\0':
64     goto gotfile;
65 greg 1.1 default:
66     goto userr;
67     }
68     else
69     break;
70 gregl 2.8 gotfile:
71 greg 1.1 if (i < argc-2)
72     goto userr;
73 gregl 2.8 if (i <= argc-1 && strcmp(argv[i], "-") &&
74     freopen(argv[i], "r", stdin) == NULL) {
75 greg 1.1 fprintf(stderr, "%s: can't open input \"%s\"\n",
76     progname, argv[i]);
77     exit(1);
78     }
79 schorsch 2.12 SET_FILE_BINARY(stdin);
80 gregl 2.8 ospec = i==argc-2 ? argv[i+1] : (char *)NULL;
81     while (transfer(ospec))
82     ;
83 greg 1.1 exit(0);
84     userr:
85 gregl 2.8 fprintf(stderr,
86     "Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n",
87 greg 1.1 progname);
88     exit(1);
89     }
90    
91    
92 gregl 2.8 transfer(ospec) /* transfer a Radiance picture */
93     char *ospec;
94 greg 1.1 {
95 gregl 2.8 char oname[128];
96     FILE *fp;
97 greg 1.2 int order;
98 greg 2.3 int xmax, ymax;
99 greg 1.1 COLR *scanin;
100     int y;
101 gregl 2.8 /* get header info. */
102     if (!(y = loadheader(stdin)))
103     return(0);
104     if (y < 0 || (order = fgetresolu(&xmax, &ymax, stdin)) < 0) {
105     fprintf(stderr, "%s: bad input format\n", progname);
106     exit(1);
107     }
108     /* did we pass the target frame? */
109     if (findframe && findframe < frameno)
110     return(0);
111     /* allocate scanline */
112     scanin = (COLR *)tempbuffer(xmax*sizeof(COLR));
113     if (scanin == NULL) {
114     perror(progname);
115     exit(1);
116     }
117     /* skip frame? */
118     if (findframe > frameno) {
119     for (y = ymax; y--; )
120     if (freadcolrs(scanin, xmax, stdin) < 0) {
121     fprintf(stderr,
122     "%s: error reading input picture\n",
123     progname);
124     exit(1);
125     }
126     return(1);
127     }
128     /* open output file/command */
129     if (ospec == NULL) {
130     strcpy(oname, "<stdout>");
131     fp = stdout;
132     } else {
133     sprintf(oname, ospec, frameno);
134     if (oname[0] == '!') {
135     if ((fp = popen(oname+1, "w")) == NULL) {
136     fprintf(stderr, "%s: cannot start \"%s\"\n",
137     progname, oname);
138     exit(1);
139     }
140     } else {
141     if (!force && access(oname, 0) >= 0) {
142     fprintf(stderr,
143     "%s: output file \"%s\" exists\n",
144     progname, oname);
145     exit(1);
146     }
147     if ((fp = fopen(oname, "w")) == NULL) {
148     fprintf(stderr, "%s: ", progname);
149     perror(oname);
150     exit(1);
151     }
152     }
153     }
154 schorsch 2.12 SET_FILE_BINARY(fp);
155 gregl 2.8 dumpheader(fp); /* put out header */
156     fputs(progname, fp);
157 greg 1.1 if (bradj)
158 gregl 2.8 fprintf(fp, " -e %+d", bradj);
159 gregl 2.7 if (!doflat)
160 gregl 2.8 fputs(" -r", fp);
161     fputc('\n', fp);
162 greg 2.2 if (bradj)
163 gregl 2.8 fputexpos(pow(2.0, (double)bradj), fp);
164     fputc('\n', fp);
165     fputresolu(order, xmax, ymax, fp);
166     /* transfer picture */
167 gregl 2.7 for (y = ymax; y--; ) {
168 gregl 2.8 if (freadcolrs(scanin, xmax, stdin) < 0) {
169     fprintf(stderr, "%s: error reading input picture\n",
170     progname);
171     exit(1);
172     }
173 greg 1.1 if (bradj)
174     shiftcolrs(scanin, xmax, bradj);
175     if (doflat)
176 gregl 2.8 fwrite((char *)scanin, sizeof(COLR), xmax, fp);
177 greg 1.1 else
178 gregl 2.8 fwritecolrs(scanin, xmax, fp);
179     if (ferror(fp)) {
180     fprintf(stderr, "%s: error writing output to \"%s\"\n",
181     progname, oname);
182     exit(1);
183     }
184 greg 1.1 }
185 gregl 2.8 /* clean up */
186     if (oname[0] == '!')
187     pclose(fp);
188     else if (ospec != NULL)
189     fclose(fp);
190     return(1);
191     }
192    
193    
194 gwlarson 2.9 int
195 gregl 2.8 addhline(s) /* add a line to our info. header */
196     char *s;
197     {
198     char fmt[32];
199     int n;
200    
201     if (formatval(fmt, s))
202     fmterr += !globmatch(PICFMT, fmt);
203     else if (!strncmp(s, "FRAME=", 6))
204     frameno = atoi(s+6);
205     n = strlen(s);
206     if (headlen)
207 greg 2.11 headlines = (char *)realloc((void *)headlines, headlen+n+1);
208 gregl 2.8 else
209     headlines = (char *)malloc(n+1);
210     if (headlines == NULL) {
211     perror(progname);
212     exit(1);
213     }
214     strcpy(headlines+headlen, s);
215     headlen += n;
216 gwlarson 2.9 return(0);
217 gregl 2.8 }
218    
219    
220     loadheader(fp) /* load an info. header into memory */
221     FILE *fp;
222     {
223     fmterr = 0; frameno = 0;
224     if (headlen) { /* free old header */
225     free(headlines);
226     headlen = 0;
227     }
228     if (getheader(fp, addhline, NULL) < 0)
229     return(0);
230     if (fmterr)
231     return(-1);
232     return(1);
233 greg 1.1 }