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. |
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" |
17 |
– |
|
20 |
|
#include "resolu.h" |
21 |
< |
|
21 |
> |
#include "clrtab.h" |
22 |
|
#include "targa.h" |
23 |
|
|
22 |
– |
#ifdef MSDOS |
23 |
– |
#include <fcntl.h> |
24 |
– |
#endif |
24 |
|
|
26 |
– |
#include <math.h> |
27 |
– |
|
28 |
– |
#ifndef BSD |
29 |
– |
#define bcopy(s,d,n) (void)memcpy(d,s,n) |
30 |
– |
extern char *memcpy(); |
31 |
– |
#endif |
32 |
– |
|
25 |
|
#define goodpic(h) (my_imType(h) && my_mapType(h)) |
26 |
|
#define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \ |
27 |
|
&& (h)->dataBits==8 && (h)->imType==0) |
28 |
|
#define my_mapType(h) ((h)->mapType==CM_HASMAP && \ |
29 |
|
((h)->CMapBits==24 || (h)->CMapBits==32)) |
30 |
|
|
31 |
< |
#define taralloc(h) (BYTE *)emalloc((h)->x*(h)->y) |
31 |
> |
#define taralloc(h) (uby8 *)emalloc((h)->x*(h)->y) |
32 |
|
|
33 |
< |
extern BYTE clrtab[][3]; |
34 |
< |
|
43 |
< |
extern char *ecalloc(), *emalloc(); |
44 |
< |
|
45 |
< |
extern long ftell(); |
46 |
< |
|
33 |
> |
uby8 clrtab[256][3]; |
34 |
> |
extern int samplefac; |
35 |
|
double gamv = 2.2; /* gamv correction */ |
48 |
– |
|
36 |
|
int bradj = 0; /* brightness adjustment */ |
50 |
– |
|
37 |
|
char *progname; |
52 |
– |
|
38 |
|
char errmsg[128]; |
54 |
– |
|
39 |
|
COLR *inl; |
40 |
< |
|
57 |
< |
BYTE *tarData; |
58 |
< |
|
40 |
> |
uby8 *tarData; |
41 |
|
int xmax, ymax; |
42 |
|
|
43 |
+ |
static int getint2(FILE *fp); |
44 |
+ |
static void putint2(int i, FILE *fp); |
45 |
+ |
static void quiterr(char *err); |
46 |
+ |
static int getthead(struct hdStruct *hp, char *ip, FILE *fp); |
47 |
+ |
static int putthead(struct hdStruct *hp, char *ip, FILE *fp); |
48 |
+ |
static int getrhead(struct hdStruct *h, FILE *fp); |
49 |
+ |
static void tg2ra(struct hdStruct *hp); |
50 |
+ |
static void getmapped(int nc, int dith); |
51 |
+ |
static void getgrey(int nc); |
52 |
+ |
static void writetarga(struct hdStruct *h, uby8 *d, FILE *fp); |
53 |
+ |
static void readtarga(struct hdStruct *h, uby8 *data, FILE *fp); |
54 |
|
|
55 |
< |
main(argc, argv) |
56 |
< |
int argc; |
57 |
< |
char *argv[]; |
55 |
> |
|
56 |
> |
int |
57 |
> |
main( |
58 |
> |
int argc, |
59 |
> |
char *argv[] |
60 |
> |
) |
61 |
|
{ |
62 |
|
struct hdStruct head; |
63 |
|
int dither = 1; |
65 |
|
int ncolors = 256; |
66 |
|
int greyscale = 0; |
67 |
|
int i; |
68 |
< |
#ifdef MSDOS |
69 |
< |
extern int _fmode; |
70 |
< |
_fmode = O_BINARY; |
75 |
< |
setmode(fileno(stdin), O_BINARY); |
76 |
< |
setmode(fileno(stdout), O_BINARY); |
77 |
< |
#endif |
68 |
> |
SET_DEFAULT_BINARY(); |
69 |
> |
SET_FILE_BINARY(stdin); |
70 |
> |
SET_FILE_BINARY(stdout); |
71 |
|
progname = argv[0]; |
72 |
+ |
samplefac = 0; |
73 |
|
|
74 |
|
for (i = 1; i < argc; i++) |
75 |
|
if (argv[i][0] == '-') |
94 |
|
case 'c': |
95 |
|
ncolors = atoi(argv[++i]); |
96 |
|
break; |
97 |
+ |
case 'n': |
98 |
+ |
samplefac = atoi(argv[++i]); |
99 |
+ |
break; |
100 |
|
default: |
101 |
|
goto userr; |
102 |
|
} |
145 |
|
quiterr(NULL); |
146 |
|
userr: |
147 |
|
fprintf(stderr, |
148 |
< |
"Usage: %s [-d][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n", |
148 |
> |
"Usage: %s [-d][-n samp][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n", |
149 |
|
progname); |
150 |
|
fprintf(stderr, " Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n", |
151 |
|
progname); |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
< |
int |
157 |
< |
getint2(fp) /* get a 2-byte positive integer */ |
158 |
< |
register FILE *fp; |
156 |
> |
static int |
157 |
> |
getint2( /* get a 2-byte positive integer */ |
158 |
> |
register FILE *fp |
159 |
> |
) |
160 |
|
{ |
161 |
|
register int b1, b2; |
162 |
|
|
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
< |
putint2(i, fp) /* put a 2-byte positive integer */ |
171 |
< |
register int i; |
172 |
< |
register FILE *fp; |
170 |
> |
static void |
171 |
> |
putint2( /* put a 2-byte positive integer */ |
172 |
> |
register int i, |
173 |
> |
register FILE *fp |
174 |
> |
) |
175 |
|
{ |
176 |
|
putc(i&0xff, fp); |
177 |
|
putc(i>>8&0xff, fp); |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
< |
quiterr(err) /* print message and exit */ |
182 |
< |
char *err; |
181 |
> |
static void |
182 |
> |
quiterr( /* print message and exit */ |
183 |
> |
char *err |
184 |
> |
) |
185 |
|
{ |
186 |
|
if (err != NULL) { |
187 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
+ |
void |
195 |
|
eputs(s) |
196 |
|
char *s; |
197 |
|
{ |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
+ |
void |
203 |
|
quit(code) |
204 |
|
int code; |
205 |
|
{ |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
< |
getthead(hp, ip, fp) /* read header from input */ |
211 |
< |
struct hdStruct *hp; |
212 |
< |
char *ip; |
213 |
< |
register FILE *fp; |
210 |
> |
static int |
211 |
> |
getthead( /* read header from input */ |
212 |
> |
struct hdStruct *hp, |
213 |
> |
char *ip, |
214 |
> |
register FILE *fp |
215 |
> |
) |
216 |
|
{ |
217 |
|
int nidbytes; |
218 |
|
|
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
< |
putthead(hp, ip, fp) /* write header to output */ |
246 |
< |
struct hdStruct *hp; |
247 |
< |
char *ip; |
248 |
< |
register FILE *fp; |
245 |
> |
static int |
246 |
> |
putthead( /* write header to output */ |
247 |
> |
struct hdStruct *hp, |
248 |
> |
char *ip, |
249 |
> |
register FILE *fp |
250 |
> |
) |
251 |
|
{ |
252 |
|
if (ip != NULL) |
253 |
|
putc(strlen(ip), fp); |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
< |
getrhead(h, fp) /* load RADIANCE input file header */ |
276 |
< |
register struct hdStruct *h; |
277 |
< |
FILE *fp; |
275 |
> |
static int |
276 |
> |
getrhead( /* load RADIANCE input file header */ |
277 |
> |
register struct hdStruct *h, |
278 |
> |
FILE *fp |
279 |
> |
) |
280 |
|
{ |
281 |
|
/* get header info. */ |
282 |
|
if (checkheader(fp, COLRFMT, NULL) < 0 || |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
< |
tg2ra(hp) /* targa file to RADIANCE file */ |
308 |
< |
struct hdStruct *hp; |
307 |
> |
static void |
308 |
> |
tg2ra( /* targa file to RADIANCE file */ |
309 |
> |
struct hdStruct *hp |
310 |
> |
) |
311 |
|
{ |
312 |
|
union { |
313 |
< |
BYTE c3[256][3]; |
314 |
< |
BYTE c4[256][4]; |
313 |
> |
uby8 c3[256][3]; |
314 |
> |
uby8 c4[256][4]; |
315 |
|
} map; |
316 |
|
COLR ctab[256]; |
317 |
|
COLR *scanline; |
350 |
|
if (fwritecolrs(scanline, xmax, stdout) < 0) |
351 |
|
quiterr("error writing RADIANCE file"); |
352 |
|
} |
353 |
< |
free((char *)scanline); |
354 |
< |
free((char *)tarData); |
353 |
> |
free((void *)scanline); |
354 |
> |
free((void *)tarData); |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
< |
getmapped(nc, dith) /* read in and quantize image */ |
359 |
< |
int nc; /* number of colors to use */ |
360 |
< |
int dith; /* use dithering? */ |
358 |
> |
static void |
359 |
> |
getmapped( /* read in and quantize image */ |
360 |
> |
int nc, /* number of colors to use */ |
361 |
> |
int dith /* use dithering? */ |
362 |
> |
) |
363 |
|
{ |
364 |
|
long fpos; |
365 |
|
register int y; |
366 |
|
|
367 |
|
setcolrgam(gamv); |
368 |
|
fpos = ftell(stdin); |
369 |
< |
new_histo(); /* build histogram */ |
369 |
> |
if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1) |
370 |
> |
quiterr("cannot initialized histogram"); |
371 |
|
for (y = ymax-1; y >= 0; y--) { |
372 |
|
if (freadcolrs(inl, xmax, stdin) < 0) |
373 |
|
quiterr("error reading Radiance input"); |
374 |
|
if (bradj) |
375 |
|
shiftcolrs(inl, xmax, bradj); |
376 |
|
colrs_gambs(inl, xmax); |
377 |
< |
cnt_colrs(inl, xmax); |
377 |
> |
if (samplefac) |
378 |
> |
neu_colrs(inl, xmax); |
379 |
> |
else |
380 |
> |
cnt_colrs(inl, xmax); |
381 |
|
} |
382 |
|
if (fseek(stdin, fpos, 0) == EOF) |
383 |
|
quiterr("Radiance input must be from a file"); |
384 |
< |
new_clrtab(nc); /* map colors */ |
384 |
> |
if (samplefac) /* map colors */ |
385 |
> |
neu_clrtab(nc); |
386 |
> |
else |
387 |
> |
new_clrtab(nc); |
388 |
|
for (y = ymax-1; y >= 0; y--) { |
389 |
|
if (freadcolrs(inl, xmax, stdin) < 0) |
390 |
|
quiterr("error reading Radiance input"); |
391 |
|
if (bradj) |
392 |
|
shiftcolrs(inl, xmax, bradj); |
393 |
|
colrs_gambs(inl, xmax); |
394 |
< |
if (dith) |
395 |
< |
dith_colrs(tarData+y*xmax, inl, xmax); |
394 |
> |
if (samplefac) |
395 |
> |
if (dith) |
396 |
> |
neu_dith_colrs(tarData+y*xmax, inl, xmax); |
397 |
> |
else |
398 |
> |
neu_map_colrs(tarData+y*xmax, inl, xmax); |
399 |
|
else |
400 |
< |
map_colrs(tarData+y*xmax, inl, xmax); |
400 |
> |
if (dith) |
401 |
> |
dith_colrs(tarData+y*xmax, inl, xmax); |
402 |
> |
else |
403 |
> |
map_colrs(tarData+y*xmax, inl, xmax); |
404 |
|
} |
405 |
|
} |
406 |
|
|
407 |
|
|
408 |
< |
getgrey(nc) /* read in and convert to greyscale image */ |
409 |
< |
int nc; /* number of colors to use */ |
408 |
> |
static void |
409 |
> |
getgrey( /* read in and convert to greyscale image */ |
410 |
> |
int nc /* number of colors to use */ |
411 |
> |
) |
412 |
|
{ |
413 |
|
int y; |
414 |
< |
register BYTE *dp; |
414 |
> |
register uby8 *dp; |
415 |
|
register int x; |
416 |
|
|
417 |
|
setcolrgam(gamv); |
439 |
|
} |
440 |
|
|
441 |
|
|
442 |
< |
writetarga(h, d, fp) /* write out targa data */ |
443 |
< |
struct hdStruct *h; |
444 |
< |
BYTE *d; |
445 |
< |
FILE *fp; |
442 |
> |
static void |
443 |
> |
writetarga( /* write out targa data */ |
444 |
> |
struct hdStruct *h, |
445 |
> |
uby8 *d, |
446 |
> |
FILE *fp |
447 |
> |
) |
448 |
|
{ |
449 |
|
register int i, j; |
450 |
|
|
452 |
|
for (j = 2; j >= 0; j--) |
453 |
|
putc(clrtab[i][j], fp); |
454 |
|
if (h->dataType == IM_CMAP) { /* uncompressed */ |
455 |
< |
if (fwrite((char *)d,h->x*sizeof(BYTE),h->y,fp) != h->y) |
455 |
> |
if (fwrite((char *)d,h->x*sizeof(uby8),h->y,fp) != h->y) |
456 |
|
quiterr("error writing targa file"); |
457 |
|
return; |
458 |
|
} |
460 |
|
} |
461 |
|
|
462 |
|
|
463 |
< |
readtarga(h, data, fp) /* read in targa data */ |
464 |
< |
struct hdStruct *h; |
465 |
< |
BYTE *data; |
466 |
< |
FILE *fp; |
463 |
> |
static void |
464 |
> |
readtarga( /* read in targa data */ |
465 |
> |
struct hdStruct *h, |
466 |
> |
uby8 *data, |
467 |
> |
FILE *fp |
468 |
> |
) |
469 |
|
{ |
470 |
|
register int cnt, c; |
471 |
< |
register BYTE *dp; |
471 |
> |
register uby8 *dp; |
472 |
|
|
473 |
|
if (h->dataType == IM_CMAP) { /* uncompressed */ |
474 |
< |
if (fread((char *)data,h->x*sizeof(BYTE),h->y,fp) != h->y) |
474 |
> |
if (fread((char *)data,h->x*sizeof(uby8),h->y,fp) != h->y) |
475 |
|
goto readerr; |
476 |
|
return; |
477 |
|
} |