Supor's JPEG decoder [2]

Source: Internet
Author: User

The part of the Harman decoding and bitstream operations.

JPEG's Harman encoding is optimized. For every n-bit long encoding, if it is smaller than Max [N], it is an end encoding. Max [N] can be calculated based on the code table given in the DHT syntax.

The bit retrieval method is common. If Assembly optimization is used, you can consider using the implementation method of the image decoding program in a H-GAME that was previously tracked. I will not talk about it here.

Note that rstn may be interspersed in this area.

Invalid huff. h

**************************************** * ***** Low-key drop separation line (1) ***************************************

/*************************************** **************************************** *******************

Superarhow's JPEG Decoder

By superarhow (superarhow@hotmail.com). All rights reserved.

**************************************** **************************************** ******************/

# Pragma once

# Include "Export dec2.h"

/* Read the next Haffman encoding from the stream and extract its corresponding bytes */
Byte pai_dec_next_huff_code (p_pai_dec_rec p_rec, p_pai_huff_table p_table );

/* Read n_bits from the stream */
Word 1__get_next_bits (p_1__dec_rec p_rec, byte n_bits );

**************************************** * ***** Low-key drop separation line (2) ***************************************

Invalid huff. c

**************************************** * ***** Low-key drop separation line (3) ***************************************

/*************************************** **************************************** *******************

Superarhow's JPEG Decoder

By superarhow (superarhow@hotmail.com). All rights reserved.

**************************************** **************************************** ******************/

# Include "Export dec2.h"

/*
* Read the next Haffman encoding from the stream and decode the corresponding bytes.
* Return the decoded bytes.
*/
Byte pai_dec_next_huff_code (p_1__dec_rec p_rec, p_1__huff_table p_table)
{
Word * p_min_code, * p_max_code;
Byte * p_codedata, * p_startindexes;
Byte * p_data;
Byte left_bits, n_left_bit_count, mask;
Word Code;

P_min_code = p_table-> huff_min_codes;
P_max_code = p_table-> huff_max_codes;
P_codedata = p_table-> start_code_indexes;
P_startindexes = p_table-> start_code_indexes;

P_data = p_rec-> p_data;
Left_bits = p_rec-> left_bits;
N_left_bit_count = p_rec-> n_left_bit_count;
Mask = 1 <(n_left_bit_count-1 );
Code = 0;
For (;;){
If (n_left_bit_count = 0 ){
/* Fetch the next bits from the stream */
N_left_bit_count = 8;
Mask = 0x80;
Left_bits = * p_data ++;
While (left_bits = 0xff ){
If (* p_data = 0 ){
P_data ++;
Break;
} Else if (* p_data> = 0xd0 & * p_data <= 0xd7 ){
/* Rstn */
N_left_bit_count = 0;
Performance_reset (p_rec );
++ P_data;
Break;
} Else {
Left_bits = * p_data ++;
Break;
}
}
If (n_left_bit_count = 0) continue;/* still not get the next byte */
}
Code = Code <1;
If (left_bits & Mask) code ++;
N_left_bit_count --;
Left_bits & = (~ Mask );
If (Code <= * p_max_code & (* p_max_code> = 0x8000 | 0x8000> = * p_min_code )){
/* Done! */
Break;
}
P_max_code ++;
P_min_code ++;
P_codedata ++;
P_startindexes ++;
Mask> = 1;
}
P_rec-> n_left_bit_count = n_left_bit_count;
P_rec-> left_bits = left_bits;
P_rec-> p_data = p_data;
Code-= * p_min_code;
If (code + * p_startindexes> = p_table-> n_code_count) _ asm int 3;
Return p_table-> codes [code + * p_startindexes];
}

/*
* Read n_bits from the stream
* Return the read location code.
*/
Word 1__get_next_bits (p_1__dec_rec p_rec, byte n_bits)
{
Word result;
Byte * p_data;
Byte left_bits, n_left_bit_count;

Result = 0;
P_data = p_rec-> p_data;
Left_bits = p_rec-> left_bits;
N_left_bit_count = p_rec-> n_left_bit_count;

While (n_bits> 0 ){

If (n_left_bit_count = 0 ){
/* Fetch the next bits from the stream */
N_left_bit_count = 8;
Left_bits = * p_data ++;
While (left_bits = 0xff ){
If (* p_data = 0 ){
++ P_data;
Break;
} Else if (* p_data> = 0xd0 & * p_data <= 0xd7 ){
/* Rstn */
N_left_bit_count = 0;
Performance_reset (p_rec );
++ P_data;
Break;
} Else {
Left_bits = * p_data ++;
Break;
}
}
If (n_left_bit_count = 0) continue;/* still not get the next byte */
}

If (n_left_bit_count> = n_bits ){
/* Fetch n_bits bits */
N_left_bit_count-= n_bits;
Result <= n_bits;
Result | = (1 <n_bits)-1) & (left_bits> n_left_bit_count ));
Break;
} Else {
/* Fetch n_left_bit_count bits */
Result <= n_left_bit_count;
Result | = (1 <n_left_bit_count)-1) & left_bits );
N_bits-= n_left_bit_count;
N_left_bit_count = 0;
}
}
/* Set the changed pointer and bit values */
P_rec-> left_bits = left_bits;
P_rec-> n_left_bit_count = n_left_bit_count;
P_rec-> p_data = p_data;
Return result;
}

**************************************** * ***** Low-key drop separation line (4) ***************************************

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.