ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_rgbe.c
Revision: 2.16
Committed: Fri Jan 2 12:47:01 2004 UTC (20 years, 4 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.15: +8 -6 lines
Log Message:
Fixed typing/prototype of getheader() and its callback.

File Contents

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