bitblt_table_gen.c

Wed, 12 Mar 2003 06:39:22 +0000

author
eric
date
Wed, 12 Mar 2003 06:39:22 +0000
changeset 97
f2e13c2ff575
parent 78
74b6b230f85d
child 98
5fb9b9f7c2b8
permissions
-rw-r--r--

changed bitblt_table_gen.c to generate both a header and a C source file (bitblt_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  * bitblt table generator
     7  * $Id: bitblt_table_gen.c,v 1.5 2003/03/11 22:39:22 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 <stdio.h>
    29 #include <stdlib.h>
    31 void gen_bit_reverse_table (bool header)
    32 {
    33   int i, j;
    35   if (header)
    36     printf ("extern ");
    37   printf ("const uint8_t bit_reverse_byte [0x100]");
    38   if (header)
    39     {
    40       printf (";\n");
    41       return;
    42     }
    43   printf (" =\n");
    44   printf ("{\n");
    45   for (i = 0; i < 0x100; i++)
    46     {
    47       if ((i & 7) == 0)
    48 	printf ("  ");
    49       j = (((i & 0x01) << 7) |
    50 	   ((i & 0x02) << 5) |
    51 	   ((i & 0x04) << 3) |
    52 	   ((i & 0x08) << 1) |
    53 	   ((i & 0x10) >> 1) |
    54 	   ((i & 0x20) >> 3) |
    55 	   ((i & 0x40) >> 5) |
    56 	   ((i & 0x80) >> 7));
    57       printf ("0x%02x", j);
    58       if (i != 0xff)
    59 	printf (",");
    60       if ((i & 7) == 7)
    61 	printf ("\n");
    62       else
    63 	printf (" ");
    64     }
    65   printf ("};\n");
    66 }
    69 int count_run (int byte, int start_bit, int desired_val)
    70 {
    71   int count = 0;
    72   int i;
    74   for (i = start_bit; i < 8; i++)
    75     {
    76       int bit = (byte >> i) & 1;
    77       if (bit == desired_val)
    78 	count++;
    79       else
    80 	break;
    81     }
    82   return (count);
    83 }
    86 void gen_run_length_table (bool header, int val, char *name)
    87 {
    88   int i, j;
    90   if (header)
    91     printf ("extern ");
    92   printf ("const uint8_t %s [8][256]", name);
    93   if (header)
    94     {
    95       printf (";\n");
    96       return;
    97     }
    98   printf (" =\n");
    99   printf ("{\n");
   100   for (i = 0; i < 8; i++)
   101     {
   102       printf ("  {\n");
   103       for (j = 0; j < 256; j++)
   104 	{
   105 	  if ((j & 15) == 0)
   106 	    printf ("  ");
   107 	  printf ("%d", count_run (j, i, val));
   108 	  if (j != 0xff)
   109 	    printf (",");
   110 	  if ((j & 15) == 15)
   111 	    printf ("\n");
   112 	  else
   113 	    printf (" ");
   114 	}
   115       printf ("  }");
   116       if (i != 7)
   117 	printf (",");
   118       printf ("\n");
   119     }
   120   printf ("};\n");
   121 }
   124 int main (int argc, char *argv[])
   125 {
   126   bool header;
   128   if (argc != 2)
   129     {
   130       fprintf (stderr, "wrong arg count\n");
   131       exit (2);
   132     }
   133   if (strcmp (argv [1], "-h") == 0)
   134     header = 1;
   135   else if (strcmp (argv [1], "-c") == 0)
   136     header = 0;
   137   else
   138     {
   139       fprintf (stderr, "wrong args\n");
   140       exit (2);
   141     }
   143   printf ("/* This file is automatically generated; do not edit */\n");
   144   printf ("\n");
   146   if (! header)
   147     {
   148       printf ("#include <stdint.h>\n");
   149       printf ("#include \"bitblt_tables.h\"\n");
   150       printf ("\n");
   151     }
   153   gen_bit_reverse_table (header);
   154   printf ("\n");
   156   gen_run_length_table (header, 0, "rle_tab");
   157   printf ("\n");
   159   return (0);
   160 }