1 |
– |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* ra_t16.c - program to convert between RADIANCE and |
6 |
|
* Targa 16, 24 and 32-bit images. |
9 |
|
*/ |
10 |
|
|
11 |
|
#include <stdio.h> |
12 |
+ |
#include <time.h> |
13 |
+ |
#include <math.h> |
14 |
|
|
15 |
+ |
#include "platform.h" |
16 |
|
#include "color.h" |
17 |
< |
|
17 |
> |
#include "resolu.h" |
18 |
> |
#include "random.h" |
19 |
|
#include "targa.h" |
20 |
|
|
21 |
|
#define goodpic(h) (((h)->dataType==IM_RGB || (h)->dataType==IM_CRGB) \ |
31 |
|
|
32 |
|
extern char *ecalloc(), *emalloc(); |
33 |
|
|
34 |
< |
extern double atof(), pow(); |
34 |
> |
double gamcor = 2.2; /* gamma correction */ |
35 |
|
|
36 |
< |
double gamma = 2.0; /* gamma correction */ |
36 |
> |
int bradj = 0; /* brightness adjustment */ |
37 |
|
|
38 |
|
char *progname; |
39 |
|
|
47 |
|
struct hdStruct head; |
48 |
|
int reverse = 0; |
49 |
|
int i; |
50 |
< |
|
50 |
> |
SET_DEFAULT_BINARY(); |
51 |
> |
SET_FILE_BINARY(stdin); |
52 |
> |
SET_FILE_BINARY(stdout); |
53 |
|
progname = argv[0]; |
54 |
|
|
55 |
|
head.dataBits = 16; |
57 |
|
if (argv[i][0] == '-') |
58 |
|
switch (argv[i][1]) { |
59 |
|
case 'g': |
60 |
< |
gamma = atof(argv[++i]); |
60 |
> |
gamcor = atof(argv[++i]); |
61 |
|
break; |
62 |
|
case 'r': |
63 |
|
reverse = !reverse; |
68 |
|
case '3': |
69 |
|
head.dataBits = 24; |
70 |
|
break; |
71 |
+ |
case 'e': |
72 |
+ |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
73 |
+ |
goto userr; |
74 |
+ |
bradj = atoi(argv[++i]); |
75 |
+ |
break; |
76 |
|
default: |
77 |
|
goto userr; |
78 |
|
} |
91 |
|
sprintf(msg, "can't open output \"%s\"", argv[i+1]); |
92 |
|
quiterr(msg); |
93 |
|
} |
94 |
+ |
/* set gamma */ |
95 |
+ |
setcolrgam(gamcor); |
96 |
|
/* convert */ |
97 |
|
if (reverse) { |
98 |
|
/* get header */ |
101 |
|
if (!goodpic(&head)) |
102 |
|
quiterr("incompatible format"); |
103 |
|
/* put header */ |
104 |
< |
printargs(argc, argv, stdout); |
104 |
> |
newheader("RADIANCE", 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 |
> |
fprtresolu(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) < 0) |
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 |
|
} |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
+ |
void |
166 |
|
eputs(s) |
167 |
|
char *s; |
168 |
|
{ |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
+ |
void |
174 |
|
quit(code) |
175 |
|
int code; |
176 |
|
{ |
201 |
|
|
202 |
|
if (ip != NULL) |
203 |
|
if (nidbytes) |
204 |
< |
fread(ip, nidbytes, 1, fp); |
204 |
> |
fread((char *)ip, nidbytes, 1, fp); |
205 |
|
else |
206 |
|
*ip = '\0'; |
207 |
|
else if (nidbytes) |
242 |
|
tg2ra(hp) /* targa file to RADIANCE file */ |
243 |
|
struct hdStruct *hp; |
244 |
|
{ |
245 |
< |
float gmap[256]; |
232 |
< |
COLOR *scanline; |
245 |
> |
COLR *scanline; |
246 |
|
unsigned char *tarData; |
247 |
|
register int i, j; |
235 |
– |
/* set up gamma correction */ |
236 |
– |
for (i = 0; i < 256; i++) |
237 |
– |
gmap[i] = pow((i+.5)/256., gamma); |
248 |
|
/* skip color table */ |
249 |
|
if (hp->mapType == CM_HASMAP) |
250 |
|
fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1); |
253 |
|
/* get data */ |
254 |
|
readtarga(hp, tarData, stdin); |
255 |
|
/* allocate input scanline */ |
256 |
< |
scanline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
256 |
> |
scanline = (COLR *)emalloc(hp->x*sizeof(COLR)); |
257 |
|
/* convert file */ |
258 |
|
for (i = hp->y-1; i >= 0; i--) { |
259 |
|
if (hp->dataBits == 16) { |
260 |
|
register unsigned short *dp; |
261 |
|
dp = (unsigned short *)tarData + i*hp->x; |
262 |
|
for (j = 0; j < hp->x; j++) { |
263 |
< |
setcolor(scanline[j], gmap[*dp>>7 & 0xf8], |
264 |
< |
gmap[*dp>>2 & 0xf8], |
265 |
< |
gmap[*dp<<3 & 0xf8]); |
263 |
> |
scanline[j][RED] = *dp>>7 & 0xf8; |
264 |
> |
scanline[j][GRN] = *dp>>2 & 0xf8; |
265 |
> |
scanline[j][BLU] = *dp<<3 & 0xf8; |
266 |
|
dp++; |
267 |
|
} |
268 |
|
} else { /* hp->dataBits == 24 */ |
269 |
|
register unsigned char *dp; |
270 |
|
dp = (unsigned char *)tarData + i*3*hp->x; |
271 |
|
for (j = 0; j < hp->x; j++) { |
272 |
< |
setcolor(scanline[j], gmap[dp[2]], |
273 |
< |
gmap[dp[1]], |
274 |
< |
gmap[dp[0]]); |
272 |
> |
scanline[j][RED] = dp[2]; |
273 |
> |
scanline[j][GRN] = dp[1]; |
274 |
> |
scanline[j][BLU] = dp[0]; |
275 |
|
dp += 3; |
276 |
|
} |
277 |
|
} |
278 |
< |
if (fwritescan(scanline, hp->x, stdout) < 0) |
278 |
> |
gambs_colrs(scanline, hp->x); |
279 |
> |
if (bradj) |
280 |
> |
shiftcolrs(scanline, hp->x, bradj); |
281 |
> |
if (fwritecolrs(scanline, hp->x, stdout) < 0) |
282 |
|
quiterr("error writing RADIANCE file"); |
283 |
|
} |
284 |
< |
free((char *)scanline); |
285 |
< |
free((char *)tarData); |
284 |
> |
free((void *)scanline); |
285 |
> |
free((void *)tarData); |
286 |
|
} |
287 |
|
|
288 |
|
|
289 |
|
ra2tg(hp) /* convert radiance to targa file */ |
290 |
|
struct hdStruct *hp; |
291 |
|
{ |
292 |
< |
unsigned char gmap[1024]; |
280 |
< |
register int i, j, c; |
292 |
> |
register int i, j; |
293 |
|
unsigned char *tarData; |
294 |
< |
COLOR *inline; |
283 |
< |
/* set up gamma correction */ |
284 |
< |
for (i = 0; i < 1024; i++) |
285 |
< |
gmap[i] = 256.*pow((i+.5)/1024., 1./gamma); |
294 |
> |
COLR *inl; |
295 |
|
/* allocate space for data */ |
296 |
< |
inline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
296 |
> |
inl = (COLR *)emalloc(hp->x*sizeof(COLR)); |
297 |
|
tarData = taralloc(hp); |
298 |
|
/* convert file */ |
299 |
|
for (j = hp->y-1; j >= 0; j--) { |
300 |
< |
if (freadscan(inline, hp->x, stdin) < 0) |
300 |
> |
if (freadcolrs(inl, hp->x, stdin) < 0) |
301 |
|
quiterr("error reading RADIANCE file"); |
302 |
+ |
if (bradj) |
303 |
+ |
shiftcolrs(inl, hp->x, bradj); |
304 |
+ |
colrs_gambs(inl, hp->x); |
305 |
|
if (hp->dataBits == 16) { |
306 |
|
register unsigned short *dp; |
307 |
+ |
register int v; |
308 |
|
dp = (unsigned short *)tarData + j*hp->x; |
309 |
|
for (i = 0; i < hp->x; i++) { |
310 |
< |
c = 1024.*colval(inline[i],RED); |
311 |
< |
if (c > 1023) c = 1023; |
312 |
< |
*dp = (gmap[c] & 0xf8)<<7; |
313 |
< |
c = 1024.*colval(inline[i],GRN); |
314 |
< |
if (c > 1023) c = 1023; |
315 |
< |
*dp |= (gmap[c] & 0xf8)<<2; |
316 |
< |
c = 1024.*colval(inline[i],BLU); |
317 |
< |
if (c > 1023) c = 1023; |
318 |
< |
*dp++ |= gmap[c]>>3; |
310 |
> |
v = inl[i][RED] + (random()&7); |
311 |
> |
if (v > 255) v = 255; |
312 |
> |
*dp = (v&0xf8)<<7; |
313 |
> |
v = inl[i][GRN] + (random()&7); |
314 |
> |
if (v > 255) v = 255; |
315 |
> |
*dp |= (v&0xf8)<<2; |
316 |
> |
v = inl[i][BLU] + (random()&7); |
317 |
> |
if (v > 255) v = 255; |
318 |
> |
*dp++ |= v>>3; |
319 |
|
} |
320 |
|
} else { /* hp->dataBits == 24 */ |
321 |
|
register unsigned char *dp; |
322 |
|
dp = (unsigned char *)tarData + j*3*hp->x; |
323 |
|
for (i = 0; i < hp->x; i++) { |
324 |
< |
c = 1024.*colval(inline[i],BLU); |
325 |
< |
if (c > 1023) c = 1023; |
326 |
< |
*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]; |
324 |
> |
*dp++ = inl[i][BLU]; |
325 |
> |
*dp++ = inl[i][GRN]; |
326 |
> |
*dp++ = inl[i][RED]; |
327 |
|
} |
328 |
|
} |
329 |
|
} |
330 |
|
/* write out targa data */ |
331 |
|
writetarga(hp, tarData, stdout); |
332 |
|
|
333 |
< |
free((char *)inline); |
334 |
< |
free((char *)tarData); |
333 |
> |
free((void *)inl); |
334 |
> |
free((void *)tarData); |
335 |
|
} |
336 |
|
|
337 |
|
|
341 |
|
FILE *fp; |
342 |
|
{ |
343 |
|
if (h->dataType == IM_RGB) { /* uncompressed */ |
344 |
< |
if (fwrite(d, 3*h->x, h->y, fp) != h->y) |
344 |
> |
if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y) |
345 |
|
quiterr("error writing targa file"); |
346 |
|
return; |
347 |
|
} |
377 |
|
int r, g, b; |
378 |
|
|
379 |
|
if (h->dataType == IM_RGB) { /* uncompressed */ |
380 |
< |
if (fread(data, 3*h->x, h->y, fp) != h->y) |
380 |
> |
if (fread((char *)data, 3*h->x, h->y, fp) != h->y) |
381 |
|
goto readerr; |
382 |
|
return; |
383 |
|
} |