1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
17 |
|
|
18 |
|
#include "color.h" |
19 |
|
|
20 |
+ |
#include "resolu.h" |
21 |
+ |
|
22 |
|
#include "pic.h" |
23 |
|
|
24 |
|
/* descriptor for a picture file or frame buffer */ |
39 |
|
|
40 |
|
extern long ftell(); |
41 |
|
|
42 |
< |
extern double atof(), pow(); |
42 |
> |
extern double pow(); |
43 |
|
|
44 |
< |
double gamma = 2.0; /* gamma correction */ |
44 |
> |
double gamma = 2.2; /* gamma correction */ |
45 |
|
|
46 |
+ |
int bradj = 0; /* brightness adjustment */ |
47 |
+ |
|
48 |
|
pic *inpic, *outpic; |
49 |
|
|
50 |
|
char *progname; |
51 |
|
|
52 |
|
char errmsg[128]; |
53 |
|
|
54 |
< |
COLR *inline; |
54 |
> |
COLR *inl; |
55 |
|
|
56 |
|
int xmax, ymax; |
57 |
|
|
82 |
|
case 'b': |
83 |
|
greyscale = !greyscale; |
84 |
|
break; |
85 |
+ |
case 'e': |
86 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
87 |
+ |
goto userr; |
88 |
+ |
bradj = atoi(argv[++i]); |
89 |
+ |
break; |
90 |
|
case 'r': |
91 |
|
reverse = !reverse; |
92 |
|
break; |
111 |
|
quiterr(errmsg); |
112 |
|
} |
113 |
|
/* get header */ |
114 |
< |
if (fread(&head, sizeof(head), 1, stdin) != 1) |
114 |
> |
if (fread((char *)&head, sizeof(head), 1, stdin) != 1) |
115 |
|
quiterr("missing header"); |
116 |
|
if (head.ras_magic != RAS_MAGIC) |
117 |
|
quiterr("bad raster format"); |
122 |
|
head.ras_depth != 8) |
123 |
|
quiterr("incompatible format"); |
124 |
|
/* put header */ |
125 |
< |
printargs(argc, argv, 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 |
|
} |
195 |
|
p->fp = stdin; |
196 |
|
else if ((p->fp = fopen(fname, "r")) == NULL) |
197 |
|
return(NULL); |
198 |
< |
/* discard header */ |
199 |
< |
getheader(p->fp, NULL); |
200 |
< |
if (fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR)) |
201 |
< |
quiterr("bad picture size"); |
198 |
> |
/* check header */ |
199 |
> |
if (checkheader(p->fp, COLRFMT, NULL) < 0 || |
200 |
> |
fgetresolu(&xmax, &ymax, p->fp) < 0) |
201 |
> |
quiterr("bad picture format"); |
202 |
|
p->nexty = 0; |
203 |
|
p->bytes_line = 0; /* variable length lines */ |
204 |
|
p->pos.y = (long *)ecalloc(ymax, sizeof(long)); |
213 |
|
h->ras_maptype = RMT_EQUAL_RGB; |
214 |
|
h->ras_maplength = 256*3; |
215 |
|
/* allocate scanline */ |
216 |
< |
inline = (COLR *)emalloc(xmax*sizeof(COLR)); |
216 |
> |
inl = (COLR *)emalloc(xmax*sizeof(COLR)); |
217 |
|
|
218 |
|
return(p); |
219 |
|
} |
233 |
|
else if ((p->fp = fopen(fname, "w")) == NULL) |
234 |
|
return(NULL); |
235 |
|
/* write header */ |
236 |
< |
fwrite(h, sizeof(*h), 1, p->fp); |
236 |
> |
fwrite((char *)h, sizeof(*h), 1, p->fp); |
237 |
|
p->nexty = -1; /* needs color map */ |
238 |
|
p->bytes_line = h->ras_width; |
239 |
|
p->pos.b = 0; |
253 |
|
scanline = (COLR *)emalloc(xmax*sizeof(COLR)); |
254 |
|
/* get color table */ |
255 |
|
for (i = 0; i < 3; i ++) |
256 |
< |
if (fread(cmap[i], h->ras_maplength/3, 1, stdin) != 1) |
256 |
> |
if (fread((char *)cmap[i], h->ras_maplength/3, 1, stdin) != 1) |
257 |
|
quiterr("error reading color table"); |
258 |
|
/* convert table */ |
259 |
|
for (i = 0; i < h->ras_maplength/3; i++) |
261 |
|
pow((cmap[0][i]+.5)/256.,gamma), |
262 |
|
pow((cmap[1][i]+.5)/256.,gamma), |
263 |
|
pow((cmap[2][i]+.5)/256.,gamma)); |
264 |
+ |
if (bradj) |
265 |
+ |
shiftcolrs(ctab, 256, bradj); |
266 |
|
/* convert file */ |
267 |
|
for (i = 0; i < ymax; i++) { |
268 |
|
for (j = 0; j < xmax; j++) { |
289 |
|
if (inpic->bytes_line == 0) { |
290 |
|
if (inpic->pos.y[y] == 0) { |
291 |
|
while (inpic->nexty < y) { |
292 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) |
292 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) |
293 |
|
quiterr("read error in picreadline3"); |
294 |
|
inpic->pos.y[++inpic->nexty] = ftell(inpic->fp); |
295 |
|
} |
299 |
|
quiterr("seek error in picreadline3"); |
300 |
|
} else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0) |
301 |
|
inpic->pos.y[inpic->nexty] = ftell(inpic->fp); |
302 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) /* read scanline */ |
302 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) /* read scanline */ |
303 |
|
quiterr("read error in picreadline3"); |
304 |
|
inpic->nexty = y+1; |
305 |
|
/* convert scanline */ |
306 |
< |
normcolrs(inline, xmax); |
306 |
> |
normcolrs(inl, xmax, bradj); |
307 |
|
for (i = 0; i < xmax; i++) { |
308 |
< |
l3[i].r = inline[i][RED]; |
309 |
< |
l3[i].g = inline[i][GRN]; |
310 |
< |
l3[i].b = inline[i][BLU]; |
308 |
> |
l3[i].r = inl[i][RED]; |
309 |
> |
l3[i].g = inl[i][GRN]; |
310 |
> |
l3[i].b = inl[i][BLU]; |
311 |
|
} |
312 |
|
} |
313 |
|
|
326 |
|
quiterr("seek error in picwriteline"); |
327 |
|
} |
328 |
|
/* write scanline */ |
329 |
< |
if (fwrite(l, sizeof(pixel), xmax, outpic->fp) != xmax) |
329 |
> |
if (fwrite((char *)l, sizeof(pixel), xmax, outpic->fp) != xmax) |
330 |
|
quiterr("write error in picwriteline"); |
331 |
|
if (xmax&1) /* on 16-bit boundary */ |
332 |
|
putc(l[xmax-1], outpic->fp); |
361 |
|
register int i, val; |
362 |
|
|
363 |
|
for (i = 0; i < 256; i++) { |
364 |
< |
val = pow(i/256.0, 1.0/gamma) * 256.0; |
364 |
> |
val = pow((i+0.5)/256.0, 1.0/gamma) * 256.0; |
365 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
366 |
|
} |
367 |
|
} |