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

📄 jdmarker.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 "jdmarker.h"
#include "jddatatype.h"
#include "jdprototype.h"
#include "malloc.h"

UINT16	g_quantization_table [4][64];
INT32	g_valptr_minus_mincode_table [10][17];
INT32	g_maxcode_table [10][18];
UINT16	g_huffsize_table [10][256];
UINT16	g_huffval_table [10][500];

UINT16 read_soi_marker (JPEG_DECODER_STRUCTURE * jpeg_decoder_structure)
{
	if (read_16_bits (jpeg_decoder_structure) != (0xFF00 | SOI))
		return ERROR_SOI_NOT_DETECTED;

	return SUCCESS;
}

UINT16 read_sof_marker (JPEG_DECODER_STRUCTURE *jpeg_decoder_structure)
{
	UINT16 i, tmp, sof;
	UINT16 frame_header_length, sample_precision;
	UINT16 number_of_lines, number_of_samples_per_line;
	UINT16 number_of_image_components_in_frame;
	UINT16 horizontal_sampling_factor, vertical_sampling_factor;
	UINT16 quantization_table_destination_selector;
	UINT16 horizontal_mcus, vertical_mcus;

	if (jpeg_decoder_structure->sof_detected)
		return ERROR_SOF_ALREADY_DETECTED;

	jpeg_decoder_structure -> sof_detected = 1;

	jpeg_decoder_structure->sof = sof = jpeg_decoder_structure->unread_marker;

	frame_header_length = read_16_bits (jpeg_decoder_structure);

	sample_precision = read_8_bits (jpeg_decoder_structure);

	if (sample_precision != 8)
	{
		if ((sof == SOF0) || (sample_precision != 12))
			return ERROR_INVALID_SAMPLE_PRECISION;

		return ERROR_TWELVE_BIT_SAMPLE_PRECISION_NOT_SUPPORTED;
	}

	jpeg_decoder_structure->number_of_lines = number_of_lines = 
		read_16_bits (jpeg_decoder_structure);

	if (number_of_lines == 0)
		return ERROR_ZERO_NUMBER_OF_LINES_NOT_SUPPORTED;

	if (number_of_lines > MAXIMUM_NUMBER_OF_LINES)
		return ERROR_LARGE_NUMBER_OF_LINES_NOT_SUPPORTED;

	jpeg_decoder_structure->number_of_samples_per_line =
		number_of_samples_per_line = read_16_bits (jpeg_decoder_structure);

	if (number_of_samples_per_line == 0)
		return ERROR_ZERO_SAMPLES_PER_LINE;

	if (number_of_samples_per_line > MAXIMUM_NUMBER_OF_SAMPLES_PER_LINE)
		return ERROR_LARGE_NUMBER_OF_SAMPLES_PER_LINE_NOT_SUPPORTED;
	/*Added New*/
	jpeg_decoder_structure->const_image_buffer = jpeg_decoder_structure->external_image_buffer = (UINT32 *)malloc((unsigned int)number_of_lines*(number_of_samples_per_line>>1)*sizeof(UINT32));
	
	
	jpeg_decoder_structure->number_of_image_components_in_frame =
		number_of_image_components_in_frame = read_8_bits (jpeg_decoder_structure);

	if (number_of_image_components_in_frame != 1 &&	number_of_image_components_in_frame != 3)
		return ERROR_INVALID_NUMBER_OF_IMAGE_COMPONENTS_IN_FRAME;

	if (frame_header_length != (number_of_image_components_in_frame * 3 + 8))
		return ERROR_INVALID_FRAME_HEADER_LENGTH;

	for (i=0; i<number_of_image_components_in_frame; i++)
	{
		jpeg_decoder_structure->component_identifier [i] = read_8_bits (jpeg_decoder_structure);

		tmp = read_8_bits (jpeg_decoder_structure);

		jpeg_decoder_structure->horizontal_sampling_factor [i] = 
			horizontal_sampling_factor = tmp >> 4;

		if (horizontal_sampling_factor == 0 || horizontal_sampling_factor > 4)
			return ERROR_INVALID_HORIZONTAL_SAMPLING_FACTOR;

		jpeg_decoder_structure->vertical_sampling_factor [i] = 
			vertical_sampling_factor = tmp & 0xf;

		if (vertical_sampling_factor == 0 || vertical_sampling_factor > 4)
			return ERROR_INVALID_VERTICAL_SAMPLING_FACTOR;

		jpeg_decoder_structure->quantization_table_destination_selector [i] =
		quantization_table_destination_selector = read_8_bits (jpeg_decoder_structure);

		if (quantization_table_destination_selector > 3)
			return ERROR_INVALID_QUANTIZATION_TABLE_DESTINATION_SELECTOR;
	}

	horizontal_mcus = (number_of_samples_per_line + 7) >> 3;
	vertical_mcus = (number_of_lines + 7) >> 3;

	jpeg_decoder_structure -> number_of_rows [0] = 8;
	jpeg_decoder_structure -> number_of_columns [0] = 8;
	jpeg_decoder_structure -> number_of_rows [1] = number_of_lines - (vertical_mcus - 1) * 8;
	jpeg_decoder_structure -> number_of_columns [1] = number_of_samples_per_line - (horizontal_mcus - 1) * 8;

	jpeg_decoder_structure ->increment1 = 4;
	jpeg_decoder_structure -> increment2 = ((4 * (number_of_samples_per_line & 0xfffe)) -(horizontal_mcus) * 4);

	if (number_of_image_components_in_frame == 1)
	{
		if (jpeg_decoder_structure->horizontal_sampling_factor [0] == 1 &&
			jpeg_decoder_structure->vertical_sampling_factor [0] == 1)
		{
			jpeg_decoder_structure->image_format = FOUR_ZERO_ZERO;
			jpeg_decoder_structure->mcu_size = 64;
			jpeg_decoder_structure->write_format = write_400_format;
		}
		else
			return ERROR_UNSUPPORTED_IMAGE_FORMAT;
	}
	else
	{
		if ((jpeg_decoder_structure -> horizontal_sampling_factor [1] == 1) &&
			(jpeg_decoder_structure -> vertical_sampling_factor [1] == 1) &&
			(jpeg_decoder_structure -> horizontal_sampling_factor [2] == 1) &&
			(jpeg_decoder_structure -> vertical_sampling_factor [2] == 1))
		{
			if (jpeg_decoder_structure -> horizontal_sampling_factor [0] ==	1 &&
				jpeg_decoder_structure -> vertical_sampling_factor [0] == 1)
			{
				jpeg_decoder_structure -> image_format = FOUR_FOUR_FOUR;
				jpeg_decoder_structure->mcu_size = 192;
				jpeg_decoder_structure->write_format = write_444_format;
			}
			else if (jpeg_decoder_structure -> horizontal_sampling_factor [0] == 2)
			{
				horizontal_mcus =	(number_of_samples_per_line + 15) >> 4;
				jpeg_decoder_structure ->increment1 = 8;
				jpeg_decoder_structure -> number_of_columns [0] = 16;
				jpeg_decoder_structure -> number_of_columns [1] = number_of_samples_per_line - (horizontal_mcus - 1) * 16;

				if (jpeg_decoder_structure -> vertical_sampling_factor [0] == 1)
				{
					jpeg_decoder_structure -> image_format = FOUR_TWO_TWO;
					jpeg_decoder_structure->mcu_size = 256;
					jpeg_decoder_structure->write_format = write_422_format;

					jpeg_decoder_structure -> increment2 = ((4 * (number_of_samples_per_line & 0xfffe)) -
						(horizontal_mcus) * 8);
				}
				else if (jpeg_decoder_structure -> vertical_sampling_factor [0] == 2)
				{
					jpeg_decoder_structure -> image_format = FOUR_TWO_ZERO;
					jpeg_decoder_structure->mcu_size = 384;
					jpeg_decoder_structure->write_format = write_420_format;

					vertical_mcus = (number_of_lines + 15) >> 4;

					jpeg_decoder_structure -> increment2 = ((8 * (number_of_samples_per_line & 0xfffe)) -
						(horizontal_mcus) * 8);

					jpeg_decoder_structure -> number_of_rows [0] = 16;
					jpeg_decoder_structure -> number_of_rows [1] = number_of_lines - (vertical_mcus - 1) * 16;
				}
			}
			else
				return ERROR_UNSUPPORTED_IMAGE_FORMAT; 
		}
		else
			return ERROR_UNSUPPORTED_IMAGE_FORMAT; 
	}

	jpeg_decoder_structure->horizontal_mcus = horizontal_mcus;
	jpeg_decoder_structure->vertical_mcus =	vertical_mcus;

	return SUCCESS;
}

UINT16 read_sos_marker (JPEG_DECODER_STRUCTURE * jpeg_decoder_structure)
{
	UINT16 i, j, tmp, sof;
	UINT16 scan_header_length;
	UINT16 number_of_image_components_in_scan;
	UINT16 scan_component_selector;
	UINT16 dc_entropy_coding_table_destination_selector;
	UINT16 ac_entropy_coding_table_destination_selector;
	UINT16 start_of_spectral_selection;
	UINT16 end_of_spectral_selection;
	UINT16 successive_approximation_bit_position_high;
	UINT16 successive_approximation_bit_position_low;
	UINT16 number_of_image_components_in_frame, component_identifier;
	UINT16 *quantization_table, quantization_table_destination_selector;

	if (! jpeg_decoder_structure->sof_detected)
		return ERROR_SOF_NOT_DETECTED;

	sof = jpeg_decoder_structure->sof;
	number_of_image_components_in_frame = jpeg_decoder_structure->number_of_image_components_in_frame;

	scan_header_length = read_16_bits (jpeg_decoder_structure);

	jpeg_decoder_structure->number_of_image_components_in_scan =
	number_of_image_components_in_scan = read_8_bits (jpeg_decoder_structure);

	if (number_of_image_components_in_scan != 1 &&
		number_of_image_components_in_scan != number_of_image_components_in_frame)
		return ERROR_INVALID_NUMBER_OF_COMPONENTS_IN_SCAN;

	if (scan_header_length != (UINT16)(number_of_image_components_in_scan * 2 + 6))
		return ERROR_INVALID_SCAN_HEADER_LENGTH;

	for (i=0; i<number_of_image_components_in_scan; i++)
	{
		scan_component_selector = read_8_bits (jpeg_decoder_structure);

		for (j=0; j<number_of_image_components_in_frame; j++)
		{
			component_identifier = jpeg_decoder_structure->component_identifier [j];

			if (scan_component_selector == component_identifier)
				break;

			if (j == number_of_image_components_in_frame - 1)
				return ERROR_INVALID_SCAN_COMPONENT_SELECTOR;
		}

		jpeg_decoder_structure->component_index_in_frame [i] = j;

		tmp = read_8_bits (jpeg_decoder_structure);

		jpeg_decoder_structure->dc_entropy_coding_table_destination_selector [i] =
		dc_entropy_coding_table_destination_selector = tmp >> 4;

		/* check for null huffman dc pointers */
		if (((sof == SOF0) &&
			(dc_entropy_coding_table_destination_selector > 1)) ||
			(dc_entropy_coding_table_destination_selector > 3))
			return ERROR_INVALID_DC_ENTROPY_CODING_DESTINATION_SELECTOR;

		jpeg_decoder_structure->ac_entropy_coding_table_destination_selector [i] =
		ac_entropy_coding_table_destination_selector = tmp & 0xf;

		/* check for null huffman ac pointers */
		if (((sof == SOF0) &&
			(ac_entropy_coding_table_destination_selector > 1)) ||
			(ac_entropy_coding_table_destination_selector > 3))
			return ERROR_INVALID_AC_ENTROPY_CODING_DESTINATION_SELECTOR;

		quantization_table_destination_selector = jpeg_decoder_structure ->
			quantization_table_destination_selector [j];

		quantization_table = jpeg_decoder_structure ->
			quantization_table [quantization_table_destination_selector];

		if (quantization_table == 0)
			return ERROR_QUANTIZATION_TABLE;
	}

	jpeg_decoder_structure->start_of_spectral_selection = 
	start_of_spectral_selection = read_8_bits (jpeg_decoder_structure);

	if (sof != SOF2 && start_of_spectral_selection != 0 ||
		start_of_spectral_selection > 63)
		return ERROR_INVALID_START_OF_SPECTRAL_SELECTION;

	jpeg_decoder_structure->end_of_spectral_selection =	
	end_of_spectral_selection = read_8_bits (jpeg_decoder_structure);

	if (sof != SOF2 && end_of_spectral_selection != 63 ||
		sof == SOF2 && start_of_spectral_selection == 0 &&
		end_of_spectral_selection != 0 ||
		start_of_spectral_selection > end_of_spectral_selection ||
		end_of_spectral_selection > 63)
		return ERROR_INVALID_END_OF_SPECTRAL_SELECTION;

	tmp = read_8_bits (jpeg_decoder_structure);

	jpeg_decoder_structure->successive_approximation_bit_position_high =
	successive_approximation_bit_position_high = tmp >> 4;

	if (sof != SOF2 && successive_approximation_bit_position_high != 0 ||
		successive_approximation_bit_position_high > 13)
		return ERROR_INVALID_SUCCESSIVE_APPROXIMATION_BIT_POSITION_HIGH;

	jpeg_decoder_structure->successive_approximation_bit_position_low =
	successive_approximation_bit_position_low =	tmp & 0xf;

	if ((sof != SOF2 && successive_approximation_bit_position_low != 0) ||
		successive_approximation_bit_position_low > 13)
		return ERROR_INVALID_SUCCESSIVE_APPROXIMATION_BIT_POSITION_LOW;

	/* initializations */

	jpeg_decoder_structure->ldc[0] = 0;
	jpeg_decoder_structure->ldc[1] = 0;
	jpeg_decoder_structure->ldc[2] = 0;
	jpeg_decoder_structure->next_restart_num = 0;

	if (start_of_spectral_selection ||
		number_of_image_components_in_scan != number_of_image_components_in_frame)
	{
		tmp = decode_noninterleaved_scan (jpeg_decoder_structure);

		if (tmp != SUCCESS)
			return tmp;

		decode_interleaved_scan (jpeg_decoder_structure, 0);
	}
	else
	{
		tmp = decode_interleaved_scan (jpeg_decoder_structure, 1);

		if (tmp != SUCCESS)
			return tmp;
	}

	wind_buffer (jpeg_decoder_structure);

	return SUCCESS;
}

UINT16 read_dqt_marker (JPEG_DECODER_STRUCTURE *jpeg_decoder_structure)
{
	UINT16 i, tmp;
	UINT16 quantization_table_definition_length;
	UINT16 quantization_table_element_precision;
	UINT16 quantization_table_destination_identifier;
	UINT16 *quantization_table;

	quantization_table_definition_length = read_16_bits	(jpeg_decoder_structure) - 2;

	while (quantization_table_definition_length >= 1 + 64)
	{
		tmp = read_8_bits (jpeg_decoder_structure);

		quantization_table_element_precision = tmp >> 4;

		if (quantization_table_element_precision == 1)
			return ERROR_SIXTEEN_BIT_QUANTIZATION_TABLE_ELEMENT_PRECISION_NOT_SUPPORTED;

		if (quantization_table_element_precision > 1)
			return ERROR_INVALID_QUANTIZATION_TABLE_ELEMENT_PRECISION;

		quantization_table_destination_identifier = tmp & 0xf;

		if (quantization_table_destination_identifier > 3)
			return ERROR_INVALID_QUANTIZATION_TABLE_DESTINATION_IDENTIFIER;

		quantization_table = g_quantization_table [quantization_table_destination_identifier]; 
		jpeg_decoder_structure->quantization_table
			[quantization_table_destination_identifier] = quantization_table;

		for (i=0; i<64;	i++)
		{
			if ((quantization_table [i] = read_8_bits (jpeg_decoder_structure)) == 0)
				return ERROR_QUANTIZATION_TABLE_ELEMENT_ZERO;
		}

		quantization_table_definition_length -= 1 + 64;
	}

	if (quantization_table_definition_length != 0)
		return ERROR_INVALID_QUANTIZATION_TABLE_DEFINITION_LENGTH;

	return SUCCESS;
}

extern INT16 count;

UINT16 read_dht_marker (JPEG_DECODER_STRUCTURE *jpeg_decoder_structure)
{
	UINT16 huffman_table_definition_length;
	UINT16 table_class, huffman_table_destination_identifier;
	HUFFMAN_STRUCTURE *huffman_structure;
	UINT16 *huffsize_table, *huffval_table;

⌨️ 快捷键说明

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