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 "pic.h" |
18 |
> |
#include "resolu.h" |
19 |
|
|
20 |
|
#include "targa.h" |
21 |
|
|
22 |
< |
/* descriptor for a picture file or frame buffer */ |
23 |
< |
typedef struct { |
24 |
< |
char *name; /* file name */ |
25 |
< |
FILE *fp; /* file pointer */ |
26 |
< |
int nexty; /* file positioning */ |
27 |
< |
int bytes_line; /* 0 == variable length lines */ |
28 |
< |
union { |
29 |
< |
long b; /* initial scanline */ |
30 |
< |
long *y; /* individual scanline */ |
31 |
< |
} pos; /* position(s) */ |
32 |
< |
} pic; |
22 |
> |
#ifdef MSDOS |
23 |
> |
#include <fcntl.h> |
24 |
> |
#endif |
25 |
|
|
26 |
< |
#define goodpic(h) (my_imType(h) && my_mapType(h)) |
27 |
< |
#define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \ |
26 |
> |
#include <math.h> |
27 |
> |
|
28 |
> |
#ifndef BSD |
29 |
> |
#define bcopy(s,d,n) (void)memcpy(d,s,n) |
30 |
> |
extern char *memcpy(); |
31 |
> |
#endif |
32 |
> |
|
33 |
> |
#define goodpic(h) (my_imType(h) && my_mapType(h)) |
34 |
> |
#define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \ |
35 |
|
&& (h)->dataBits==8 && (h)->imType==0) |
36 |
< |
#define my_mapType(h) ((h)->mapType==CM_HASMAP && \ |
36 |
> |
#define my_mapType(h) ((h)->mapType==CM_HASMAP && \ |
37 |
|
((h)->CMapBits==24 || (h)->CMapBits==32)) |
38 |
|
|
39 |
< |
#define taralloc(h) (pixel *)emalloc((h)->x*(h)->y*sizeof(pixel)) |
39 |
> |
#define taralloc(h) (BYTE *)emalloc((h)->x*(h)->y) |
40 |
|
|
41 |
< |
extern pic *openinput(); |
41 |
> |
extern BYTE clrtab[][3]; |
42 |
|
|
43 |
|
extern char *ecalloc(), *emalloc(); |
44 |
|
|
45 |
|
extern long ftell(); |
46 |
|
|
47 |
< |
extern double atof(), pow(); |
47 |
> |
double gamv = 2.2; /* gamv correction */ |
48 |
|
|
49 |
< |
double gamma = 2.0; /* gamma correction */ |
49 |
> |
int bradj = 0; /* brightness adjustment */ |
50 |
|
|
52 |
– |
pic *inpic; |
53 |
– |
|
51 |
|
char *progname; |
52 |
|
|
53 |
|
char errmsg[128]; |
54 |
|
|
55 |
< |
COLR *inline; |
55 |
> |
COLR *inl; |
56 |
|
|
57 |
< |
pixel *tarData; |
57 |
> |
BYTE *tarData; |
58 |
|
|
59 |
|
int xmax, ymax; |
60 |
|
|
63 |
|
int argc; |
64 |
|
char *argv[]; |
65 |
|
{ |
66 |
< |
colormap rasmap; |
70 |
< |
struct hdStruct head; |
66 |
> |
struct hdStruct head; |
67 |
|
int dither = 1; |
68 |
|
int reverse = 0; |
69 |
|
int ncolors = 256; |
70 |
|
int greyscale = 0; |
71 |
|
int i; |
72 |
< |
|
72 |
> |
#ifdef MSDOS |
73 |
> |
extern int _fmode; |
74 |
> |
_fmode = O_BINARY; |
75 |
> |
setmode(fileno(stdin), O_BINARY); |
76 |
> |
setmode(fileno(stdout), O_BINARY); |
77 |
> |
#endif |
78 |
|
progname = argv[0]; |
79 |
|
|
80 |
|
for (i = 1; i < argc; i++) |
84 |
|
dither = !dither; |
85 |
|
break; |
86 |
|
case 'g': |
87 |
< |
gamma = atof(argv[++i]); |
87 |
> |
gamv = atof(argv[++i]); |
88 |
|
break; |
89 |
|
case 'r': |
90 |
|
reverse = !reverse; |
92 |
|
case 'b': |
93 |
|
greyscale = 1; |
94 |
|
break; |
95 |
+ |
case 'e': |
96 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
97 |
+ |
goto userr; |
98 |
+ |
bradj = atoi(argv[++i]); |
99 |
+ |
break; |
100 |
|
case 'c': |
101 |
|
ncolors = atoi(argv[++i]); |
102 |
|
break; |
106 |
|
else |
107 |
|
break; |
108 |
|
|
109 |
+ |
if (i < argc-2) |
110 |
+ |
goto userr; |
111 |
+ |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
112 |
+ |
sprintf(errmsg, "cannot open input \"%s\"", argv[i]); |
113 |
+ |
quiterr(errmsg); |
114 |
+ |
} |
115 |
+ |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
116 |
+ |
sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]); |
117 |
+ |
quiterr(errmsg); |
118 |
+ |
} |
119 |
|
if (reverse) { |
104 |
– |
if (i < argc-2) |
105 |
– |
goto userr; |
106 |
– |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
107 |
– |
sprintf(errmsg, "can't open input \"%s\"", argv[i]); |
108 |
– |
quiterr(errmsg); |
109 |
– |
} |
120 |
|
/* get header */ |
121 |
|
if (getthead(&head, NULL, stdin) < 0) |
122 |
|
quiterr("bad targa file"); |
124 |
|
quiterr("incompatible format"); |
125 |
|
xmax = head.x; |
126 |
|
ymax = head.y; |
117 |
– |
/* open output file */ |
118 |
– |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
119 |
– |
sprintf(errmsg, "can't open output \"%s\"", argv[i+1]); |
120 |
– |
quiterr(errmsg); |
121 |
– |
} |
127 |
|
/* put header */ |
128 |
< |
printargs(argc, argv, stdout); |
128 |
> |
printargs(i, argv, stdout); |
129 |
> |
fputformat(COLRFMT, stdout); |
130 |
|
putchar('\n'); |
131 |
< |
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
131 |
> |
fprtresolu(xmax, ymax, stdout); |
132 |
|
/* convert file */ |
133 |
|
tg2ra(&head); |
134 |
|
} else { |
135 |
< |
if (i > argc-1 || i < argc-2) |
136 |
< |
goto userr; |
131 |
< |
if ((inpic = openinput(argv[i], &head)) == NULL) { |
132 |
< |
sprintf(errmsg, "can't open input \"%s\"", argv[i]); |
133 |
< |
quiterr(errmsg); |
134 |
< |
} |
135 |
< |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
136 |
< |
sprintf(errmsg, "can't open output \"%s\"", argv[i+1]); |
137 |
< |
quiterr(errmsg); |
138 |
< |
} |
135 |
> |
if (getrhead(&head, stdin) < 0) |
136 |
> |
quiterr("bad Radiance input"); |
137 |
|
/* write header */ |
138 |
|
putthead(&head, NULL, stdout); |
139 |
|
/* convert file */ |
140 |
|
if (greyscale) |
141 |
< |
biq(dither,ncolors,1,rasmap); |
141 |
> |
getgrey(ncolors); |
142 |
|
else |
143 |
< |
ciq(dither,ncolors,1,rasmap); |
143 |
> |
getmapped(ncolors, dither); |
144 |
|
/* write data */ |
145 |
|
writetarga(&head, tarData, stdout); |
146 |
|
} |
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 gamv][-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 gamv][-e +/-stops] [input [output]]\n", |
153 |
|
progname); |
154 |
|
exit(1); |
155 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
getthead(hp, ip, fp) /* read header from input */ |
206 |
< |
struct hdStruct *hp; |
206 |
> |
struct hdStruct *hp; |
207 |
|
char *ip; |
208 |
|
register FILE *fp; |
209 |
|
{ |
225 |
|
|
226 |
|
if (ip != NULL) |
227 |
|
if (nidbytes) |
228 |
< |
fread(ip, nidbytes, 1, fp); |
228 |
> |
fread((char *)ip, nidbytes, 1, fp); |
229 |
|
else |
230 |
|
*ip = '\0'; |
231 |
|
else if (nidbytes) |
236 |
|
|
237 |
|
|
238 |
|
putthead(hp, ip, fp) /* write header to output */ |
239 |
< |
struct hdStruct *hp; |
239 |
> |
struct hdStruct *hp; |
240 |
|
char *ip; |
241 |
|
register FILE *fp; |
242 |
|
{ |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
< |
pic * |
269 |
< |
openinput(fname, h) /* open RADIANCE input file */ |
270 |
< |
char *fname; |
266 |
> |
getrhead(h, fp) /* load RADIANCE input file header */ |
267 |
|
register struct hdStruct *h; |
268 |
+ |
FILE *fp; |
269 |
|
{ |
270 |
< |
register pic *p; |
271 |
< |
|
272 |
< |
p = (pic *)emalloc(sizeof(pic)); |
273 |
< |
p->name = fname; |
277 |
< |
if (fname == NULL) |
278 |
< |
p->fp = stdin; |
279 |
< |
else if ((p->fp = fopen(fname, "r")) == NULL) |
280 |
< |
return(NULL); |
281 |
< |
/* discard header */ |
282 |
< |
getheader(p->fp, NULL); |
283 |
< |
if (fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR)) |
284 |
< |
quiterr("bad picture size"); |
285 |
< |
p->nexty = 0; |
286 |
< |
p->bytes_line = 0; /* variable length lines */ |
287 |
< |
p->pos.y = (long *)ecalloc(ymax, sizeof(long)); |
288 |
< |
p->pos.y[0] = ftell(p->fp); |
270 |
> |
/* get header info. */ |
271 |
> |
if (checkheader(fp, COLRFMT, NULL) < 0 || |
272 |
> |
fgetresolu(&xmax, &ymax, fp) < 0) |
273 |
> |
return(-1); |
274 |
|
/* assign header */ |
275 |
|
h->textSize = 0; |
276 |
|
h->mapType = CM_HASMAP; |
285 |
|
h->dataBits = 8; |
286 |
|
h->imType = 0; |
287 |
|
/* allocate scanline */ |
288 |
< |
inline = (COLR *)emalloc(xmax*sizeof(COLR)); |
288 |
> |
inl = (COLR *)emalloc(xmax*sizeof(COLR)); |
289 |
|
/* allocate targa data */ |
290 |
|
tarData = taralloc(h); |
291 |
|
|
292 |
< |
return(p); |
292 |
> |
return(0); |
293 |
|
} |
294 |
|
|
295 |
|
|
296 |
|
tg2ra(hp) /* targa file to RADIANCE file */ |
297 |
< |
struct hdStruct *hp; |
297 |
> |
struct hdStruct *hp; |
298 |
|
{ |
299 |
|
union { |
300 |
|
BYTE c3[256][3]; |
305 |
|
register int i, j; |
306 |
|
|
307 |
|
/* get color table */ |
308 |
< |
if ((hp->CMapBits==24 ? fread(map.c3, sizeof(map.c3), 1, stdin) : |
309 |
< |
fread(map.c4, sizeof(map.c4), 1, stdin)) != 1) |
308 |
> |
if ((hp->CMapBits==24 ? fread((char *)(map.c3+hp->mapOrig), |
309 |
> |
3*hp->mapLength,1,stdin) : |
310 |
> |
fread((char *)(map.c4+hp->mapOrig), |
311 |
> |
4*hp->mapLength,1,stdin)) != 1) |
312 |
|
quiterr("error reading color table"); |
313 |
|
/* convert table */ |
314 |
|
for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++) |
315 |
|
if (hp->CMapBits == 24) |
316 |
|
setcolr(ctab[i], |
317 |
< |
pow((map.c3[i][2]+.5)/256.,gamma), |
318 |
< |
pow((map.c3[i][1]+.5)/256.,gamma), |
319 |
< |
pow((map.c3[i][0]+.5)/256.,gamma)); |
317 |
> |
pow((map.c3[i][2]+.5)/256.,gamv), |
318 |
> |
pow((map.c3[i][1]+.5)/256.,gamv), |
319 |
> |
pow((map.c3[i][0]+.5)/256.,gamv)); |
320 |
|
else |
321 |
|
setcolr(ctab[i], |
322 |
< |
pow((map.c4[i][3]+.5)/256.,gamma), |
323 |
< |
pow((map.c4[i][2]+.5)/256.,gamma), |
324 |
< |
pow((map.c4[i][1]+.5)/256.,gamma)); |
325 |
< |
|
322 |
> |
pow((map.c4[i][3]+.5)/256.,gamv), |
323 |
> |
pow((map.c4[i][2]+.5)/256.,gamv), |
324 |
> |
pow((map.c4[i][1]+.5)/256.,gamv)); |
325 |
> |
if (bradj) |
326 |
> |
shiftcolrs(ctab, 256, bradj); |
327 |
|
/* allocate targa data */ |
328 |
|
tarData = taralloc(hp); |
329 |
|
/* get data */ |
342 |
|
} |
343 |
|
|
344 |
|
|
345 |
< |
picreadline3(y, l3) /* read in 3-byte scanline */ |
346 |
< |
int y; |
347 |
< |
register rgbpixel *l3; |
345 |
> |
getmapped(nc, dith) /* read in and quantize image */ |
346 |
> |
int nc; /* number of colors to use */ |
347 |
> |
int dith; /* use dithering? */ |
348 |
|
{ |
349 |
< |
register BYTE *l4; |
350 |
< |
register int shift, c; |
363 |
< |
int i; |
349 |
> |
long fpos; |
350 |
> |
register int y; |
351 |
|
|
352 |
< |
if (inpic->nexty != y) { /* find scanline */ |
353 |
< |
if (inpic->bytes_line == 0) { |
354 |
< |
if (inpic->pos.y[y] == 0) { |
355 |
< |
while (inpic->nexty < y) { |
356 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) |
357 |
< |
quiterr("read error in picreadline3"); |
358 |
< |
inpic->pos.y[++inpic->nexty] = ftell(inpic->fp); |
359 |
< |
} |
360 |
< |
} else if (fseek(inpic->fp, inpic->pos.y[y], 0) == EOF) |
361 |
< |
quiterr("seek error in picreadline3"); |
375 |
< |
} else if (fseek(inpic->fp, y*inpic->bytes_line+inpic->pos.b, 0) == EOF) |
376 |
< |
quiterr("seek error in picreadline3"); |
377 |
< |
} else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0) |
378 |
< |
inpic->pos.y[inpic->nexty] = ftell(inpic->fp); |
379 |
< |
if (freadcolrs(inline, xmax, inpic->fp) < 0) /* read scanline */ |
380 |
< |
quiterr("read error in picreadline3"); |
381 |
< |
inpic->nexty = y+1; |
382 |
< |
/* convert scanline */ |
383 |
< |
for (l4=inline[0], i=xmax; i--; l4+=4, l3++) { |
384 |
< |
shift = l4[EXP] - COLXS; |
385 |
< |
if (shift >= 8) { |
386 |
< |
l3->r = l3->g = l3->b = 255; |
387 |
< |
} 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 |
< |
} |
352 |
> |
setcolrgam(gamv); |
353 |
> |
fpos = ftell(stdin); |
354 |
> |
new_histo(); /* build histogram */ |
355 |
> |
for (y = ymax-1; y >= 0; y--) { |
356 |
> |
if (freadcolrs(inl, xmax, stdin) < 0) |
357 |
> |
quiterr("error reading Radiance input"); |
358 |
> |
if (bradj) |
359 |
> |
shiftcolrs(inl, xmax, bradj); |
360 |
> |
colrs_gambs(inl, xmax); |
361 |
> |
cnt_colrs(inl, xmax); |
362 |
|
} |
363 |
+ |
if (fseek(stdin, fpos, 0) == EOF) |
364 |
+ |
quiterr("Radiance input must be from a file"); |
365 |
+ |
new_clrtab(nc); /* map colors */ |
366 |
+ |
for (y = ymax-1; y >= 0; y--) { |
367 |
+ |
if (freadcolrs(inl, xmax, stdin) < 0) |
368 |
+ |
quiterr("error reading Radiance input"); |
369 |
+ |
if (bradj) |
370 |
+ |
shiftcolrs(inl, xmax, bradj); |
371 |
+ |
colrs_gambs(inl, xmax); |
372 |
+ |
if (dith) |
373 |
+ |
dith_colrs(tarData+y*xmax, inl, xmax); |
374 |
+ |
else |
375 |
+ |
map_colrs(tarData+y*xmax, inl, xmax); |
376 |
+ |
} |
377 |
|
} |
378 |
|
|
379 |
|
|
380 |
< |
picwriteline(y, l) /* save output scanline */ |
381 |
< |
int y; |
411 |
< |
pixel *l; |
380 |
> |
getgrey(nc) /* read in and convert to greyscale image */ |
381 |
> |
int nc; /* number of colors to use */ |
382 |
|
{ |
383 |
< |
bcopy(l, &tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel)); |
383 |
> |
int y; |
384 |
> |
register BYTE *dp; |
385 |
> |
register int x; |
386 |
> |
|
387 |
> |
setcolrgam(gamv); |
388 |
> |
dp = tarData+xmax*ymax;; |
389 |
> |
for (y = ymax-1; y >= 0; y--) { |
390 |
> |
if (freadcolrs(inl, xmax, stdin) < 0) |
391 |
> |
quiterr("error reading Radiance input"); |
392 |
> |
if (bradj) |
393 |
> |
shiftcolrs(inl, xmax, bradj); |
394 |
> |
x = xmax; |
395 |
> |
while (x--) |
396 |
> |
inl[x][GRN] = normbright(inl[x]); |
397 |
> |
colrs_gambs(inl, xmax); |
398 |
> |
x = xmax; |
399 |
> |
if (nc < 256) |
400 |
> |
while (x--) |
401 |
> |
*--dp = ((long)inl[x][GRN]*nc+nc/2)>>8; |
402 |
> |
else |
403 |
> |
while (x--) |
404 |
> |
*--dp = inl[x][GRN]; |
405 |
> |
} |
406 |
> |
for (x = 0; x < nc; x++) |
407 |
> |
clrtab[x][RED] = clrtab[x][GRN] = |
408 |
> |
clrtab[x][BLU] = ((long)x*256+128)/nc; |
409 |
|
} |
410 |
|
|
411 |
|
|
412 |
|
writetarga(h, d, fp) /* write out targa data */ |
413 |
< |
struct hdStruct *h; |
414 |
< |
pixel *d; |
413 |
> |
struct hdStruct *h; |
414 |
> |
BYTE *d; |
415 |
|
FILE *fp; |
416 |
|
{ |
417 |
+ |
register int i, j; |
418 |
+ |
|
419 |
+ |
for (i = 0; i < h->mapLength; i++) /* write color map */ |
420 |
+ |
for (j = 2; j >= 0; j--) |
421 |
+ |
putc(clrtab[i][j], fp); |
422 |
|
if (h->dataType == IM_CMAP) { /* uncompressed */ |
423 |
< |
if (fwrite(d, h->x*sizeof(pixel), h->y, fp) != h->y) |
423 |
> |
if (fwrite((char *)d,h->x*sizeof(BYTE),h->y,fp) != h->y) |
424 |
|
quiterr("error writing targa file"); |
425 |
|
return; |
426 |
|
} |
429 |
|
|
430 |
|
|
431 |
|
readtarga(h, data, fp) /* read in targa data */ |
432 |
< |
struct hdStruct *h; |
433 |
< |
pixel *data; |
432 |
> |
struct hdStruct *h; |
433 |
> |
BYTE *data; |
434 |
|
FILE *fp; |
435 |
|
{ |
436 |
|
register int cnt, c; |
437 |
< |
register pixel *dp; |
437 |
> |
register BYTE *dp; |
438 |
|
|
439 |
|
if (h->dataType == IM_CMAP) { /* uncompressed */ |
440 |
< |
if (fread(data, h->x*sizeof(pixel), h->y, fp) != h->y) |
440 |
> |
if (fread((char *)data,h->x*sizeof(BYTE),h->y,fp) != h->y) |
441 |
|
goto readerr; |
442 |
|
return; |
443 |
|
} |
460 |
|
return; |
461 |
|
readerr: |
462 |
|
quiterr("error reading targa file"); |
463 |
– |
} |
464 |
– |
|
465 |
– |
|
466 |
– |
picwritecm(cm) /* write out color map */ |
467 |
– |
colormap cm; |
468 |
– |
{ |
469 |
– |
register int i, j; |
470 |
– |
|
471 |
– |
for (j = 0; j < 256; j++) |
472 |
– |
for (i = 2; i >= 0; i--) |
473 |
– |
putc(cm[i][j], stdout); |
474 |
– |
} |
475 |
– |
|
476 |
– |
|
477 |
– |
picreadcm(map) /* do gamma correction if requested */ |
478 |
– |
colormap map; |
479 |
– |
{ |
480 |
– |
register int i, val; |
481 |
– |
|
482 |
– |
for (i = 0; i < 256; i++) { |
483 |
– |
val = pow(i/256.0, 1.0/gamma) * 256.0; |
484 |
– |
map[0][i] = map[1][i] = map[2][i] = val; |
485 |
– |
} |
463 |
|
} |