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 */ |
59 |
|
|
60 |
|
char errmsg[128]; |
61 |
|
|
62 |
< |
COLR *inline; |
62 |
> |
COLR *inl; |
63 |
|
|
64 |
|
pixel *tarData; |
65 |
|
|
124 |
|
quiterr(errmsg); |
125 |
|
} |
126 |
|
/* put header */ |
127 |
< |
printargs(argc, argv, stdout); |
127 |
> |
printargs(i, argv, stdout); |
128 |
> |
fputformat(COLRFMT, stdout); |
129 |
|
putchar('\n'); |
130 |
< |
printf("-Y %d +X %d\n", ymax, xmax); |
130 |
> |
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
131 |
|
/* convert file */ |
132 |
|
tg2ra(&head); |
133 |
|
} else { |
232 |
|
|
233 |
|
if (ip != NULL) |
234 |
|
if (nidbytes) |
235 |
< |
fread(ip, nidbytes, 1, fp); |
235 |
> |
fread((char *)ip, nidbytes, 1, fp); |
236 |
|
else |
237 |
|
*ip = '\0'; |
238 |
|
else if (nidbytes) |
283 |
|
p->fp = stdin; |
284 |
|
else if ((p->fp = fopen(fname, "r")) == NULL) |
285 |
|
return(NULL); |
286 |
< |
/* discard header */ |
287 |
< |
getheader(p->fp, NULL); |
288 |
< |
if (fscanf(p->fp, "-Y %d +X %d\n", &ymax, &xmax) != 2) |
289 |
< |
quiterr("bad picture size"); |
286 |
> |
/* get header info. */ |
287 |
> |
if (checkheader(p->fp, COLRFMT, NULL) < 0 || |
288 |
> |
fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR)) |
289 |
> |
quiterr("bad picture format"); |
290 |
|
p->nexty = 0; |
291 |
|
p->bytes_line = 0; /* variable length lines */ |
292 |
|
p->pos.y = (long *)ecalloc(ymax, sizeof(long)); |
305 |
|
h->dataBits = 8; |
306 |
|
h->imType = 0; |
307 |
|
/* allocate scanline */ |
308 |
< |
inline = (COLR *)emalloc(xmax*sizeof(COLR)); |
308 |
> |
inl = (COLR *)emalloc(xmax*sizeof(COLR)); |
309 |
|
/* allocate targa data */ |
310 |
|
tarData = taralloc(h); |
311 |
|
|
325 |
|
register int i, j; |
326 |
|
|
327 |
|
/* get color table */ |
328 |
< |
if ((hp->CMapBits==24 ? fread(map.c3, sizeof(map.c3), 1, stdin) : |
329 |
< |
fread(map.c4, sizeof(map.c4), 1, stdin)) != 1) |
328 |
> |
if ((hp->CMapBits==24 ? fread((char *)map.c3,sizeof(map.c3),1,stdin) : |
329 |
> |
fread((char *)map.c4,sizeof(map.c4),1,stdin)) != 1) |
330 |
|
quiterr("error reading color table"); |
331 |
|
/* convert table */ |
332 |
|
for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++) |
363 |
|
int y; |
364 |
|
register rgbpixel *l3; |
365 |
|
{ |
366 |
< |
register BYTE *l4; |
362 |
< |
register int shift, c; |
363 |
< |
int i; |
366 |
> |
register int i; |
367 |
|
|
368 |
< |
if (inpic->nexty != y) { /* find scanline */ |
368 |
> |
if (inpic->nexty != y) { /* find scanline */ |
369 |
|
if (inpic->bytes_line == 0) { |
370 |
|
if (inpic->pos.y[y] == 0) { |
371 |
|
while (inpic->nexty < y) { |
372 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) |
372 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) |
373 |
|
quiterr("read error in picreadline3"); |
374 |
|
inpic->pos.y[++inpic->nexty] = ftell(inpic->fp); |
375 |
|
} |
379 |
|
quiterr("seek error in picreadline3"); |
380 |
|
} else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0) |
381 |
|
inpic->pos.y[inpic->nexty] = ftell(inpic->fp); |
382 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) /* read scanline */ |
382 |
> |
if (freadcolrs(inl, xmax, inpic->fp) < 0) /* read scanline */ |
383 |
|
quiterr("read error in picreadline3"); |
384 |
|
inpic->nexty = y+1; |
385 |
|
/* convert scanline */ |
386 |
< |
for (l4=inline[0], i=xmax; i--; l4+=4, l3++) { |
387 |
< |
shift = l4[EXP] - COLXS; |
388 |
< |
if (shift >= 8) { |
389 |
< |
l3->r = l3->g = l3->b = 255; |
390 |
< |
} 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 |
< |
} |
386 |
> |
normcolrs(inl, xmax, 0); |
387 |
> |
for (i = 0; i < xmax; i++) { |
388 |
> |
l3[i].r = inl[i][RED]; |
389 |
> |
l3[i].g = inl[i][GRN]; |
390 |
> |
l3[i].b = inl[i][BLU]; |
391 |
|
} |
392 |
|
} |
393 |
|
|
396 |
|
int y; |
397 |
|
pixel *l; |
398 |
|
{ |
399 |
< |
bcopy(l, &tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel)); |
399 |
> |
bcopy((char *)l, (char *)&tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel)); |
400 |
|
} |
401 |
|
|
402 |
|
|
406 |
|
FILE *fp; |
407 |
|
{ |
408 |
|
if (h->dataType == IM_CMAP) { /* uncompressed */ |
409 |
< |
if (fwrite(d, h->x*sizeof(pixel), h->y, fp) != h->y) |
409 |
> |
if (fwrite((char *)d,h->x*sizeof(pixel),h->y,fp) != h->y) |
410 |
|
quiterr("error writing targa file"); |
411 |
|
return; |
412 |
|
} |
423 |
|
register pixel *dp; |
424 |
|
|
425 |
|
if (h->dataType == IM_CMAP) { /* uncompressed */ |
426 |
< |
if (fread(data, h->x*sizeof(pixel), h->y, fp) != h->y) |
426 |
> |
if (fread((char *)data,h->x*sizeof(pixel),h->y,fp) != h->y) |
427 |
|
goto readerr; |
428 |
|
return; |
429 |
|
} |
466 |
|
register int i, val; |
467 |
|
|
468 |
|
for (i = 0; i < 256; i++) { |
469 |
< |
val = pow(i/256.0, 1.0/gamma) * 256.0; |
469 |
> |
val = pow((i+0.5)/256.0, 1.0/gamma) * 256.0; |
470 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
471 |
|
} |
472 |
|
} |