forked from brouhaha/tumble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitblt_table_gen.c
171 lines (151 loc) · 3.37 KB
/
bitblt_table_gen.c
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
/*
* tumble: build a PDF file from image files
*
* bitblt table generator
* Copyright 2003, 2017 Eric Smith <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. Note that permission is
* not granted to redistribute this program under the terms of any
* other version of the General Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void gen_bit_reverse_table (bool header)
{
int i, j;
if (header)
printf ("extern ");
printf ("const uint8_t bit_reverse_byte [0x100]");
if (header)
{
printf (";\n");
return;
}
printf (" =\n");
printf ("{\n");
for (i = 0; i < 0x100; i++)
{
if ((i & 7) == 0)
printf (" ");
j = (((i & 0x01) << 7) |
((i & 0x02) << 5) |
((i & 0x04) << 3) |
((i & 0x08) << 1) |
((i & 0x10) >> 1) |
((i & 0x20) >> 3) |
((i & 0x40) >> 5) |
((i & 0x80) >> 7));
printf ("0x%02x", j);
if (i != 0xff)
printf (",");
if ((i & 7) == 7)
printf ("\n");
else
printf (" ");
}
printf ("};\n");
}
int count_run (int byte, int start_bit, int desired_val)
{
int count = 0;
int i;
#ifdef WORDS_BIGENDIAN
for (i = 7 - start_bit; i >= 0; i--)
{
int bit = (byte >> i) & 1;
if (bit == desired_val)
count++;
else
break;
}
#else
for (i = start_bit; i < 8; i++)
{
int bit = (byte >> i) & 1;
if (bit == desired_val)
count++;
else
break;
}
#endif
return (count);
}
void gen_run_length_table (bool header, int val, char *name)
{
int i, j;
if (header)
printf ("extern ");
printf ("const uint8_t %s [8][256]", name);
if (header)
{
printf (";\n");
return;
}
printf (" =\n");
printf ("{\n");
for (i = 0; i < 8; i++)
{
printf (" {\n");
for (j = 0; j < 256; j++)
{
if ((j & 15) == 0)
printf (" ");
printf ("%d", count_run (j, i, val));
if (j != 0xff)
printf (",");
if ((j & 15) == 15)
printf ("\n");
else
printf (" ");
}
printf (" }");
if (i != 7)
printf (",");
printf ("\n");
}
printf ("};\n");
}
int main (int argc, char *argv[])
{
bool header;
if (argc != 2)
{
fprintf (stderr, "wrong arg count\n");
exit (2);
}
if (strcmp (argv [1], "-h") == 0)
header = 1;
else if (strcmp (argv [1], "-c") == 0)
header = 0;
else
{
fprintf (stderr, "wrong args\n");
exit (2);
}
printf ("/* This file is automatically generated; do not edit */\n");
printf ("\n");
if (! header)
{
printf ("#include <stdint.h>\n");
printf ("#include \"bitblt_tables.h\"\n");
printf ("\n");
}
gen_bit_reverse_table (header);
printf ("\n");
gen_run_length_table (header, 0, "rle_tab");
printf ("\n");
return (0);
}