• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kviewshell

ZPCodec.cpp

Go to the documentation of this file.
00001 //C-  -*- C++ -*-
00002 //C- -------------------------------------------------------------------
00003 //C- DjVuLibre-3.5
00004 //C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
00005 //C- Copyright (c) 2001  AT&T
00006 //C-
00007 //C- This software is subject to, and may be distributed under, the
00008 //C- GNU General Public License, Version 2. The license should have
00009 //C- accompanied the software or you may obtain a copy of the license
00010 //C- from the Free Software Foundation at http://www.fsf.org .
00011 //C-
00012 //C- This program is distributed in the hope that it will be useful,
00013 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //C- GNU General Public License for more details.
00016 //C- 
00017 //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
00018 //C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech 
00019 //C- Software authorized us to replace the original DjVu(r) Reference 
00020 //C- Library notice by the following text (see doc/lizard2002.djvu):
00021 //C-
00022 //C-  ------------------------------------------------------------------
00023 //C- | DjVu (r) Reference Library (v. 3.5)
00024 //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
00025 //C- | The DjVu Reference Library is protected by U.S. Pat. No.
00026 //C- | 6,058,214 and patents pending.
00027 //C- |
00028 //C- | This software is subject to, and may be distributed under, the
00029 //C- | GNU General Public License, Version 2. The license should have
00030 //C- | accompanied the software or you may obtain a copy of the license
00031 //C- | from the Free Software Foundation at http://www.fsf.org .
00032 //C- |
00033 //C- | The computer code originally released by LizardTech under this
00034 //C- | license and unmodified by other parties is deemed "the LIZARDTECH
00035 //C- | ORIGINAL CODE."  Subject to any third party intellectual property
00036 //C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
00037 //C- | non-exclusive license to make, use, sell, or otherwise dispose of 
00038 //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
00039 //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
00040 //C- | General Public License.   This grant only confers the right to 
00041 //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
00042 //C- | the extent such infringement is reasonably necessary to enable 
00043 //C- | recipient to make, have made, practice, sell, or otherwise dispose 
00044 //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
00045 //C- | any greater extent that may be necessary to utilize further 
00046 //C- | modifications or combinations.
00047 //C- |
00048 //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
00049 //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
00050 //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
00051 //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
00052 //C- +------------------------------------------------------------------
00053 // 
00054 // $Id: ZPCodec.cpp,v 1.10 2004/08/06 14:49:34 leonb Exp $
00055 // $Name: release_3_5_15 $
00056 
00057 #ifdef HAVE_CONFIG_H
00058 # include "config.h"
00059 #endif
00060 #if NEED_GNUG_PRAGMAS
00061 # pragma implementation
00062 #endif
00063 
00064 // From: Leon Bottou, 1/31/2002
00065 // Almost equal to my initial code.
00066 
00067 #include "ZPCodec.h"
00068 #include "ByteStream.h"
00069 #include "GException.h"
00070 #include <stdlib.h>
00071 #include <assert.h>
00072 #include <math.h>
00073 #include <stdio.h>
00074 
00075 
00076 #ifdef HAVE_NAMESPACES
00077 namespace DJVU {
00078 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
00079 }
00080 #endif
00081 #endif
00082 
00084 // CODER SPECIFICATION
00086 
00087 
00088 #ifndef ZPCODER
00089 #ifndef ZCODER
00090 #define ZPCODER
00091 #endif
00092 #endif
00093 #ifdef ZCODER
00094 
00095 // The ZCODER option is provided for documentation purposes only. The ZCODER
00096 // might come dangerously close to U.S. patent 5059976 (Mitsubishi).  This is
00097 // why we always use the ZPCODER, although it usually produces 1% larger files.
00098 #warning "The ZCODER may infringe non-LizardTech patent(s)."
00099 #warning "You should use the ZPCODER instead."
00100 #endif
00101 
00102 
00104 // ZP CODER DEFAULT ADAPTATION TABLE
00106 
00107 
00108 // See ZPCodec::ZPCodec to see how this
00109 // default table is modified when not
00110 // using the DjVu compatibility mode.
00111 
00112 
00113 static ZPCodec::Table default_ztable[256] = 
00114 {
00115 #ifdef ZPCODER
00116   /* This table has been designed for the ZPCoder
00117    * by running the following command in file 'zptable.sn':
00118    * (fast-crude (steady-mat 0.0035  0.0002) 260)))
00119    */
00120   { 0x8000,  0x0000,  84, 145 },    /* 000: p=0.500000 (    0,    0) */
00121   { 0x8000,  0x0000,   3,   4 },    /* 001: p=0.500000 (    0,    0) */
00122   { 0x8000,  0x0000,   4,   3 },    /* 002: p=0.500000 (    0,    0) */
00123   { 0x6bbd,  0x10a5,   5,   1 },    /* 003: p=0.465226 (    0,    0) */
00124   { 0x6bbd,  0x10a5,   6,   2 },    /* 004: p=0.465226 (    0,    0) */
00125   { 0x5d45,  0x1f28,   7,   3 },    /* 005: p=0.430708 (    0,    0) */
00126   { 0x5d45,  0x1f28,   8,   4 },    /* 006: p=0.430708 (    0,    0) */
00127   { 0x51b9,  0x2bd3,   9,   5 },    /* 007: p=0.396718 (    0,    0) */
00128   { 0x51b9,  0x2bd3,  10,   6 },    /* 008: p=0.396718 (    0,    0) */
00129   { 0x4813,  0x36e3,  11,   7 },    /* 009: p=0.363535 (    0,    0) */
00130   { 0x4813,  0x36e3,  12,   8 },    /* 010: p=0.363535 (    0,    0) */
00131   { 0x3fd5,  0x408c,  13,   9 },    /* 011: p=0.331418 (    0,    0) */
00132   { 0x3fd5,  0x408c,  14,  10 },    /* 012: p=0.331418 (    0,    0) */
00133   { 0x38b1,  0x48fd,  15,  11 },    /* 013: p=0.300585 (    0,    0) */
00134   { 0x38b1,  0x48fd,  16,  12 },    /* 014: p=0.300585 (    0,    0) */
00135   { 0x3275,  0x505d,  17,  13 },    /* 015: p=0.271213 (    0,    0) */
00136   { 0x3275,  0x505d,  18,  14 },    /* 016: p=0.271213 (    0,    0) */
00137   { 0x2cfd,  0x56d0,  19,  15 },    /* 017: p=0.243438 (    0,    0) */
00138   { 0x2cfd,  0x56d0,  20,  16 },    /* 018: p=0.243438 (    0,    0) */
00139   { 0x2825,  0x5c71,  21,  17 },    /* 019: p=0.217391 (    0,    0) */
00140   { 0x2825,  0x5c71,  22,  18 },    /* 020: p=0.217391 (    0,    0) */
00141   { 0x23ab,  0x615b,  23,  19 },    /* 021: p=0.193150 (    0,    0) */
00142   { 0x23ab,  0x615b,  24,  20 },    /* 022: p=0.193150 (    0,    0) */
00143   { 0x1f87,  0x65a5,  25,  21 },    /* 023: p=0.170728 (    0,    0) */
00144   { 0x1f87,  0x65a5,  26,  22 },    /* 024: p=0.170728 (    0,    0) */
00145   { 0x1bbb,  0x6962,  27,  23 },    /* 025: p=0.150158 (    0,    0) */
00146   { 0x1bbb,  0x6962,  28,  24 },    /* 026: p=0.150158 (    0,    0) */
00147   { 0x1845,  0x6ca2,  29,  25 },    /* 027: p=0.131418 (    0,    0) */
00148   { 0x1845,  0x6ca2,  30,  26 },    /* 028: p=0.131418 (    0,    0) */
00149   { 0x1523,  0x6f74,  31,  27 },    /* 029: p=0.114460 (    0,    0) */
00150   { 0x1523,  0x6f74,  32,  28 },    /* 030: p=0.114460 (    0,    0) */
00151   { 0x1253,  0x71e6,  33,  29 },    /* 031: p=0.099230 (    0,    0) */
00152   { 0x1253,  0x71e6,  34,  30 },    /* 032: p=0.099230 (    0,    0) */
00153   { 0x0fcf,  0x7404,  35,  31 },    /* 033: p=0.085611 (    0,    0) */
00154   { 0x0fcf,  0x7404,  36,  32 },    /* 034: p=0.085611 (    0,    0) */
00155   { 0x0d95,  0x75d6,  37,  33 },    /* 035: p=0.073550 (    0,    0) */
00156   { 0x0d95,  0x75d6,  38,  34 },    /* 036: p=0.073550 (    0,    0) */
00157   { 0x0b9d,  0x7768,  39,  35 },    /* 037: p=0.062888 (    0,    0) */
00158   { 0x0b9d,  0x7768,  40,  36 },    /* 038: p=0.062888 (    0,    0) */
00159   { 0x09e3,  0x78c2,  41,  37 },    /* 039: p=0.053539 (    0,    0) */
00160   { 0x09e3,  0x78c2,  42,  38 },    /* 040: p=0.053539 (    0,    0) */
00161   { 0x0861,  0x79ea,  43,  39 },    /* 041: p=0.045365 (    0,    0) */
00162   { 0x0861,  0x79ea,  44,  40 },    /* 042: p=0.045365 (    0,    0) */
00163   { 0x0711,  0x7ae7,  45,  41 },    /* 043: p=0.038272 (    0,    0) */
00164   { 0x0711,  0x7ae7,  46,  42 },    /* 044: p=0.038272 (    0,    0) */
00165   { 0x05f1,  0x7bbe,  47,  43 },    /* 045: p=0.032174 (    0,    0) */
00166   { 0x05f1,  0x7bbe,  48,  44 },    /* 046: p=0.032174 (    0,    0) */
00167   { 0x04f9,  0x7c75,  49,  45 },    /* 047: p=0.026928 (    0,    0) */
00168   { 0x04f9,  0x7c75,  50,  46 },    /* 048: p=0.026928 (    0,    0) */
00169   { 0x0425,  0x7d0f,  51,  47 },    /* 049: p=0.022444 (    0,    0) */
00170   { 0x0425,  0x7d0f,  52,  48 },    /* 050: p=0.022444 (    0,    0) */
00171   { 0x0371,  0x7d91,  53,  49 },    /* 051: p=0.018636 (    0,    0) */
00172   { 0x0371,  0x7d91,  54,  50 },    /* 052: p=0.018636 (    0,    0) */
00173   { 0x02d9,  0x7dfe,  55,  51 },    /* 053: p=0.015421 (    0,    0) */
00174   { 0x02d9,  0x7dfe,  56,  52 },    /* 054: p=0.015421 (    0,    0) */
00175   { 0x0259,  0x7e5a,  57,  53 },    /* 055: p=0.012713 (    0,    0) */
00176   { 0x0259,  0x7e5a,  58,  54 },    /* 056: p=0.012713 (    0,    0) */
00177   { 0x01ed,  0x7ea6,  59,  55 },    /* 057: p=0.010419 (    0,    0) */
00178   { 0x01ed,  0x7ea6,  60,  56 },    /* 058: p=0.010419 (    0,    0) */
00179   { 0x0193,  0x7ee6,  61,  57 },    /* 059: p=0.008525 (    0,    0) */
00180   { 0x0193,  0x7ee6,  62,  58 },    /* 060: p=0.008525 (    0,    0) */
00181   { 0x0149,  0x7f1a,  63,  59 },    /* 061: p=0.006959 (    0,    0) */
00182   { 0x0149,  0x7f1a,  64,  60 },    /* 062: p=0.006959 (    0,    0) */
00183   { 0x010b,  0x7f45,  65,  61 },    /* 063: p=0.005648 (    0,    0) */
00184   { 0x010b,  0x7f45,  66,  62 },    /* 064: p=0.005648 (    0,    0) */
00185   { 0x00d5,  0x7f6b,  67,  63 },    /* 065: p=0.004506 (    0,    0) */
00186   { 0x00d5,  0x7f6b,  68,  64 },    /* 066: p=0.004506 (    0,    0) */
00187   { 0x00a5,  0x7f8d,  69,  65 },    /* 067: p=0.003480 (    0,    0) */
00188   { 0x00a5,  0x7f8d,  70,  66 },    /* 068: p=0.003480 (    0,    0) */
00189   { 0x007b,  0x7faa,  71,  67 },    /* 069: p=0.002602 (    0,    0) */
00190   { 0x007b,  0x7faa,  72,  68 },    /* 070: p=0.002602 (    0,    0) */
00191   { 0x0057,  0x7fc3,  73,  69 },    /* 071: p=0.001843 (    0,    0) */
00192   { 0x0057,  0x7fc3,  74,  70 },    /* 072: p=0.001843 (    0,    0) */
00193   { 0x003b,  0x7fd7,  75,  71 },    /* 073: p=0.001248 (    0,    0) */
00194   { 0x003b,  0x7fd7,  76,  72 },    /* 074: p=0.001248 (    0,    0) */
00195   { 0x0023,  0x7fe7,  77,  73 },    /* 075: p=0.000749 (    0,    0) */
00196   { 0x0023,  0x7fe7,  78,  74 },    /* 076: p=0.000749 (    0,    0) */
00197   { 0x0013,  0x7ff2,  79,  75 },    /* 077: p=0.000402 (    0,    0) */
00198   { 0x0013,  0x7ff2,  80,  76 },    /* 078: p=0.000402 (    0,    0) */
00199   { 0x0007,  0x7ffa,  81,  77 },    /* 079: p=0.000153 (    0,    0) */
00200   { 0x0007,  0x7ffa,  82,  78 },    /* 080: p=0.000153 (    0,    0) */
00201   { 0x0001,  0x7fff,  81,  79 },    /* 081: p=0.000027 (    0,    0) */
00202   { 0x0001,  0x7fff,  82,  80 },    /* 082: p=0.000027 (    0,    0) */
00203   { 0x5695,  0x0000,   9,  85 },    /* 083: p=0.411764 (    2,    3) */
00204   { 0x24ee,  0x0000,  86, 226 },    /* 084: p=0.199988 (    1,    0) */
00205   { 0x8000,  0x0000,   5,   6 },    /* 085: p=0.500000 (    3,    3) */
00206   { 0x0d30,  0x0000,  88, 176 },    /* 086: p=0.071422 (    4,    0) */
00207   { 0x481a,  0x0000,  89, 143 },    /* 087: p=0.363634 (    1,    2) */
00208   { 0x0481,  0x0000,  90, 138 },    /* 088: p=0.024388 (   13,    0) */
00209   { 0x3579,  0x0000,  91, 141 },    /* 089: p=0.285711 (    1,    3) */
00210   { 0x017a,  0x0000,  92, 112 },    /* 090: p=0.007999 (   41,    0) */
00211   { 0x24ef,  0x0000,  93, 135 },    /* 091: p=0.199997 (    1,    5) */
00212   { 0x007b,  0x0000,  94, 104 },    /* 092: p=0.002611 (  127,    0) */
00213   { 0x1978,  0x0000,  95, 133 },    /* 093: p=0.137929 (    1,    8) */
00214   { 0x0028,  0x0000,  96, 100 },    /* 094: p=0.000849 (  392,    0) */
00215   { 0x10ca,  0x0000,  97, 129 },    /* 095: p=0.090907 (    1,   13) */
00216   { 0x000d,  0x0000,  82,  98 },    /* 096: p=0.000276 ( 1208,    0) */
00217   { 0x0b5d,  0x0000,  99, 127 },    /* 097: p=0.061537 (    1,   20) */
00218   { 0x0034,  0x0000,  76,  72 },    /* 098: p=0.001102 ( 1208,    1) */
00219   { 0x078a,  0x0000, 101, 125 },    /* 099: p=0.040815 (    1,   31) */
00220   { 0x00a0,  0x0000,  70, 102 },    /* 100: p=0.003387 (  392,    1) */
00221   { 0x050f,  0x0000, 103, 123 },    /* 101: p=0.027397 (    1,   47) */
00222   { 0x0117,  0x0000,  66,  60 },    /* 102: p=0.005912 (  392,    2) */
00223   { 0x0358,  0x0000, 105, 121 },    /* 103: p=0.018099 (    1,   72) */
00224   { 0x01ea,  0x0000, 106, 110 },    /* 104: p=0.010362 (  127,    1) */
00225   { 0x0234,  0x0000, 107, 119 },    /* 105: p=0.011940 (    1,  110) */
00226   { 0x0144,  0x0000,  66, 108 },    /* 106: p=0.006849 (  193,    1) */
00227   { 0x0173,  0x0000, 109, 117 },    /* 107: p=0.007858 (    1,  168) */
00228   { 0x0234,  0x0000,  60,  54 },    /* 108: p=0.011925 (  193,    2) */
00229   { 0x00f5,  0x0000, 111, 115 },    /* 109: p=0.005175 (    1,  256) */
00230   { 0x0353,  0x0000,  56,  48 },    /* 110: p=0.017995 (  127,    2) */
00231   { 0x00a1,  0x0000,  69, 113 },    /* 111: p=0.003413 (    1,  389) */
00232   { 0x05c5,  0x0000, 114, 134 },    /* 112: p=0.031249 (   41,    1) */
00233   { 0x011a,  0x0000,  65,  59 },    /* 113: p=0.005957 (    2,  389) */
00234   { 0x03cf,  0x0000, 116, 132 },    /* 114: p=0.020618 (   63,    1) */
00235   { 0x01aa,  0x0000,  61,  55 },    /* 115: p=0.009020 (    2,  256) */
00236   { 0x0285,  0x0000, 118, 130 },    /* 116: p=0.013652 (   96,    1) */
00237   { 0x0286,  0x0000,  57,  51 },    /* 117: p=0.013672 (    2,  168) */
00238   { 0x01ab,  0x0000, 120, 128 },    /* 118: p=0.009029 (  146,    1) */
00239   { 0x03d3,  0x0000,  53,  47 },    /* 119: p=0.020710 (    2,  110) */
00240   { 0x011a,  0x0000, 122, 126 },    /* 120: p=0.005961 (  222,    1) */
00241   { 0x05c5,  0x0000,  49,  41 },    /* 121: p=0.031250 (    2,   72) */
00242   { 0x00ba,  0x0000, 124,  62 },    /* 122: p=0.003925 (  338,    1) */
00243   { 0x08ad,  0x0000,  43,  37 },    /* 123: p=0.046979 (    2,   47) */
00244   { 0x007a,  0x0000,  72,  66 },    /* 124: p=0.002586 (  514,    1) */
00245   { 0x0ccc,  0x0000,  39,  31 },    /* 125: p=0.069306 (    2,   31) */
00246   { 0x01eb,  0x0000,  60,  54 },    /* 126: p=0.010386 (  222,    2) */
00247   { 0x1302,  0x0000,  33,  25 },    /* 127: p=0.102940 (    2,   20) */
00248   { 0x02e6,  0x0000,  56,  50 },    /* 128: p=0.015695 (  146,    2) */
00249   { 0x1b81,  0x0000,  29, 131 },    /* 129: p=0.148935 (    2,   13) */
00250   { 0x045e,  0x0000,  52,  46 },    /* 130: p=0.023648 (   96,    2) */
00251   { 0x24ef,  0x0000,  23,  17 },    /* 131: p=0.199999 (    3,   13) */
00252   { 0x0690,  0x0000,  48,  40 },    /* 132: p=0.035533 (   63,    2) */
00253   { 0x2865,  0x0000,  23,  15 },    /* 133: p=0.218748 (    2,    8) */
00254   { 0x09de,  0x0000,  42, 136 },    /* 134: p=0.053434 (   41,    2) */
00255   { 0x3987,  0x0000, 137,   7 },    /* 135: p=0.304346 (    2,    5) */
00256   { 0x0dc8,  0x0000,  38,  32 },    /* 136: p=0.074626 (   41,    3) */
00257   { 0x2c99,  0x0000,  21, 139 },    /* 137: p=0.241378 (    2,    7) */
00258   { 0x10ca,  0x0000, 140, 172 },    /* 138: p=0.090907 (   13,    1) */
00259   { 0x3b5f,  0x0000,  15,   9 },    /* 139: p=0.312499 (    3,    7) */
00260   { 0x0b5d,  0x0000, 142, 170 },    /* 140: p=0.061537 (   20,    1) */
00261   { 0x5695,  0x0000,   9,  85 },    /* 141: p=0.411764 (    2,    3) */
00262   { 0x078a,  0x0000, 144, 168 },    /* 142: p=0.040815 (   31,    1) */
00263   { 0x8000,  0x0000, 141, 248 },    /* 143: p=0.500000 (    2,    2) */
00264   { 0x050f,  0x0000, 146, 166 },    /* 144: p=0.027397 (   47,    1) */
00265   { 0x24ee,  0x0000, 147, 247 },    /* 145: p=0.199988 (    0,    1) */
00266   { 0x0358,  0x0000, 148, 164 },    /* 146: p=0.018099 (   72,    1) */
00267   { 0x0d30,  0x0000, 149, 197 },    /* 147: p=0.071422 (    0,    4) */
00268   { 0x0234,  0x0000, 150, 162 },    /* 148: p=0.011940 (  110,    1) */
00269   { 0x0481,  0x0000, 151,  95 },    /* 149: p=0.024388 (    0,   13) */
00270   { 0x0173,  0x0000, 152, 160 },    /* 150: p=0.007858 (  168,    1) */
00271   { 0x017a,  0x0000, 153, 173 },    /* 151: p=0.007999 (    0,   41) */
00272   { 0x00f5,  0x0000, 154, 158 },    /* 152: p=0.005175 (  256,    1) */
00273   { 0x007b,  0x0000, 155, 165 },    /* 153: p=0.002611 (    0,  127) */
00274   { 0x00a1,  0x0000,  70, 156 },    /* 154: p=0.003413 (  389,    1) */
00275   { 0x0028,  0x0000, 157, 161 },    /* 155: p=0.000849 (    0,  392) */
00276   { 0x011a,  0x0000,  66,  60 },    /* 156: p=0.005957 (  389,    2) */
00277   { 0x000d,  0x0000,  81, 159 },    /* 157: p=0.000276 (    0, 1208) */
00278   { 0x01aa,  0x0000,  62,  56 },    /* 158: p=0.009020 (  256,    2) */
00279   { 0x0034,  0x0000,  75,  71 },    /* 159: p=0.001102 (    1, 1208) */
00280   { 0x0286,  0x0000,  58,  52 },    /* 160: p=0.013672 (  168,    2) */
00281   { 0x00a0,  0x0000,  69, 163 },    /* 161: p=0.003387 (    1,  392) */
00282   { 0x03d3,  0x0000,  54,  48 },    /* 162: p=0.020710 (  110,    2) */
00283   { 0x0117,  0x0000,  65,  59 },    /* 163: p=0.005912 (    2,  392) */
00284   { 0x05c5,  0x0000,  50,  42 },    /* 164: p=0.031250 (   72,    2) */
00285   { 0x01ea,  0x0000, 167, 171 },    /* 165: p=0.010362 (    1,  127) */
00286   { 0x08ad,  0x0000,  44,  38 },    /* 166: p=0.046979 (   47,    2) */
00287   { 0x0144,  0x0000,  65, 169 },    /* 167: p=0.006849 (    1,  193) */
00288   { 0x0ccc,  0x0000,  40,  32 },    /* 168: p=0.069306 (   31,    2) */
00289   { 0x0234,  0x0000,  59,  53 },    /* 169: p=0.011925 (    2,  193) */
00290   { 0x1302,  0x0000,  34,  26 },    /* 170: p=0.102940 (   20,    2) */
00291   { 0x0353,  0x0000,  55,  47 },    /* 171: p=0.017995 (    2,  127) */
00292   { 0x1b81,  0x0000,  30, 174 },    /* 172: p=0.148935 (   13,    2) */
00293   { 0x05c5,  0x0000, 175, 193 },    /* 173: p=0.031249 (    1,   41) */
00294   { 0x24ef,  0x0000,  24,  18 },    /* 174: p=0.199999 (   13,    3) */
00295   { 0x03cf,  0x0000, 177, 191 },    /* 175: p=0.020618 (    1,   63) */
00296   { 0x2b74,  0x0000, 178, 222 },    /* 176: p=0.235291 (    4,    1) */
00297   { 0x0285,  0x0000, 179, 189 },    /* 177: p=0.013652 (    1,   96) */
00298   { 0x201d,  0x0000, 180, 218 },    /* 178: p=0.173910 (    6,    1) */
00299   { 0x01ab,  0x0000, 181, 187 },    /* 179: p=0.009029 (    1,  146) */
00300   { 0x1715,  0x0000, 182, 216 },    /* 180: p=0.124998 (    9,    1) */
00301   { 0x011a,  0x0000, 183, 185 },    /* 181: p=0.005961 (    1,  222) */
00302   { 0x0fb7,  0x0000, 184, 214 },    /* 182: p=0.085105 (   14,    1) */
00303   { 0x00ba,  0x0000,  69,  61 },    /* 183: p=0.003925 (    1,  338) */
00304   { 0x0a67,  0x0000, 186, 212 },    /* 184: p=0.056337 (   22,    1) */
00305   { 0x01eb,  0x0000,  59,  53 },    /* 185: p=0.010386 (    2,  222) */
00306   { 0x06e7,  0x0000, 188, 210 },    /* 186: p=0.037382 (   34,    1) */
00307   { 0x02e6,  0x0000,  55,  49 },    /* 187: p=0.015695 (    2,  146) */
00308   { 0x0496,  0x0000, 190, 208 },    /* 188: p=0.024844 (   52,    1) */
00309   { 0x045e,  0x0000,  51,  45 },    /* 189: p=0.023648 (    2,   96) */
00310   { 0x030d,  0x0000, 192, 206 },    /* 190: p=0.016529 (   79,    1) */
00311   { 0x0690,  0x0000,  47,  39 },    /* 191: p=0.035533 (    2,   63) */
00312   { 0x0206,  0x0000, 194, 204 },    /* 192: p=0.010959 (  120,    1) */
00313   { 0x09de,  0x0000,  41, 195 },    /* 193: p=0.053434 (    2,   41) */
00314   { 0x0155,  0x0000, 196, 202 },    /* 194: p=0.007220 (  183,    1) */
00315   { 0x0dc8,  0x0000,  37,  31 },    /* 195: p=0.074626 (    3,   41) */
00316   { 0x00e1,  0x0000, 198, 200 },    /* 196: p=0.004750 (  279,    1) */
00317   { 0x2b74,  0x0000, 199, 243 },    /* 197: p=0.235291 (    1,    4) */
00318   { 0x0094,  0x0000,  72,  64 },    /* 198: p=0.003132 (  424,    1) */
00319   { 0x201d,  0x0000, 201, 239 },    /* 199: p=0.173910 (    1,    6) */
00320   { 0x0188,  0x0000,  62,  56 },    /* 200: p=0.008284 (  279,    2) */
00321   { 0x1715,  0x0000, 203, 237 },    /* 201: p=0.124998 (    1,    9) */
00322   { 0x0252,  0x0000,  58,  52 },    /* 202: p=0.012567 (  183,    2) */
00323   { 0x0fb7,  0x0000, 205, 235 },    /* 203: p=0.085105 (    1,   14) */
00324   { 0x0383,  0x0000,  54,  48 },    /* 204: p=0.019021 (  120,    2) */
00325   { 0x0a67,  0x0000, 207, 233 },    /* 205: p=0.056337 (    1,   22) */
00326   { 0x0547,  0x0000,  50,  44 },    /* 206: p=0.028571 (   79,    2) */
00327   { 0x06e7,  0x0000, 209, 231 },    /* 207: p=0.037382 (    1,   34) */
00328   { 0x07e2,  0x0000,  46,  38 },    /* 208: p=0.042682 (   52,    2) */
00329   { 0x0496,  0x0000, 211, 229 },    /* 209: p=0.024844 (    1,   52) */
00330   { 0x0bc0,  0x0000,  40,  34 },    /* 210: p=0.063636 (   34,    2) */
00331   { 0x030d,  0x0000, 213, 227 },    /* 211: p=0.016529 (    1,   79) */
00332   { 0x1178,  0x0000,  36,  28 },    /* 212: p=0.094593 (   22,    2) */
00333   { 0x0206,  0x0000, 215, 225 },    /* 213: p=0.010959 (    1,  120) */
00334   { 0x19da,  0x0000,  30,  22 },    /* 214: p=0.139999 (   14,    2) */
00335   { 0x0155,  0x0000, 217, 223 },    /* 215: p=0.007220 (    1,  183) */
00336   { 0x24ef,  0x0000,  26,  16 },    /* 216: p=0.199998 (    9,    2) */
00337   { 0x00e1,  0x0000, 219, 221 },    /* 217: p=0.004750 (    1,  279) */
00338   { 0x320e,  0x0000,  20, 220 },    /* 218: p=0.269229 (    6,    2) */
00339   { 0x0094,  0x0000,  71,  63 },    /* 219: p=0.003132 (    1,  424) */
00340   { 0x432a,  0x0000,  14,   8 },    /* 220: p=0.344827 (    6,    3) */
00341   { 0x0188,  0x0000,  61,  55 },    /* 221: p=0.008284 (    2,  279) */
00342   { 0x447d,  0x0000,  14, 224 },    /* 222: p=0.349998 (    4,    2) */
00343   { 0x0252,  0x0000,  57,  51 },    /* 223: p=0.012567 (    2,  183) */
00344   { 0x5ece,  0x0000,   8,   2 },    /* 224: p=0.434782 (    4,    3) */
00345   { 0x0383,  0x0000,  53,  47 },    /* 225: p=0.019021 (    2,  120) */
00346   { 0x8000,  0x0000, 228,  87 },    /* 226: p=0.500000 (    1,    1) */
00347   { 0x0547,  0x0000,  49,  43 },    /* 227: p=0.028571 (    2,   79) */
00348   { 0x481a,  0x0000, 230, 246 },    /* 228: p=0.363634 (    2,    1) */
00349   { 0x07e2,  0x0000,  45,  37 },    /* 229: p=0.042682 (    2,   52) */
00350   { 0x3579,  0x0000, 232, 244 },    /* 230: p=0.285711 (    3,    1) */
00351   { 0x0bc0,  0x0000,  39,  33 },    /* 231: p=0.063636 (    2,   34) */
00352   { 0x24ef,  0x0000, 234, 238 },    /* 232: p=0.199997 (    5,    1) */
00353   { 0x1178,  0x0000,  35,  27 },    /* 233: p=0.094593 (    2,   22) */
00354   { 0x1978,  0x0000, 138, 236 },    /* 234: p=0.137929 (    8,    1) */
00355   { 0x19da,  0x0000,  29,  21 },    /* 235: p=0.139999 (    2,   14) */
00356   { 0x2865,  0x0000,  24,  16 },    /* 236: p=0.218748 (    8,    2) */
00357   { 0x24ef,  0x0000,  25,  15 },    /* 237: p=0.199998 (    2,    9) */
00358   { 0x3987,  0x0000, 240,   8 },    /* 238: p=0.304346 (    5,    2) */
00359   { 0x320e,  0x0000,  19, 241 },    /* 239: p=0.269229 (    2,    6) */
00360   { 0x2c99,  0x0000,  22, 242 },    /* 240: p=0.241378 (    7,    2) */
00361   { 0x432a,  0x0000,  13,   7 },    /* 241: p=0.344827 (    3,    6) */
00362   { 0x3b5f,  0x0000,  16,  10 },    /* 242: p=0.312499 (    7,    3) */
00363   { 0x447d,  0x0000,  13, 245 },    /* 243: p=0.349998 (    2,    4) */
00364   { 0x5695,  0x0000,  10,   2 },    /* 244: p=0.411764 (    3,    2) */
00365   { 0x5ece,  0x0000,   7,   1 },    /* 245: p=0.434782 (    3,    4) */
00366   { 0x8000,  0x0000, 244,  83 },    /* 246: p=0.500000 (    2,    2) */
00367   { 0x8000,  0x0000, 249, 250 },    /* 247: p=0.500000 (    1,    1) */
00368   { 0x5695,  0x0000,  10,   2 },    /* 248: p=0.411764 (    3,    2) */
00369   { 0x481a,  0x0000,  89, 143 },    /* 249: p=0.363634 (    1,    2) */
00370   { 0x481a,  0x0000, 230, 246 },    /* 250: p=0.363634 (    2,    1) */
00371 #endif
00372 #ifdef ZCODER
00373   /* This table has been designed for the ZCoder
00374    * by running the following command in file 'ztable2.sn':
00375    * (fast-crude (steady-mat 0.0035  0.0002) 260)))
00376    */
00377   { 0x8000,  0x0000,  84, 139 },    /* 000: p=0.500000 (    0,    0) */
00378   { 0x8000,  0x0000,   3,   4 },    /* 001: p=0.500000 (    0,    0) */
00379   { 0x8000,  0x0000,   4,   3 },    /* 002: p=0.500000 (    0,    0) */
00380   { 0x7399,  0x10a5,   5,   1 },    /* 003: p=0.465226 (    0,    0) */
00381   { 0x7399,  0x10a5,   6,   2 },    /* 004: p=0.465226 (    0,    0) */
00382   { 0x6813,  0x1f28,   7,   3 },    /* 005: p=0.430708 (    0,    0) */
00383   { 0x6813,  0x1f28,   8,   4 },    /* 006: p=0.430708 (    0,    0) */
00384   { 0x5d65,  0x2bd3,   9,   5 },    /* 007: p=0.396718 (    0,    0) */
00385   { 0x5d65,  0x2bd3,  10,   6 },    /* 008: p=0.396718 (    0,    0) */
00386   { 0x5387,  0x36e3,  11,   7 },    /* 009: p=0.363535 (    0,    0) */
00387   { 0x5387,  0x36e3,  12,   8 },    /* 010: p=0.363535 (    0,    0) */
00388   { 0x4a73,  0x408c,  13,   9 },    /* 011: p=0.331418 (    0,    0) */
00389   { 0x4a73,  0x408c,  14,  10 },    /* 012: p=0.331418 (    0,    0) */
00390   { 0x421f,  0x48fe,  15,  11 },    /* 013: p=0.300562 (    0,    0) */
00391   { 0x421f,  0x48fe,  16,  12 },    /* 014: p=0.300562 (    0,    0) */
00392   { 0x3a85,  0x5060,  17,  13 },    /* 015: p=0.271166 (    0,    0) */
00393   { 0x3a85,  0x5060,  18,  14 },    /* 016: p=0.271166 (    0,    0) */
00394   { 0x339b,  0x56d3,  19,  15 },    /* 017: p=0.243389 (    0,    0) */
00395   { 0x339b,  0x56d3,  20,  16 },    /* 018: p=0.243389 (    0,    0) */
00396   { 0x2d59,  0x5c73,  21,  17 },    /* 019: p=0.217351 (    0,    0) */
00397   { 0x2d59,  0x5c73,  22,  18 },    /* 020: p=0.217351 (    0,    0) */
00398   { 0x27b3,  0x615e,  23,  19 },    /* 021: p=0.193091 (    0,    0) */
00399   { 0x27b3,  0x615e,  24,  20 },    /* 022: p=0.193091 (    0,    0) */
00400   { 0x22a1,  0x65a7,  25,  21 },    /* 023: p=0.170683 (    0,    0) */
00401   { 0x22a1,  0x65a7,  26,  22 },    /* 024: p=0.170683 (    0,    0) */
00402   { 0x1e19,  0x6963,  27,  23 },    /* 025: p=0.150134 (    0,    0) */
00403   { 0x1e19,  0x6963,  28,  24 },    /* 026: p=0.150134 (    0,    0) */
00404   { 0x1a0f,  0x6ca3,  29,  25 },    /* 027: p=0.131397 (    0,    0) */
00405   { 0x1a0f,  0x6ca3,  30,  26 },    /* 028: p=0.131397 (    0,    0) */
00406   { 0x167b,  0x6f75,  31,  27 },    /* 029: p=0.114441 (    0,    0) */
00407   { 0x167b,  0x6f75,  32,  28 },    /* 030: p=0.114441 (    0,    0) */
00408   { 0x1353,  0x71e6,  33,  29 },    /* 031: p=0.099214 (    0,    0) */
00409   { 0x1353,  0x71e6,  34,  30 },    /* 032: p=0.099214 (    0,    0) */
00410   { 0x108d,  0x7403,  35,  31 },    /* 033: p=0.085616 (    0,    0) */
00411   { 0x108d,  0x7403,  36,  32 },    /* 034: p=0.085616 (    0,    0) */
00412   { 0x0e1f,  0x75d7,  37,  33 },    /* 035: p=0.073525 (    0,    0) */
00413   { 0x0e1f,  0x75d7,  38,  34 },    /* 036: p=0.073525 (    0,    0) */
00414   { 0x0c01,  0x7769,  39,  35 },    /* 037: p=0.062871 (    0,    0) */
00415   { 0x0c01,  0x7769,  40,  36 },    /* 038: p=0.062871 (    0,    0) */
00416   { 0x0a2b,  0x78c2,  41,  37 },    /* 039: p=0.053524 (    0,    0) */
00417   { 0x0a2b,  0x78c2,  42,  38 },    /* 040: p=0.053524 (    0,    0) */
00418   { 0x0895,  0x79ea,  43,  39 },    /* 041: p=0.045374 (    0,    0) */
00419   { 0x0895,  0x79ea,  44,  40 },    /* 042: p=0.045374 (    0,    0) */
00420   { 0x0737,  0x7ae7,  45,  41 },    /* 043: p=0.038280 (    0,    0) */
00421   { 0x0737,  0x7ae7,  46,  42 },    /* 044: p=0.038280 (    0,    0) */
00422   { 0x060b,  0x7bbe,  47,  43 },    /* 045: p=0.032175 (    0,    0) */
00423   { 0x060b,  0x7bbe,  48,  44 },    /* 046: p=0.032175 (    0,    0) */
00424   { 0x050b,  0x7c75,  49,  45 },    /* 047: p=0.026926 (    0,    0) */
00425   { 0x050b,  0x7c75,  50,  46 },    /* 048: p=0.026926 (    0,    0) */
00426   { 0x0431,  0x7d10,  51,  47 },    /* 049: p=0.022430 (    0,    0) */
00427   { 0x0431,  0x7d10,  52,  48 },    /* 050: p=0.022430 (    0,    0) */
00428   { 0x0379,  0x7d92,  53,  49 },    /* 051: p=0.018623 (    0,    0) */
00429   { 0x0379,  0x7d92,  54,  50 },    /* 052: p=0.018623 (    0,    0) */
00430   { 0x02dd,  0x7dff,  55,  51 },    /* 053: p=0.015386 (    0,    0) */
00431   { 0x02dd,  0x7dff,  56,  52 },    /* 054: p=0.015386 (    0,    0) */
00432   { 0x025b,  0x7e5b,  57,  53 },    /* 055: p=0.012671 (    0,    0) */
00433   { 0x025b,  0x7e5b,  58,  54 },    /* 056: p=0.012671 (    0,    0) */
00434   { 0x01ef,  0x7ea7,  59,  55 },    /* 057: p=0.010414 (    0,    0) */
00435   { 0x01ef,  0x7ea7,  60,  56 },    /* 058: p=0.010414 (    0,    0) */
00436   { 0x0195,  0x7ee6,  61,  57 },    /* 059: p=0.008529 (    0,    0) */
00437   { 0x0195,  0x7ee6,  62,  58 },    /* 060: p=0.008529 (    0,    0) */
00438   { 0x0149,  0x7f1b,  63,  59 },    /* 061: p=0.006935 (    0,    0) */
00439   { 0x0149,  0x7f1b,  64,  60 },    /* 062: p=0.006935 (    0,    0) */
00440   { 0x010b,  0x7f46,  65,  61 },    /* 063: p=0.005631 (    0,    0) */
00441   { 0x010b,  0x7f46,  66,  62 },    /* 064: p=0.005631 (    0,    0) */
00442   { 0x00d5,  0x7f6c,  67,  63 },    /* 065: p=0.004495 (    0,    0) */
00443   { 0x00d5,  0x7f6c,  68,  64 },    /* 066: p=0.004495 (    0,    0) */
00444   { 0x00a5,  0x7f8d,  69,  65 },    /* 067: p=0.003484 (    0,    0) */
00445   { 0x00a5,  0x7f8d,  70,  66 },    /* 068: p=0.003484 (    0,    0) */
00446   { 0x007b,  0x7faa,  71,  67 },    /* 069: p=0.002592 (    0,    0) */
00447   { 0x007b,  0x7faa,  72,  68 },    /* 070: p=0.002592 (    0,    0) */
00448   { 0x0057,  0x7fc3,  73,  69 },    /* 071: p=0.001835 (    0,    0) */
00449   { 0x0057,  0x7fc3,  74,  70 },    /* 072: p=0.001835 (    0,    0) */
00450   { 0x0039,  0x7fd8,  75,  71 },    /* 073: p=0.001211 (    0,    0) */
00451   { 0x0039,  0x7fd8,  76,  72 },    /* 074: p=0.001211 (    0,    0) */
00452   { 0x0023,  0x7fe7,  77,  73 },    /* 075: p=0.000740 (    0,    0) */
00453   { 0x0023,  0x7fe7,  78,  74 },    /* 076: p=0.000740 (    0,    0) */
00454   { 0x0013,  0x7ff2,  79,  75 },    /* 077: p=0.000402 (    0,    0) */
00455   { 0x0013,  0x7ff2,  80,  76 },    /* 078: p=0.000402 (    0,    0) */
00456   { 0x0007,  0x7ffa,  81,  77 },    /* 079: p=0.000153 (    0,    0) */
00457   { 0x0007,  0x7ffa,  82,  78 },    /* 080: p=0.000153 (    0,    0) */
00458   { 0x0001,  0x7fff,  81,  79 },    /* 081: p=0.000027 (    0,    0) */
00459   { 0x0001,  0x7fff,  82,  80 },    /* 082: p=0.000027 (    0,    0) */
00460   { 0x620b,  0x0000,   9,  85 },    /* 083: p=0.411764 (    2,    3) */
00461   { 0x294a,  0x0000,  86, 216 },    /* 084: p=0.199988 (    1,    0) */
00462   { 0x8000,  0x0000,   5,   6 },    /* 085: p=0.500000 (    3,    3) */
00463   { 0x0db3,  0x0000,  88, 168 },    /* 086: p=0.071422 (    4,    0) */
00464   { 0x538e,  0x0000,  89, 137 },    /* 087: p=0.363634 (    1,    2) */
00465   { 0x0490,  0x0000,  90, 134 },    /* 088: p=0.024388 (   13,    0) */
00466   { 0x3e3e,  0x0000,  91, 135 },    /* 089: p=0.285711 (    1,    3) */
00467   { 0x017c,  0x0000,  92, 112 },    /* 090: p=0.007999 (   41,    0) */
00468   { 0x294a,  0x0000,  93, 133 },    /* 091: p=0.199997 (    1,    5) */
00469   { 0x007c,  0x0000,  94, 104 },    /* 092: p=0.002611 (  127,    0) */
00470   { 0x1b75,  0x0000,  95, 131 },    /* 093: p=0.137929 (    1,    8) */
00471   { 0x0028,  0x0000,  96, 100 },    /* 094: p=0.000849 (  392,    0) */
00472   { 0x12fc,  0x0000,  97, 129 },    /* 095: p=0.097559 (    1,   12) */
00473   { 0x000d,  0x0000,  82,  98 },    /* 096: p=0.000276 ( 1208,    0) */
00474   { 0x0cfb,  0x0000,  99, 125 },    /* 097: p=0.067795 (    1,   18) */
00475   { 0x0034,  0x0000,  76,  72 },    /* 098: p=0.001102 ( 1208,    1) */
00476   { 0x08cd,  0x0000, 101, 123 },    /* 099: p=0.046511 (    1,   27) */
00477   { 0x00a0,  0x0000,  70, 102 },    /* 100: p=0.003387 (  392,    1) */
00478   { 0x05de,  0x0000, 103, 119 },    /* 101: p=0.031249 (    1,   41) */
00479   { 0x0118,  0x0000,  66,  60 },    /* 102: p=0.005912 (  392,    2) */
00480   { 0x03e9,  0x0000, 105, 117 },    /* 103: p=0.020942 (    1,   62) */
00481   { 0x01ed,  0x0000, 106, 110 },    /* 104: p=0.010362 (  127,    1) */
00482   { 0x0298,  0x0000, 107, 115 },    /* 105: p=0.013937 (    1,   94) */
00483   { 0x0145,  0x0000,  66, 108 },    /* 106: p=0.006849 (  193,    1) */
00484   { 0x01b6,  0x0000, 109, 113 },    /* 107: p=0.009216 (    1,  143) */
00485   { 0x0237,  0x0000,  60,  54 },    /* 108: p=0.011925 (  193,    2) */
00486   { 0x0121,  0x0000,  65, 111 },    /* 109: p=0.006097 (    1,  217) */
00487   { 0x035b,  0x0000,  56,  48 },    /* 110: p=0.017995 (  127,    2) */
00488   { 0x01f9,  0x0000,  59,  53 },    /* 111: p=0.010622 (    2,  217) */
00489   { 0x05de,  0x0000, 114, 130 },    /* 112: p=0.031249 (   41,    1) */
00490   { 0x02fc,  0x0000,  55,  49 },    /* 113: p=0.016018 (    2,  143) */
00491   { 0x03e9,  0x0000, 116, 128 },    /* 114: p=0.020942 (   62,    1) */
00492   { 0x0484,  0x0000,  51,  45 },    /* 115: p=0.024138 (    2,   94) */
00493   { 0x0298,  0x0000, 118, 126 },    /* 116: p=0.013937 (   94,    1) */
00494   { 0x06ca,  0x0000,  47,  39 },    /* 117: p=0.036082 (    2,   62) */
00495   { 0x01b6,  0x0000, 120, 124 },    /* 118: p=0.009216 (  143,    1) */
00496   { 0x0a27,  0x0000,  41, 121 },    /* 119: p=0.053434 (    2,   41) */
00497   { 0x0121,  0x0000,  66, 122 },    /* 120: p=0.006097 (  217,    1) */
00498   { 0x0e57,  0x0000,  37,  31 },    /* 121: p=0.074626 (    3,   41) */
00499   { 0x01f9,  0x0000,  60,  54 },    /* 122: p=0.010622 (  217,    2) */
00500   { 0x0f25,  0x0000,  37,  29 },    /* 123: p=0.078651 (    2,   27) */
00501   { 0x02fc,  0x0000,  56,  50 },    /* 124: p=0.016018 (  143,    2) */
00502   { 0x1629,  0x0000,  33, 127 },    /* 125: p=0.112902 (    2,   18) */
00503   { 0x0484,  0x0000,  52,  46 },    /* 126: p=0.024138 (   94,    2) */
00504   { 0x1ee8,  0x0000,  27,  21 },    /* 127: p=0.153845 (    3,   18) */
00505   { 0x06ca,  0x0000,  48,  40 },    /* 128: p=0.036082 (   62,    2) */
00506   { 0x200f,  0x0000,  27,  19 },    /* 129: p=0.159089 (    2,   12) */
00507   { 0x0a27,  0x0000,  42, 132 },    /* 130: p=0.053434 (   41,    2) */
00508   { 0x2dae,  0x0000,  21,  15 },    /* 131: p=0.218748 (    2,    8) */
00509   { 0x0e57,  0x0000,  38,  32 },    /* 132: p=0.074626 (   41,    3) */
00510   { 0x4320,  0x0000,  15,   7 },    /* 133: p=0.304346 (    2,    5) */
00511   { 0x11a0,  0x0000, 136, 164 },    /* 134: p=0.090907 (   13,    1) */
00512   { 0x620b,  0x0000,   9,  85 },    /* 135: p=0.411764 (    2,    3) */
00513   { 0x0bbe,  0x0000, 138, 162 },    /* 136: p=0.061537 (   20,    1) */
00514   { 0x8000,  0x0000, 135, 248 },    /* 137: p=0.500000 (    2,    2) */
00515   { 0x07f3,  0x0000, 140, 160 },    /* 138: p=0.042104 (   30,    1) */
00516   { 0x294a,  0x0000, 141, 247 },    /* 139: p=0.199988 (    0,    1) */
00517   { 0x053e,  0x0000, 142, 158 },    /* 140: p=0.027971 (   46,    1) */
00518   { 0x0db3,  0x0000, 143, 199 },    /* 141: p=0.071422 (    0,    4) */
00519   { 0x0378,  0x0000, 144, 156 },    /* 142: p=0.018604 (   70,    1) */
00520   { 0x0490,  0x0000, 145, 167 },    /* 143: p=0.024388 (    0,   13) */
00521   { 0x024d,  0x0000, 146, 154 },    /* 144: p=0.012384 (  106,    1) */
00522   { 0x017c,  0x0000, 147, 101 },    /* 145: p=0.007999 (    0,   41) */
00523   { 0x0185,  0x0000, 148, 152 },    /* 146: p=0.008197 (  161,    1) */
00524   { 0x007c,  0x0000, 149, 159 },    /* 147: p=0.002611 (    0,  127) */
00525   { 0x0100,  0x0000,  68, 150 },    /* 148: p=0.005405 (  245,    1) */
00526   { 0x0028,  0x0000, 151, 155 },    /* 149: p=0.000849 (    0,  392) */
00527   { 0x01c0,  0x0000,  62,  56 },    /* 150: p=0.009421 (  245,    2) */
00528   { 0x000d,  0x0000,  81, 153 },    /* 151: p=0.000276 (    0, 1208) */
00529   { 0x02a7,  0x0000,  58,  52 },    /* 152: p=0.014256 (  161,    2) */
00530   { 0x0034,  0x0000,  75,  71 },    /* 153: p=0.001102 (    1, 1208) */
00531   { 0x0403,  0x0000,  54,  46 },    /* 154: p=0.021472 (  106,    2) */
00532   { 0x00a0,  0x0000,  69, 157 },    /* 155: p=0.003387 (    1,  392) */
00533   { 0x0608,  0x0000,  48,  42 },    /* 156: p=0.032110 (   70,    2) */
00534   { 0x0118,  0x0000,  65,  59 },    /* 157: p=0.005912 (    2,  392) */
00535   { 0x0915,  0x0000,  44,  38 },    /* 158: p=0.047945 (   46,    2) */
00536   { 0x01ed,  0x0000, 161, 165 },    /* 159: p=0.010362 (    1,  127) */
00537   { 0x0db4,  0x0000,  40,  32 },    /* 160: p=0.071428 (   30,    2) */
00538   { 0x0145,  0x0000,  65, 163 },    /* 161: p=0.006849 (    1,  193) */
00539   { 0x1417,  0x0000,  34,  26 },    /* 162: p=0.102940 (   20,    2) */
00540   { 0x0237,  0x0000,  59,  53 },    /* 163: p=0.011925 (    2,  193) */
00541   { 0x1dd6,  0x0000,  30, 166 },    /* 164: p=0.148935 (   13,    2) */
00542   { 0x035b,  0x0000,  55,  47 },    /* 165: p=0.017995 (    2,  127) */
00543   { 0x294a,  0x0000,  24,  18 },    /* 166: p=0.199999 (   13,    3) */
00544   { 0x11a0,  0x0000, 169, 195 },    /* 167: p=0.090907 (    1,   13) */
00545   { 0x31a3,  0x0000, 170, 212 },    /* 168: p=0.235291 (    4,    1) */
00546   { 0x0bbe,  0x0000, 171, 193 },    /* 169: p=0.061537 (    1,   20) */
00547   { 0x235a,  0x0000, 172, 208 },    /* 170: p=0.173910 (    6,    1) */
00548   { 0x07f3,  0x0000, 173, 191 },    /* 171: p=0.042104 (    1,   30) */
00549   { 0x18b3,  0x0000, 174, 206 },    /* 172: p=0.124998 (    9,    1) */
00550   { 0x053e,  0x0000, 175, 189 },    /* 173: p=0.027971 (    1,   46) */
00551   { 0x1073,  0x0000, 176, 204 },    /* 174: p=0.085105 (   14,    1) */
00552   { 0x0378,  0x0000, 177, 187 },    /* 175: p=0.018604 (    1,   70) */
00553   { 0x0b35,  0x0000, 178, 200 },    /* 176: p=0.058822 (   21,    1) */
00554   { 0x024d,  0x0000, 179, 185 },    /* 177: p=0.012384 (    1,  106) */
00555   { 0x0778,  0x0000, 180, 198 },    /* 178: p=0.039603 (   32,    1) */
00556   { 0x0185,  0x0000, 181, 183 },    /* 179: p=0.008197 (    1,  161) */
00557   { 0x04ed,  0x0000, 182, 194 },    /* 180: p=0.026315 (   49,    1) */
00558   { 0x0100,  0x0000,  67,  59 },    /* 181: p=0.005405 (    1,  245) */
00559   { 0x0349,  0x0000, 184, 192 },    /* 182: p=0.017621 (   74,    1) */
00560   { 0x02a7,  0x0000,  57,  51 },    /* 183: p=0.014256 (    2,  161) */
00561   { 0x022e,  0x0000, 186, 190 },    /* 184: p=0.011730 (  112,    1) */
00562   { 0x0403,  0x0000,  53,  45 },    /* 185: p=0.021472 (    2,  106) */
00563   { 0x0171,  0x0000,  64, 188 },    /* 186: p=0.007767 (  170,    1) */
00564   { 0x0608,  0x0000,  47,  41 },    /* 187: p=0.032110 (    2,   70) */
00565   { 0x0283,  0x0000,  58,  52 },    /* 188: p=0.013513 (  170,    2) */
00566   { 0x0915,  0x0000,  43,  37 },    /* 189: p=0.047945 (    2,   46) */
00567   { 0x03cc,  0x0000,  54,  48 },    /* 190: p=0.020349 (  112,    2) */
00568   { 0x0db4,  0x0000,  39,  31 },    /* 191: p=0.071428 (    2,   30) */
00569   { 0x05b6,  0x0000,  50,  42 },    /* 192: p=0.030434 (   74,    2) */
00570   { 0x1417,  0x0000,  33,  25 },    /* 193: p=0.102940 (    2,   20) */
00571   { 0x088a,  0x0000,  44, 196 },    /* 194: p=0.045161 (   49,    2) */
00572   { 0x1dd6,  0x0000,  29, 197 },    /* 195: p=0.148935 (    2,   13) */
00573   { 0x0c16,  0x0000,  40,  34 },    /* 196: p=0.063291 (   49,    3) */
00574   { 0x294a,  0x0000,  23,  17 },    /* 197: p=0.199999 (    3,   13) */
00575   { 0x0ce2,  0x0000,  40,  32 },    /* 198: p=0.067307 (   32,    2) */
00576   { 0x31a3,  0x0000, 201, 243 },    /* 199: p=0.235291 (    1,    4) */
00577   { 0x1332,  0x0000,  36, 202 },    /* 200: p=0.098590 (   21,    2) */
00578   { 0x235a,  0x0000, 203, 239 },    /* 201: p=0.173910 (    1,    6) */
00579   { 0x1adc,  0x0000,  30,  24 },    /* 202: p=0.135134 (   21,    3) */
00580   { 0x18b3,  0x0000, 205, 237 },    /* 203: p=0.124998 (    1,    9) */
00581   { 0x1be7,  0x0000,  30,  22 },    /* 204: p=0.139999 (   14,    2) */
00582   { 0x1073,  0x0000, 207, 235 },    /* 205: p=0.085105 (    1,   14) */
00583   { 0x294a,  0x0000,  26,  16 },    /* 206: p=0.199998 (    9,    2) */
00584   { 0x0b35,  0x0000, 209, 231 },    /* 207: p=0.058822 (    1,   21) */
00585   { 0x3a07,  0x0000,  20, 210 },    /* 208: p=0.269229 (    6,    2) */
00586   { 0x0778,  0x0000, 211, 229 },    /* 209: p=0.039603 (    1,   32) */
00587   { 0x4e30,  0x0000,  14,   8 },    /* 210: p=0.344827 (    6,    3) */
00588   { 0x04ed,  0x0000, 213, 225 },    /* 211: p=0.026315 (    1,   49) */
00589   { 0x4fa6,  0x0000,  14, 214 },    /* 212: p=0.349998 (    4,    2) */
00590   { 0x0349,  0x0000, 215, 223 },    /* 213: p=0.017621 (    1,   74) */
00591   { 0x6966,  0x0000,   8,   2 },    /* 214: p=0.434782 (    4,    3) */
00592   { 0x022e,  0x0000, 217, 221 },    /* 215: p=0.011730 (    1,  112) */
00593   { 0x8000,  0x0000, 218,  87 },    /* 216: p=0.500000 (    1,    1) */
00594   { 0x0171,  0x0000,  63, 219 },    /* 217: p=0.007767 (    1,  170) */
00595   { 0x538e,  0x0000, 220, 246 },    /* 218: p=0.363634 (    2,    1) */
00596   { 0x0283,  0x0000,  57,  51 },    /* 219: p=0.013513 (    2,  170) */
00597   { 0x3e3e,  0x0000, 222, 244 },    /* 220: p=0.285711 (    3,    1) */
00598   { 0x03cc,  0x0000,  53,  47 },    /* 221: p=0.020349 (    2,  112) */
00599   { 0x294a,  0x0000, 224, 242 },    /* 222: p=0.199997 (    5,    1) */
00600   { 0x05b6,  0x0000,  49,  41 },    /* 223: p=0.030434 (    2,   74) */
00601   { 0x1b75,  0x0000, 226, 240 },    /* 224: p=0.137929 (    8,    1) */
00602   { 0x088a,  0x0000,  43, 227 },    /* 225: p=0.045161 (    2,   49) */
00603   { 0x12fc,  0x0000, 228, 238 },    /* 226: p=0.097559 (   12,    1) */
00604   { 0x0c16,  0x0000,  39,  33 },    /* 227: p=0.063291 (    3,   49) */
00605   { 0x0cfb,  0x0000, 230, 234 },    /* 228: p=0.067795 (   18,    1) */
00606   { 0x0ce2,  0x0000,  39,  31 },    /* 229: p=0.067307 (    2,   32) */
00607   { 0x08cd,  0x0000, 112, 232 },    /* 230: p=0.046511 (   27,    1) */
00608   { 0x1332,  0x0000,  35, 233 },    /* 231: p=0.098590 (    2,   21) */
00609   { 0x0f25,  0x0000,  38,  30 },    /* 232: p=0.078651 (   27,    2) */
00610   { 0x1adc,  0x0000,  29,  23 },    /* 233: p=0.135134 (    3,   21) */
00611   { 0x1629,  0x0000,  34, 236 },    /* 234: p=0.112902 (   18,    2) */
00612   { 0x1be7,  0x0000,  29,  21 },    /* 235: p=0.139999 (    2,   14) */
00613   { 0x1ee8,  0x0000,  28,  22 },    /* 236: p=0.153845 (   18,    3) */
00614   { 0x294a,  0x0000,  25,  15 },    /* 237: p=0.199998 (    2,    9) */
00615   { 0x200f,  0x0000,  28,  20 },    /* 238: p=0.159089 (   12,    2) */
00616   { 0x3a07,  0x0000,  19, 241 },    /* 239: p=0.269229 (    2,    6) */
00617   { 0x2dae,  0x0000,  22,  16 },    /* 240: p=0.218748 (    8,    2) */
00618   { 0x4e30,  0x0000,  13,   7 },    /* 241: p=0.344827 (    3,    6) */
00619   { 0x4320,  0x0000,  16,   8 },    /* 242: p=0.304346 (    5,    2) */
00620   { 0x4fa6,  0x0000,  13, 245 },    /* 243: p=0.349998 (    2,    4) */
00621   { 0x620b,  0x0000,  10,   2 },    /* 244: p=0.411764 (    3,    2) */
00622   { 0x6966,  0x0000,   7,   1 },    /* 245: p=0.434782 (    3,    4) */
00623   { 0x8000,  0x0000, 244,  83 },    /* 246: p=0.500000 (    2,    2) */
00624   { 0x8000,  0x0000, 249, 250 },    /* 247: p=0.500000 (    1,    1) */
00625   { 0x620b,  0x0000,  10,   2 },    /* 248: p=0.411764 (    3,    2) */
00626   { 0x538e,  0x0000,  89, 137 },    /* 249: p=0.363634 (    1,    2) */
00627   { 0x538e,  0x0000, 220, 246 },    /* 250: p=0.363634 (    2,    1) */
00628 #endif
00629 };
00630 
00631 
00632 
00634 // CONSTRUCTOR/DESTRUCTOR
00636 
00637 class ZPCodec::Encode : public ZPCodec
00638 {
00639 public:
00640   Encode(GP<ByteStream> gbs, const bool djvucompat);
00641   virtual ~Encode();
00642 private:
00643   void init(void); 
00644 };
00645 
00646 ZPCodec::Encode::Encode(GP<ByteStream> gbs, const bool djvucompat)
00647 : ZPCodec(gbs,true,djvucompat)
00648 {
00649   init();
00650   // Codebit counter
00651 #ifdef ZPCODEC_BITCOUNT
00652   bitcount = 0;
00653 #endif
00654 }
00655 
00656 ZPCodec::Encode::~Encode()
00657 {
00658   eflush();
00659 }
00660  
00661 class ZPCodec::Decode : public ZPCodec
00662 {
00663 public:
00664   Decode(GP<ByteStream> gbs, const bool djvucompat);
00665   virtual ~Decode();
00666 private:
00667   void init(void); 
00668 };
00669 
00670 ZPCodec::Decode::Decode(GP<ByteStream> gbs, const bool djvucompat)
00671 : ZPCodec(gbs,false,djvucompat)
00672 {
00673   init();
00674   // Codebit counter
00675 #ifdef ZPCODEC_BITCOUNT
00676   bitcount = 0;
00677 #endif
00678 }
00679  
00680 ZPCodec::Decode::~Decode() {}
00681 
00682 ZPCodec::ZPCodec(GP<ByteStream> xgbs, const bool xencoding, const bool djvucompat)
00683 : gbs(xgbs), bs(xgbs), encoding(xencoding), fence(0), subend(0), buffer(0), nrun(0)
00684 {
00685   // Create machine independent ffz table
00686   for (int i=0; i<256; i++)
00687     {
00688       ffzt[i]=0;
00689       for (int j=i; j&0x80; j<<=1)
00690         ffzt[i] += 1;
00691     }
00692   // Initialize table
00693   newtable(default_ztable);
00694   // Patch table table (and lose DjVu compatibility).
00695   if (!djvucompat)
00696     {
00697       for (int j=0; j<256; j++)
00698         {
00699           unsigned short a = 0x10000-p[j];
00700           while (a>=0x8000)  a=(unsigned short)(a<<1);
00701           if (m[j]>0 && a+p[j]>=0x8000 && a>=m[j])
00702             {
00703               BitContext x = default_ztable[j].dn;
00704               BitContext y = default_ztable[x].dn;
00705               dn[j] = y;
00706             }
00707         }
00708     }
00709 }
00710 
00711 ZPCodec::~ZPCodec() {}
00712 
00713 GP<ZPCodec>
00714 ZPCodec::create(GP<ByteStream> gbs, const bool encoding, const bool djvucompat)
00715 {
00716   GP<ZPCodec> retval;
00717   if(encoding)
00718   {
00719     retval=new ZPCodec::Encode(gbs,djvucompat);
00720   }else
00721   {
00722     retval=new ZPCodec::Decode(gbs,djvucompat);
00723   }
00724   return retval;
00725 }
00726 
00728 // Z CODER DECODE ALGORITHM
00730 
00731 
00732 
00733 void 
00734 ZPCodec::Decode::init(void)
00735 {
00736   assert(sizeof(unsigned int)==4);
00737   assert(sizeof(unsigned short)==2);
00738   a = 0;
00739   /* Read first 16 bits of code */
00740   if (! bs->read((void*)&byte, 1))
00741     byte = 0xff;
00742   code = (byte<<8);
00743   if (! bs->read((void*)&byte, 1))
00744     byte = 0xff;
00745   code = code | byte;
00746   /* Preload buffer */
00747   delay = 25;
00748   scount = 0;
00749   preload();
00750   /* Compute initial fence */
00751   fence = code;
00752   if (code >= 0x8000)
00753     fence = 0x7fff;
00754 }
00755 
00756 
00757 void
00758 ZPCodec::preload(void)
00759 {
00760   while (scount<=24)
00761     {
00762       if (bs->read((void*)&byte, 1) < 1) 
00763         {
00764           byte = 0xff;
00765           if (--delay < 1)
00766             G_THROW( ByteStream::EndOfFile );
00767         }
00768       buffer = (buffer<<8) | byte;
00769       scount += 8;
00770     }
00771 }
00772 
00773 
00774 inline int
00775 ZPCodec::ffz(unsigned int x)
00776 {
00777   // DO NOT DEFINE FASTBSR : 
00778   // Test shows that it hardly helps on a PPro,
00779   // and rumors are that BSR is very slow on PPlain.
00780 #if defined(FASTBSR) && defined(_MSC_VER) && defined(_M_IX86)
00781   int r;
00782   __asm  { 
00783         mov  ebx, x
00784         xor  ebx, 0xffff
00785         mov  eax, -1
00786         bsr  eax, ebx
00787         mov  r, eax
00788   }
00789   return 15 - r;
00790 #elif defined(FASTBSR) && defined(__GNUC__) && defined(__i386__)
00791   int r, dummy;
00792   __asm__ const ( "movl %2,%1\n\t"
00793                   "xorl $0xffff, %1\n\t"
00794                   "movl $-1, %0\n\t"
00795                   "bsrl %1, %0"
00796                   : "=&q" (r), "=q" (dummy) : "rm" (x) );
00797   return 15 - r;
00798 #else
00799   return (x>=0xff00) ? (ffzt[x&0xff]+8) : (ffzt[(x>>8)&0xff]);
00800 #endif
00801 }
00802 
00803 
00804 int 
00805 ZPCodec::decode_sub(BitContext &ctx, unsigned int z)
00806 {
00807   /* Save bit */
00808   int bit = (ctx & 1);
00809   /* Avoid interval reversion */
00810 #ifdef ZPCODER
00811   unsigned int d = 0x6000 + ((z+a)>>2);
00812   if (z > d) 
00813     z = d;
00814 #endif
00815 #ifdef ZCODER
00816   if (z >= 0x8000)
00817     z = 0x4000 + (z>>1);
00818 #endif
00819   /* Test MPS/LPS */
00820   if (z > code)
00821     {
00822       /* LPS branch */
00823       z = 0x10000 - z;
00824       a = a + z;
00825       code = code + z;
00826       /* LPS adaptation */
00827       ctx = dn[ctx];
00828       /* LPS renormalization */
00829       int shift = ffz(a);
00830       scount -= shift;
00831       a = (unsigned short)(a<<shift);
00832       code = (unsigned short)(code<<shift) | ((buffer>>scount) & ((1<<shift)-1));
00833 #ifdef ZPCODEC_BITCOUNT
00834       bitcount += shift;
00835 #endif
00836       if (scount<16) preload();
00837       /* Adjust fence */
00838       fence = code;
00839       if (code >= 0x8000)
00840         fence = 0x7fff;
00841       return bit ^ 1;
00842     }
00843   else
00844     {
00845       /* MPS adaptation */
00846       if (a >= m[ctx])
00847         ctx = up[ctx];
00848       /* MPS renormalization */
00849       scount -= 1;
00850       a = (unsigned short)(z<<1);
00851       code = (unsigned short)(code<<1) | ((buffer>>scount) & 1);
00852 #ifdef ZPCODEC_BITCOUNT
00853       bitcount += 1;
00854 #endif
00855       if (scount<16) preload();
00856       /* Adjust fence */
00857       fence = code;
00858       if (code >= 0x8000)
00859         fence = 0x7fff;
00860       return bit;
00861     }
00862 }
00863 
00864 
00865 int 
00866 ZPCodec::decode_sub_simple(int mps, unsigned int z)
00867 {
00868   /* Test MPS/LPS */
00869   if (z > code)
00870     {
00871       /* LPS branch */
00872       z = 0x10000 - z;
00873       a = a + z;
00874       code = code + z;
00875       /* LPS renormalization */
00876       int shift = ffz(a);
00877       scount -= shift;
00878       a = (unsigned short)(a<<shift);
00879       code = (unsigned short)(code<<shift) | ((buffer>>scount) & ((1<<shift)-1));
00880 #ifdef ZPCODEC_BITCOUNT
00881       bitcount += shift;
00882 #endif
00883       if (scount<16) preload();
00884       /* Adjust fence */
00885       fence = code;
00886       if (code >= 0x8000)
00887         fence = 0x7fff;
00888       return mps ^ 1;
00889     }
00890   else
00891     {
00892       /* MPS renormalization */
00893       scount -= 1;
00894       a = (unsigned short)(z<<1);
00895       code = (unsigned short)(code<<1) | ((buffer>>scount) & 1);
00896 #ifdef ZPCODEC_BITCOUNT
00897       bitcount += 1;
00898 #endif
00899       if (scount<16) preload();
00900       /* Adjust fence */
00901       fence = code;
00902       if (code >= 0x8000)
00903         fence = 0x7fff;
00904       return mps;
00905     }
00906 }
00907 
00908 
00909 int  
00910 ZPCodec::decode_sub_nolearn(int mps, unsigned int z)
00911 {
00912 #ifdef ZPCODER
00913   unsigned int d = 0x6000 + ((z+a)>>2);
00914   if (z > d) 
00915         z = d;
00916 #endif
00917 #ifdef ZCODER
00918   if (z >= 0x8000)
00919     z = 0x4000 + (z>>1);
00920 #endif
00921   /* Test MPS/LPS */
00922   if (z > code)
00923     {
00924       /* LPS branch */
00925       z = 0x10000 - z;
00926       a = a + z;
00927       code = code + z;
00928       /* LPS renormalization */
00929       int shift = ffz(a);
00930       scount -= shift;
00931       a = (unsigned short)(a<<shift);
00932       code = (unsigned short)(code<<shift) | ((buffer>>scount) & ((1<<shift)-1));
00933 #ifdef ZPCODEC_BITCOUNT
00934       bitcount += shift;
00935 #endif
00936       if (scount<16) preload();
00937       /* Adjust fence */
00938       fence = code;
00939       if (code >= 0x8000)
00940         fence = 0x7fff;
00941       return mps ^ 1;
00942     }
00943   else
00944     {
00945       /* MPS renormalization */
00946       scount -= 1;
00947       a = (unsigned short)(z<<1);
00948       code = (unsigned short)(code<<1) | ((buffer>>scount) & 1);
00949 #ifdef ZPCODEC_BITCOUNT
00950       bitcount += 1;
00951 #endif
00952       if (scount<16) preload();
00953       /* Adjust fence */
00954       fence = code;
00955       if (code >= 0x8000)
00956         fence = 0x7fff;
00957       return mps;
00958     }
00959 }
00960 
00961 
00962 
00963 
00964 
00966 // Z CODER ENCODE ALGORITHM
00968 
00969 
00970 
00971 
00972 void 
00973 ZPCodec::Encode::init(void)
00974 {
00975   assert(sizeof(unsigned int)==4);
00976   assert(sizeof(unsigned short)==2);
00977   a = 0;
00978   scount = 0;
00979   byte = 0;
00980   delay = 25;
00981   subend = 0;
00982   buffer = 0xffffff;
00983   nrun = 0;
00984 }
00985 
00986 void
00987 ZPCodec::outbit(int bit)
00988 {
00989   if (delay > 0)
00990     {
00991       if (delay < 0xff) // delay=0xff suspends emission forever
00992         delay -= 1;
00993     }
00994   else
00995     {
00996       /* Insert a bit */
00997       byte = (byte<<1) | bit;
00998       /* Output a byte */
00999       if (++scount == 8)
01000         {
01001           if (!encoding)
01002             G_THROW( ERR_MSG("ZPCodec.no_encoding") );
01003           if (bs->write((void*)&byte, 1) != 1)
01004             G_THROW( ERR_MSG("ZPCodec.write_error") );
01005           scount = 0;
01006           byte = 0;
01007         }
01008     }
01009 }
01010 
01011 void 
01012 ZPCodec::zemit(int b)
01013 {
01014   /* Shift new bit into 3bytes buffer */
01015   buffer = (buffer<<1) + b;
01016   /* Examine bit going out of the 3bytes buffer */
01017   b = (buffer >> 24);
01018   buffer = (buffer & 0xffffff);
01019   /* The following lines have been changed in order to emphazise the
01020    * similarity between this bit counting and the scheme of Witten, Neal & Cleary
01021    * (WN&C).  Corresponding changes have been made in outbit and eflush.
01022    * Variable 'nrun' is similar to the 'bits_to_follow' in the W&N code.
01023    */
01024   switch(b)
01025     {
01026       /* Similar to WN&C upper renormalization */
01027     case 1:
01028       outbit(1);
01029       while (nrun-- > 0)
01030         outbit(0);
01031       nrun = 0;
01032       break;
01033       /* Similar to WN&C lower renormalization */
01034     case 0xff:
01035       outbit(0);
01036       while (nrun-- > 0)
01037         outbit(1);
01038       nrun = 0;
01039       break;
01040       /* Similar to WN&C central renormalization */
01041     case 0:
01042       nrun += 1;
01043       break;
01044     default:
01045       assert(0);
01046     }
01047   /* Code bit counter */
01048 #ifdef ZPCODEC_BITCOUNT
01049   bitcount += 1;
01050 #endif
01051 }
01052 
01053 void 
01054 ZPCodec::eflush()
01055 {
01056   /* adjust subend */
01057   if (subend > 0x8000)
01058     subend = 0x10000;
01059   else if (subend > 0)
01060     subend = 0x8000;
01061   /* zemit many mps bits */
01062   while (buffer != 0xffffff  || subend )
01063     {
01064       zemit(1 - (subend>>15) );
01065       subend = (unsigned short)(subend<<1);
01066     }
01067   /* zemit pending run */
01068   outbit(1);
01069   while (nrun-- > 0)
01070     outbit(0);
01071   nrun = 0;
01072   /* zemit 1 until full byte */
01073   while (scount > 0)
01074     outbit(1);
01075   /* prevent further emission */
01076   delay = 0xff;
01077 }
01078 
01079 void 
01080 ZPCodec::encode_mps(BitContext &ctx, unsigned int z)
01081 {
01082   /* Avoid interval reversion */
01083 #ifdef ZPCODER
01084   unsigned int d = 0x6000 + ((z+a)>>2);
01085   if (z > d) 
01086     z = d;
01087 #endif
01088 #ifdef ZCODER
01089   if (z >= 0x8000)
01090     z = 0x4000 + (z>>1);
01091 #endif
01092   /* Adaptation */
01093   if (a >= m[ctx])
01094     ctx = up[ctx];
01095   /* Code MPS */
01096   a = z;
01097   /* Export bits */
01098   if (a >= 0x8000)
01099     {
01100       zemit(1 - (subend>>15) );
01101       subend = (unsigned short)(subend<<1);
01102       a = (unsigned short)(a<<1);
01103     }
01104 }
01105 
01106 
01107 void 
01108 ZPCodec::encode_lps(BitContext &ctx, unsigned int z)
01109 {
01110   /* Avoid interval reversion */
01111 #ifdef ZPCODER
01112   unsigned int d = 0x6000 + ((z+a)>>2);
01113   if (z > d) 
01114     z = d;
01115 #endif
01116 #ifdef ZCODER
01117   if (z >= 0x8000)
01118     z = 0x4000 + (z>>1);
01119 #endif
01120   /* Adaptation */
01121   ctx = dn[ctx];
01122   /* Code LPS */
01123   z = 0x10000 - z;
01124   subend += z;
01125   a += z;
01126   /* Export bits */
01127   while (a >= 0x8000)
01128     {
01129       zemit(1 - (subend>>15) );
01130       subend = (unsigned short)(subend<<1);
01131       a = (unsigned short)(a<<1);
01132     }
01133 }
01134 
01135 
01136 void 
01137 ZPCodec::encode_mps_simple(unsigned int z)
01138 {
01139   /* Code MPS */
01140   a = z;
01141   /* Export bits */
01142   if (a >= 0x8000)
01143     {
01144       zemit(1 - (subend>>15) );
01145       subend = (unsigned short)(subend<<1);
01146       a = (unsigned short)(a<<1);
01147     }
01148 }
01149 
01150 void 
01151 ZPCodec::encode_lps_simple(unsigned int z)
01152 {
01153   /* Code LPS */
01154   z = 0x10000 - z;
01155   subend += z;
01156   a += z;
01157   /* Export bits */
01158   while (a >= 0x8000)
01159     {
01160       zemit(1 - (subend>>15) );
01161       subend = (unsigned short)(subend<<1);
01162       a = (unsigned short)(a<<1);
01163     }
01164 }
01165 
01166 
01167 void 
01168 ZPCodec::encode_mps_nolearn(unsigned int z)
01169 {
01170 #ifdef ZPCODER
01171   unsigned int d = 0x6000 + ((z+a)>>2);
01172   if (z > d) 
01173     z = d;
01174 #endif
01175 #ifdef ZCODER
01176   if (z >= 0x8000)
01177     z = 0x4000 + (z>>1);
01178 #endif
01179   /* Code MPS */
01180   a = z;
01181   /* Export bits */
01182   if (a >= 0x8000)
01183     {
01184       zemit(1 - (subend>>15) );
01185       subend = (unsigned short)(subend<<1);
01186       a = (unsigned short)(a<<1);
01187     }
01188 }
01189 
01190 
01191 void 
01192 ZPCodec::encode_lps_nolearn(unsigned int z)
01193 {
01194 #ifdef ZPCODER
01195   unsigned int d = 0x6000 + ((z+a)>>2);
01196   if (z > d) 
01197     z = d;
01198 #endif
01199 #ifdef ZCODER
01200   if (z >= 0x8000)
01201     z = 0x4000 + (z>>1);
01202 #endif
01203   /* Code LPS */
01204   z = 0x10000 - z;
01205   subend += z;
01206   a += z;
01207   /* Export bits */
01208   while (a >= 0x8000)
01209     {
01210       zemit(1 - (subend>>15) );
01211       subend = (unsigned short)(subend<<1);
01212       a = (unsigned short)(a<<1);
01213     }
01214 }
01215 
01216 
01217 
01218 
01219 
01220 
01222 // TABLE AND PARAMETER MANAGEMENT
01224 
01225 
01226 
01227 
01228 void 
01229 ZPCodec::newtable(ZPCodec::Table *table)
01230 {
01231   for (int i=0; i<256; i++)
01232     {
01233       p[i]  = table[i].p;
01234       m[i]  = table[i].m;
01235       up[i] = table[i].up;
01236       dn[i] = table[i].dn;
01237     }
01238 }
01239 
01240 static float 
01241 p_to_plps(unsigned short p)
01242 {
01243   float fplps;
01244   float fp = (float)(p) / (float)(0x10000);
01245   const float log2 = (float)0.69314718055994530942;
01246 #ifdef ZCODER
01247   fplps = fp - (fp+0.5) * log(fp+0.5) + (fp-0.5)*log2;
01248 #endif
01249 #ifdef ZPCODER
01250   if (fp <= (1.0/6.0) )
01251     fplps = fp * 2 * log2;
01252   else
01253     fplps = (float)((1.5*fp-0.25) - (1.5*fp+0.25)*log(1.5*fp+0.25) + (0.5*fp-0.25)*log2);
01254 #endif
01255   return fplps;
01256 }
01257 
01258 
01259 BitContext 
01260 ZPCodec::state(float prob1)
01261 {
01262   // Return a state representing 'prob1' in the steady chain
01263   // FixMe: This is quite slow! 
01264   int mps = (prob1 <= 0.5 ? 0 : 1);
01265   float plps = (float)(mps ? 1.0 - prob1 : prob1);
01266   // Locate steady chain (ordered, decreasing)
01267   int sz = 0;
01268   int lo = (mps ? 1 : 2);
01269   while (p[lo+sz+sz+2] < p[lo+sz+sz]) sz+=1;
01270   // Bisection
01271   while (sz > 1)
01272     {
01273       int nsz = sz >> 1;
01274       float nplps = p_to_plps( p[lo+nsz+nsz] );
01275       if (nplps < plps)
01276         { sz=nsz; }
01277       else
01278         { lo=lo+nsz+nsz; sz=sz-nsz; }
01279     }
01280   // Choose closest one
01281   float f1 = p_to_plps(p[lo])-plps;
01282   float f2 = plps-p_to_plps(p[lo+2]);
01283   return (f1<f2) ? lo : lo+2;
01284 }
01285 
01286 
01287 #ifdef HAVE_NAMESPACES
01288 }
01289 # ifndef NOT_USING_DJVU_NAMESPACE
01290 using namespace DJVU;
01291 # endif
01292 #endif

kviewshell

Skip menu "kviewshell"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • kviewshell
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal