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