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