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"; |
15 |
|
|
16 |
|
#include "color.h" |
17 |
|
|
18 |
+ |
#include "random.h" |
19 |
+ |
|
20 |
|
#include "targa.h" |
21 |
|
|
22 |
|
#define goodpic(h) (((h)->dataType==IM_RGB || (h)->dataType==IM_CRGB) \ |
36 |
|
|
37 |
|
double gamma = 2.0; /* gamma correction */ |
38 |
|
|
39 |
+ |
int bradj = 0; /* brightness adjustment */ |
40 |
+ |
|
41 |
|
char *progname; |
42 |
|
|
43 |
|
char msg[128]; |
69 |
|
case '3': |
70 |
|
head.dataBits = 24; |
71 |
|
break; |
72 |
+ |
case 'e': |
73 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
74 |
+ |
goto userr; |
75 |
+ |
bradj = atoi(argv[++i]); |
76 |
+ |
break; |
77 |
|
default: |
78 |
|
goto userr; |
79 |
|
} |
92 |
|
sprintf(msg, "can't open output \"%s\"", argv[i+1]); |
93 |
|
quiterr(msg); |
94 |
|
} |
95 |
+ |
/* set gamma */ |
96 |
+ |
setcolrgam(gamma); |
97 |
|
/* convert */ |
98 |
|
if (reverse) { |
99 |
|
/* get header */ |
102 |
|
if (!goodpic(&head)) |
103 |
|
quiterr("incompatible format"); |
104 |
|
/* put header */ |
105 |
< |
printargs(argc, argv, stdout); |
105 |
> |
printargs(i, argv, stdout); |
106 |
> |
fputformat(COLRFMT, stdout); |
107 |
|
putchar('\n'); |
108 |
< |
printf("-Y %d +X %d\n", head.y, head.x); |
108 |
> |
fputresolu(YMAJOR|YDECR, head.x, head.y, stdout); |
109 |
|
/* convert file */ |
110 |
|
tg2ra(&head); |
111 |
|
} else { |
112 |
< |
getheader(stdin, NULL); |
113 |
< |
if (scanf("-Y %d +X %d\n", &head.y, &head.x) != 2) |
112 |
> |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
113 |
> |
fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR)) |
114 |
|
quiterr("bad picture file"); |
115 |
|
/* assign header */ |
116 |
|
head.textSize = 0; |
126 |
|
} |
127 |
|
exit(0); |
128 |
|
userr: |
129 |
< |
fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma] [input [output]]\n", |
129 |
> |
fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma][-e +/-stops] [input [output]]\n", |
130 |
|
progname); |
131 |
|
exit(1); |
132 |
|
} |
199 |
|
|
200 |
|
if (ip != NULL) |
201 |
|
if (nidbytes) |
202 |
< |
fread(ip, nidbytes, 1, fp); |
202 |
> |
fread((char *)ip, nidbytes, 1, fp); |
203 |
|
else |
204 |
|
*ip = '\0'; |
205 |
|
else if (nidbytes) |
240 |
|
tg2ra(hp) /* targa file to RADIANCE file */ |
241 |
|
struct hdStruct *hp; |
242 |
|
{ |
243 |
< |
float gmap[256]; |
232 |
< |
COLOR *scanline; |
243 |
> |
COLR *scanline; |
244 |
|
unsigned char *tarData; |
245 |
|
register int i, j; |
235 |
– |
/* set up gamma correction */ |
236 |
– |
for (i = 0; i < 256; i++) |
237 |
– |
gmap[i] = pow((i+.5)/256., gamma); |
246 |
|
/* skip color table */ |
247 |
|
if (hp->mapType == CM_HASMAP) |
248 |
|
fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1); |
251 |
|
/* get data */ |
252 |
|
readtarga(hp, tarData, stdin); |
253 |
|
/* allocate input scanline */ |
254 |
< |
scanline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
254 |
> |
scanline = (COLR *)emalloc(hp->x*sizeof(COLR)); |
255 |
|
/* convert file */ |
256 |
|
for (i = hp->y-1; i >= 0; i--) { |
257 |
|
if (hp->dataBits == 16) { |
258 |
|
register unsigned short *dp; |
259 |
|
dp = (unsigned short *)tarData + i*hp->x; |
260 |
|
for (j = 0; j < hp->x; j++) { |
261 |
< |
setcolor(scanline[j], gmap[*dp>>7 & 0xf8], |
262 |
< |
gmap[*dp>>2 & 0xf8], |
263 |
< |
gmap[*dp<<3 & 0xf8]); |
261 |
> |
scanline[j][RED] = *dp>>7 & 0xf8; |
262 |
> |
scanline[j][GRN] = *dp>>2 & 0xf8; |
263 |
> |
scanline[j][BLU] = *dp<<3 & 0xf8; |
264 |
|
dp++; |
265 |
|
} |
266 |
|
} else { /* hp->dataBits == 24 */ |
267 |
|
register unsigned char *dp; |
268 |
|
dp = (unsigned char *)tarData + i*3*hp->x; |
269 |
|
for (j = 0; j < hp->x; j++) { |
270 |
< |
setcolor(scanline[j], gmap[dp[2]], |
271 |
< |
gmap[dp[1]], |
272 |
< |
gmap[dp[0]]); |
270 |
> |
scanline[j][RED] = dp[2]; |
271 |
> |
scanline[j][GRN] = dp[1]; |
272 |
> |
scanline[j][BLU] = dp[0]; |
273 |
|
dp += 3; |
274 |
|
} |
275 |
|
} |
276 |
< |
if (fwritescan(scanline, hp->x, stdout) < 0) |
276 |
> |
gambs_colrs(scanline, hp->x); |
277 |
> |
if (bradj) |
278 |
> |
shiftcolrs(scanline, hp->x, bradj); |
279 |
> |
if (fwritecolrs(scanline, hp->x, stdout) < 0) |
280 |
|
quiterr("error writing RADIANCE file"); |
281 |
|
} |
282 |
|
free((char *)scanline); |
287 |
|
ra2tg(hp) /* convert radiance to targa file */ |
288 |
|
struct hdStruct *hp; |
289 |
|
{ |
290 |
< |
unsigned char gmap[1024]; |
280 |
< |
register int i, j, c; |
290 |
> |
register int i, j; |
291 |
|
unsigned char *tarData; |
292 |
< |
COLOR *inline; |
283 |
< |
/* set up gamma correction */ |
284 |
< |
for (i = 0; i < 1024; i++) |
285 |
< |
gmap[i] = 256.*pow((i+.5)/1024., 1./gamma); |
292 |
> |
COLR *inl; |
293 |
|
/* allocate space for data */ |
294 |
< |
inline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
294 |
> |
inl = (COLR *)emalloc(hp->x*sizeof(COLR)); |
295 |
|
tarData = taralloc(hp); |
296 |
|
/* convert file */ |
297 |
|
for (j = hp->y-1; j >= 0; j--) { |
298 |
< |
if (freadscan(inline, hp->x, stdin) < 0) |
298 |
> |
if (freadcolrs(inl, hp->x, stdin) < 0) |
299 |
|
quiterr("error reading RADIANCE file"); |
300 |
+ |
if (bradj) |
301 |
+ |
shiftcolrs(inl, hp->x, bradj); |
302 |
+ |
colrs_gambs(inl, hp->x); |
303 |
|
if (hp->dataBits == 16) { |
304 |
|
register unsigned short *dp; |
305 |
+ |
register int v; |
306 |
|
dp = (unsigned short *)tarData + j*hp->x; |
307 |
|
for (i = 0; i < hp->x; i++) { |
308 |
< |
c = 1024.*colval(inline[i],RED); |
309 |
< |
if (c > 1023) c = 1023; |
310 |
< |
*dp = (gmap[c] & 0xf8)<<7; |
311 |
< |
c = 1024.*colval(inline[i],GRN); |
312 |
< |
if (c > 1023) c = 1023; |
313 |
< |
*dp |= (gmap[c] & 0xf8)<<2; |
314 |
< |
c = 1024.*colval(inline[i],BLU); |
315 |
< |
if (c > 1023) c = 1023; |
316 |
< |
*dp++ |= gmap[c]>>3; |
308 |
> |
v = inl[i][RED] + (random()&7); |
309 |
> |
if (v > 255) v = 255; |
310 |
> |
*dp = (v&0xf8)<<7; |
311 |
> |
v = inl[i][GRN] + (random()&7); |
312 |
> |
if (v > 255) v = 255; |
313 |
> |
*dp |= (v&0xf8)<<2; |
314 |
> |
v = inl[i][BLU] + (random()&7); |
315 |
> |
if (v > 255) v = 255; |
316 |
> |
*dp++ |= v>>3; |
317 |
|
} |
318 |
|
} else { /* hp->dataBits == 24 */ |
319 |
|
register unsigned char *dp; |
320 |
|
dp = (unsigned char *)tarData + j*3*hp->x; |
321 |
|
for (i = 0; i < hp->x; i++) { |
322 |
< |
c = 1024.*colval(inline[i],BLU); |
323 |
< |
if (c > 1023) c = 1023; |
324 |
< |
*dp++ = gmap[c]; |
314 |
< |
c = 1024.*colval(inline[i],GRN); |
315 |
< |
if (c > 1023) c = 1023; |
316 |
< |
*dp++ = gmap[c]; |
317 |
< |
c = 1024.*colval(inline[i],RED); |
318 |
< |
if (c > 1023) c = 1023; |
319 |
< |
*dp++ = gmap[c]; |
322 |
> |
*dp++ = inl[i][BLU]; |
323 |
> |
*dp++ = inl[i][GRN]; |
324 |
> |
*dp++ = inl[i][RED]; |
325 |
|
} |
326 |
|
} |
327 |
|
} |
328 |
|
/* write out targa data */ |
329 |
|
writetarga(hp, tarData, stdout); |
330 |
|
|
331 |
< |
free((char *)inline); |
331 |
> |
free((char *)inl); |
332 |
|
free((char *)tarData); |
333 |
|
} |
334 |
|
|
339 |
|
FILE *fp; |
340 |
|
{ |
341 |
|
if (h->dataType == IM_RGB) { /* uncompressed */ |
342 |
< |
if (fwrite(d, 3*h->x, h->y, fp) != h->y) |
342 |
> |
if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y) |
343 |
|
quiterr("error writing targa file"); |
344 |
|
return; |
345 |
|
} |
375 |
|
int r, g, b; |
376 |
|
|
377 |
|
if (h->dataType == IM_RGB) { /* uncompressed */ |
378 |
< |
if (fread(data, 3*h->x, h->y, fp) != h->y) |
378 |
> |
if (fread((char *)data, 3*h->x, h->y, fp) != h->y) |
379 |
|
goto readerr; |
380 |
|
return; |
381 |
|
} |