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

📄 patterncoding.c

📁 CCSDS空间图像图像压缩标准c源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
Implementation of CCSDS 122.0-B-1 Recommended Standard
Please note:
(1)	Before you download and use the program, you must read and agree the license agreement carefully. 
(2)	We supply the source code and program WITHOUT ANY WARRANTIES. The users will be responsible 
        for any loses or damages caused by the use of the source code and the program. 

Author: 
Hongqiang Wang
Department of Electrical Engineering
University of Nebraska-Lincoln
Email: hqwang@bigred.unl.edu, hqwang@eecomm.unl.edu

Your comment and suggestions are welcome. Please report bugs to me via email and I would greatly appreciate it. 
Jan. 21, 2007
*/ 

#include <stdio.h>
#include <stdlib.h>
#include "global.h"

const int bit2_pattern[] = {0, 2, 1, 3};
const int bit3_pattern[] = {1, 4, 0, 5, 2, 6, 3, 7};
const int bit3_pattern_TranD[] = {0, 3, 0, 4, 1, 5, 2, 6};
const int bit4_pattern_TypeCi[] = {10, 1, 3, 6, 2, 5, 9, 12, 0, 8, 7, 13, 4, 14, 11, 15};
const int bit4_pattern_TypeHij_TranHi[] = {0, 1, 3, 6, 2, 5, 9, 11, 0, 8, 7, 12, 4, 13, 10, 14};

extern void StagesEnCodingGaggles1(StructCodingPara *PtrCoding, 
								 BitPlaneBits *BlockInfo, 
								 UCHAR8 BlocksInGaggles, 
								 UCHAR8 Option[], 
								 BOOL FlagCodeOptionOutput[]);

extern void StagesEnCodingGaggles2(StructCodingPara *PtrCoding,
								 BitPlaneBits *BlockInfo, 
								 UCHAR8 BlocksInGaggles,
								 UCHAR8 Option[], 
								 BOOL FlagCodeOptionOutput[]);

extern void StagesEnCodingGaggles3(StructCodingPara *PtrCoding, 
								 BitPlaneBits *BlockInfo, 
								 UCHAR8 BlocksInGaggles,
								 UCHAR8 Option[], 
								 BOOL FlagCodeOptionOutput[]);



void PatternMapping(StrSymbolDetails *StrSymbol)
{	
	switch (StrSymbol->sym_len)
	{
	case 0: return;
	case 1: StrSymbol->sym_mapped_pattern = StrSymbol->sym_val;
		break;
	case 2:		
		StrSymbol->sym_mapped_pattern  = bit2_pattern[StrSymbol->sym_val];	
		break;
	case 3:
		if(StrSymbol->type == ENUM_TRAN_D)
			StrSymbol->sym_mapped_pattern  = bit3_pattern_TranD[StrSymbol->sym_val];	
		else 
			StrSymbol->sym_mapped_pattern  = bit3_pattern[StrSymbol->sym_val];	
		break;	
	case 4:	
		if (StrSymbol->type == ENUM_TYPE_CI)
			StrSymbol->sym_mapped_pattern  = bit4_pattern_TypeCi[StrSymbol->sym_val];
		else if ((StrSymbol->type == ENUM_TRAN_HI)||(StrSymbol->type == ENUM_TYPE_HIJ))
			StrSymbol->sym_mapped_pattern  = bit4_pattern_TypeHij_TranHi[StrSymbol->sym_val];	
		break;	
	default:
		ErrorMsg(BPE_PATTERNING_CODING_ERROR);//"Pattern mapping ErrorMsg occurs!\n");
	}
}

void DeMappingPattern(StrSymbolDetails *StrSymbol)
{

	UCHAR8 i;

	switch (StrSymbol->sym_len)
	{
	case 1: StrSymbol->sym_val = StrSymbol->sym_mapped_pattern;
		break;
	case 2:		
		for( i = 0; i < 4; i ++)
		{
			if(StrSymbol->sym_mapped_pattern == bit2_pattern[i])
			{
				StrSymbol->sym_val = i;
				break;
			}
		}
		break;
	case 3:
		if(StrSymbol->type == ENUM_TRAN_D)
		{
			for( i = 1; i < 8; i ++)
			{
				if(StrSymbol->sym_mapped_pattern == bit3_pattern_TranD[i])
				{
					StrSymbol->sym_val = i;
					break;
				}
			}
		}
		else
		{
			for( i = 0; i < 8; i ++)
			{
				if(StrSymbol->sym_mapped_pattern == bit3_pattern[i])
				{
					StrSymbol->sym_val = i;
					break;
				}
			}
		}
		break;	
	case 4:		 

		if (StrSymbol->type  == ENUM_TYPE_CI)
		{
			for( i = 0; i < 16 ; i ++)
			{
				if(StrSymbol->sym_mapped_pattern == bit4_pattern_TypeCi[i])
				{
					StrSymbol->sym_val = i;
					break;
				}
			}
		}
		else if ((StrSymbol->type  == ENUM_TRAN_HI)||(StrSymbol->type  == ENUM_TYPE_HIJ))
		{
			for( i = 1; i < 16 ; i ++)
			{
				if(StrSymbol->sym_mapped_pattern == bit4_pattern_TypeHij_TranHi[i])
				{
					StrSymbol->sym_val = i;
					break;
				}
			}
		}
		break;		
	default:
		ErrorMsg(BPE_PATTERNING_CODING_ERROR); 
	}	
}

void BitPlaneSymbolReset(StrSymbolDetails *SymbolStr) 
{
	SymbolStr->sign = 0;
	SymbolStr->sym_len = 0;
	SymbolStr->sym_mapped_pattern = 0;
	SymbolStr->sym_val = 0;						
	SymbolStr->type = 0;
} 


void CodingOptions(StructCodingPara *PtrCoding, 
				   BitPlaneBits *BlockInfo,
				   UINT32 BlocksInGaggle, 
				   UCHAR8 Option[])
{

	DWORD32 BlockSeq = 0;	
	UCHAR8 SymbolIndex = 0;
	DWORD32 bitsCounter_2Bits[2] = {0};
	DWORD32 bitsCounter_3Bits[3] = {0};
	DWORD32 bitsCounter_4Bits[4] = {0};

	for ( BlockSeq = 0; BlockSeq < BlocksInGaggle; BlockSeq ++)		
	{
		UCHAR8 SymbolIndex = 0;
		if (BlockInfo[BlockSeq].BitMaxAC < PtrCoding->BitPlane)
			continue;		

		for(SymbolIndex = 0; SymbolIndex < MAX_SYMBOLS_IN_BLOCK; SymbolIndex ++)
		{		
			// 4.2 Pattern statistics and mapping. 
			if(BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].type == ENUM_NONE )
				continue;
			else if(BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_len == 1)
			{
				PatternMapping(&(BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex]) );
				continue;
			}			
			PatternMapping(&(BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex]));
		// 4.3 Entropy coding by truncated sample split code. 
		// 4.4 Truncated sample split code. 						
			if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_len == 2)
			{
				// try nk = 0 
				switch(BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern)   // code option 0;
				{
				case 0:
					bitsCounter_2Bits[0] ++;
					break;
				case 1:				
					bitsCounter_2Bits[0] += 2;
					break;
				case 2:				
					bitsCounter_2Bits[0] += 3;
					break;
				case 3:				
					bitsCounter_2Bits[0] += 3;
					break;
				default:
					ErrorMsg(BPE_PATTERNING_CODING_ERROR);
				}
				bitsCounter_2Bits[1] += 2;  //uncoded
			}
			else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_len == 3)
			{
				// try option = 0 
				if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 2)				
					bitsCounter_3Bits[0] +=  BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern + 1;

				else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <=5)					
					bitsCounter_3Bits[0] += 5;

				else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <=7)
					bitsCounter_3Bits[0] += 6;
				else
					ErrorMsg(BPE_PATTERNING_CODING_ERROR);

				// option  = 1;	
				if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 1)				
					bitsCounter_3Bits[1] += 2;

				else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 3)					
					bitsCounter_3Bits[1] += 3;

				else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 7)
					bitsCounter_3Bits[1] += 4;
				else
					ErrorMsg(BPE_PATTERNING_CODING_ERROR);
				// option  = 3;	uncoded. 
				bitsCounter_3Bits[2] += 3;

			}
			else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_len == 4)
			{
				// try nk = 0 

				if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 3)				
					bitsCounter_4Bits[0] += BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern + 1;

				else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 7)					
					bitsCounter_4Bits[0] += 7;

				else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 15)
					bitsCounter_4Bits[0] += 8;
				else
					ErrorMsg(BPE_PATTERNING_CODING_ERROR);
				// nk = 1;	

				if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 1)				
					bitsCounter_4Bits[1] +=  2;

				else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 3)					
					bitsCounter_4Bits[1] += 3;

				else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 5)
					bitsCounter_4Bits[1] += 4;
				
				else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 11)
					bitsCounter_4Bits[1] += 6;

				else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 15)
					bitsCounter_4Bits[1] += 7;
				else
					ErrorMsg(BPE_PATTERNING_CODING_ERROR);
				// nk = 2;	
				
				if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 3)				
					bitsCounter_4Bits[2] +=  3;

				else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 7)					
					bitsCounter_4Bits[2] += 4;

				else if (BlockInfo[BlockSeq].SymbolsBlock[SymbolIndex].sym_mapped_pattern <= 15)
					bitsCounter_4Bits[2] += 5;			
				else
					ErrorMsg(BPE_PATTERNING_CODING_ERROR);

				//code option 3: uncoded. 
				bitsCounter_4Bits[3] += 4;
			}
		} 
	} 

	// now determine the coding ID. 
	// 4.5 Pattern selection.

	// 2-bit codeword
	if(	bitsCounter_2Bits[0] < bitsCounter_2Bits[1] )
		Option[0] = 0; // codes. 
	else
		Option[0] = 1; // no-coding

	// 3-bit codeword
	if((bitsCounter_3Bits[2] <= bitsCounter_3Bits[0]) &&
		(bitsCounter_3Bits[2] <= bitsCounter_3Bits[1]))
	{
		Option[1] = 3;
	}
	else if((bitsCounter_3Bits[0] <= bitsCounter_3Bits[1]) && 
		(bitsCounter_3Bits[0] <= bitsCounter_3Bits[2]))
	{
		Option[1] = 0;
	}
	else if((bitsCounter_3Bits[1]  <= bitsCounter_3Bits[0]) && 		
		(bitsCounter_3Bits[1]  <= bitsCounter_3Bits[2]))
	{
		Option[1] = 1;
	}
	// 4-bit codeword
	if((bitsCounter_4Bits[3]  <= bitsCounter_4Bits[1]) && 
		(bitsCounter_4Bits[3]  <= bitsCounter_4Bits[0])&& 
		(bitsCounter_4Bits[3]  <= bitsCounter_4Bits[2]))
	{
		Option[2] = 3;	
	}
	else if((bitsCounter_4Bits[0]  <= bitsCounter_4Bits[1]) && 
			(bitsCounter_4Bits[0]  <= bitsCounter_4Bits[2]) && 
			(bitsCounter_4Bits[0]  <= bitsCounter_4Bits[3]))
	{
		Option[2] = 0;
	}
	else if((bitsCounter_4Bits[1]  <= bitsCounter_4Bits[0]) && 
		(bitsCounter_4Bits[1]  <= bitsCounter_4Bits[2])&& 
		(bitsCounter_4Bits[1]  <= bitsCounter_4Bits[3]))
	{
		Option[2] = 1;
	}		
	else if((bitsCounter_4Bits[2]  <= bitsCounter_4Bits[1]) && 
		(bitsCounter_4Bits[2]  <= bitsCounter_4Bits[0])&& 
		(bitsCounter_4Bits[2]  <= bitsCounter_4Bits[3]))
	{
		Option[2] = 2;	
	}				
	return;
}

void RefBitsDe( StructCodingPara *PtrCoding,
			   BitPlaneBits * BlockInfo)

{
	UINT32 BlockSeq;
	UCHAR8 i;
	UCHAR8 j;
	UCHAR8 k;
	UCHAR8 p;
	DWORD32 TempWord; 
	long ** block;
	UCHAR8 BitPlane = PtrCoding->BitPlane;

	PtrCoding->DecodingStopLocations.BlockNoStopDecoding = 0;

	for( BlockSeq = 0; BlockSeq < PtrCoding->PtrHeader->Header.Part3.S_20Bits; BlockSeq ++)
	{
		if (BlockInfo[BlockSeq].BitMaxAC < BitPlane)
			continue;
		// encoding TypeP first.

		block = BlockInfo[BlockSeq].PtrBlockAddress;

		if (BlockInfo[BlockSeq].RefineBits.RefineParent.ParentSymbolLength > 0)
		{
			if((PtrCoding->SegmentFull == TRUE) || (PtrCoding->RateReached == TRUE))
				return;

			for ( i = 0; i < 3; i++)

⌨️ 快捷键说明

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