g4_table_gen.c

Wed, 12 Mar 2003 06:57:46 +0000

author
eric
date
Wed, 12 Mar 2003 06:57:46 +0000
changeset 99
26f7edf15ef4
parent 78
74b6b230f85d
child 125
e2ef1c2f9eca
permissions
-rw-r--r--

changed g4_table_gen to generate both a header and a C source file (g4_tables.[ch]).

     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.3 2003/03/11 22:57:46 eric Exp $
     8  * Copyright 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 (bool header)
    85 {
    86   int i;
    88   if (header)
    89     printf ("extern ");
    90   printf ("const g4_bits g4_long_makeup_code [12]");
    91   if (header)
    92     {
    93       printf (";\n");
    94       return;
    95     }
    96   printf (" =\n");
    97   printf ("  {\n");
    98   for (i = 0; i < 12; i++)
    99     emit_code (4, long_makeup_code [i], i == 11, 1, i * 64 + 1792);
   100   printf ("  };\n");
   101 }
   104 char *makeup_code [64][2] =
   105   {
   106     { /*   64 */ "11011",     "0000001111" },
   107     { /*  128 */ "10010",     "000011001000" },
   108     { /*  192 */ "010111",    "000011001001" },
   109     { /*  256 */ "0110111",   "000001011011" },
   110     { /*  320 */ "00110110",  "000000110011" },
   111     { /*  384 */ "00110111",  "000000110100" },
   112     { /*  448 */ "01100100",  "000000110101" },
   113     { /*  512 */ "01100101",  "0000001101100" },
   114     { /*  576 */ "01101000",  "0000001101101" },
   115     { /*  640 */ "01100111",  "0000001001010" },
   116     { /*  704 */ "011001100", "0000001001011" },
   117     { /*  768 */ "011001101", "0000001001100" },
   118     { /*  832 */ "011010010", "0000001001101" },
   119     { /*  896 */ "011010011", "0000001110010" },
   120     { /*  960 */ "011010100", "0000001110011" },
   121     { /* 1024 */ "011010101", "0000001110100" },
   122     { /* 1088 */ "011010110", "0000001110101" },
   123     { /* 1152 */ "011010111", "0000001110110" },
   124     { /* 1216 */ "011011000", "0000001110111" },
   125     { /* 1280 */ "011011001", "0000001010010" },
   126     { /* 1344 */ "011011010", "0000001010011" },
   127     { /* 1408 */ "011011011", "0000001010100" },
   128     { /* 1472 */ "010011000", "0000001010101" },
   129     { /* 1536 */ "010011001", "0000001011010" },
   130     { /* 1600 */ "010011010", "0000001011011" },
   131     { /* 1664 */ "011000",    "0000001100100" },
   132     { /* 1728 */ "010011011", "0000001100101" }
   133   };
   136 void print_makeup_code (bool header)
   137 {
   138   int i;
   140   if (header)
   141     printf ("extern ");
   142   printf ("const g4_bits g4_makeup_code [2] [27]");
   143   if (header)
   144     {
   145       printf (";\n");
   146       return;
   147     }
   148   printf (" =\n");
   149   printf ("  {\n");
   150   printf ("    {\n");
   151   printf ("      /* white */\n");
   152   for (i = 0; i <= 26; i++)
   153     emit_code (6, makeup_code [i][0], i == 26, 1, (i + 1) * 64);
   154   printf ("    },\n");
   155   printf ("    {\n");
   156   printf ("      /* black */\n");
   157   for (i = 0; i <= 26; i++)
   158     emit_code (6, makeup_code [i][1], i == 26, 1, (i + 1) * 64);
   159   printf ("    }\n");
   160   printf ("  };\n");
   161 }
   164 char *h_code [64][2] =
   165   {
   166     { /*  0 */ "00110101", "0000110111" },
   167     { /*  1 */ "000111",   "010" },
   168     { /*  2 */ "0111",     "11" },
   169     { /*  3 */ "1000",     "10" },
   170     { /*  4 */ "1011",     "011" },
   171     { /*  5 */ "1100",     "0011" },
   172     { /*  6 */ "1110",     "0010" },
   173     { /*  7 */ "1111",     "00011" },
   174     { /*  8 */ "10011",    "000101" },
   175     { /*  9 */ "10100",    "000100" },
   176     { /* 10 */ "00111",    "0000100" },
   177     { /* 11 */ "01000",    "0000101" },
   178     { /* 12 */ "001000",   "0000111" },
   179     { /* 13 */ "000011",   "00000100" },
   180     { /* 14 */ "110100",   "00000111" },
   181     { /* 15 */ "110101",   "000011000" },
   182     { /* 16 */ "101010",   "0000010111" },
   183     { /* 17 */ "101011",   "0000011000" },
   184     { /* 18 */ "0100111",  "0000001000" },
   185     { /* 19 */ "0001100",  "00001100111" },
   186     { /* 20 */ "0001000",  "00001101000" },
   187     { /* 21 */ "0010111",  "00001101100" },
   188     { /* 22 */ "0000011",  "00000110111" },
   189     { /* 23 */ "0000100",  "00000101000" },
   190     { /* 24 */ "0101000",  "00000010111" },
   191     { /* 25 */ "0101011",  "00000011000" },
   192     { /* 26 */ "0010011",  "000011001010" },
   193     { /* 27 */ "0100100",  "000011001011" },
   194     { /* 28 */ "0011000",  "000011001100" },
   195     { /* 29 */ "00000010", "000011001101" },
   196     { /* 30 */ "00000011", "000001101000" },
   197     { /* 31 */ "00011010", "000001101001" },
   198     { /* 32 */ "00011011", "000001101010" },
   199     { /* 33 */ "00010010", "000001101011" },
   200     { /* 34 */ "00010011", "000011010010" },
   201     { /* 35 */ "00010100", "000011010011" },
   202     { /* 36 */ "00010101", "000011010100" },
   203     { /* 37 */ "00010110", "000011010101" },
   204     { /* 38 */ "00010111", "000011010110" },
   205     { /* 39 */ "00101000", "000011010111" },
   206     { /* 40 */ "00101001", "000001101100" },
   207     { /* 41 */ "00101010", "000001101101" },
   208     { /* 42 */ "00101011", "000011011010" },
   209     { /* 43 */ "00101100", "000011011011" },
   210     { /* 44 */ "00101101", "000001010100" },
   211     { /* 45 */ "00000100", "000001010101" },
   212     { /* 46 */ "00000101", "000001010110" },
   213     { /* 47 */ "00001010", "000001010111" },
   214     { /* 48 */ "00001011", "000001100100" },
   215     { /* 49 */ "01010010", "000001100101" },
   216     { /* 50 */ "01010011", "000001010010" },
   217     { /* 51 */ "01010100", "000001010011" },
   218     { /* 52 */ "01010101", "000000100100" },
   219     { /* 53 */ "00100100", "000000110111" },
   220     { /* 54 */ "00100101", "000000111000" },
   221     { /* 55 */ "01011000", "000000100111" },
   222     { /* 56 */ "01011001", "000000101000" },
   223     { /* 57 */ "01011010", "000001011000" },
   224     { /* 58 */ "01011011", "000001011001" },
   225     { /* 59 */ "01001010", "000000101011" },
   226     { /* 60 */ "01001011", "000000101100" },
   227     { /* 61 */ "00110010", "000001011010" },
   228     { /* 62 */ "00110011", "000001100110" },
   229     { /* 63 */ "00110100", "000001100111" }
   230   };
   233 void print_h_code (bool header)
   234 {
   235   int i;
   237   if (header)
   238     printf ("extern ");
   239   printf ("const g4_bits g4_h_code [2] [64]");
   240   if (header)
   241     {
   242       printf (";\n");
   243       return;
   244     }
   245   printf (" =\n");
   246   printf ("  {\n");
   247   printf ("    {\n");
   248   printf ("      /* white */\n");
   249   for (i = 0; i <= 63; i++)
   250     emit_code (6, h_code [i][0], i == 63, 1, i);
   251   printf ("    },\n");
   252   printf ("    {\n");
   253   printf ("      /* black */\n");
   254   for (i = 0; i <= 63; i++)
   255     emit_code (6, h_code [i][1], i == 63, 1, i);
   256   printf ("    }\n");
   257   printf ("  };\n");
   258 }
   261 char *v_code [7] =
   262   {
   263     /* -3 */ "0000010",
   264     /* -2 */ "000010",
   265     /* -1 */ "010",
   266     /*  0 */ "1",
   267     /*  1 */ "011",
   268     /*  2 */ "000011",
   269     /*  3 */ "0000011"
   270   };
   273 void print_v_code (bool header)
   274 {
   275   int i;
   277   if (header)
   278     printf ("extern ");
   279   printf ("const g4_bits g4_vert_code [7]");
   280   if (header)
   281     {
   282       printf (";\n");
   283       return;
   284     }
   285   printf ("=\n");
   286   printf ("  {\n");
   287   for (i = 0; i <= 6; i++)
   288     emit_code (4, v_code [i], i == 6, 1, i - 3);
   289   printf ("  };\n");
   290 }
   293 int main (int argc, char *argv [])
   294 {
   295   bool header;
   297   if (argc != 2)
   298     {
   299       fprintf (stderr, "wrong arg count\n");
   300       exit (2);
   301     }
   302   if (strcmp (argv [1], "-h") == 0)
   303     header = 1;
   304   else if (strcmp (argv [1], "-c") == 0)
   305     header = 0;
   306   else
   307     {
   308       fprintf (stderr, "wrong args\n");
   309       exit (2);
   310     }
   312   printf ("/* This file is automatically generated; do not edit */\n");
   313   printf ("\n");
   315   if (header)
   316     {
   317       printf ("typedef struct\n");
   318       printf ("{\n");
   319       printf ("  uint32_t count;\n");
   320       printf ("  uint32_t bits;\n");
   321       printf ("} g4_bits;\n");
   322     }
   323   else
   324     {
   325       printf ("#include <stdint.h>\n");
   326       printf ("#include \"g4_tables.h\"\n");
   327     }
   328   printf ("\n");
   330   print_long_makeup_code (header);
   331   printf ("\n");
   333   print_makeup_code (header);
   334   printf ("\n");
   336   print_h_code (header);
   337   printf ("\n");
   339   print_v_code (header);
   341   exit (0);
   342 }