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