CODEWORD.CPP
上传用户:xr_qian
上传日期:2007-01-05
资源大小:443k
文件大小:22k
源码类别:

通讯/手机编程

开发平台:

DOS

  1. // ******************************************************************** //
  2. //                                                                      //
  3. //      CODEWORD.CPP                                                    //
  4. //      Copyright (c) 1993, Michael Holmes and Bob Flanders             //
  5. //      C++ Communication Utilities                                     //
  6. //                                                                      //
  7. //      This file contains the tables to support conversion between     //
  8. //      G3 fax codewords and a bitmapped array.                         //
  9. //                                                                      //
  10. //      The code_entry structure is used in converting from the         //
  11. //      G3 format to a bitmapped format.  The code_table variable       //
  12. //      is arranged as a two dimensional array.  The indices are on     //
  13. //      color (white and black) and number of bits.  Based on the       //
  14. //      current color being searched for and the number of bits         //
  15. //      retrieved from the input stream, the shorter arrays are         //
  16. //      searched.  If a match is found, the value element represents    //
  17. //      the number of pels for the this codeword.                       //
  18. //                                                                      //
  19. //      The encode_table structure is used to encode a bitmap into      //
  20. //      a G3 fax format data stream.  This table is indexed by the      //
  21. //      number of bits and their color.  The first 64 entries are       //
  22. //      direct translations. The remaining entries (make up codes)      //
  23. //      are used as a prefix to the first 64 values.                    //
  24. //                                                                      //
  25. // ******************************************************************** //
  26. struct  code_entry                          // codeword translation entries
  27.     {
  28.     unsigned
  29.     char code;                              // codeword (right justified)
  30.     int  value;                             // value of codeword
  31.     } code_bl2[] =                          // 2 bit black codewords
  32.         {
  33.         {    2,    3 },                         // 10
  34.         {    3,    2 },                         // 11
  35.         {    0,    0 }
  36.         },
  37.     code_bl3[] =                            // 3 bit black codewords
  38.         {
  39.         {    2,    1 },                         // 010
  40.         {    3,    4 },                         // 011
  41.         {    0,    0 }
  42.         },
  43.     code_wh4[] =                            // 4 bit white codewords
  44.         {
  45.         {    7,    2 },                         // 0111
  46.         {    8,    3 },                         // 1000
  47.         {   11,    4 },                         // 1011
  48.         {   12,    5 },                         // 1100
  49.         {   14,    6 },                         // 1110
  50.         {   15,    7 },                         // 1111
  51.         {    0,    0 }
  52.         },
  53.     code_bl4[] =                            // 4 bit black codewords
  54.         {
  55.         {    2,    6 },                         // 0010
  56.         {    3,    5 },                         // 0011
  57.         {    0,    0 }
  58.         },
  59.     code_wh5[] =                            // 5 bit white codewords
  60.         {
  61.         {    7,   10 },                         // 00111
  62.         {    8,   11 },                         // 01000
  63.         {   18,  128 },                         // 10010
  64.         {   19,    8 },                         // 10011
  65.         {   20,    9 },                         // 10100
  66.         {   27,   64 },                         // 11011
  67.         {    0,    0 }
  68.         },
  69.     code_bl5[] =                            // 5 bit black codewords
  70.         {
  71.         {    3,    7 },                         // 00011
  72.         {    0,    0 }
  73.         },
  74.     code_wh6[] =                            // 6 bit white codewords
  75.         {
  76.         {    3,   13 },                         // 000011
  77.         {    7,    1 },                         // 000111
  78.         {    8,   12 },                         // 001000
  79.         {   23,  192 },                         // 010111
  80.         {   24, 1664 },                         // 011000
  81.         {   42,   16 },                         // 101010
  82.         {   43,   17 },                         // 101011
  83.         {   52,   14 },                         // 110100
  84.         {   53,   15 },                         // 110101
  85.         {    0,    0 }
  86.         },
  87.     code_bl6[] =                            // 6 bit black codewords
  88.         {
  89.         {    4,    9 },                         // 000100
  90.         {    5,    8 },                         // 000101
  91.         {    0,    0 }
  92.         },
  93.     code_wh7[] =                            // 7 bit white codewords
  94.         {
  95.         {    3,   22 },                         // 0000011
  96.         {    4,   23 },                         // 0000100
  97.         {    8,   20 },                         // 0001000
  98.         {   12,   19 },                         // 0001100
  99.         {   19,   26 },                         // 0010011
  100.         {   23,   21 },                         // 0010111
  101.         {   24,   28 },                         // 0011000
  102.         {   36,   27 },                         // 0100100
  103.         {   39,   18 },                         // 0100111
  104.         {   40,   24 },                         // 0101000
  105.         {   43,   25 },                         // 0101011
  106.         {   55,  256 },                         // 0110111
  107.         {    0,    0 }
  108.         },
  109.     code_bl7[] =                            // 7 bit black codewords
  110.         {
  111.         {    4,   10 },                         // 0000100
  112.         {    5,   11 },                         // 0000101
  113.         {    7,   12 },                         // 0000111
  114.         {    0,    0 }
  115.         },
  116.     code_wh8[] =                            // 8 bit white codewords
  117.         {
  118.         {    2,   29 },                         // 00000010
  119.         {    3,   30 },                         // 00000011
  120.         {    4,   45 },                         // 00000100
  121.         {    5,   46 },                         // 00000101
  122.         {   10,   47 },                         // 00001010
  123.         {   11,   48 },                         // 00001011
  124.         {   18,   33 },                         // 00010010
  125.         {   19,   34 },                         // 00010011
  126.         {   20,   35 },                         // 00010100
  127.         {   21,   36 },                         // 00010101
  128.         {   22,   37 },                         // 00010110
  129.         {   23,   38 },                         // 00010111
  130.         {   26,   31 },                         // 00011010
  131.         {   27,   32 },                         // 00011011
  132.         {   36,   53 },                         // 00100100
  133.         {   37,   54 },                         // 00100101
  134.         {   40,   39 },                         // 00101000
  135.         {   41,   40 },                         // 00101001
  136.         {   42,   41 },                         // 00101010
  137.         {   43,   42 },                         // 00101011
  138.         {   44,   43 },                         // 00101100
  139.         {   45,   44 },                         // 00101101
  140.         {   50,   61 },                         // 00110010
  141.         {   51,   62 },                         // 00110011
  142.         {   52,   63 },                         // 00110100
  143.         {   53,    0 },                         // 00110101
  144.         {   54,  320 },                         // 00110110
  145.         {   55,  384 },                         // 00110111
  146.         {   74,   59 },                         // 01001010
  147.         {   75,   60 },                         // 01001011
  148.         {   82,   49 },                         // 01010010
  149.         {   83,   50 },                         // 01010011
  150.         {   84,   51 },                         // 01010100
  151.         {   85,   52 },                         // 01010101
  152.         {   88,   55 },                         // 01011000
  153.         {   89,   56 },                         // 01011001
  154.         {   90,   57 },                         // 01011010
  155.         {   91,   58 },                         // 01011011
  156.         {  100,  448 },                         // 01100100
  157.         {  101,  512 },                         // 01100101
  158.         {  103,  640 },                         // 01100111
  159.         {  104,  576 },                         // 01101000
  160.         {    0,    0 }
  161.         },
  162.     code_bl8[] =                            // 8 bit black codewords
  163.         {
  164.         {    4,   13 },                         // 00000100
  165.         {    7,   14 },                         // 00000111
  166.         {    0,    0 }
  167.         },
  168.     code_wh9[] =                            // 9 bit white codewords
  169.         {
  170.         {  152, 1472 },                         // 010011000
  171.         {  153, 1536 },                         // 010011001
  172.         {  154, 1600 },                         // 010011010
  173.         {  155, 1728 },                         // 010011011
  174.         {  204,  704 },                         // 011001100
  175.         {  205,  768 },                         // 011001101
  176.         {  210,  832 },                         // 011010010
  177.         {  211,  896 },                         // 011010011
  178.         {  212,  960 },                         // 011010100
  179.         {  213, 1024 },                         // 011010101
  180.         {  214, 1088 },                         // 011010110
  181.         {  215, 1152 },                         // 011010111
  182.         {  216, 1216 },                         // 011011000
  183.         {  217, 1280 },                         // 011011001
  184.         {  218, 1344 },                         // 011011010
  185.         {  219, 1408 },                         // 011011011
  186.         {    0,    0 }
  187.         },
  188.     code_bl9[] =                            // 9 bit black codewords
  189.         {
  190.         {   24,   15 },                         // 000011000
  191.         {    0,    0 }
  192.         },
  193.     code_bl10[] =                           // 10 bit black codewords
  194.         {
  195.         {    8,   18 },                         // 0000001000
  196.         {   15,   64 },                         // 0000001111
  197.         {   23,   16 },                         // 0000010111
  198.         {   24,   17 },                         // 0000011000
  199.         {   55,    0 },                         // 0000110111
  200.         {    0,    0 }
  201.         },
  202.     code_bl11[] =                           // 11 bit black codewords
  203.         {
  204.         {   23,   24 },                         // 00000010111
  205.         {   24,   25 },                         // 00000011000
  206.         {   40,   23 },                         // 00000101000
  207.         {   55,   22 },                         // 00000110111
  208.         {  103,   19 },                         // 00001100111
  209.         {  104,   20 },                         // 00001101000
  210.         {  108,   21 },                         // 00001101100
  211.         {    0,    0 }
  212.         },
  213.     code_bl12[] =                           // 12 bit black codewords
  214.         {
  215.         {   36,   52 },                         // 000000100100
  216.         {   39,   55 },                         // 000000100111
  217.         {   40,   56 },                         // 000000101000
  218.         {   43,   59 },                         // 000000101011
  219.         {   44,   60 },                         // 000000101100
  220.         {   51,  320 },                         // 000000110011
  221.         {   52,  384 },                         // 000000110100
  222.         {   53,  448 },                         // 000000110101
  223.         {   55,   53 },                         // 000000110111
  224.         {   56,   54 },                         // 000000111000
  225.         {   82,   50 },                         // 000001010010
  226.         {   83,   51 },                         // 000001010011
  227.         {   84,   44 },                         // 000001010100
  228.         {   85,   45 },                         // 000001010101
  229.         {   86,   46 },                         // 000001010110
  230.         {   87,   47 },                         // 000001010111
  231.         {   88,   57 },                         // 000001011000
  232.         {   89,   58 },                         // 000001011001
  233.         {   90,   61 },                         // 000001011010
  234.         {   91,  256 },                         // 000001011011
  235.         {  100,   48 },                         // 000001100100
  236.         {  101,   49 },                         // 000001100101
  237.         {  102,   62 },                         // 000001100110
  238.         {  103,   63 },                         // 000001100111
  239.         {  104,   30 },                         // 000001101000
  240.         {  105,   31 },                         // 000001101001
  241.         {  106,   32 },                         // 000001101010
  242.         {  107,   33 },                         // 000001101011
  243.         {  108,   40 },                         // 000001101100
  244.         {  109,   41 },                         // 000001101101
  245.         {  200,  128 },                         // 000011001000
  246.         {  201,  192 },                         // 000011001001
  247.         {  202,   26 },                         // 000011001010
  248.         {  203,   27 },                         // 000011001011
  249.         {  204,   28 },                         // 000011001100
  250.         {  205,   29 },                         // 000011001101
  251.         {  210,   34 },                         // 000011010010
  252.         {  211,   35 },                         // 000011010011
  253.         {  212,   36 },                         // 000011010100
  254.         {  213,   37 },                         // 000011010101
  255.         {  214,   38 },                         // 000011010110
  256.         {  215,   39 },                         // 000011010111
  257.         {  218,   42 },                         // 000011011010
  258.         {  219,   43 },                         // 000011011011
  259.         {    0,    0 }
  260.         },
  261.     code_bl13[] =                           // 13 bit black codewords
  262.         {
  263.         {   74,  640 },                         // 0000001001010
  264.         {   75,  704 },                         // 0000001001011
  265.         {   76,  768 },                         // 0000001001100
  266.         {   77,  832 },                         // 0000001001101
  267.         {   82, 1280 },                         // 0000001010010
  268.         {   83, 1344 },                         // 0000001010011
  269.         {   84, 1408 },                         // 0000001010100
  270.         {   85, 1472 },                         // 0000001010101
  271.         {   90, 1536 },                         // 0000001011010
  272.         {   91, 1600 },                         // 0000001011011
  273.         {  100, 1664 },                         // 0000001100100
  274.         {  101, 1728 },                         // 0000001100101
  275.         {  108,  512 },                         // 0000001101100
  276.         {  109,  576 },                         // 0000001101101
  277.         {  114,  896 },                         // 0000001110010
  278.         {  115,  960 },                         // 0000001110011
  279.         {  116, 1024 },                         // 0000001110100
  280.         {  117, 1088 },                         // 0000001110101
  281.         {  118, 1152 },                         // 0000001110110
  282.         {  119, 1216 },                         // 0000001110111
  283.         {    0,    0 }
  284.         },
  285.    *code_table[13][2] =                     // codeword table pointer
  286.         {
  287.         {        0,        0  },                // 1 bit entry
  288.         {        0, code_bl2  },                // 2 bit entries
  289.         {        0, code_bl3  },                // 3 bits entries
  290.         { code_wh4, code_bl4  },                // 4 bits entries
  291.         { code_wh5, code_bl5  },                // 5 bits entries
  292.         { code_wh6, code_bl6  },                // 6 bits entries
  293.         { code_wh7, code_bl7  },                // 7 bits entries
  294.         { code_wh8, code_bl8  },                // 8 bits entries
  295.         { code_wh9, code_bl9  },                // 9 bits entries
  296.         {        0, code_bl10 },                // 10 bits entries
  297.         {        0, code_bl11 },                // 11 bits entries
  298.         {        0, code_bl12 },                // 12 bits entries
  299.         {        0, code_bl13 },                // 13 bits entries
  300.         };
  301. struct  encode_entry                        // encode entry
  302.     {
  303.     unsigned
  304.     char code,                              // codeword (right justified)
  305.          bits;                              // length of codeword in bits
  306.     } encode_table[91][2] =                 // encode codeword table
  307.         {
  308.         { {   53,  8 }, {   55, 10 } },         //    0
  309.         { {    7,  6 }, {    2,  3 } },         //    1
  310.         { {    7,  4 }, {    3,  2 } },         //    2
  311.         { {    8,  4 }, {    2,  2 } },         //    3
  312.         { {   11,  4 }, {    3,  3 } },         //    4
  313.         { {   12,  4 }, {    3,  4 } },         //    5
  314.         { {   14,  4 }, {    2,  4 } },         //    6
  315.         { {   15,  4 }, {    3,  5 } },         //    7
  316.         { {   19,  5 }, {    5,  6 } },         //    8
  317.         { {   20,  5 }, {    4,  6 } },         //    9
  318.         { {    7,  5 }, {    4,  7 } },         //   10
  319.         { {    8,  5 }, {    5,  7 } },         //   11
  320.         { {    8,  6 }, {    7,  7 } },         //   12
  321.         { {    3,  6 }, {    4,  8 } },         //   13
  322.         { {   52,  6 }, {    7,  8 } },         //   14
  323.         { {   53,  6 }, {   24,  9 } },         //   15
  324.         { {   42,  6 }, {   23, 10 } },         //   16
  325.         { {   43,  6 }, {   24, 10 } },         //   17
  326.         { {   39,  7 }, {    8, 10 } },         //   18
  327.         { {   12,  7 }, {  103, 11 } },         //   19
  328.         { {    8,  7 }, {  104, 11 } },         //   20
  329.         { {   23,  7 }, {  108, 11 } },         //   21
  330.         { {    3,  7 }, {   55, 11 } },         //   22
  331.         { {    4,  7 }, {   40, 11 } },         //   23
  332.         { {   40,  7 }, {   23, 11 } },         //   24
  333.         { {   43,  7 }, {   24, 11 } },         //   25
  334.         { {   19,  7 }, {  202, 12 } },         //   26
  335.         { {   36,  7 }, {  203, 12 } },         //   27
  336.         { {   24,  7 }, {  204, 12 } },         //   28
  337.         { {    2,  8 }, {  205, 12 } },         //   29
  338.         { {    3,  8 }, {  104, 12 } },         //   30
  339.         { {   26,  8 }, {  105, 12 } },         //   31
  340.         { {   27,  8 }, {  106, 12 } },         //   32
  341.         { {   18,  8 }, {  107, 12 } },         //   33
  342.         { {   19,  8 }, {  210, 12 } },         //   34
  343.         { {   20,  8 }, {  211, 12 } },         //   35
  344.         { {   21,  8 }, {  212, 12 } },         //   36
  345.         { {   22,  8 }, {  213, 12 } },         //   37
  346.         { {   23,  8 }, {  214, 12 } },         //   38
  347.         { {   40,  8 }, {  215, 12 } },         //   39
  348.         { {   41,  8 }, {  108, 12 } },         //   40
  349.         { {   42,  8 }, {  109, 12 } },         //   41
  350.         { {   43,  8 }, {  218, 12 } },         //   42
  351.         { {   44,  8 }, {  219, 12 } },         //   43
  352.         { {   45,  8 }, {   84, 12 } },         //   44
  353.         { {    4,  8 }, {   85, 12 } },         //   45
  354.         { {    5,  8 }, {   86, 12 } },         //   46
  355.         { {   10,  8 }, {   87, 12 } },         //   47
  356.         { {   11,  8 }, {  100, 12 } },         //   48
  357.         { {   82,  8 }, {  101, 12 } },         //   49
  358.         { {   83,  8 }, {   82, 12 } },         //   50
  359.         { {   84,  8 }, {   83, 12 } },         //   51
  360.         { {   85,  8 }, {   36, 12 } },         //   52
  361.         { {   36,  8 }, {   55, 12 } },         //   53
  362.         { {   37,  8 }, {   56, 12 } },         //   54
  363.         { {   88,  8 }, {   39, 12 } },         //   55
  364.         { {   89,  8 }, {   40, 12 } },         //   56
  365.         { {   90,  8 }, {   88, 12 } },         //   57
  366.         { {   91,  8 }, {   89, 12 } },         //   58
  367.         { {   74,  8 }, {   43, 12 } },         //   59
  368.         { {   75,  8 }, {   44, 12 } },         //   60
  369.         { {   50,  8 }, {   90, 12 } },         //   61
  370.         { {   51,  8 }, {  102, 12 } },         //   62
  371.         { {   52,  8 }, {  103, 12 } },         //   63
  372.         { {   27,  5 }, {   15, 10 } },         //   64
  373.         { {   18,  5 }, {  200, 12 } },         //  128
  374.         { {   23,  6 }, {  201, 12 } },         //  192
  375.         { {   55,  7 }, {   91, 12 } },         //  256
  376.         { {   54,  8 }, {   51, 12 } },         //  320
  377.         { {   55,  8 }, {   52, 12 } },         //  384
  378.         { {  100,  8 }, {   53, 12 } },         //  448
  379.         { {  101,  8 }, {  108, 13 } },         //  512
  380.         { {  104,  8 }, {  109, 13 } },         //  576
  381.         { {  103,  8 }, {   74, 13 } },         //  640
  382.         { {  204,  9 }, {   75, 13 } },         //  704
  383.         { {  205,  9 }, {   76, 13 } },         //  768
  384.         { {  210,  9 }, {   77, 13 } },         //  832
  385.         { {  211,  9 }, {  114, 13 } },         //  896
  386.         { {  212,  9 }, {  115, 13 } },         //  960
  387.         { {  213,  9 }, {  116, 13 } },         // 1024
  388.         { {  214,  9 }, {  117, 13 } },         // 1088
  389.         { {  215,  9 }, {  118, 13 } },         // 1152
  390.         { {  216,  9 }, {  119, 13 } },         // 1216
  391.         { {  217,  9 }, {   82, 13 } },         // 1280
  392.         { {  218,  9 }, {   83, 13 } },         // 1344
  393.         { {  219,  9 }, {   84, 13 } },         // 1408
  394.         { {  152,  9 }, {   85, 13 } },         // 1472
  395.         { {  153,  9 }, {   90, 13 } },         // 1536
  396.         { {  154,  9 }, {   91, 13 } },         // 1600
  397.         { {   24,  6 }, {  100, 13 } },         // 1664
  398.         { {  155,  9 }, {  101, 13 } }          // 1728
  399.         };