有兴趣可以看看 因为有些部分不是我写的 所以我就不传全部了 只传我写的代码的一部分
如果能看懂小波变换与编码算法思想的人 应该可以看懂这段代码 否则还是不要看了
这段是EBCOT编码 当年我就这个编码花了比较长的时间才搞明白 其他的 EZW S+P等几个简单的 如果看懂了算法 代码很容易就可以编好的 所以那些代码就不帖了 我也不知道丢在哪里去了
Code
#include <time.h>
#ifndef _EBCOT_H_
#define _EBCOT_H_
#include "stdio.h"
#include "stdlib.h"
#include "mq.h"
#define KRUN 9
#define KUNI 18
#define LL 0
#define LH 1
#define HL 2
#define HH 3
static char
SIGNIFICANCE_TABLE1[3][3][5] =
{
0,1,2,2,2,
3,3,3,3,3,
4,4,4,4,4,
5,6,6,6,6,
7,7,7,7,7,
7,7,7,7,7,
8,8,8,8,8,
8,8,8,8,8,
};
static char
SIGNIFICANCE_TABLE2[3][3][5] =
{
0,1,2,2,2,
5,6,6,6,6,
8,8,8,8,8,
3,3,3,3,3,
7,7,7,7,7,
4,4,4,4,4,
7,7,7,7,7,
8,8,8,8,8,
};
static char
SIGNIFICANCE_TABLE3[5][5] =
{
0,1,2,2,2,
3,4,5,5,5,
6,7,7,7,7,
8,8,8,8,8,
8,8,8,8,8,
};
static char
SIGN_TABLE[3][3][2] =
{
14,-1,
13,-1,
12,-1,
11,-1,
10,1,
11,1,
12,1,
13,1,
14,1,
};
#define HEIGHT 42
#define WIDTH 51
struct Tile
{
short int value[HEIGHT][WIDTH];
char sign[HEIGHT][WIDTH];
char sig_state[HEIGHT][WIDTH];
char sig_state1[HEIGHT][WIDTH];
char tilekind;
char bp;
int tileHeight;
int tileWidth;
};
struct Tile tile;
char Sig(int h,int w)
{
// return tile.sig_state[h-1][w-1]+tile.sig_state[h-1][w]+tile.sig_state[h-1][w+1]
// + tile.sig_state[h][w-1]+tile.sig_state[h][w+1]
// + tile.sig_state[h+1][w-1]+tile.sig_state[h+1][w]+tile.sig_state[h+1][w+1];
return tile.sig_state[h][w-1]+tile.sig_state[h][w+1];
/*
int kh=tile.sig_state[h][w-1] + tile.sig_state[h][w+1];
int kv=tile.sig_state[h-1][w] + tile.sig_state[h+1][w];
int kd=tile.sig_state[h-1][w-1] + tile.sig_state[h-1][w+1] + tile.sig_state[h+1][w-1] + tile.sig_state[h+1][w+1];
if(tile.tilekind==HL) return SIGNIFICANCE_TABLE2[kh][kv][kd];
if(tile.tilekind=HH) return SIGNIFICANCE_TABLE3[kd][kh+kv];
return SIGNIFICANCE_TABLE1[kh][kv][kd];
*/
}
void Get_KSign_XFlip(int h,int w,char * Ksign,char * Xflip)
{
int xh=tile.sign[h][w-1]*tile.sig_state[h][w-1]+tile.sign[h][w+1]*tile.sig_state[h][w+1];
int xv=tile.sign[h-1][w]*tile.sig_state[h-1][w]+tile.sign[h+1][w]*tile.sig_state[h+1][w];
int _xh=min(1,xh)*xh>0?1:-1;
int _xv=min(1,xv)*xv>0?1:-1;
*Ksign=SIGN_TABLE[_xh+1][_xv+1][0];
*Xflip=SIGN_TABLE[_xh+1][_xv+1][1];
}
char Kmag(int h,int w)
{
// char Ksign,Xflip;
return tile.sig_state[h][w]+16;
// if(tile.sig_state[h][w]==1) return 17;
// Get_KSign_XFlip(h,w,&Ksign,&Xflip);
// if(Ksign>0) return 16;
return 15;
}
//----------------------------------------
static char lastSign=1;
void Encode_Sign(int h,int w)
{
if(tile.sign[h][w]==lastSign)
{
mq_encode(0,10);
}
else
{
mq_encode(1,10);
lastSign ^= 1;
}
/*
char Ksign,Xflip;
Get_KSign_XFlip(h,w,&Ksign,&Xflip);
if(tile.sign[h][w]*Xflip==1)
{
mq_encode(0,Ksign);
}
else
{
mq_encode(1,Ksign);
}
*/
}
void Decode_Sign(int h,int w)
{
int x=mq_decode(10);
if(x==0)
{
tile.sign[h][w]=lastSign;
}
else
{
lastSign ^= 1;
tile.sign[h][w]=lastSign;
}
/*
char Ksign,Xflip;
Get_KSign_XFlip(h,w,&Ksign,&Xflip);
int x=mq_decode(Ksign);
if(x==0)
{
tile.sign[h][w]=Xflip;
}
else
{
tile.sign[h][w]=-Xflip;
}
*/
}
void SeparateSign()
{
int w,h;
for(h=0; h<HEIGHT; h++)
for(w=0; w<WIDTH; w++)
{
if(tile.value[h][w]>=0)
{
tile.sign[h][w]=1;
}
else
{
tile.sign[h][w]=0;
tile.value[h][w]=-tile.value[h][w];
}
}
}
void CombinationSign()
{
int w,h;
for(h=0; h<HEIGHT; h++)
for(w=0; w<WIDTH; w++)
{
if(tile.sign[h][w]==0)
tile.value[h][w]=-tile.value[h][w];
}
}
void Pass2()
{
int h,w;
lastSign=1;
for(h=1; h<=tile.tileHeight; h++)
for(w=1; w<tile.tileWidth; w+=4)
{
char Vp;
char r=0;
if( (!tile.sig_state[h][w-1]) && (!tile.sig_state[h][w]) && (!tile.sig_state[h][w+1]) && (!tile.sig_state[h][w+2]) && (!tile.sig_state[h][w+3]) && (!tile.sig_state[h][w+4]) )
{
Vp=(tile.value[h][w+r] >> tile.bp) & 1;
while(Vp==0)
{
if(++r==4) break;
Vp=(tile.value[h][w+r] >> tile.bp) & 1;
}
if(r==4)
{
mq_encode(0,KRUN);
}
else
{
mq_encode(1,KRUN);
mq_encode(r/2,KUNI);
mq_encode(r%2,KUNI);
}
}
for(; r<4; r++)
{
if(tile.sig_state[h][w+r]==0 )
{
Vp=(tile.value[h][w+r] >> tile.bp) & 1;
mq_encode(Vp, Sig(h,w+r) );
if(Vp)
{
tile.sig_state[h][w+r]=1;
// tile.sig_state1[h][w+r]=1;
Encode_Sign(h,w+r);
}
}
else
{
Vp=(tile.value[h][w+r] >> tile.bp) & 1;
mq_encode(Vp,Kmag(h,w+r) );
// tile.sig_state[h][w+r]=tile.sig_state1[h][w+r];
}
}
}
}
void De_Pass2()
{
int h,w;
lastSign=1;
for(h=1; h<=tile.tileHeight; h++)
for(w=1; w<tile.tileWidth; w+=4)
{
char Vp;
char r=0,x;
if( (!tile.sig_state[h][w-1]) && (!tile.sig_state[h][w]) && (!tile.sig_state[h][w+1]) && (!tile.sig_state[h][w+2]) && (!tile.sig_state[h][w+3]) && (!tile.sig_state[h][w+4]) )
{
x=mq_decode(KRUN);
if(x==0)
{
r=4;
}
else
{
r=mq_decode(KUNI);
r=2*r+mq_decode(KUNI);
//tile.value[h+r][w] |= (1<<tile.bp);
}
}
for(; r<4; r++)
{
if(tile.sig_state[h][w+r]==0 )
{
Vp=mq_decode( Sig(h,w+r) );
tile.value[h][w+r] |= (Vp<<tile.bp);
if(Vp)
{
tile.sig_state[h][w+r]=1;
// tile.sig_state1[h][w+r]=1;
Decode_Sign(h,w+r);
}
}
else
{
tile.value[h][w+r] |= mq_decode( Kmag(h,w+r) )<<tile.bp;
//tile.sig_state[h][w+r]=tile.sig_state1[h][w+r];
}
}
}
}
void InitEncode()
{
memset(tile.value, 0, sizeof(short int)*WIDTH*HEIGHT);
}
int Encode(int highLayer,int lowLayer,unsigned char * pStream)
{
int length=0;
if(highLayer<lowLayer) return length;
mq_enc_init(pStream);
memset(tile.sign, 0 ,sizeof(char)*3*WIDTH*HEIGHT);
SeparateSign();
tile.bp=highLayer;
Pass2( );
while(--tile.bp>=lowLayer)
{
Pass2();
}
length=mq_enc_flush();
//mq_enc_flush();
mq_enc_destroy();
return length;
}
int Decode(int highLayer,int lowLayer,unsigned char * pStream)
{
int length=0;
if(highLayer<lowLayer) return length;
mq_dec_init(pStream);
memset(tile.sign, 0 ,sizeof(char)*3*WIDTH*HEIGHT);
memset(tile.value, 0, sizeof(short int)*WIDTH*HEIGHT);
tile.bp=highLayer;
De_Pass2();
while(--tile.bp>=lowLayer)
{
De_Pass2();
}
CombinationSign();
length=mq_dec_len();
mq_dec_destroy();
return length;
}
unsigned char stream[100000];
int testEBC2()
{
short int vv[HEIGHT][WIDTH];
int h, w;
int count=0,c=1;
tile.tilekind=LL;
tile.tileHeight=10;
tile.tileWidth=10;
memset(tile.value, 0, sizeof(short int )*WIDTH*HEIGHT);
tile.value[0][0]=0;
tile.value[1][0]=0;
tile.value[2][0]=0;
tile.value[3][0]=0;
tile.value[4][0]=0;
tile.value[0][1]=0;
tile.value[1][1]=-22;
tile.value[2][1]=-1;
tile.value[3][1]=18;
tile.value[4][1]=-9;
for(h=1; h<=tile.tileHeight; h++)
for(w=1; w<=tile.tileWidth; w++)
{
tile.value[h][w]=(h+w)%256;
vv[h][w]=tile.value[h][w];
}
Encode(7,0,stream);
memset(tile.value,0,sizeof( short int )*WIDTH*HEIGHT);
Decode(7,0,stream);
for(h=1; h<=tile.tileHeight; h++)
for(w=1; w<=tile.tileWidth; w++)
{
c=vv[h][w]-tile.value[h][w];
if(c!=0)
{
count++;
}
}
return 0;
}
#endif
#include <time.h>
#ifndef _EBCOT_H_
#define _EBCOT_H_
#include "stdio.h"
#include "stdlib.h"
#include "mq.h"
#define KRUN 9
#define KUNI 18
#define LL 0
#define LH 1
#define HL 2
#define HH 3
static char
SIGNIFICANCE_TABLE1[3][3][5] =
{
0,1,2,2,2,
3,3,3,3,3,
4,4,4,4,4,
5,6,6,6,6,
7,7,7,7,7,
7,7,7,7,7,
8,8,8,8,8,
8,8,8,8,8,
};
static char
SIGNIFICANCE_TABLE2[3][3][5] =
{
0,1,2,2,2,
5,6,6,6,6,
8,8,8,8,8,
3,3,3,3,3,
7,7,7,7,7,
4,4,4,4,4,
7,7,7,7,7,
8,8,8,8,8,
};
static char
SIGNIFICANCE_TABLE3[5][5] =
{
0,1,2,2,2,
3,4,5,5,5,
6,7,7,7,7,
8,8,8,8,8,
8,8,8,8,8,
};
static char
SIGN_TABLE[3][3][2] =
{
14,-1,
13,-1,
12,-1,
11,-1,
10,1,
11,1,
12,1,
13,1,
14,1,
};
#define HEIGHT 42
#define WIDTH 51
struct Tile
{
short int value[HEIGHT][WIDTH];
char sign[HEIGHT][WIDTH];
char sig_state[HEIGHT][WIDTH];
char sig_state1[HEIGHT][WIDTH];
char tilekind;
char bp;
int tileHeight;
int tileWidth;
};
struct Tile tile;
char Sig(int h,int w)
{
// return tile.sig_state[h-1][w-1]+tile.sig_state[h-1][w]+tile.sig_state[h-1][w+1]
// + tile.sig_state[h][w-1]+tile.sig_state[h][w+1]
// + tile.sig_state[h+1][w-1]+tile.sig_state[h+1][w]+tile.sig_state[h+1][w+1];
return tile.sig_state[h][w-1]+tile.sig_state[h][w+1];
/*
int kh=tile.sig_state[h][w-1] + tile.sig_state[h][w+1];
int kv=tile.sig_state[h-1][w] + tile.sig_state[h+1][w];
int kd=tile.sig_state[h-1][w-1] + tile.sig_state[h-1][w+1] + tile.sig_state[h+1][w-1] + tile.sig_state[h+1][w+1];
if(tile.tilekind==HL) return SIGNIFICANCE_TABLE2[kh][kv][kd];
if(tile.tilekind=HH) return SIGNIFICANCE_TABLE3[kd][kh+kv];
return SIGNIFICANCE_TABLE1[kh][kv][kd];
*/
}
void Get_KSign_XFlip(int h,int w,char * Ksign,char * Xflip)
{
int xh=tile.sign[h][w-1]*tile.sig_state[h][w-1]+tile.sign[h][w+1]*tile.sig_state[h][w+1];
int xv=tile.sign[h-1][w]*tile.sig_state[h-1][w]+tile.sign[h+1][w]*tile.sig_state[h+1][w];
int _xh=min(1,xh)*xh>0?1:-1;
int _xv=min(1,xv)*xv>0?1:-1;
*Ksign=SIGN_TABLE[_xh+1][_xv+1][0];
*Xflip=SIGN_TABLE[_xh+1][_xv+1][1];
}
char Kmag(int h,int w)
{
// char Ksign,Xflip;
return tile.sig_state[h][w]+16;
// if(tile.sig_state[h][w]==1) return 17;
// Get_KSign_XFlip(h,w,&Ksign,&Xflip);
// if(Ksign>0) return 16;
return 15;
}
//----------------------------------------
static char lastSign=1;
void Encode_Sign(int h,int w)
{
if(tile.sign[h][w]==lastSign)
{
mq_encode(0,10);
}
else
{
mq_encode(1,10);
lastSign ^= 1;
}
/*
char Ksign,Xflip;
Get_KSign_XFlip(h,w,&Ksign,&Xflip);
if(tile.sign[h][w]*Xflip==1)
{
mq_encode(0,Ksign);
}
else
{
mq_encode(1,Ksign);
}
*/
}
void Decode_Sign(int h,int w)
{
int x=mq_decode(10);
if(x==0)
{
tile.sign[h][w]=lastSign;
}
else
{
lastSign ^= 1;
tile.sign[h][w]=lastSign;
}
/*
char Ksign,Xflip;
Get_KSign_XFlip(h,w,&Ksign,&Xflip);
int x=mq_decode(Ksign);
if(x==0)
{
tile.sign[h][w]=Xflip;
}
else
{
tile.sign[h][w]=-Xflip;
}
*/
}
void SeparateSign()
{
int w,h;
for(h=0; h<HEIGHT; h++)
for(w=0; w<WIDTH; w++)
{
if(tile.value[h][w]>=0)
{
tile.sign[h][w]=1;
}
else
{
tile.sign[h][w]=0;
tile.value[h][w]=-tile.value[h][w];
}
}
}
void CombinationSign()
{
int w,h;
for(h=0; h<HEIGHT; h++)
for(w=0; w<WIDTH; w++)
{
if(tile.sign[h][w]==0)
tile.value[h][w]=-tile.value[h][w];
}
}
void Pass2()
{
int h,w;
lastSign=1;
for(h=1; h<=tile.tileHeight; h++)
for(w=1; w<tile.tileWidth; w+=4)
{
char Vp;
char r=0;
if( (!tile.sig_state[h][w-1]) && (!tile.sig_state[h][w]) && (!tile.sig_state[h][w+1]) && (!tile.sig_state[h][w+2]) && (!tile.sig_state[h][w+3]) && (!tile.sig_state[h][w+4]) )
{
Vp=(tile.value[h][w+r] >> tile.bp) & 1;
while(Vp==0)
{
if(++r==4) break;
Vp=(tile.value[h][w+r] >> tile.bp) & 1;
}
if(r==4)
{
mq_encode(0,KRUN);
}
else
{
mq_encode(1,KRUN);
mq_encode(r/2,KUNI);
mq_encode(r%2,KUNI);
}
}
for(; r<4; r++)
{
if(tile.sig_state[h][w+r]==0 )
{
Vp=(tile.value[h][w+r] >> tile.bp) & 1;
mq_encode(Vp, Sig(h,w+r) );
if(Vp)
{
tile.sig_state[h][w+r]=1;
// tile.sig_state1[h][w+r]=1;
Encode_Sign(h,w+r);
}
}
else
{
Vp=(tile.value[h][w+r] >> tile.bp) & 1;
mq_encode(Vp,Kmag(h,w+r) );
// tile.sig_state[h][w+r]=tile.sig_state1[h][w+r];
}
}
}
}
void De_Pass2()
{
int h,w;
lastSign=1;
for(h=1; h<=tile.tileHeight; h++)
for(w=1; w<tile.tileWidth; w+=4)
{
char Vp;
char r=0,x;
if( (!tile.sig_state[h][w-1]) && (!tile.sig_state[h][w]) && (!tile.sig_state[h][w+1]) && (!tile.sig_state[h][w+2]) && (!tile.sig_state[h][w+3]) && (!tile.sig_state[h][w+4]) )
{
x=mq_decode(KRUN);
if(x==0)
{
r=4;
}
else
{
r=mq_decode(KUNI);
r=2*r+mq_decode(KUNI);
//tile.value[h+r][w] |= (1<<tile.bp);
}
}
for(; r<4; r++)
{
if(tile.sig_state[h][w+r]==0 )
{
Vp=mq_decode( Sig(h,w+r) );
tile.value[h][w+r] |= (Vp<<tile.bp);
if(Vp)
{
tile.sig_state[h][w+r]=1;
// tile.sig_state1[h][w+r]=1;
Decode_Sign(h,w+r);
}
}
else
{
tile.value[h][w+r] |= mq_decode( Kmag(h,w+r) )<<tile.bp;
//tile.sig_state[h][w+r]=tile.sig_state1[h][w+r];
}
}
}
}
void InitEncode()
{
memset(tile.value, 0, sizeof(short int)*WIDTH*HEIGHT);
}
int Encode(int highLayer,int lowLayer,unsigned char * pStream)
{
int length=0;
if(highLayer<lowLayer) return length;
mq_enc_init(pStream);
memset(tile.sign, 0 ,sizeof(char)*3*WIDTH*HEIGHT);
SeparateSign();
tile.bp=highLayer;
Pass2( );
while(--tile.bp>=lowLayer)
{
Pass2();
}
length=mq_enc_flush();
//mq_enc_flush();
mq_enc_destroy();
return length;
}
int Decode(int highLayer,int lowLayer,unsigned char * pStream)
{
int length=0;
if(highLayer<lowLayer) return length;
mq_dec_init(pStream);
memset(tile.sign, 0 ,sizeof(char)*3*WIDTH*HEIGHT);
memset(tile.value, 0, sizeof(short int)*WIDTH*HEIGHT);
tile.bp=highLayer;
De_Pass2();
while(--tile.bp>=lowLayer)
{
De_Pass2();
}
CombinationSign();
length=mq_dec_len();
mq_dec_destroy();
return length;
}
unsigned char stream[100000];
int testEBC2()
{
short int vv[HEIGHT][WIDTH];
int h, w;
int count=0,c=1;
tile.tilekind=LL;
tile.tileHeight=10;
tile.tileWidth=10;
memset(tile.value, 0, sizeof(short int )*WIDTH*HEIGHT);
tile.value[0][0]=0;
tile.value[1][0]=0;
tile.value[2][0]=0;
tile.value[3][0]=0;
tile.value[4][0]=0;
tile.value[0][1]=0;
tile.value[1][1]=-22;
tile.value[2][1]=-1;
tile.value[3][1]=18;
tile.value[4][1]=-9;
for(h=1; h<=tile.tileHeight; h++)
for(w=1; w<=tile.tileWidth; w++)
{
tile.value[h][w]=(h+w)%256;
vv[h][w]=tile.value[h][w];
}
Encode(7,0,stream);
memset(tile.value,0,sizeof( short int )*WIDTH*HEIGHT);
Decode(7,0,stream);
for(h=1; h<=tile.tileHeight; h++)
for(w=1; w<=tile.tileWidth; w++)
{
c=vv[h][w]-tile.value[h][w];
if(c!=0)
{
count++;
}
}
return 0;
}
#endif