1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: ra_hexbit.c,v 3.2 2003/02/22 02:07:27 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Create a 4x1 hex bitmap from a Radiance picture. |
6 |
*/ |
7 |
|
8 |
#include <stdio.h> |
9 |
#include <time.h> |
10 |
|
11 |
#include "color.h" |
12 |
#include "resolu.h" |
13 |
|
14 |
char *progname; |
15 |
int xmax, ymax; |
16 |
double thresh = 0.5; /* threshold value */ |
17 |
COLR threshclr; |
18 |
|
19 |
#define abovethresh(c) ((c)[EXP]>threshclr[EXP] || \ |
20 |
((c)[EXP]==threshclr[EXP] && (c)[GRN]>threshclr[GRN])) |
21 |
|
22 |
static void quiterr(char *err); |
23 |
static void ra2hex(void); |
24 |
|
25 |
|
26 |
int |
27 |
main( |
28 |
int argc, |
29 |
char *argv[] |
30 |
) |
31 |
{ |
32 |
int i; |
33 |
|
34 |
progname = argv[0]; |
35 |
|
36 |
for (i = 1; i < argc; i++) |
37 |
if (argv[i][0] == '-') |
38 |
switch (argv[i][1]) { |
39 |
case 't': /* threshold value */ |
40 |
thresh = atof(argv[++i]); |
41 |
break; |
42 |
default: |
43 |
goto userr; |
44 |
} |
45 |
else |
46 |
break; |
47 |
|
48 |
if (i < argc-2) |
49 |
goto userr; |
50 |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
51 |
fprintf(stderr, "%s: can't open input \"%s\"\n", |
52 |
progname, argv[i]); |
53 |
exit(1); |
54 |
} |
55 |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
56 |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
57 |
progname, argv[i+1]); |
58 |
exit(1); |
59 |
} |
60 |
/* assign threshold color */ |
61 |
setcolr(threshclr, thresh, thresh, thresh); |
62 |
/* get our header */ |
63 |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
64 |
fgetresolu(&xmax, &ymax, stdin) < 0) |
65 |
quiterr("bad picture format"); |
66 |
/* convert file */ |
67 |
ra2hex(); |
68 |
exit(0); |
69 |
userr: |
70 |
fprintf(stderr, |
71 |
"Usage: %s [-t thresh] [input [output]]\n", progname); |
72 |
exit(1); |
73 |
} |
74 |
|
75 |
|
76 |
static void |
77 |
quiterr( /* print message and exit */ |
78 |
char *err |
79 |
) |
80 |
{ |
81 |
if (err != NULL) { |
82 |
fprintf(stderr, "%s: %s\n", progname, err); |
83 |
exit(1); |
84 |
} |
85 |
exit(0); |
86 |
} |
87 |
|
88 |
|
89 |
static void |
90 |
ra2hex(void) /* convert Radiance scanlines to 4x1 bit hex */ |
91 |
{ |
92 |
static char cmap[] = "0123456789ABCDEF"; |
93 |
COLR *scanin; |
94 |
register int x, c, t; |
95 |
int y; |
96 |
/* allocate scanline */ |
97 |
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
98 |
if (scanin == NULL) |
99 |
quiterr("out of memory in ra2skel"); |
100 |
/* convert image */ |
101 |
for (y = ymax-1; y >= 0; y--) { |
102 |
if (freadcolrs(scanin, xmax, stdin) < 0) |
103 |
quiterr("error reading Radiance picture"); |
104 |
c = 0; |
105 |
for (x = 0; x < xmax; x++) |
106 |
if ((t = 03 - (x&03))) |
107 |
c |= abovethresh(scanin[x]) << t; |
108 |
else { |
109 |
c |= abovethresh(scanin[x]); |
110 |
putchar(cmap[c]); |
111 |
c = 0; |
112 |
} |
113 |
if (t) |
114 |
fputc(cmap[c], stdout); |
115 |
fputc('\n', stdout); |
116 |
if (ferror(stdout)) |
117 |
quiterr("error writing hex bit file"); |
118 |
} |
119 |
/* free scanline */ |
120 |
free((void *)scanin); |
121 |
} |