Implementation of arithmetic coding using C ++

Source: Internet
Author: User

Arithmetic Coding plays an important role in image data compression standards (such as JPEG and JBIG. In arithmetic encoding, messages are encoded using real numbers ranging from 0 to 1. Arithmetic Coding uses two basic parameters: the probability of a symbol and Its Encoding interval. The probability of the source symbol determines the compression encoding efficiency, and also determines the interval of the source symbol in the coding process. These intervals are between 0 and 1. The encoding interval determines the output after the compressed symbol.

In arithmetic coding, you need to enter symbols. The probability of each symbol also has a sequence of symbols to be encoded. The initial encoding interval can be calculated based on the probability. First, several variables are available at the end: high -- upper limit of the current encoding, low -- lower limit of the current encoding, and high -- intermediate variable, used to calculate the upper limit of the current interval of the next encoding symbol, low -- intermediate variable, used to calculate the lower limit of the current interval of the next encoding symbol, d -- the distance between the current intervals. The current interval of the 1st encoding symbols is its initial encoding interval, and the current interval of the I encoding symbol is [low, high) after the I-1 ), the current interval algorithm for the I + 1 encoding symbol is as follows: high = low + D * the upper limit corresponding to the I + 1 Initial encoding symbol, low = low + D * lower limit corresponding to the I + 1 encoding symbol, and then the probability of high = high, low = low, D = D * encoding symbol I.

The encoding procedure is as follows:

# Include <iostream. h>
# Define M 100
# Define N 4
Class suanshu
{
Int count, length;
Char number [N], N;
Long Double CHANCE [N], C;
Char code [m];
Long double high, low, high, low, D;
Public:
Suanshu ()
{High = 0; Low = 0 ;}
Void get_number ();
Void get_code ();
Void coding ();
~ Suanshu (){}
};

Void suanshu: get_number ()
{
Cout <"Please input the number and its chance." <Endl;
For (INT I = 0; I <n; I ++)
{
Cin> N> C;
Number [I] = N;
CHANCE [I] = C;
}
If (I = 20)
Cout <"the number is full." <Endl;
Count = I;
}

Void suanshu: get_code ()
{
Cout <"Please input the Code's length :";
Cin> length;
While (length> = m)
{
Cout <"the length is too larger, please input a smaller one .";
Cin> length;
}
For (INT I = 0; I <length; I ++)
{
Cin> code [I];
}
}

Void suanshu: Coding ()
{
Int I, j = 0;
For (I = 0; I <count; I ++)
If (code [0] = number [I]) break;
While (j <I)
Low + = CHANCE [J ++];
D = CHANCE [J];
High = low + D;
For (I = 1; I <length; I ++)
For (j = 0; j <count; j ++)
{
If (code [I] = number [J])
{
If (j = 0)
{
Low = low;
High = low + CHANCE [J] * D;
High = high;
D * = CHANCE [J];
}
Else
{
Float channel = 0.0;
For (int K = 0; k <= J-1; k ++)
Chance_l + = CHANCE [k];
Low = low + D * chance_l;
High = low + D * (chance_l + CHANCE [J]);
Low = low;
High = high;
D * = CHANCE [J];
}
}
Else continue;
}
Cout <"the result is:" <low <Endl;
}

Int main ()
{
Suanshu;
A. get_number ();
A. get_code ();
A. coding ();
Return 0;
}

This program runs in vc6.0 and XP Professional Edition.

 

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.