1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#include <stdio.h>
/*
* RAS2BIN -- Convert a hex encoded raster to binary.
*
* Usage: ras2bin -encoding encoding -nx nx -ny ny infile outfile
*
* encoding hex1, hex2, hex1-rle, hex2-rle
* nx, ny raster size in pixels
* input input hex encoded, possible RLE (run length) encoded
* block of text. whitespace is ignored otherwise the file
* should contain only data.
* output output file to receive the binary array of pixels
*
* Perhaps this ought to take ras2text output and generate a Sun rasterfile,
* but currently we just decode the pixel data and output it in binary.
*/
main (argc, argv)
int argc;
char **argv;
{
char *infile=NULL, *outfile=NULL;
char pixels[102400];
int in, out, nx, ny, bias;
char *encoding;
int i;
for (i=1; i < argc; i++)
if (argv[i][0] == '-') {
if (strncmp (argv[i], "-en", 3) == 0)
encoding = argv[++i];
else if (strcmp (argv[i], "-nx") == 0)
nx = atoi (argv[++i]);
else if (strcmp (argv[i], "-ny") == 0)
ny = atoi (argv[++i]);
else if (strcmp (argv[i], "-bias") == 0)
bias = atoi (argv[++i]);
} else if (!infile) {
infile = argv[i];
} else if (!outfile)
outfile = argv[i];
if (!infile || !outfile || nx <= 0 || ny <= 0) {
fprintf (stderr, "bad usage\n");
exit (1);
}
printf ("input = %s\n", infile);
if ((in = open (infile, 0)) < 0) {
fprintf (stderr, "cannot open %s\n", infile);
exit (2);
}
printf ("read %d chars from input\n", read (in, pixels, 102400));
close (in);
printf ("output = %s\n", outfile);
if ((out = creat (outfile, 0644)) < 0) {
fprintf (stderr, "cannot open %s\n", outfile);
exit (3);
}
printRaster (out, pixels, encoding, nx, ny, bias);
close (out);
}
printRaster (out, pixels, encoding, nx, ny, bias)
int out;
char *pixels;
char *encoding;
int nx, ny;
int bias;
{
register char *ip;
register unsigned char *op;
register int v, i, j;
static unsigned char hex1[256], hex2[256];
static int have_tables = 0;
unsigned char *data, *otop;
char *sv_pixels = pixels;
/* Generate hex to binary lookup tables in first call. */
if (!have_tables) {
/* Generate char-to-binary table for the hex1 encoding. */
for (i=0; i < 256; i++)
hex1[i] = 0177;
for (i='0'; i <= '9'; i++)
hex1[i] = i - '0';
for (i='A'; i <= 'Z'; i++)
hex1[i] = i - 'A' + 10;
for (i='a'; i <= 'z'; i++)
hex1[i] = i - 'a' + 36;
hex1['$'] = 62;
hex1['_'] = 63;
/* Generate char-to-binary table for the hex2 encoding. */
for (i=0; i < 256; i++)
hex2[i] = 0177;
for (i='0'; i <= '9'; i++)
hex2[i] = i - '0';
for (i='a'; i <= 'f'; i++)
hex2[i] = i - 'a' + 10;
for (i='A'; i <= 'F'; i++)
hex2[i] = i - 'A' + 10;
have_tables++;
}
/* Decode the pixel data. */
if (!(data = (unsigned char *) malloc (nx * ny)))
return (-1);
otop = data + nx * ny;
/* Uncompress the input if RLE compression is indicated. */
if (strcmp (&encoding[strlen(encoding)-4], "-rle") == 0) {
int buflen = nx * ny * 2;
char *ibuf, *op;
int ch;
/* Get buffer to hold the uncompressed pixel data array. */
if (!(ibuf = (char *) malloc (buflen + 1)))
goto err;
/* Uncompress the pixel array. */
for (ip=pixels, op=ibuf; *ip; ) {
while (isspace (*ip))
ip++;
if ((ch = *ip++) == '@') {
if ((i = hex1[*ip++]) >= 0x7f)
while (*ip && ((i = hex1[*ip++]) >= 0x7f))
;
if (op-ibuf + i + 1 > buflen)
goto err;
for (v = *(op-1), i++; --i >= 0; )
*op++ = v;
} else if (ch == '%') {
if ((i = hex2[*ip++]) >= 0x7f)
while (*ip && ((i = hex2[*ip++]) >= 0x7f))
;
if ((j = hex2[*ip++]) >= 0x7f)
while (*ip && ((j = hex2[*ip++]) >= 0x7f))
;
i = ((i << 4) | j) + 1;
if (op-ibuf + i > buflen)
goto err;
for (v = *(op-1); --i >= 0; )
*op++ = v;
} else
*op++ = ch;
}
*op = '\0';
pixels = ibuf;
}
/* Convert the ascii pixels array to a binary data array.
*/
if (strcmp (encoding, "numeric") == 0) {
while (isspace (*ip))
ip++;
for (ip=pixels, op=data; *ip && op < otop; ) {
for (v=0; isdigit(*ip); )
v = v * 10 + *ip++ - '0';
*op++ = v + bias;
while (isspace (*ip))
ip++;
}
} else if (strncmp (encoding, "hex1", 4) == 0) {
for (ip=pixels, op=data; *ip && op < otop; ) {
if ((v = hex1[*ip++]) > 0xf)
while (*ip && ((v = hex1[*ip++]) > 0xf))
;
*op++ = v + bias;
}
} else if (strncmp (encoding, "hex2", 4) == 0) {
for (ip=pixels, op=data; *ip && op < otop; ) {
if ((v = hex2[*ip++]) > 0xf)
while (*ip && ((v = hex2[*ip++]) > 0xf))
;
if ((i = hex2[*ip++]) > 0xf)
while (*ip && ((i = hex2[*ip++]) > 0xf))
;
*op++ = ((v << 4) | i) + bias;
}
} else {
err: free ((char *)data);
if (pixels != sv_pixels)
free (pixels);
return (-1);
}
/* Write the pixels. */
write (out, data, op - data);
free ((char *)data);
if (pixels != sv_pixels)
free (pixels);
return (0);
}
|