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.6 by greg, Thu Nov 18 09:55:20 1993 UTC vs.
Revision 2.26 by greg, Sat Jun 7 05:09:46 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 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  
11 #include  <stdio.h>
8   #include  <math.h>
9 +
10 + #include  "platform.h"
11 + #include  "rtio.h"
12   #include  "color.h"
13   #include  "resolu.h"
14  
15 < #ifdef MSDOS
17 < #include  <fcntl.h>
18 < #endif
15 > #define dumpheader(fp)  putbinary(headlines, 1, headlen, fp)
16  
20 extern char  *malloc();
21
17   int  bradj = 0;                         /* brightness adjustment */
23
18   int  doflat = 1;                        /* produce flat file */
19 + int  force = 0;                         /* force file overwrite? */
20 + int  findframe = 0;                     /* find a specific frame? */
21 + int  frameno = 0;                       /* current frame number */
22 + int  fmterr = 0;                        /* got input format error */
23 + char  *headlines = NULL;                /* current header info. */
24 + int  headlen1 = 0;                      /* length of initial frame header */
25 + int  headlen = 0;                       /* current header length */
26 + char  fmt[MAXFMTLEN];                   /* input format */
27  
28   char  *progname;
29  
30 + static gethfunc addhline;
31 + static int transfer(char *ospec);
32 + static int loadheader(FILE *fp);
33  
34 < main(argc, argv)
35 < int  argc;
36 < char  *argv[];
34 >
35 > int
36 > main(int  argc, char  *argv[])
37   {
38 +        char    *ospec;
39          int  i;
40 < #ifdef MSDOS
35 <        extern int  _fmode;
36 <        _fmode = O_BINARY;
37 <        setmode(fileno(stdin), O_BINARY);
38 <        setmode(fileno(stdout), O_BINARY);
39 < #endif
40 >
41          progname = argv[0];
42  
43          for (i = 1; i < argc; i++)
# Line 50 | Line 51 | char  *argv[];
51                                          goto userr;
52                                  bradj = atoi(argv[++i]);
53                                  break;
54 +                        case 'n':
55 +                                findframe = atoi(argv[++i]);
56 +                                break;
57 +                        case 'f':
58 +                                force++;
59 +                                break;
60 +                        case '\0':
61 +                                goto gotfile;
62                          default:
63                                  goto userr;
64                          }
65                  else
66                          break;
67 <
67 > gotfile:
68          if (i < argc-2)
69                  goto userr;
70 <        if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
70 >        if (i <= argc-1 && strcmp(argv[i], "-") &&
71 >                        freopen(argv[i], "r", stdin) == NULL) {
72                  fprintf(stderr, "%s: can't open input \"%s\"\n",
73                                  progname, argv[i]);
74                  exit(1);
75          }
76 <        if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
77 <                fprintf(stderr, "%s: can't open output \"%s\"\n",
78 <                                progname, argv[i+1]);
79 <                exit(1);
70 <        }
71 <        transfer();
76 >        SET_FILE_BINARY(stdin);
77 >        ospec = i==argc-2 ? argv[i+1] : (char *)NULL;
78 >        while (transfer(ospec))
79 >                ;
80          exit(0);
81   userr:
82 <        fprintf(stderr, "Usage: %s [-r][-e +/-stops] [input [output]]\n",
82 >        fprintf(stderr,
83 >        "Usage: %s [-r][-e +/-stops][-f][-n frame] [input [outspec]]\n",
84                          progname);
85          exit(1);
86   }
87  
88  
89 < quiterr(err)            /* print message and exit */
90 < char  *err;
89 > static int
90 > transfer(                       /* transfer a Radiance picture */
91 >        char    *ospec
92 > )
93   {
94 <        if (err != NULL) {
95 <                fprintf(stderr, "%s: %s\n", progname, err);
85 <                exit(1);
86 <        }
87 <        exit(0);
88 < }
89 <
90 <
91 < transfer()              /* transfer Radiance picture */
92 < {
94 >        char    oname[PATH_MAX];
95 >        FILE    *fp;
96          int     order;
97          int     xmax, ymax;
98          COLR    *scanin;
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");
104 <        fputs(progname, stdout);
105 <        if (bradj)
103 <                printf(" -e %+d", bradj);
104 <        if (doflat)
105 <                fputs("\n", stdout);
106 <        else {
107 <                fputs(" -r\n", stdout);
108 <                fputformat(COLRFMT, stdout);
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 <        if (bradj)
108 <                fputexpos(pow(2.0, (double)bradj), stdout);
109 <        fputs("\n", stdout);
110 <        fputresolu(order, xmax, ymax, stdout);
114 <                                                /* allocate scanline */
107 >                                        /* did we pass the target frame? */
108 >        if (findframe && findframe < frameno)
109 >                return(0);
110 >                                        /* allocate scanline */
111          scanin = (COLR *)malloc(xmax*sizeof(COLR));
112 <        if (scanin == NULL)
113 <                quiterr("out of memory in transfer");
114 <                                                /* convert image */
115 <        for (y = ymax-1; y >= 0; y--) {
116 <                if (freadcolrs(scanin, xmax, stdin) < 0)
117 <                        quiterr("error reading input picture");
112 >        if (scanin == NULL) {
113 >                perror(progname);
114 >                exit(1);
115 >        }
116 >                                        /* skip frame? */
117 >        if (findframe > frameno) {
118 >                if (NCSAMP > 3) {
119 >                        if (fseek(stdin, ymax*xmax*LSCOLR, SEEK_CUR) < 0) {
120 >                                perror(progname);
121 >                                exit(1);
122 >                        }
123 >                } else
124 >                    for (y = ymax; y--; )
125 >                        if (freadcolrs(scanin, xmax, stdin) < 0) {
126 >                                fprintf(stderr,
127 >                                        "%s: error reading input picture\n",
128 >                                                progname);
129 >                                exit(1);
130 >                        }
131 >                free(scanin);
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 >        SET_FILE_BINARY(fp);
161 >        newheader("RADIANCE", fp);              /* put out header */
162 >        dumpheader(fp);
163 >        fputs(progname, fp);
164 >        if (bradj)
165 >                fprintf(fp, " -e %+d", bradj);
166 >        if (!doflat)
167 >                fputs(" -r", fp);
168 >        fputc('\n', fp);
169 >        if (bradj)
170 >                fputexpos(pow(2.0, (double)bradj), fp);
171 >        if (frameno)
172 >                fprintf(fp, "FRAME=%d\n", frameno);
173 >        if (fmt[0])
174 >                fputformat(fmt, fp);
175 >        fputc('\n', fp);
176 >        fputresolu(order, xmax, ymax, fp);
177 >                                        /* transfer picture */
178 >        for (y = ymax; y--; ) {
179 >                if (fread2colrs(scanin, xmax, stdin, NCSAMP, WLPART) < 0) {
180 >                        fprintf(stderr, "%s: error reading input picture\n",
181 >                                        progname);
182 >                        exit(1);
183 >                }
184                  if (bradj)
185                          shiftcolrs(scanin, xmax, bradj);
186 <                if (doflat)
187 <                        fwrite((char *)scanin, sizeof(COLR), xmax, stdout);
186 >                if (doflat ? (putbinary(scanin, sizeof(COLR), xmax, fp) != xmax) :
187 >                                (fwritecolrs(scanin, xmax, fp) < 0))
188 >                        goto writerr;
189 >        }
190 >        free(scanin);                   /* clean up */
191 >        if (fflush(fp) == EOF)
192 >                goto writerr;
193 >        if (oname[0] == '!') {
194 >                if (pclose(fp) != 0)
195 >                        fprintf(stderr, "%s: warning - bad status from \"%s\"\n",
196 >                                        progname, oname);
197 >        } else if (ospec != NULL)
198 >                fclose(fp);
199 >        return(1);
200 > writerr:
201 >        fprintf(stderr, "%s: error writing output to \"%s\"\n",
202 >                        progname, oname);
203 >        exit(1);
204 > }
205 >
206 >
207 > static int
208 > addhline(                       /* add a line to our info. header */
209 >        char    *s,
210 >        void    *p
211 > )
212 > {
213 >        int     n;
214 >
215 >        if (isheadid(s))
216 >                return(0);
217 >        if (!strncmp(s, "FRAME=", 6)) {
218 >                frameno = atoi(s+6);
219 >                return(0);
220 >        }
221 >        if (formatval(fmt, s)) {
222 >                if (!strcmp(fmt, SPECFMT))
223 >                        strcpy(fmt, COLRFMT);
224                  else
225 <                        fwritecolrs(scanin, xmax, stdout);
226 <                if (ferror(stdout))
129 <                        quiterr("error writing output picture");
225 >                        fmterr += !globmatch(PICFMT, fmt);
226 >                return(0);
227          }
228 <                                                /* free scanline */
229 <        free((char *)scanin);
228 >        if (isncomp(s)) {
229 >                NCSAMP = ncompval(s);
230 >                return(NCSAMP - 3);
231 >        }
232 >        if (iswlsplit(s)) {
233 >                wlsplitval(WLPART, s);
234 >                return(0);
235 >        }
236 >        n = strlen(s);
237 >        if (headlen)
238 >                headlines = (char *)realloc((void *)headlines, headlen+n+1);
239 >        else
240 >                headlines = (char *)malloc(n+1);
241 >        if (headlines == NULL) {
242 >                perror(progname);
243 >                exit(1);
244 >        }
245 >        strcpy(headlines+headlen, s);
246 >        headlen += n;
247 >        return(0);
248 > }
249 >
250 >
251 > static int
252 > loadheader(                     /* load an info. header into memory */
253 >        FILE    *fp
254 > )
255 > {
256 >        fmterr = 0; frameno = 0;
257 >                                /* revert to initial header length */
258 >        if (!headlen1) headlen1 = headlen;
259 >        else headlen = headlen1;
260 >
261 >        if (getheader(fp, addhline, NULL) < 0)
262 >                return(0);
263 >        if (fmterr)
264 >                return(-1);
265 >        return(1);
266   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines