1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: ra_t16.c,v 2.7 2003/06/05 19:29:34 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* ra_t16.c - program to convert between RADIANCE and |
6 |
* Targa 16, 24 and 32-bit images. |
7 |
* |
8 |
* 11/2/88 Adapted from ra_t8.c |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
#include <string.h> |
13 |
#include <time.h> |
14 |
#include <math.h> |
15 |
|
16 |
#include "platform.h" |
17 |
#include "rtmisc.h" |
18 |
#include "color.h" |
19 |
#include "resolu.h" |
20 |
#include "random.h" |
21 |
#include "targa.h" |
22 |
|
23 |
#define goodpic(h) (((h)->dataType==IM_RGB || (h)->dataType==IM_CRGB) \ |
24 |
&& ((h)->dataBits==16 || (h)->dataBits==24)) |
25 |
|
26 |
#define taralloc(h) (unsigned char *)emalloc((h)->x*(h)->y*(h)->dataBits/8) |
27 |
|
28 |
#define readtarga(h,d,f) ((h)->dataBits==16 ? \ |
29 |
readt16(h,(unsigned short*)d,f) : readt24(h,d,f)) |
30 |
|
31 |
#define writetarga(h,d,f) ((h)->dataBits==16 ? \ |
32 |
writet16(h,(unsigned short*)d,f) : writet24(h,d,f)) |
33 |
|
34 |
double gamcor = 2.2; /* gamma correction */ |
35 |
int bradj = 0; /* brightness adjustment */ |
36 |
char *progname; |
37 |
char msg[128]; |
38 |
|
39 |
static int getint2(FILE *fp); |
40 |
static void putint2(int i, FILE *fp); |
41 |
static void quiterr(char *err); |
42 |
static int getthead(struct hdStruct *hp, char *ip, FILE *fp); |
43 |
static int putthead(struct hdStruct *hp, char *ip, FILE *fp); |
44 |
static void tg2ra(struct hdStruct *hp); |
45 |
static void ra2tg(struct hdStruct *hp); |
46 |
static void writet24(struct hdStruct *h, unsigned char *d, FILE *fp); |
47 |
static void writet16(struct hdStruct *h, unsigned short *d, FILE *fp); |
48 |
static void readt24(struct hdStruct *h, unsigned char *data, FILE *fp); |
49 |
static void readt16(struct hdStruct *h, unsigned short *data, FILE *fp); |
50 |
|
51 |
|
52 |
int |
53 |
main(int argc, char *argv[]) |
54 |
{ |
55 |
struct hdStruct head; |
56 |
int reverse = 0; |
57 |
int i; |
58 |
SET_DEFAULT_BINARY(); |
59 |
SET_FILE_BINARY(stdin); |
60 |
SET_FILE_BINARY(stdout); |
61 |
progname = argv[0]; |
62 |
|
63 |
head.dataBits = 16; |
64 |
for (i = 1; i < argc; i++) |
65 |
if (argv[i][0] == '-') |
66 |
switch (argv[i][1]) { |
67 |
case 'g': |
68 |
gamcor = atof(argv[++i]); |
69 |
break; |
70 |
case 'r': |
71 |
reverse = !reverse; |
72 |
break; |
73 |
case '2': |
74 |
head.dataBits = 16; |
75 |
break; |
76 |
case '3': |
77 |
head.dataBits = 24; |
78 |
break; |
79 |
case 'e': |
80 |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
81 |
goto userr; |
82 |
bradj = atoi(argv[++i]); |
83 |
break; |
84 |
default: |
85 |
goto userr; |
86 |
} |
87 |
else |
88 |
break; |
89 |
|
90 |
if (i < argc-2) |
91 |
goto userr; |
92 |
/* open input file */ |
93 |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
94 |
sprintf(msg, "can't open input \"%s\"", argv[i]); |
95 |
quiterr(msg); |
96 |
} |
97 |
/* open output file */ |
98 |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
99 |
sprintf(msg, "can't open output \"%s\"", argv[i+1]); |
100 |
quiterr(msg); |
101 |
} |
102 |
/* set gamma */ |
103 |
setcolrgam(gamcor); |
104 |
/* convert */ |
105 |
if (reverse) { |
106 |
/* get header */ |
107 |
if (getthead(&head, NULL, stdin) < 0) |
108 |
quiterr("bad targa file"); |
109 |
if (!goodpic(&head)) |
110 |
quiterr("incompatible format"); |
111 |
/* put header */ |
112 |
newheader("RADIANCE", stdout); |
113 |
printargs(i, argv, stdout); |
114 |
fputformat(COLRFMT, stdout); |
115 |
putchar('\n'); |
116 |
fprtresolu(head.x, head.y, stdout); |
117 |
/* convert file */ |
118 |
tg2ra(&head); |
119 |
} else { |
120 |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
121 |
fgetresolu(&head.x, &head.y, stdin) < 0) |
122 |
quiterr("bad picture file"); |
123 |
/* assign header */ |
124 |
head.textSize = 0; |
125 |
head.mapType = CM_NOMAP; |
126 |
head.dataType = IM_RGB; |
127 |
head.XOffset = 0; |
128 |
head.YOffset = 0; |
129 |
head.imType = 0; |
130 |
/* write header */ |
131 |
putthead(&head, NULL, stdout); |
132 |
/* convert file */ |
133 |
ra2tg(&head); |
134 |
} |
135 |
exit(0); |
136 |
userr: |
137 |
fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma][-e +/-stops] [input [output]]\n", |
138 |
progname); |
139 |
exit(1); |
140 |
} |
141 |
|
142 |
|
143 |
static int |
144 |
getint2( /* get a 2-byte positive integer */ |
145 |
register FILE *fp |
146 |
) |
147 |
{ |
148 |
register int b1, b2; |
149 |
|
150 |
if ((b1 = getc(fp)) == EOF || (b2 = getc(fp)) == EOF) |
151 |
quiterr("read error"); |
152 |
|
153 |
return(b1 | b2<<8); |
154 |
} |
155 |
|
156 |
|
157 |
static void |
158 |
putint2( /* put a 2-byte positive integer */ |
159 |
register int i, |
160 |
register FILE *fp |
161 |
) |
162 |
{ |
163 |
putc(i&0xff, fp); |
164 |
putc(i>>8&0xff, fp); |
165 |
} |
166 |
|
167 |
|
168 |
static void |
169 |
quiterr( /* print message and exit */ |
170 |
char *err |
171 |
) |
172 |
{ |
173 |
fprintf(stderr, "%s: %s\n", progname, err); |
174 |
exit(1); |
175 |
} |
176 |
|
177 |
|
178 |
void |
179 |
eputs( |
180 |
char *s |
181 |
) |
182 |
{ |
183 |
fputs(s, stderr); |
184 |
} |
185 |
|
186 |
|
187 |
void |
188 |
quit( |
189 |
int code |
190 |
) |
191 |
{ |
192 |
exit(code); |
193 |
} |
194 |
|
195 |
|
196 |
static int |
197 |
getthead( /* read header from input */ |
198 |
struct hdStruct *hp, |
199 |
char *ip, |
200 |
register FILE *fp |
201 |
) |
202 |
{ |
203 |
int nidbytes; |
204 |
|
205 |
if ((nidbytes = getc(fp)) == EOF) |
206 |
return(-1); |
207 |
hp->mapType = getc(fp); |
208 |
hp->dataType = getc(fp); |
209 |
hp->mapOrig = getint2(fp); |
210 |
hp->mapLength = getint2(fp); |
211 |
hp->CMapBits = getc(fp); |
212 |
hp->XOffset = getint2(fp); |
213 |
hp->YOffset = getint2(fp); |
214 |
hp->x = getint2(fp); |
215 |
hp->y = getint2(fp); |
216 |
hp->dataBits = getc(fp); |
217 |
hp->imType = getc(fp); |
218 |
|
219 |
if (ip != NULL) |
220 |
if (nidbytes) |
221 |
fread((char *)ip, nidbytes, 1, fp); |
222 |
else |
223 |
*ip = '\0'; |
224 |
else if (nidbytes) |
225 |
fseek(fp, (long)nidbytes, 1); |
226 |
|
227 |
return(feof(fp) || ferror(fp) ? -1 : 0); |
228 |
} |
229 |
|
230 |
|
231 |
static int |
232 |
putthead( /* write header to output */ |
233 |
struct hdStruct *hp, |
234 |
char *ip, |
235 |
register FILE *fp |
236 |
) |
237 |
{ |
238 |
if (ip != NULL) |
239 |
putc(strlen(ip), fp); |
240 |
else |
241 |
putc(0, fp); |
242 |
putc(hp->mapType, fp); |
243 |
putc(hp->dataType, fp); |
244 |
putint2(hp->mapOrig, fp); |
245 |
putint2(hp->mapLength, fp); |
246 |
putc(hp->CMapBits, fp); |
247 |
putint2(hp->XOffset, fp); |
248 |
putint2(hp->YOffset, fp); |
249 |
putint2(hp->x, fp); |
250 |
putint2(hp->y, fp); |
251 |
putc(hp->dataBits, fp); |
252 |
putc(hp->imType, fp); |
253 |
|
254 |
if (ip != NULL) |
255 |
fputs(ip, fp); |
256 |
|
257 |
return(ferror(fp) ? -1 : 0); |
258 |
} |
259 |
|
260 |
|
261 |
static void |
262 |
tg2ra( /* targa file to RADIANCE file */ |
263 |
struct hdStruct *hp |
264 |
) |
265 |
{ |
266 |
COLR *scanline; |
267 |
unsigned char *tarData; |
268 |
register int i, j; |
269 |
/* skip color table */ |
270 |
if (hp->mapType == CM_HASMAP) |
271 |
fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1); |
272 |
/* allocate targa data */ |
273 |
tarData = taralloc(hp); |
274 |
/* get data */ |
275 |
readtarga(hp, tarData, stdin); |
276 |
/* allocate input scanline */ |
277 |
scanline = (COLR *)emalloc(hp->x*sizeof(COLR)); |
278 |
/* convert file */ |
279 |
for (i = hp->y-1; i >= 0; i--) { |
280 |
if (hp->dataBits == 16) { |
281 |
register unsigned short *dp; |
282 |
dp = (unsigned short *)tarData + i*hp->x; |
283 |
for (j = 0; j < hp->x; j++) { |
284 |
scanline[j][RED] = *dp>>7 & 0xf8; |
285 |
scanline[j][GRN] = *dp>>2 & 0xf8; |
286 |
scanline[j][BLU] = *dp<<3 & 0xf8; |
287 |
dp++; |
288 |
} |
289 |
} else { /* hp->dataBits == 24 */ |
290 |
register unsigned char *dp; |
291 |
dp = (unsigned char *)tarData + i*3*hp->x; |
292 |
for (j = 0; j < hp->x; j++) { |
293 |
scanline[j][RED] = dp[2]; |
294 |
scanline[j][GRN] = dp[1]; |
295 |
scanline[j][BLU] = dp[0]; |
296 |
dp += 3; |
297 |
} |
298 |
} |
299 |
gambs_colrs(scanline, hp->x); |
300 |
if (bradj) |
301 |
shiftcolrs(scanline, hp->x, bradj); |
302 |
if (fwritecolrs(scanline, hp->x, stdout) < 0) |
303 |
quiterr("error writing RADIANCE file"); |
304 |
} |
305 |
free((void *)scanline); |
306 |
free((void *)tarData); |
307 |
} |
308 |
|
309 |
|
310 |
static void |
311 |
ra2tg( /* convert radiance to targa file */ |
312 |
struct hdStruct *hp |
313 |
) |
314 |
{ |
315 |
register int i, j; |
316 |
unsigned char *tarData; |
317 |
COLR *inl; |
318 |
/* allocate space for data */ |
319 |
inl = (COLR *)emalloc(hp->x*sizeof(COLR)); |
320 |
tarData = taralloc(hp); |
321 |
/* convert file */ |
322 |
for (j = hp->y-1; j >= 0; j--) { |
323 |
if (freadcolrs(inl, hp->x, stdin) < 0) |
324 |
quiterr("error reading RADIANCE file"); |
325 |
if (bradj) |
326 |
shiftcolrs(inl, hp->x, bradj); |
327 |
colrs_gambs(inl, hp->x); |
328 |
if (hp->dataBits == 16) { |
329 |
register unsigned short *dp; |
330 |
register int v; |
331 |
dp = (unsigned short *)tarData + j*hp->x; |
332 |
for (i = 0; i < hp->x; i++) { |
333 |
v = inl[i][RED] + (random()&7); |
334 |
if (v > 255) v = 255; |
335 |
*dp = (v&0xf8)<<7; |
336 |
v = inl[i][GRN] + (random()&7); |
337 |
if (v > 255) v = 255; |
338 |
*dp |= (v&0xf8)<<2; |
339 |
v = inl[i][BLU] + (random()&7); |
340 |
if (v > 255) v = 255; |
341 |
*dp++ |= v>>3; |
342 |
} |
343 |
} else { /* hp->dataBits == 24 */ |
344 |
register unsigned char *dp; |
345 |
dp = (unsigned char *)tarData + j*3*hp->x; |
346 |
for (i = 0; i < hp->x; i++) { |
347 |
*dp++ = inl[i][BLU]; |
348 |
*dp++ = inl[i][GRN]; |
349 |
*dp++ = inl[i][RED]; |
350 |
} |
351 |
} |
352 |
} |
353 |
/* write out targa data */ |
354 |
writetarga(hp, tarData, stdout); |
355 |
|
356 |
free((void *)inl); |
357 |
free((void *)tarData); |
358 |
} |
359 |
|
360 |
|
361 |
static void |
362 |
writet24( /* write out 24-bit targa data */ |
363 |
struct hdStruct *h, |
364 |
unsigned char *d, |
365 |
FILE *fp |
366 |
) |
367 |
{ |
368 |
if (h->dataType == IM_RGB) { /* uncompressed */ |
369 |
if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y) |
370 |
quiterr("error writing targa file"); |
371 |
return; |
372 |
} |
373 |
quiterr("unsupported output type"); |
374 |
} |
375 |
|
376 |
|
377 |
static void |
378 |
writet16( /* write out 16-bit targa data */ |
379 |
struct hdStruct *h, |
380 |
register unsigned short *d, |
381 |
FILE *fp |
382 |
) |
383 |
{ |
384 |
register int cnt; |
385 |
|
386 |
if (h->dataType == IM_RGB) { /* uncompressed */ |
387 |
for (cnt = h->x*h->y; cnt-- > 0; ) |
388 |
putint2(*d++, fp); |
389 |
if (ferror(fp)) |
390 |
quiterr("error writing targa file"); |
391 |
return; |
392 |
} |
393 |
quiterr("unsupported output type"); |
394 |
} |
395 |
|
396 |
|
397 |
static void |
398 |
readt24( /* read in 24-bit targa data */ |
399 |
register struct hdStruct *h, |
400 |
unsigned char *data, |
401 |
FILE *fp |
402 |
) |
403 |
{ |
404 |
register int cnt, c; |
405 |
register unsigned char *dp; |
406 |
int r, g, b; |
407 |
|
408 |
if (h->dataType == IM_RGB) { /* uncompressed */ |
409 |
if (fread((char *)data, 3*h->x, h->y, fp) != h->y) |
410 |
goto readerr; |
411 |
return; |
412 |
} |
413 |
for (dp = data; dp < data+3*h->x*h->y; ) { |
414 |
if ((c = getc(fp)) == EOF) |
415 |
goto readerr; |
416 |
cnt = (c & 0x7f) + 1; |
417 |
if (c & 0x80) { /* repeated pixel */ |
418 |
b = getc(fp); g = getc(fp); |
419 |
if ((r = getc(fp)) == EOF) |
420 |
goto readerr; |
421 |
while (cnt--) { |
422 |
*dp++ = b; |
423 |
*dp++ = g; |
424 |
*dp++ = r; |
425 |
} |
426 |
} else /* non-repeating pixels */ |
427 |
while (cnt--) { |
428 |
*dp++ = getc(fp); *dp++ = getc(fp); |
429 |
if ((r = getc(fp)) == EOF) |
430 |
goto readerr; |
431 |
*dp++ = r; |
432 |
} |
433 |
} |
434 |
return; |
435 |
readerr: |
436 |
quiterr("error reading targa file"); |
437 |
} |
438 |
|
439 |
|
440 |
static void |
441 |
readt16( /* read in 16-bit targa data */ |
442 |
register struct hdStruct *h, |
443 |
unsigned short *data, |
444 |
FILE *fp |
445 |
) |
446 |
{ |
447 |
register int cnt, c; |
448 |
register unsigned short *dp; |
449 |
|
450 |
if (h->dataType == IM_RGB) { /* uncompressed */ |
451 |
dp = data; |
452 |
for (cnt = h->x*h->y; cnt-- > 0; ) |
453 |
*dp++ = getint2(fp); |
454 |
return; |
455 |
} |
456 |
for (dp = data; dp < data+h->x*h->y; ) { |
457 |
if ((c = getc(fp)) == EOF) |
458 |
goto readerr; |
459 |
cnt = (c & 0x7f) + 1; |
460 |
if (c & 0x80) { /* repeated pixel */ |
461 |
c = getint2(fp); |
462 |
while (cnt--) |
463 |
*dp++ = c; |
464 |
} else /* non-repeating pixels */ |
465 |
while (cnt--) |
466 |
*dp++ = getint2(fp); |
467 |
} |
468 |
return; |
469 |
readerr: |
470 |
quiterr("error reading targa file"); |
471 |
} |