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"; |
19 |
|
|
20 |
|
#include "targa.h" |
21 |
|
|
22 |
+ |
#ifndef BSD |
23 |
+ |
#define bcopy(s,d,n) (void)memcpy(d,s,n) |
24 |
+ |
extern char *memcpy(); |
25 |
+ |
#endif |
26 |
|
/* descriptor for a picture file or frame buffer */ |
27 |
|
typedef struct { |
28 |
|
char *name; /* file name */ |
51 |
|
|
52 |
|
extern double atof(), pow(); |
53 |
|
|
54 |
< |
double gamma = 2.0; /* gamma correction */ |
54 |
> |
double gamma = 2.2; /* gamma correction */ |
55 |
|
|
56 |
+ |
int bradj = 0; /* brightness adjustment */ |
57 |
+ |
|
58 |
|
pic *inpic; |
59 |
|
|
60 |
|
char *progname; |
61 |
|
|
62 |
|
char errmsg[128]; |
63 |
|
|
64 |
< |
COLR *inline; |
64 |
> |
COLR *inl; |
65 |
|
|
66 |
|
pixel *tarData; |
67 |
|
|
97 |
|
case 'b': |
98 |
|
greyscale = 1; |
99 |
|
break; |
100 |
+ |
case 'e': |
101 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
102 |
+ |
goto userr; |
103 |
+ |
bradj = atoi(argv[++i]); |
104 |
+ |
break; |
105 |
|
case 'c': |
106 |
|
ncolors = atoi(argv[++i]); |
107 |
|
break; |
131 |
|
quiterr(errmsg); |
132 |
|
} |
133 |
|
/* put header */ |
134 |
< |
printargs(argc, argv, stdout); |
134 |
> |
printargs(i, argv, stdout); |
135 |
> |
fputformat(COLRFMT, stdout); |
136 |
|
putchar('\n'); |
137 |
|
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
138 |
|
/* convert file */ |
139 |
|
tg2ra(&head); |
140 |
|
} else { |
141 |
< |
if (i > argc-1 || i < argc-2) |
141 |
> |
if (i < argc-2 || (!greyscale && i > argc-1)) |
142 |
|
goto userr; |
143 |
|
if ((inpic = openinput(argv[i], &head)) == NULL) { |
144 |
|
sprintf(errmsg, "can't open input \"%s\"", argv[i]); |
161 |
|
quiterr(NULL); |
162 |
|
userr: |
163 |
|
fprintf(stderr, |
164 |
< |
"Usage: %s [-d][-c ncolors][-b][-g gamma] input [output]\n", |
164 |
> |
"Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n", |
165 |
|
progname); |
166 |
< |
fprintf(stderr, " Or: %s -r [-g gamma] [input [output]]\n", |
166 |
> |
fprintf(stderr, " Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n", |
167 |
|
progname); |
168 |
|
exit(1); |
169 |
|
} |
239 |
|
|
240 |
|
if (ip != NULL) |
241 |
|
if (nidbytes) |
242 |
< |
fread(ip, nidbytes, 1, fp); |
242 |
> |
fread((char *)ip, nidbytes, 1, fp); |
243 |
|
else |
244 |
|
*ip = '\0'; |
245 |
|
else if (nidbytes) |
290 |
|
p->fp = stdin; |
291 |
|
else if ((p->fp = fopen(fname, "r")) == NULL) |
292 |
|
return(NULL); |
293 |
< |
/* discard header */ |
294 |
< |
getheader(p->fp, NULL); |
295 |
< |
if (fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR)) |
296 |
< |
quiterr("bad picture size"); |
293 |
> |
/* get header info. */ |
294 |
> |
if (checkheader(p->fp, COLRFMT, NULL) < 0 || |
295 |
> |
fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR)) |
296 |
> |
quiterr("bad picture format"); |
297 |
|
p->nexty = 0; |
298 |
|
p->bytes_line = 0; /* variable length lines */ |
299 |
|
p->pos.y = (long *)ecalloc(ymax, sizeof(long)); |
312 |
|
h->dataBits = 8; |
313 |
|
h->imType = 0; |
314 |
|
/* allocate scanline */ |
315 |
< |
inline = (COLR *)emalloc(xmax*sizeof(COLR)); |
315 |
> |
inl = (COLR *)emalloc(xmax*sizeof(COLR)); |
316 |
|
/* allocate targa data */ |
317 |
|
tarData = taralloc(h); |
318 |
|
|
332 |
|
register int i, j; |
333 |
|
|
334 |
|
/* get color table */ |
335 |
< |
if ((hp->CMapBits==24 ? fread(map.c3, sizeof(map.c3), 1, stdin) : |
336 |
< |
fread(map.c4, sizeof(map.c4), 1, stdin)) != 1) |
335 |
> |
if ((hp->CMapBits==24 ? fread((char *)map.c3,sizeof(map.c3),1,stdin) : |
336 |
> |
fread((char *)map.c4,sizeof(map.c4),1,stdin)) != 1) |
337 |
|
quiterr("error reading color table"); |
338 |
|
/* convert table */ |
339 |
|
for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++) |
347 |
|
pow((map.c4[i][3]+.5)/256.,gamma), |
348 |
|
pow((map.c4[i][2]+.5)/256.,gamma), |
349 |
|
pow((map.c4[i][1]+.5)/256.,gamma)); |
350 |
< |
|
350 |
> |
if (bradj) |
351 |
> |
shiftcolrs(ctab, 256, bradj); |
352 |
|
/* allocate targa data */ |
353 |
|
tarData = taralloc(hp); |
354 |
|
/* get data */ |
371 |
|
int y; |
372 |
|
register rgbpixel *l3; |
373 |
|
{ |
374 |
< |
register BYTE *l4; |
362 |
< |
register int shift, c; |
363 |
< |
int i; |
374 |
> |
register int i; |
375 |
|
|
376 |
< |
if (inpic->nexty != y) { /* find scanline */ |
376 |
> |
if (inpic->nexty != y) { /* find scanline */ |
377 |
|
if (inpic->bytes_line == 0) { |
378 |
|
if (inpic->pos.y[y] == 0) { |
379 |
|
while (inpic->nexty < y) { |
380 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) |
380 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) |
381 |
|
quiterr("read error in picreadline3"); |
382 |
|
inpic->pos.y[++inpic->nexty] = ftell(inpic->fp); |
383 |
|
} |
387 |
|
quiterr("seek error in picreadline3"); |
388 |
|
} else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0) |
389 |
|
inpic->pos.y[inpic->nexty] = ftell(inpic->fp); |
390 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) /* read scanline */ |
390 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) /* read scanline */ |
391 |
|
quiterr("read error in picreadline3"); |
392 |
|
inpic->nexty = y+1; |
393 |
|
/* convert scanline */ |
394 |
< |
for (l4=inline[0], i=xmax; i--; l4+=4, l3++) { |
395 |
< |
shift = l4[EXP] - COLXS; |
396 |
< |
if (shift >= 8) { |
397 |
< |
l3->r = l3->g = l3->b = 255; |
398 |
< |
} else if (shift <= -8) { |
388 |
< |
l3->r = l3->g = l3->b = 0; |
389 |
< |
} else if (shift > 0) { |
390 |
< |
c = l4[RED] << shift; |
391 |
< |
l3->r = c > 255 ? 255 : c; |
392 |
< |
c = l4[GRN] << shift; |
393 |
< |
l3->g = c > 255 ? 255 : c; |
394 |
< |
c = l4[BLU] << shift; |
395 |
< |
l3->b = c > 255 ? 255 : c; |
396 |
< |
} else if (shift < 0) { |
397 |
< |
l3->r = l4[RED] >> -shift; |
398 |
< |
l3->g = l4[GRN] >> -shift; |
399 |
< |
l3->b = l4[BLU] >> -shift; |
400 |
< |
} else { |
401 |
< |
l3->r = l4[RED]; |
402 |
< |
l3->g = l4[GRN]; |
403 |
< |
l3->b = l4[BLU]; |
404 |
< |
} |
394 |
> |
normcolrs(inl, xmax, bradj); |
395 |
> |
for (i = 0; i < xmax; i++) { |
396 |
> |
l3[i].r = inl[i][RED]; |
397 |
> |
l3[i].g = inl[i][GRN]; |
398 |
> |
l3[i].b = inl[i][BLU]; |
399 |
|
} |
400 |
|
} |
401 |
|
|
404 |
|
int y; |
405 |
|
pixel *l; |
406 |
|
{ |
407 |
< |
bcopy(l, &tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel)); |
407 |
> |
bcopy((char *)l, (char *)&tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel)); |
408 |
|
} |
409 |
|
|
410 |
|
|
414 |
|
FILE *fp; |
415 |
|
{ |
416 |
|
if (h->dataType == IM_CMAP) { /* uncompressed */ |
417 |
< |
if (fwrite(d, h->x*sizeof(pixel), h->y, fp) != h->y) |
417 |
> |
if (fwrite((char *)d,h->x*sizeof(pixel),h->y,fp) != h->y) |
418 |
|
quiterr("error writing targa file"); |
419 |
|
return; |
420 |
|
} |
431 |
|
register pixel *dp; |
432 |
|
|
433 |
|
if (h->dataType == IM_CMAP) { /* uncompressed */ |
434 |
< |
if (fread(data, h->x*sizeof(pixel), h->y, fp) != h->y) |
434 |
> |
if (fread((char *)data,h->x*sizeof(pixel),h->y,fp) != h->y) |
435 |
|
goto readerr; |
436 |
|
return; |
437 |
|
} |
474 |
|
register int i, val; |
475 |
|
|
476 |
|
for (i = 0; i < 256; i++) { |
477 |
< |
val = pow(i/256.0, 1.0/gamma) * 256.0; |
477 |
> |
val = pow((i+0.5)/256.0, 1.0/gamma) * 256.0; |
478 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
479 |
|
} |
480 |
|
} |