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

📄 dc_endecoding.c

📁 CCSDS空间图像图像压缩标准c源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	UCHAR8 ID_Length = 0;

	if ( PtrCoding->N == 2) 
	{
		Max_k = 0;
		ID_Length = 1;
	}
	else if ( PtrCoding->N <=4)
	{
		Max_k = 2;
		ID_Length = 2;	
	}
	else if ( PtrCoding->N <=8)
	{
		Max_k = 6;
		ID_Length = 3;	
	}
	else if ( PtrCoding->N <=10)
	{
		Max_k = 8;
		ID_Length = 4;	
	}
	else 
	{
		ErrorMsg(BPE_DATA_ERROR);	
	}

	 gaggles = GAGGLE_SIZE -1;
	if (PtrCoding->PtrHeader->Header.Part3.S_20Bits < gaggles)
		gaggles = PtrCoding->PtrHeader->Header.Part3.S_20Bits - 1;

	GaggleStartIndex = 1;
	DCGaggleDecoding(PtrCoding, BlockInfo, 	GaggleStartIndex, gaggles, Max_k, ID_Length);	

	GaggleStartIndex += gaggles;
	gaggles = GAGGLE_SIZE;
	
	while(PtrCoding->PtrHeader->Header.Part3.S_20Bits - GaggleStartIndex >= gaggles)
	{	
		DCGaggleDecoding(PtrCoding, BlockInfo, GaggleStartIndex,
			gaggles, Max_k, ID_Length);
		GaggleStartIndex += gaggles;	

	}
	
	gaggles = PtrCoding->PtrHeader->Header.Part3.S_20Bits - GaggleStartIndex;
	
	DCGaggleDecoding(PtrCoding, BlockInfo, GaggleStartIndex,
		gaggles, Max_k, ID_Length);
	return BPE_OK;
}


void DPCM_DCMapper(BitPlaneBits *BlockInfo, 
				   int size,
				   short N)
{
	long * diff_DC = NULL;
	long i = 0;
	long theta = 0;
	long X_Min = 0;
	DWORD32 Bits1 = 0;
	DWORD32 X_Max;
	DWORD32 Max_Mapped = 0;
	X_Min = - (1<<(N-1));
	X_Max = ((1<<(N-1)) - 1);

	diff_DC = (long *)calloc(size,sizeof(long)); 
	BlockInfo[0].MappedDC = BlockInfo[0].ShiftedDC; // the reference DC 

	for(i = 0; i < (N - 1); i ++)
		Bits1 = ((Bits1++)<<1);

	if((BlockInfo[0].ShiftedDC & (1 << (N -1))) > 0) // minus 0		
		BlockInfo[0].ShiftedDC = -(short)(((~(BlockInfo[0].ShiftedDC - 1)) & Bits1));
	else 
		BlockInfo[0].ShiftedDC = BlockInfo[0].ShiftedDC;  // positive. 

	diff_DC[0] = BlockInfo[0].ShiftedDC;

	for(i = 1; i < size; i ++)
	{
		if((BlockInfo[i].ShiftedDC & (1 << (N -1))) > 0) // minus 0		
			BlockInfo[i].ShiftedDC = -(short)((~(BlockInfo[i].ShiftedDC - 1)) & Bits1);
		else 
			BlockInfo[i].ShiftedDC = BlockInfo[i].ShiftedDC;  // positive. 
		diff_DC[i] = BlockInfo[i].ShiftedDC - BlockInfo[i - 1].ShiftedDC ;  //DCtwos[i] - DCtwos[i - 1];
	}

	for ( i = 1; i < size; i ++)
	{
		theta = min(BlockInfo[i - 1].ShiftedDC - X_Min, X_Max - BlockInfo[i - 1].ShiftedDC);
		if (diff_DC[i] >= 0 && diff_DC[i] <= theta)
			BlockInfo[i].MappedDC = 2 * diff_DC[i];
		else if(diff_DC[i] <0 && diff_DC[i] >= -theta)
			BlockInfo[i].MappedDC = - 2 * diff_DC[i] - 1;
		else
			BlockInfo[i].MappedDC = theta + abs(diff_DC[i]);

		if(BlockInfo[i].MappedDC > Max_Mapped)
			Max_Mapped = BlockInfo[i].MappedDC;
	}
	free(diff_DC);
	return;
}

void DCEncoding(StructCodingPara *PtrCoding, 
				 long **BlockString,  
				 BitPlaneBits *BlockInfo)

{
	WORD16 BitDepthDC = 0;
	WORD16 BitDepthAC = 0;
	WORD16 QuantizationFactorQ_prime;
	UINT32 i;
	UINT32 k;
	UINT32 p;

	SLONG ACBitMaxOneBlock = 1; 
	DWORD32 counter = 0; 
	SLONG Max_DC = 0;
	SLONG MaxACSegment = 0;
	DWORD32 BlockIndex;	

	SLONG DC_Min = 0x10000;
	SLONG DC_Max = -0x10000;

	DWORD32 Temp_BitMaxDC = 0;

	for(BlockIndex = PtrCoding->BlockCounter;
		BlockIndex < PtrCoding->BlockCounter + PtrCoding->PtrHeader->Header.Part3.S_20Bits; 
		BlockIndex++)
	{
		UINT32 IndexStart = BlockIndex - PtrCoding->BlockCounter; 

		BlockInfo[IndexStart].PtrBlockAddress = 
			(BlockString + BlockIndex * BLOCK_SIZE) ;

		*BlockInfo[IndexStart].PtrBlockAddress = 
			*(BlockString + BlockIndex * BLOCK_SIZE);
	
		/******			Begin the DC coding processing, Coding stage 0		*****/				

			// Begin to see if the DC value is bigger than the previous one. 
			// determine BitDepthAC and Bit2sDC;
			
		if ((AMPLITUDE(BlockInfo[IndexStart].PtrBlockAddress[0][0])) > AMPLITUDE(Max_DC) )
			Max_DC = BlockInfo[IndexStart].PtrBlockAddress[0][0];

		if ((DWORD32)(AMPLITUDE(BlockInfo[IndexStart].PtrBlockAddress[0][0])) > Temp_BitMaxDC )
			Temp_BitMaxDC =AMPLITUDE(BlockInfo[IndexStart].PtrBlockAddress[0][0]);


		if (BlockInfo[IndexStart].PtrBlockAddress[0][0] > DC_Max)
			DC_Max = BlockInfo[IndexStart].PtrBlockAddress[0][0];

		if (BlockInfo[IndexStart].PtrBlockAddress[0][0] < DC_Min)
			DC_Min = BlockInfo[IndexStart].PtrBlockAddress[0][0];


		BlockInfo[IndexStart].BitMaxAC = 0; 
		ACBitMaxOneBlock = 0;
		// search for the max bit length of AC of each block
		for (k = 0; k < BLOCK_SIZE; k ++)
			for (p = 0; p < BLOCK_SIZE; p++)
			{
				UINT32 AbsAC = 0;
				if ( k == 0 && p ==0)
					continue;
				
				AbsAC = AMPLITUDE(BlockInfo[IndexStart].PtrBlockAddress[k][p]);				
				if((SLONG) AbsAC > ACBitMaxOneBlock)
					ACBitMaxOneBlock = AbsAC;
				if((SLONG)AbsAC > MaxACSegment)
					MaxACSegment = AbsAC;
			}	
		BlockInfo[IndexStart].BitMaxAC = 0;
		while (ACBitMaxOneBlock > 0)
		{
			ACBitMaxOneBlock >>= 1;
			BlockInfo[IndexStart].BitMaxAC ++;
		}			
	}

	PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits = 0;
	PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits = 0;

	while (MaxACSegment > 0)
	{
		MaxACSegment >>= 1;
		PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits ++;
	}
	
	//Max_DC can be minus
	
	if (DC_Min >= 0)
		Max_DC = DC_Max;

	else if( DC_Max <= 0)
		Max_DC = DC_Min;

	else if(DC_Max >= AMPLITUDE(DC_Min))
		Max_DC = DC_Max;
	else 
		Max_DC = DC_Min;

	if(Max_DC >= 0)
	{
		while (Max_DC > 0)
		{
			Max_DC >>= 1;
			PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits ++;
		}
	}
	else
	{ 
		DWORD32 temp = -Max_DC;
		while (temp > 0)
		{
			temp >>= 1;
			PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits ++;
		}
		if( (1 << (PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits -1)) == -Max_DC)
			PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits --;
	}

	PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits++; // include the sign bit. 
	
	// consider one bit to indicate negative or positive. 
	// because BitDepthAC and BitDepthDC will be transmitted to the decoder, the decoder can get
	// exactly the same ns, which can be used for the shifting operation of DC values. 
	BitDepthDC = PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits;
	BitDepthAC = PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits;

	HeaderOutput(PtrCoding);

	if (PtrCoding->SegmentFull == TRUE)
	{
		ErrorMsg(BPE_RATE_ERROR);
	}

	if (BitDepthDC <= 3)
		QuantizationFactorQ_prime = 0;
	else if (((BitDepthDC - (1 + (BitDepthAC >> 1))) <= 1)&&(BitDepthDC > 3))
		QuantizationFactorQ_prime = BitDepthDC - 3;
	else if (((BitDepthDC - (1 + (BitDepthAC >> 1))) > 10)&&(BitDepthDC > 3))
		QuantizationFactorQ_prime = BitDepthDC - 10;
	else 
		QuantizationFactorQ_prime = 1 + (BitDepthAC>>1);

	if(PtrCoding->PtrHeader->Header.Part4.DWTType == INTEGER_WAVELET) 
		// have to consider the scaling factor
		PtrCoding->QuantizationFactorQ = max(QuantizationFactorQ_prime,
		PtrCoding->PtrHeader->Header.Part4.CustomWtLL3_2bits);
	else
		PtrCoding->QuantizationFactorQ = (UCHAR8) QuantizationFactorQ_prime;
	
	/////////////////////////  Shift the DC component //////////////////////
	
	k = 0;
	for ( i = PtrCoding->QuantizationFactorQ; i > 0; i --)
	{
		k <<= 1;
		k ++; 
	}   // now temp == 000111. ns number of "1"s.
	
	for(i = 0; i < PtrCoding->PtrHeader->Header.Part3.S_20Bits; i ++)// shift the DC component to the right
	{
		DWORD32 NewNum = ConvTwosComp(BlockInfo[i].PtrBlockAddress[0][0], BitDepthDC);
		BlockInfo[i].ShiftedDC = (NewNum >> PtrCoding->QuantizationFactorQ);
		BlockInfo[i].DCRemainder = (WORD16)(NewNum & k);
	}

	PtrCoding->N = max(PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits
		- PtrCoding->QuantizationFactorQ, 1);

	// the maximum value of the N is 10. 

	if (PtrCoding->N == 1)
	{	
		for(i = 0; i < PtrCoding->PtrHeader->Header.Part3.S_20Bits; i ++)// shift the DC component to the right
			BitsOutput(PtrCoding,BlockInfo[i].ShiftedDC, 1);
		return;
	}
	// 2.2 Sample-spit entropy for bit shifted DC's
	DPCM_DCMapper(BlockInfo, PtrCoding->PtrHeader->Header.Part3.S_20Bits, PtrCoding->N);
		//PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits - PtrCoding->N);
	DCEntropyEncoder(PtrCoding, BlockInfo);

   //ADDITIONAL BIT PLANES OF DC COEFFICIENTS 
	//The coding of quantized DC coefficients described 
	//in Section 4.3.1 effectively encodes the first 
	//BitDepthDC-q bits of each DC coefficient.
	//When q >BitDepthAC, the next q-BitDepthAC bits 
	//of each DC coefficient appear in the coded bitstream.
	//The appropriate bits of each DC coefficient are concatenated, 
	//for each bit plane. That is, we have the (q-1)th most-significant
	//bit of each DC coefficient, followed by the (q-2)th most-significant bit,
	//and so on, until the BitDepthACth bit of each DC coefficient 
	//(Recall that bits are indexed so that the least significant bit is 
	//referred to as the 0th most significant bit.)

}


void DPCM_DCDeMapper(BitPlaneBits *BlockInfo, 
					 int size,
					 short N)
{

	long theta = 0;  
	long i;
	DWORD32 X_Max = (1 << (N-1)) - 1;
	long X_Min = - (1<< (N-1));	
	
	long * diff_DC; 
	
	DWORD32 Bits1 = 0;

	diff_DC = (long *)calloc(size,sizeof(long)); 
	
	BlockInfo[0].ShiftedDC = BlockInfo[0].MappedDC;
	diff_DC[0] = BlockInfo[0].ShiftedDC;

	for(i = 0; i < (N - 1); i ++)
		Bits1 = ((Bits1++)<<1);

	if((BlockInfo[0].ShiftedDC & (1 << (N -1))) > 0) // minus 0		
		BlockInfo[0].ShiftedDC = -(short)(((~(BlockInfo[0].ShiftedDC - 1)) & Bits1));
	else 
		BlockInfo[0].ShiftedDC = BlockInfo[0].ShiftedDC;  // positive. 

	for ( i = 1; i < size; i ++)
	{
		theta = min(BlockInfo[i-1].ShiftedDC - X_Min, X_Max - BlockInfo[i-1].ShiftedDC);
		
		if((float)BlockInfo[i].MappedDC / 2 == BlockInfo[i].MappedDC / 2)
		{
			diff_DC[i] = BlockInfo[i].MappedDC / 2;
			if(diff_DC[i] >= 0 && diff_DC[i] <= theta)
			{
				BlockInfo[i].ShiftedDC = diff_DC[i] + BlockInfo[i - 1].ShiftedDC;
				continue;
			}
		}
		else 
		{
			diff_DC[i] = -(long)(BlockInfo[i].MappedDC + 1) / 2 ;
			if(diff_DC[i] <= 0 && diff_DC[i] >= -theta)
			{
				BlockInfo[i].ShiftedDC = diff_DC[i] + BlockInfo[i - 1].ShiftedDC;
				continue;
			}
		}		
		diff_DC[i] = BlockInfo[i].MappedDC - theta;
		BlockInfo[i].ShiftedDC = diff_DC[i] + BlockInfo[i - 1].ShiftedDC;

		if(	(long)BlockInfo[i].ShiftedDC < X_Min || BlockInfo[i].ShiftedDC > X_Max)
		{
			diff_DC[i] = -diff_DC[i];
			BlockInfo[i].ShiftedDC = diff_DC[i] + BlockInfo[i - 1].ShiftedDC;
		}
	}
	free(diff_DC);		
	return;	
}

short DCDeCoding(StructCodingPara *PtrCoding,  StructFreBlockString *StrBlocks,  BitPlaneBits *BlockInfo)
{
	UINT32 i; 
	int j;
	int k;
	short BitDepthDC;
	short BitDepthAC;
	WORD16 QuantizationFactorQ_prime;

	BitDepthDC = PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits;
	BitDepthAC = PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits;

	for(i = 0; i < PtrCoding->PtrHeader->Header.Part3.S_20Bits; i++)
	{
		BlockInfo[i].PtrBlockAddress = (StrBlocks->FreqBlkString  + i * BLOCK_SIZE) ;
		*BlockInfo[i].PtrBlockAddress = *(StrBlocks->FreqBlkString  + i * BLOCK_SIZE);		
		BlockInfo[i].PtrBlockAddressFloating= (StrBlocks->FloatingFreqBlk  + i * BLOCK_SIZE) ;
		*BlockInfo[i].PtrBlockAddressFloating = *(StrBlocks->FloatingFreqBlk  + i * BLOCK_SIZE);
	}

	if (BitDepthDC <= 3)
		QuantizationFactorQ_prime = 0;
	else if (((BitDepthDC - (1 + (BitDepthAC >> 1))) <= 1)&&(BitDepthDC > 3))
		QuantizationFactorQ_prime = BitDepthDC - 3;
	else if (((BitDepthDC - (1 + (BitDepthAC >> 1))) > 10)&&(BitDepthDC > 3))
		QuantizationFactorQ_prime = BitDepthDC - 10;
	else 
		QuantizationFactorQ_prime = (1 + (BitDepthAC>>1));

	if(PtrCoding->PtrHeader->Header.Part4.DWTType == INTEGER_WAVELET) 
		// have to consider the scaling factor
		PtrCoding->QuantizationFactorQ = max(QuantizationFactorQ_prime,
		PtrCoding->PtrHeader->Header.Part4.CustomWtLL3_2bits);
	else
		PtrCoding->QuantizationFactorQ = (UCHAR8) QuantizationFactorQ_prime;
	// 2.2 Sample-spit entropy for bit shifted DC's
	PtrCoding->N = max(PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits 	- PtrCoding->QuantizationFactorQ, 1);

	if (PtrCoding->N ==1)
	{	
		for(i = 0; i < PtrCoding->PtrHeader->Header.Part3.S_20Bits; i ++) // shift the DC component to the right
			BitsRead(PtrCoding, &BlockInfo[i].ShiftedDC, 1);
	}
	else
	{
		if ((k = DCEntropyDecoder(PtrCoding, BlockInfo)) != BPE_OK)
			return k;
		DPCM_DCDeMapper(BlockInfo, PtrCoding->PtrHeader->Header.Part3.S_20Bits, PtrCoding->N);					
	}
/*	if (PtrCoding->QuantizationFactorQ >	PtrCoding->PtrHeader->Header.Part1.BitDepthAC_5Bits )
	{
		 PtrCoding->N = (UCHAR8)PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits;
		for(i = 0; i < (UINT32)PtrCoding->PtrHeader->Header.Part3.S_20Bits; i ++) // shift back the DC component to the right
			*(*BlockInfo[i].PtrBlockAddress) = DeConvTwosComp(BlockInfo[i].ShiftedDC, PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits);
	}
	else */ 
	{
		 PtrCoding->N = PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits - PtrCoding->QuantizationFactorQ; 	

		 for(i = 0; i <(UINT32) PtrCoding->PtrHeader->Header.Part3.S_20Bits; i ++)// shift the DC component to the right
		{
			BlockInfo[i].ShiftedDC <<= PtrCoding->QuantizationFactorQ;
			*(*BlockInfo[i].PtrBlockAddress) = DeConvTwosComp(BlockInfo[i].ShiftedDC, PtrCoding->PtrHeader->Header.Part1.BitDepthDC_5Bits);			
		}
	}		
	j =  PtrCoding->Bits->SegBitCounter;
	return BPE_OK;
}

⌨️ 快捷键说明

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