Sat, 22 Feb 2003 10:02:06 +0000
Generate G4 tables from strings of ones and zeros.
1 /*
2 * t2p: Create a PDF file from the contents of one or more TIFF
3 * bilevel image files. The images in the resulting PDF file
4 * will be compressed using ITU-T T.6 (G4) fax encoding.
5 *
6 * G4 table generator
7 * $Id: g4_table_gen.c,v 1.1 2003/02/22 02:02:06 eric Exp $
8 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation. Note that permission is
13 * not granted to redistribute this program under the terms of any
14 * other version of the General Public License.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
24 */
27 #include <stdbool.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
34 void emit_code (int indent, char *code, int last, bool comment, int cval)
35 {
36 int i;
37 int count = 0;
38 uint32_t val = 0;
40 printf ("%*s{ ", indent, "");
42 printf ("%d, ", strlen (code));
44 for (i = 0; i < strlen (code); i++)
45 switch (code [i])
46 {
47 case '0': val = (val << 1); count++; break;
48 case '1': val = (val << 1) + 1; count++; break;
49 case ' ': break;
50 default:
51 fprintf (stderr, "internal error\n");
52 exit (2);
53 }
55 printf ("0x%0*x", (count + 3)/4, val);
57 printf (" }");
58 if (! last)
59 printf (",");
60 if (comment)
61 printf (" /* %d */", cval);
62 printf ("\n");
63 }
66 char *long_makeup_code [12] =
67 {
68 /* 1792 */ "00000001000",
69 /* 1856 */ "00000001100",
70 /* 1920 */ "00000001101",
71 /* 1984 */ "000000010010",
72 /* 2048 */ "000000010011",
73 /* 2112 */ "000000010100",
74 /* 2176 */ "000000010101",
75 /* 2240 */ "000000010110",
76 /* 2304 */ "000000010111",
77 /* 2368 */ "000000011100",
78 /* 2432 */ "000000011101",
79 /* 2496 */ "000000011110"
80 /* 2560 "000000011111" hard-coded, doesn't need to be in table */
81 };
84 void print_long_makeup_code (void)
85 {
86 int i;
88 printf ("static g4_bits g4_long_makeup_code [12] =\n");
89 printf (" {\n");
90 for (i = 0; i < 12; i++)
91 emit_code (4, long_makeup_code [i], i == 11, 1, i * 64 + 1792);
92 printf (" };\n");
93 }
96 char *makeup_code [64][2] =
97 {
98 { /* 64 */ "11011", "0000001111" },
99 { /* 128 */ "10010", "000011001000" },
100 { /* 192 */ "010111", "000011001001" },
101 { /* 256 */ "0110111", "000001011011" },
102 { /* 320 */ "00110110", "000000110011" },
103 { /* 384 */ "00110111", "000000110100" },
104 { /* 448 */ "01100100", "000000110101" },
105 { /* 512 */ "01100101", "0000001101100" },
106 { /* 576 */ "01101000", "0000001101101" },
107 { /* 640 */ "01100111", "0000001001010" },
108 { /* 704 */ "011001100", "0000001001011" },
109 { /* 768 */ "011001101", "0000001001100" },
110 { /* 832 */ "011010010", "0000001001101" },
111 { /* 896 */ "011010011", "0000001110010" },
112 { /* 960 */ "011010100", "0000001110011" },
113 { /* 1024 */ "011010101", "0000001110100" },
114 { /* 1088 */ "011010110", "0000001110101" },
115 { /* 1152 */ "011010111", "0000001110110" },
116 { /* 1216 */ "011011000", "0000001110111" },
117 { /* 1280 */ "011011001", "0000001010010" },
118 { /* 1344 */ "011011010", "0000001010011" },
119 { /* 1408 */ "011011011", "0000001010100" },
120 { /* 1472 */ "010011000", "0000001010101" },
121 { /* 1536 */ "010011001", "0000001011010" },
122 { /* 1600 */ "010011010", "0000001011011" },
123 { /* 1664 */ "011000", "0000001100100" },
124 { /* 1728 */ "010011011", "0000001100101" }
125 };
128 void print_makeup_code (void)
129 {
130 int i;
132 printf ("static g4_bits g4_makeup_code [2] [27] =\n");
133 printf (" {\n");
134 printf (" {\n");
135 printf (" /* white */\n");
136 for (i = 0; i <= 26; i++)
137 emit_code (6, makeup_code [i][0], i == 26, 1, (i + 1) * 64);
138 printf (" },\n");
139 printf (" {\n");
140 printf (" /* black */\n");
141 for (i = 0; i <= 26; i++)
142 emit_code (6, makeup_code [i][1], i == 26, 1, (i + 1) * 64);
143 printf (" }\n");
144 printf (" };\n");
145 }
148 char *h_code [64][2] =
149 {
150 { /* 0 */ "00110101", "0000110111" },
151 { /* 1 */ "000111", "010" },
152 { /* 2 */ "0111", "11" },
153 { /* 3 */ "1000", "10" },
154 { /* 4 */ "1011", "011" },
155 { /* 5 */ "1100", "0011" },
156 { /* 6 */ "1110", "0010" },
157 { /* 7 */ "1111", "00011" },
158 { /* 8 */ "10011", "000101" },
159 { /* 9 */ "10100", "000100" },
160 { /* 10 */ "00111", "0000100" },
161 { /* 11 */ "01000", "0000101" },
162 { /* 12 */ "001000", "0000111" },
163 { /* 13 */ "000011", "00000100" },
164 { /* 14 */ "110100", "00000111" },
165 { /* 15 */ "110101", "000011000" },
166 { /* 16 */ "101010", "0000010111" },
167 { /* 17 */ "101011", "0000011000" },
168 { /* 18 */ "0100111", "0000001000" },
169 { /* 19 */ "0001100", "00001100111" },
170 { /* 20 */ "0001000", "00001101000" },
171 { /* 21 */ "0010111", "00001101100" },
172 { /* 22 */ "0000011", "00000110111" },
173 { /* 23 */ "0000100", "00000101000" },
174 { /* 24 */ "0101000", "00000010111" },
175 { /* 25 */ "0101011", "00000011000" },
176 { /* 26 */ "0010011", "000011001010" },
177 { /* 27 */ "0100100", "000011001011" },
178 { /* 28 */ "0011000", "000011001100" },
179 { /* 29 */ "00000010", "000011001101" },
180 { /* 30 */ "00000011", "000001101000" },
181 { /* 31 */ "00011010", "000001101001" },
182 { /* 32 */ "00011011", "000001101010" },
183 { /* 33 */ "00010010", "000001101011" },
184 { /* 34 */ "00010011", "000011010010" },
185 { /* 35 */ "00010100", "000011010011" },
186 { /* 36 */ "00010101", "000011010100" },
187 { /* 37 */ "00010110", "000011010101" },
188 { /* 38 */ "00010111", "000011010110" },
189 { /* 39 */ "00101000", "000011010111" },
190 { /* 40 */ "00101001", "000001101100" },
191 { /* 41 */ "00101010", "000001101101" },
192 { /* 42 */ "00101011", "000011011010" },
193 { /* 43 */ "00101100", "000011011011" },
194 { /* 44 */ "00101101", "000001010100" },
195 { /* 45 */ "00000100", "000001010101" },
196 { /* 46 */ "00000101", "000001010110" },
197 { /* 47 */ "00001010", "000001010111" },
198 { /* 48 */ "00001011", "000001100100" },
199 { /* 49 */ "01010010", "000001100101" },
200 { /* 50 */ "01010011", "000001010010" },
201 { /* 51 */ "01010100", "000001010011" },
202 { /* 52 */ "01010101", "000000100100" },
203 { /* 53 */ "00100100", "000000110111" },
204 { /* 54 */ "00100101", "000000111000" },
205 { /* 55 */ "01011000", "000000100111" },
206 { /* 56 */ "01011001", "000000101000" },
207 { /* 57 */ "01011010", "000001011000" },
208 { /* 58 */ "01011011", "000001011001" },
209 { /* 59 */ "01001010", "000000101011" },
210 { /* 60 */ "01001011", "000000101100" },
211 { /* 61 */ "00110010", "000001011010" },
212 { /* 62 */ "00110011", "000001100110" },
213 { /* 63 */ "00110100", "000001100111" }
214 };
217 void print_h_code (void)
218 {
219 int i;
221 printf ("static g4_bits g4_h_code [2] [64] =\n");
222 printf (" {\n");
223 printf (" {\n");
224 printf (" /* white */\n");
225 for (i = 0; i <= 63; i++)
226 emit_code (6, h_code [i][0], i == 63, 1, i);
227 printf (" },\n");
228 printf (" {\n");
229 printf (" /* black */\n");
230 for (i = 0; i <= 63; i++)
231 emit_code (6, h_code [i][1], i == 63, 1, i);
232 printf (" }\n");
233 printf (" };\n");
234 }
237 char *v_code [7] =
238 {
239 /* -3 */ "0000010",
240 /* -2 */ "000010",
241 /* -1 */ "010",
242 /* 0 */ "1",
243 /* 1 */ "011",
244 /* 2 */ "000011",
245 /* 3 */ "0000011"
246 };
249 void print_v_code (void)
250 {
251 int i;
253 printf ("static g4_bits g4_vert_code [7] =\n");
254 printf (" {\n");
255 for (i = 0; i <= 6; i++)
256 emit_code (4, v_code [i], i == 6, 1, i - 3);
257 printf (" };\n");
258 }
261 int main (int argc, char *argv [])
262 {
263 printf ("/* This file is automatically generated; do not edit */\n");
264 printf ("\n");
265 printf ("typedef struct\n");
266 printf ("{\n");
267 printf (" uint32_t count;\n");
268 printf (" uint32_t bits;\n");
269 printf ("} g4_bits;\n");
270 printf ("\n");
272 print_long_makeup_code ();
273 printf ("\n");
275 print_makeup_code ();
276 printf ("\n");
278 print_h_code ();
279 printf ("\n");
281 print_v_code ();
283 exit (0);
284 }