示例#1
0
//void ExpandFile( BIT_FILE *input, FILE *output, int argc, char *argv[] )
void ExpandFile( BIT_FILE *input, FILE *output, int, char*[] )
{
    int		c;
    int		run_count;
    int		bits;
    long	count;

	//Update so Silence and Compand do not conflict
    int silence_match[6] = { 31, 0, 31, 0, 30, 0 };
	int inputBuff[6];
	int i;

	bits = (int) InputBits( input, 8 );

	count = InputBits( input, 32 );

    while ( ( c = (int) InputBits( input, bits ) ) != EOF ) 
		{
		if ( c == 31 )
			{
			inputBuff[0] = c;

			for(i = 1; i < 6; i++ )
				{
				inputBuff[i] = (int) InputBits( input, bits );
				}

			if( memcmp( &inputBuff, &silence_match, 6 ) )
				{
				for(i = 0; i < 6; i++ )
					{
					c = inputBuff[i];
					putc( expand[ inputBuff[i] ], output );
					}
				}
			else
				{
				run_count = (int) InputBits( input, 8 );
				while ( run_count-- > 0 )
					putc( 0x80, output );
				}
			}
		else
			putc( expand[ c ], output );
		}

}
示例#2
0
/*
* The expansion routine gets the number of bits per code from the
* compressed file, then builds an expansion table. Each of the
* "steps" codes expands to a unique eight-bit code that lies on
* the exponential encoding curve.
*/
void ExpandFile( BIT_FILE *input, FILE *output, int argc, char *argv[] ){
/*
BIT_FILE *input:
FILE *output;
int argc;
char *argv[];
{
*/
    int steps;
    int bits;
    int value;
    int last_value;
    int i;
    int c;
    long count;
    int expand[ 256 ];
/*
* First this routine reads in the number of bits, then it builds
* the expansion table. Once the table is built, expanding the file
* is simply a matter of performing a table lookup on each code.
*/
    bits = (int) InputBits( input, 8 );
    printf( "Expanding using %d bits per sample...\n", bits );
    steps = ( 1 << (bits - 1 ) );
    last_value = 0;
    for ( i = 1; i <= steps; i++ ) {
        value = 128.0 * ( pow(2.0, (double) i / steps ) - 1.0 ) + 0.5;
        expand[ steps + i - 1 ] = 128 + ( value + last_value ) / 2;
        expand[ steps - i ] = 127 - ( value + last_value ) / 2;
        last_value = value;
}
/*
* The actual file size is stored at the start of the compressed
* file. It is read in to determine how many codes need to be
* expanded. Once that is done, expansion takes place rapidly.
*/
    for ( count = InputBits( input, 32 ); count > 0 ; count-- ) {
        c = (int) InputBits( input, bits );
        putc(expand[ c ], output );
    }
    while ( argc-- > 0 )
        printf( "Unused argument: %s\n", *argv++ ) ;
}
示例#3
0
void CharCount(char *argv)
{
	int i, sum=0;
	unsigned long byte;
	BIT_FILE *input_file;
	//get pointers of the input file and output file by argument from command line
	input_file = OpenInputBitFile(argv);

	//initialize symboy counter array symbo[NUM]
	memset(symbol, 0, sizeof(int)*NUM);
	while (1) {
		byte = InputBits(input_file, 8);
        symbol[byte]++;		
		if (byte == 256) {
			break;
		}
	}
    for (i=0; i<NUM; i++) {
		sum += symbol[i];
	}
	printf("total weight: %d\n", sum);
}
示例#4
0
/*

*-------------------------------------------------------------------------------

* 3.1 LZSS_ExpandData

*-------------------------------------------------------------------------------

*

* LZSS expand data stream

*

* This routine expands data from an in buffer with the LZSS algorithm.

* The routine read in flag bits to decide whether to read in an uncompressed

* character or an index/length pair and expands the data stream to an out

* buffer.

*

* param inBuf Input data buffer with bit access

* param outBuf Output data buffer with byte access

*

* return Void

*

*/
void LZSS_ExpandData(LZSS_InputBuffer_T *inBuf, LZSS_OutputBuffer_T *outBuf)

{

	/** Index var. for loop */

	Uint8_T i = 0;

	/** Current window position */

	Uint16_T winPos = 1;

	/** Byte to write to output buffer */

	Uint8_T outByte = 0;

	/** Length of the data match found */

	Uint8_T matchLen = 0;

	/** Position in the window of the data match */

	Uint16_T matchPos = 0;

	/** Indicator of End Of Stream reached */

	BOOL_T eosReached = FALSE;

	while (!eosReached)

	{

		/* If next bit is 1, next byte is uncompressed*/

		if (InputBit(inBuf) == 1)

		{

			/* Get uncompressed byte */

			outByte = (Uint8_T)InputBits(inBuf, (Uint8_T)8);

			/* Output byte*/

			OutputByte(outByte, outBuf);

			/* Add byte in window */

			LZSS_window[winPos] = outByte;

			/* Increase window position */

			winPos = LZSS_MOD_WINDOW(winPos + 1);

		}

		/* If next bit is 0, compressed data follows */

		else

		{

			/* Get compressed data as window position of match*/

			matchPos = (Uint16_T)InputBits(inBuf, LZSS_INDEX_BIT_COUNT);

			/* If end of stream, exit */

			if (matchPos == LZSS_END_OF_STREAM)

			{

				eosReached = TRUE;

			}

			else

			{

				/* Get length of string match */

				matchLen = (Uint8_T)InputBits(inBuf, LZSS_LENGTH_BIT_COUNT);

				/* Add break even + 1 to get the correct length. Length zero and

				* the break even value are subtracted from the length during

				* compression to save space. */

				matchLen = matchLen + (LZSS_BREAK_EVEN + 1);

				/* For every byte in match */

				for (i = 0; i < matchLen; i++)

				{

					/* Get matching byte from window */

					outByte = LZSS_window[LZSS_MOD_WINDOW(matchPos + i)];

					/* Output byte */

					OutputByte(outByte, outBuf);

					/* Add matched byte to current window position */

					LZSS_window[winPos] = outByte;

					/* Increase window position */

					winPos = LZSS_MOD_WINDOW(winPos + 1);

				}

			}

		}

	}

}
示例#5
0
文件: lzw12.c 项目: wisaly/pujia
 * The file expander operates much like the encoder.  It has to
 * read in codes, the convert the codes to a string of characters.
 * The only catch in the whole operation occurs when the encoder
 * encounters a CHAR+STRING+CHAR+STRING+CHAR sequence.  When this
 * occurs, the encoder outputs a code that is not presently defined
 * in the table.  This is handled as an exception.
 */
QUICK_EXPAND(lzw12)
    unsigned int next_code;
    unsigned int new_code;
    unsigned int old_code;
    int character;
    unsigned int count;

    next_code = FIRST_CODE;
    old_code = (unsigned int) InputBits( input, BITS );
    if ( old_code == END_OF_STREAM )
        goto quit;
    character = old_code;
    mn_putc( old_code, &output );

    while ( ( new_code = (unsigned int) InputBits( input, BITS ) )
              != END_OF_STREAM ) {
/*
** This code checks for the CHARACTER+STRING+CHARACTER+STRING+CHARACTER
** case which generates an undefined code.  It handles it by decoding
** the last code, and adding a single character to the end of the decode string.
*/
        if ( new_code >= next_code ) {
            decode_stack[ 0 ] = (char) character;
            count = lzw12_decode_string( 1, old_code );
示例#6
0
ubyte *lzw_expand( ubyte *inputbuf, ubyte *outputbuf, int length )
{
	BIT_BUF *input;
	unsigned int new_code;
	unsigned int old_code;
	int character;
	unsigned int count;
	int counter;

	input = OpenInputBitBuf( inputbuf );
	if ( outputbuf == NULL )
		outputbuf = (ubyte *)malloc(length*sizeof(ubyte));

	InitializeStorage();
	counter = 0;
	for ( ; ; )
	{

		InitializeDictionary();
		old_code = (unsigned int) InputBits( input, current_code_bits );
		if ( old_code == END_OF_STREAM )
		{
			CloseInputBitBuf( input );
			return outputbuf;
		}
		character = old_code;

		if (counter<length)
		{
			outputbuf[counter++] = ( ubyte ) old_code;
		}
		else 
		{
			//printf( "ERROR:Tried to write %d\n", old_code );
			//exit(1);
			return 0;
		}

		for ( ; ; )
		{

			new_code = (unsigned int) InputBits( input, current_code_bits );
			if ( new_code == END_OF_STREAM )
			{
				CloseInputBitBuf( input );
				FreeStorage();
				return outputbuf;
			}
			if ( new_code == FLUSH_CODE )
				break;

			if ( new_code == BUMP_CODE )
			{
				current_code_bits++;
				continue;
			}

			if ( new_code >= next_code )
			{
				decode_stack[ 0 ] = (char) character;
				count = decode_string( 1, old_code );
			}
			else
			{
				count = decode_string( 0, new_code );
			}

			character = decode_stack[ count - 1 ];
			while ( count > 0 ) {
				// This lets the case counter==length pass through.
				// This is a hack.
				if (counter<length) {
 					//printf("%x ", ( ubyte ) decode_stack[ count ]);
					outputbuf[counter++] = ( ubyte ) decode_stack[ --count ];

				} else if (counter>length) {
					printf( "ERROR:Tried to write %d\n", decode_stack[ --count ] );
					exit(1);
				} else
					count--;
			}
			dict[ next_code ].parent_code = old_code;
			dict[ next_code ].character = (char) character;
			next_code++;
			old_code = new_code;
		}
	}
}