⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spiht.cpp

📁 根据paper思想
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// SPIHT.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "SPIHT.h"
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

//CWinApp theApp;
//
//using namespace std;
//
//int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
//{
//	int nRetCode = 0;
//
//	// initialize MFC and print and error on failure
//	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
//	{
//		// TODO: change error code to suit your needs
//		cerr << _T("Fatal Error: MFC initialization failed") << endl;
//		nRetCode = 1;
//	}
//	else
//	{
//		// TODO: code your application's behavior here.
//		CString strHello;
//		strHello.LoadString(IDS_HELLO);
//		cout << (LPCTSTR)strHello << endl;
//	}
//
//	return nRetCode;


//////////////////////////////////////////////////////////////////////////
// CLinkList Construction/Destruction
//////////////////////////////////////////////////////////////////////////

CLinkList::CLinkList()
{
	head = new ListElement;
	current = new ListElement;
	end = new ListElement;
	length = 0;
}

CLinkList::~CLinkList()
{
}

void CLinkList::Create()
{  
	head = NULL;
	current = NULL;
	end = NULL;
	length = 0;
}


void CLinkList::Append(ListType d)
{
	ListElement *p = (ListElement *) new ListElement;
  if (p!=NULL) {
    p->data = d;
    p->next = NULL;
	p->previous = end;
    if (end==NULL)
	{
		head = p;
		current = p;
		end = p;
	}
	else{
		 end->next = p;
         end = p;
	}
    length++;
  }
  else
	  assert(false);
}

void CLinkList::Destroy()
{
	ListElement *p;
	
	p = head;
	/*while (p!=NULL) {
		head = p->next;
	    delete p;
		p = head;
	}*/
	head = NULL;
	current = NULL;
	end = NULL;
	length = 0;
}

void CLinkList::Reset()
{
	current = head;
}

CLinkList::ListType CLinkList::GetCurrentElement(char *found)
{
	if(current==NULL)
	{
		assert(found);
	}
	else 
		*found = 1;
    return current->data;
	
}

void CLinkList::RemoveCurrentElement()
{
  ListElement *p;
  if (current==NULL){
	  assert(false);
	  return;
  }
  else if(current==head){	
	  if(end==current){
		 delete current;
		 end = NULL;
		 current = NULL;
		 head = NULL;
	  }
	  else
	  {
		 head = head->next;
		 head->previous = NULL;
		 delete current;
		 current = head;
	  }
  }
  else{	
	 p = current->previous;
	 p->next = current->next;
	 delete current;
	 current = p->next;
	 if(current!=NULL)
		 current->previous = p;
	 else
		 end = p;
  }
  length--;

}
//////////////////////////////////////////////////////////////////////////
//CSPIHT Construction/Destruction
//////////////////////////////////////////////////////////////////////////

CSPIHT::CSPIHT()
{
	strFileNameOut = "out.spiht";
	strFileNameIn = "enc.w";
	pNewlyAppended = NULL;
	cAction = TO_DISK;
	SPIHTFile = NULL;
	MR = NULL;
	M = NULL;
	LIS = NULL;
	LIP = NULL;
	LSP = NULL;
	CompressedFileInfo.pFirstByte = NULL;
	pByte = NULL;
}

CSPIHT::~CSPIHT()
{
	if(LSP!=NULL)
	{	
		delete LSP;
		LSP = NULL;
	}
	if(LIP!=NULL)
	{
		delete LIP;
		LIP = NULL;
	}
	if(LIS!=NULL)
	{
		delete LIS;
		LIS = NULL;
	}
	if(M!=NULL)
	{
		delete M;
		M = NULL;
	}
	if(MR!=NULL)
	{
		delete MR;
		MR =NULL;
	}
	if(pByte!=NULL)
	{
		delete[] pByte;
		pByte = NULL;
		}
}

void CSPIHT::PutBit(char bit)
{	
    static unsigned long lTotalBits = (unsigned long)(nYDim*nXDim*BitRate);
	if((zeroes+ones)>lTotalBits){
		stop = 1; 
		return;
	}
	
	if (bit==1){ 
		outputbyte |= mask;
		ones++;
	}
	else
		zeroes++;
	mask >>= 1;
	static long lByteCount = -1;
	if (mask==0) {
		lByteCount++;
		*pByte = outputbyte;
		pByte++;
		outputbyte = 0;
		mask = 0x80;
	}
}

void CSPIHT::SetSubbandSize(int x,int y)
{
	int i;
	int xsize = x;
	int ysize = y;
	subbandxsize = new int[subbands];
	assert(subbandxsize);
	subbandysize = new int[subbands];
	assert(subbandysize);

	
	for(i = header.level-1; i >= 0; i--)
	{
		subbandxsize[3*i+1] = (xsize>>=1);
		subbandysize[3*i+1] = (ysize>>=1);

		subbandxsize[3*i+2] = xsize;
		subbandysize[3*i+2] = ysize;

		subbandxsize[3*i+3] = xsize;
		subbandysize[3*i+3] = ysize;
	}
	subbandxsize[0] = xsize;
	subbandysize[0] = ysize;
		
}


int CSPIHT::FindSubband(int x,int y)
{
	int i;
	int temp;

	if((x < subbandxsize[0])&&(y < subbandysize[0]))
		return 0;
	
	for(i = level-1; i >= 0; i--)
	{
		if((x >= subbandxsize[3*i+1])&&(y < subbandysize[3*i+1]))
		{
			temp = 1;
			break;	
		}
		if((x < subbandxsize[3*i+1])&&(y >= subbandysize[3*i+1]))
		{
			temp = 2;
			break;
		}
		if((x >= subbandxsize[3*i+1])&&(y >= subbandysize[3*i+1]))
		{
			temp = 3;
			break;
		}
	}
	
	return (3*i+temp);	
}


void CSPIHT::DestroySubsize()
{
	delete []subbandxsize;
	delete []subbandysize;
	subbandxsize = NULL;
	subbandysize = NULL;
}

void CSPIHT::DumpBuffer()
{
	if(mask!=0)
		*pByte = outputbyte; 
	
}

void CSPIHT::Initialization()
{
	/* Create and initialize list LSP */
	CLinkList::ListType d;
	LSP = new CLinkList;
	LSP->Create();

    /* Create and initialize list LIP*/
	LIP = new CLinkList;
	LIP->Create();
	int i,j;
	for(j=0;j<subbandysize[0];j++)
	{
		for(i=0;i<subbandxsize[0];i++)
		{
			d.x = i;
			d.y = j;
			LIP->Append(d);
		}
	}

	///* Create and initialize list LIP
	//	LIP = new CLinkList;
	//	LIP->Create();
	//
	//		d.x = 0;
	//		d.y = 0;
	//		LIP->Append(d);
	//		d.x = 1;
	//		d.y = 0;
	//		LIP->Append(d);
	//		d.x = 0;
	//		d.y = 1;
	//		LIP->Append(d);
	//		d.x = 1;
	//		d.y = 1;
	//		LIP->Append(d);
	
	 
		
		
	/*Create and initialize list LIS*/
	LIS = new CLinkList;
	LIS->Create();
		
	for(j = 0; j < subbandysize[0]; j++)
	{
		for(i = 0; i < subbandxsize[0]; i++)
		{
			if((i%2 != 0)||(j%2 !=0))
			{
				d.x = i;
				d.y = j;
				d.type = TYPE_A;
				LIS->Append(d);
			}
				
		}
	}
		
 
		
		///* Create and initialize list LIS */
		//		LIS = new CLinkList;
		//		LIS->Create();
		//		d.x = 1;
		//		d.y = 0;
		//		d.type = TYPE_A;
		//		LIS->Append(d);
		//		d.x = 0;
		//		d.y = 1;
		//		LIS->Append(d);
		//		d.x = 1;
		//		d.y = 1;
		//		LIS->Append(d);  
		

		
}

BOOL CSPIHT::ZeroTree(CMatrix2D<ElementType> *m,int threshold,int band,int min_x,int max_x,int min_y,int max_y)
{
	int i, j;
	ElementType temp;
	char stop;
	
	stop = 0;


	if(band > subbands-3)
	{
		return (1);
	}	

	while ((band+3) < subbands)  
	{
		for (i=min_y; i<max_y; i++) 
		{
			for (j=min_x; j<max_x; j++) 
			{
				
				if(band%3 == 1)
				{
					temp = abs(m->m[i][subbandxsize[band+3]+j]);
				}
				else if(band%3 == 2)
				{
					temp = abs(m->m[subbandysize[band+3]+i][j]);
				}
				else
				{
					temp = abs(m->m[subbandysize[band+3]+i][subbandxsize[band+3]+j]);
				}
				
				if (temp>=threshold) 
				{
					stop = 1;
					break;
				}
			}
			if (stop==1) break;
		}
		if (stop==1) break;
		min_x <<= 1;
		max_x <<= 1;
		min_y <<= 1;
		max_y <<= 1;
		band += 3;
	}


	if (stop==1) return (0);
	return (1);
}

BOOL CSPIHT::TestSubset(CMatrix2D<ElementType> *m, int x, int y, int threshold, unsigned char type,int band)
{
    int i, j, stop;
	int min_x,min_y,max_x,max_y,temp,subband0;
	stop = 0;
	if(type==TYPE_A)
	{
		if(band==0)
		{
			if((x%2 != 0)&&(y%2 == 0))
			{
				min_x = x -1;
				max_x = min_x + 2;
				min_y = y;
				max_y = min_y + 2;
				for(i = min_y; i < max_y; i++)
				{
					for(j = min_x; j < max_x; j++)
					{
						temp = abs(m->m[i][subbandxsize[0]+j]);
						if (temp>=threshold) 
						{
							stop = 1;
							break;
						}
					}
					if(stop==1) break;
				}
				subband0 = 1;
			}
			if((x%2 == 0)&&(y%2 != 0))
			{
				min_x = x;
				max_x = min_x + 2;
				min_y = y - 1;
				max_y = min_y + 2;
				for(i = min_y; i < max_y; i++)
				{
					for(j = min_x; j < max_x; j++)
					{
						temp = abs(m->m[subbandysize[0]+i][j]);
						if (temp>=threshold) 
						{
							stop = 1;
							break;
						}
						
					}
					if(stop==1) break;
				}
				subband0 = 2;
			}
			if((x%2 != 0)&&(y%2 != 0))
			{
				min_x = x -1;
				max_x = min_x + 2;
				min_y = y -1;
				max_y = min_y + 2;
				for(i = min_y; i < max_y; i++)
				{
					for(j = min_x; j < max_x; j++)
					{
						temp = abs(m->m[subbandysize[0]+i][subbandxsize[0]+j]);
						if (temp>=threshold) 
						{
							stop = 1;
							break;
						}
						
					}
					if(stop==1) break;
				}
				subband0 = 3;
			}
			if(stop == 1) return(1);
			
			min_x <<= 1;
			max_x <<= 1;
			min_y <<= 1;
			max_y <<= 1;
			return (!ZeroTree(m,threshold,band+subband0,min_x,max_x,min_y,max_y));
		}
		else
		{
			if (band%3 == 1) 
			{
				min_x = (x-subbandxsize[band]) << 1;
				max_x = min_x + 2;
				min_y = y << 1;		
				max_y = min_y + 2;
			}
			else if(band%3 == 2) 
			{
				min_x = x << 1;
				max_x = min_x + 2;
				min_y = (y-subbandysize[band]) << 1;		
				max_y = min_y + 2;
			}
			else
			{
				min_x = (x-subbandxsize[band]) << 1;
				max_x = min_x + 2;
				min_y = (y-subbandysize[band]) << 1;		
				max_y = min_y + 2;
			}
			
			return (!ZeroTree(m,threshold,band,min_x,max_x,min_y,max_y));
		}
	}
	   
	else
	{
		if (band == 0)
		{
			if((x%2 != 0)&&(y%2 == 0))
			{
				min_x = x -1;
				max_x = min_x +2;
				min_y = y;
				max_y = min_y +2;
				subband0 = 1;

⌨️ 快捷键说明

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