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"; |
15 |
|
|
16 |
|
#include "color.h" |
17 |
|
|
18 |
+ |
#include "resolu.h" |
19 |
+ |
|
20 |
|
#include "pic.h" |
21 |
|
|
22 |
|
#include "targa.h" |
51 |
|
|
52 |
|
extern long ftell(); |
53 |
|
|
54 |
< |
extern double atof(), pow(); |
54 |
> |
extern double pow(); |
55 |
|
|
56 |
< |
double gamma = 2.0; /* gamma correction */ |
56 |
> |
double gamma = 2.2; /* gamma correction */ |
57 |
|
|
58 |
+ |
int bradj = 0; /* brightness adjustment */ |
59 |
+ |
|
60 |
|
pic *inpic; |
61 |
|
|
62 |
|
char *progname; |
63 |
|
|
64 |
|
char errmsg[128]; |
65 |
|
|
66 |
< |
COLR *inline; |
66 |
> |
COLR *inl; |
67 |
|
|
68 |
|
pixel *tarData; |
69 |
|
|
99 |
|
case 'b': |
100 |
|
greyscale = 1; |
101 |
|
break; |
102 |
+ |
case 'e': |
103 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
104 |
+ |
goto userr; |
105 |
+ |
bradj = atoi(argv[++i]); |
106 |
+ |
break; |
107 |
|
case 'c': |
108 |
|
ncolors = atoi(argv[++i]); |
109 |
|
break; |
133 |
|
quiterr(errmsg); |
134 |
|
} |
135 |
|
/* put header */ |
136 |
< |
printargs(argc, argv, stdout); |
136 |
> |
printargs(i, argv, stdout); |
137 |
> |
fputformat(COLRFMT, stdout); |
138 |
|
putchar('\n'); |
139 |
< |
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
139 |
> |
fprtresolu(xmax, ymax, stdout); |
140 |
|
/* convert file */ |
141 |
|
tg2ra(&head); |
142 |
|
} else { |
143 |
< |
if (i > argc-1 || i < argc-2) |
143 |
> |
if (i < argc-2 || (!greyscale && i > argc-1)) |
144 |
|
goto userr; |
145 |
|
if ((inpic = openinput(argv[i], &head)) == NULL) { |
146 |
|
sprintf(errmsg, "can't open input \"%s\"", argv[i]); |
163 |
|
quiterr(NULL); |
164 |
|
userr: |
165 |
|
fprintf(stderr, |
166 |
< |
"Usage: %s [-d][-c ncolors][-b][-g gamma] input [output]\n", |
166 |
> |
"Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n", |
167 |
|
progname); |
168 |
< |
fprintf(stderr, " Or: %s -r [-g gamma] [input [output]]\n", |
168 |
> |
fprintf(stderr, " Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n", |
169 |
|
progname); |
170 |
|
exit(1); |
171 |
|
} |
292 |
|
p->fp = stdin; |
293 |
|
else if ((p->fp = fopen(fname, "r")) == NULL) |
294 |
|
return(NULL); |
295 |
< |
/* discard header */ |
296 |
< |
getheader(p->fp, NULL); |
297 |
< |
if (fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR)) |
298 |
< |
quiterr("bad picture size"); |
295 |
> |
/* get header info. */ |
296 |
> |
if (checkheader(p->fp, COLRFMT, NULL) < 0 || |
297 |
> |
fgetresolu(&xmax, &ymax, p->fp) < 0) |
298 |
> |
quiterr("bad picture format"); |
299 |
|
p->nexty = 0; |
300 |
|
p->bytes_line = 0; /* variable length lines */ |
301 |
|
p->pos.y = (long *)ecalloc(ymax, sizeof(long)); |
314 |
|
h->dataBits = 8; |
315 |
|
h->imType = 0; |
316 |
|
/* allocate scanline */ |
317 |
< |
inline = (COLR *)emalloc(xmax*sizeof(COLR)); |
317 |
> |
inl = (COLR *)emalloc(xmax*sizeof(COLR)); |
318 |
|
/* allocate targa data */ |
319 |
|
tarData = taralloc(h); |
320 |
|
|
334 |
|
register int i, j; |
335 |
|
|
336 |
|
/* get color table */ |
337 |
< |
if ((hp->CMapBits==24 ? fread((char *)map.c3,sizeof(map.c3),1,stdin) : |
338 |
< |
fread((char *)map.c4,sizeof(map.c4),1,stdin)) != 1) |
337 |
> |
if ((hp->CMapBits==24 ? fread((char *)(map.c3+hp->mapOrig), |
338 |
> |
3*hp->mapLength,1,stdin) : |
339 |
> |
fread((char *)(map.c4+hp->mapOrig), |
340 |
> |
4*hp->mapLength,1,stdin)) != 1) |
341 |
|
quiterr("error reading color table"); |
342 |
|
/* convert table */ |
343 |
|
for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++) |
351 |
|
pow((map.c4[i][3]+.5)/256.,gamma), |
352 |
|
pow((map.c4[i][2]+.5)/256.,gamma), |
353 |
|
pow((map.c4[i][1]+.5)/256.,gamma)); |
354 |
< |
|
354 |
> |
if (bradj) |
355 |
> |
shiftcolrs(ctab, 256, bradj); |
356 |
|
/* allocate targa data */ |
357 |
|
tarData = taralloc(hp); |
358 |
|
/* get data */ |
381 |
|
if (inpic->bytes_line == 0) { |
382 |
|
if (inpic->pos.y[y] == 0) { |
383 |
|
while (inpic->nexty < y) { |
384 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) |
384 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) |
385 |
|
quiterr("read error in picreadline3"); |
386 |
|
inpic->pos.y[++inpic->nexty] = ftell(inpic->fp); |
387 |
|
} |
391 |
|
quiterr("seek error in picreadline3"); |
392 |
|
} else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0) |
393 |
|
inpic->pos.y[inpic->nexty] = ftell(inpic->fp); |
394 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) /* read scanline */ |
394 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) /* read scanline */ |
395 |
|
quiterr("read error in picreadline3"); |
396 |
|
inpic->nexty = y+1; |
397 |
|
/* convert scanline */ |
398 |
< |
normcolrs(inline, xmax, 0); |
398 |
> |
normcolrs(inl, xmax, bradj); |
399 |
|
for (i = 0; i < xmax; i++) { |
400 |
< |
l3[i].r = inline[i][RED]; |
401 |
< |
l3[i].g = inline[i][GRN]; |
402 |
< |
l3[i].b = inline[i][BLU]; |
400 |
> |
l3[i].r = inl[i][RED]; |
401 |
> |
l3[i].g = inl[i][GRN]; |
402 |
> |
l3[i].b = inl[i][BLU]; |
403 |
|
} |
404 |
|
} |
405 |
|
|
478 |
|
register int i, val; |
479 |
|
|
480 |
|
for (i = 0; i < 256; i++) { |
481 |
< |
val = pow(i/256.0, 1.0/gamma) * 256.0; |
481 |
> |
val = pow((i+0.5)/256.0, 1.0/gamma) * 256.0; |
482 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
483 |
|
} |
484 |
|
} |