imgtools/imglib/compress/byte_pair.h
changeset 590 360bd6b35136
parent 0 044383f39525
child 654 7c11c3d8d025
equal deleted inserted replaced
588:c7c26511138f 590:360bd6b35136
    16 */
    16 */
    17 
    17 
    18 #ifndef BYTE_PAIR_H
    18 #ifndef BYTE_PAIR_H
    19 #define BYTE_PAIR_H
    19 #define BYTE_PAIR_H
    20 
    20 
       
    21  
    21 #ifdef __VC32__
    22 #ifdef __VC32__
    22  #ifdef __MSVCDOTNET__
    23  #ifdef __MSVCDOTNET__
    23   #include <strstream>
    24   #include <strstream>
    24   #include <iomanip>
    25   #include <iomanip>
    25  #else //!__MSVCDOTNET__
    26  #else //!__MSVCDOTNET__
    72 const TUint8 ByteHead = 'H';
    73 const TUint8 ByteHead = 'H';
    73 const TUint8 ByteTail = 'T';
    74 const TUint8 ByteTail = 'T';
    74 const TUint8 ByteNew = 'N';
    75 const TUint8 ByteNew = 'N';
    75 class CBytePair {
    76 class CBytePair {
    76     private:
    77     private:
    77         TBool   iFastCompress;
       
    78         TInt    marker;
    78         TInt    marker;
       
    79         TInt    markerCount;
    79         TUint8  Mask[MaxBlockSize];
    80         TUint8  Mask[MaxBlockSize];
    80         TUint16 ByteCount[0x100];
    81         TUint16 ByteCount[0x100];
    81         TUint16 ByteIndex[0x100];
    82         TUint16 ByteIndex[0x100];
    82         TUint16 BytePos[MaxBlockSize];
    83         TUint16 BytePos[MaxBlockSize];
    83         TPairCountIndex PairCount[0x10000];
    84         TPairCountIndex PairCount[0x10000];
    87         TUint16 PairPosNext;
    88         TUint16 PairPosNext;
    88         TUint16 PairLists[MaxBlockSize/2+2];
    89         TUint16 PairLists[MaxBlockSize/2+2];
    89         TInt    PairListHigh;
    90         TInt    PairListHigh;
    90 
    91 
    91         void CountBytes(TUint8* data, TInt size) {
    92         void CountBytes(TUint8* data, TInt size) {
    92             TUint32 *p;
    93 	    memset(reinterpret_cast<char*>(ByteCount),0,sizeof(ByteCount));
    93             p = (TUint32*)ByteCount;
    94             memset(reinterpret_cast<char*>(ByteIndex),0xff,sizeof(ByteIndex));
    94             while (p < (TUint32*)ByteCount + sizeof(ByteCount)/4) {
    95             memset(reinterpret_cast<char*>(BytePos),0xff, sizeof(BytePos));
    95                 *p++ = 0;
    96             TUint8* dataEnd = data + size;
    96             }
       
    97             p = (TUint32*)ByteIndex;
       
    98             while (p < (TUint32*)ByteIndex + sizeof(ByteIndex)/4) {
       
    99                 *p++ = 0xffffffff;
       
   100             }
       
   101             p = (TUint32*)BytePos;
       
   102             while (p< (TUint32*)BytePos + sizeof(BytePos)/4) {
       
   103                 *p++ = 0xffffffff;
       
   104             }
       
   105             TUint8* dataEnd = data+size;
       
   106             int pos = 0;
    97             int pos = 0;
   107             while(data<dataEnd) {
    98             while(data < dataEnd) {
   108                 BytePos[pos] = ByteIndex[*data];
    99                 BytePos[pos] = ByteIndex[*data];
   109                 ByteIndex[*data] = (TUint16)pos;
   100                 ByteIndex[*data] = (TUint16)pos;
   110                 pos ++;
   101                 pos ++;
   111                 ++ByteCount[*data++];
   102                 ++ByteCount[*data++];
   112             }
   103             }
   115             ByteCount[b] = 0xffff;
   106             ByteCount[b] = 0xffff;
   116         }
   107         }
   117         int TieBreak(int b1,int b2) {
   108         int TieBreak(int b1,int b2) {
   118             return -ByteCount[b1]-ByteCount[b2];
   109             return -ByteCount[b1]-ByteCount[b2];
   119         }
   110         }
       
   111         void SortN(TUint16 *a, TInt n);
       
   112         void InsertN(TUint16 *a, TInt n, TUint16 v);
       
   113         TInt PosN(TUint16 index, TInt minFrequency);
   120 
   114 
   121         void Initialize(TUint8* data, TInt size);
   115         void Initialize(TUint8* data, TInt size);
   122         TInt MostCommonPair(TInt& pair);
   116         TInt MostCommonPair(TInt& pair, TInt minFrequency);
   123         TInt LeastCommonByte(TInt& byte);
   117         TInt LeastCommonByte(TInt& byte);
   124         inline void InsertPair(const TUint16 pair, const TUint16 pos) {
   118         inline void InsertPair(const TUint16 pair, const TUint16 pos) {
   125             //ClockInsert1 = clock();
   119             //ClockInsert1 = clock();
   126             //cout << "Pair:" << hex << setw(4) << setfill ('0') << pair << endl;
   120             //cout << "Pair:" << hex << setw(4) << setfill ('0') << pair << endl;
   127             TUint16 count = PairCount[pair].Count;
   121             TUint16 count = PairCount[pair].Count;
   342                 
   336                 
   343             }
   337             }
   344         }
   338         }
   345 #endif
   339 #endif
   346 	public:
   340 	public:
   347         CBytePair(TBool fastCompress) {
       
   348             iFastCompress = fastCompress; 
       
   349         }
       
   350         TInt Compress(TUint8* dst, TUint8* src, TInt size);
   341         TInt Compress(TUint8* dst, TUint8* src, TInt size);
   351         TInt Decompress(TUint8* dst, TInt dstSize, TUint8* src, TInt srcSize, TUint8*& srcNext);
   342         TInt Decompress(TUint8* dst, TInt dstSize, TUint8* src, TInt srcSize, TUint8*& srcNext);
   352 };
   343 };
   353 TInt BytePairCompress(TUint8* dst, TUint8* src, TInt size, CBytePair *aBPE);
   344 TInt BytePairCompress(TUint8* dst, TUint8* src, TInt size, CBytePair *aBPE);
   354 
   345