1 |
greg |
1.7 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
greg |
1.1 |
|
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 |
greg |
1.7 |
int bradj = 0; /* brightness adjustment */ |
22 |
|
|
|
23 |
greg |
1.1 |
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 |
greg |
1.7 |
case 'e': |
45 |
|
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
46 |
|
|
goto userr; |
47 |
|
|
bradj = atoi(argv[++i]); |
48 |
|
|
break; |
49 |
greg |
1.1 |
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 |
greg |
1.3 |
setcolrgam(gamma); |
71 |
greg |
1.1 |
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 |
greg |
1.6 |
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 |
greg |
1.1 |
quiterr("incompatible format"); |
84 |
|
|
/* put header */ |
85 |
|
|
printargs(i, argv, stdout); |
86 |
greg |
1.5 |
fputformat(COLRFMT, stdout); |
87 |
greg |
1.1 |
putchar('\n'); |
88 |
|
|
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
89 |
|
|
/* convert file */ |
90 |
greg |
1.6 |
pr2ra(head.ras_type); |
91 |
greg |
1.1 |
} else { |
92 |
greg |
1.5 |
/* get header info. */ |
93 |
|
|
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
94 |
|
|
fgetresolu(&xmax, &ymax, stdin) != (YMAJOR|YDECR)) |
95 |
|
|
quiterr("bad picture format"); |
96 |
greg |
1.1 |
/* 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 |
greg |
1.7 |
fprintf(stderr, "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n", |
112 |
greg |
1.1 |
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 |
greg |
1.6 |
pr2ra(rf) /* convert 24-bit scanlines to Radiance picture */ |
129 |
|
|
int rf; |
130 |
greg |
1.1 |
{ |
131 |
greg |
1.3 |
COLR *scanout; |
132 |
greg |
1.1 |
register int x; |
133 |
|
|
int y; |
134 |
|
|
/* allocate scanline */ |
135 |
greg |
1.3 |
scanout = (COLR *)malloc(xmax*sizeof(COLR)); |
136 |
greg |
1.1 |
if (scanout == NULL) |
137 |
|
|
quiterr("out of memory in pr2ra"); |
138 |
|
|
/* convert image */ |
139 |
|
|
for (y = ymax-1; y >= 0; y--) { |
140 |
greg |
1.6 |
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 |
greg |
1.3 |
if (feof(stdin) || ferror(stdin)) |
151 |
|
|
quiterr("error reading rasterfile"); |
152 |
|
|
gambs_colrs(scanout, xmax); |
153 |
greg |
1.7 |
if (bradj) |
154 |
|
|
shiftcolrs(scanout, xmax, bradj); |
155 |
greg |
1.3 |
if (fwritecolrs(scanout, xmax, stdout) < 0) |
156 |
greg |
1.1 |
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 |
greg |
1.3 |
COLR *scanin; |
166 |
greg |
1.1 |
register int x; |
167 |
|
|
int y; |
168 |
|
|
/* allocate scanline */ |
169 |
greg |
1.3 |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
170 |
greg |
1.1 |
if (scanin == NULL) |
171 |
greg |
1.8 |
quiterr("out of memory in ra2pr"); |
172 |
greg |
1.1 |
/* convert image */ |
173 |
|
|
for (y = ymax-1; y >= 0; y--) { |
174 |
greg |
1.3 |
if (freadcolrs(scanin, xmax, stdin) < 0) |
175 |
greg |
1.1 |
quiterr("error reading Radiance picture"); |
176 |
greg |
1.7 |
if (bradj) |
177 |
|
|
shiftcolrs(scanin, xmax, bradj); |
178 |
greg |
1.3 |
colrs_gambs(scanin, xmax); |
179 |
greg |
1.1 |
for (x = 0; x < xmax; x++) { |
180 |
greg |
1.4 |
putc(scanin[x][BLU], stdout); |
181 |
|
|
putc(scanin[x][GRN], stdout); |
182 |
greg |
1.3 |
putc(scanin[x][RED], stdout); |
183 |
greg |
1.1 |
} |
184 |
|
|
if (ferror(stdout)) |
185 |
|
|
quiterr("error writing rasterfile"); |
186 |
|
|
} |
187 |
|
|
/* free scanline */ |
188 |
|
|
free((char *)scanin); |
189 |
|
|
} |