1 |
< |
/* Copyright (c) 1990 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
10 |
|
|
11 |
|
#include <stdio.h> |
12 |
|
|
13 |
+ |
#ifdef MSDOS |
14 |
+ |
#include <fcntl.h> |
15 |
+ |
#endif |
16 |
+ |
|
17 |
+ |
#include <math.h> |
18 |
+ |
|
19 |
|
#include "rasterfile.h" |
20 |
|
|
21 |
|
#include "color.h" |
22 |
|
|
23 |
< |
extern double atof(), pow(); |
23 |
> |
#include "resolu.h" |
24 |
|
|
25 |
< |
double gamma = 2.0; /* gamma correction */ |
25 |
> |
extern char *malloc(); |
26 |
|
|
27 |
+ |
double gamcor = 2.2; /* gamma correction */ |
28 |
+ |
|
29 |
+ |
int bradj = 0; /* brightness adjustment */ |
30 |
+ |
|
31 |
|
char *progname; |
32 |
|
|
33 |
|
int xmax, ymax; |
40 |
|
struct rasterfile head; |
41 |
|
int reverse = 0; |
42 |
|
int i; |
43 |
< |
|
43 |
> |
#ifdef MSDOS |
44 |
> |
extern int _fmode; |
45 |
> |
_fmode = O_BINARY; |
46 |
> |
setmode(fileno(stdin), O_BINARY); |
47 |
> |
setmode(fileno(stdout), O_BINARY); |
48 |
> |
#endif |
49 |
|
progname = argv[0]; |
50 |
|
|
51 |
+ |
head.ras_type = RT_STANDARD; |
52 |
|
for (i = 1; i < argc; i++) |
53 |
|
if (argv[i][0] == '-') |
54 |
|
switch (argv[i][1]) { |
55 |
|
case 'g': |
56 |
< |
gamma = atof(argv[++i]); |
56 |
> |
gamcor = atof(argv[++i]); |
57 |
|
break; |
58 |
+ |
case 'e': |
59 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
60 |
+ |
goto userr; |
61 |
+ |
bradj = atoi(argv[++i]); |
62 |
+ |
break; |
63 |
|
case 'r': |
64 |
< |
reverse = !reverse; |
64 |
> |
if (!strcmp(argv[i], "-rgb")) |
65 |
> |
head.ras_type = RT_FORMAT_RGB; |
66 |
> |
else |
67 |
> |
reverse = 1; |
68 |
|
break; |
69 |
|
default: |
70 |
|
goto userr; |
80 |
|
exit(1); |
81 |
|
} |
82 |
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
83 |
< |
fprintf(stderr, "can't open output \"%s\"\n", |
83 |
> |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
84 |
|
progname, argv[i+1]); |
85 |
|
exit(1); |
86 |
|
} |
87 |
+ |
setcolrgam(gamcor); |
88 |
|
if (reverse) { |
89 |
|
/* get header */ |
90 |
|
if (fread((char *)&head, sizeof(head), 1, stdin) != 1) |
93 |
|
quiterr("bad raster format"); |
94 |
|
xmax = head.ras_width; |
95 |
|
ymax = head.ras_height; |
96 |
< |
if (head.ras_type != RT_STANDARD || |
97 |
< |
head.ras_maptype != RMT_NONE || |
98 |
< |
head.ras_depth != 24) |
96 |
> |
if ((head.ras_type != RT_STANDARD |
97 |
> |
&& head.ras_type != RT_FORMAT_RGB) |
98 |
> |
|| head.ras_maptype != RMT_NONE |
99 |
> |
|| head.ras_depth != 24) |
100 |
|
quiterr("incompatible format"); |
101 |
|
/* put header */ |
102 |
|
printargs(i, argv, stdout); |
103 |
+ |
fputformat(COLRFMT, stdout); |
104 |
|
putchar('\n'); |
105 |
< |
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
105 |
> |
fprtresolu(xmax, ymax, stdout); |
106 |
|
/* convert file */ |
107 |
< |
pr2ra(); |
107 |
> |
pr2ra(head.ras_type, head.ras_length/ymax - xmax*3); |
108 |
|
} else { |
109 |
< |
/* discard input header */ |
110 |
< |
getheader(stdin, NULL); |
111 |
< |
/* get resolution */ |
112 |
< |
if (fgetresolu(&xmax, &ymax, stdin) != (YMAJOR|YDECR)) |
86 |
< |
quiterr("bad picture size"); |
109 |
> |
/* get header info. */ |
110 |
> |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
111 |
> |
fgetresolu(&xmax, &ymax, stdin) < 0) |
112 |
> |
quiterr("bad picture format"); |
113 |
|
/* write rasterfile header */ |
114 |
|
head.ras_magic = RAS_MAGIC; |
115 |
< |
head.ras_width = xmax; |
115 |
> |
head.ras_width = xmax + (xmax&1); |
116 |
|
head.ras_height = ymax; |
117 |
|
head.ras_depth = 24; |
118 |
< |
head.ras_length = xmax*ymax*3; |
93 |
< |
head.ras_type = RT_STANDARD; |
118 |
> |
head.ras_length = head.ras_width*head.ras_height*3; |
119 |
|
head.ras_maptype = RMT_NONE; |
120 |
|
head.ras_maplength = 0; |
121 |
|
fwrite((char *)&head, sizeof(head), 1, stdout); |
122 |
|
/* convert file */ |
123 |
< |
ra2pr(); |
123 |
> |
ra2pr(head.ras_type, head.ras_length/ymax - xmax*3); |
124 |
|
} |
125 |
|
exit(0); |
126 |
|
userr: |
127 |
< |
fprintf(stderr, "Usage: %s [-r][-g gamma] [input [output]]\n", |
127 |
> |
fprintf(stderr, "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n", |
128 |
|
progname); |
129 |
|
exit(1); |
130 |
|
} |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
< |
pr2ra() /* convert 24-bit scanlines to Radiance picture */ |
144 |
> |
pr2ra(rf, pad) /* convert 24-bit scanlines to Radiance picture */ |
145 |
> |
int rf; |
146 |
> |
int pad; |
147 |
|
{ |
148 |
< |
float gmap[256]; |
122 |
< |
int r, g, b; |
123 |
< |
COLOR *scanout; |
148 |
> |
COLR *scanout; |
149 |
|
register int x; |
150 |
|
int y; |
151 |
|
/* allocate scanline */ |
152 |
< |
scanout = (COLOR *)malloc(xmax*sizeof(COLOR)); |
152 |
> |
scanout = (COLR *)malloc(xmax*sizeof(COLR)); |
153 |
|
if (scanout == NULL) |
154 |
|
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); |
155 |
|
/* convert image */ |
156 |
|
for (y = ymax-1; y >= 0; y--) { |
157 |
< |
for (x = 0; x < xmax; x++) { |
158 |
< |
r = getc(stdin); g = getc(stdin); |
159 |
< |
if ((b = getc(stdin)) == EOF) |
160 |
< |
quiterr("error reading rasterfile"); |
161 |
< |
setcolor(scanout[x], gmap[r], gmap[g], gmap[b]); |
162 |
< |
} |
163 |
< |
if (fwritescan(scanout, xmax, stdout) < 0) |
157 |
> |
if (rf == RT_FORMAT_RGB) |
158 |
> |
for (x = 0; x < xmax; x++) { |
159 |
> |
scanout[x][RED] = getc(stdin); |
160 |
> |
scanout[x][GRN] = getc(stdin); |
161 |
> |
scanout[x][BLU] = getc(stdin); |
162 |
> |
} |
163 |
> |
else |
164 |
> |
for (x = 0; x < xmax; x++) { |
165 |
> |
scanout[x][BLU] = getc(stdin); |
166 |
> |
scanout[x][GRN] = getc(stdin); |
167 |
> |
scanout[x][RED] = getc(stdin); |
168 |
> |
} |
169 |
> |
for (x = pad; x--; getc(stdin)); |
170 |
> |
if (feof(stdin) || ferror(stdin)) |
171 |
> |
quiterr("error reading rasterfile"); |
172 |
> |
gambs_colrs(scanout, xmax); |
173 |
> |
if (bradj) |
174 |
> |
shiftcolrs(scanout, xmax, bradj); |
175 |
> |
if (fwritecolrs(scanout, xmax, stdout) < 0) |
176 |
|
quiterr("error writing Radiance picture"); |
177 |
|
} |
178 |
|
/* free scanline */ |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
< |
ra2pr() /* convert Radiance scanlines to 24-bit rasterfile */ |
183 |
> |
ra2pr(rf, pad) /* convert Radiance scanlines to 24-bit rasterfile */ |
184 |
> |
int rf; |
185 |
> |
int pad; |
186 |
|
{ |
187 |
< |
#define map(v) ((v)>=1.0 ? 255 : gmap[(int)(1024.*(v))]) |
188 |
< |
unsigned char gmap[1024]; |
153 |
< |
COLOR *scanin; |
187 |
> |
int ord[3]; |
188 |
> |
COLR *scanin; |
189 |
|
register int x; |
155 |
– |
register int c; |
190 |
|
int y; |
191 |
|
/* allocate scanline */ |
192 |
< |
scanin = (COLOR *)malloc(xmax*sizeof(COLOR)); |
192 |
> |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
193 |
|
if (scanin == NULL) |
194 |
< |
quiterr("out of memory in pr2ra"); |
195 |
< |
/* compute gamma correction */ |
196 |
< |
for (x = 0; x < 1024; x++) |
197 |
< |
gmap[x] = 256.*pow((x+.5)/1024., 1./gamma); |
194 |
> |
quiterr("out of memory in ra2pr"); |
195 |
> |
if (rf == RT_FORMAT_RGB) { |
196 |
> |
ord[0] = RED; ord[1] = GRN; ord[2] = BLU; |
197 |
> |
} else { |
198 |
> |
ord[0] = BLU; ord[1] = GRN; ord[2] = RED; |
199 |
> |
} |
200 |
|
/* convert image */ |
201 |
|
for (y = ymax-1; y >= 0; y--) { |
202 |
< |
if (freadscan(scanin, xmax, stdin) < 0) |
202 |
> |
if (freadcolrs(scanin, xmax, stdin) < 0) |
203 |
|
quiterr("error reading Radiance picture"); |
204 |
< |
for (x = 0; x < xmax; x++) { |
205 |
< |
c = map(colval(scanin[x],RED)); |
206 |
< |
putc(c, stdout); |
207 |
< |
c = map(colval(scanin[x],GRN)); |
208 |
< |
putc(c, stdout); |
209 |
< |
c = map(colval(scanin[x],BLU)); |
210 |
< |
putc(c, stdout); |
211 |
< |
} |
204 |
> |
if (bradj) |
205 |
> |
shiftcolrs(scanin, xmax, bradj); |
206 |
> |
colrs_gambs(scanin, xmax); |
207 |
> |
if (rf == RT_FORMAT_RGB) |
208 |
> |
for (x = 0; x < xmax; x++) { |
209 |
> |
putc(scanin[x][RED], stdout); |
210 |
> |
putc(scanin[x][GRN], stdout); |
211 |
> |
putc(scanin[x][BLU], stdout); |
212 |
> |
} |
213 |
> |
else |
214 |
> |
for (x = 0; x < xmax; x++) { |
215 |
> |
putc(scanin[x][BLU], stdout); |
216 |
> |
putc(scanin[x][GRN], stdout); |
217 |
> |
putc(scanin[x][RED], stdout); |
218 |
> |
} |
219 |
> |
for (x = 0; x < pad; x++) |
220 |
> |
putc(scanin[xmax-1][ord[x%3]], stdout); |
221 |
|
if (ferror(stdout)) |
222 |
|
quiterr("error writing rasterfile"); |
223 |
|
} |
224 |
|
/* free scanline */ |
225 |
|
free((char *)scanin); |
181 |
– |
#undef map |
226 |
|
} |