ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_bn.c
Revision: 2.9
Committed: Sun Mar 28 20:33:14 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6P1, rad3R6
Changes since 2.8: +36 -22 lines
Log Message:
Continued ANSIfication, and other fixes and clarifications.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.9 static const char RCSid[] = "$Id: ra_bn.c,v 2.8 2003/06/05 19:29:34 schorsch Exp $";
3 greg 1.1 #endif
4     /*
5     * ra_bn.c - program to convert between RADIANCE and barneyscan picture format.
6     *
7     * 10/12/88
8     */
9    
10     #include <stdio.h>
11 schorsch 2.9 #include <string.h>
12 greg 2.7 #include <time.h>
13 greg 2.4 #include <math.h>
14    
15 schorsch 2.8 #include "platform.h"
16 greg 1.1 #include "color.h"
17 greg 1.9 #include "resolu.h"
18 greg 1.1
19 greg 2.5 double gamcor = 2.0; /* gamma correction */
20 greg 1.8 int bradj = 0; /* brightness adjustment */
21 greg 1.1 char *progname;
22     char errmsg[128];
23     FILE *rafp, *bnfp[3];
24 schorsch 2.9 int xmax, ymax;
25 greg 1.1
26 schorsch 2.9 static void quiterr(char *err);
27     static int openbarney(char *fname, char *mode);
28     static int getint(FILE *fp);
29     static void putint(int val, FILE *fp);
30     static void ra2bn(void);
31     static void bn2ra(void);
32 greg 1.1
33    
34 schorsch 2.9 int
35     main(
36     int argc,
37     char *argv[]
38     )
39 greg 1.1 {
40     int reverse = 0;
41     int i;
42 schorsch 2.8 SET_DEFAULT_BINARY();
43     SET_FILE_BINARY(stdin);
44     SET_FILE_BINARY(stdout);
45 greg 1.1 progname = argv[0];
46    
47     for (i = 1; i < argc; i++)
48     if (argv[i][0] == '-')
49     switch (argv[i][1]) {
50     case 'g':
51 greg 2.5 gamcor = atof(argv[++i]);
52 greg 1.1 break;
53     case 'r':
54     reverse = !reverse;
55     break;
56 greg 1.8 case 'e':
57     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
58     goto userr;
59     bradj = atoi(argv[++i]);
60     break;
61 greg 1.1 default:
62     goto userr;
63     }
64     else
65     break;
66 greg 1.6 /* set gamma correction */
67 greg 2.5 setcolrgam(gamcor);
68 greg 1.1
69     if (reverse) {
70     if (i > argc-1 || i < argc-2)
71     goto userr;
72     if (openbarney(argv[i], "r") < 0) {
73     sprintf(errmsg, "cannot open input \"%s\"", argv[i]);
74     quiterr(errmsg);
75     }
76     if (i == argc-1 || !strcmp(argv[i+1], "-"))
77     rafp = stdout;
78     else if ((rafp = fopen(argv[i+1], "w")) == NULL) {
79     sprintf(errmsg, "cannot open output \"%s\"",
80     argv[i+1]);
81     quiterr(errmsg);
82     }
83     /* put header */
84 greg 2.6 newheader("RADIANCE", stdout);
85 greg 1.3 printargs(i, argv, rafp);
86 greg 1.7 fputformat(COLRFMT, rafp);
87 greg 1.1 putc('\n', rafp);
88 greg 1.9 fprtresolu(xmax, ymax, rafp);
89 greg 1.1 /* convert file */
90     bn2ra();
91     } else {
92     if (i != argc-2)
93     goto userr;
94     if (!strcmp(argv[i], "-"))
95     rafp = stdin;
96     else if ((rafp = fopen(argv[i], "r")) == NULL) {
97     sprintf(errmsg, "cannot open input \"%s\"",
98     argv[i]);
99     quiterr(errmsg);
100     }
101     /* get header */
102 greg 1.7 if (checkheader(rafp, COLRFMT, NULL) < 0 ||
103 greg 1.9 fgetresolu(&xmax, &ymax, rafp) < 0)
104 greg 1.1 quiterr("bad RADIANCE format");
105     if (openbarney(argv[i+1], "w") < 0) {
106     sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]);
107     quiterr(errmsg);
108     }
109     /* convert file */
110     ra2bn();
111     }
112     quiterr(NULL);
113     userr:
114 greg 1.8 fprintf(stderr, "Usage: %s [-g gamma][-e +/-stops] {input|-} output\n",
115 greg 1.1 progname);
116 greg 1.8 fprintf(stderr, " or: %s -r [-g gamma][-e +/-stops] input [output|-]\n",
117     progname);
118 greg 1.1 exit(1);
119     }
120    
121    
122 schorsch 2.9 static void
123     quiterr( /* print message and exit */
124     char *err
125     )
126 greg 1.1 {
127     if (err != NULL) {
128     fprintf(stderr, "%s: %s\n", progname, err);
129     exit(1);
130     }
131     exit(0);
132     }
133    
134    
135 schorsch 2.9 static int
136     openbarney( /* open barneyscan files */
137     char *fname,
138     char *mode
139     )
140 greg 1.1 {
141     static char suffix[3][4] = {"red", "grn", "blu"};
142     int i;
143     char file[128];
144    
145     for (i = 0; i < 3; i++) {
146     sprintf(file, "%s.%s", fname, suffix[i]);
147     if ((bnfp[i] = fopen(file, mode)) == NULL)
148     return(-1);
149     }
150     if (mode[0] == 'r') { /* get header */
151     xmax = getint(bnfp[0]);
152     ymax = getint(bnfp[0]);
153     for (i = 1; i < 3; i++)
154     if (getint(bnfp[i]) != xmax || getint(bnfp[i]) != ymax)
155     return(-1);
156     } else { /* put header */
157     for (i = 0; i < 3; i++) {
158     putint(xmax, bnfp[i]);
159     putint(ymax, bnfp[i]);
160     }
161     }
162     return(0);
163     }
164    
165    
166 schorsch 2.9 static int
167     getint( /* get short int from barneyscan file */
168     register FILE *fp
169     )
170 greg 1.1 {
171     register short val;
172    
173     val = getc(fp);
174     val |= getc(fp) << 8;
175    
176     return(val);
177     }
178    
179    
180 schorsch 2.9 static void
181     putint( /* put short int to barneyscan file */
182     register int val,
183     register FILE *fp
184     )
185 greg 1.1 {
186     putc(val&0xff, fp);
187     putc((val >> 8)&0xff, fp);
188     }
189    
190    
191 schorsch 2.9 static void
192     ra2bn(void) /* convert radiance to barneyscan */
193 greg 1.1 {
194 greg 1.6 register int i;
195     register COLR *inl;
196 greg 1.1 int j;
197    
198 greg 1.6 if ((inl = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
199 greg 1.1 quiterr("out of memory");
200     for (j = 0; j < ymax; j++) {
201 greg 1.6 if (freadcolrs(inl, xmax, rafp) < 0)
202 greg 1.1 quiterr("error reading RADIANCE file");
203 greg 1.8 if (bradj)
204     shiftcolrs(inl, xmax, bradj);
205 greg 1.6 colrs_gambs(inl, xmax);
206     for (i = 0; i < xmax; i++) {
207     putc(inl[i][RED], bnfp[0]);
208     putc(inl[i][GRN], bnfp[1]);
209     putc(inl[i][BLU], bnfp[2]);
210     }
211     if (ferror(bnfp[0]) || ferror(bnfp[1]) || ferror(bnfp[2]))
212     quiterr("error writing Barney files");
213 greg 1.1 }
214 greg 2.7 free((void *)inl);
215 greg 1.1 }
216    
217    
218 schorsch 2.9 static void
219     bn2ra(void) /* convert barneyscan to radiance */
220 greg 1.1 {
221 greg 1.6 register int i;
222     register COLR *outline;
223 greg 1.1 int j;
224    
225 greg 1.6 if ((outline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
226 greg 1.1 quiterr("out of memory");
227     for (j = 0; j < ymax; j++) {
228 greg 1.6 for (i = 0; i < xmax; i++) {
229     outline[i][RED] = getc(bnfp[0]);
230     outline[i][GRN] = getc(bnfp[1]);
231     outline[i][BLU] = getc(bnfp[2]);
232     }
233     if (feof(bnfp[0]) || feof(bnfp[1]) || feof(bnfp[2]))
234     quiterr("error reading barney file");
235     gambs_colrs(outline, xmax);
236 greg 1.8 if (bradj)
237     shiftcolrs(outline, xmax, bradj);
238 greg 1.6 if (fwritecolrs(outline, xmax, rafp) < 0)
239 greg 1.1 quiterr("error writing RADIANCE file");
240     }
241 greg 2.7 free((void *)outline);
242 greg 1.1 }