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.7 by gregl, Fri Jan 23 09:28:29 1998 UTC vs.
Revision 2.8 by gregl, Fri Jan 23 11:38:39 1998 UTC

# Line 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17   #include  <fcntl.h>
18   #endif
19  
20 < extern char  *malloc();
20 > extern char     *malloc(), *realloc(), *tempbuffer();
21 > extern int      addhline();
22  
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 30 | Line 42 | main(argc, argv)
42   int  argc;
43   char  *argv[];
44   {
45 +        char    *ospec;
46          int  i;
47  
48          progname = argv[0];
# Line 45 | 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          }
61        if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
62                fprintf(stderr, "%s: can't open output \"%s\"\n",
63                                progname, argv[i+1]);
64                exit(1);
65        }
83   #ifdef MSDOS
84          setmode(fileno(stdin), O_BINARY);
68        setmode(fileno(stdout), O_BINARY);
85   #endif
86 <        transfer();
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 <        static char     ourfmt[LPICFMT+1] = PICFMT;
101 >        char    oname[128];
102 >        FILE    *fp;
103          int     order;
104          int     xmax, ymax;
105          COLR    *scanin;
106          int     y;
107 <                                /* get header info. */
108 <        if ((y = checkheader(stdin, ourfmt, stdout)) < 0 ||
109 <                        (order = fgetresolu(&xmax, &ymax, stdin)) < 0)
110 <                quiterr("bad picture format");
111 <        if (!y)
112 <                strcpy(ourfmt, COLRFMT);
113 <        fputs(progname, 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 <                fprintf(stdout, " -e %+d", bradj);
166 >                fprintf(fp, " -e %+d", bradj);
167          if (!doflat)
168 <                fputs(" -r", stdout);
169 <        fputc('\n', stdout);
168 >                fputs(" -r", fp);
169 >        fputc('\n', fp);
170          if (bradj)
171 <                fputexpos(pow(2.0, (double)bradj), stdout);
172 <        fputformat(ourfmt, stdout);
173 <        fputc('\n', stdout);
174 <        fputresolu(order, xmax, ymax, stdout);
114 <                                                /* allocate scanline */
115 <        scanin = (COLR *)malloc(xmax*sizeof(COLR));
116 <        if (scanin == NULL)
117 <                quiterr("out of memory in transfer");
118 <                                                /* convert image */
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 <                        quiterr("error reading input picture");
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 output picture");
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