Performs ZP-Coder encoding and decoding.
Performs ZP-Coder encoding and decoding. A ZPCodec object must either constructed for encoding or for decoding. The ZPCodec object is connected with a ByteStream object specified at construction time. A ZPCodec object constructed for decoding reads code bits from the ByteStream and returns a message bit whenever function decoder is called. A ZPCodec constructed for encoding processes the message bits provided by function encoder and writes the corresponding code bits to ByteStream bs.You should never directly access a ByteStream object connected to a valid ZPCodec object. The most direct way to access the ByteStream object consists of using the "pass-thru" versions of functions encoder and decoder.
The ByteStream object can be accessed again after the destruction of the ZPCodec object. Note that the encoder always flushes its internal buffers and writes a few final code bytes when the ZPCodec object is destroyed. Note also that the decoder often reads a few bytes beyond the last code byte written by the encoder. This lag means that you must reposition the ByteStream after the destruction of the ZPCodec object and before re-using the ByteStream object (see IFFByteStream.)
Please note also that the decoder has no way to reliably indicate the end of the message bit sequence. The content of the message must be designed in a way which indicates when to stop decoding. Simple ways to achieve this consists of announcing the message length at the beginning (like a pascal style string), or of defining a termination code (like a null terminated string).
void encoder(int bit, BitContext &ctx);
int decoder(BitContext &ctx);
void encoder(int bit);
int decoder();
int bitcount;
struct Table { unsigned short p; unsigned short m; BitContext up; BitContext dn; }; void newtable(ZPCodec::Table *table); BitContext state(float prob1); void encoder_nolearn(int pix, BitContext &ctx); int decoder_nolearn(BitContext &ctx); protected: ByteStream *bs; char encoding; unsigned char byte; unsigned char scount; unsigned char delay; unsigned int a; unsigned int code; unsigned int fence; unsigned int subend; unsigned int buffer; unsigned int nrun; unsigned int p[256]; unsigned int m[256]; BitContext up[256]; BitContext dn[256]; char ffzt[256]; void einit (void); void eflush (void); void outbit(int bit); void emit(int b); void encode_mps(BitContext &ctx, unsigned int z); void encode_lps(BitContext &ctx, unsigned int z); void encode_mps_simple(unsigned int z); void encode_lps_simple(unsigned int z); void encode_mps_nolearn(unsigned int z); void encode_lps_nolearn(unsigned int z); void dinit(void); void preload(void); int ffz(unsigned int x); int decode_sub(BitContext &ctx, unsigned int z); int decode_sub_simple(int mps, unsigned int z); int decode_sub_nolearn(int mps, unsigned int z); private: ZPCodec(const ZPCodec&); ZPCodec& operator=(const ZPCodec&); };
inline void ZPCodec::encoder(int bit, BitContext &ctx) { unsigned int z = a + p[ctx]; if (bit != (ctx & 1)) encode_lps(ctx, z); else if (z >= 0x8000) encode_mps(ctx, z); else a = z; }
inline int ZPCodec::decoder(BitContext &ctx) { unsigned int z = a + p[ctx]; if (z <= fence) { a = z; return (ctx&1); } return decode_sub(ctx, z); }
inline void ZPCodec::encoder_nolearn(int bit, BitContext &ctx) { unsigned int z = a + p[ctx]; if (bit != (ctx & 1)) encode_lps_nolearn(z); else if (z >= 0x8000) encode_mps_nolearn(z); else a = z; }
inline int ZPCodec::decoder_nolearn(BitContext &ctx) { unsigned int z = a + p[ctx]; if (z <= fence) { a = z; return (ctx&1); } return decode_sub_nolearn( (ctx&1), z); }
inline void ZPCodec::encoder(int bit) { if (bit) encode_lps_simple(0x8000 + (a>>1)); else encode_mps_simple(0x8000 + (a>>1)); }
inline int ZPCodec::decoder() { return decode_sub_simple(0, 0x8000 + (a>>1)); }
Alphabetic index HTML hierarchy of classes or Java