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) \ |
93 |
|
if (!goodpic(&head)) |
94 |
|
quiterr("incompatible format"); |
95 |
|
/* put header */ |
96 |
< |
printargs(argc, argv, stdout); |
96 |
> |
printargs(i, argv, stdout); |
97 |
|
putchar('\n'); |
98 |
< |
printf("-Y %d +X %d\n", head.y, head.x); |
98 |
> |
fputresolu(YMAJOR|YDECR, head.x, head.y, stdout); |
99 |
|
/* convert file */ |
100 |
|
tg2ra(&head); |
101 |
|
} else { |
102 |
|
getheader(stdin, NULL); |
103 |
< |
if (scanf("-Y %d +X %d\n", &head.y, &head.x) != 2) |
103 |
> |
if (fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR)) |
104 |
|
quiterr("bad picture file"); |
105 |
|
/* assign header */ |
106 |
|
head.textSize = 0; |
189 |
|
|
190 |
|
if (ip != NULL) |
191 |
|
if (nidbytes) |
192 |
< |
fread(ip, nidbytes, 1, fp); |
192 |
> |
fread((char *)ip, nidbytes, 1, fp); |
193 |
|
else |
194 |
|
*ip = '\0'; |
195 |
|
else if (nidbytes) |
261 |
|
register unsigned char *dp; |
262 |
|
dp = (unsigned char *)tarData + i*3*hp->x; |
263 |
|
for (j = 0; j < hp->x; j++) { |
264 |
< |
setcolor(scanline[j], gmap[dp[0]], |
264 |
> |
setcolor(scanline[j], gmap[dp[2]], |
265 |
|
gmap[dp[1]], |
266 |
< |
gmap[dp[2]]); |
266 |
> |
gmap[dp[0]]); |
267 |
|
dp += 3; |
268 |
|
} |
269 |
|
} |
278 |
|
ra2tg(hp) /* convert radiance to targa file */ |
279 |
|
struct hdStruct *hp; |
280 |
|
{ |
281 |
+ |
#define map(v) (v >= 1.0 ? 1023 : (int)(v*1023.+.5)) |
282 |
|
unsigned char gmap[1024]; |
283 |
< |
register int i, j, c; |
283 |
> |
register int i, j; |
284 |
|
unsigned char *tarData; |
285 |
< |
COLOR *inline; |
285 |
> |
COLOR *inl; |
286 |
|
/* set up gamma correction */ |
287 |
< |
for (i = 0; i < 1024; i++) |
288 |
< |
gmap[i] = 256.*pow((i+.5)/1024., 1./gamma); |
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 |
> |
} |
291 |
|
/* allocate space for data */ |
292 |
< |
inline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
292 |
> |
inl = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
293 |
|
tarData = taralloc(hp); |
294 |
|
/* convert file */ |
295 |
|
for (j = hp->y-1; j >= 0; j--) { |
296 |
< |
if (freadscan(inline, hp->x, stdin) < 0) |
296 |
> |
if (freadscan(inl, hp->x, stdin) < 0) |
297 |
|
quiterr("error reading RADIANCE file"); |
298 |
|
if (hp->dataBits == 16) { |
299 |
|
register unsigned short *dp; |
300 |
|
dp = (unsigned short *)tarData + j*hp->x; |
301 |
|
for (i = 0; i < hp->x; i++) { |
302 |
< |
c = 1024.*colval(inline[i],RED); |
303 |
< |
if (c > 1023) c = 1023; |
304 |
< |
*dp = (gmap[c] & 0xf8)<<7; |
305 |
< |
c = 1024.*colval(inline[i],GRN); |
306 |
< |
if (c > 1023) c = 1023; |
307 |
< |
*dp |= (gmap[c] & 0xf8)<<2; |
303 |
< |
c = 1024.*colval(inline[i],BLU); |
304 |
< |
if (c > 1023) c = 1023; |
305 |
< |
*dp++ |= gmap[c]>>3; |
302 |
> |
*dp = ((gmap[map(colval(inl[i],RED))] |
303 |
> |
+(random()&7)) & 0xf8)<<7; |
304 |
> |
*dp |= ((gmap[map(colval(inl[i],GRN))] |
305 |
> |
+(random()&7)) & 0xf8)<<2; |
306 |
> |
*dp++ |= (gmap[map(colval(inl[i],BLU))] |
307 |
> |
+(random()&7))>>3; |
308 |
|
} |
309 |
|
} else { /* hp->dataBits == 24 */ |
310 |
|
register unsigned char *dp; |
311 |
|
dp = (unsigned char *)tarData + j*3*hp->x; |
312 |
|
for (i = 0; i < hp->x; i++) { |
313 |
< |
c = 1024.*colval(inline[i],RED); |
314 |
< |
if (c > 1023) c = 1023; |
315 |
< |
*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],BLU); |
318 |
< |
if (c > 1023) c = 1023; |
319 |
< |
*dp++ = gmap[c]; |
313 |
> |
*dp++ = gmap[map(colval(inl[i],BLU))]; |
314 |
> |
*dp++ = gmap[map(colval(inl[i],GRN))]; |
315 |
> |
*dp++ = gmap[map(colval(inl[i],RED))]; |
316 |
|
} |
317 |
|
} |
318 |
|
} |
319 |
|
/* write out targa data */ |
320 |
|
writetarga(hp, tarData, stdout); |
321 |
|
|
322 |
< |
free((char *)inline); |
322 |
> |
free((char *)inl); |
323 |
|
free((char *)tarData); |
324 |
+ |
#undef map |
325 |
|
} |
326 |
|
|
327 |
|
|
331 |
|
FILE *fp; |
332 |
|
{ |
333 |
|
if (h->dataType == IM_RGB) { /* uncompressed */ |
334 |
< |
if (fwrite(d, 3*h->x, h->y, fp) != h->y) |
334 |
> |
if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y) |
335 |
|
quiterr("error writing targa file"); |
336 |
|
return; |
337 |
|
} |
367 |
|
int r, g, b; |
368 |
|
|
369 |
|
if (h->dataType == IM_RGB) { /* uncompressed */ |
370 |
< |
if (fread(data, 3*h->x, h->y, fp) != h->y) |
370 |
> |
if (fread((char *)data, 3*h->x, h->y, fp) != h->y) |
371 |
|
goto readerr; |
372 |
|
return; |
373 |
|
} |
376 |
|
goto readerr; |
377 |
|
cnt = (c & 0x7f) + 1; |
378 |
|
if (c & 0x80) { /* repeated pixel */ |
379 |
< |
r = getc(fp); g = getc(fp); |
380 |
< |
if ((b = getc(fp)) == EOF) |
379 |
> |
b = getc(fp); g = getc(fp); |
380 |
> |
if ((r = getc(fp)) == EOF) |
381 |
|
goto readerr; |
382 |
|
while (cnt--) { |
386 |
– |
*dp++ = r; |
387 |
– |
*dp++ = g; |
383 |
|
*dp++ = b; |
384 |
+ |
*dp++ = g; |
385 |
+ |
*dp++ = r; |
386 |
|
} |
387 |
|
} else /* non-repeating pixels */ |
388 |
|
while (cnt--) { |
389 |
|
*dp++ = getc(fp); *dp++ = getc(fp); |
390 |
< |
if ((b = getc(fp)) == EOF) |
390 |
> |
if ((r = getc(fp)) == EOF) |
391 |
|
goto readerr; |
392 |
< |
*dp++ = b; |
392 |
> |
*dp++ = r; |
393 |
|
} |
394 |
|
} |
395 |
|
return; |