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

kviewshell

ZPCodec.h

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.h,v 1.9 2003/11/07 22:08:22 leonb Exp $
00055 // $Name: release_3_5_15 $
00056 
00057 #ifndef _ZPCODEC_H
00058 #define _ZPCODEC_H
00059 #ifdef HAVE_CONFIG_H
00060 #include "config.h"
00061 #endif
00062 #if NEED_GNUG_PRAGMAS
00063 # pragma interface
00064 #endif
00065 
00066 // From: Leon Bottou, 1/31/2002
00067 // Almost equal to my initial code.
00068 
00069 #include "GContainer.h"
00070 
00071 #ifdef HAVE_NAMESPACES
00072 namespace DJVU {
00073 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
00074 }
00075 #endif
00076 #endif
00077 
00078 class ByteStream;
00079 
00080 
00081 
00194 
00195 
00213 typedef unsigned char  BitContext;
00214 
00215 
00245 class ZPCodec : public GPEnabled {
00246 protected:
00247   ZPCodec (GP<ByteStream> gbs, const bool encoding, const bool djvucompat=false);
00248 public:
00249   class Encode;
00250   class Decode;
00251 
00253   ~ZPCodec();
00263   static GP<ZPCodec> create(
00264      GP<ByteStream> gbs, const bool encoding, const bool djvucompat=false);
00265 
00270   void encoder(int bit, BitContext &ctx);
00271 
00275   int  decoder(BitContext &ctx);
00276 
00281   void encoder(int bit);
00282 
00285   int  decoder(void);
00286 #ifdef ZPCODEC_BITCOUNT
00287 
00293   int bitcount;
00294 #endif
00295   // Table management (advanced stuff)
00296   struct Table { 
00297     unsigned short p;
00298     unsigned short m;
00299     BitContext     up;
00300     BitContext     dn;
00301   };
00302   void newtable(ZPCodec::Table *table);
00303   BitContext state(float prob1);
00304   // Non-adaptive encoder/decoder
00305   void encoder_nolearn(int pix, BitContext &ctx);
00306   int  decoder_nolearn(BitContext &ctx);
00307   inline int  IWdecoder(void);
00308   inline void IWencoder(const bool bit);
00309 protected:
00310   // coder status
00311   GP<ByteStream> gbs;           // Where the data goes/comes from
00312   ByteStream *bs;               // Where the data goes/comes from
00313   const bool encoding;          // Direction (0=decoding, 1=encoding)
00314   unsigned char byte;
00315   unsigned char scount;
00316   unsigned char delay;
00317   unsigned int  a;
00318   unsigned int  code;
00319   unsigned int  fence;
00320   unsigned int  subend;
00321   unsigned int  buffer;
00322   unsigned int  nrun;
00323   // table
00324   unsigned int  p[256];
00325   unsigned int  m[256];
00326   BitContext    up[256];
00327   BitContext    dn[256];
00328   // machine independent ffz
00329   char          ffzt[256];
00330   // encoder private
00331   void einit (void);
00332   void eflush (void);
00333   void outbit(int bit);
00334   void zemit(int b);
00335   void encode_mps(BitContext &ctx, unsigned int z);
00336   void encode_lps(BitContext &ctx, unsigned int z);
00337   void encode_mps_simple(unsigned int z);
00338   void encode_lps_simple(unsigned int z);
00339   void encode_mps_nolearn(unsigned int z);
00340   void encode_lps_nolearn(unsigned int z);
00341   // decoder private
00342   void dinit(void);
00343   void preload(void);
00344   int  ffz(unsigned int x);
00345   int  decode_sub(BitContext &ctx, unsigned int z);
00346   int  decode_sub_simple(int mps, unsigned int z);
00347   int  decode_sub_nolearn(int mps, unsigned int z);
00348 private:
00349   // no copy allowed (hate c++)
00350   ZPCodec(const ZPCodec&);
00351   ZPCodec& operator=(const ZPCodec&);
00352 #ifdef ZPCODEC_FRIEND
00353   friend ZPCODEC_FRIEND;
00354 #endif
00355 };
00356 
00357 
00358 
00359 
00360 
00361 
00362 // INLINE CODE
00363 
00364 inline void 
00365 ZPCodec::encoder(int bit, BitContext &ctx) 
00366 {
00367   unsigned int z = a + p[ctx];
00368   if (bit != (ctx & 1))
00369   {
00370     encode_lps(ctx, z);
00371   }else if (z >= 0x8000)
00372   {
00373     encode_mps(ctx, z);
00374   }else
00375   {
00376     a = z;
00377   }
00378 }
00379 
00380 inline int
00381 ZPCodec::IWdecoder(void)
00382 {
00383   return decode_sub_simple(0,0x8000 + ((a+a+a) >> 3));
00384 }
00385 
00386 inline int
00387 ZPCodec::decoder(BitContext &ctx) 
00388 {
00389   unsigned int z = a + p[ctx];
00390   if (z <= fence) 
00391     { a = z; return (ctx&1); } 
00392   return decode_sub(ctx, z);
00393 }
00394 
00395 inline void 
00396 ZPCodec::encoder_nolearn(int bit, BitContext &ctx) 
00397 {
00398   unsigned int z = a + p[ctx];
00399   if (bit != (ctx & 1))
00400     encode_lps_nolearn(z);
00401   else if (z >= 0x8000)
00402     encode_mps_nolearn(z);
00403   else
00404     a = z;
00405 }
00406 
00407 inline int
00408 ZPCodec::decoder_nolearn(BitContext &ctx) 
00409 {
00410   unsigned int z = a + p[ctx];
00411   if (z <= fence) 
00412     { a = z; return (ctx&1); } 
00413   return decode_sub_nolearn( (ctx&1), z);
00414 }
00415 
00416 inline void 
00417 ZPCodec::encoder(int bit)
00418 {
00419   if (bit)
00420     encode_lps_simple(0x8000 + (a>>1));
00421   else
00422     encode_mps_simple(0x8000 + (a>>1));
00423 }
00424 
00425 inline int
00426 ZPCodec::decoder(void)
00427 {
00428   return decode_sub_simple(0, 0x8000 + (a>>1));
00429 }
00430 
00431 inline void
00432 ZPCodec::IWencoder(const bool bit)
00433 {
00434   const int z = 0x8000 + ((a+a+a) >> 3);
00435   if (bit)
00436   {
00437     encode_lps_simple(z);
00438   }else
00439   {
00440     encode_mps_simple(z);
00441   }
00442 }
00443 
00444 // ------------ ADDITIONAL DOCUMENTATION
00445 
00736 
00737 // ------------ THE END
00738 
00739 #ifdef HAVE_NAMESPACES
00740 }
00741 # ifndef NOT_USING_DJVU_NAMESPACE
00742 using namespace DJVU;
00743 # endif
00744 #endif
00745 #endif
00746 
00747 

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