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 |
|
* |
9 |
|
*/ |
10 |
|
|
11 |
|
#include <stdio.h> |
12 |
+ |
#include <math.h> |
13 |
+ |
#include <time.h> |
14 |
|
|
15 |
+ |
#include "rtmisc.h" |
16 |
|
#include "rasterfile.h" |
17 |
– |
|
17 |
|
#include "color.h" |
18 |
< |
|
18 |
> |
#include "resolu.h" |
19 |
|
#include "pic.h" |
20 |
|
|
21 |
|
/* descriptor for a picture file or frame buffer */ |
30 |
|
} pos; /* position(s) */ |
31 |
|
} pic; |
32 |
|
|
34 |
– |
extern pic *openinput(), *openoutput(); |
33 |
|
|
34 |
< |
extern char *ecalloc(), *emalloc(); |
35 |
< |
|
38 |
< |
extern long ftell(); |
39 |
< |
|
40 |
< |
extern double atof(), pow(); |
41 |
< |
|
42 |
< |
double gamma = 2.0; /* gamma correction */ |
43 |
< |
|
34 |
> |
double gamcor = 2.2; /* gamma correction */ |
35 |
> |
int bradj = 0; /* brightness adjustment */ |
36 |
|
pic *inpic, *outpic; |
45 |
– |
|
37 |
|
char *progname; |
47 |
– |
|
38 |
|
char errmsg[128]; |
39 |
< |
|
50 |
< |
COLR *inline; |
51 |
< |
|
39 |
> |
COLR *inl; |
40 |
|
int xmax, ymax; |
41 |
|
|
42 |
+ |
static void quiterr(char *err); |
43 |
+ |
static pic * openinput(char *fname, struct rasterfile *h); |
44 |
+ |
static pic * openoutput(char *fname, struct rasterfile *h); |
45 |
+ |
static void pr2ra(struct rasterfile *h); |
46 |
|
|
47 |
< |
main(argc, argv) |
48 |
< |
int argc; |
49 |
< |
char *argv[]; |
47 |
> |
|
48 |
> |
int |
49 |
> |
main(int argc, char *argv[]) |
50 |
|
{ |
51 |
|
colormap rasmap; |
52 |
|
struct rasterfile head; |
65 |
|
dither = !dither; |
66 |
|
break; |
67 |
|
case 'g': |
68 |
< |
gamma = atof(argv[++i]); |
68 |
> |
gamcor = atof(argv[++i]); |
69 |
|
break; |
70 |
|
case 'b': |
71 |
|
greyscale = !greyscale; |
72 |
|
break; |
73 |
+ |
case 'e': |
74 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
75 |
+ |
goto userr; |
76 |
+ |
bradj = atoi(argv[++i]); |
77 |
+ |
break; |
78 |
|
case 'r': |
79 |
|
reverse = !reverse; |
80 |
|
break; |
99 |
|
quiterr(errmsg); |
100 |
|
} |
101 |
|
/* get header */ |
102 |
< |
if (fread(&head, sizeof(head), 1, stdin) != 1) |
102 |
> |
if (fread((char *)&head, sizeof(head), 1, stdin) != 1) |
103 |
|
quiterr("missing header"); |
104 |
|
if (head.ras_magic != RAS_MAGIC) |
105 |
|
quiterr("bad raster format"); |
110 |
|
head.ras_depth != 8) |
111 |
|
quiterr("incompatible format"); |
112 |
|
/* put header */ |
113 |
< |
printargs(argc, argv, stdout); |
113 |
> |
newheader("RADIANCE", stdout); |
114 |
> |
printargs(i, argv, stdout); |
115 |
> |
fputformat(COLRFMT, stdout); |
116 |
|
putchar('\n'); |
117 |
< |
printf("-Y %d +X %d\n", ymax, xmax); |
117 |
> |
fprtresolu(xmax, ymax, stdout); |
118 |
|
/* convert file */ |
119 |
|
pr2ra(&head); |
120 |
|
} else { |
121 |
< |
if (i > argc-1 || i < argc-2) |
121 |
> |
if (i < argc-2 || (!greyscale && i > argc-1)) |
122 |
|
goto userr; |
123 |
|
if ((inpic = openinput(argv[i], &head)) == NULL) { |
124 |
|
sprintf(errmsg, "can't open input \"%s\"", argv[i]); |
138 |
|
quiterr(NULL); |
139 |
|
userr: |
140 |
|
fprintf(stderr, |
141 |
< |
"Usage: %s [-d][-c ncolors][-b][-g gamma] input [output]\n", |
141 |
> |
"Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n", |
142 |
|
progname); |
143 |
< |
fprintf(stderr, " Or: %s -r [-g gamma] [input [output]]\n", |
143 |
> |
fprintf(stderr, " Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n", |
144 |
|
progname); |
145 |
|
exit(1); |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
< |
quiterr(err) /* print message and exit */ |
150 |
< |
char *err; |
149 |
> |
static void |
150 |
> |
quiterr( /* print message and exit */ |
151 |
> |
char *err |
152 |
> |
) |
153 |
|
{ |
154 |
|
if (err != NULL) { |
155 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
+ |
void |
163 |
|
eputs(s) |
164 |
|
char *s; |
165 |
|
{ |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
+ |
void |
171 |
|
quit(code) |
172 |
|
int code; |
173 |
|
{ |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
< |
pic * |
179 |
< |
openinput(fname, h) /* open RADIANCE input file */ |
180 |
< |
char *fname; |
181 |
< |
register struct rasterfile *h; |
178 |
> |
static pic * |
179 |
> |
openinput( /* open RADIANCE input file */ |
180 |
> |
char *fname, |
181 |
> |
register struct rasterfile *h |
182 |
> |
) |
183 |
|
{ |
184 |
|
register pic *p; |
185 |
|
|
189 |
|
p->fp = stdin; |
190 |
|
else if ((p->fp = fopen(fname, "r")) == NULL) |
191 |
|
return(NULL); |
192 |
< |
/* discard header */ |
193 |
< |
getheader(p->fp, NULL); |
194 |
< |
if (fscanf(p->fp, "-Y %d +X %d\n", &ymax, &xmax) != 2) |
195 |
< |
quiterr("bad picture size"); |
192 |
> |
/* check header */ |
193 |
> |
if (checkheader(p->fp, COLRFMT, NULL) < 0 || |
194 |
> |
fgetresolu(&xmax, &ymax, p->fp) < 0) |
195 |
> |
quiterr("bad picture format"); |
196 |
|
p->nexty = 0; |
197 |
|
p->bytes_line = 0; /* variable length lines */ |
198 |
|
p->pos.y = (long *)ecalloc(ymax, sizeof(long)); |
207 |
|
h->ras_maptype = RMT_EQUAL_RGB; |
208 |
|
h->ras_maplength = 256*3; |
209 |
|
/* allocate scanline */ |
210 |
< |
inline = (COLR *)emalloc(xmax*sizeof(COLR)); |
210 |
> |
inl = (COLR *)emalloc(xmax*sizeof(COLR)); |
211 |
|
|
212 |
|
return(p); |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
< |
pic * |
217 |
< |
openoutput(fname, h) /* open output rasterfile */ |
218 |
< |
char *fname; |
219 |
< |
register struct rasterfile *h; |
216 |
> |
static pic * |
217 |
> |
openoutput( /* open output rasterfile */ |
218 |
> |
char *fname, |
219 |
> |
register struct rasterfile *h |
220 |
> |
) |
221 |
|
{ |
222 |
|
register pic *p; |
223 |
|
|
228 |
|
else if ((p->fp = fopen(fname, "w")) == NULL) |
229 |
|
return(NULL); |
230 |
|
/* write header */ |
231 |
< |
fwrite(h, sizeof(*h), 1, p->fp); |
231 |
> |
fwrite((char *)h, sizeof(*h), 1, p->fp); |
232 |
|
p->nexty = -1; /* needs color map */ |
233 |
|
p->bytes_line = h->ras_width; |
234 |
|
p->pos.b = 0; |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
< |
pr2ra(h) /* pixrect file to RADIANCE file */ |
241 |
< |
struct rasterfile *h; |
240 |
> |
static void |
241 |
> |
pr2ra( /* pixrect file to RADIANCE file */ |
242 |
> |
struct rasterfile *h |
243 |
> |
) |
244 |
|
{ |
245 |
|
BYTE cmap[3][256]; |
246 |
|
COLR ctab[256]; |
250 |
|
scanline = (COLR *)emalloc(xmax*sizeof(COLR)); |
251 |
|
/* get color table */ |
252 |
|
for (i = 0; i < 3; i ++) |
253 |
< |
if (fread(cmap[i], h->ras_maplength/3, 1, stdin) != 1) |
253 |
> |
if (fread((char *)cmap[i], h->ras_maplength/3, 1, stdin) != 1) |
254 |
|
quiterr("error reading color table"); |
255 |
|
/* convert table */ |
256 |
|
for (i = 0; i < h->ras_maplength/3; i++) |
257 |
|
setcolr(ctab[i], |
258 |
< |
pow((cmap[0][i]+.5)/256.,gamma), |
259 |
< |
pow((cmap[1][i]+.5)/256.,gamma), |
260 |
< |
pow((cmap[2][i]+.5)/256.,gamma)); |
258 |
> |
pow((cmap[0][i]+.5)/256.,gamcor), |
259 |
> |
pow((cmap[1][i]+.5)/256.,gamcor), |
260 |
> |
pow((cmap[2][i]+.5)/256.,gamcor)); |
261 |
> |
if (bradj) |
262 |
> |
shiftcolrs(ctab, 256, bradj); |
263 |
|
/* convert file */ |
264 |
|
for (i = 0; i < ymax; i++) { |
265 |
|
for (j = 0; j < xmax; j++) { |
272 |
|
if (fwritecolrs(scanline, xmax, stdout) < 0) |
273 |
|
quiterr("error writing RADIANCE file"); |
274 |
|
} |
275 |
< |
free((char *)scanline); |
275 |
> |
free((void *)scanline); |
276 |
|
} |
277 |
|
|
278 |
|
|
279 |
< |
picreadline3(y, l3) /* read in 3-byte scanline */ |
280 |
< |
int y; |
281 |
< |
register rgbpixel *l3; |
279 |
> |
extern void |
280 |
> |
picreadline3( /* read in 3-byte scanline */ |
281 |
> |
int y, |
282 |
> |
register rgbpixel *l3 |
283 |
> |
) |
284 |
|
{ |
285 |
< |
register BYTE *l4; |
275 |
< |
register int shift, c; |
276 |
< |
int i; |
285 |
> |
register int i; |
286 |
|
|
287 |
< |
if (inpic->nexty != y) { /* find scanline */ |
287 |
> |
if (inpic->nexty != y) { /* find scanline */ |
288 |
|
if (inpic->bytes_line == 0) { |
289 |
|
if (inpic->pos.y[y] == 0) { |
290 |
|
while (inpic->nexty < y) { |
291 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) |
291 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) |
292 |
|
quiterr("read error in picreadline3"); |
293 |
|
inpic->pos.y[++inpic->nexty] = ftell(inpic->fp); |
294 |
|
} |
298 |
|
quiterr("seek error in picreadline3"); |
299 |
|
} else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0) |
300 |
|
inpic->pos.y[inpic->nexty] = ftell(inpic->fp); |
301 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) /* read scanline */ |
301 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) /* read scanline */ |
302 |
|
quiterr("read error in picreadline3"); |
303 |
|
inpic->nexty = y+1; |
304 |
|
/* convert scanline */ |
305 |
< |
for (l4=inline[0], i=xmax; i--; l4+=4, l3++) { |
306 |
< |
shift = l4[EXP] - COLXS; |
307 |
< |
if (shift >= 8) { |
308 |
< |
l3->r = l3->g = l3->b = 255; |
309 |
< |
} else if (shift <= -8) { |
301 |
< |
l3->r = l3->g = l3->b = 0; |
302 |
< |
} else if (shift > 0) { |
303 |
< |
c = l4[RED] << shift; |
304 |
< |
l3->r = c > 255 ? 255 : c; |
305 |
< |
c = l4[GRN] << shift; |
306 |
< |
l3->g = c > 255 ? 255 : c; |
307 |
< |
c = l4[BLU] << shift; |
308 |
< |
l3->b = c > 255 ? 255 : c; |
309 |
< |
} else if (shift < 0) { |
310 |
< |
l3->r = l4[RED] >> -shift; |
311 |
< |
l3->g = l4[GRN] >> -shift; |
312 |
< |
l3->b = l4[BLU] >> -shift; |
313 |
< |
} else { |
314 |
< |
l3->r = l4[RED]; |
315 |
< |
l3->g = l4[GRN]; |
316 |
< |
l3->b = l4[BLU]; |
317 |
< |
} |
305 |
> |
normcolrs(inl, xmax, bradj); |
306 |
> |
for (i = 0; i < xmax; i++) { |
307 |
> |
l3[i].r = inl[i][RED]; |
308 |
> |
l3[i].g = inl[i][GRN]; |
309 |
> |
l3[i].b = inl[i][BLU]; |
310 |
|
} |
311 |
|
} |
312 |
|
|
313 |
|
|
314 |
< |
picwriteline(y, l) /* write out scanline */ |
315 |
< |
int y; |
316 |
< |
register pixel *l; |
314 |
> |
extern void |
315 |
> |
picwriteline( /* write out scanline */ |
316 |
> |
int y, |
317 |
> |
register pixel *l |
318 |
> |
) |
319 |
|
{ |
320 |
|
if (outpic->nexty != y) { /* seek to scanline */ |
321 |
|
if (outpic->bytes_line == 0) { |
327 |
|
quiterr("seek error in picwriteline"); |
328 |
|
} |
329 |
|
/* write scanline */ |
330 |
< |
if (fwrite(l, sizeof(pixel), xmax, outpic->fp) != xmax) |
330 |
> |
if (fwrite((char *)l, sizeof(pixel), xmax, outpic->fp) != xmax) |
331 |
|
quiterr("write error in picwriteline"); |
332 |
|
if (xmax&1) /* on 16-bit boundary */ |
333 |
|
putc(l[xmax-1], outpic->fp); |
337 |
|
} |
338 |
|
|
339 |
|
|
340 |
< |
picwritecm(cm) /* write out color map */ |
341 |
< |
colormap cm; |
340 |
> |
extern void |
341 |
> |
picwritecm( /* write out color map */ |
342 |
> |
colormap cm |
343 |
> |
) |
344 |
|
{ |
345 |
|
register int i, j; |
346 |
|
|
358 |
|
} |
359 |
|
|
360 |
|
|
361 |
< |
picreadcm(map) /* do gamma correction if requested */ |
362 |
< |
colormap map; |
361 |
> |
extern void |
362 |
> |
picreadcm( /* do gamma correction if requested */ |
363 |
> |
colormap map |
364 |
> |
) |
365 |
|
{ |
366 |
|
register int i, val; |
367 |
|
|
368 |
|
for (i = 0; i < 256; i++) { |
369 |
< |
val = pow(i/256.0, 1.0/gamma) * 256.0; |
369 |
> |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
370 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
371 |
|
} |
372 |
|
} |