1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* program to convert between RADIANCE and 24-bit rasterfiles. |
6 |
|
*/ |
7 |
|
|
8 |
|
#include <stdio.h> |
9 |
+ |
#include <time.h> |
10 |
+ |
#include <math.h> |
11 |
+ |
#include <string.h> |
12 |
|
|
13 |
+ |
#include "platform.h" |
14 |
|
#include "rasterfile.h" |
14 |
– |
|
15 |
|
#include "color.h" |
16 |
+ |
#include "resolu.h" |
17 |
|
|
18 |
< |
extern double atof(), pow(); |
18 |
> |
double gamcor = 2.2; /* gamma correction */ |
19 |
|
|
19 |
– |
double gamma = 2.0; /* gamma correction */ |
20 |
– |
|
20 |
|
int bradj = 0; /* brightness adjustment */ |
21 |
|
|
22 |
|
char *progname; |
31 |
|
struct rasterfile head; |
32 |
|
int reverse = 0; |
33 |
|
int i; |
34 |
< |
|
34 |
> |
SET_DEFAULT_BINARY(); |
35 |
> |
SET_FILE_BINARY(stdin); |
36 |
> |
SET_FILE_BINARY(stdout); |
37 |
|
progname = argv[0]; |
38 |
|
|
39 |
+ |
head.ras_type = RT_STANDARD; |
40 |
|
for (i = 1; i < argc; i++) |
41 |
|
if (argv[i][0] == '-') |
42 |
|
switch (argv[i][1]) { |
43 |
|
case 'g': |
44 |
< |
gamma = atof(argv[++i]); |
44 |
> |
gamcor = atof(argv[++i]); |
45 |
|
break; |
46 |
|
case 'e': |
47 |
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
49 |
|
bradj = atoi(argv[++i]); |
50 |
|
break; |
51 |
|
case 'r': |
52 |
< |
reverse = !reverse; |
52 |
> |
if (!strcmp(argv[i], "-rgb")) |
53 |
> |
head.ras_type = RT_FORMAT_RGB; |
54 |
> |
else |
55 |
> |
reverse = 1; |
56 |
|
break; |
57 |
|
default: |
58 |
|
goto userr; |
68 |
|
exit(1); |
69 |
|
} |
70 |
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
71 |
< |
fprintf(stderr, "can't open output \"%s\"\n", |
71 |
> |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
72 |
|
progname, argv[i+1]); |
73 |
|
exit(1); |
74 |
|
} |
75 |
< |
setcolrgam(gamma); |
75 |
> |
setcolrgam(gamcor); |
76 |
|
if (reverse) { |
77 |
|
/* get header */ |
78 |
|
if (fread((char *)&head, sizeof(head), 1, stdin) != 1) |
87 |
|
|| head.ras_depth != 24) |
88 |
|
quiterr("incompatible format"); |
89 |
|
/* put header */ |
90 |
+ |
newheader("RADIANCE", stdout); |
91 |
|
printargs(i, argv, stdout); |
92 |
|
fputformat(COLRFMT, stdout); |
93 |
|
putchar('\n'); |
94 |
< |
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
94 |
> |
fprtresolu(xmax, ymax, stdout); |
95 |
|
/* convert file */ |
96 |
< |
pr2ra(head.ras_type); |
96 |
> |
pr2ra(head.ras_type, head.ras_length/ymax - xmax*3); |
97 |
|
} else { |
98 |
|
/* get header info. */ |
99 |
|
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
100 |
< |
fgetresolu(&xmax, &ymax, stdin) != (YMAJOR|YDECR)) |
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; |
104 |
> |
head.ras_width = xmax + (xmax&1); |
105 |
|
head.ras_height = ymax; |
106 |
|
head.ras_depth = 24; |
107 |
< |
head.ras_length = xmax*ymax*3; |
102 |
< |
head.ras_type = RT_STANDARD; |
107 |
> |
head.ras_length = head.ras_width*head.ras_height*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(); |
112 |
> |
ra2pr(head.ras_type, head.ras_length/ymax - xmax*3); |
113 |
|
} |
114 |
|
exit(0); |
115 |
|
userr: |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
< |
pr2ra(rf) /* convert 24-bit scanlines to Radiance picture */ |
133 |
> |
pr2ra(rf, pad) /* convert 24-bit scanlines to Radiance picture */ |
134 |
|
int rf; |
135 |
+ |
int pad; |
136 |
|
{ |
137 |
|
COLR *scanout; |
138 |
|
register int x; |
143 |
|
quiterr("out of memory in pr2ra"); |
144 |
|
/* convert image */ |
145 |
|
for (y = ymax-1; y >= 0; y--) { |
146 |
< |
for (x = 0; x < xmax; x++) |
147 |
< |
if (rf == RT_FORMAT_RGB) { |
146 |
> |
if (rf == RT_FORMAT_RGB) |
147 |
> |
for (x = 0; x < xmax; x++) { |
148 |
|
scanout[x][RED] = getc(stdin); |
149 |
|
scanout[x][GRN] = getc(stdin); |
150 |
|
scanout[x][BLU] = getc(stdin); |
151 |
< |
} else { |
151 |
> |
} |
152 |
> |
else |
153 |
> |
for (x = 0; x < xmax; x++) { |
154 |
|
scanout[x][BLU] = getc(stdin); |
155 |
|
scanout[x][GRN] = getc(stdin); |
156 |
|
scanout[x][RED] = getc(stdin); |
157 |
|
} |
158 |
+ |
for (x = pad; x--; getc(stdin)); |
159 |
|
if (feof(stdin) || ferror(stdin)) |
160 |
|
quiterr("error reading rasterfile"); |
161 |
|
gambs_colrs(scanout, xmax); |
165 |
|
quiterr("error writing Radiance picture"); |
166 |
|
} |
167 |
|
/* free scanline */ |
168 |
< |
free((char *)scanout); |
168 |
> |
free((void *)scanout); |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
< |
ra2pr() /* convert Radiance scanlines to 24-bit rasterfile */ |
172 |
> |
ra2pr(rf, pad) /* convert Radiance scanlines to 24-bit rasterfile */ |
173 |
> |
int rf; |
174 |
> |
int pad; |
175 |
|
{ |
176 |
+ |
int ord[3]; |
177 |
|
COLR *scanin; |
178 |
|
register int x; |
179 |
|
int y; |
181 |
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
182 |
|
if (scanin == NULL) |
183 |
|
quiterr("out of memory in ra2pr"); |
184 |
+ |
if (rf == RT_FORMAT_RGB) { |
185 |
+ |
ord[0] = RED; ord[1] = GRN; ord[2] = BLU; |
186 |
+ |
} else { |
187 |
+ |
ord[0] = BLU; ord[1] = GRN; ord[2] = RED; |
188 |
+ |
} |
189 |
|
/* convert image */ |
190 |
|
for (y = ymax-1; y >= 0; y--) { |
191 |
|
if (freadcolrs(scanin, xmax, stdin) < 0) |
193 |
|
if (bradj) |
194 |
|
shiftcolrs(scanin, xmax, bradj); |
195 |
|
colrs_gambs(scanin, xmax); |
196 |
< |
for (x = 0; x < xmax; x++) { |
197 |
< |
putc(scanin[x][BLU], stdout); |
198 |
< |
putc(scanin[x][GRN], stdout); |
199 |
< |
putc(scanin[x][RED], stdout); |
200 |
< |
} |
196 |
> |
if (rf == RT_FORMAT_RGB) |
197 |
> |
for (x = 0; x < xmax; x++) { |
198 |
> |
putc(scanin[x][RED], stdout); |
199 |
> |
putc(scanin[x][GRN], stdout); |
200 |
> |
putc(scanin[x][BLU], stdout); |
201 |
> |
} |
202 |
> |
else |
203 |
> |
for (x = 0; x < xmax; x++) { |
204 |
> |
putc(scanin[x][BLU], stdout); |
205 |
> |
putc(scanin[x][GRN], stdout); |
206 |
> |
putc(scanin[x][RED], stdout); |
207 |
> |
} |
208 |
> |
for (x = 0; x < pad; x++) |
209 |
> |
putc(scanin[xmax-1][ord[x%3]], stdout); |
210 |
|
if (ferror(stdout)) |
211 |
|
quiterr("error writing rasterfile"); |
212 |
|
} |
213 |
|
/* free scanline */ |
214 |
< |
free((char *)scanin); |
214 |
> |
free((void *)scanin); |
215 |
|
} |