ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_bn.c
Revision: 2.5
Committed: Fri Oct 30 09:11:35 1992 UTC (31 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +3 -3 lines
Log Message:
renamed gamma to avoid conflict with some math libraries

File Contents

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