| 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"; |
| 39 |
|
|
| 40 |
|
extern double atof(), pow(); |
| 41 |
|
|
| 42 |
< |
double gamma = 2.0; /* gamma correction */ |
| 42 |
> |
double gamma = 2.2; /* gamma correction */ |
| 43 |
|
|
| 44 |
+ |
int bradj = 0; /* brightness adjustment */ |
| 45 |
+ |
|
| 46 |
|
pic *inpic, *outpic; |
| 47 |
|
|
| 48 |
|
char *progname; |
| 49 |
|
|
| 50 |
|
char errmsg[128]; |
| 51 |
|
|
| 52 |
< |
COLR *inline; |
| 52 |
> |
COLR *inl; |
| 53 |
|
|
| 54 |
|
int xmax, ymax; |
| 55 |
|
|
| 80 |
|
case 'b': |
| 81 |
|
greyscale = !greyscale; |
| 82 |
|
break; |
| 83 |
+ |
case 'e': |
| 84 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
| 85 |
+ |
goto userr; |
| 86 |
+ |
bradj = atoi(argv[++i]); |
| 87 |
+ |
break; |
| 88 |
|
case 'r': |
| 89 |
|
reverse = !reverse; |
| 90 |
|
break; |
| 109 |
|
quiterr(errmsg); |
| 110 |
|
} |
| 111 |
|
/* get header */ |
| 112 |
< |
if (fread(&head, sizeof(head), 1, stdin) != 1) |
| 112 |
> |
if (fread((char *)&head, sizeof(head), 1, stdin) != 1) |
| 113 |
|
quiterr("missing header"); |
| 114 |
|
if (head.ras_magic != RAS_MAGIC) |
| 115 |
|
quiterr("bad raster format"); |
| 120 |
|
head.ras_depth != 8) |
| 121 |
|
quiterr("incompatible format"); |
| 122 |
|
/* put header */ |
| 123 |
< |
printargs(argc, argv, stdout); |
| 123 |
> |
printargs(i, argv, stdout); |
| 124 |
> |
fputformat(COLRFMT, stdout); |
| 125 |
|
putchar('\n'); |
| 126 |
< |
printf("-Y %d +X %d\n", ymax, xmax); |
| 126 |
> |
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
| 127 |
|
/* convert file */ |
| 128 |
|
pr2ra(&head); |
| 129 |
|
} else { |
| 130 |
< |
if (i > argc-1 || i < argc-2) |
| 130 |
> |
if (i < argc-2 || (!greyscale && i > argc-1)) |
| 131 |
|
goto userr; |
| 132 |
|
if ((inpic = openinput(argv[i], &head)) == NULL) { |
| 133 |
|
sprintf(errmsg, "can't open input \"%s\"", argv[i]); |
| 147 |
|
quiterr(NULL); |
| 148 |
|
userr: |
| 149 |
|
fprintf(stderr, |
| 150 |
< |
"Usage: %s [-d][-c ncolors][-b][-g gamma] input [output]\n", |
| 150 |
> |
"Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n", |
| 151 |
|
progname); |
| 152 |
< |
fprintf(stderr, " Or: %s -r [-g gamma] [input [output]]\n", |
| 152 |
> |
fprintf(stderr, " Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n", |
| 153 |
|
progname); |
| 154 |
|
exit(1); |
| 155 |
|
} |
| 193 |
|
p->fp = stdin; |
| 194 |
|
else if ((p->fp = fopen(fname, "r")) == NULL) |
| 195 |
|
return(NULL); |
| 196 |
< |
/* discard header */ |
| 197 |
< |
getheader(p->fp, NULL); |
| 198 |
< |
if (fscanf(p->fp, "-Y %d +X %d\n", &ymax, &xmax) != 2) |
| 199 |
< |
quiterr("bad picture size"); |
| 196 |
> |
/* check header */ |
| 197 |
> |
if (checkheader(p->fp, COLRFMT, NULL) < 0 || |
| 198 |
> |
fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR)) |
| 199 |
> |
quiterr("bad picture format"); |
| 200 |
|
p->nexty = 0; |
| 201 |
|
p->bytes_line = 0; /* variable length lines */ |
| 202 |
|
p->pos.y = (long *)ecalloc(ymax, sizeof(long)); |
| 211 |
|
h->ras_maptype = RMT_EQUAL_RGB; |
| 212 |
|
h->ras_maplength = 256*3; |
| 213 |
|
/* allocate scanline */ |
| 214 |
< |
inline = (COLR *)emalloc(xmax*sizeof(COLR)); |
| 214 |
> |
inl = (COLR *)emalloc(xmax*sizeof(COLR)); |
| 215 |
|
|
| 216 |
|
return(p); |
| 217 |
|
} |
| 231 |
|
else if ((p->fp = fopen(fname, "w")) == NULL) |
| 232 |
|
return(NULL); |
| 233 |
|
/* write header */ |
| 234 |
< |
fwrite(h, sizeof(*h), 1, p->fp); |
| 234 |
> |
fwrite((char *)h, sizeof(*h), 1, p->fp); |
| 235 |
|
p->nexty = -1; /* needs color map */ |
| 236 |
|
p->bytes_line = h->ras_width; |
| 237 |
|
p->pos.b = 0; |
| 251 |
|
scanline = (COLR *)emalloc(xmax*sizeof(COLR)); |
| 252 |
|
/* get color table */ |
| 253 |
|
for (i = 0; i < 3; i ++) |
| 254 |
< |
if (fread(cmap[i], h->ras_maplength/3, 1, stdin) != 1) |
| 254 |
> |
if (fread((char *)cmap[i], h->ras_maplength/3, 1, stdin) != 1) |
| 255 |
|
quiterr("error reading color table"); |
| 256 |
|
/* convert table */ |
| 257 |
|
for (i = 0; i < h->ras_maplength/3; i++) |
| 259 |
|
pow((cmap[0][i]+.5)/256.,gamma), |
| 260 |
|
pow((cmap[1][i]+.5)/256.,gamma), |
| 261 |
|
pow((cmap[2][i]+.5)/256.,gamma)); |
| 262 |
+ |
if (bradj) |
| 263 |
+ |
shiftcolrs(ctab, 256, bradj); |
| 264 |
|
/* convert file */ |
| 265 |
|
for (i = 0; i < ymax; i++) { |
| 266 |
|
for (j = 0; j < xmax; j++) { |
| 281 |
|
int y; |
| 282 |
|
register rgbpixel *l3; |
| 283 |
|
{ |
| 284 |
< |
register BYTE *l4; |
| 275 |
< |
register int shift, c; |
| 276 |
< |
int i; |
| 284 |
> |
register int i; |
| 285 |
|
|
| 286 |
< |
if (inpic->nexty != y) { /* find scanline */ |
| 286 |
> |
if (inpic->nexty != y) { /* find scanline */ |
| 287 |
|
if (inpic->bytes_line == 0) { |
| 288 |
|
if (inpic->pos.y[y] == 0) { |
| 289 |
|
while (inpic->nexty < y) { |
| 290 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) |
| 290 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) |
| 291 |
|
quiterr("read error in picreadline3"); |
| 292 |
|
inpic->pos.y[++inpic->nexty] = ftell(inpic->fp); |
| 293 |
|
} |
| 297 |
|
quiterr("seek error in picreadline3"); |
| 298 |
|
} else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0) |
| 299 |
|
inpic->pos.y[inpic->nexty] = ftell(inpic->fp); |
| 300 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) /* read scanline */ |
| 300 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) /* read scanline */ |
| 301 |
|
quiterr("read error in picreadline3"); |
| 302 |
|
inpic->nexty = y+1; |
| 303 |
|
/* convert scanline */ |
| 304 |
< |
for (l4=inline[0], i=xmax; i--; l4+=4, l3++) { |
| 305 |
< |
shift = l4[EXP] - COLXS; |
| 306 |
< |
if (shift >= 8) { |
| 307 |
< |
l3->r = l3->g = l3->b = 255; |
| 308 |
< |
} 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 |
< |
} |
| 304 |
> |
normcolrs(inl, xmax, bradj); |
| 305 |
> |
for (i = 0; i < xmax; i++) { |
| 306 |
> |
l3[i].r = inl[i][RED]; |
| 307 |
> |
l3[i].g = inl[i][GRN]; |
| 308 |
> |
l3[i].b = inl[i][BLU]; |
| 309 |
|
} |
| 310 |
|
} |
| 311 |
|
|
| 324 |
|
quiterr("seek error in picwriteline"); |
| 325 |
|
} |
| 326 |
|
/* write scanline */ |
| 327 |
< |
if (fwrite(l, sizeof(pixel), xmax, outpic->fp) != xmax) |
| 327 |
> |
if (fwrite((char *)l, sizeof(pixel), xmax, outpic->fp) != xmax) |
| 328 |
|
quiterr("write error in picwriteline"); |
| 329 |
|
if (xmax&1) /* on 16-bit boundary */ |
| 330 |
|
putc(l[xmax-1], outpic->fp); |
| 359 |
|
register int i, val; |
| 360 |
|
|
| 361 |
|
for (i = 0; i < 256; i++) { |
| 362 |
< |
val = pow(i/256.0, 1.0/gamma) * 256.0; |
| 362 |
> |
val = pow((i+0.5)/256.0, 1.0/gamma) * 256.0; |
| 363 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
| 364 |
|
} |
| 365 |
|
} |