📄 ippcompress.c
字号:
/* //////////////////////////////// "ippcompress.c" /////////////////////////////
//
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright(c) 2005 Intel Corporation. All Rights Reserved.
//
//
// Sample of IPP DC ( data compression ) domain functions usage
//
*/
#include "ippcompress.h"
#define DEFAULTBLOCKSIZE 921600 /* default program blocksize */
#define ZLIBBLOCKSIZE 65536 /* 64k block is a default blocksize 4 zlib */
#define BLOCKUNIT 102400 /* 100 times 1024 */
#define EXCHANGEBUFFERS(PSRC, PDST) \
pTmp = PSRC; PSRC = PDST; PDST = pTmp
/* Globals */
Ipp8u * addBuff;
IppHuffState_8u * pMyHuffState1;
IppGITStrategyHint strategyHint;
IppGITState_8u * pGITState;
IppMTFState_8u * pMyMTF;
IppLZSSState_8u * pLZSSState;
int blocksize;
/* usage cases string for output */
char usage_message[] = \
"\nCopyright(c) 2005 Intel Corporation. All Rights Reserved.\n\nippcompress example\n"
"\nUsage: %s [-1|-2|-3|-4] [-e|-d] -sf source_file -df destination_file {-b number} {-sh strategy_hint}\n"
"\n -e \t Does the encoding of source_file using method 1..4 and writes results to 'destination_file'"
"\n -d \t Does the decoding of source_file using method 1..4 and writes results to 'destination_file'\n"
"\n -sf\t Specifies the source file name"
"\n -df\t Specifies the custom destination file name\n"
"\n -1\t Does the BWT->MTF->RLE->Huffman encoding/decoding scheme"
"\n -2\t Does the BWT->MTF->GIT encoding/decoding scheme"
"\n -3\t Does the LZSS encoding/decoding scheme"
"\n -4\t Does the LZ77 encoding/decoding scheme\n"
"\n -b \t (optional) Sets the custom size of the encoding input block from 1...9 where 1 means 100Kb, 2 means 200Kb, ... 9 means 900Kb\n"
"\n -sh\t (optional) Sets the strategy hint for GIT (f - fixed order, l - left reorder, r - right reorder)\n"
"\n Note, that order of the arguments is predefined.\n"
"\n\nExample: %s 1 e sf file1 df file1.z_ipp_2 b 5\n";
/* if user unproperly uses the program */
void usage(char *name) {
fprintf( stderr, usage_message, name, name );
exit(0);
}
/*************************************************************************************************
* Function:
* int readBlockFromFile (FILE *in, Ipp8u **block, int blocksize)
*
* Reads the block of data into 'Ipp8u **block' of size 'int blocksize' from file 'FILE *in'
* and returns the number bytes readed.
*
************************************************************************************************/
int readBlockFromFile (FILE *in, Ipp8u **block, int blockssize) {
return (int)fread( *block, sizeof(Ipp8u), blockssize, in );
}
/*************************************************************************************************
* Function:
* int writeBlockToFile ( FILE *to, Ipp8u *block, int blocksize )
*
* Writes the block of data from 'Ipp8u **block' of size 'int blocksize' to file 'FILE *to'
* and returns the number bytes writed.
*
************************************************************************************************/
int writeBlockToFile ( FILE *to, Ipp8u *block, int blockssize ) {
return (int)fwrite( block, sizeof(Ipp8u), blockssize, to );
}
/*************************************************************************************************
* Function:
* IppStatus DoTheForwardBWT( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen)
*
* Function for relization of forward BWT
* Gets the pSrc as a source vector of length SrcLen, does BWT and writes result to pDst
* ( and length of pDst in pDstLen )
*
* Main Parameters:
* pSrc - pointer to Source buffer,
* pDst - pointer to Destination buffer,
* SrcLen - Length of source buffer,
* pDstLen - Length of destination buffer
*
* Returns:
* ippStsNullPtrErr If One or several pointer(s) is NULL
* ippStsSizeErr Length of the source vector is less or equal zero
* ippStsNoErr No errors
*
************************************************************************************************/
IppStatus DoTheForwardBWT( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen) {
IppStatus st;
int firstMeetAt = 0;
/* checks the pointer is not null */
if( NULL == pSrc || NULL == pDst )
return ippStsNullPtrErr;
/* checks the pointer is not null */
if( NULL == addBuff) {
return ippStsNullPtrErr;
}
*pDstLen = SrcLen + sizeof(int);
/* does the forward BWT */
if( ippStsNoErr != ( st = ippsBWTFwd_8u( pSrc, pDst + sizeof(int), SrcLen, &firstMeetAt, addBuff ) ) ) {
fprintf( stderr, "Error <%d> while trying forward BWT. Exiting.\n", st );
return st;
}
((int*)pDst)[0] = firstMeetAt;
return ippStsNoErr; /* No errors */
}
/*************************************************************************************************
* Function:
* IppStatus DoTheInverseBWT( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen)
*
* Function for relization of backward BWT
* Gets the pSrc as a source vector of length SrcLen, does BWT and writes result to pDst
* ( and length of pDst in pDstLen )
*
* Main Parameters:
* pSrc - pointer to Source buffer,
* pDst - pointer to Destination buffer,
* SrcLen - Length of source buffer,
* pDstLen - Length of destination buffer
*
* Returns:
* ippStsNullPtrErr If One or several pointer(s) is NULL
* ippStsSizeErr Length of the source vector is less or equal zero
* ippStsNoErr No errors
*
************************************************************************************************/
IppStatus DoTheInverseBWT( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen) {
IppStatus st;
int bwtFirstShot = 0;
/* checks the pointer is not null */
if( NULL == pSrc || NULL == pDst )
return ippStsNullPtrErr;
/* reads first four bytes to get the index of BWT */
bwtFirstShot = ((int*)pSrc)[0];
/* checks the pointer is not null */
if( NULL == addBuff ) {
return ippStsNullPtrErr;
}
/* does the backward BWT */
if( ippStsNoErr != ( st = ippsBWTInv_8u( pSrc + ( sizeof(int) ), pDst, SrcLen - sizeof(int), bwtFirstShot, addBuff ) ) ) {
fprintf(stderr, "Error <%d> while trying do BWT. Exiting.\n", st);
return st;
}
/* set the destination pointer to corresponding values */
*pDstLen = SrcLen - sizeof(int);
return ippStsNoErr; /* No errors */
}
/*************************************************************************************************
* Function:
* IppStatus DoTheForwardMTF( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen)
*
* Function for relization of forward MTF
* Gets the pSrc as a source vector of length SrcLen, does forward MTF and
* writes result to pDst ( and length of pDst in pDstLen )
*
* Main Parameters:
* pSrc - pointer to Source buffer,
* pDst - pointer to Destination buffer,
* SrcLen - Length of source buffer,
* pDstLen - Length of destination buffer
*
* Returns:
* ippStsNullPtrErr If One or several pointer(s) is NULL
* ippStsSizeErr Length of the source vector is less or equal zero
* ippStsNoErr No errors
*
************************************************************************************************/
IppStatus DoTheForwardMTF( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen) {
/* variables */
IppStatus st = ippStsNoErr;
/* checks the pointer is not null */
if( NULL == pSrc || NULL == pDst )
return ippStsNullPtrErr;
/* does the forward MTF transformation */
if( ippStsNoErr != ( st = ippsMTFFwd_8u( pSrc, pDst, SrcLen, pMyMTF) ) ) {
fprintf(stderr, "Error <%d> while trying do forward MTF. Exiting.\n", st);
return st;
}
*pDstLen = SrcLen;
return ippStsNoErr; /* No errors */
}
/*************************************************************************************************
* Function:
* IppStatus DoTheInverseMTF( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen)
*
* Function for relization of backward MTF
* Gets the pSrc as a source vector of length SrcLen, does backward MTF and
* writes result to pDst ( and length of pDst in pDstLen )
*
* Main Parameters:
* pSrc - pointer to Source buffer,
* pDst - pointer to Destination buffer,
* SrcLen - Length of source buffer,
* pDstLen - Length of destination buffer
*
* Returns:
* ippStsNullPtrErr If One or several pointer(s) is NULL
* ippStsSizeErr Length of the source vector is less or equal zero
* ippStsNoErr No errors
*
************************************************************************************************/
IppStatus DoTheInverseMTF( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen) {
IppStatus st;
/* checks the pointer is not null */
if( NULL == pSrc || NULL == pDst )
return ippStsNullPtrErr;
/* does the backward MTF transformation */
if( ippStsNoErr != ( st = ippsMTFInv_8u( pSrc, pDst, SrcLen, pMyMTF) ) ) {
fprintf(stderr, "Error <%d> while trying do backward MTF. Exiting.\n", st);
return st;
}
*pDstLen = SrcLen;
return ippStsNoErr; /* No errors */
}
/*************************************************************************************************
* Function:
* IppStatus DecodeRLE( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen )
*
* Function for relization of RLE Decoding
* Gets the pSrc as a source vector of length SrcLen, does RLE decoding and
* writes result to pDst ( and length of pDst in pDstLen )
*
* Main Parameters:
* pSrc - pointer to Source buffer,
* pDst - pointer to Destination buffer,
* SrcLen - Length of source buffer,
* pDstLen - Length of destination buffer
*
* Returns:
* ippStsNullPtrErr If One or several pointer(s) is NULL
* ippStsSizeErr Length of the source vector is less or equal zero
* ippStsNoErr No errors
*
************************************************************************************************/
IppStatus DecodeRLE( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen ) {
IppStatus st;
/* store pointer */
Ipp8u * pSrc1 = pSrc;
/* checks the pointer is not null */
if( NULL == pSrc || NULL == pDst )
return ippStsNullPtrErr;
/* does the RLE decoding */
st = ippsDecodeRLE_8u( &pSrc1, &SrcLen, pDst, pDstLen );
/* check the returned status of RLE decoding */
if ( st != ippStsNoErr && st != ippStsDstSizeLessExpected ) {
fprintf(stderr, "Error <%d> while trying RLE decoding. Exiting.\n", st);
return st;
}
return ippStsNoErr; /* No errors */
}
/*************************************************************************************************
* Function:
* IppStatus EncodeRLE( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen )
*
* Function for relization of RLE Encoding
* Gets the pSrc as a source vector of length SrcLen, does RLE Encoding and
* writes result to pDst ( and length of pDst in pDstLen )
*
* Main Parameters:
* pSrc - pointer to Source buffer,
* pDst - pointer to Destination buffer,
* SrcLen - Length of source buffer,
* pDstLen - Length of destination buffer
*
* Returns:
* ippStsNullPtrErr If One or several pointer(s) is NULL
* ippStsSizeErr Length of the source vector is less or equal zero
* ippStsNoErr No errors
*
************************************************************************************************/
IppStatus EncodeRLE( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen ) {
/* variables */
IppStatus st;
Ipp8u *pSrc1 = pSrc;
/* checks the pointer is not null */
if( NULL == pSrc || NULL == pDst )
return ippStsNullPtrErr;
/* do the RLE encoding */
st = ippsEncodeRLE_8u( &pSrc1, &SrcLen, pDst, pDstLen );
if( st != ippStsNoErr ) {
fprintf( stderr, "Error <%d> while trying RLE encoding. Exiting.\n", st );
}
return ippStsNoErr; /* No errors */
}
/*************************************************************************************************
* Function:
* IppStatus DecodeHuffman( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen )
*
* Function for realization of Huffman Decoding
* Gets the pSrc as a source vector of length SrcLen, does Huffman decoding and
* writes result to pDst ( and length of pDst in pDstLen )
*
* Main Parameters:
* pSrc - pointer to Source buffer,
* pDst - pointer to Destination buffer,
* SrcLen - Length of source buffer,
* pDstLen - Length of destination buffer
*
* Returns:
* ippStsNullPtrErr If One or several pointer(s) is NULL
* ippStsSizeErr Length of the source vector is less or equal zero
* ippStsNoErr No errors
*
************************************************************************************************/
IppStatus DecodeHuffman( Ipp8u * pSrc, int SrcLen, Ipp8u *pDst, int *pDstLen ) {
int tmpLen = *pDstLen, /* length of codeTable */
codeLenTable[256], i;
IppHuffState_8u * pMyHuffState = NULL;
IppStatus st;
for( i = 0; i < 256; i++ )
codeLenTable[i] = 0;
/* checks the pointer is not null */
if( NULL == pSrc || NULL == pDst )
return ippStsNullPtrErr;
/* extracts the codeLenTable from srcBuff */
if( ippStsNoErr != ( st = ippsHuffLenCodeTableUnpack_8u( pSrc, &tmpLen, codeLenTable ) ) ) {
fprintf(stderr, "Error <%d> while decoding Huffman (Unpack Table)\n", st);
return st;
}
/* Init the memory needed for huffman structure */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -