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

📄 jdhuffman.c

📁 void III_hufman_decode(struct Granule *gr,int part2_start, int freqline[SBLIMIT][SSLIMIT]) {
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "jdconfig.h"
#include "jderror.h"
#include "jddatatype.h"
#include "jdprototype.h"

UINT16 sequential_huffman (JPEG_DECODER_STRUCTURE *jpeg_decoder_structure,
							INT16 *block, UINT16 component_index)
{
	UINT32 lcode;
	UINT16 lsize;

	INT16 dc, ac;
	UINT16 coefficient, huffval, index, huffcode, huffsize;
	UINT16 run_length, size;

	UINT16 dc_entropy_coding_table_destination_selector;
	UINT16 ac_entropy_coding_table_destination_selector;
	INT32 *valptr_minus_mincode_table, *maxcode_table;
	UINT16 *huffval_table, *huffsize_table;
	HUFFMAN_STRUCTURE *huffman_structure;

	dc_entropy_coding_table_destination_selector = jpeg_decoder_structure->
		dc_entropy_coding_table_destination_selector [component_index];

	ac_entropy_coding_table_destination_selector = jpeg_decoder_structure ->
		ac_entropy_coding_table_destination_selector [component_index];

	huffman_structure = &jpeg_decoder_structure->
		huffman_structure [0][dc_entropy_coding_table_destination_selector];

	huffsize_table = huffman_structure->huffsize_table;
	valptr_minus_mincode_table = huffman_structure->valptr_minus_mincode_table;
	maxcode_table = huffman_structure->maxcode_table;
	huffval_table = huffman_structure->huffval_table;

	lcode = jpeg_decoder_structure->lcode;
	lsize = jpeg_decoder_structure->lsize;

	if (lsize < 16)
	{
		lcode |= (UINT32) read_16huff_bits (jpeg_decoder_structure) << (16 - lsize);
		lsize += 16;
	}

#if NBITS
	huffsize = huffsize_table [lcode >> 32 - NBITS];

	if (huffsize)
		huffcode = lcode >> (32 - huffsize);
	else
#endif
	{
		huffsize = NBITS + 1;
		huffcode = lcode >> 32 - (NBITS + 1);

		while ((INT32) huffcode > maxcode_table [huffsize])
		{
			huffsize ++;
			huffcode = lcode >> (32 - huffsize);
		}

		if (huffsize > 16)
			return ERROR_INVALID_HUFFMAN_CODE;
	}

	index = huffcode + valptr_minus_mincode_table [huffsize];
	huffval = huffval_table [index];

	lcode <<= huffsize;
	lsize -= huffsize;

	if (lsize < huffval)
	{
		lcode |= (UINT32) read_16huff_bits (jpeg_decoder_structure)
			<< (16 - lsize);
		lsize += 16;
	}

	if (huffval == 0)
		dc = 0;
	else
		dc = lcode >> (32 - huffval);

	lcode <<= huffval;
	lsize -= huffval;

	if (dc < (1 << (huffval - 1)))
		dc += (- 1 << huffval) + 1;

	jpeg_decoder_structure -> ldc [component_index] =
		block [0] = dc + jpeg_decoder_structure -> ldc [component_index];
	 
	huffman_structure = &jpeg_decoder_structure->
		huffman_structure [1][ac_entropy_coding_table_destination_selector];

	huffsize_table = huffman_structure->huffsize_table;
	maxcode_table = huffman_structure->maxcode_table;
	valptr_minus_mincode_table = huffman_structure->valptr_minus_mincode_table;
	huffval_table = huffman_structure->huffval_table;

	for (coefficient =  1; coefficient < 64; coefficient ++)
	{
		if (lsize < 16)
		{
			lcode |= (UINT32) read_16huff_bits (jpeg_decoder_structure) << (16 - lsize);
			lsize += 16;
		}

#if NBITS
		huffsize = huffsize_table [lcode >> 32 - NBITS];

		if (huffsize)
			huffcode = lcode >> (32 - huffsize);
		else
#endif
		{
 			huffsize = NBITS + 1;
			huffcode = lcode >> 32 - (NBITS + 1);

			while ((INT32) huffcode > maxcode_table [huffsize])
			{
				huffsize ++;
				huffcode = lcode >> (32 - huffsize);
			}

			if (huffsize > 16)
				return ERROR_INVALID_HUFFMAN_CODE;
		}

		index = huffcode + valptr_minus_mincode_table [huffsize];
		huffval = huffval_table [index];

		lcode <<= huffsize;
		lsize -= huffsize;

		run_length = huffval >> 4;
		size = huffval & 15;
	
		if (size)
		{
			coefficient += run_length;

			if (lsize < size)
			{
				lcode |= (UINT32) read_16huff_bits (jpeg_decoder_structure)
					<< (16 - lsize);
				lsize += 16;
			}

			ac = lcode >> (32 - size);

			lcode <<= size;
			lsize -= size;

			if (ac < (1 << (size - 1)))
				ac += (- 1 << size) + 1;

			block [coefficient] = ac;
		}
		else
		{
			if (run_length != 15)
				break;

			coefficient += 15;
		}
	}

	jpeg_decoder_structure->lcode = lcode;
	jpeg_decoder_structure->lsize = lsize;

	return SUCCESS;
}

UINT16 first_dc_huffman (JPEG_DECODER_STRUCTURE *jpeg_decoder_structure, INT16 *block,
	UINT16 component_index)
{
	UINT16 index, lsize, huffcode, huffsize, huffval;
	INT16 dc, ldc;
	UINT32 lcode;

	UINT16 dc_entropy_coding_table_destination_selector;
	UINT16 successive_approximation_bit_position_low;
	UINT16 *huffsize_table, *huffval_table;
	INT32 *valptr_minus_mincode_table, *maxcode_table;
	HUFFMAN_STRUCTURE *huffman_structure;

	lcode = jpeg_decoder_structure -> lcode;
	lsize = jpeg_decoder_structure -> lsize;

	if (lsize < 16)
	{
		lcode |= (UINT32) read_16huff_bits (jpeg_decoder_structure) <<
			(16 - lsize);
		lsize += 16;
	}

	dc_entropy_coding_table_destination_selector = jpeg_decoder_structure ->
		dc_entropy_coding_table_destination_selector [component_index];

	huffman_structure = & jpeg_decoder_structure ->
		huffman_structure [0][dc_entropy_coding_table_destination_selector];

	huffsize_table = huffman_structure -> huffsize_table;
	valptr_minus_mincode_table =
		huffman_structure -> valptr_minus_mincode_table;
	huffval_table = huffman_structure -> huffval_table;
	maxcode_table = huffman_structure -> maxcode_table;
	
#if NBITS
	huffsize = huffsize_table [lcode >> 32 - NBITS];

	if (huffsize)
		huffcode = lcode >> (32 - huffsize);
	else
#endif
	{
		huffsize = NBITS + 1;
		huffcode = lcode >> 32 - (NBITS + 1);

		while ((INT32) huffcode > maxcode_table [huffsize])
		{
			huffsize ++;
			huffcode = lcode >> (32 - huffsize);
		}

		if (huffsize > 16)
			return ERROR_INVALID_HUFFMAN_CODE;
	}

	index = huffcode + valptr_minus_mincode_table [huffsize];
	huffval = huffval_table [index];

	lcode <<= huffsize;
	lsize -= huffsize;

	if (lsize < huffval)
	{
		lcode |= (UINT32) read_16huff_bits (jpeg_decoder_structure) <<
			(16 - lsize);
		lsize += 16;
	}

	if (huffval == 0)
		dc = 0;
	else
		dc = lcode >> (32 - huffval);
	//dc = lcode >> (32 - huffval);

	lcode <<= huffval;
	lsize -= huffval;

	if (dc < (1 << (huffval - 1)))
		dc += (- 1 << huffval) + 1;

	ldc = jpeg_decoder_structure -> ldc [component_index];
	dc += ldc;

	jpeg_decoder_structure -> ldc [component_index] = dc;

	successive_approximation_bit_position_low = jpeg_decoder_structure ->
		successive_approximation_bit_position_low;
	
	dc <<= successive_approximation_bit_position_low;

	block [0] = dc;

	jpeg_decoder_structure -> lcode = lcode;
	jpeg_decoder_structure -> lsize = lsize;

	return SUCCESS;
}

UINT16 refine_dc_huffman (JPEG_DECODER_STRUCTURE *jpeg_decoder_structure, INT16 *block,
	UINT16 component_index)
{
	UINT16 lsize, successive_approximation_bit_position_low;
	UINT32 lcode;

	lcode = jpeg_decoder_structure -> lcode;
	lsize = jpeg_decoder_structure -> lsize;

	if (lsize == 0)
	{
		lcode = (UINT32) read_16huff_bits (jpeg_decoder_structure) << 16;
		lsize = 16;
	}

	successive_approximation_bit_position_low =
		jpeg_decoder_structure -> successive_approximation_bit_position_low;

	block [0] |= (lcode >> 31) << successive_approximation_bit_position_low;

	lcode <<= 1;
	lsize -= 1;

	jpeg_decoder_structure -> lcode = lcode;
	jpeg_decoder_structure -> lsize = lsize;

	return SUCCESS;
}

UINT16 first_ac_huffman (JPEG_DECODER_STRUCTURE *jpeg_decoder_structure, INT16 *block,
	UINT16 component_index)
{
	UINT16 eobrun, ac_entropy_coding_table_destination_selector, lsize;
	UINT16 start_of_spectral_selection, end_of_spectral_selection;
	UINT16 successive_approximation_bit_position_low, huffsize, huffval, huffcode;
	UINT16 *huffsize_table, *huffval_table, coefficient, index, run_length, size;
	INT16 ac;
	UINT32 lcode;
	INT32 * valptr_minus_mincode_table, * maxcode_table;
	HUFFMAN_STRUCTURE *huffman_structure;

	eobrun = jpeg_decoder_structure -> eobrun;

	if (eobrun > 0)
		eobrun --;
	else
	{
		ac_entropy_coding_table_destination_selector =
			jpeg_decoder_structure -> ac_entropy_coding_table_destination_selector [0];

		huffman_structure = & jpeg_decoder_structure ->
			huffman_structure [1][ac_entropy_coding_table_destination_selector];

		huffsize_table = huffman_structure -> huffsize_table;
		valptr_minus_mincode_table =
			huffman_structure -> valptr_minus_mincode_table;
		huffval_table = huffman_structure -> huffval_table;
		maxcode_table = huffman_structure -> maxcode_table;

⌨️ 快捷键说明

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