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 <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 |
< |
|
20 |
> |
#include "resolu.h" |
21 |
|
#include "random.h" |
19 |
– |
|
22 |
|
#include "targa.h" |
23 |
|
|
24 |
|
#define goodpic(h) (((h)->dataType==IM_RGB || (h)->dataType==IM_CRGB) \ |
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 ? readt16(h,d,f) : \ |
30 |
< |
readt24(h,d,f)) |
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 ? writet16(h,d,f) : \ |
33 |
< |
writet24(h,d,f)) |
32 |
> |
#define writetarga(h,d,f) ((h)->dataBits==16 ? \ |
33 |
> |
writet16(h,(unsigned short*)d,f) : writet24(h,d,f)) |
34 |
|
|
35 |
< |
extern char *ecalloc(), *emalloc(); |
36 |
< |
|
35 |
< |
extern double atof(), pow(); |
36 |
< |
|
37 |
< |
double gamma = 2.0; /* gamma correction */ |
38 |
< |
|
35 |
> |
double gamcor = 2.2; /* gamma correction */ |
36 |
> |
int bradj = 0; /* brightness adjustment */ |
37 |
|
char *progname; |
40 |
– |
|
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 |
< |
main(argc, argv) |
53 |
< |
int argc; |
54 |
< |
char *argv[]; |
52 |
> |
|
53 |
> |
int |
54 |
> |
main(int argc, char *argv[]) |
55 |
|
{ |
56 |
|
struct hdStruct head; |
57 |
|
int reverse = 0; |
58 |
|
int i; |
59 |
< |
|
59 |
> |
SET_DEFAULT_BINARY(); |
60 |
> |
SET_FILE_BINARY(stdin); |
61 |
> |
SET_FILE_BINARY(stdout); |
62 |
|
progname = argv[0]; |
63 |
|
|
64 |
|
head.dataBits = 16; |
66 |
|
if (argv[i][0] == '-') |
67 |
|
switch (argv[i][1]) { |
68 |
|
case 'g': |
69 |
< |
gamma = atof(argv[++i]); |
69 |
> |
gamcor = atof(argv[++i]); |
70 |
|
break; |
71 |
|
case 'r': |
72 |
|
reverse = !reverse; |
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 |
|
} |
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 */ |
110 |
|
if (!goodpic(&head)) |
111 |
|
quiterr("incompatible format"); |
112 |
|
/* put header */ |
113 |
< |
printargs(argc, argv, stdout); |
113 |
> |
newheader("RADIANCE", stdout); |
114 |
> |
printargs(i, argv, stdout); |
115 |
> |
fputformat(COLRFMT, stdout); |
116 |
|
putchar('\n'); |
117 |
< |
fputresolu(YMAJOR|YDECR, head.x, head.y, stdout); |
117 |
> |
fprtresolu(head.x, head.y, stdout); |
118 |
|
/* convert file */ |
119 |
|
tg2ra(&head); |
120 |
|
} else { |
121 |
< |
getheader(stdin, NULL); |
122 |
< |
if (fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR)) |
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; |
135 |
|
} |
136 |
|
exit(0); |
137 |
|
userr: |
138 |
< |
fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma] [input [output]]\n", |
138 |
> |
fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma][-e +/-stops] [input [output]]\n", |
139 |
|
progname); |
140 |
|
exit(1); |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
< |
int |
145 |
< |
getint2(fp) /* get a 2-byte positive integer */ |
146 |
< |
register FILE *fp; |
144 |
> |
static int |
145 |
> |
getint2( /* get a 2-byte positive integer */ |
146 |
> |
register FILE *fp |
147 |
> |
) |
148 |
|
{ |
149 |
|
register int b1, b2; |
150 |
|
|
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
< |
putint2(i, fp) /* put a 2-byte positive integer */ |
159 |
< |
register int i; |
160 |
< |
register FILE *fp; |
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 |
< |
quiterr(err) /* print message and exit */ |
170 |
< |
char *err; |
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 |
< |
eputs(s) |
180 |
< |
char *s; |
179 |
> |
void |
180 |
> |
eputs( |
181 |
> |
char *s |
182 |
> |
) |
183 |
|
{ |
184 |
|
fputs(s, stderr); |
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
< |
quit(code) |
189 |
< |
int code; |
188 |
> |
void |
189 |
> |
quit( |
190 |
> |
int code |
191 |
> |
) |
192 |
|
{ |
193 |
|
exit(code); |
194 |
|
} |
195 |
|
|
196 |
|
|
197 |
< |
getthead(hp, ip, fp) /* read header from input */ |
198 |
< |
struct hdStruct *hp; |
199 |
< |
char *ip; |
200 |
< |
register FILE *fp; |
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 |
|
|
219 |
|
|
220 |
|
if (ip != NULL) |
221 |
|
if (nidbytes) |
222 |
< |
fread(ip, nidbytes, 1, fp); |
222 |
> |
fread((char *)ip, nidbytes, 1, fp); |
223 |
|
else |
224 |
|
*ip = '\0'; |
225 |
|
else if (nidbytes) |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
< |
putthead(hp, ip, fp) /* write header to output */ |
233 |
< |
struct hdStruct *hp; |
234 |
< |
char *ip; |
235 |
< |
register FILE *fp; |
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); |
259 |
|
} |
260 |
|
|
261 |
|
|
262 |
< |
tg2ra(hp) /* targa file to RADIANCE file */ |
263 |
< |
struct hdStruct *hp; |
262 |
> |
static void |
263 |
> |
tg2ra( /* targa file to RADIANCE file */ |
264 |
> |
struct hdStruct *hp |
265 |
> |
) |
266 |
|
{ |
267 |
< |
float gmap[256]; |
234 |
< |
COLOR *scanline; |
267 |
> |
COLR *scanline; |
268 |
|
unsigned char *tarData; |
269 |
|
register int i, j; |
237 |
– |
/* set up gamma correction */ |
238 |
– |
for (i = 0; i < 256; i++) |
239 |
– |
gmap[i] = pow((i+.5)/256., gamma); |
270 |
|
/* skip color table */ |
271 |
|
if (hp->mapType == CM_HASMAP) |
272 |
|
fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1); |
275 |
|
/* get data */ |
276 |
|
readtarga(hp, tarData, stdin); |
277 |
|
/* allocate input scanline */ |
278 |
< |
scanline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
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 |
< |
setcolor(scanline[j], gmap[*dp>>7 & 0xf8], |
286 |
< |
gmap[*dp>>2 & 0xf8], |
287 |
< |
gmap[*dp<<3 & 0xf8]); |
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 |
< |
setcolor(scanline[j], gmap[dp[2]], |
295 |
< |
gmap[dp[1]], |
296 |
< |
gmap[dp[0]]); |
294 |
> |
scanline[j][RED] = dp[2]; |
295 |
> |
scanline[j][GRN] = dp[1]; |
296 |
> |
scanline[j][BLU] = dp[0]; |
297 |
|
dp += 3; |
298 |
|
} |
299 |
|
} |
300 |
< |
if (fwritescan(scanline, hp->x, stdout) < 0) |
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((char *)scanline); |
307 |
< |
free((char *)tarData); |
306 |
> |
free((void *)scanline); |
307 |
> |
free((void *)tarData); |
308 |
|
} |
309 |
|
|
310 |
|
|
311 |
< |
ra2tg(hp) /* convert radiance to targa file */ |
312 |
< |
struct hdStruct *hp; |
311 |
> |
static void |
312 |
> |
ra2tg( /* convert radiance to targa file */ |
313 |
> |
struct hdStruct *hp |
314 |
> |
) |
315 |
|
{ |
281 |
– |
#define map(v) (v >= 1.0 ? 1023 : (int)(v*1023.+.5)) |
282 |
– |
unsigned char gmap[1024]; |
316 |
|
register int i, j; |
317 |
|
unsigned char *tarData; |
318 |
< |
COLOR *inline; |
286 |
< |
/* set up gamma correction */ |
287 |
< |
for (i = 0; i < 1024; i++) { |
288 |
< |
j = 256.*pow((i+.5)/1024., 1./gamma); |
289 |
< |
gmap[i] = hp->dataBits == 16 && j > 248 ? 248 : j; |
290 |
< |
} |
318 |
> |
COLR *inl; |
319 |
|
/* allocate space for data */ |
320 |
< |
inline = (COLOR *)emalloc(hp->x*sizeof(COLOR)); |
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 (freadscan(inline, hp->x, stdin) < 0) |
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 |
< |
*dp = ((gmap[map(colval(inline[i],RED))] |
335 |
< |
+(random()&7)) & 0xf8)<<7; |
336 |
< |
*dp |= ((gmap[map(colval(inline[i],GRN))] |
337 |
< |
+(random()&7)) & 0xf8)<<2; |
338 |
< |
*dp++ |= (gmap[map(colval(inline[i],BLU))] |
339 |
< |
+(random()&7))>>3; |
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++ = gmap[map(colval(inline[i],BLU))]; |
349 |
< |
*dp++ = gmap[map(colval(inline[i],GRN))]; |
350 |
< |
*dp++ = gmap[map(colval(inline[i],RED))]; |
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((char *)inline); |
358 |
< |
free((char *)tarData); |
324 |
< |
#undef map |
357 |
> |
free((void *)inl); |
358 |
> |
free((void *)tarData); |
359 |
|
} |
360 |
|
|
361 |
|
|
362 |
< |
writet24(h, d, fp) /* write out 24-bit targa data */ |
363 |
< |
struct hdStruct *h; |
364 |
< |
unsigned char *d; |
365 |
< |
FILE *fp; |
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(d, 3*h->x, h->y, fp) != h->y) |
370 |
> |
if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y) |
371 |
|
quiterr("error writing targa file"); |
372 |
|
return; |
373 |
|
} |
375 |
|
} |
376 |
|
|
377 |
|
|
378 |
< |
writet16(h, d, fp) /* write out 16-bit targa data */ |
379 |
< |
struct hdStruct *h; |
380 |
< |
register unsigned short *d; |
381 |
< |
FILE *fp; |
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 |
|
|
395 |
|
} |
396 |
|
|
397 |
|
|
398 |
< |
readt24(h, data, fp) /* read in 24-bit targa data */ |
399 |
< |
register struct hdStruct *h; |
400 |
< |
unsigned char *data; |
401 |
< |
FILE *fp; |
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(data, 3*h->x, h->y, fp) != h->y) |
410 |
> |
if (fread((char *)data, 3*h->x, h->y, fp) != h->y) |
411 |
|
goto readerr; |
412 |
|
return; |
413 |
|
} |
438 |
|
} |
439 |
|
|
440 |
|
|
441 |
< |
readt16(h, data, fp) /* read in 16-bit targa data */ |
442 |
< |
register struct hdStruct *h; |
443 |
< |
unsigned short *data; |
444 |
< |
FILE *fp; |
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; |