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

📄 ezw.cpp

📁 单独的ezw编解码
💻 CPP
📖 第 1 页 / 共 4 页
字号:


//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

uLong magic_cookie = 0xf9a42bb1; /* Used to indicate "allocated" state */

/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
struct BlockHeader {
    uLong marker;
    long  time;
    struct BlockHeader* prev;
    struct BlockHeader* next;
    char*  file;
    long  lineNo;
    long  size;
#ifdef DOUBLE
    uchar dummy[4];   /* pad structure to a power of two */
#endif
};


/* --------------------- Static variables --------------------------------- */

static struct BlockHeader* _AllocListHead = 0; /* Head of list of */
                                               /* allocated blocks */
static struct BlockHeader* _AllocListTail = 0; /* Tail of list of */
                                               /* allocated blocks */
static short        _LeakCheckerActive = 0;
static long         _Time = 0;          /* Clock: ticks on every new and */
                                        /* delete */

static long         _MaxMem = 0;        /* Max memory used so far */
static long         _CurrentAlloc = 0;  /* Amount of memory currently */
                                        /* allocated */
static long         _BeginTime = 0;     /* Time at which leack checker was */
                                        /* instantiated */
/*------------------------------------------------------------------------- */

/* code */

/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*
 * Calloc function: same as Malloc, but initializes the content with
 *                  zeros.
 */
void *Calloc ( size_t n, size_t s, int line_no, char *file_name )
{
    size_t size = n*s;
    unsigned char *buffer = (unsigned char *)Malloc ( size, line_no, file_name );
    int i;

    for ( i=0 ; i<(int)size ; i++ )
        buffer[i] = 0;

    return buffer;
}


/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*
 * Realloc function: reallocates a contiguous segment of memory using
 *                   the same pointer variable.
 */
void *Realloc ( void *p, size_t s, int line_no, char *file_name )
{
    uchar *newp;
    struct BlockHeader* q;
    
    /* Check for integrity of memory in p */
    q = (struct BlockHeader*)
        ( (uchar*) p - sizeof (struct BlockHeader));
    if (q->marker != magic_cookie && _LeakCheckerActive)
        fprintf (stderr, "Realloc(%8lx): memory corrupted", (long)p);

    /* Allocate new segment and copy p into it */
    newp = (uchar *)Malloc ( s, line_no, file_name );
    newp = (uchar *)memcpy (newp, p, (size_t)q->size);

    /* Free p and return new segment */
    Free (p);
    return (void *)newp;
}


/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*
 * Malloc function: uses a linked list to keep track of all the memory
 *                  segments that have been allocated so far. It
 *                  includes information about the size of the segment,
 *                  the file where the allocation was done, the line
 *                  where the function was called, and the order in
 *                  which the call was executed.
 */
void* Malloc (size_t n, int line_no, char* file_name)
{
    struct BlockHeader* q;
    long size;
    uchar* p;

    if (n == 0)
        return NULL;
	
    size = (long)n;
    /* Allocate  extra bytes */
    p = (uchar*) malloc (n + sizeof (struct BlockHeader));
    if (!p) {
        fprintf (stderr, "Malloc(): allocating %u bytes: no memory!", n);
        exit (1);
    }
    
    _CurrentAlloc += (long)n;
    if (_CurrentAlloc > _MaxMem)
        _MaxMem = _CurrentAlloc;
    q = (struct BlockHeader*) p;
    /* Put a magic marker */
    q->marker = magic_cookie;
    q->time   = _Time++;
    q->size   = size;
    q->file   = file_name;
    q->lineNo = (long)line_no;
    memset (p + sizeof(struct BlockHeader),  '\02', (unsigned int) size);
    /* Uninitialized allocated memory has 02 in it */
    
	 /* Insert at tail of allocated list */
    if (_AllocListTail) {
        _AllocListTail->next = q;
        q->prev = _AllocListTail;
        q->next = 0;
        _AllocListTail = q;
    }
    else {
        _AllocListHead = _AllocListTail = q;
        q->prev = q->next = 0;
    }
    return p + sizeof(struct BlockHeader);
}

/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*
 * Free function: frees previously allocated memory and puts 03 in
 *                it to verify later on. The entry in the linked list
 *                is removed and all the other entries are reorganized.
 */
void Free (void* p)
{
    
    struct BlockHeader* q;
        
        if ( p == NULL )
            fprintf (stderr, "Free(%8lx): empty memory\n", (long)p);
        q = (struct BlockHeader*)
            ( (uchar*) p - sizeof (struct BlockHeader));
        if (q->marker != magic_cookie && _LeakCheckerActive)
            fprintf (stderr, "Free(%8lx): memory corrupted\n", (long)p);
    
        _CurrentAlloc -= q->size;
        if (_AllocListHead) {
            if (q->prev)
                q->prev->next = q->next;
            if (q->next)
                q->next->prev = q->prev;
            if (q == _AllocListHead)
                _AllocListHead = q->next;
            if (q == _AllocListTail)
                _AllocListTail = q->prev;
            memset (q,  '\03', (unsigned int) (sizeof(struct BlockHeader) +
                                               (unsigned int)q->size));
                /* Freed  memory has 03 in it */
        }
   
    free (q);
    _Time++;
}


/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*
 * PrintLeaks function: this routine prints the content of the linked
 *                      list with all the information about memory
 *                      allocated so far. If everything that was
 *                      allocated was freed, then the list should be
 *                      empty and nothing should be printed.
*/
int PrintLeaks ()
{

#ifdef __MEMCHK_ENABLE_

	struct BlockHeader* q = _AllocListHead;
	long count = 0;
	
	while (q) {
		if (q->time >= _BeginTime)
			count++;
		q = q->next;
	}
	if (count) {
		fprintf (stderr, "\nMemory status:\n"
			"--------------\n");
		fprintf(stderr, "Max:%d  Current:%d\n", MaxMemory(), CurrentMemoryAllocated());	
	
		q = _AllocListHead;
		while (q) {
			if (q->time >= _BeginTime) {
				/* Only output if the allocation occurred after the leak */
				/* checker was created */
				fprintf (stderr,
					"Time: %ld Address: %08x Size: %ld line %d file '%s'\n",
					q->time, (unsigned long)q, q->size,
					(int)q->lineNo, q->file);
			}
			q = q->next;
		}
		return 1;
	}
	else{
		//fprintf(stderr, "No allocated memory.\n");
		return 0;
	}

#else

	return 0;

#endif

}


/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*
 * FreeLeaks functions: frees intermediate allocated memory that cannot
 *                      be free'd by any other mean since an unexpected
 *                      error has occurred.
*/
void FreeLeaks ()
{

#ifdef __MEMCHK_ENABLE_
	struct BlockHeader* q   = _AllocListHead;
	struct BlockHeader* tmp;
	long count = 0;
	while (q) {
		if (q->time >= _BeginTime)
			count++;
		q = q->next;
	}
	if (count) {
		q = _AllocListHead;
		while (q) {
			tmp = q->next;
			if (q->time >= _BeginTime) {
				/* Only free if the allocation occurred after the leak */
				/* checker was created */
				free (q);
			}
			q = tmp;
		}
	}
	_AllocListHead = _AllocListTail = 0;
	_CurrentAlloc = 0;
#endif

}


/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*
 * MaxMemory functions: returns the maximum memory used so far. 
 *
 */
long MaxMemory()
{
#ifdef __MEMCHK_ENABLE_
	return _MaxMem;
#else
	return 0;
#endif
}

/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
/*
 * MaxMemory functions: returns the maximum memory used so far. 
 *
 */
long CurrentMemoryAllocated()
{
#ifdef __MEMCHK_ENABLE_
	return _CurrentAlloc;
#else
	return 0;
#endif
}
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
int EZWEncodeInterface(int xsize,int ysize,int level,double rate,int &maxbitplane)
{
	Coder *coder;
	int i,j;
	FILE *fp = NULL;
	int *file = NULL;
	int *ever = (int *)calloc(1,sizeof(int));
	TEST_MALLOC(ever);
	/*--------------------------------------------------------------------------------------------*/
	//编码的输入值:
	//小波系数coder->SubbandPtr[][];
	//分辨率级数coder->NScale;Note: if enc->curtile->tcmpts->numrlvls = 6
	//								then coder->NScale = 5	
	//图像大小coder->ImageXSize;coder->ImageYSize;
	//比特率rate
	//小波变换核coder->WaveletIndex;编解码本身没有用到,需要写入码流的头部
	/*--------------------------------------------------------------------------------------------*/
	coder = (Coder *)calloc(1,sizeof(Coder));
	TEST_MALLOC(coder);
	
	coder->FXSize = xsize;
	coder->FYSize = ysize;
	coder->NScale = level;
	coder->nSubbands = 3*coder->NScale+1;
	coder->TargetBitRate=rate;	

	
	//读入小波系数
	file = (int *)malloc((sizeof(int))*(coder->FXSize)*(coder->FYSize));
	if((fp=fopen("DwtR.txt","r")) == NULL)
		printf( "The file was not opened\n" );
	
	for(i = 0; i < ysize; i++){
		for(j = 0; j < xsize; j++){
			fscanf(fp,"%d ",ever);
			file[i*xsize+j] = *ever;
		}
	}
	fclose(fp);
	free(ever);
	/*-----------------------------------------------------------------------------------------*/
	//ezwcomp仅用于BuildCodingArrays中小波系数和子带大小的初始赋值
	/*-----------------------------------------------------------------------------------------*/
	EZWEncode(coder,file);

	//输出的参数,比特平面数
	maxbitplane = coder->MaxBitPlane;
	
	if (coder!=NULL) {
		free(coder->SubbandXSize);
		free(coder->SubbandYSize);
		free(coder->Coeff);
		free(coder->SubbandPtr);
		free(coder);
	}
	free(file);
	return 0;	
}

int EZWDecodeInterface(int xsize,int ysize,int level,double rate,int maxbitplane)
{
	Coder *coder;
	int i,j,k;
	FILE *fp = NULL;
	int *file = NULL;
	/*---------------------------------------------------------------------------------------------*/
	//decode的返回值:
	//小波系数
	//分辨率级数
	//图像大小
	/*---------------------------------------------------------------------------------------------*/	
	coder = (Coder *)calloc(1,sizeof(Coder));
	TEST_MALLOC(coder);
	sprintf(coder->EncodeImageFileName, "%s.%s", "Encoded", "txt");
	
	coder->FXSize = xsize;
	coder->FYSize = ysize;
	coder->NScale = level;
	coder->TargetBitRate = rate;
	coder->MaxBitPlane = maxbitplane;
	
	coder->nSubbands = 3*coder->NScale+1;
	coder->CurrentBitPlane = coder->MaxBitPlane;
	coder->MaxThreshold = 1<<coder->MaxBitPlane;
	coder->CurrentThreshold = coder->MaxThreshold;
	
	EZWDecode(coder);
	
	file = (int *)calloc(coder->ImageXSize*coder->ImageYSize,sizeof(int));
	for (j = 0; j < coder->SubbandYSize[0]; j++){
		for (i = 0; i < coder->SubbandXSize[0]; i++){
			file[j*coder->FXSize+i] = coder->SubbandPtr[0][j*coder->SubbandXSize[0]+i];
		}
	}
	
	for (k = 0; k < coder->NScale-1; k++) {
		for (j = 0; j < coder->SubbandYSize[3*k+1]; j++){
			for (i = 0; i < coder->SubbandXSize[3*k+1]; i++){
				file[j*coder->FXSize+(coder->SubbandXSize[3*k+1]+i)]
					= coder->SubbandPtr[3*k+1][j*coder->SubbandXSize[3*k+1]+i];
			}
		}
		
		for (j = 0; j < coder->SubbandYSize[3*k+2]; j++){
			for (i = 0; i < coder->SubbandXSize[3*k+2]; i++){
				file[(coder->SubbandYSize[3*k+2]+j)*coder->FXSize+i]
					= coder->SubbandPtr[3*k+2][j*coder->SubbandXSize[3*k+2]+i];
			}
		}
		
		for (j = 0; j < coder->SubbandYSize[k*3+3]; j++){
			for (i = 0; i < coder->SubbandXSize[k*3+3]; i++){
				file[(coder->SubbandYSize[k*3+3]+j)*coder->FXSize+(coder->SubbandXSize[k*3+3]+i)]
					= coder->SubbandPtr[3*k+3][j*coder->SubbandXSize[k*3+3]+i];
			}
		}
	}
	
	//highest pass
	k=coder->NScale -1;
	for(j = 0; j < coder->SubbandYSize[3*k+1]; j++){
		for(i = 0; i < coder->FXSize-coder->SubbandXSize[3*k+1];i++){
			file[j*coder->FXSize+(coder->SubbandXSize[3*k+1]+i)] = 
				coder->SubbandPtr[3*k+1][j*coder->SubbandXSize[3*k+1]+i];
		}
	}
	for(j = 0; j < coder->FYSize-coder->SubbandYSize[3*k+2]; j++){
		for(i = 0; i < coder->SubbandXSize[3*k+2];i++){				 
			file[(coder->SubbandYSize[3*k+2]+j)*coder->FXSize+i] = 
				coder->SubbandPtr[3*k+2][j*coder->SubbandXSize[3*k+2]+i];
		}
	}
	for(j = 0; j < coder->FYSize-coder->SubbandYSize[k*3+3]; j++){
		for(i = 0; i < coder->FXSize-coder->SubbandXSize[k*3+3]; i++){
			file[(coder->SubbandYSize[k*3+3]+j)*coder->FXSize+(coder->SubbandXSize[k*3+3]+i)] =
				coder->SubbandPtr[3*k+3][j*coder->SubbandXSize[k*3+3]+i];
		}
	}
	
	/*-----------------------------------------------------------------------------------*/
	//编码后的小波系数dec.txt
	if( (fp  = fopen( "IDwtS.txt", "w" )) == NULL )
			printf( "The file was not opened\n" );
	else
			printf( "The file was opened\n" );		
	for(i=0;i<coder->FYSize;i++){
		for(j=0;j<coder->FXSize;j++)
		{
			fprintf(fp,"%d ",file[i*coder->FXSize+j]);
		}
		fprintf(fp,"\n");
		}
		fclose(fp);
		free(file);
	/*-----------------------------------------------------------------------------------*/	
	if (coder!=NULL) {
		free(coder->SubbandXSize);
		free(coder->SubbandYSize);
		free(coder->Coeff);
		free(coder->SubbandPtr);
		free(coder);
	}
	return 0;
}
void main()
{
	int maxbitplane = 0;
	int xsize = 256;
	int ysize = 256;
	int scale = 6;
	double r = 1;
	int ind = 2;//0:编码;1:解码; 2:编解码同时进行
	if (ind == 0) 
	{
		EZWEncodeInterface(xsize,ysize,scale,r,maxbitplane);
			return ;
	}
	else if (ind == 1)
	{
		EZWDecodeInterface(xsize,ysize,scale,r,maxbitplane);
			return ;
	}
	else
	{
		EZWEncodeInterface(xsize,ysize,scale,r,maxbitplane);
			
			
		EZWDecodeInterface(xsize,ysize,scale,r,maxbitplane); 
				return ;
			
			
	}
	return;	
}



⌨️ 快捷键说明

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