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"; |
13 |
|
|
14 |
|
#include <stdio.h> |
15 |
|
|
16 |
+ |
#ifdef MSDOS |
17 |
+ |
#include <fcntl.h> |
18 |
+ |
#endif |
19 |
+ |
|
20 |
+ |
#include <math.h> |
21 |
+ |
|
22 |
|
#include "color.h" |
23 |
|
|
24 |
+ |
#include "resolu.h" |
25 |
+ |
|
26 |
|
#include "random.h" |
27 |
|
|
28 |
|
#include "targa.h" |
40 |
|
|
41 |
|
extern char *ecalloc(), *emalloc(); |
42 |
|
|
43 |
< |
extern double atof(), pow(); |
43 |
> |
double gamma = 2.2; /* gamma correction */ |
44 |
|
|
45 |
< |
double gamma = 2.0; /* gamma correction */ |
45 |
> |
int bradj = 0; /* brightness adjustment */ |
46 |
|
|
47 |
|
char *progname; |
48 |
|
|
56 |
|
struct hdStruct head; |
57 |
|
int reverse = 0; |
58 |
|
int i; |
59 |
< |
|
59 |
> |
#ifdef MSDOS |
60 |
> |
extern int _fmode; |
61 |
> |
_fmode = O_BINARY; |
62 |
> |
setmode(fileno(stdin), O_BINARY); |
63 |
> |
setmode(fileno(stdout), O_BINARY); |
64 |
> |
#endif |
65 |
|
progname = argv[0]; |
66 |
|
|
67 |
|
head.dataBits = 16; |
80 |
|
case '3': |
81 |
|
head.dataBits = 24; |
82 |
|
break; |
83 |
+ |
case 'e': |
84 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
85 |
+ |
goto userr; |
86 |
+ |
bradj = atoi(argv[++i]); |
87 |
+ |
break; |
88 |
|
default: |
89 |
|
goto userr; |
90 |
|
} |
103 |
|
sprintf(msg, "can't open output \"%s\"", argv[i+1]); |
104 |
|
quiterr(msg); |
105 |
|
} |
106 |
+ |
/* set gamma */ |
107 |
+ |
setcolrgam(gamma); |
108 |
|
/* convert */ |
109 |
|
if (reverse) { |
110 |
|
/* get header */ |
113 |
|
if (!goodpic(&head)) |
114 |
|
quiterr("incompatible format"); |
115 |
|
/* put header */ |
116 |
< |
printargs(argc, argv, stdout); |
116 |
> |
printargs(i, argv, stdout); |
117 |
> |
fputformat(COLRFMT, stdout); |
118 |
|
putchar('\n'); |
119 |
< |
fputresolu(YMAJOR|YDECR, head.x, head.y, stdout); |
119 |
> |
fprtresolu(head.x, head.y, stdout); |
120 |
|
/* convert file */ |
121 |
|
tg2ra(&head); |
122 |
|
} else { |
123 |
< |
getheader(stdin, NULL); |
124 |
< |
if (fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR)) |
123 |
> |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
124 |
> |
fgetresolu(&head.x, &head.y, stdin) < 0) |
125 |
|
quiterr("bad picture file"); |
126 |
|
/* assign header */ |
127 |
|
head.textSize = 0; |
137 |
|
} |
138 |
|
exit(0); |
139 |
|
userr: |
140 |
< |
fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma] [input [output]]\n", |
140 |
> |
fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma][-e +/-stops] [input [output]]\n", |
141 |
|
progname); |
142 |
|
exit(1); |
143 |
|
} |
210 |
|
|
211 |
|
if (ip != NULL) |
212 |
|
if (nidbytes) |
213 |
< |
fread(ip, nidbytes, 1, fp); |
213 |
> |
fread((char *)ip, nidbytes, 1, fp); |
214 |
|
else |
215 |
|
*ip = '\0'; |
216 |
|
else if (nidbytes) |
251 |
|
tg2ra(hp) /* targa file to RADIANCE file */ |
252 |
|
struct hdStruct *hp; |
253 |
|
{ |
254 |
< |
float gmap[256]; |
234 |
< |
COLOR *scanline; |
254 |
> |
COLR *scanline; |
255 |
|
unsigned char *tarData; |
256 |
|
register int i, j; |
237 |
– |
/* set up gamma correction */ |
238 |
– |
for (i = 0; i < 256; i++) |
239 |
– |
gmap[i] = pow((i+.5)/256., gamma); |
257 |
|
/* skip color table */ |
258 |
|
if (hp->mapType == CM_HASMAP) |
259 |
|
fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1); |
262 |
|
/* get data */ |
263 |
|
readtarga(hp, tarData, stdin); |
264 |
|
/* allocate input scanline */ |
265 |
< |
scanline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
265 |
> |
scanline = (COLR *)emalloc(hp->x*sizeof(COLR)); |
266 |
|
/* convert file */ |
267 |
|
for (i = hp->y-1; i >= 0; i--) { |
268 |
|
if (hp->dataBits == 16) { |
269 |
|
register unsigned short *dp; |
270 |
|
dp = (unsigned short *)tarData + i*hp->x; |
271 |
|
for (j = 0; j < hp->x; j++) { |
272 |
< |
setcolor(scanline[j], gmap[*dp>>7 & 0xf8], |
273 |
< |
gmap[*dp>>2 & 0xf8], |
274 |
< |
gmap[*dp<<3 & 0xf8]); |
272 |
> |
scanline[j][RED] = *dp>>7 & 0xf8; |
273 |
> |
scanline[j][GRN] = *dp>>2 & 0xf8; |
274 |
> |
scanline[j][BLU] = *dp<<3 & 0xf8; |
275 |
|
dp++; |
276 |
|
} |
277 |
|
} else { /* hp->dataBits == 24 */ |
278 |
|
register unsigned char *dp; |
279 |
|
dp = (unsigned char *)tarData + i*3*hp->x; |
280 |
|
for (j = 0; j < hp->x; j++) { |
281 |
< |
setcolor(scanline[j], gmap[dp[2]], |
282 |
< |
gmap[dp[1]], |
283 |
< |
gmap[dp[0]]); |
281 |
> |
scanline[j][RED] = dp[2]; |
282 |
> |
scanline[j][GRN] = dp[1]; |
283 |
> |
scanline[j][BLU] = dp[0]; |
284 |
|
dp += 3; |
285 |
|
} |
286 |
|
} |
287 |
< |
if (fwritescan(scanline, hp->x, stdout) < 0) |
287 |
> |
gambs_colrs(scanline, hp->x); |
288 |
> |
if (bradj) |
289 |
> |
shiftcolrs(scanline, hp->x, bradj); |
290 |
> |
if (fwritecolrs(scanline, hp->x, stdout) < 0) |
291 |
|
quiterr("error writing RADIANCE file"); |
292 |
|
} |
293 |
|
free((char *)scanline); |
298 |
|
ra2tg(hp) /* convert radiance to targa file */ |
299 |
|
struct hdStruct *hp; |
300 |
|
{ |
281 |
– |
#define map(v) (v >= 1.0 ? 1023 : (int)(v*1023.+.5)) |
282 |
– |
unsigned char gmap[1024]; |
301 |
|
register int i, j; |
302 |
|
unsigned char *tarData; |
303 |
< |
COLOR *inline; |
286 |
< |
/* set up gamma correction */ |
287 |
< |
for (i = 0; i < 1024; i++) { |
288 |
< |
j = 256.*pow((i+.5)/1024., 1./gamma); |
289 |
< |
gmap[i] = hp->dataBits == 16 && j > 248 ? 248 : j; |
290 |
< |
} |
303 |
> |
COLR *inl; |
304 |
|
/* allocate space for data */ |
305 |
< |
inline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
305 |
> |
inl = (COLR *)emalloc(hp->x*sizeof(COLR)); |
306 |
|
tarData = taralloc(hp); |
307 |
|
/* convert file */ |
308 |
|
for (j = hp->y-1; j >= 0; j--) { |
309 |
< |
if (freadscan(inline, hp->x, stdin) < 0) |
309 |
> |
if (freadcolrs(inl, hp->x, stdin) < 0) |
310 |
|
quiterr("error reading RADIANCE file"); |
311 |
+ |
if (bradj) |
312 |
+ |
shiftcolrs(inl, hp->x, bradj); |
313 |
+ |
colrs_gambs(inl, hp->x); |
314 |
|
if (hp->dataBits == 16) { |
315 |
|
register unsigned short *dp; |
316 |
+ |
register int v; |
317 |
|
dp = (unsigned short *)tarData + j*hp->x; |
318 |
|
for (i = 0; i < hp->x; i++) { |
319 |
< |
*dp = ((gmap[map(colval(inline[i],RED))] |
320 |
< |
+(random()&7)) & 0xf8)<<7; |
321 |
< |
*dp |= ((gmap[map(colval(inline[i],GRN))] |
322 |
< |
+(random()&7)) & 0xf8)<<2; |
323 |
< |
*dp++ |= (gmap[map(colval(inline[i],BLU))] |
324 |
< |
+(random()&7))>>3; |
319 |
> |
v = inl[i][RED] + (random()&7); |
320 |
> |
if (v > 255) v = 255; |
321 |
> |
*dp = (v&0xf8)<<7; |
322 |
> |
v = inl[i][GRN] + (random()&7); |
323 |
> |
if (v > 255) v = 255; |
324 |
> |
*dp |= (v&0xf8)<<2; |
325 |
> |
v = inl[i][BLU] + (random()&7); |
326 |
> |
if (v > 255) v = 255; |
327 |
> |
*dp++ |= v>>3; |
328 |
|
} |
329 |
|
} else { /* hp->dataBits == 24 */ |
330 |
|
register unsigned char *dp; |
331 |
|
dp = (unsigned char *)tarData + j*3*hp->x; |
332 |
|
for (i = 0; i < hp->x; i++) { |
333 |
< |
*dp++ = gmap[map(colval(inline[i],BLU))]; |
334 |
< |
*dp++ = gmap[map(colval(inline[i],GRN))]; |
335 |
< |
*dp++ = gmap[map(colval(inline[i],RED))]; |
333 |
> |
*dp++ = inl[i][BLU]; |
334 |
> |
*dp++ = inl[i][GRN]; |
335 |
> |
*dp++ = inl[i][RED]; |
336 |
|
} |
337 |
|
} |
338 |
|
} |
339 |
|
/* write out targa data */ |
340 |
|
writetarga(hp, tarData, stdout); |
341 |
|
|
342 |
< |
free((char *)inline); |
342 |
> |
free((char *)inl); |
343 |
|
free((char *)tarData); |
324 |
– |
#undef map |
344 |
|
} |
345 |
|
|
346 |
|
|
350 |
|
FILE *fp; |
351 |
|
{ |
352 |
|
if (h->dataType == IM_RGB) { /* uncompressed */ |
353 |
< |
if (fwrite(d, 3*h->x, h->y, fp) != h->y) |
353 |
> |
if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y) |
354 |
|
quiterr("error writing targa file"); |
355 |
|
return; |
356 |
|
} |
386 |
|
int r, g, b; |
387 |
|
|
388 |
|
if (h->dataType == IM_RGB) { /* uncompressed */ |
389 |
< |
if (fread(data, 3*h->x, h->y, fp) != h->y) |
389 |
> |
if (fread((char *)data, 3*h->x, h->y, fp) != h->y) |
390 |
|
goto readerr; |
391 |
|
return; |
392 |
|
} |