g4_table_gen.c

Fri, 14 Mar 2003 08:24:37 +0000

author
eric
date
Fri, 14 Mar 2003 08:24:37 +0000
changeset 131
4b8c80d77f76
parent 125
e2ef1c2f9eca
permissions
-rw-r--r--

finished implementing page labels.

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