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.

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