g4_table_gen.c

changeset 71
3e2d23e25fc3
child 78
74b6b230f85d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/g4_table_gen.c	Sat Feb 22 10:02:06 2003 +0000
     1.3 @@ -0,0 +1,284 @@
     1.4 +/*
     1.5 + * t2p: Create a PDF file from the contents of one or more TIFF
     1.6 + *      bilevel image files.  The images in the resulting PDF file
     1.7 + *      will be compressed using ITU-T T.6 (G4) fax encoding.
     1.8 + *
     1.9 + * G4 table generator
    1.10 + * $Id: g4_table_gen.c,v 1.1 2003/02/22 02:02:06 eric Exp $
    1.11 + * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
    1.12 + *
    1.13 + * This program is free software; you can redistribute it and/or modify
    1.14 + * it under the terms of the GNU General Public License version 2 as
    1.15 + * published by the Free Software Foundation.  Note that permission is
    1.16 + * not granted to redistribute this program under the terms of any
    1.17 + * other version of the General Public License.
    1.18 + *
    1.19 + * This program is distributed in the hope that it will be useful,
    1.20 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.21 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.22 + * GNU General Public License for more details.
    1.23 + *
    1.24 + * You should have received a copy of the GNU General Public License
    1.25 + * along with this program; if not, write to the Free Software
    1.26 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    1.27 + */
    1.28 +
    1.29 +
    1.30 +#include <stdbool.h>
    1.31 +#include <stdint.h>
    1.32 +#include <stdio.h>
    1.33 +#include <stdlib.h>
    1.34 +#include <string.h>
    1.35 +
    1.36 +
    1.37 +void emit_code (int indent, char *code, int last, bool comment, int cval)
    1.38 +{
    1.39 +  int i;
    1.40 +  int count = 0;
    1.41 +  uint32_t val = 0;
    1.42 +
    1.43 +  printf ("%*s{ ", indent, "");
    1.44 +
    1.45 +  printf ("%d, ", strlen (code));
    1.46 +
    1.47 +  for (i = 0; i < strlen (code); i++)
    1.48 +    switch (code [i])
    1.49 +      {
    1.50 +      case '0': val = (val << 1);     count++; break;
    1.51 +      case '1': val = (val << 1) + 1; count++; break;
    1.52 +      case ' ': break;
    1.53 +      default:
    1.54 +	fprintf (stderr, "internal error\n");
    1.55 +	exit (2);
    1.56 +      }
    1.57 +
    1.58 +  printf ("0x%0*x", (count + 3)/4, val);
    1.59 +
    1.60 +  printf (" }");
    1.61 +  if (! last)
    1.62 +    printf (",");
    1.63 +  if (comment)
    1.64 +    printf ("  /* %d */", cval);
    1.65 +  printf ("\n");
    1.66 +}
    1.67 +
    1.68 +
    1.69 +char *long_makeup_code [12] =
    1.70 +  {
    1.71 +    /* 1792 */ "00000001000",
    1.72 +    /* 1856 */ "00000001100",
    1.73 +    /* 1920 */ "00000001101",
    1.74 +    /* 1984 */ "000000010010",
    1.75 +    /* 2048 */ "000000010011",
    1.76 +    /* 2112 */ "000000010100",
    1.77 +    /* 2176 */ "000000010101",
    1.78 +    /* 2240 */ "000000010110",
    1.79 +    /* 2304 */ "000000010111",
    1.80 +    /* 2368 */ "000000011100",
    1.81 +    /* 2432 */ "000000011101",
    1.82 +    /* 2496 */ "000000011110"
    1.83 +    /* 2560    "000000011111"  hard-coded, doesn't need to be in table */
    1.84 +  };
    1.85 +
    1.86 +
    1.87 +void print_long_makeup_code (void)
    1.88 +{
    1.89 +  int i;
    1.90 +
    1.91 +  printf ("static g4_bits g4_long_makeup_code [12] =\n");
    1.92 +  printf ("  {\n");
    1.93 +  for (i = 0; i < 12; i++)
    1.94 +    emit_code (4, long_makeup_code [i], i == 11, 1, i * 64 + 1792);
    1.95 +  printf ("  };\n");
    1.96 +}
    1.97 +
    1.98 +
    1.99 +char *makeup_code [64][2] =
   1.100 +  {
   1.101 +    { /*   64 */ "11011",     "0000001111" },
   1.102 +    { /*  128 */ "10010",     "000011001000" },
   1.103 +    { /*  192 */ "010111",    "000011001001" },
   1.104 +    { /*  256 */ "0110111",   "000001011011" },
   1.105 +    { /*  320 */ "00110110",  "000000110011" },
   1.106 +    { /*  384 */ "00110111",  "000000110100" },
   1.107 +    { /*  448 */ "01100100",  "000000110101" },
   1.108 +    { /*  512 */ "01100101",  "0000001101100" },
   1.109 +    { /*  576 */ "01101000",  "0000001101101" },
   1.110 +    { /*  640 */ "01100111",  "0000001001010" },
   1.111 +    { /*  704 */ "011001100", "0000001001011" },
   1.112 +    { /*  768 */ "011001101", "0000001001100" },
   1.113 +    { /*  832 */ "011010010", "0000001001101" },
   1.114 +    { /*  896 */ "011010011", "0000001110010" },
   1.115 +    { /*  960 */ "011010100", "0000001110011" },
   1.116 +    { /* 1024 */ "011010101", "0000001110100" },
   1.117 +    { /* 1088 */ "011010110", "0000001110101" },
   1.118 +    { /* 1152 */ "011010111", "0000001110110" },
   1.119 +    { /* 1216 */ "011011000", "0000001110111" },
   1.120 +    { /* 1280 */ "011011001", "0000001010010" },
   1.121 +    { /* 1344 */ "011011010", "0000001010011" },
   1.122 +    { /* 1408 */ "011011011", "0000001010100" },
   1.123 +    { /* 1472 */ "010011000", "0000001010101" },
   1.124 +    { /* 1536 */ "010011001", "0000001011010" },
   1.125 +    { /* 1600 */ "010011010", "0000001011011" },
   1.126 +    { /* 1664 */ "011000",    "0000001100100" },
   1.127 +    { /* 1728 */ "010011011", "0000001100101" }
   1.128 +  };
   1.129 +
   1.130 +
   1.131 +void print_makeup_code (void)
   1.132 +{
   1.133 +  int i;
   1.134 +
   1.135 +  printf ("static g4_bits g4_makeup_code [2] [27] =\n");
   1.136 +  printf ("  {\n");
   1.137 +  printf ("    {\n");
   1.138 +  printf ("      /* white */\n");
   1.139 +  for (i = 0; i <= 26; i++)
   1.140 +    emit_code (6, makeup_code [i][0], i == 26, 1, (i + 1) * 64);
   1.141 +  printf ("    },\n");
   1.142 +  printf ("    {\n");
   1.143 +  printf ("      /* black */\n");
   1.144 +  for (i = 0; i <= 26; i++)
   1.145 +    emit_code (6, makeup_code [i][1], i == 26, 1, (i + 1) * 64);
   1.146 +  printf ("    }\n");
   1.147 +  printf ("  };\n");
   1.148 +}
   1.149 +
   1.150 +
   1.151 +char *h_code [64][2] =
   1.152 +  {
   1.153 +    { /*  0 */ "00110101", "0000110111" },
   1.154 +    { /*  1 */ "000111",   "010" },
   1.155 +    { /*  2 */ "0111",     "11" },
   1.156 +    { /*  3 */ "1000",     "10" },
   1.157 +    { /*  4 */ "1011",     "011" },
   1.158 +    { /*  5 */ "1100",     "0011" },
   1.159 +    { /*  6 */ "1110",     "0010" },
   1.160 +    { /*  7 */ "1111",     "00011" },
   1.161 +    { /*  8 */ "10011",    "000101" },
   1.162 +    { /*  9 */ "10100",    "000100" },
   1.163 +    { /* 10 */ "00111",    "0000100" },
   1.164 +    { /* 11 */ "01000",    "0000101" },
   1.165 +    { /* 12 */ "001000",   "0000111" },
   1.166 +    { /* 13 */ "000011",   "00000100" },
   1.167 +    { /* 14 */ "110100",   "00000111" },
   1.168 +    { /* 15 */ "110101",   "000011000" },
   1.169 +    { /* 16 */ "101010",   "0000010111" },
   1.170 +    { /* 17 */ "101011",   "0000011000" },
   1.171 +    { /* 18 */ "0100111",  "0000001000" },
   1.172 +    { /* 19 */ "0001100",  "00001100111" },
   1.173 +    { /* 20 */ "0001000",  "00001101000" },
   1.174 +    { /* 21 */ "0010111",  "00001101100" },
   1.175 +    { /* 22 */ "0000011",  "00000110111" },
   1.176 +    { /* 23 */ "0000100",  "00000101000" },
   1.177 +    { /* 24 */ "0101000",  "00000010111" },
   1.178 +    { /* 25 */ "0101011",  "00000011000" },
   1.179 +    { /* 26 */ "0010011",  "000011001010" },
   1.180 +    { /* 27 */ "0100100",  "000011001011" },
   1.181 +    { /* 28 */ "0011000",  "000011001100" },
   1.182 +    { /* 29 */ "00000010", "000011001101" },
   1.183 +    { /* 30 */ "00000011", "000001101000" },
   1.184 +    { /* 31 */ "00011010", "000001101001" },
   1.185 +    { /* 32 */ "00011011", "000001101010" },
   1.186 +    { /* 33 */ "00010010", "000001101011" },
   1.187 +    { /* 34 */ "00010011", "000011010010" },
   1.188 +    { /* 35 */ "00010100", "000011010011" },
   1.189 +    { /* 36 */ "00010101", "000011010100" },
   1.190 +    { /* 37 */ "00010110", "000011010101" },
   1.191 +    { /* 38 */ "00010111", "000011010110" },
   1.192 +    { /* 39 */ "00101000", "000011010111" },
   1.193 +    { /* 40 */ "00101001", "000001101100" },
   1.194 +    { /* 41 */ "00101010", "000001101101" },
   1.195 +    { /* 42 */ "00101011", "000011011010" },
   1.196 +    { /* 43 */ "00101100", "000011011011" },
   1.197 +    { /* 44 */ "00101101", "000001010100" },
   1.198 +    { /* 45 */ "00000100", "000001010101" },
   1.199 +    { /* 46 */ "00000101", "000001010110" },
   1.200 +    { /* 47 */ "00001010", "000001010111" },
   1.201 +    { /* 48 */ "00001011", "000001100100" },
   1.202 +    { /* 49 */ "01010010", "000001100101" },
   1.203 +    { /* 50 */ "01010011", "000001010010" },
   1.204 +    { /* 51 */ "01010100", "000001010011" },
   1.205 +    { /* 52 */ "01010101", "000000100100" },
   1.206 +    { /* 53 */ "00100100", "000000110111" },
   1.207 +    { /* 54 */ "00100101", "000000111000" },
   1.208 +    { /* 55 */ "01011000", "000000100111" },
   1.209 +    { /* 56 */ "01011001", "000000101000" },
   1.210 +    { /* 57 */ "01011010", "000001011000" },
   1.211 +    { /* 58 */ "01011011", "000001011001" },
   1.212 +    { /* 59 */ "01001010", "000000101011" },
   1.213 +    { /* 60 */ "01001011", "000000101100" },
   1.214 +    { /* 61 */ "00110010", "000001011010" },
   1.215 +    { /* 62 */ "00110011", "000001100110" },
   1.216 +    { /* 63 */ "00110100", "000001100111" }
   1.217 +  };
   1.218 +
   1.219 +
   1.220 +void print_h_code (void)
   1.221 +{
   1.222 +  int i;
   1.223 +
   1.224 +  printf ("static g4_bits g4_h_code [2] [64] =\n");
   1.225 +  printf ("  {\n");
   1.226 +  printf ("    {\n");
   1.227 +  printf ("      /* white */\n");
   1.228 +  for (i = 0; i <= 63; i++)
   1.229 +    emit_code (6, h_code [i][0], i == 63, 1, i);
   1.230 +  printf ("    },\n");
   1.231 +  printf ("    {\n");
   1.232 +  printf ("      /* black */\n");
   1.233 +  for (i = 0; i <= 63; i++)
   1.234 +    emit_code (6, h_code [i][1], i == 63, 1, i);
   1.235 +  printf ("    }\n");
   1.236 +  printf ("  };\n");
   1.237 +}
   1.238 +
   1.239 +
   1.240 +char *v_code [7] =
   1.241 +  {
   1.242 +    /* -3 */ "0000010",
   1.243 +    /* -2 */ "000010",
   1.244 +    /* -1 */ "010",
   1.245 +    /*  0 */ "1",
   1.246 +    /*  1 */ "011",
   1.247 +    /*  2 */ "000011",
   1.248 +    /*  3 */ "0000011"
   1.249 +  };
   1.250 +
   1.251 +
   1.252 +void print_v_code (void)
   1.253 +{
   1.254 +  int i;
   1.255 +
   1.256 +  printf ("static g4_bits g4_vert_code [7] =\n");
   1.257 +  printf ("  {\n");
   1.258 +  for (i = 0; i <= 6; i++)
   1.259 +    emit_code (4, v_code [i], i == 6, 1, i - 3);
   1.260 +  printf ("  };\n");
   1.261 +}
   1.262 +
   1.263 +
   1.264 +int main (int argc, char *argv [])
   1.265 +{
   1.266 +  printf ("/* This file is automatically generated; do not edit */\n");
   1.267 +  printf ("\n");
   1.268 +  printf ("typedef struct\n");
   1.269 +  printf ("{\n");
   1.270 +  printf ("  uint32_t count;\n");
   1.271 +  printf ("  uint32_t bits;\n");
   1.272 +  printf ("} g4_bits;\n");
   1.273 +  printf ("\n");
   1.274 +
   1.275 +  print_long_makeup_code ();
   1.276 +  printf ("\n");
   1.277 +
   1.278 +  print_makeup_code ();
   1.279 +  printf ("\n");
   1.280 +
   1.281 +  print_h_code ();
   1.282 +  printf ("\n");
   1.283 +
   1.284 +  print_v_code ();
   1.285 +
   1.286 +  exit (0);
   1.287 +}