1 |
– |
/* Copyright (c) 1991 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 "rtmisc.h" |
18 |
|
#include "color.h" |
19 |
< |
|
19 |
> |
#include "resolu.h" |
20 |
|
#include "random.h" |
19 |
– |
|
21 |
|
#include "targa.h" |
22 |
|
|
23 |
|
#define goodpic(h) (((h)->dataType==IM_RGB || (h)->dataType==IM_CRGB) \ |
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 ? readt16(h,d,f) : \ |
29 |
< |
readt24(h,d,f)) |
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 ? writet16(h,d,f) : \ |
32 |
< |
writet24(h,d,f)) |
31 |
> |
#define writetarga(h,d,f) ((h)->dataBits==16 ? \ |
32 |
> |
writet16(h,(unsigned short*)d,f) : writet24(h,d,f)) |
33 |
|
|
34 |
< |
extern char *ecalloc(), *emalloc(); |
35 |
< |
|
35 |
< |
extern double atof(), pow(); |
36 |
< |
|
37 |
< |
double gamma = 2.0; /* gamma correction */ |
38 |
< |
|
34 |
> |
double gamcor = 2.2; /* gamma correction */ |
35 |
> |
int bradj = 0; /* brightness adjustment */ |
36 |
|
char *progname; |
40 |
– |
|
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 |
< |
main(argc, argv) |
52 |
< |
int argc; |
53 |
< |
char *argv[]; |
51 |
> |
|
52 |
> |
int |
53 |
> |
main(int argc, char *argv[]) |
54 |
|
{ |
55 |
|
struct hdStruct head; |
56 |
|
int reverse = 0; |
57 |
|
int i; |
58 |
< |
|
58 |
> |
SET_DEFAULT_BINARY(); |
59 |
> |
SET_FILE_BINARY(stdin); |
60 |
> |
SET_FILE_BINARY(stdout); |
61 |
|
progname = argv[0]; |
62 |
|
|
63 |
|
head.dataBits = 16; |
65 |
|
if (argv[i][0] == '-') |
66 |
|
switch (argv[i][1]) { |
67 |
|
case 'g': |
68 |
< |
gamma = atof(argv[++i]); |
68 |
> |
gamcor = atof(argv[++i]); |
69 |
|
break; |
70 |
|
case 'r': |
71 |
|
reverse = !reverse; |
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 |
|
} |
100 |
|
quiterr(msg); |
101 |
|
} |
102 |
|
/* set gamma */ |
103 |
< |
setcolrgam(gamma); |
103 |
> |
setcolrgam(gamcor); |
104 |
|
/* convert */ |
105 |
|
if (reverse) { |
106 |
|
/* get header */ |
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 |
< |
fputresolu(YMAJOR|YDECR, head.x, head.y, stdout); |
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) != (YMAJOR|YDECR)) |
121 |
> |
fgetresolu(&head.x, &head.y, stdin) < 0) |
122 |
|
quiterr("bad picture file"); |
123 |
|
/* assign header */ |
124 |
|
head.textSize = 0; |
134 |
|
} |
135 |
|
exit(0); |
136 |
|
userr: |
137 |
< |
fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma] [input [output]]\n", |
137 |
> |
fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma][-e +/-stops] [input [output]]\n", |
138 |
|
progname); |
139 |
|
exit(1); |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
< |
int |
144 |
< |
getint2(fp) /* get a 2-byte positive integer */ |
145 |
< |
register FILE *fp; |
143 |
> |
static int |
144 |
> |
getint2( /* get a 2-byte positive integer */ |
145 |
> |
register FILE *fp |
146 |
> |
) |
147 |
|
{ |
148 |
|
register int b1, b2; |
149 |
|
|
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
< |
putint2(i, fp) /* put a 2-byte positive integer */ |
158 |
< |
register int i; |
159 |
< |
register FILE *fp; |
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 |
< |
quiterr(err) /* print message and exit */ |
169 |
< |
char *err; |
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 |
< |
eputs(s) |
179 |
< |
char *s; |
178 |
> |
void |
179 |
> |
eputs( |
180 |
> |
char *s |
181 |
> |
) |
182 |
|
{ |
183 |
|
fputs(s, stderr); |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
< |
quit(code) |
188 |
< |
int code; |
187 |
> |
void |
188 |
> |
quit( |
189 |
> |
int code |
190 |
> |
) |
191 |
|
{ |
192 |
|
exit(code); |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
< |
getthead(hp, ip, fp) /* read header from input */ |
197 |
< |
struct hdStruct *hp; |
198 |
< |
char *ip; |
199 |
< |
register FILE *fp; |
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 |
|
|
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
< |
putthead(hp, ip, fp) /* write header to output */ |
232 |
< |
struct hdStruct *hp; |
233 |
< |
char *ip; |
234 |
< |
register FILE *fp; |
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); |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
< |
tg2ra(hp) /* targa file to RADIANCE file */ |
262 |
< |
struct hdStruct *hp; |
261 |
> |
static void |
262 |
> |
tg2ra( /* targa file to RADIANCE file */ |
263 |
> |
struct hdStruct *hp |
264 |
> |
) |
265 |
|
{ |
266 |
|
COLR *scanline; |
267 |
|
unsigned char *tarData; |
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((char *)scanline); |
306 |
< |
free((char *)tarData); |
305 |
> |
free((void *)scanline); |
306 |
> |
free((void *)tarData); |
307 |
|
} |
308 |
|
|
309 |
|
|
310 |
< |
ra2tg(hp) /* convert radiance to targa file */ |
311 |
< |
struct hdStruct *hp; |
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; |
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; |
353 |
|
/* write out targa data */ |
354 |
|
writetarga(hp, tarData, stdout); |
355 |
|
|
356 |
< |
free((char *)inl); |
357 |
< |
free((char *)tarData); |
356 |
> |
free((void *)inl); |
357 |
> |
free((void *)tarData); |
358 |
|
} |
359 |
|
|
360 |
|
|
361 |
< |
writet24(h, d, fp) /* write out 24-bit targa data */ |
362 |
< |
struct hdStruct *h; |
363 |
< |
unsigned char *d; |
364 |
< |
FILE *fp; |
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) |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
< |
writet16(h, d, fp) /* write out 16-bit targa data */ |
378 |
< |
struct hdStruct *h; |
379 |
< |
register unsigned short *d; |
380 |
< |
FILE *fp; |
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 |
|
|
394 |
|
} |
395 |
|
|
396 |
|
|
397 |
< |
readt24(h, data, fp) /* read in 24-bit targa data */ |
398 |
< |
register struct hdStruct *h; |
399 |
< |
unsigned char *data; |
400 |
< |
FILE *fp; |
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; |
437 |
|
} |
438 |
|
|
439 |
|
|
440 |
< |
readt16(h, data, fp) /* read in 16-bit targa data */ |
441 |
< |
register struct hdStruct *h; |
442 |
< |
unsigned short *data; |
443 |
< |
FILE *fp; |
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; |