ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_rgbe.c
(Generate patch)

Comparing ray/src/px/ra_rgbe.c (file contents):
Revision 1.2 by greg, Mon Nov 11 14:01:34 1991 UTC vs.
Revision 2.12 by schorsch, Thu Jun 5 19:29:34 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  program to convert from RADIANCE RLE to flat format
6   */
7  
8   #include  <stdio.h>
9 + #include  <math.h>
10 + #include  <time.h>
11  
12 + #include  "platform.h"
13   #include  "color.h"
14   #include  "resolu.h"
15  
16 + extern int      addhline();
17 +
18 + #define dumpheader(fp)  fwrite(headlines, 1, headlen, fp)
19 +
20   int  bradj = 0;                         /* brightness adjustment */
21  
22   int  doflat = 1;                        /* produce flat file */
23  
24 + 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   char  *progname;
34  
35  
# Line 24 | Line 37 | main(argc, argv)
37   int  argc;
38   char  *argv[];
39   {
40 +        char    *ospec;
41          int  i;
42 <        
42 >
43          progname = argv[0];
44  
45          for (i = 1; i < argc; i++)
# Line 39 | Line 53 | char  *argv[];
53                                          goto userr;
54                                  bradj = atoi(argv[++i]);
55                                  break;
56 +                        case 'n':
57 +                                findframe = atoi(argv[++i]);
58 +                                break;
59 +                        case 'f':
60 +                                force++;
61 +                                break;
62 +                        case '\0':
63 +                                goto gotfile;
64                          default:
65                                  goto userr;
66                          }
67                  else
68                          break;
69 <
69 > gotfile:
70          if (i < argc-2)
71                  goto userr;
72 <        if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
72 >        if (i <= argc-1 && strcmp(argv[i], "-") &&
73 >                        freopen(argv[i], "r", stdin) == NULL) {
74                  fprintf(stderr, "%s: can't open input \"%s\"\n",
75                                  progname, argv[i]);
76                  exit(1);
77          }
78 <        if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
79 <                fprintf(stderr, "can't open output \"%s\"\n",
80 <                                progname, argv[i+1]);
81 <                exit(1);
59 <        }
60 <        transfer();
78 >        SET_FILE_BINARY(stdin);
79 >        ospec = i==argc-2 ? argv[i+1] : (char *)NULL;
80 >        while (transfer(ospec))
81 >                ;
82          exit(0);
83   userr:
84 <        fprintf(stderr, "Usage: %s [-r][-e +/-stops] [input [output]]\n",
84 >        fprintf(stderr,
85 >        "Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n",
86                          progname);
87          exit(1);
88   }
89  
90  
91 < quiterr(err)            /* print message and exit */
92 < char  *err;
91 > transfer(ospec)                 /* transfer a Radiance picture */
92 > char    *ospec;
93   {
94 <        if (err != NULL) {
95 <                fprintf(stderr, "%s: %s\n", progname, err);
74 <                exit(1);
75 <        }
76 <        exit(0);
77 < }
78 <
79 <
80 < transfer()              /* transfer Radiance picture */
81 < {
82 <        extern double   pow();
94 >        char    oname[128];
95 >        FILE    *fp;
96          int     order;
97 <        int     xmax, ymax;
97 >        int     xmax, ymax;
98          COLR    *scanin;
86        register int    x;
99          int     y;
100 <                                /* get header info. */
101 <        if (checkheader(stdin, COLRFMT, stdout) < 0 ||
102 <                        (order = fgetresolu(&xmax, &ymax, stdin)) < 0)
103 <                quiterr("bad picture format");
100 >                                        /* 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 >        SET_FILE_BINARY(fp);
154 >        dumpheader(fp);                 /* put out header */
155 >        fputs(progname, fp);
156          if (bradj)
157 <                fputexpos(pow(2.0, (double)bradj), stdout);
158 <        if (!doflat) {
159 <                fputformat(COLRFMT, stdout);
160 <                printf("%s -r\n\n", progname);
161 <        } else
162 <                printf("%s\n\n", progname);
163 <        fputresolu(order, xmax, ymax, stdout);
164 <                                                /* allocate scanline */
165 <        scanin = (COLR *)malloc(xmax*sizeof(COLR));
166 <        if (scanin == NULL)
167 <                quiterr("out of memory in transfer");
168 <                                                /* convert image */
169 <        for (y = ymax-1; y >= 0; y--) {
170 <                if (freadcolrs(scanin, xmax, stdin) < 0)
171 <                        quiterr("error reading input picture");
157 >                fprintf(fp, " -e %+d", bradj);
158 >        if (!doflat)
159 >                fputs(" -r", fp);
160 >        fputc('\n', fp);
161 >        if (bradj)
162 >                fputexpos(pow(2.0, (double)bradj), fp);
163 >        fputc('\n', fp);
164 >        fputresolu(order, xmax, ymax, fp);
165 >                                        /* transfer picture */
166 >        for (y = ymax; y--; ) {
167 >                if (freadcolrs(scanin, xmax, stdin) < 0) {
168 >                        fprintf(stderr, "%s: error reading input picture\n",
169 >                                        progname);
170 >                        exit(1);
171 >                }
172                  if (bradj)
173                          shiftcolrs(scanin, xmax, bradj);
174                  if (doflat)
175 <                        fwrite((char *)scanin, sizeof(COLR), xmax, stdout);
175 >                        fwrite((char *)scanin, sizeof(COLR), xmax, fp);
176                  else
177 <                        fwritecolrs(scanin, xmax, stdout);
178 <                if (ferror(stdout))
179 <                        quiterr("error writing rasterfile");
177 >                        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          }
184 <                                                /* free scanline */
185 <        free((char *)scanin);
184 >                                        /* clean up */
185 >        if (oname[0] == '!')
186 >                pclose(fp);
187 >        else if (ospec != NULL)
188 >                fclose(fp);
189 >        return(1);
190 > }
191 >
192 >
193 > int
194 > 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 >                headlines = (char *)realloc((void *)headlines, headlen+n+1);
207 >        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 >        return(0);
216 > }
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines