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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines