📄 spiht.cpp
字号:
/*
* Parepare matrix which contains data reconstructed.
*/
/*扩展后的图像大小*/
pXDim = nXDim;/*注意此处要赋初值*/
pYDim = nYDim;
if ((nXDim>>level)<<level != nXDim)
{
pXDim = ((nXDim>>level)<<level) + (1<<level);
}
if((nYDim>>level)<<level != nYDim)
{
pYDim = ((nYDim>>level)<<level) + (1<<level);
}
SetSubbandSize(pXDim,pYDim);
MR = new CMatrix2D<ElementType>;
MR->Create(pYDim,pXDim);
if (MR==NULL)
cout<<"Warning: Failed to create matrix!\n";
MR->Clear();
/*
* Prepare to decoding input file.
*/
threshold = header.threshold;
inputbyte = 0;
mask = 0;
BitCount = 0;
zeroes = 0;
ones = 0;
stop = 0;
/*
* Do the SPIHT decoding.
*/
Initialization();
pass_count = 0;
while(threshold!=0)
{
pass_count++;
SortingPass2(MR,threshold);
RefinementPass2(MR,threshold);
threshold >>= 1;
if(stop==1)
break;
}
DestroySubsize();
if(cAction == FROM_DISK)
fclose(SPIHTFile);
//输出解码后的系数
FILE * ptr=fopen("dec.w","w");
for(int i = 0; i < nYDim; i++){
for(int j = 0; j < nXDim; j++){
fprintf(ptr,"%4d",MR->m[i][j]);
}
fprintf(ptr,"\n");
}
fclose(ptr);
/*
* Release memory used.
*/
MR->Destroy();
LIP->Destroy();
LIS->Destroy();
LSP->Destroy();
pByte = CompressedFileInfo.pFirstByte;
}
char CSPIHT::GetBit()
{
char bit;
static long lByteCount = -1;
static unsigned long lTotalBits = (unsigned long)ceil(nYDim*nXDim*BitRate);
if (mask==0) {
lByteCount++;
inputbyte = *pByte;
pByte++;
mask = 0x80;
}
if(BitCount>lTotalBits){
stop = 1;
return 0;
}
if ((inputbyte&mask)==0)
bit = 0;
else
bit = 1;
mask >>= 1;
BitCount++;
return (bit);
}
void CSPIHT::SortingPass2(CMatrix2D<ElementType> *m, int threshold)
{
int temp, s, i, j;
int maxx,maxy;
int band;
char found;
CLinkList::ListType d, d1;
pNewlyAppended = LSP->end;
/* Deal with elements in the LIP */
if(stop==1)
return;
while(LIP->current!=NULL)
{
d = LIP->GetCurrentElement(&found);
if(found==0){
cout<<"error: can't get list element!\n";
return;
}
s = GetBit();
if(stop==1)
return;
if(s==0) {
LIP->current = LIP->current->next;
}
else
{
LIP->RemoveCurrentElement();
LSP->Append(d);
s = GetBit();
if(stop==1)
return;
temp = threshold+(threshold>>1);
if(s==1)
m->m[d.y][d.x] = temp;
else
m->m[d.y][d.x] = -temp;
}
}
LIP->Reset();
/* Deal with elements in the LIS */
while(LIS->current!=NULL)
{
d = LIS->GetCurrentElement(&found);
if(found==0) {
cout<<"error: can't get list element!\n";
return;
}
band = FindSubband(d.x,d.y);
if(d.type==TYPE_A)
{
s = GetBit();
if(stop==1)
return;
if(s==1)
{
if(band!=0)
{
if (band%3 == 1)
{
maxx = (d.x-subbandxsize[band]) << 1;
maxy = d.y << 1;
}
else if (band%3 == 2)
{
maxx=d.x << 1;
maxy=(d.y-subbandysize[band]) << 1;
}
else
{
maxx=(d.x-subbandxsize[band]) << 1;
maxy=(d.y-subbandysize[band]) << 1;
}
for(j = maxy; j < maxy+2; j++)
{
for(i = maxx; i < maxx+2; i++)
{
if(band%3 == 1)
{
d1.x = subbandxsize[band+3]+i;
d1.y = j;
}
else if(band%3 == 2)
{
d1.x = i;
d1.y = subbandysize[band+3]+j;
}
else
{
d1.x = subbandxsize[band+3]+i;
d1.y = subbandysize[band+3]+j;
}
s = GetBit();
if(stop==1)
return;
if(s==1)
{
LSP->Append(d1);
s = GetBit();
if(stop==1)
return;
temp = threshold+(threshold>>1);
if(s==1)
m->m[d1.y][d1.x] = temp;
else
m->m[d1.y][d1.x] = -temp;
}
else
{
LIP->Append(d1);
}
}
}
}
else
{
if((d.x%2 != 0)&&(d.y%2 == 0))
{
maxx = d.x -1;
maxy = d.y;
for(j = maxy; j < maxy + 2; j++)
{
for(i = maxx; i < maxx + 2; i++)
{
d1.x = subbandxsize[0]+i;
d1.y = j;
s = GetBit();
if(stop==1)
return;
if(s==1)
{
LSP->Append(d1);
s = GetBit();
if(stop==1)
return;
temp = threshold+(threshold>>1);
if(s==1)
m->m[d1.y][d1.x] = temp;
else
m->m[d1.y][d1.x] = -temp;
}
else
{
LIP->Append(d1);
}
}
}
}
if((d.x%2 == 0)&&(d.y%2 != 0))
{
maxx = d.x;
maxy = d.y -1;
for(j = maxy; j < maxy + 2; j++)
{
for(i = maxx; i < maxx + 2; i++)
{
d1.x = i;
d1.y = subbandysize[0]+j;
s = GetBit();
if(stop==1)
return;
if(s==1)
{
LSP->Append(d1);
s = GetBit();
if(stop==1)
return;
temp = threshold+(threshold>>1);
if(s==1)
m->m[d1.y][d1.x] = temp;
else
m->m[d1.y][d1.x] = -temp;
}
else
{
LIP->Append(d1);
}
}
}
}
if((d.x%2 != 0)&&(d.y%2 != 0))
{
maxx = d.x - 1;
maxy = d.y - 1;
for(j = maxy; j < maxy + 2; j++)
{
for(i = maxx; i < maxx + 2; i++)
{
d1.x = subbandxsize[0]+i;
d1.y = subbandysize[0]+j;
s = GetBit();
if(stop==1)
return;
if(s==1)
{
LSP->Append(d1);
s = GetBit();
if(stop==1)
return;
temp = threshold+(threshold>>1);
if(s==1)
m->m[d1.y][d1.x] = temp;
else
m->m[d1.y][d1.x] = -temp;
}
else
{
LIP->Append(d1);
}
}
}
}
}
//如果有孙子节点
if(band < subbands - 6)
{
d1 = d;
d1.type = TYPE_B;
LIS->Append(d1);
}
LIS->RemoveCurrentElement();
}
else
LIS->current = LIS->current->next;
}
else
{
s = GetBit();
if(stop==1)
return;
if(s==1)
{
if(band != 0)
{
if (band%3 == 1)
{
maxx = (d.x-subbandxsize[band]) << 1;
maxy = d.y << 1;
}
else if (band%3 == 2)
{
maxx = d.x << 1;
maxy = (d.y-subbandysize[band]) << 1;
}
else
{
maxx = (d.x-subbandxsize[band]) << 1;
maxy = (d.y-subbandysize[band]) << 1;
}
for(j = maxy; j < maxy+2; j++)
{
for(i = maxx; i < maxx+2; i++)
{
if(band%3 == 1)
{
d1.x = subbandxsize[band+3]+i;
d1.y = j;
}
else if(band%3 == 2)
{
d1.x = i;
d1.y = subbandysize[band+3]+j;
}
else
{
d1.x = subbandxsize[band+3]+i;
d1.y = subbandysize[band+3]+j;
}
d1.type = TYPE_A;
LIS->Append(d1);
}
}
LIS->RemoveCurrentElement();
}
else
{
if((d.x%2 != 0)&&(d.y%2 == 0))
{
maxx = d.x -1;
maxy = d.y;
for(j = maxy; j < maxy + 2; j++)
{
for(i = maxx; i < maxx + 2; i++)
{
d1.x = subbandxsize[0]+i;
d1.y = j;
d1.type = TYPE_A;
LIS->Append(d1);
}
}
}
if((d.x%2 == 0)&&(d.y%2 != 0))
{
maxx = d.x;
maxy = d.y -1;
for(j = maxy; j < maxy + 2; j++)
{
for(i = maxx; i < maxx + 2; i++)
{
d1.x = i;
d1.y = subbandysize[0]+j;
d1.type = TYPE_A;
LIS->Append(d1);
}
}
}
if((d.x%2 != 0)&&(d.y%2 != 0))
{
maxx = d.x - 1;
maxy = d.y - 1;
for(j = maxy; j < maxy + 2; j++)
{
for(i = maxx; i < maxx + 2; i++)
{
d1.x = subbandxsize[0]+i;
d1.y = subbandysize[0]+j;
d1.type = TYPE_A;
LIS->Append(d1);
}
}
}
LIS->RemoveCurrentElement();
}
}
else
LIS->current = LIS->current->next;
}
}
LIS->Reset();
}
void CSPIHT::RefinementPass2(CMatrix2D<ElementType> *m, int threshold)
{
CLinkList::ListElement *p;
int s, i, j,temp;
if(pNewlyAppended==NULL)
return;
p = LSP->head;
while(p!=pNewlyAppended->next)
{
s = GetBit();
if(stop==1)
return;
i = p->data.x;
j = p->data.y;
temp = abs(m->m[j][i]);
if(s==1)
temp |= (threshold>>1);
else{
temp &= (~threshold);
temp |= (threshold>>1);
}
if(m->m[j][i]>=0)
m->m[j][i] = temp;
else
m->m[j][i] = -temp;
p = p->next;
}
}
void CSPIHT::SetBitRate(float b)
{
BitRate = b;
}
void CSPIHT::SetLevel(char l)
{
level = l;
}
void CSPIHT::SetImageSize(int x, int y)
{
nXDim = x;
nYDim = y;
}
//////////////////////////////////////////////////////////////////////////
//ENCODE需要输入参数:
// 图像高
// 图像宽
// 小波级数
// BITRATE
// 存放小波系数的文件"enc.w",以文本格式存放和读取
// 编码后的文件"out.spiht",以二进制格式存放
//
//////////////////////////////////////////////////////////////////////////
void main()
{
CSPIHT *op = new CSPIHT;
int * ever = new int;
int xsize=233;//图像宽度
int ysize=230;//图像高度
FILE * ptr=NULL;
int * file = new int[xsize*ysize];
op->SetImageSize(xsize,ysize);
op->SetBitRate(1.3);
op->SetLevel(5);
// ofstream fout(op->strFileNameOut,ios::app);//以附加模式打开
// ifstream fin(op->strFileNameIn);
// if (!fin) {
// cout << "error: unable to open input file!\n";
// return;
// }
// if (!fout) {
// cout << "error: unable to open output file!\n";
// return;
// }
//char *buf = new char [256*256];
// for(int i = 0; i < 256*256; i++){
// fin >> buf[i];
// file[i] = atoi(&buf[i]);
// }
if((ptr=fopen(op->strFileNameIn,"r")) == NULL)
cout<<"The enc.w file was not opened"<<endl;
for(int i = 0; i < ysize; i++){
for(int j = 0; j < xsize; j++){
fscanf(ptr,"%4d",ever);
file[i*xsize+j] = *ever;
}
}
fclose(ptr);
delete ever;
op->Encode(file);
// op->strFileNameIn = op->strFileNameOut;
// op->Decode();
delete op;
delete []file;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -