1 /*
2 * tumble: build a PDF file from image files
3 *
4 * bitblt table generator
5 * Copyright 2003, 2017 Eric Smith <spacewar@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation. Note that permission is
10 * not granted to redistribute this program under the terms of any
11 * other version of the General Public License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
21 */
22
23
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29
gen_bit_reverse_table(bool header)30 void gen_bit_reverse_table (bool header)
31 {
32 int i, j;
33
34 if (header)
35 printf ("extern ");
36 printf ("const uint8_t bit_reverse_byte [0x100]");
37 if (header)
38 {
39 printf (";\n");
40 return;
41 }
42 printf (" =\n");
43 printf ("{\n");
44 for (i = 0; i < 0x100; i++)
45 {
46 if ((i & 7) == 0)
47 printf (" ");
48 j = (((i & 0x01) << 7) |
49 ((i & 0x02) << 5) |
50 ((i & 0x04) << 3) |
51 ((i & 0x08) << 1) |
52 ((i & 0x10) >> 1) |
53 ((i & 0x20) >> 3) |
54 ((i & 0x40) >> 5) |
55 ((i & 0x80) >> 7));
56 printf ("0x%02x", j);
57 if (i != 0xff)
58 printf (",");
59 if ((i & 7) == 7)
60 printf ("\n");
61 else
62 printf (" ");
63 }
64 printf ("};\n");
65 }
66
67
count_run(int byte,int start_bit,int desired_val)68 int count_run (int byte, int start_bit, int desired_val)
69 {
70 int count = 0;
71 int i;
72
73 #ifdef WORDS_BIGENDIAN
74 for (i = 7 - start_bit; i >= 0; i--)
75 {
76 int bit = (byte >> i) & 1;
77 if (bit == desired_val)
78 count++;
79 else
80 break;
81 }
82 #else
83 for (i = start_bit; i < 8; i++)
84 {
85 int bit = (byte >> i) & 1;
86 if (bit == desired_val)
87 count++;
88 else
89 break;
90 }
91 #endif
92
93 return (count);
94 }
95
96
gen_run_length_table(bool header,int val,char * name)97 void gen_run_length_table (bool header, int val, char *name)
98 {
99 int i, j;
100
101 if (header)
102 printf ("extern ");
103 printf ("const uint8_t %s [8][256]", name);
104 if (header)
105 {
106 printf (";\n");
107 return;
108 }
109 printf (" =\n");
110 printf ("{\n");
111 for (i = 0; i < 8; i++)
112 {
113 printf (" {\n");
114 for (j = 0; j < 256; j++)
115 {
116 if ((j & 15) == 0)
117 printf (" ");
118 printf ("%d", count_run (j, i, val));
119 if (j != 0xff)
120 printf (",");
121 if ((j & 15) == 15)
122 printf ("\n");
123 else
124 printf (" ");
125 }
126 printf (" }");
127 if (i != 7)
128 printf (",");
129 printf ("\n");
130 }
131 printf ("};\n");
132 }
133
134
main(int argc,char * argv[])135 int main (int argc, char *argv[])
136 {
137 bool header;
138
139 if (argc != 2)
140 {
141 fprintf (stderr, "wrong arg count\n");
142 exit (2);
143 }
144 if (strcmp (argv [1], "-h") == 0)
145 header = 1;
146 else if (strcmp (argv [1], "-c") == 0)
147 header = 0;
148 else
149 {
150 fprintf (stderr, "wrong args\n");
151 exit (2);
152 }
153
154 printf ("/* This file is automatically generated; do not edit */\n");
155 printf ("\n");
156
157 if (! header)
158 {
159 printf ("#include <stdint.h>\n");
160 printf ("#include \"bitblt_tables.h\"\n");
161 printf ("\n");
162 }
163
164 gen_bit_reverse_table (header);
165 printf ("\n");
166
167 gen_run_length_table (header, 0, "rle_tab");
168 printf ("\n");
169
170 return (0);
171 }
172