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) \ |
85 |
|
sprintf(msg, "can't open output \"%s\"", argv[i+1]); |
86 |
|
quiterr(msg); |
87 |
|
} |
88 |
+ |
/* set gamma */ |
89 |
+ |
setcolrgam(gamma); |
90 |
|
/* convert */ |
91 |
|
if (reverse) { |
92 |
|
/* get header */ |
95 |
|
if (!goodpic(&head)) |
96 |
|
quiterr("incompatible format"); |
97 |
|
/* put header */ |
98 |
< |
printargs(argc, argv, stdout); |
98 |
> |
printargs(i, argv, stdout); |
99 |
|
putchar('\n'); |
100 |
|
fputresolu(YMAJOR|YDECR, head.x, head.y, stdout); |
101 |
|
/* convert file */ |
191 |
|
|
192 |
|
if (ip != NULL) |
193 |
|
if (nidbytes) |
194 |
< |
fread(ip, nidbytes, 1, fp); |
194 |
> |
fread((char *)ip, nidbytes, 1, fp); |
195 |
|
else |
196 |
|
*ip = '\0'; |
197 |
|
else if (nidbytes) |
232 |
|
tg2ra(hp) /* targa file to RADIANCE file */ |
233 |
|
struct hdStruct *hp; |
234 |
|
{ |
235 |
< |
float gmap[256]; |
232 |
< |
COLOR *scanline; |
235 |
> |
COLR *scanline; |
236 |
|
unsigned char *tarData; |
237 |
|
register int i, j; |
235 |
– |
/* set up gamma correction */ |
236 |
– |
for (i = 0; i < 256; i++) |
237 |
– |
gmap[i] = pow((i+.5)/256., gamma); |
238 |
|
/* skip color table */ |
239 |
|
if (hp->mapType == CM_HASMAP) |
240 |
|
fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1); |
243 |
|
/* get data */ |
244 |
|
readtarga(hp, tarData, stdin); |
245 |
|
/* allocate input scanline */ |
246 |
< |
scanline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
246 |
> |
scanline = (COLR *)emalloc(hp->x*sizeof(COLR)); |
247 |
|
/* convert file */ |
248 |
|
for (i = hp->y-1; i >= 0; i--) { |
249 |
|
if (hp->dataBits == 16) { |
250 |
|
register unsigned short *dp; |
251 |
|
dp = (unsigned short *)tarData + i*hp->x; |
252 |
|
for (j = 0; j < hp->x; j++) { |
253 |
< |
setcolor(scanline[j], gmap[*dp>>7 & 0xf8], |
254 |
< |
gmap[*dp>>2 & 0xf8], |
255 |
< |
gmap[*dp<<3 & 0xf8]); |
253 |
> |
scanline[j][RED] = *dp>>7 & 0xf8; |
254 |
> |
scanline[j][GRN] = *dp>>2 & 0xf8; |
255 |
> |
scanline[j][BLU] = *dp<<3 & 0xf8; |
256 |
|
dp++; |
257 |
|
} |
258 |
|
} else { /* hp->dataBits == 24 */ |
259 |
|
register unsigned char *dp; |
260 |
|
dp = (unsigned char *)tarData + i*3*hp->x; |
261 |
|
for (j = 0; j < hp->x; j++) { |
262 |
< |
setcolor(scanline[j], gmap[dp[2]], |
263 |
< |
gmap[dp[1]], |
264 |
< |
gmap[dp[0]]); |
262 |
> |
scanline[j][RED] = dp[2]; |
263 |
> |
scanline[j][GRN] = dp[1]; |
264 |
> |
scanline[j][BLU] = dp[0]; |
265 |
|
dp += 3; |
266 |
|
} |
267 |
|
} |
268 |
< |
if (fwritescan(scanline, hp->x, stdout) < 0) |
268 |
> |
gambs_colrs(scanline, hp->x); |
269 |
> |
if (fwritecolrs(scanline, hp->x, stdout) < 0) |
270 |
|
quiterr("error writing RADIANCE file"); |
271 |
|
} |
272 |
|
free((char *)scanline); |
277 |
|
ra2tg(hp) /* convert radiance to targa file */ |
278 |
|
struct hdStruct *hp; |
279 |
|
{ |
279 |
– |
#define map(v) (v >= 1.0 ? 1023 : (int)(v*1023.+.5)) |
280 |
– |
unsigned char gmap[1024]; |
280 |
|
register int i, j; |
281 |
|
unsigned char *tarData; |
282 |
< |
COLOR *inline; |
284 |
< |
/* set up gamma correction */ |
285 |
< |
for (i = 0; i < 1024; i++) { |
286 |
< |
j = 256.*pow((i+.5)/1024., 1./gamma); |
287 |
< |
gmap[i] = hp->dataBits == 16 && j > 248 ? 248 : j; |
288 |
< |
} |
282 |
> |
COLR *inl; |
283 |
|
/* allocate space for data */ |
284 |
< |
inline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
284 |
> |
inl = (COLR *)emalloc(hp->x*sizeof(COLR)); |
285 |
|
tarData = taralloc(hp); |
286 |
|
/* convert file */ |
287 |
|
for (j = hp->y-1; j >= 0; j--) { |
288 |
< |
if (freadscan(inline, hp->x, stdin) < 0) |
288 |
> |
if (freadcolrs(inl, hp->x, stdin) < 0) |
289 |
|
quiterr("error reading RADIANCE file"); |
290 |
+ |
colrs_gambs(inl, hp->x); |
291 |
|
if (hp->dataBits == 16) { |
292 |
|
register unsigned short *dp; |
293 |
|
dp = (unsigned short *)tarData + j*hp->x; |
294 |
|
for (i = 0; i < hp->x; i++) { |
295 |
< |
*dp = ((gmap[map(colval(inline[i],RED))] |
296 |
< |
+(random()&7)) & 0xf8)<<7; |
297 |
< |
*dp |= ((gmap[map(colval(inline[i],GRN))] |
303 |
< |
+(random()&7)) & 0xf8)<<2; |
304 |
< |
*dp++ |= (gmap[map(colval(inline[i],BLU))] |
305 |
< |
+(random()&7))>>3; |
295 |
> |
*dp = ((inl[i][RED]+(random()&7)) & 0xf8)<<7; |
296 |
> |
*dp |= ((inl[i][GRN]+(random()&7)) & 0xf8)<<2; |
297 |
> |
*dp++ |= (inl[i][BLU]+(random()&7))>>3; |
298 |
|
} |
299 |
|
} else { /* hp->dataBits == 24 */ |
300 |
|
register unsigned char *dp; |
301 |
|
dp = (unsigned char *)tarData + j*3*hp->x; |
302 |
|
for (i = 0; i < hp->x; i++) { |
303 |
< |
*dp++ = gmap[map(colval(inline[i],BLU))]; |
304 |
< |
*dp++ = gmap[map(colval(inline[i],GRN))]; |
305 |
< |
*dp++ = gmap[map(colval(inline[i],RED))]; |
303 |
> |
*dp++ = inl[i][RED]; |
304 |
> |
*dp++ = inl[i][GRN]; |
305 |
> |
*dp++ = inl[i][BLU]; |
306 |
|
} |
307 |
|
} |
308 |
|
} |
309 |
|
/* write out targa data */ |
310 |
|
writetarga(hp, tarData, stdout); |
311 |
|
|
312 |
< |
free((char *)inline); |
312 |
> |
free((char *)inl); |
313 |
|
free((char *)tarData); |
322 |
– |
#undef map |
314 |
|
} |
315 |
|
|
316 |
|
|
320 |
|
FILE *fp; |
321 |
|
{ |
322 |
|
if (h->dataType == IM_RGB) { /* uncompressed */ |
323 |
< |
if (fwrite(d, 3*h->x, h->y, fp) != h->y) |
323 |
> |
if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y) |
324 |
|
quiterr("error writing targa file"); |
325 |
|
return; |
326 |
|
} |
356 |
|
int r, g, b; |
357 |
|
|
358 |
|
if (h->dataType == IM_RGB) { /* uncompressed */ |
359 |
< |
if (fread(data, 3*h->x, h->y, fp) != h->y) |
359 |
> |
if (fread((char *)data, 3*h->x, h->y, fp) != h->y) |
360 |
|
goto readerr; |
361 |
|
return; |
362 |
|
} |