kviewshell
ZPCodec.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
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
00067
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
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
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
00311 GP<ByteStream> gbs;
00312 ByteStream *bs;
00313 const bool 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
00324 unsigned int p[256];
00325 unsigned int m[256];
00326 BitContext up[256];
00327 BitContext dn[256];
00328
00329 char ffzt[256];
00330
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
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
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
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
00445
00736
00737
00738
00739 #ifdef HAVE_NAMESPACES
00740 }
00741 # ifndef NOT_USING_DJVU_NAMESPACE
00742 using namespace DJVU;
00743 # endif
00744 #endif
00745 #endif
00746
00747