ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_rgbe.c
Revision: 2.17
Committed: Sun Mar 28 20:33:14 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad5R0, rad3R7P2, rad3R7P1, rad4R2, rad4R1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9, rad4R2P1
Changes since 2.16: +13 -12 lines
Log Message:
Continued ANSIfication, and other fixes and clarifications.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.17 static const char RCSid[] = "$Id: ra_rgbe.c,v 2.16 2004/01/02 12:47:01 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 schorsch 2.14 #include "rtprocess.h"
15 greg 2.4 #include "color.h"
16     #include "resolu.h"
17    
18 gregl 2.8 #define dumpheader(fp) fwrite(headlines, 1, headlen, fp)
19    
20 greg 1.1 int bradj = 0; /* brightness adjustment */
21     int doflat = 1; /* produce flat file */
22 gregl 2.8 int force = 0; /* force file overwrite? */
23     int findframe = 0; /* find a specific frame? */
24     int frameno = 0; /* current frame number */
25     int fmterr = 0; /* got input format error */
26     char *headlines; /* current header info. */
27     int headlen; /* current header length */
28    
29 greg 1.1 char *progname;
30    
31 schorsch 2.16 static gethfunc addhline;
32 schorsch 2.17 static int transfer(char *ospec);
33     static int loadheader(FILE *fp);
34 schorsch 2.16
35 greg 1.1
36 schorsch 2.17 int
37     main(int argc, char *argv[])
38 greg 1.1 {
39 gregl 2.8 char *ospec;
40 greg 1.1 int i;
41 gregl 2.7
42 greg 1.1 progname = argv[0];
43    
44     for (i = 1; i < argc; i++)
45     if (argv[i][0] == '-')
46     switch (argv[i][1]) {
47     case 'r':
48     doflat = !doflat;
49     break;
50     case 'e':
51     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
52     goto userr;
53     bradj = atoi(argv[++i]);
54     break;
55 gregl 2.8 case 'n':
56     findframe = atoi(argv[++i]);
57     break;
58     case 'f':
59     force++;
60     break;
61     case '\0':
62     goto gotfile;
63 greg 1.1 default:
64     goto userr;
65     }
66     else
67     break;
68 gregl 2.8 gotfile:
69 greg 1.1 if (i < argc-2)
70     goto userr;
71 gregl 2.8 if (i <= argc-1 && strcmp(argv[i], "-") &&
72     freopen(argv[i], "r", stdin) == NULL) {
73 greg 1.1 fprintf(stderr, "%s: can't open input \"%s\"\n",
74     progname, argv[i]);
75     exit(1);
76     }
77 schorsch 2.12 SET_FILE_BINARY(stdin);
78 gregl 2.8 ospec = i==argc-2 ? argv[i+1] : (char *)NULL;
79     while (transfer(ospec))
80     ;
81 greg 1.1 exit(0);
82     userr:
83 gregl 2.8 fprintf(stderr,
84     "Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n",
85 greg 1.1 progname);
86     exit(1);
87     }
88    
89    
90 schorsch 2.17 static int
91     transfer( /* transfer a Radiance picture */
92     char *ospec
93     )
94 greg 1.1 {
95 schorsch 2.15 char oname[PATH_MAX];
96 gregl 2.8 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 schorsch 2.16 static int
195     addhline( /* add a line to our info. header */
196     char *s,
197     void *p
198     )
199 gregl 2.8 {
200     char fmt[32];
201     int n;
202    
203     if (formatval(fmt, s))
204     fmterr += !globmatch(PICFMT, fmt);
205     else if (!strncmp(s, "FRAME=", 6))
206     frameno = atoi(s+6);
207     n = strlen(s);
208     if (headlen)
209 greg 2.11 headlines = (char *)realloc((void *)headlines, headlen+n+1);
210 gregl 2.8 else
211     headlines = (char *)malloc(n+1);
212     if (headlines == NULL) {
213     perror(progname);
214     exit(1);
215     }
216     strcpy(headlines+headlen, s);
217     headlen += n;
218 gwlarson 2.9 return(0);
219 gregl 2.8 }
220    
221    
222 schorsch 2.17 static int
223     loadheader( /* load an info. header into memory */
224     FILE *fp
225     )
226 gregl 2.8 {
227     fmterr = 0; frameno = 0;
228     if (headlen) { /* free old header */
229     free(headlines);
230     headlen = 0;
231     }
232     if (getheader(fp, addhline, NULL) < 0)
233     return(0);
234     if (fmterr)
235     return(-1);
236     return(1);
237 greg 1.1 }