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