ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_bn.c
Revision: 2.6
Committed: Sun Feb 27 10:17:10 1994 UTC (30 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +1 -0 lines
Log Message:
Added new ID to first line of header and changed use of formatval

File Contents

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