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) \ |
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 |
> |
fputformat(COLRFMT, stdout); |
100 |
|
putchar('\n'); |
101 |
|
fputresolu(YMAJOR|YDECR, head.x, head.y, stdout); |
102 |
|
/* convert file */ |
103 |
|
tg2ra(&head); |
104 |
|
} else { |
105 |
< |
getheader(stdin, NULL); |
106 |
< |
if (fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR)) |
105 |
> |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
106 |
> |
fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR)) |
107 |
|
quiterr("bad picture file"); |
108 |
|
/* assign header */ |
109 |
|
head.textSize = 0; |
192 |
|
|
193 |
|
if (ip != NULL) |
194 |
|
if (nidbytes) |
195 |
< |
fread(ip, nidbytes, 1, fp); |
195 |
> |
fread((char *)ip, nidbytes, 1, fp); |
196 |
|
else |
197 |
|
*ip = '\0'; |
198 |
|
else if (nidbytes) |
233 |
|
tg2ra(hp) /* targa file to RADIANCE file */ |
234 |
|
struct hdStruct *hp; |
235 |
|
{ |
236 |
< |
float gmap[256]; |
232 |
< |
COLOR *scanline; |
236 |
> |
COLR *scanline; |
237 |
|
unsigned char *tarData; |
238 |
|
register int i, j; |
235 |
– |
/* set up gamma correction */ |
236 |
– |
for (i = 0; i < 256; i++) |
237 |
– |
gmap[i] = pow((i+.5)/256., gamma); |
239 |
|
/* skip color table */ |
240 |
|
if (hp->mapType == CM_HASMAP) |
241 |
|
fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1); |
244 |
|
/* get data */ |
245 |
|
readtarga(hp, tarData, stdin); |
246 |
|
/* allocate input scanline */ |
247 |
< |
scanline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
247 |
> |
scanline = (COLR *)emalloc(hp->x*sizeof(COLR)); |
248 |
|
/* convert file */ |
249 |
|
for (i = hp->y-1; i >= 0; i--) { |
250 |
|
if (hp->dataBits == 16) { |
251 |
|
register unsigned short *dp; |
252 |
|
dp = (unsigned short *)tarData + i*hp->x; |
253 |
|
for (j = 0; j < hp->x; j++) { |
254 |
< |
setcolor(scanline[j], gmap[*dp>>7 & 0xf8], |
255 |
< |
gmap[*dp>>2 & 0xf8], |
256 |
< |
gmap[*dp<<3 & 0xf8]); |
254 |
> |
scanline[j][RED] = *dp>>7 & 0xf8; |
255 |
> |
scanline[j][GRN] = *dp>>2 & 0xf8; |
256 |
> |
scanline[j][BLU] = *dp<<3 & 0xf8; |
257 |
|
dp++; |
258 |
|
} |
259 |
|
} else { /* hp->dataBits == 24 */ |
260 |
|
register unsigned char *dp; |
261 |
|
dp = (unsigned char *)tarData + i*3*hp->x; |
262 |
|
for (j = 0; j < hp->x; j++) { |
263 |
< |
setcolor(scanline[j], gmap[dp[2]], |
264 |
< |
gmap[dp[1]], |
265 |
< |
gmap[dp[0]]); |
263 |
> |
scanline[j][RED] = dp[2]; |
264 |
> |
scanline[j][GRN] = dp[1]; |
265 |
> |
scanline[j][BLU] = dp[0]; |
266 |
|
dp += 3; |
267 |
|
} |
268 |
|
} |
269 |
< |
if (fwritescan(scanline, hp->x, stdout) < 0) |
269 |
> |
gambs_colrs(scanline, hp->x); |
270 |
> |
if (fwritecolrs(scanline, hp->x, stdout) < 0) |
271 |
|
quiterr("error writing RADIANCE file"); |
272 |
|
} |
273 |
|
free((char *)scanline); |
278 |
|
ra2tg(hp) /* convert radiance to targa file */ |
279 |
|
struct hdStruct *hp; |
280 |
|
{ |
279 |
– |
#define map(v) (v >= 1.0 ? 1023 : (int)(v*1023.+.5)) |
280 |
– |
unsigned char gmap[1024]; |
281 |
|
register int i, j; |
282 |
|
unsigned char *tarData; |
283 |
< |
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 |
< |
} |
283 |
> |
COLR *inl; |
284 |
|
/* allocate space for data */ |
285 |
< |
inline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
285 |
> |
inl = (COLR *)emalloc(hp->x*sizeof(COLR)); |
286 |
|
tarData = taralloc(hp); |
287 |
|
/* convert file */ |
288 |
|
for (j = hp->y-1; j >= 0; j--) { |
289 |
< |
if (freadscan(inline, hp->x, stdin) < 0) |
289 |
> |
if (freadcolrs(inl, hp->x, stdin) < 0) |
290 |
|
quiterr("error reading RADIANCE file"); |
291 |
+ |
colrs_gambs(inl, hp->x); |
292 |
|
if (hp->dataBits == 16) { |
293 |
|
register unsigned short *dp; |
294 |
+ |
register int v; |
295 |
|
dp = (unsigned short *)tarData + j*hp->x; |
296 |
|
for (i = 0; i < hp->x; i++) { |
297 |
< |
*dp = ((gmap[map(colval(inline[i],RED))] |
298 |
< |
+(random()&7)) & 0xf8)<<7; |
299 |
< |
*dp |= ((gmap[map(colval(inline[i],GRN))] |
300 |
< |
+(random()&7)) & 0xf8)<<2; |
301 |
< |
*dp++ |= (gmap[map(colval(inline[i],BLU))] |
302 |
< |
+(random()&7))>>3; |
297 |
> |
v = inl[i][RED] + (random()&7); |
298 |
> |
if (v > 255) v = 255; |
299 |
> |
*dp = (v&0xf8)<<7; |
300 |
> |
v = inl[i][GRN] + (random()&7); |
301 |
> |
if (v > 255) v = 255; |
302 |
> |
*dp |= (v&0xf8)<<2; |
303 |
> |
v = inl[i][BLU] + (random()&7); |
304 |
> |
if (v > 255) v = 255; |
305 |
> |
*dp++ |= v>>3; |
306 |
|
} |
307 |
|
} else { /* hp->dataBits == 24 */ |
308 |
|
register unsigned char *dp; |
309 |
|
dp = (unsigned char *)tarData + j*3*hp->x; |
310 |
|
for (i = 0; i < hp->x; i++) { |
311 |
< |
*dp++ = gmap[map(colval(inline[i],BLU))]; |
312 |
< |
*dp++ = gmap[map(colval(inline[i],GRN))]; |
313 |
< |
*dp++ = gmap[map(colval(inline[i],RED))]; |
311 |
> |
*dp++ = inl[i][BLU]; |
312 |
> |
*dp++ = inl[i][GRN]; |
313 |
> |
*dp++ = inl[i][RED]; |
314 |
|
} |
315 |
|
} |
316 |
|
} |
317 |
|
/* write out targa data */ |
318 |
|
writetarga(hp, tarData, stdout); |
319 |
|
|
320 |
< |
free((char *)inline); |
320 |
> |
free((char *)inl); |
321 |
|
free((char *)tarData); |
322 |
– |
#undef map |
322 |
|
} |
323 |
|
|
324 |
|
|
328 |
|
FILE *fp; |
329 |
|
{ |
330 |
|
if (h->dataType == IM_RGB) { /* uncompressed */ |
331 |
< |
if (fwrite(d, 3*h->x, h->y, fp) != h->y) |
331 |
> |
if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y) |
332 |
|
quiterr("error writing targa file"); |
333 |
|
return; |
334 |
|
} |
364 |
|
int r, g, b; |
365 |
|
|
366 |
|
if (h->dataType == IM_RGB) { /* uncompressed */ |
367 |
< |
if (fread(data, 3*h->x, h->y, fp) != h->y) |
367 |
> |
if (fread((char *)data, 3*h->x, h->y, fp) != h->y) |
368 |
|
goto readerr; |
369 |
|
return; |
370 |
|
} |