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 2.3 by greg, Mon Sep 21 12:15:13 1992 UTC vs.
Revision 2.8 by gregl, Fri Jan 23 11:38:39 1998 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines