ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_rgbe.c
Revision: 2.12
Committed: Thu Jun 5 19:29:34 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.11: +5 -11 lines
Log Message:
Macros for setting binary file mode. Replacing MSDOS by _WIN32.

File Contents

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