1 |
greg |
1.7 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
greg |
1.1 |
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* program to convert between RADIANCE and 24-bit rasterfiles. |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include <stdio.h> |
12 |
|
|
|
13 |
|
|
#include "rasterfile.h" |
14 |
|
|
|
15 |
|
|
#include "color.h" |
16 |
|
|
|
17 |
greg |
1.11 |
#include "resolu.h" |
18 |
|
|
|
19 |
greg |
2.2 |
extern double pow(); |
20 |
greg |
1.1 |
|
21 |
greg |
1.10 |
double gamma = 2.2; /* gamma correction */ |
22 |
greg |
1.1 |
|
23 |
greg |
1.7 |
int bradj = 0; /* brightness adjustment */ |
24 |
|
|
|
25 |
greg |
1.1 |
char *progname; |
26 |
|
|
|
27 |
|
|
int xmax, ymax; |
28 |
|
|
|
29 |
|
|
|
30 |
|
|
main(argc, argv) |
31 |
|
|
int argc; |
32 |
|
|
char *argv[]; |
33 |
|
|
{ |
34 |
|
|
struct rasterfile head; |
35 |
|
|
int reverse = 0; |
36 |
|
|
int i; |
37 |
|
|
|
38 |
|
|
progname = argv[0]; |
39 |
|
|
|
40 |
greg |
1.9 |
head.ras_type = RT_STANDARD; |
41 |
greg |
1.1 |
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 |
greg |
1.7 |
case 'e': |
48 |
|
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
49 |
|
|
goto userr; |
50 |
|
|
bradj = atoi(argv[++i]); |
51 |
|
|
break; |
52 |
greg |
1.1 |
case 'r': |
53 |
greg |
1.9 |
if (!strcmp(argv[i], "-rgb")) |
54 |
|
|
head.ras_type = RT_FORMAT_RGB; |
55 |
|
|
else |
56 |
|
|
reverse = 1; |
57 |
greg |
1.1 |
break; |
58 |
|
|
default: |
59 |
|
|
goto userr; |
60 |
|
|
} |
61 |
|
|
else |
62 |
|
|
break; |
63 |
|
|
|
64 |
|
|
if (i < argc-2) |
65 |
|
|
goto userr; |
66 |
|
|
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
67 |
|
|
fprintf(stderr, "%s: can't open input \"%s\"\n", |
68 |
|
|
progname, argv[i]); |
69 |
|
|
exit(1); |
70 |
|
|
} |
71 |
|
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
72 |
|
|
fprintf(stderr, "can't open output \"%s\"\n", |
73 |
|
|
progname, argv[i+1]); |
74 |
|
|
exit(1); |
75 |
|
|
} |
76 |
greg |
1.3 |
setcolrgam(gamma); |
77 |
greg |
1.1 |
if (reverse) { |
78 |
|
|
/* get header */ |
79 |
|
|
if (fread((char *)&head, sizeof(head), 1, stdin) != 1) |
80 |
|
|
quiterr("missing header"); |
81 |
|
|
if (head.ras_magic != RAS_MAGIC) |
82 |
|
|
quiterr("bad raster format"); |
83 |
|
|
xmax = head.ras_width; |
84 |
|
|
ymax = head.ras_height; |
85 |
greg |
1.6 |
if ((head.ras_type != RT_STANDARD |
86 |
|
|
&& head.ras_type != RT_FORMAT_RGB) |
87 |
|
|
|| head.ras_maptype != RMT_NONE |
88 |
|
|
|| head.ras_depth != 24) |
89 |
greg |
1.1 |
quiterr("incompatible format"); |
90 |
|
|
/* put header */ |
91 |
|
|
printargs(i, argv, stdout); |
92 |
greg |
1.5 |
fputformat(COLRFMT, stdout); |
93 |
greg |
1.1 |
putchar('\n'); |
94 |
greg |
1.11 |
fprtresolu(xmax, ymax, stdout); |
95 |
greg |
1.1 |
/* convert file */ |
96 |
greg |
1.6 |
pr2ra(head.ras_type); |
97 |
greg |
1.1 |
} else { |
98 |
greg |
1.5 |
/* get header info. */ |
99 |
|
|
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
100 |
greg |
1.11 |
fgetresolu(&xmax, &ymax, stdin) < 0) |
101 |
greg |
1.5 |
quiterr("bad picture format"); |
102 |
greg |
1.1 |
/* write rasterfile header */ |
103 |
|
|
head.ras_magic = RAS_MAGIC; |
104 |
|
|
head.ras_width = xmax; |
105 |
|
|
head.ras_height = ymax; |
106 |
|
|
head.ras_depth = 24; |
107 |
|
|
head.ras_length = xmax*ymax*3; |
108 |
|
|
head.ras_maptype = RMT_NONE; |
109 |
|
|
head.ras_maplength = 0; |
110 |
|
|
fwrite((char *)&head, sizeof(head), 1, stdout); |
111 |
|
|
/* convert file */ |
112 |
greg |
1.9 |
ra2pr(head.ras_type); |
113 |
greg |
1.1 |
} |
114 |
|
|
exit(0); |
115 |
|
|
userr: |
116 |
greg |
1.7 |
fprintf(stderr, "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n", |
117 |
greg |
1.1 |
progname); |
118 |
|
|
exit(1); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
|
122 |
|
|
quiterr(err) /* print message and exit */ |
123 |
|
|
char *err; |
124 |
|
|
{ |
125 |
|
|
if (err != NULL) { |
126 |
|
|
fprintf(stderr, "%s: %s\n", progname, err); |
127 |
|
|
exit(1); |
128 |
|
|
} |
129 |
|
|
exit(0); |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
|
133 |
greg |
1.6 |
pr2ra(rf) /* convert 24-bit scanlines to Radiance picture */ |
134 |
|
|
int rf; |
135 |
greg |
1.1 |
{ |
136 |
greg |
1.3 |
COLR *scanout; |
137 |
greg |
1.1 |
register int x; |
138 |
|
|
int y; |
139 |
|
|
/* allocate scanline */ |
140 |
greg |
1.3 |
scanout = (COLR *)malloc(xmax*sizeof(COLR)); |
141 |
greg |
1.1 |
if (scanout == NULL) |
142 |
|
|
quiterr("out of memory in pr2ra"); |
143 |
|
|
/* convert image */ |
144 |
|
|
for (y = ymax-1; y >= 0; y--) { |
145 |
greg |
1.9 |
if (rf == RT_FORMAT_RGB) |
146 |
|
|
for (x = 0; x < xmax; x++) { |
147 |
greg |
1.6 |
scanout[x][RED] = getc(stdin); |
148 |
|
|
scanout[x][GRN] = getc(stdin); |
149 |
|
|
scanout[x][BLU] = getc(stdin); |
150 |
greg |
1.9 |
} |
151 |
|
|
else |
152 |
|
|
for (x = 0; x < xmax; x++) { |
153 |
greg |
1.6 |
scanout[x][BLU] = getc(stdin); |
154 |
|
|
scanout[x][GRN] = getc(stdin); |
155 |
|
|
scanout[x][RED] = getc(stdin); |
156 |
|
|
} |
157 |
greg |
1.3 |
if (feof(stdin) || ferror(stdin)) |
158 |
|
|
quiterr("error reading rasterfile"); |
159 |
|
|
gambs_colrs(scanout, xmax); |
160 |
greg |
1.7 |
if (bradj) |
161 |
|
|
shiftcolrs(scanout, xmax, bradj); |
162 |
greg |
1.3 |
if (fwritecolrs(scanout, xmax, stdout) < 0) |
163 |
greg |
1.1 |
quiterr("error writing Radiance picture"); |
164 |
|
|
} |
165 |
|
|
/* free scanline */ |
166 |
|
|
free((char *)scanout); |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
|
170 |
greg |
1.9 |
ra2pr(rf) /* convert Radiance scanlines to 24-bit rasterfile */ |
171 |
|
|
int rf; |
172 |
greg |
1.1 |
{ |
173 |
greg |
1.3 |
COLR *scanin; |
174 |
greg |
1.1 |
register int x; |
175 |
|
|
int y; |
176 |
|
|
/* allocate scanline */ |
177 |
greg |
1.3 |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
178 |
greg |
1.1 |
if (scanin == NULL) |
179 |
greg |
1.8 |
quiterr("out of memory in ra2pr"); |
180 |
greg |
1.1 |
/* convert image */ |
181 |
|
|
for (y = ymax-1; y >= 0; y--) { |
182 |
greg |
1.3 |
if (freadcolrs(scanin, xmax, stdin) < 0) |
183 |
greg |
1.1 |
quiterr("error reading Radiance picture"); |
184 |
greg |
1.7 |
if (bradj) |
185 |
|
|
shiftcolrs(scanin, xmax, bradj); |
186 |
greg |
1.3 |
colrs_gambs(scanin, xmax); |
187 |
greg |
1.9 |
if (rf == RT_FORMAT_RGB) |
188 |
|
|
for (x = 0; x < xmax; x++) { |
189 |
|
|
putc(scanin[x][RED], stdout); |
190 |
|
|
putc(scanin[x][GRN], stdout); |
191 |
|
|
putc(scanin[x][BLU], stdout); |
192 |
|
|
} |
193 |
|
|
else |
194 |
|
|
for (x = 0; x < xmax; x++) { |
195 |
|
|
putc(scanin[x][BLU], stdout); |
196 |
|
|
putc(scanin[x][GRN], stdout); |
197 |
|
|
putc(scanin[x][RED], stdout); |
198 |
|
|
} |
199 |
greg |
1.1 |
if (ferror(stdout)) |
200 |
|
|
quiterr("error writing rasterfile"); |
201 |
|
|
} |
202 |
|
|
/* free scanline */ |
203 |
|
|
free((char *)scanin); |
204 |
|
|
} |