aricoder.h

来自「this is source code for image compressio」· C头文件 代码 · 共 108 行

H
108
字号
/*---------------------------------------------------------------------------*/
// Image Compression Toolbox v1.2
// written by
// Satish Kumar S
// satishkumr@lycos.com
//
// Copyright 1999 Satish Kumar S
//
// Permission is granted to use this software for research purposes as
// long as this notice stays attached to this software.
/*---------------------------------------------------------------------------*/
#ifndef _ARICODER_H
#define _ARICODER_H

#define onelog2     1.4426950408889634

//--------- Bitstream part.
class BitStream
{
private:
    unsigned char buf;
    int nBits;
    FILE *fp;
public:
    int  numBitsWritten;
    int  numBitsRead;
    BitStream(FILE *givenfp);
    ~BitStream();
    void WriteBit(unsigned int bit);
    void WriteBits(unsigned int bits,int n);
    void Flush();
    unsigned int ReadBit();
    unsigned int ReadBits(int n);
};

//-------- Arithmetic Encoder & Decoder part.

#define CodeValueBits   16		                // Number of bits in a code value

#define TopValue (((long)1<<CodeValueBits)-1)   // Largest code value

#define FirstQtr  (TopValue/4+1)	// Point after first quarter
#define Half	  (2*FirstQtr)		// Point after first half
#define ThirdQtr  (3*FirstQtr)		// Point after third quarter
#define MaxAllowedFreq  16383       // 2^14-1

class AdaptiveModel
{
    unsigned int *Tree;         // the cumulative freq tree.
    unsigned int numSyms;       // number of alphabets.
public:
    unsigned int totfreq;

    AdaptiveModel();
    ~AdaptiveModel();
    int SetNumSyms(int n);
    void Scaledown();
    int GetCumul(int Ix);
    int GetIndex(unsigned int Freq);
    void PutValue(int Val, int Ix);
    int GetFreq(int Ix);
};

class ArithEncoder
{
    unsigned int High, Low;
    unsigned int folw;      // 'follow with' counter.

    BitStream *bs;
public:
    AdaptiveModel SrcModel;

    ArithEncoder(BitStream *bitstream);
    ~ArithEncoder();
    int  SetNumSyms(int n);
    void InitEncode();
    unsigned int Encode(unsigned int symbol,int update=1);
    void Flush();
    inline void WriteBitPlusFollow(int bit)
    {
        bs->WriteBit(bit);
        bit = 1-bit;
        while(folw)
        {
            bs->WriteBit(bit);
            folw--;
        }
    }
    double EncodingCost(unsigned int symbol);
};

class ArithDecoder
{
    unsigned int High, Low;
    unsigned int folw;      // 'follow with' counter.
    unsigned int Value;         // value.

    AdaptiveModel SrcModel;
    BitStream *bs;
public:
    ArithDecoder(BitStream *bitstream);
    ~ArithDecoder();
    int  SetNumSyms(int n);
    void InitDecode();
    unsigned int Decode();
};

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?