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