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 |
#include "resolu.h" |
18 |
|
19 |
extern double pow(); |
20 |
|
21 |
double gamma = 2.2; /* gamma correction */ |
22 |
|
23 |
int bradj = 0; /* brightness adjustment */ |
24 |
|
25 |
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 |
head.ras_type = RT_STANDARD; |
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 'e': |
48 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
49 |
goto userr; |
50 |
bradj = atoi(argv[++i]); |
51 |
break; |
52 |
case 'r': |
53 |
if (!strcmp(argv[i], "-rgb")) |
54 |
head.ras_type = RT_FORMAT_RGB; |
55 |
else |
56 |
reverse = 1; |
57 |
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 |
setcolrgam(gamma); |
77 |
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 |
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 |
quiterr("incompatible format"); |
90 |
/* put header */ |
91 |
printargs(i, argv, stdout); |
92 |
fputformat(COLRFMT, stdout); |
93 |
putchar('\n'); |
94 |
fprtresolu(xmax, ymax, stdout); |
95 |
/* convert file */ |
96 |
pr2ra(head.ras_type); |
97 |
} else { |
98 |
/* get header info. */ |
99 |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
100 |
fgetresolu(&xmax, &ymax, stdin) < 0) |
101 |
quiterr("bad picture format"); |
102 |
/* 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 |
ra2pr(head.ras_type); |
113 |
} |
114 |
exit(0); |
115 |
userr: |
116 |
fprintf(stderr, "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n", |
117 |
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 |
pr2ra(rf) /* convert 24-bit scanlines to Radiance picture */ |
134 |
int rf; |
135 |
{ |
136 |
COLR *scanout; |
137 |
register int x; |
138 |
int y; |
139 |
/* allocate scanline */ |
140 |
scanout = (COLR *)malloc(xmax*sizeof(COLR)); |
141 |
if (scanout == NULL) |
142 |
quiterr("out of memory in pr2ra"); |
143 |
/* convert image */ |
144 |
for (y = ymax-1; y >= 0; y--) { |
145 |
if (rf == RT_FORMAT_RGB) |
146 |
for (x = 0; x < xmax; x++) { |
147 |
scanout[x][RED] = getc(stdin); |
148 |
scanout[x][GRN] = getc(stdin); |
149 |
scanout[x][BLU] = getc(stdin); |
150 |
} |
151 |
else |
152 |
for (x = 0; x < xmax; x++) { |
153 |
scanout[x][BLU] = getc(stdin); |
154 |
scanout[x][GRN] = getc(stdin); |
155 |
scanout[x][RED] = getc(stdin); |
156 |
} |
157 |
if (feof(stdin) || ferror(stdin)) |
158 |
quiterr("error reading rasterfile"); |
159 |
gambs_colrs(scanout, xmax); |
160 |
if (bradj) |
161 |
shiftcolrs(scanout, xmax, bradj); |
162 |
if (fwritecolrs(scanout, xmax, stdout) < 0) |
163 |
quiterr("error writing Radiance picture"); |
164 |
} |
165 |
/* free scanline */ |
166 |
free((char *)scanout); |
167 |
} |
168 |
|
169 |
|
170 |
ra2pr(rf) /* convert Radiance scanlines to 24-bit rasterfile */ |
171 |
int rf; |
172 |
{ |
173 |
COLR *scanin; |
174 |
register int x; |
175 |
int y; |
176 |
/* allocate scanline */ |
177 |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
178 |
if (scanin == NULL) |
179 |
quiterr("out of memory in ra2pr"); |
180 |
/* convert image */ |
181 |
for (y = ymax-1; y >= 0; y--) { |
182 |
if (freadcolrs(scanin, xmax, stdin) < 0) |
183 |
quiterr("error reading Radiance picture"); |
184 |
if (bradj) |
185 |
shiftcolrs(scanin, xmax, bradj); |
186 |
colrs_gambs(scanin, xmax); |
187 |
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 |
if (ferror(stdout)) |
200 |
quiterr("error writing rasterfile"); |
201 |
} |
202 |
/* free scanline */ |
203 |
free((char *)scanin); |
204 |
} |