1 |
– |
/* Copyright (c) 1992 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_t8.c - program to convert between RADIANCE and |
6 |
|
* Targa 8-bit color-mapped images. |
8 |
|
* 8/22/88 Adapted from ra_pr.c |
9 |
|
*/ |
10 |
|
|
11 |
< |
#include <stdio.h> |
11 |
> |
#include <math.h> |
12 |
|
|
13 |
+ |
#include "platform.h" |
14 |
+ |
#include "rtio.h" |
15 |
+ |
#include "rtmisc.h" |
16 |
|
#include "color.h" |
17 |
– |
|
17 |
|
#include "resolu.h" |
18 |
< |
|
18 |
> |
#include "clrtab.h" |
19 |
|
#include "targa.h" |
20 |
|
|
22 |
– |
#ifdef MSDOS |
23 |
– |
#include <fcntl.h> |
24 |
– |
#endif |
21 |
|
|
26 |
– |
#include <math.h> |
27 |
– |
|
28 |
– |
#ifndef BSD |
29 |
– |
#define bcopy(s,d,n) (void)memcpy(d,s,n) |
30 |
– |
#endif |
31 |
– |
|
22 |
|
#define goodpic(h) (my_imType(h) && my_mapType(h)) |
23 |
|
#define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \ |
24 |
|
&& (h)->dataBits==8 && (h)->imType==0) |
25 |
|
#define my_mapType(h) ((h)->mapType==CM_HASMAP && \ |
26 |
|
((h)->CMapBits==24 || (h)->CMapBits==32)) |
27 |
|
|
28 |
< |
#define taralloc(h) (BYTE *)emalloc((h)->x*(h)->y) |
28 |
> |
#define taralloc(h) (uby8 *)emalloc((h)->x*(h)->y) |
29 |
|
|
30 |
< |
BYTE clrtab[256][3]; |
41 |
< |
|
30 |
> |
uby8 clrtab[256][3]; |
31 |
|
extern int samplefac; |
43 |
– |
|
44 |
– |
extern char *ecalloc(), *emalloc(); |
45 |
– |
|
46 |
– |
extern long ftell(); |
47 |
– |
|
32 |
|
double gamv = 2.2; /* gamv correction */ |
49 |
– |
|
33 |
|
int bradj = 0; /* brightness adjustment */ |
51 |
– |
|
34 |
|
char *progname; |
53 |
– |
|
35 |
|
char errmsg[128]; |
55 |
– |
|
36 |
|
COLR *inl; |
37 |
< |
|
58 |
< |
BYTE *tarData; |
59 |
< |
|
37 |
> |
uby8 *tarData; |
38 |
|
int xmax, ymax; |
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 int getrhead(struct hdStruct *h, FILE *fp); |
46 |
+ |
static void tg2ra(struct hdStruct *hp); |
47 |
+ |
static void getmapped(int nc, int dith); |
48 |
+ |
static void getgrey(int nc); |
49 |
+ |
static void writetarga(struct hdStruct *h, uby8 *d, FILE *fp); |
50 |
+ |
static void readtarga(struct hdStruct *h, uby8 *data, FILE *fp); |
51 |
|
|
52 |
< |
main(argc, argv) |
53 |
< |
int argc; |
54 |
< |
char *argv[]; |
52 |
> |
|
53 |
> |
int |
54 |
> |
main( |
55 |
> |
int argc, |
56 |
> |
char *argv[] |
57 |
> |
) |
58 |
|
{ |
59 |
|
struct hdStruct head; |
60 |
|
int dither = 1; |
62 |
|
int ncolors = 256; |
63 |
|
int greyscale = 0; |
64 |
|
int i; |
65 |
< |
#ifdef MSDOS |
66 |
< |
extern int _fmode; |
67 |
< |
_fmode = O_BINARY; |
76 |
< |
setmode(fileno(stdin), O_BINARY); |
77 |
< |
setmode(fileno(stdout), O_BINARY); |
78 |
< |
#endif |
65 |
> |
SET_DEFAULT_BINARY(); |
66 |
> |
SET_FILE_BINARY(stdin); |
67 |
> |
SET_FILE_BINARY(stdout); |
68 |
|
progname = argv[0]; |
69 |
|
samplefac = 0; |
70 |
|
|
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
< |
int |
154 |
< |
getint2(fp) /* get a 2-byte positive integer */ |
155 |
< |
register FILE *fp; |
153 |
> |
static int |
154 |
> |
getint2( /* get a 2-byte positive integer */ |
155 |
> |
FILE *fp |
156 |
> |
) |
157 |
|
{ |
158 |
< |
register int b1, b2; |
158 |
> |
int b1, b2; |
159 |
|
|
160 |
|
if ((b1 = getc(fp)) == EOF || (b2 = getc(fp)) == EOF) |
161 |
|
quiterr("read error"); |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
< |
putint2(i, fp) /* put a 2-byte positive integer */ |
168 |
< |
register int i; |
169 |
< |
register FILE *fp; |
167 |
> |
static void |
168 |
> |
putint2( /* put a 2-byte positive integer */ |
169 |
> |
int i, |
170 |
> |
FILE *fp |
171 |
> |
) |
172 |
|
{ |
173 |
|
putc(i&0xff, fp); |
174 |
|
putc(i>>8&0xff, fp); |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
< |
quiterr(err) /* print message and exit */ |
179 |
< |
char *err; |
178 |
> |
static void |
179 |
> |
quiterr( /* print message and exit */ |
180 |
> |
char *err |
181 |
> |
) |
182 |
|
{ |
183 |
|
if (err != NULL) { |
184 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
< |
eputs(s) |
192 |
< |
char *s; |
191 |
> |
void |
192 |
> |
eputs(const char *s) |
193 |
|
{ |
194 |
|
fputs(s, stderr); |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
< |
quit(code) |
199 |
< |
int code; |
198 |
> |
void |
199 |
> |
quit(int code) |
200 |
|
{ |
201 |
|
exit(code); |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
< |
getthead(hp, ip, fp) /* read header from input */ |
206 |
< |
struct hdStruct *hp; |
207 |
< |
char *ip; |
208 |
< |
register FILE *fp; |
205 |
> |
static int |
206 |
> |
getthead( /* read header from input */ |
207 |
> |
struct hdStruct *hp, |
208 |
> |
char *ip, |
209 |
> |
FILE *fp |
210 |
> |
) |
211 |
|
{ |
212 |
|
int nidbytes; |
213 |
|
|
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
< |
putthead(hp, ip, fp) /* write header to output */ |
241 |
< |
struct hdStruct *hp; |
242 |
< |
char *ip; |
243 |
< |
register FILE *fp; |
240 |
> |
static int |
241 |
> |
putthead( /* write header to output */ |
242 |
> |
struct hdStruct *hp, |
243 |
> |
char *ip, |
244 |
> |
FILE *fp |
245 |
> |
) |
246 |
|
{ |
247 |
|
if (ip != NULL) |
248 |
|
putc(strlen(ip), fp); |
267 |
|
} |
268 |
|
|
269 |
|
|
270 |
< |
getrhead(h, fp) /* load RADIANCE input file header */ |
271 |
< |
register struct hdStruct *h; |
272 |
< |
FILE *fp; |
270 |
> |
static int |
271 |
> |
getrhead( /* load RADIANCE input file header */ |
272 |
> |
struct hdStruct *h, |
273 |
> |
FILE *fp |
274 |
> |
) |
275 |
|
{ |
276 |
|
/* get header info. */ |
277 |
|
if (checkheader(fp, COLRFMT, NULL) < 0 || |
299 |
|
} |
300 |
|
|
301 |
|
|
302 |
< |
tg2ra(hp) /* targa file to RADIANCE file */ |
303 |
< |
struct hdStruct *hp; |
302 |
> |
static void |
303 |
> |
tg2ra( /* targa file to RADIANCE file */ |
304 |
> |
struct hdStruct *hp |
305 |
> |
) |
306 |
|
{ |
307 |
|
union { |
308 |
< |
BYTE c3[256][3]; |
309 |
< |
BYTE c4[256][4]; |
308 |
> |
uby8 c3[256][3]; |
309 |
> |
uby8 c4[256][4]; |
310 |
|
} map; |
311 |
|
COLR ctab[256]; |
312 |
|
COLR *scanline; |
313 |
< |
register int i, j; |
313 |
> |
int i, j; |
314 |
|
|
315 |
|
/* get color table */ |
316 |
|
if ((hp->CMapBits==24 ? fread((char *)(map.c3+hp->mapOrig), |
345 |
|
if (fwritecolrs(scanline, xmax, stdout) < 0) |
346 |
|
quiterr("error writing RADIANCE file"); |
347 |
|
} |
348 |
< |
free((char *)scanline); |
349 |
< |
free((char *)tarData); |
348 |
> |
free((void *)scanline); |
349 |
> |
free((void *)tarData); |
350 |
|
} |
351 |
|
|
352 |
|
|
353 |
< |
getmapped(nc, dith) /* read in and quantize image */ |
354 |
< |
int nc; /* number of colors to use */ |
355 |
< |
int dith; /* use dithering? */ |
353 |
> |
static void |
354 |
> |
getmapped( /* read in and quantize image */ |
355 |
> |
int nc, /* number of colors to use */ |
356 |
> |
int dith /* use dithering? */ |
357 |
> |
) |
358 |
|
{ |
359 |
|
long fpos; |
360 |
< |
register int y; |
360 |
> |
int y; |
361 |
|
|
362 |
|
setcolrgam(gamv); |
363 |
|
fpos = ftell(stdin); |
400 |
|
} |
401 |
|
|
402 |
|
|
403 |
< |
getgrey(nc) /* read in and convert to greyscale image */ |
404 |
< |
int nc; /* number of colors to use */ |
403 |
> |
static void |
404 |
> |
getgrey( /* read in and convert to greyscale image */ |
405 |
> |
int nc /* number of colors to use */ |
406 |
> |
) |
407 |
|
{ |
408 |
|
int y; |
409 |
< |
register BYTE *dp; |
410 |
< |
register int x; |
409 |
> |
uby8 *dp; |
410 |
> |
int x; |
411 |
|
|
412 |
|
setcolrgam(gamv); |
413 |
|
dp = tarData+xmax*ymax;; |
434 |
|
} |
435 |
|
|
436 |
|
|
437 |
< |
writetarga(h, d, fp) /* write out targa data */ |
438 |
< |
struct hdStruct *h; |
439 |
< |
BYTE *d; |
440 |
< |
FILE *fp; |
437 |
> |
static void |
438 |
> |
writetarga( /* write out targa data */ |
439 |
> |
struct hdStruct *h, |
440 |
> |
uby8 *d, |
441 |
> |
FILE *fp |
442 |
> |
) |
443 |
|
{ |
444 |
< |
register int i, j; |
444 |
> |
int i, j; |
445 |
|
|
446 |
|
for (i = 0; i < h->mapLength; i++) /* write color map */ |
447 |
|
for (j = 2; j >= 0; j--) |
448 |
|
putc(clrtab[i][j], fp); |
449 |
|
if (h->dataType == IM_CMAP) { /* uncompressed */ |
450 |
< |
if (fwrite((char *)d,h->x*sizeof(BYTE),h->y,fp) != h->y) |
450 |
> |
if (fwrite((char *)d,h->x*sizeof(uby8),h->y,fp) != h->y) |
451 |
|
quiterr("error writing targa file"); |
452 |
|
return; |
453 |
|
} |
455 |
|
} |
456 |
|
|
457 |
|
|
458 |
< |
readtarga(h, data, fp) /* read in targa data */ |
459 |
< |
struct hdStruct *h; |
460 |
< |
BYTE *data; |
461 |
< |
FILE *fp; |
458 |
> |
static void |
459 |
> |
readtarga( /* read in targa data */ |
460 |
> |
struct hdStruct *h, |
461 |
> |
uby8 *data, |
462 |
> |
FILE *fp |
463 |
> |
) |
464 |
|
{ |
465 |
< |
register int cnt, c; |
466 |
< |
register BYTE *dp; |
465 |
> |
int cnt, c; |
466 |
> |
uby8 *dp; |
467 |
|
|
468 |
|
if (h->dataType == IM_CMAP) { /* uncompressed */ |
469 |
< |
if (fread((char *)data,h->x*sizeof(BYTE),h->y,fp) != h->y) |
469 |
> |
if (fread((char *)data,h->x*sizeof(uby8),h->y,fp) != h->y) |
470 |
|
goto readerr; |
471 |
|
return; |
472 |
|
} |