ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_bn.c
Revision: 1.8
Committed: Wed Aug 7 08:36:29 1991 UTC (32 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.7: +15 -3 lines
Log Message:
added -e +/-stops option

File Contents

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