1 |
– |
/* Copyright (c) 1986 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 |
|
* ra_pr.c - program to convert between RADIANCE and pixrect picture format. |
6 |
|
* |
10 |
|
|
11 |
|
#include <stdio.h> |
12 |
|
|
13 |
+ |
#include <math.h> |
14 |
+ |
|
15 |
+ |
#include <time.h> |
16 |
+ |
|
17 |
|
#include "rasterfile.h" |
18 |
|
|
19 |
|
#include "color.h" |
20 |
|
|
21 |
+ |
#include "resolu.h" |
22 |
+ |
|
23 |
|
#include "pic.h" |
24 |
|
|
25 |
|
/* descriptor for a picture file or frame buffer */ |
40 |
|
|
41 |
|
extern long ftell(); |
42 |
|
|
43 |
< |
extern double atof(), pow(); |
43 |
> |
double gamcor = 2.2; /* gamma correction */ |
44 |
|
|
45 |
< |
double gamma = 2.0; /* gamma correction */ |
45 |
> |
int bradj = 0; /* brightness adjustment */ |
46 |
|
|
47 |
|
pic *inpic, *outpic; |
48 |
|
|
50 |
|
|
51 |
|
char errmsg[128]; |
52 |
|
|
53 |
< |
COLR *inline; |
53 |
> |
COLR *inl; |
54 |
|
|
55 |
|
int xmax, ymax; |
56 |
|
|
76 |
|
dither = !dither; |
77 |
|
break; |
78 |
|
case 'g': |
79 |
< |
gamma = atof(argv[++i]); |
79 |
> |
gamcor = atof(argv[++i]); |
80 |
|
break; |
81 |
|
case 'b': |
82 |
|
greyscale = !greyscale; |
83 |
|
break; |
84 |
+ |
case 'e': |
85 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
86 |
+ |
goto userr; |
87 |
+ |
bradj = atoi(argv[++i]); |
88 |
+ |
break; |
89 |
|
case 'r': |
90 |
|
reverse = !reverse; |
91 |
|
break; |
110 |
|
quiterr(errmsg); |
111 |
|
} |
112 |
|
/* get header */ |
113 |
< |
if (fread(&head, sizeof(head), 1, stdin) != 1) |
113 |
> |
if (fread((char *)&head, sizeof(head), 1, stdin) != 1) |
114 |
|
quiterr("missing header"); |
115 |
|
if (head.ras_magic != RAS_MAGIC) |
116 |
|
quiterr("bad raster format"); |
121 |
|
head.ras_depth != 8) |
122 |
|
quiterr("incompatible format"); |
123 |
|
/* put header */ |
124 |
< |
printargs(argc, argv, stdout); |
124 |
> |
newheader("RADIANCE", stdout); |
125 |
> |
printargs(i, argv, stdout); |
126 |
> |
fputformat(COLRFMT, stdout); |
127 |
|
putchar('\n'); |
128 |
< |
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
128 |
> |
fprtresolu(xmax, ymax, stdout); |
129 |
|
/* convert file */ |
130 |
|
pr2ra(&head); |
131 |
|
} else { |
132 |
< |
if (i > argc-1 || i < argc-2) |
132 |
> |
if (i < argc-2 || (!greyscale && i > argc-1)) |
133 |
|
goto userr; |
134 |
|
if ((inpic = openinput(argv[i], &head)) == NULL) { |
135 |
|
sprintf(errmsg, "can't open input \"%s\"", argv[i]); |
149 |
|
quiterr(NULL); |
150 |
|
userr: |
151 |
|
fprintf(stderr, |
152 |
< |
"Usage: %s [-d][-c ncolors][-b][-g gamma] input [output]\n", |
152 |
> |
"Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n", |
153 |
|
progname); |
154 |
< |
fprintf(stderr, " Or: %s -r [-g gamma] [input [output]]\n", |
154 |
> |
fprintf(stderr, " Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n", |
155 |
|
progname); |
156 |
|
exit(1); |
157 |
|
} |
168 |
|
} |
169 |
|
|
170 |
|
|
171 |
+ |
void |
172 |
|
eputs(s) |
173 |
|
char *s; |
174 |
|
{ |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
+ |
void |
180 |
|
quit(code) |
181 |
|
int code; |
182 |
|
{ |
197 |
|
p->fp = stdin; |
198 |
|
else if ((p->fp = fopen(fname, "r")) == NULL) |
199 |
|
return(NULL); |
200 |
< |
/* discard header */ |
201 |
< |
getheader(p->fp, NULL); |
202 |
< |
if (fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR)) |
203 |
< |
quiterr("bad picture size"); |
200 |
> |
/* check header */ |
201 |
> |
if (checkheader(p->fp, COLRFMT, NULL) < 0 || |
202 |
> |
fgetresolu(&xmax, &ymax, p->fp) < 0) |
203 |
> |
quiterr("bad picture format"); |
204 |
|
p->nexty = 0; |
205 |
|
p->bytes_line = 0; /* variable length lines */ |
206 |
|
p->pos.y = (long *)ecalloc(ymax, sizeof(long)); |
215 |
|
h->ras_maptype = RMT_EQUAL_RGB; |
216 |
|
h->ras_maplength = 256*3; |
217 |
|
/* allocate scanline */ |
218 |
< |
inline = (COLR *)emalloc(xmax*sizeof(COLR)); |
218 |
> |
inl = (COLR *)emalloc(xmax*sizeof(COLR)); |
219 |
|
|
220 |
|
return(p); |
221 |
|
} |
235 |
|
else if ((p->fp = fopen(fname, "w")) == NULL) |
236 |
|
return(NULL); |
237 |
|
/* write header */ |
238 |
< |
fwrite(h, sizeof(*h), 1, p->fp); |
238 |
> |
fwrite((char *)h, sizeof(*h), 1, p->fp); |
239 |
|
p->nexty = -1; /* needs color map */ |
240 |
|
p->bytes_line = h->ras_width; |
241 |
|
p->pos.b = 0; |
255 |
|
scanline = (COLR *)emalloc(xmax*sizeof(COLR)); |
256 |
|
/* get color table */ |
257 |
|
for (i = 0; i < 3; i ++) |
258 |
< |
if (fread(cmap[i], h->ras_maplength/3, 1, stdin) != 1) |
258 |
> |
if (fread((char *)cmap[i], h->ras_maplength/3, 1, stdin) != 1) |
259 |
|
quiterr("error reading color table"); |
260 |
|
/* convert table */ |
261 |
|
for (i = 0; i < h->ras_maplength/3; i++) |
262 |
|
setcolr(ctab[i], |
263 |
< |
pow((cmap[0][i]+.5)/256.,gamma), |
264 |
< |
pow((cmap[1][i]+.5)/256.,gamma), |
265 |
< |
pow((cmap[2][i]+.5)/256.,gamma)); |
263 |
> |
pow((cmap[0][i]+.5)/256.,gamcor), |
264 |
> |
pow((cmap[1][i]+.5)/256.,gamcor), |
265 |
> |
pow((cmap[2][i]+.5)/256.,gamcor)); |
266 |
> |
if (bradj) |
267 |
> |
shiftcolrs(ctab, 256, bradj); |
268 |
|
/* convert file */ |
269 |
|
for (i = 0; i < ymax; i++) { |
270 |
|
for (j = 0; j < xmax; j++) { |
277 |
|
if (fwritecolrs(scanline, xmax, stdout) < 0) |
278 |
|
quiterr("error writing RADIANCE file"); |
279 |
|
} |
280 |
< |
free((char *)scanline); |
280 |
> |
free((void *)scanline); |
281 |
|
} |
282 |
|
|
283 |
|
|
291 |
|
if (inpic->bytes_line == 0) { |
292 |
|
if (inpic->pos.y[y] == 0) { |
293 |
|
while (inpic->nexty < y) { |
294 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) |
294 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) |
295 |
|
quiterr("read error in picreadline3"); |
296 |
|
inpic->pos.y[++inpic->nexty] = ftell(inpic->fp); |
297 |
|
} |
301 |
|
quiterr("seek error in picreadline3"); |
302 |
|
} else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0) |
303 |
|
inpic->pos.y[inpic->nexty] = ftell(inpic->fp); |
304 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) /* read scanline */ |
304 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) /* read scanline */ |
305 |
|
quiterr("read error in picreadline3"); |
306 |
|
inpic->nexty = y+1; |
307 |
|
/* convert scanline */ |
308 |
< |
normcolrs(inline, xmax); |
308 |
> |
normcolrs(inl, xmax, bradj); |
309 |
|
for (i = 0; i < xmax; i++) { |
310 |
< |
l3[i].r = inline[i][RED]; |
311 |
< |
l3[i].g = inline[i][GRN]; |
312 |
< |
l3[i].b = inline[i][BLU]; |
310 |
> |
l3[i].r = inl[i][RED]; |
311 |
> |
l3[i].g = inl[i][GRN]; |
312 |
> |
l3[i].b = inl[i][BLU]; |
313 |
|
} |
314 |
|
} |
315 |
|
|
328 |
|
quiterr("seek error in picwriteline"); |
329 |
|
} |
330 |
|
/* write scanline */ |
331 |
< |
if (fwrite(l, sizeof(pixel), xmax, outpic->fp) != xmax) |
331 |
> |
if (fwrite((char *)l, sizeof(pixel), xmax, outpic->fp) != xmax) |
332 |
|
quiterr("write error in picwriteline"); |
333 |
|
if (xmax&1) /* on 16-bit boundary */ |
334 |
|
putc(l[xmax-1], outpic->fp); |
363 |
|
register int i, val; |
364 |
|
|
365 |
|
for (i = 0; i < 256; i++) { |
366 |
< |
val = pow(i/256.0, 1.0/gamma) * 256.0; |
366 |
> |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
367 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
368 |
|
} |
369 |
|
} |