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