ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_bn.c
Revision: 1.7
Committed: Thu Apr 18 14:35:35 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.6: +3 -2 lines
Log Message:
added format information to headers

File Contents

# Content
1 /* Copyright (c) 1986 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 #include "color.h"
16
17 extern double atof(), pow();
18
19 double gamma = 2.0; /* gamma correction */
20
21 char *progname;
22
23 char errmsg[128];
24
25 FILE *rafp, *bnfp[3];
26
27 int xmax, ymax;
28
29
30 main(argc, argv)
31 int argc;
32 char *argv[];
33 {
34 int reverse = 0;
35 int i;
36
37 progname = argv[0];
38
39 for (i = 1; i < argc; i++)
40 if (argv[i][0] == '-')
41 switch (argv[i][1]) {
42 case 'g':
43 gamma = atof(argv[++i]);
44 break;
45 case 'r':
46 reverse = !reverse;
47 break;
48 default:
49 goto userr;
50 }
51 else
52 break;
53 /* set gamma correction */
54 setcolrgam(gamma);
55
56 if (reverse) {
57 if (i > argc-1 || i < argc-2)
58 goto userr;
59 if (openbarney(argv[i], "r") < 0) {
60 sprintf(errmsg, "cannot open input \"%s\"", argv[i]);
61 quiterr(errmsg);
62 }
63 if (i == argc-1 || !strcmp(argv[i+1], "-"))
64 rafp = stdout;
65 else if ((rafp = fopen(argv[i+1], "w")) == NULL) {
66 sprintf(errmsg, "cannot open output \"%s\"",
67 argv[i+1]);
68 quiterr(errmsg);
69 }
70 /* put header */
71 printargs(i, argv, rafp);
72 fputformat(COLRFMT, rafp);
73 putc('\n', rafp);
74 fputresolu(YMAJOR|YDECR, xmax, ymax, rafp);
75 /* convert file */
76 bn2ra();
77 } else {
78 if (i != argc-2)
79 goto userr;
80 if (!strcmp(argv[i], "-"))
81 rafp = stdin;
82 else if ((rafp = fopen(argv[i], "r")) == NULL) {
83 sprintf(errmsg, "cannot open input \"%s\"",
84 argv[i]);
85 quiterr(errmsg);
86 }
87 /* get header */
88 if (checkheader(rafp, COLRFMT, NULL) < 0 ||
89 fgetresolu(&xmax, &ymax, rafp) != (YMAJOR|YDECR))
90 quiterr("bad RADIANCE format");
91 if (openbarney(argv[i+1], "w") < 0) {
92 sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]);
93 quiterr(errmsg);
94 }
95 /* convert file */
96 ra2bn();
97 }
98 quiterr(NULL);
99 userr:
100 fprintf(stderr, "Usage: %s [-g gamma] {input|-} output\n", progname);
101 fprintf(stderr, " or: %s -r [-g gamma] input [output|-]\n",
102 progname);
103 exit(1);
104 }
105
106
107 quiterr(err) /* print message and exit */
108 char *err;
109 {
110 if (err != NULL) {
111 fprintf(stderr, "%s: %s\n", progname, err);
112 exit(1);
113 }
114 exit(0);
115 }
116
117
118 openbarney(fname, mode) /* open barneyscan files */
119 char *fname;
120 char *mode;
121 {
122 static char suffix[3][4] = {"red", "grn", "blu"};
123 int i;
124 char file[128];
125
126 for (i = 0; i < 3; i++) {
127 sprintf(file, "%s.%s", fname, suffix[i]);
128 if ((bnfp[i] = fopen(file, mode)) == NULL)
129 return(-1);
130 }
131 if (mode[0] == 'r') { /* get header */
132 xmax = getint(bnfp[0]);
133 ymax = getint(bnfp[0]);
134 for (i = 1; i < 3; i++)
135 if (getint(bnfp[i]) != xmax || getint(bnfp[i]) != ymax)
136 return(-1);
137 } else { /* put header */
138 for (i = 0; i < 3; i++) {
139 putint(xmax, bnfp[i]);
140 putint(ymax, bnfp[i]);
141 }
142 }
143 return(0);
144 }
145
146
147 int
148 getint(fp) /* get short int from barneyscan file */
149 register FILE *fp;
150 {
151 register short val;
152
153 val = getc(fp);
154 val |= getc(fp) << 8;
155
156 return(val);
157 }
158
159
160 putint(val, fp) /* put short int to barneyscan file */
161 register int val;
162 register FILE *fp;
163 {
164 putc(val&0xff, fp);
165 putc((val >> 8)&0xff, fp);
166 }
167
168
169 ra2bn() /* convert radiance to barneyscan */
170 {
171 register int i;
172 register COLR *inl;
173 int j;
174
175 if ((inl = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
176 quiterr("out of memory");
177 for (j = 0; j < ymax; j++) {
178 if (freadcolrs(inl, xmax, rafp) < 0)
179 quiterr("error reading RADIANCE file");
180 colrs_gambs(inl, xmax);
181 for (i = 0; i < xmax; i++) {
182 putc(inl[i][RED], bnfp[0]);
183 putc(inl[i][GRN], bnfp[1]);
184 putc(inl[i][BLU], bnfp[2]);
185 }
186 if (ferror(bnfp[0]) || ferror(bnfp[1]) || ferror(bnfp[2]))
187 quiterr("error writing Barney files");
188 }
189 free((char *)inl);
190 }
191
192
193 bn2ra() /* convert barneyscan to radiance */
194 {
195 register int i;
196 register COLR *outline;
197 int j;
198
199 if ((outline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
200 quiterr("out of memory");
201 for (j = 0; j < ymax; j++) {
202 for (i = 0; i < xmax; i++) {
203 outline[i][RED] = getc(bnfp[0]);
204 outline[i][GRN] = getc(bnfp[1]);
205 outline[i][BLU] = getc(bnfp[2]);
206 }
207 if (feof(bnfp[0]) || feof(bnfp[1]) || feof(bnfp[2]))
208 quiterr("error reading barney file");
209 gambs_colrs(outline, xmax);
210 if (fwritecolrs(outline, xmax, rafp) < 0)
211 quiterr("error writing RADIANCE file");
212 }
213 free((char *)outline);
214 }