1 |
< |
/* Copyright (c) 1986 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 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" |
23 |
|
|
24 |
< |
#ifndef BSD |
25 |
< |
#define bcopy(s,d,n) (void)memcpy(d,s,n) |
24 |
> |
#ifdef MSDOS |
25 |
> |
#include <fcntl.h> |
26 |
> |
#endif |
27 |
> |
|
28 |
> |
#include <math.h> |
29 |
> |
|
30 |
> |
#ifndef BSD |
31 |
> |
#define bcopy(s,d,n) (void)memcpy(d,s,n) |
32 |
|
extern char *memcpy(); |
33 |
|
#endif |
34 |
|
/* descriptor for a picture file or frame buffer */ |
43 |
|
} pos; /* position(s) */ |
44 |
|
} pic; |
45 |
|
|
46 |
< |
#define goodpic(h) (my_imType(h) && my_mapType(h)) |
47 |
< |
#define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \ |
46 |
> |
#define goodpic(h) (my_imType(h) && my_mapType(h)) |
47 |
> |
#define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \ |
48 |
|
&& (h)->dataBits==8 && (h)->imType==0) |
49 |
< |
#define my_mapType(h) ((h)->mapType==CM_HASMAP && \ |
49 |
> |
#define my_mapType(h) ((h)->mapType==CM_HASMAP && \ |
50 |
|
((h)->CMapBits==24 || (h)->CMapBits==32)) |
51 |
|
|
52 |
< |
#define taralloc(h) (pixel *)emalloc((h)->x*(h)->y*sizeof(pixel)) |
52 |
> |
#define taralloc(h) (pixel *)emalloc((h)->x*(h)->y*sizeof(pixel)) |
53 |
|
|
54 |
|
extern pic *openinput(); |
55 |
|
|
57 |
|
|
58 |
|
extern long ftell(); |
59 |
|
|
60 |
< |
extern double atof(), pow(); |
60 |
> |
double gamma = 2.2; /* gamma correction */ |
61 |
|
|
62 |
< |
double gamma = 2.0; /* gamma correction */ |
62 |
> |
int bradj = 0; /* brightness adjustment */ |
63 |
|
|
64 |
|
pic *inpic; |
65 |
|
|
79 |
|
char *argv[]; |
80 |
|
{ |
81 |
|
colormap rasmap; |
82 |
< |
struct hdStruct head; |
82 |
> |
struct hdStruct head; |
83 |
|
int dither = 1; |
84 |
|
int reverse = 0; |
85 |
|
int ncolors = 256; |
86 |
|
int greyscale = 0; |
87 |
|
int i; |
88 |
< |
|
88 |
> |
#ifdef MSDOS |
89 |
> |
extern int _fmode; |
90 |
> |
_fmode = O_BINARY; |
91 |
> |
setmode(fileno(stdin), O_BINARY); |
92 |
> |
setmode(fileno(stdout), O_BINARY); |
93 |
> |
#endif |
94 |
|
progname = argv[0]; |
95 |
|
|
96 |
|
for (i = 1; i < argc; i++) |
108 |
|
case 'b': |
109 |
|
greyscale = 1; |
110 |
|
break; |
111 |
+ |
case 'e': |
112 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
113 |
+ |
goto userr; |
114 |
+ |
bradj = atoi(argv[++i]); |
115 |
+ |
break; |
116 |
|
case 'c': |
117 |
|
ncolors = atoi(argv[++i]); |
118 |
|
break; |
143 |
|
} |
144 |
|
/* put header */ |
145 |
|
printargs(i, argv, stdout); |
146 |
+ |
fputformat(COLRFMT, stdout); |
147 |
|
putchar('\n'); |
148 |
< |
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
148 |
> |
fprtresolu(xmax, ymax, stdout); |
149 |
|
/* convert file */ |
150 |
|
tg2ra(&head); |
151 |
|
} else { |
152 |
< |
if (i > argc-1 || i < argc-2) |
152 |
> |
if (i < argc-2 || (!greyscale && i > argc-1)) |
153 |
|
goto userr; |
154 |
|
if ((inpic = openinput(argv[i], &head)) == NULL) { |
155 |
|
sprintf(errmsg, "can't open input \"%s\"", argv[i]); |
172 |
|
quiterr(NULL); |
173 |
|
userr: |
174 |
|
fprintf(stderr, |
175 |
< |
"Usage: %s [-d][-c ncolors][-b][-g gamma] input [output]\n", |
175 |
> |
"Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n", |
176 |
|
progname); |
177 |
< |
fprintf(stderr, " Or: %s -r [-g gamma] [input [output]]\n", |
177 |
> |
fprintf(stderr, " Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n", |
178 |
|
progname); |
179 |
|
exit(1); |
180 |
|
} |
228 |
|
|
229 |
|
|
230 |
|
getthead(hp, ip, fp) /* read header from input */ |
231 |
< |
struct hdStruct *hp; |
231 |
> |
struct hdStruct *hp; |
232 |
|
char *ip; |
233 |
|
register FILE *fp; |
234 |
|
{ |
261 |
|
|
262 |
|
|
263 |
|
putthead(hp, ip, fp) /* write header to output */ |
264 |
< |
struct hdStruct *hp; |
264 |
> |
struct hdStruct *hp; |
265 |
|
char *ip; |
266 |
|
register FILE *fp; |
267 |
|
{ |
301 |
|
p->fp = stdin; |
302 |
|
else if ((p->fp = fopen(fname, "r")) == NULL) |
303 |
|
return(NULL); |
304 |
< |
/* discard header */ |
305 |
< |
getheader(p->fp, NULL); |
306 |
< |
if (fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR)) |
307 |
< |
quiterr("bad picture size"); |
304 |
> |
/* get header info. */ |
305 |
> |
if (checkheader(p->fp, COLRFMT, NULL) < 0 || |
306 |
> |
fgetresolu(&xmax, &ymax, p->fp) < 0) |
307 |
> |
quiterr("bad picture format"); |
308 |
|
p->nexty = 0; |
309 |
|
p->bytes_line = 0; /* variable length lines */ |
310 |
|
p->pos.y = (long *)ecalloc(ymax, sizeof(long)); |
332 |
|
|
333 |
|
|
334 |
|
tg2ra(hp) /* targa file to RADIANCE file */ |
335 |
< |
struct hdStruct *hp; |
335 |
> |
struct hdStruct *hp; |
336 |
|
{ |
337 |
|
union { |
338 |
|
BYTE c3[256][3]; |
343 |
|
register int i, j; |
344 |
|
|
345 |
|
/* get color table */ |
346 |
< |
if ((hp->CMapBits==24 ? fread((char *)map.c3,sizeof(map.c3),1,stdin) : |
347 |
< |
fread((char *)map.c4,sizeof(map.c4),1,stdin)) != 1) |
346 |
> |
if ((hp->CMapBits==24 ? fread((char *)(map.c3+hp->mapOrig), |
347 |
> |
3*hp->mapLength,1,stdin) : |
348 |
> |
fread((char *)(map.c4+hp->mapOrig), |
349 |
> |
4*hp->mapLength,1,stdin)) != 1) |
350 |
|
quiterr("error reading color table"); |
351 |
|
/* convert table */ |
352 |
|
for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++) |
360 |
|
pow((map.c4[i][3]+.5)/256.,gamma), |
361 |
|
pow((map.c4[i][2]+.5)/256.,gamma), |
362 |
|
pow((map.c4[i][1]+.5)/256.,gamma)); |
363 |
< |
|
363 |
> |
if (bradj) |
364 |
> |
shiftcolrs(ctab, 256, bradj); |
365 |
|
/* allocate targa data */ |
366 |
|
tarData = taralloc(hp); |
367 |
|
/* get data */ |
404 |
|
quiterr("read error in picreadline3"); |
405 |
|
inpic->nexty = y+1; |
406 |
|
/* convert scanline */ |
407 |
< |
normcolrs(inl, xmax, 0); |
407 |
> |
normcolrs(inl, xmax, bradj); |
408 |
|
for (i = 0; i < xmax; i++) { |
409 |
|
l3[i].r = inl[i][RED]; |
410 |
|
l3[i].g = inl[i][GRN]; |
422 |
|
|
423 |
|
|
424 |
|
writetarga(h, d, fp) /* write out targa data */ |
425 |
< |
struct hdStruct *h; |
425 |
> |
struct hdStruct *h; |
426 |
|
pixel *d; |
427 |
|
FILE *fp; |
428 |
|
{ |
436 |
|
|
437 |
|
|
438 |
|
readtarga(h, data, fp) /* read in targa data */ |
439 |
< |
struct hdStruct *h; |
439 |
> |
struct hdStruct *h; |
440 |
|
pixel *data; |
441 |
|
FILE *fp; |
442 |
|
{ |
487 |
|
register int i, val; |
488 |
|
|
489 |
|
for (i = 0; i < 256; i++) { |
490 |
< |
val = pow(i/256.0, 1.0/gamma) * 256.0; |
490 |
> |
val = pow((i+0.5)/256.0, 1.0/gamma) * 256.0; |
491 |
|
map[0][i] = map[1][i] = map[2][i] = val; |
492 |
|
} |
493 |
|
} |