3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by rramyaasmi
0

you mean it won't compile? What compiler and operating system?

iam using dev-c++ IDE.iam gettin more linker error.i don't know to clear the errors.

Edited by rramyaasmi: n/a

0

You really need to be more specific. Making such generic statements makes it very difficult for us to help you.

A linker error is returned when the implementation for a function you're calling can't be found. This typicallly happens because you either didn't include the proper object file in your linking process or you didn't properly implement a function that you called. Did you include the proper implementation file in your project?

0
source_en .zip contains all files.Basically i don't know how to run it.if i compile each and every file iam gettin error in #include "   .h" so i combined .h files whereever needed. tats y iam gettin error.iam using dev c++ ide.
0

Please STOP putting your comments in code tags -- code tags are just for c++ code, not the comments you want to make.

winzip can not open the zip file you posted.

Edited by Ancient Dragon: n/a

0

sorry iam new to tis forum.here i attached files.help me to run tis file.
source code is for "arithmetic coding for data compression".

Attachments
#include "ArithmeticCoderC.h"
#include "tools.h"

// constants to split the number space of 32 bit integers
// most significant bit kept free to prevent overflows
const unsigned int g_FirstQuarter = 0x20000000;
const unsigned int g_ThirdQuarter = 0x60000000;
const unsigned int g_Half         = 0x40000000;

ArithmeticCoderC::ArithmeticCoderC()
{
	mBitCount = 0;
	mBitBuffer = 0;

	mLow = 0;
	mHigh = 0x7FFFFFFF; // just work with least significant 31 bits
	mScale = 0;

	mBuffer = 0;
	mStep = 0;
}

void ArithmeticCoderC::SetFile( fstream *file )
{
	mFile = file;
}

void ArithmeticCoderC::SetBit( const unsigned char bit )
{
	// add bit to the buffer
	mBitBuffer = (mBitBuffer << 1) | bit;
	mBitCount++;

	if(mBitCount == 8) // buffer full
	{
		// write
		mFile->write(reinterpret_cast<char*>(&mBitBuffer),sizeof(mBitBuffer));
		mBitCount = 0;
	}
}

void ArithmeticCoderC::SetBitFlush()
{
	// fill buffer with 0 up to the next byte
	while( mBitCount != 0 )
		SetBit( 0 );
}

unsigned char ArithmeticCoderC::GetBit()
{
	if(mBitCount == 0) // buffer empty
	{
		if( !( mFile->eof() ) ) // file read completely?
			mFile->read(reinterpret_cast<char*>(&mBitBuffer),sizeof(mBitBuffer));
		else
			mBitBuffer = 0; // append zeros

		mBitCount = 8;
	}

	// extract bit from buffer
	unsigned char bit = mBitBuffer >> 7;
	mBitBuffer <<= 1;
	mBitCount--;

	return bit;
}

void ArithmeticCoderC::Encode( const unsigned int low_count,
															 const unsigned int high_count,
															 const unsigned int total )
// total < 2^29
{
	// partition number space into single steps
	mStep = ( mHigh - mLow + 1 ) / total; // interval open at the top => +1

	// update upper bound
	mHigh = mLow + mStep * high_count - 1; // interval open at the top => -1

	// update lower bound
	mLow = mLow + mStep * low_count;

	// apply e1/e2 mapping
	while( ( mHigh < g_Half ) || ( mLow >= g_Half ) )
		{
			if( mHigh < g_Half )
			{
				SetBit( 0 );
				mLow = mLow * 2;
				mHigh = mHigh * 2 + 1;

				// perform e3 mappings
				for(; mScale > 0; mScale-- )
					SetBit( 1 );
			}
			else if( mLow >= g_Half )
			{
				SetBit( 1 );
				mLow = 2 * ( mLow - g_Half );
				mHigh = 2 * ( mHigh - g_Half ) + 1;

				// perform e3 mappings
				for(; mScale > 0; mScale-- )
					SetBit( 0 );
			}
		}

	// e3
	while( ( g_FirstQuarter <= mLow ) && ( mHigh < g_ThirdQuarter ) )
	{
		// keep necessary e3 mappings in mind
		mScale++;
		mLow = 2 * ( mLow - g_FirstQuarter );
		mHigh = 2 * ( mHigh - g_FirstQuarter ) + 1;
	}
}

void ArithmeticCoderC::EncodeFinish()
{
	// There are two possibilities of how mLow and mHigh can be distributed,
	// which means that two bits are enough to distinguish them.

	if( mLow < g_FirstQuarter ) // mLow < FirstQuarter < Half <= mHigh
	{
		SetBit( 0 );

		for( int i=0; i<mScale+1; i++ ) // perform e3-skaling
			SetBit(1);
	}
	else // mLow < Half < ThirdQuarter <= mHigh
	{
		SetBit( 1 ); // zeros added automatically by the decoder; no need to send them
	}

	// empty the output buffer
	SetBitFlush();
}

void ArithmeticCoderC::DecodeStart()
{
	// Fill buffer with bits from the input stream
	for( int i=0; i<31; i++ ) // just use the 31 least significant bits
		mBuffer = ( mBuffer << 1 ) | GetBit();
}

unsigned int ArithmeticCoderC::DecodeTarget( const unsigned int total )
// total < 2^29
{
	// split number space into single steps
	mStep = ( mHigh - mLow + 1 ) / total; // interval open at the top => +1

	// return current value
	return ( mBuffer - mLow ) / mStep;
}

void ArithmeticCoderC::Decode( const unsigned int low_count,
															 const unsigned int high_count )
{
	// update upper bound
	mHigh = mLow + mStep * high_count - 1; // interval open at the top => -1

	// update lower bound
	mLow = mLow + mStep * low_count;

	// e1/e2 mapping
	while( ( mHigh < g_Half ) || ( mLow >= g_Half ) )
		{
			if( mHigh < g_Half )
			{
				mLow = mLow * 2;
				mHigh = mHigh * 2 + 1;
				mBuffer = 2 * mBuffer + GetBit();
			}
			else if( mLow >= g_Half )
			{
				mLow = 2 * ( mLow - g_Half );
				mHigh = 2 * ( mHigh - g_Half ) + 1;
				mBuffer = 2 * ( mBuffer - g_Half ) + GetBit();
			}
			mScale = 0;
		}

	// e3 mapping
	while( ( g_FirstQuarter <= mLow ) && ( mHigh < g_ThirdQuarter ) )
	{
		mScale++;
		mLow = 2 * ( mLow - g_FirstQuarter );
		mHigh = 2 * ( mHigh - g_FirstQuarter ) + 1;
		mBuffer = 2 * ( mBuffer - g_FirstQuarter ) + GetBit();
	}
}
#ifndef __ARITHMETICCODERC_H__
#define __ARITHMETICCODERC_H__

#include <fstream>
using namespace std;

class ArithmeticCoderC  
{
public:
	ArithmeticCoderC();

	void SetFile( fstream *file );

	void Encode( const unsigned int low_count, 
	             const unsigned int high_count, 
	             const unsigned int total );
	void EncodeFinish();

	void DecodeStart();
	unsigned int DecodeTarget( const unsigned int total );
	void Decode( const unsigned int low_count, 
							 const unsigned int high_count );

protected:
	// bit operations
	void SetBit( const unsigned char bit );
	void SetBitFlush();
	unsigned char GetBit();

	unsigned char mBitBuffer;
	unsigned char mBitCount;

	// in-/output stream
	fstream *mFile;

	// encoder & decoder
	unsigned int mLow;
	unsigned int mHigh;
	unsigned int mStep;
	unsigned int mScale;

	// decoder
	unsigned int mBuffer;
};

#endif
#include <iostream>
#include <fstream>
using namespace std;

#include "ModelOrder0C.h"

// signature: "ACMC" (0x434D4341, intel byte order)
const int g_Signature = 0x434D4341;

int __cdecl main(int argc, char *argv[])
{
	cout << "Arithmetic Coding" << endl;

	if( argc != 3 )
	{
		cout << "Syntax: AC source target" << endl;
		return 1;
	}

	fstream source, target;
	ModelI* model;

	// choose model, here just order-0
	model = new ModelOrder0C;

	source.open( argv[1], ios::in | ios::binary );
	target.open( argv[2], ios::out | ios::binary );

	if( !source.is_open() )
	{
		cout << "Cannot open input stream";
		return 2;
	}
	if( !target.is_open() )
	{
		cout << "Cannot open output stream";
		return 3;
	}

	unsigned int signature;
	source.read(reinterpret_cast<char*>(&signature),sizeof(signature));
	if( signature == g_Signature )
	{
		cout << "Decoding " << argv[1] << " to " << argv[2] << endl;
		model->Process( &source, &target, MODE_DECODE );
	}
	else
	{
		cout << "Encoding " << argv[1] << " to " << argv[2] << endl;
		source.seekg( 0, ios::beg );
		target.write( reinterpret_cast<const char*>(&g_Signature),
									sizeof(g_Signature) );
		model->Process( &source, &target, MODE_ENCODE );
	}

	source.close();
	target.close();

	return 0;
}
#include "ModelI.h"

void ModelI::Process( fstream *source, fstream *target, ModeE mode )
{
	mSource = source;
	mTarget = target;

	if( mode == MODE_ENCODE )
	{
		mAC.SetFile( mTarget );

		// encode
		Encode();

		mAC.EncodeFinish();
	}
	else // MODE_DECODE
	{
		mAC.SetFile( mSource );

		mAC.DecodeStart();

		// decode
		Decode();
	}
};
#ifndef __MODELI_H__
#define __MODELI_H__

#include "ArithmeticCoderC.h"

enum ModeE
{
	MODE_ENCODE = 0,
	MODE_DECODE
};

class ModelI  
{
public:
	void Process( fstream *source, fstream *target, ModeE mode );

protected:
	virtual void Encode() = 0;
	virtual void Decode() = 0;

	ArithmeticCoderC mAC;
	fstream *mSource;
	fstream *mTarget;
};

#endif
#include "ModelOrder0C.h"

ModelOrder0C::ModelOrder0C()
{
	// initialize probabilities with 1
	mTotal = 257; // 256 + escape symbol for termination
	for( unsigned int i=0; i<257; i++ )
		mCumCount[i] = 1;
}

void ModelOrder0C::Encode()
{

	while( !mSource->eof() )
	{
		unsigned char symbol;

		// read symbol
		mSource->read( reinterpret_cast<char*>(&symbol), sizeof( symbol ) );

		if( !mSource->eof() )
		{
			// cumulate frequencies
			unsigned int low_count = 0;
			for( unsigned char j=0; j<symbol; j++ )
				low_count += mCumCount[j];

			// encode symbol
			mAC.Encode( low_count, low_count + mCumCount[j], mTotal );

			// update model
			mCumCount[ symbol ]++;
			mTotal++;
		}
	}

	// write escape symbol for termination
	mAC.Encode( mTotal-1, mTotal, mTotal );
}

void ModelOrder0C::Decode()
{
	unsigned int symbol;

	do
	{
		unsigned int value;

		// read value
		value = mAC.DecodeTarget( mTotal );

		unsigned int low_count = 0;

		// determine symbol
		for( symbol=0; low_count + mCumCount[symbol] <= value; symbol++ )
			low_count += mCumCount[symbol];

		// write symbol
		if( symbol < 256 )
			mTarget->write( reinterpret_cast<char*>(&symbol), sizeof( char ) );

		// adapt decoder
		mAC.Decode( low_count, low_count + mCumCount[ symbol ] );

		// update model
		mCumCount[ symbol ]++;
		mTotal++;
	}
	while( symbol != 256 ); // until termination symbol read
}
#ifndef __MODELORDER0C_H__
#define __MODELORDER0C_H__

#include "ModelI.h"

class ModelOrder0C : public ModelI  
{
public:
	ModelOrder0C();

protected:
	void Encode();
	void Decode();

	unsigned int mCumCount[ 257 ];
	unsigned int mTotal;
};

#endif
#ifndef __TOOLS_H__
#define __TOOLS_H__

int inline min( int a, int b )
{
	return a<b?a:b;
};

#endif
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.