1 |
greg |
1.1 |
/* Copyright (c) 1990 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 |
|
|
char *progname; |
22 |
|
|
|
23 |
|
|
int xmax, ymax; |
24 |
|
|
|
25 |
|
|
|
26 |
|
|
main(argc, argv) |
27 |
|
|
int argc; |
28 |
|
|
char *argv[]; |
29 |
|
|
{ |
30 |
|
|
struct rasterfile head; |
31 |
|
|
int reverse = 0; |
32 |
|
|
int i; |
33 |
|
|
|
34 |
|
|
progname = argv[0]; |
35 |
|
|
|
36 |
|
|
for (i = 1; i < argc; i++) |
37 |
|
|
if (argv[i][0] == '-') |
38 |
|
|
switch (argv[i][1]) { |
39 |
|
|
case 'g': |
40 |
|
|
gamma = atof(argv[++i]); |
41 |
|
|
break; |
42 |
|
|
case 'r': |
43 |
|
|
reverse = !reverse; |
44 |
|
|
break; |
45 |
|
|
default: |
46 |
|
|
goto userr; |
47 |
|
|
} |
48 |
|
|
else |
49 |
|
|
break; |
50 |
|
|
|
51 |
|
|
if (i < argc-2) |
52 |
|
|
goto userr; |
53 |
|
|
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
54 |
|
|
fprintf(stderr, "%s: can't open input \"%s\"\n", |
55 |
|
|
progname, argv[i]); |
56 |
|
|
exit(1); |
57 |
|
|
} |
58 |
|
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
59 |
|
|
fprintf(stderr, "can't open output \"%s\"\n", |
60 |
|
|
progname, argv[i+1]); |
61 |
|
|
exit(1); |
62 |
|
|
} |
63 |
|
|
if (reverse) { |
64 |
|
|
/* get header */ |
65 |
|
|
if (fread((char *)&head, sizeof(head), 1, stdin) != 1) |
66 |
|
|
quiterr("missing header"); |
67 |
|
|
if (head.ras_magic != RAS_MAGIC) |
68 |
|
|
quiterr("bad raster format"); |
69 |
|
|
xmax = head.ras_width; |
70 |
|
|
ymax = head.ras_height; |
71 |
|
|
if (head.ras_type != RT_STANDARD || |
72 |
|
|
head.ras_maptype != RMT_NONE || |
73 |
|
|
head.ras_depth != 24) |
74 |
|
|
quiterr("incompatible format"); |
75 |
|
|
/* put header */ |
76 |
|
|
printargs(i, argv, stdout); |
77 |
|
|
putchar('\n'); |
78 |
|
|
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
79 |
|
|
/* convert file */ |
80 |
|
|
pr2ra(); |
81 |
|
|
} else { |
82 |
|
|
/* discard input header */ |
83 |
|
|
getheader(stdin, NULL); |
84 |
|
|
/* get resolution */ |
85 |
|
|
if (fgetresolu(&xmax, &ymax, stdin) != (YMAJOR|YDECR)) |
86 |
|
|
quiterr("bad picture size"); |
87 |
|
|
/* write rasterfile header */ |
88 |
|
|
head.ras_magic = RAS_MAGIC; |
89 |
|
|
head.ras_width = xmax; |
90 |
|
|
head.ras_height = ymax; |
91 |
|
|
head.ras_depth = 24; |
92 |
|
|
head.ras_length = xmax*ymax*3; |
93 |
|
|
head.ras_type = RT_STANDARD; |
94 |
|
|
head.ras_maptype = RMT_NONE; |
95 |
|
|
head.ras_maplength = 0; |
96 |
|
|
fwrite((char *)&head, sizeof(head), 1, stdout); |
97 |
|
|
/* convert file */ |
98 |
|
|
ra2pr(); |
99 |
|
|
} |
100 |
|
|
exit(0); |
101 |
|
|
userr: |
102 |
|
|
fprintf(stderr, "Usage: %s [-r][-g gamma] [input [output]]\n", |
103 |
|
|
progname); |
104 |
|
|
exit(1); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
|
108 |
|
|
quiterr(err) /* print message and exit */ |
109 |
|
|
char *err; |
110 |
|
|
{ |
111 |
|
|
if (err != NULL) { |
112 |
|
|
fprintf(stderr, "%s: %s\n", progname, err); |
113 |
|
|
exit(1); |
114 |
|
|
} |
115 |
|
|
exit(0); |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
|
119 |
|
|
pr2ra() /* convert 24-bit scanlines to Radiance picture */ |
120 |
|
|
{ |
121 |
|
|
float gmap[256]; |
122 |
|
|
int r, g, b; |
123 |
|
|
COLOR *scanout; |
124 |
|
|
register int x; |
125 |
|
|
int y; |
126 |
|
|
/* allocate scanline */ |
127 |
|
|
scanout = (COLOR *)malloc(xmax*sizeof(COLOR)); |
128 |
|
|
if (scanout == NULL) |
129 |
|
|
quiterr("out of memory in pr2ra"); |
130 |
|
|
/* compute gamma correction */ |
131 |
|
|
for (x = 0; x < 256; x++) |
132 |
|
|
gmap[x] = pow((x+.5)/256., gamma); |
133 |
|
|
/* convert image */ |
134 |
|
|
for (y = ymax-1; y >= 0; y--) { |
135 |
|
|
for (x = 0; x < xmax; x++) { |
136 |
|
|
r = getc(stdin); g = getc(stdin); |
137 |
|
|
if ((b = getc(stdin)) == EOF) |
138 |
|
|
quiterr("error reading rasterfile"); |
139 |
|
|
setcolor(scanout[x], gmap[r], gmap[g], gmap[b]); |
140 |
|
|
} |
141 |
|
|
if (fwritescan(scanout, xmax, stdout) < 0) |
142 |
|
|
quiterr("error writing Radiance picture"); |
143 |
|
|
} |
144 |
|
|
/* free scanline */ |
145 |
|
|
free((char *)scanout); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
|
149 |
|
|
ra2pr() /* convert Radiance scanlines to 24-bit rasterfile */ |
150 |
|
|
{ |
151 |
|
|
#define map(v) ((v)>=1.0 ? 255 : gmap[(int)(1024.*(v))]) |
152 |
|
|
unsigned char gmap[1024]; |
153 |
|
|
COLOR *scanin; |
154 |
|
|
register int x; |
155 |
|
|
register int c; |
156 |
|
|
int y; |
157 |
|
|
/* allocate scanline */ |
158 |
|
|
scanin = (COLOR *)malloc(xmax*sizeof(COLOR)); |
159 |
|
|
if (scanin == NULL) |
160 |
|
|
quiterr("out of memory in pr2ra"); |
161 |
|
|
/* compute gamma correction */ |
162 |
|
|
for (x = 0; x < 256; x++) |
163 |
|
|
gmap[x] = 256.*pow((x+.5)/1024., 1./gamma); |
164 |
|
|
/* convert image */ |
165 |
|
|
for (y = ymax-1; y >= 0; y--) { |
166 |
|
|
if (freadscan(scanin, xmax, stdin) < 0) |
167 |
|
|
quiterr("error reading Radiance picture"); |
168 |
|
|
for (x = 0; x < xmax; x++) { |
169 |
|
|
c = map(colval(scanin[x],RED)); |
170 |
|
|
putc(c, stdout); |
171 |
|
|
c = map(colval(scanin[x],GRN)); |
172 |
|
|
putc(c, stdout); |
173 |
|
|
c = map(colval(scanin[x],BLU)); |
174 |
|
|
putc(c, stdout); |
175 |
|
|
} |
176 |
|
|
if (ferror(stdout)) |
177 |
|
|
quiterr("error writing rasterfile"); |
178 |
|
|
} |
179 |
|
|
/* free scanline */ |
180 |
|
|
free((char *)scanin); |
181 |
|
|
#undef map |
182 |
|
|
} |