ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_rgbe.c
Revision: 2.22
Committed: Tue Jun 11 17:24:01 2024 UTC (10 months, 3 weeks ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.21: +33 -22 lines
Log Message:
perf(ra_rgbe): Improved error-checking and include initial header in all subsequent frames

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.22 static const char RCSid[] = "$Id: ra_rgbe.c,v 2.21 2019/12/28 18:05:14 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * program to convert from RADIANCE RLE to flat format
6     */
7    
8 greg 2.4 #include <math.h>
9 schorsch 2.12
10     #include "platform.h"
11 greg 2.19 #include "rtio.h"
12 greg 2.18 #include "paths.h"
13 greg 2.4 #include "color.h"
14     #include "resolu.h"
15    
16 greg 2.19 #define dumpheader(fp) putbinary(headlines, 1, headlen, fp)
17 gregl 2.8
18 greg 1.1 int bradj = 0; /* brightness adjustment */
19     int doflat = 1; /* produce flat file */
20 gregl 2.8 int force = 0; /* force file overwrite? */
21     int findframe = 0; /* find a specific frame? */
22     int frameno = 0; /* current frame number */
23     int fmterr = 0; /* got input format error */
24 greg 2.22 char *headlines = NULL; /* current header info. */
25     int headlen1 = 0; /* length of initial frame header */
26     int headlen = 0; /* current header length */
27     char fmt[MAXFMTLEN]; /* input format */
28 gregl 2.8
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 greg 2.22 newheader("RADIANCE", fp); /* put out header */
156     dumpheader(fp);
157 gregl 2.8 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 greg 2.22 if (frameno)
166     fprintf(fp, "FRAME=%d\n", frameno);
167     if (fmt[0])
168     fputformat(fmt, fp);
169 gregl 2.8 fputc('\n', fp);
170     fputresolu(order, xmax, ymax, fp);
171     /* transfer picture */
172 gregl 2.7 for (y = ymax; y--; ) {
173 gregl 2.8 if (freadcolrs(scanin, xmax, stdin) < 0) {
174     fprintf(stderr, "%s: error reading input picture\n",
175     progname);
176     exit(1);
177     }
178 greg 1.1 if (bradj)
179     shiftcolrs(scanin, xmax, bradj);
180 greg 2.22 if (doflat ? (putbinary(scanin, sizeof(COLR), xmax, fp) != xmax) :
181     (fwritecolrs(scanin, xmax, fp) < 0))
182     goto writerr;
183 greg 1.1 }
184 greg 2.22 if (fflush(fp) == EOF) /* clean up */
185     goto writerr;
186 gregl 2.8 if (oname[0] == '!')
187     pclose(fp);
188     else if (ospec != NULL)
189     fclose(fp);
190     return(1);
191 greg 2.22 writerr:
192     fprintf(stderr, "%s: error writing output to \"%s\"\n",
193     progname, oname);
194     exit(1);
195 gregl 2.8 }
196    
197    
198 schorsch 2.16 static int
199     addhline( /* add a line to our info. header */
200     char *s,
201     void *p
202     )
203 gregl 2.8 {
204     int n;
205    
206 greg 2.22 if (isheadid(s))
207     return(0);
208     if (!strncmp(s, "FRAME=", 6)) {
209     frameno = atoi(s+6);
210     return(0);
211     }
212     if (formatval(fmt, s)) {
213 gregl 2.8 fmterr += !globmatch(PICFMT, fmt);
214 greg 2.22 return(0);
215     }
216 gregl 2.8 n = strlen(s);
217     if (headlen)
218 greg 2.11 headlines = (char *)realloc((void *)headlines, headlen+n+1);
219 gregl 2.8 else
220     headlines = (char *)malloc(n+1);
221     if (headlines == NULL) {
222     perror(progname);
223     exit(1);
224     }
225     strcpy(headlines+headlen, s);
226     headlen += n;
227 gwlarson 2.9 return(0);
228 gregl 2.8 }
229    
230    
231 schorsch 2.17 static int
232     loadheader( /* load an info. header into memory */
233     FILE *fp
234     )
235 gregl 2.8 {
236     fmterr = 0; frameno = 0;
237 greg 2.22 /* revert to initial header length */
238     if (!headlen1) headlen1 = headlen;
239     else headlen = headlen1;
240    
241 gregl 2.8 if (getheader(fp, addhline, NULL) < 0)
242     return(0);
243     if (fmterr)
244     return(-1);
245     return(1);
246 greg 1.1 }