home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / LIBTIFF / mkg3states.c < prev    next >
C/C++ Source or Header  |  1996-11-18  |  25KB  |  807 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/mkg3states.c,v 1.16 93/08/25 09:08:33 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * Program to construct Group 3 & Group 4 decoding tables.
  31.  *
  32.  * This code is derived from code by Michael P. Marking.  In
  33.  * particular, the algorithms to generate the null_mode and
  34.  * horiz_mode state tables are his.  See the comments below
  35.  * for more information.
  36.  *
  37.  * BEGIN (from the original source)
  38.  LEGAL
  39.  *    Copyright 1989, 1990 Michael P. Marking, Post Office Box 8039,
  40.  *    Scottsdale, Arizona 85252-8039. All rights reserved.
  41.  *
  42.  *    License is granted by the copyright holder to distribute and use this
  43.  *    code without payment of royalties or the necessity of notification as
  44.  *    long as this notice (all the text under "LEGAL") is included.
  45.  *
  46.  *    Reference: $Id: mkg3states.c,v 1.16 93/08/25 09:08:33 sam Exp $
  47.  *
  48.  *    This program is offered without any warranty of any kind. It includes
  49.  *    no warranty of merchantability or fitness for any purpose. Testing and
  50.  *    suitability for any use are the sole responsibility of the user.
  51.  *
  52.  INFORMATION
  53.  *    Although there is no support offered with this program, the author will
  54.  *    endeavor to correct errors. Updates will also be made available from
  55.  *    time to time.
  56.  *
  57.  *    Contact: Michael P. Marking, Post Office Box 8039, Scottsdale, Arizona
  58.  *    85252-8039 USA. Replies are not guaranteed to be swift. Beginning
  59.  *    July 1990, e-mail may be sent to uunet!ipel!marking.
  60.  *
  61.  *    Also beginning in July 1990, this code will be archived at the
  62.  *    ipel!phoenix BBS in file g3g4.zoo. The 24-hour telephone number
  63.  *    for 300/1200/2400 is (602)274-0462. When logging in, specify user
  64.  *    "public", system "bbs", and password "public".
  65.  *
  66.  *    This code is also available from the C Users Group in volume 317.
  67.  *
  68.  * END (from the original source)
  69.  */
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include "tiffcomp.h"
  73.  
  74. #ifndef TRUE
  75. #define    TRUE    1
  76. #define    FALSE    0
  77. #endif
  78.  
  79. #define WHITE    0
  80. #define BLACK    1
  81.  
  82. /*
  83.  * G3 2D and G4 decoding modes.  Note that
  84.  * the vertical modes are ordered so that
  85.  * (mode - MODE_VERT_V0) gives the vertical
  86.  * adjustment for the b1 parameter.
  87.  */
  88. #define MODE_NULL    0
  89. #define MODE_PASS    1
  90. #define MODE_HORIZ    2
  91. #define MODE_VERT_VL3    3
  92. #define MODE_VERT_VL2    4
  93. #define MODE_VERT_VL1    5
  94. #define MODE_VERT_V0    6
  95. #define MODE_VERT_VR1    7
  96. #define MODE_VERT_VR2    8
  97. #define MODE_VERT_VR3    9
  98. #define MODE_UNCOMP    10
  99. #define MODE_ERROR    11
  100. #define MODE_ERROR_1    12
  101.  
  102. unsigned long
  103. append_0(unsigned long prefix)
  104. {
  105.     return (prefix + (1L<<16));
  106. }
  107.  
  108. unsigned long
  109. append_1(unsigned long prefix)
  110. {
  111.     static unsigned short prefix_bit[16] = {
  112.     0x8000, 0x4000, 0x2000, 0x1000,
  113.     0x0800, 0x0400, 0x0200, 0x0100,
  114.     0x0080, 0x0040, 0x0020, 0x0010,
  115.     0x0008, 0x0004, 0x0002, 0x0001
  116.     };
  117.     unsigned char len = (prefix >> 16) & 0xf;
  118.     return (append_0(prefix) + prefix_bit[len]);
  119. }
  120.  
  121. #define    G3CODES
  122. #include "t4.h"
  123.  
  124. short
  125. search_table(unsigned long prefix, const tableentry* tab, int n)
  126. {
  127.     unsigned short len = (prefix >> 16) & 0xf;
  128.     unsigned short code = (prefix & 0xffff) >> (16 - len);
  129.  
  130.     while (n-- > 0) {
  131.     if (tab->length == len && tab->code == code)
  132.         return ((short) tab->runlen);
  133.     tab++;
  134.     }
  135.     return (G3CODE_INCOMP);
  136. }
  137.  
  138. #define    NCODES(a)    (sizeof (a) / sizeof (a[0]))
  139. short
  140. white_run_length(unsigned long prefix)
  141. {
  142.     return (search_table(prefix, TIFFFaxWhiteCodes, NCODES(TIFFFaxWhiteCodes)));
  143. }
  144.  
  145. short
  146. black_run_length(unsigned long prefix)
  147. {
  148.     return (search_table(prefix, TIFFFaxBlackCodes, NCODES(TIFFFaxBlackCodes)));
  149. }
  150. #undef NCODES
  151.  
  152. #define MAX_NULLPREFIX    200    /* max # of null-mode prefixes */
  153. typedef    unsigned char NullModeTable[MAX_NULLPREFIX][256];
  154. #define MAX_HORIZPREFIX    250    /* max # of incomplete 1-D prefixes */
  155. typedef    unsigned char HorizModeTable[MAX_HORIZPREFIX][256];
  156.  
  157.   /* the bit string corresponding to this row of the decoding table */
  158. long    null_mode_prefix[MAX_NULLPREFIX];
  159. NullModeTable null_mode;        /* MODE_*, indexed by bit and byte */
  160. NullModeTable null_mode_next_state;    /* next row of decoding tables to use */
  161.   /* number of prefixes or rows in the G4 decoding tables */
  162. short    null_mode_prefix_count = 0;
  163.  
  164. /*
  165.  * 2D uncompressed mode codes.  Note
  166.  * that two groups of codes are arranged
  167.  * so that the decoder can caluclate the
  168.  * length of the run by subtracting the
  169.  * code from a known base value.
  170.  */
  171. #define    UNCOMP_INCOMP    0
  172. /* runs of [0]*1 */
  173. #define    UNCOMP_RUN0    1
  174. #define    UNCOMP_RUN1    2
  175. #define    UNCOMP_RUN2    3
  176. #define    UNCOMP_RUN3    4
  177. #define    UNCOMP_RUN4    5
  178. #define    UNCOMP_RUN5    6
  179. #define    UNCOMP_RUN6    7
  180. /* runs of [0]* w/ terminating color */
  181. #define    UNCOMP_TRUN0    8
  182. #define    UNCOMP_TRUN1    9
  183. #define    UNCOMP_TRUN2    10
  184. #define    UNCOMP_TRUN3    11
  185. #define    UNCOMP_TRUN4    12
  186. /* special code for unexpected EOF */
  187. #define    UNCOMP_EOF    13
  188. /* invalid code encountered */
  189. #define    UNCOMP_INVALID    14
  190.  
  191. long    uncomp_mode_prefix[MAX_NULLPREFIX];
  192. NullModeTable uncomp_mode;
  193. NullModeTable uncomp_mode_next_state;
  194. short    uncomp_mode_prefix_count = 0;
  195.  
  196. /*
  197.  * Decoding action values for horiz_mode.
  198.  */
  199. #define ACT_INCOMP    0        /* incompletely decoded code */
  200. #define ACT_INVALID    1        /* invalide code */
  201. #define    ACT_WRUNT    2        /* terminating white run code */
  202. #define    ACT_WRUN    65        /* non-terminating white run code */
  203. #define    ACT_BRUNT    106        /* terminating black run code */
  204. #define    ACT_BRUN    169        /* non-terminating black run code */
  205. #define ACT_EOL        210        /* end-of-line code */
  206. HorizModeTable horiz_mode;
  207.  
  208. short
  209. horiz_mode_code_black(short runlen)
  210. {
  211.     return (runlen < 64 ? runlen + ACT_BRUNT : (runlen / 64) + ACT_BRUN);
  212. }
  213.  
  214. short
  215. horiz_mode_code_white(short runlen)
  216. {
  217.     return (runlen < 64 ? runlen + ACT_WRUNT : (runlen / 64) + ACT_WRUN);
  218. }
  219.  
  220. /*
  221.  * If the corresponding horiz_mode entry is ACT_INCOMP
  222.  * this entry is a row number for decoding the next byte;
  223.  * otherwise, it is the bit number with which to continue
  224.  * decoding the next codeword.
  225.  */
  226. HorizModeTable horiz_mode_next_state;
  227.         /* prefixes corresponding to the rows of the decoding table */
  228. long    horiz_mode_prefix[MAX_HORIZPREFIX];
  229.         /* color of next run, BLACK or WHITE */
  230. char    horiz_mode_color[MAX_HORIZPREFIX];
  231. short    horiz_mode_prefix_count = 0;
  232.  
  233. static    unsigned char bit_mask[8] =
  234.     { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
  235.  
  236. void    build_null_mode_tables(void);
  237. short    find_horiz_mode_prefix(long, char);
  238. short    find_null_mode_prefix(long);
  239. short    null_mode_type(long);
  240. void    build_horiz_mode_tables(void);
  241. short    horiz_mode_code_black(short);
  242. short    horiz_mode_code_white(short);
  243. void    build_uncomp_mode_tables(void);
  244. void    write_tables(FILE*);
  245.  
  246. int    verbose = FALSE;
  247. char    *storage_class = "";
  248. int    packoutput = TRUE;
  249.  
  250. void
  251. main(int argc, char** argv)
  252. {
  253.     while (argc > 1 && argv[1][0] == '-') {
  254.     if (strcmp(argv[1], "-v") == 0) {
  255.         verbose = TRUE;
  256.         argc--, argv++;
  257.     } else if (strcmp(argv[1], "-c") == 0) {
  258.         storage_class = "const ";
  259.         argc--, argv++;
  260.     } else if (strcmp(argv[1], "-p") == 0) {
  261.         packoutput = FALSE;
  262.         argc--, argv++;
  263.     }
  264.     }
  265.     build_null_mode_tables();        /* null mode decoding tables */
  266.     if (verbose) {
  267.     fprintf(stderr, "%d null mode prefixes defined\n",
  268.         (int) null_mode_prefix_count);
  269.     fprintf(stderr, "building uncompressed mode scripts...\n");
  270.     }
  271.     build_uncomp_mode_tables();        /* uncompressed mode decoding tables */
  272.     if (verbose) {
  273.     fprintf(stderr, "%d uncompressed mode prefixes defined\n",
  274.         (int) uncomp_mode_prefix_count);
  275.     fprintf(stderr, "building 1D scripts...\n");
  276.     }
  277.     build_horiz_mode_tables();        /* 1D decoding tables */
  278.     if (verbose)
  279.     fprintf(stderr, "%d incomplete prefixes defined\n",
  280.         (int) horiz_mode_prefix_count);
  281.     write_tables(stdout);
  282.     exit(0);
  283. }
  284.  
  285. void
  286. write_null_mode_table(FILE* fd, NullModeTable table, char* name)
  287. {
  288.     int i, j, lastNonZero;
  289.     char* outersep;
  290.     char* sep;
  291.  
  292.     fprintf(fd, "%su_char\t%s[%d][256] = {", storage_class,
  293.     name, (int) null_mode_prefix_count);
  294.     outersep = "";
  295.     if (!packoutput) {
  296.     for (i = 0; i < null_mode_prefix_count; i++) {
  297.         fprintf(fd, "%s\n/* prefix %d */ {\n", outersep, i);
  298.         sep = "    ";
  299.         for (j = 0; j < 256; j++) {
  300.         fprintf(fd, "%s%2d", sep, (int) table[i][j]);
  301.         if (((j+1) % 16) == 0) {
  302.             fprintf(fd, ", /* %3d-%3d */\n", j-15, j);
  303.             sep = "    ";
  304.         } else
  305.             sep = ",";
  306.         }
  307.         fprintf(fd, "}");
  308.         outersep = ",";
  309.     }
  310.     } else {
  311.     for (i = 0; i < null_mode_prefix_count; i++) {
  312.         fprintf(fd, "%s{\n", outersep);
  313.         for (j = 255; j > 0; j--)
  314.         if (table[i][j] != 0)
  315.             break;
  316.         sep = "";
  317.         lastNonZero = j;
  318.         for (j = 0; j <= lastNonZero; j++) {
  319.         fprintf(fd, "%s%d", sep, (int) table[i][j]);
  320.         if (((j+1) % 24) == 0)
  321.             putc('\n', fd);
  322.         sep = ",";
  323.         }
  324.         fprintf(fd, "}");
  325.         outersep = ",";
  326.     }
  327.     }
  328.     fprintf(fd, "\n};\n");
  329. }
  330.  
  331. void
  332. write_horiz_mode_table(FILE* fd, HorizModeTable table, char* name)
  333. {
  334.     int i, j, lastNonZero;
  335.     char* outersep;
  336.     char* sep;
  337.  
  338.     fprintf(fd, "%s u_char\t%s[%d][256] = {", storage_class,
  339.     name, (int) horiz_mode_prefix_count);
  340.     outersep = "";
  341.     if (!packoutput) {
  342.     for (i = 0; i < horiz_mode_prefix_count; i++) {
  343.         fprintf(fd, "%s\n/* prefix %d */ {\n", outersep, i);
  344.         sep = "    ";
  345.         for (j = 0; j < 256; j++) {
  346.         fprintf(fd, "%s%3d", sep, (int) table[i][j]);
  347.         if (((j+1) % 14) == 0) {
  348.             fprintf(fd, ", /* %3d-%3d */\n", j-13, j);
  349.             sep = "    ";
  350.         } else
  351.             sep = ",";
  352.         }
  353.         fprintf(fd, "\n}");
  354.         outersep = ",";
  355.     }
  356.     } else {
  357.     outersep = "";
  358.     for (i = 0; i < horiz_mode_prefix_count; i++) {
  359.         fprintf(fd, "%s{\n", outersep);
  360.         for (j = 255; j > 0; j--)
  361.         if (table[i][j] != 0)
  362.             break;
  363.         sep = "";
  364.         lastNonZero = j;
  365.         for (j = 0; j <= lastNonZero; j++) {
  366.         fprintf(fd, "%s%d", sep, (int) table[i][j]);
  367.         if (((j+1) % 24) == 0)
  368.             putc('\n', fd);
  369.         sep = ",";
  370.         }
  371.         fprintf(fd, "}");
  372.         outersep = ",";
  373.     }
  374.     }
  375.     fprintf(fd, "\n};\n");
  376. }
  377.  
  378. void
  379. write_define(FILE* fd, char* name, int value, char* comment)
  380. {
  381.     fprintf(fd, "#define\t%s\t%d", name, value);
  382.     if (!packoutput && comment)
  383.     fprintf(fd, "\t/* %s */", comment);
  384.     fprintf(fd, "\n");
  385. }
  386.  
  387. void
  388. write_preamble(FILE* fd)
  389. {
  390.     fprintf(fd, "%s\n",
  391. "/* DO NOT EDIT THIS FILE, IT WAS AUTOMATICALLY CREATED BY mkg3state */");
  392.     write_define(fd, "ACT_INCOMP", ACT_INCOMP, "incompletely decoded code");
  393.     write_define(fd, "ACT_INVALID", ACT_INVALID, "invalide code");
  394.     write_define(fd, "ACT_WRUNT", ACT_WRUNT, "terminating white run code");
  395.     write_define(fd, "ACT_WRUN", ACT_WRUN, "non-terminating white run code");
  396.     write_define(fd, "ACT_BRUNT", ACT_BRUNT, "terminating black run code");
  397.     write_define(fd, "ACT_BRUN", ACT_BRUN, "non-terminating black run code");
  398.     write_define(fd, "ACT_EOL", ACT_EOL, "end-of-line code");
  399.     fprintf(fd, "\n");
  400.     fprintf(fd, "/* modes that the decoder can be in */\n");
  401.     write_define(fd, "MODE_NULL", MODE_NULL, NULL);
  402.     write_define(fd, "MODE_PASS", MODE_PASS, NULL);
  403.     write_define(fd, "MODE_HORIZ", MODE_HORIZ, NULL);
  404.     write_define(fd, "MODE_VERT_V0", MODE_VERT_V0, NULL);
  405.     write_define(fd, "MODE_VERT_VR1", MODE_VERT_VR1, NULL);
  406.     write_define(fd, "MODE_VERT_VR2", MODE_VERT_VR2, NULL);
  407.     write_define(fd, "MODE_VERT_VR3", MODE_VERT_VR3, NULL);
  408.     write_define(fd, "MODE_VERT_VL1", MODE_VERT_VL1, NULL);
  409.     write_define(fd, "MODE_VERT_VL2", MODE_VERT_VL2, NULL);
  410.     write_define(fd, "MODE_VERT_VL3", MODE_VERT_VL3, NULL);
  411.     write_define(fd, "MODE_UNCOMP", MODE_UNCOMP, NULL);
  412.     write_define(fd, "MODE_ERROR", MODE_ERROR, NULL);
  413.     write_define(fd, "MODE_ERROR_1", MODE_ERROR_1, NULL);
  414.     fprintf(fd, "\n");
  415.     fprintf(fd, "#define\tRUNLENGTH(ix)    (TIFFFaxWhiteCodes[ix].runlen)\n");
  416.     fprintf(fd, "\n");
  417.     write_define(fd, "UNCOMP_INCOMP", UNCOMP_INCOMP, NULL);
  418.     fprintf(fd, "/* runs of [0]*1 */\n");
  419.     write_define(fd, "UNCOMP_RUN0", UNCOMP_RUN0, NULL);
  420.     write_define(fd, "UNCOMP_RUN1", UNCOMP_RUN1, NULL);
  421.     write_define(fd, "UNCOMP_RUN2", UNCOMP_RUN2, NULL);
  422.     write_define(fd, "UNCOMP_RUN3", UNCOMP_RUN3, NULL);
  423.     write_define(fd, "UNCOMP_RUN4", UNCOMP_RUN4, NULL);
  424.     write_define(fd, "UNCOMP_RUN5", UNCOMP_RUN5, NULL);
  425.     write_define(fd, "UNCOMP_RUN6", UNCOMP_RUN6, NULL);
  426.     fprintf(fd, "/* runs of [0]* w/ terminating color */\n");
  427.     write_define(fd, "UNCOMP_TRUN0", UNCOMP_TRUN0, NULL);
  428.     write_define(fd, "UNCOMP_TRUN1", UNCOMP_TRUN1, NULL);
  429.     write_define(fd, "UNCOMP_TRUN2", UNCOMP_TRUN2, NULL);
  430.     write_define(fd, "UNCOMP_TRUN3", UNCOMP_TRUN3, NULL);
  431.     write_define(fd, "UNCOMP_TRUN4", UNCOMP_TRUN4, NULL);
  432.     fprintf(fd, "/* special code for unexpected EOF */\n");
  433.     write_define(fd, "UNCOMP_EOF", UNCOMP_EOF, NULL);
  434.     fprintf(fd, "/* invalid code encountered */\n");
  435.     write_define(fd, "UNCOMP_INVALID", UNCOMP_INVALID, NULL);
  436.     fprintf(fd, "/* codes >= terminate uncompress mode */\n");
  437.     fprintf(fd, "#define\tUNCOMP_EXIT    UNCOMP_TRUN0\n");
  438.     fprintf(fd, "\n");
  439. }
  440.  
  441. void
  442. extern_table(FILE* fd, char* name)
  443. {
  444.     fprintf(fd, "extern\t%su_char %s[][256];\n", storage_class, name);
  445. }
  446.  
  447. void
  448. write_tables(FILE* fd)
  449. {
  450.     write_preamble(fd);
  451.     fprintf(fd, "#ifdef G3STATES\n");
  452.     write_null_mode_table(fd, null_mode, "TIFFFax2DMode");
  453.     write_null_mode_table(fd, null_mode_next_state, "TIFFFax2DNextState");
  454.     write_null_mode_table(fd, uncomp_mode, "TIFFFaxUncompAction");
  455.     write_null_mode_table(fd, uncomp_mode_next_state, "TIFFFaxUncompNextState");
  456.     write_horiz_mode_table(fd, horiz_mode, "TIFFFax1DAction");
  457.     write_horiz_mode_table(fd, horiz_mode_next_state, "TIFFFax1DNextState");
  458.     fprintf(fd, "#else\n");
  459.     extern_table(fd, "TIFFFax2DMode");
  460.     extern_table(fd, "TIFFFax2DNextState");
  461.     extern_table(fd, "TIFFFaxUncompAction");
  462.     extern_table(fd, "TIFFFaxUncompNextState");
  463.     extern_table(fd, "TIFFFax1DAction");
  464.     extern_table(fd, "TIFFFax1DNextState");
  465.     fprintf(fd, "#endif\n");
  466. }
  467.  
  468. short
  469. find_null_mode_prefix(long prefix)
  470. {
  471.     short j1;
  472.  
  473.     if (prefix == 0L)
  474.     return (0);
  475.     for (j1 = 8; j1 < null_mode_prefix_count; j1++)
  476.     if (prefix == null_mode_prefix[j1])
  477.         return (j1);
  478.     if (null_mode_prefix_count == MAX_NULLPREFIX) {
  479.     fprintf(stderr, "ERROR: null mode prefix table overflow\n");
  480.     exit(1);
  481.     }
  482.     if (verbose)
  483.     fprintf(stderr, "adding null mode prefix[%d] 0x%lx\n",
  484.         (int) null_mode_prefix_count, prefix);
  485.     null_mode_prefix[null_mode_prefix_count++] = prefix;
  486.     return (null_mode_prefix_count-1);
  487. }
  488.  
  489. short
  490. find_horiz_mode_prefix(long prefix, char color)
  491. {
  492.     short j1;
  493.  
  494.     for (j1 = 0; j1 < horiz_mode_prefix_count; j1++)
  495.     if (prefix == horiz_mode_prefix[j1] && horiz_mode_color[j1] == color)
  496.         return (j1);
  497.     /*
  498.      * It wasn't found, so add it to the tables, but first, is there room?
  499.      */
  500.     if (horiz_mode_prefix_count == MAX_HORIZPREFIX) {
  501.     fprintf(stderr, "ERROR: 1D prefix table overflow\n");
  502.     exit(1);
  503.     }
  504.     /* OK, there's room... */
  505.     if (verbose)
  506.     fprintf(stderr, "\nhoriz mode prefix %d, color %c = 0x%lx ",
  507.         (int) horiz_mode_prefix_count, "WB"[color], prefix);
  508.     horiz_mode_prefix[horiz_mode_prefix_count] = prefix;
  509.     horiz_mode_color[horiz_mode_prefix_count] = color;
  510.     horiz_mode_prefix_count++;
  511.     return (horiz_mode_prefix_count - 1);
  512. }
  513.  
  514. short
  515. find_uncomp_mode_prefix(long prefix)
  516. {
  517.     short j1;
  518.  
  519.     if (prefix == 0L)
  520.     return (0);
  521.     for (j1 = 8; j1 < uncomp_mode_prefix_count; j1++)
  522.     if (prefix == uncomp_mode_prefix[j1])
  523.         return (j1);
  524.     if (uncomp_mode_prefix_count == MAX_NULLPREFIX) {
  525.     fprintf(stderr, "ERROR: uncomp mode prefix table overflow\n");
  526.     exit(1);
  527.     }
  528.     if (verbose)
  529.     fprintf(stderr, "adding uncomp mode prefix[%d] 0x%lx\n",
  530.         (int) uncomp_mode_prefix_count, prefix);
  531.     uncomp_mode_prefix[uncomp_mode_prefix_count++] = prefix;
  532.     return (uncomp_mode_prefix_count-1);
  533. }
  534.  
  535. short
  536. null_mode_type(long prefix)
  537. {
  538.     switch (prefix) {
  539.     case 0x18000L: return (MODE_VERT_V0);    /* 1 */
  540.     case 0x36000L: return (MODE_VERT_VR1);    /* 011 */
  541.     case 0x34000L: return (MODE_VERT_VL1);    /* 010 */
  542.     case 0x32000L: return (MODE_HORIZ);        /* 001 */
  543.     case 0x41000L: return (MODE_PASS);        /* 0001 */
  544.     case 0x60C00L: return (MODE_VERT_VR2);    /* 0000 11 */
  545.     case 0x60800L: return (MODE_VERT_VL2);    /* 0000 10 */
  546.     case 0x70600L: return (MODE_VERT_VR3);    /* 0000 011 */
  547.     case 0x70400L: return (MODE_VERT_VL3);    /* 0000 010 */
  548.     case 0x80200L: return (MODE_ERROR);        /* 0000 0010 */
  549.     case 0x90300L: return (MODE_ERROR);        /* 0000 0011 0 */
  550.     case 0xA0380L: return (MODE_ERROR);        /* 0000 0011 10 */
  551.     case 0xA03C0L: return (MODE_UNCOMP);    /* 0000 0011 11 */
  552.     /*
  553.      * Under the assumption that there are no
  554.      * errors in the file, then this bit string
  555.      * can only be the beginning of an EOL code.
  556.      */
  557.     case 0x70000L: return (MODE_ERROR_1);    /* 0000 000 */
  558.     }
  559.     return (-1);
  560. }
  561.  
  562. short
  563. uncomp_mode_type(long prefix)
  564. {
  565.     short code;
  566.     short len;
  567.     switch (prefix) {
  568.     case 0x18000L: return (UNCOMP_RUN1);    /* 1 */
  569.     case 0x24000L: return (UNCOMP_RUN2);    /* 01 */
  570.     case 0x32000L: return (UNCOMP_RUN3);    /* 001 */
  571.     case 0x41000L: return (UNCOMP_RUN4);    /* 0001 */
  572.     case 0x50800L: return (UNCOMP_RUN5);    /* 0000 1 */
  573.     case 0x60400L: return (UNCOMP_RUN6);    /* 0000 01 */
  574.     case 0x70200L: return (UNCOMP_TRUN0);    /* 0000 001 */
  575.     case 0x80100L: return (UNCOMP_TRUN1);    /* 0000 0001 */
  576.     case 0x90080L: return (UNCOMP_TRUN2);    /* 0000 0000 1 */
  577.     case 0xA0040L: return (UNCOMP_TRUN3);    /* 0000 0000 01 */
  578.     case 0xB0020L: return (UNCOMP_TRUN4);    /* 0000 0000 001 */
  579.     }
  580.     code = prefix & 0xffffL;
  581.     len = (prefix >> 16) & 0xf;
  582.     return ((code || len > 10) ? UNCOMP_INVALID : -1);
  583. }
  584.  
  585. #define    BASESTATE(b)    ((unsigned char) ((b) & 0x7))
  586.  
  587. void
  588. build_null_mode_tables(void)
  589. {
  590.     short prefix;
  591.  
  592.     /*
  593.      * Note: the first eight entries correspond to
  594.      * a null prefix and starting bit numbers 0-7.
  595.      */
  596.     null_mode_prefix_count = 8;
  597.     for (prefix = 0; prefix < null_mode_prefix_count; prefix++) {
  598.     short byte;
  599.     for (byte = 0; byte < 256; byte++) {
  600.         short firstbit;
  601.         short bit;
  602.         long curprefix;
  603.         char found_code = FALSE;
  604.  
  605.         if (prefix < 8) {
  606.         curprefix = 0L;
  607.         firstbit = prefix;
  608.         } else {
  609.         curprefix = null_mode_prefix[prefix];
  610.         firstbit = 0;
  611.         }
  612.         for (bit = firstbit; bit < 8 && !found_code; bit++) {
  613.         short mode;
  614.  
  615.         if (bit_mask[bit] & byte)
  616.             curprefix = append_1(curprefix);
  617.         else
  618.             curprefix = append_0(curprefix);
  619.         switch (mode = null_mode_type(curprefix)) {
  620.         case MODE_PASS:
  621.         case MODE_HORIZ:
  622.         case MODE_VERT_V0:
  623.         case MODE_VERT_VR1:
  624.         case MODE_VERT_VR2:
  625.         case MODE_VERT_VR3:
  626.         case MODE_VERT_VL1:
  627.         case MODE_VERT_VL2:
  628.         case MODE_VERT_VL3:
  629.         case MODE_UNCOMP:
  630.         case MODE_ERROR:
  631.         case MODE_ERROR_1:
  632.             /*
  633.              * NOTE: if the bit number is 8, then the table
  634.              * entry will be zero, which indicates a new byte
  635.              * is to be fetched during the decoding process
  636.              */
  637.             found_code = TRUE;
  638.             null_mode[prefix][byte] = (unsigned char) mode;
  639.             null_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  640.             break;
  641.         }
  642.         }
  643.         if (!found_code) {
  644.         null_mode_next_state[prefix][byte] = (unsigned char)
  645.             find_null_mode_prefix(curprefix);
  646.         /*
  647.          * This indicates to the decoder that
  648.          * no valid code has yet been identified.
  649.          */
  650.         null_mode[prefix][byte] = MODE_NULL;
  651.         }
  652.     }
  653.     }
  654. }
  655.  
  656. void
  657. build_horiz_mode_tables(void)
  658. {
  659.     unsigned short byte;
  660.     short prefix;
  661.  
  662.     /*
  663.      * The first 8 are for white,
  664.      * the second 8 are for black,
  665.      * beginning with bits 0-7.
  666.      */
  667.     horiz_mode_prefix_count = 16;
  668.     for (prefix = 0; prefix < horiz_mode_prefix_count; prefix++)
  669.     for (byte = 0; byte < 256; byte++) {
  670.         short bits_digested = 0;
  671.         short bit;
  672.         short firstbit;
  673.         char color;
  674.         unsigned long curprefix;
  675.  
  676.         if (prefix < 8) {
  677.         color = WHITE;
  678.         curprefix = 0L;
  679.         firstbit = prefix;
  680.         } else if (prefix < 16) {
  681.         color = BLACK;
  682.         curprefix = 0L;
  683.         firstbit = prefix - 8;
  684.         } else {
  685.         color = horiz_mode_color[prefix];
  686.         curprefix = horiz_mode_prefix[prefix];
  687.         firstbit = 0;
  688.         }
  689.         for (bit = firstbit; bit < 8 && !bits_digested; bit++) {
  690.         if (bit_mask[bit] & byte)
  691.             curprefix = append_1(curprefix);
  692.         else
  693.             curprefix = append_0(curprefix);
  694.         /*
  695.          * The following conversion allows for arbitrary strings of
  696.          * zeroes to precede the end-of-line code 0000 0000 0001.
  697.          * It assumes no errors in the data, and is based on
  698.          * the assumption that the code replaced (12 consecutive
  699.          * zeroes) can only be "legally" encountered before the
  700.          * end-of-line code.  This assumption is valid only for
  701.          * a Group 3 image; the combination will never occur
  702.          * in horizontal mode in a proper Group 4 image.
  703.          */
  704.         if (curprefix == 0xC0000L)
  705.             curprefix = 0xB0000L;
  706.         if (color == WHITE) {
  707.             short runlength = white_run_length(curprefix);
  708.  
  709.             if (runlength == G3CODE_INVALID) {
  710.             horiz_mode[prefix][byte] = (unsigned char) ACT_INVALID;
  711.             horiz_mode_next_state[prefix][byte] = (unsigned char) bit;
  712.             bits_digested = bit + 1;
  713.             } else if (runlength == G3CODE_EOL) { /* Group 3 only */
  714.             horiz_mode[prefix][byte] = (unsigned char) ACT_EOL;
  715.             horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  716.             bits_digested = bit + 1;
  717.             } else if (runlength != G3CODE_INCOMP) {
  718.             horiz_mode[prefix][byte] = (unsigned char)
  719.                 horiz_mode_code_white(runlength);
  720.             horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  721.             bits_digested = bit + 1;
  722.             }
  723.         } else {        /* color == BLACK */
  724.             short runlength = black_run_length(curprefix);
  725.  
  726.             if (runlength == G3CODE_INVALID) {
  727.             horiz_mode[prefix][byte] = (unsigned char) ACT_INVALID;
  728.             horiz_mode_next_state[prefix][byte] = (unsigned char) (bit+8);
  729.             bits_digested = bit + 1;
  730.             } else if (runlength == G3CODE_EOL) { /* Group 3 only */
  731.             horiz_mode[prefix][byte] = (unsigned char) ACT_EOL;
  732.             horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  733.             bits_digested = bit + 1;
  734.             } else if (runlength != G3CODE_INCOMP) {
  735.             horiz_mode[prefix][byte] = (unsigned char)
  736.                 horiz_mode_code_black(runlength);
  737.             horiz_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  738.             bits_digested = bit + 1;
  739.             }
  740.         }
  741.         }
  742.         if (!bits_digested) {    /* no codewords after examining byte */
  743.         horiz_mode[prefix][byte] = (unsigned char) ACT_INCOMP;
  744.         horiz_mode_next_state[prefix][byte] = (unsigned char)
  745.             find_horiz_mode_prefix(curprefix, color);
  746.         }
  747.     }
  748. }
  749.  
  750. void
  751. build_uncomp_mode_tables(void)
  752. {
  753.     short prefix;
  754.  
  755.     /*
  756.      * Note: the first eight entries correspond to
  757.      * a null prefix and starting bit numbers 0-7.
  758.      */
  759.     uncomp_mode_prefix_count = 8;
  760.     for (prefix = 0; prefix < uncomp_mode_prefix_count; prefix++) {
  761.     short byte;
  762.     for (byte = 0; byte < 256; byte++) {
  763.         short firstbit;
  764.         short bit;
  765.         long curprefix;
  766.         char found_code = FALSE;
  767.  
  768.         if (prefix < 8) {
  769.         curprefix = 0L;
  770.         firstbit = prefix;
  771.         } else {
  772.         curprefix = uncomp_mode_prefix[prefix];
  773.         firstbit = 0;
  774.         }
  775.         for (bit = firstbit; bit < 8 && !found_code; bit++) {
  776.         short mode;
  777.  
  778.         if (bit_mask[bit] & byte)
  779.             curprefix = append_1(curprefix);
  780.         else
  781.             curprefix = append_0(curprefix);
  782.         mode = uncomp_mode_type(curprefix);
  783.         if (mode != -1) {
  784.             /*
  785.              * NOTE: if the bit number is 8, then the table
  786.              * entry will be zero, which indicates a new byte
  787.              * is to be fetched during the decoding process
  788.              */
  789.             found_code = TRUE;
  790.             uncomp_mode[prefix][byte] = (unsigned char) mode;
  791.             uncomp_mode_next_state[prefix][byte] = BASESTATE(bit+1);
  792.             break;
  793.         }
  794.         }
  795.         if (!found_code) {
  796.         uncomp_mode_next_state[prefix][byte] = (unsigned char)
  797.             find_uncomp_mode_prefix(curprefix);
  798.         /*
  799.          * This indicates to the decoder that
  800.          * no valid code has yet been identified.
  801.          */
  802.         uncomp_mode[prefix][byte] = UNCOMP_INCOMP;
  803.         }
  804.     }
  805.     }
  806. }
  807.