⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ippcompress.c

📁 intel的ipp性能库的示例代码
💻 C
📖 第 1 页 / 共 3 页
字号:

                /* converts the size of encoded block (int) to the 4 byte (Ipp8u) */
                ((int*)header)[0] = blocksize;

                /* writes the 4 bytes (block length) into output file */
                bytesWrited = writeBlockToFile( fo, header, sizeof(int) );

                /* converts the size of encoded block (int) to the 4 byte (Ipp8u) */
                ((int*)header)[0] = (int)strategyHint;

                /* writes the 4 bytes (block length) into output file */
                bytesWrited = writeBlockToFile( fo, header, sizeof(int) );

                /* allocation of additional buffer for BWT */
                if( ippStsNoErr != ( st = ippsBWTFwdGetSize_8u( blocksize, &bwtsize ) ) ) {
                    fprintf( stderr, "Error <%d> while trying allocate memory for GIT. Exiting.\n", st );
                    return st;
                }

                addBuff = (Ipp8u *)ippMalloc( bwtsize * sizeof(Ipp8u) );

                if( NULL == addBuff ) {
                    fprintf( stderr, "Error <%d> while trying allocate memory for BWT. Exiting.\n", st );
                    return ippStsNullPtrErr;
                }

                if( ippStsNoErr != ( st = ippsEncodeGITInitAlloc_8u( blocksize << 1, blocksize << 1, &pGITState ) ) ) {
                    fprintf( stderr, "Error <%d> while trying allocate memory for GIT. Exiting.\n", st );
                    return st;
                }

                while( ( bytesReaded = readBlockFromFile( fi, &srcBuff, blocksize ) ) > 0  ) {

                    bytesEncoded = blocksize; /* possible amount of data */

                    if( ippStsNoErr != ( st = DoTheForwardBWT( srcBuff, bytesReaded, dstBuff, &bwtLen ) ) ) {
                        fprintf(stderr, "Error <%d> while trying forward BWT Transformation. Exiting.\n", st );
                        return st;
                    }

                    EXCHANGEBUFFERS( srcBuff, dstBuff );

                    if( ippStsNoErr != ( st = EncodeGIT( srcBuff, bwtLen, dstBuff, &bytesEncoded ) ) ) {
                        fprintf(stderr, "Error <%d> while trying to encode GIT. Exiting.\n", st );
                        return st;
                    }

                    /* converts the size of encoded block (int) to the 4 byte (Ipp8u) */
                    ((int*)header)[0] = bytesEncoded;
                    /* writes the 4 bytes (block length) into output file */
                    bytesWrited = writeBlockToFile( fo, header, sizeof(int) );
                    /* writes the (block length) bytes into output file */
                    bytesWrited = writeBlockToFile( fo, dstBuff, bytesEncoded );
                }


                fclose(fi);
                fclose(fo);

            break;

            case 3:

                /* converts the size of encoded block (int) to the 4 byte (Ipp8u) */
                ((int*)header)[0] = blocksize;

                /* writes the 4 bytes (block length) into output file */
                bytesWrited = writeBlockToFile( fo, header, sizeof(int) );

                ippsEncodeLZSSInitAlloc_8u(&pLZSSState);

                pSrc = srcBuff;
                pDst = dstBuff;

                dstLen = blocksize; /* sets the maximum buffer size */

                if( ( bytesReaded = readBlockFromFile( fi, &srcBuff, sizeof(Ipp8u) * blocksize ) ) < 0 ) {
                    fprintf( stderr, "Error while reading source file\n");
                    exit(11);
                }

                for( ; ; ) {

                    st = ippsEncodeLZSS_8u(&pSrc, &bytesReaded, &pDst, &dstLen, pLZSSState);

                    if( st == ippStsDstSizeLessExpected ) {

                        fwrite( dstBuff, sizeof(Ipp8u), (blocksize - dstLen), fo );
                        dstLen = blocksize;
                        pDst = dstBuff;

                    } else if( st == ippStsNoErr ) {

                        fwrite( dstBuff, sizeof(Ipp8u), (blocksize - dstLen), fo );
                        pDst = dstBuff;
                        dstLen = blocksize;
                        bytesReaded = readBlockFromFile( fi, &srcBuff, sizeof(Ipp8u) * blocksize );

                        if( bytesReaded <= 0 )
                            break;

                        pSrc = srcBuff;
                    }

                }

                if( ippStsNoErr != ( st = ippsEncodeLZSSFlush_8u(&pDst, &dstLen, pLZSSState) ) ) {
                    fprintf( stderr, "Error <%d> while flushing LZSS endings. Exiting.\n", st );
                } else {
                    fwrite( dstBuff, sizeof(Ipp8u), ( blocksize - dstLen ), fo );
                }

                fclose(fi);
                fclose(fo);

            break;

            case 4:

                bytesReaded = 1;

                for( ; bytesReaded != 0 ; ) {

                    if( ( bytesReaded = readBlockFromFile( fi, &srcBuff, blocksize ) ) < 0 ) {
                        fprintf( stderr, "Error while reading source file.\n");
                        exit(11);
                    }

                    if( gzwrite( gzfile, srcBuff, (unsigned)bytesReaded ) != bytesReaded ) exit(12);

                }

                gzclose( gzfile );
                fclose(fi);

            break;
        }

    break;

    case 'd':

        /* allocate memory 4 the source buffer */
        srcBuff = (Ipp8u *)ippMalloc( ( DEFAULTBLOCKSIZE << 1 ) * sizeof(Ipp8u) );
        dstBuff = (Ipp8u *)ippMalloc( ( DEFAULTBLOCKSIZE << 1 ) * sizeof(Ipp8u) );

        pSrc = srcBuff;
        pDst = dstBuff;

        switch( wayofwork ) {

            case 1: /* bwt->mtf->rle->huffman */

                /* allocation of additional buffer for BWT */
                if( ippStsNoErr != ( st = ippsBWTInvGetSize_8u( DEFAULTBLOCKSIZE << 1, &bwtsize ) ) )
                    return st;

                addBuff = (Ipp8u *)ippMalloc( bwtsize * sizeof( Ipp8u ) );

                if( NULL == addBuff ) {
                    return ippStsNullPtrErr;
                }

                /* allocates the memory needed for MTF transformation structure */
                if( ippStsNoErr != ( st = ippsMTFInitAlloc_8u( &pMyMTF ) ) )
                    return st;

                while( ( bytesReaded = readBlockFromFile( fi, &header, 4 ) ) > 0 ) {

                    bytesReaded = ((int*)header)[0];

                    /* when we got size of block and convert it to int, read the (blocksize) bytes */
                    bytesReaded = readBlockFromFile( fi, &srcBuff, bytesReaded );

                    /* decodes the block of size (blocksize) */
                    bytesDecoded = DEFAULTBLOCKSIZE;
                    rleLen = DEFAULTBLOCKSIZE;
                    mtfLen = DEFAULTBLOCKSIZE;
                    hufLen = DEFAULTBLOCKSIZE;

                    if( ippStsNoErr != ( st = DecodeHuffman( srcBuff, bytesReaded, dstBuff, &hufLen ) ) ) {
                        fprintf(stderr, "Error <%d> while trying to decode Huffman. Exiting.\n", st );
                        return st;
                    }

                    EXCHANGEBUFFERS( srcBuff, dstBuff );

                    if( ippStsNoErr != ( st = DecodeRLE( srcBuff, hufLen, dstBuff, &rleLen ) ) ) {
                        fprintf(stderr, "Error <%d> while trying to decode RLE. Exiting.\n", st );
                        return st;
                    }

                    EXCHANGEBUFFERS( srcBuff, dstBuff );

                    if( ippStsNoErr != ( st = DoTheInverseMTF( srcBuff, rleLen, dstBuff, &mtfLen ) ) ) {
                        fprintf(stderr, "Error <%d> while trying backward MTF Transformation. Exiting.\n", st );
                        return st;
                    }

                    EXCHANGEBUFFERS( srcBuff, dstBuff );

                    if( ippStsNoErr != ( st = DoTheInverseBWT( srcBuff, mtfLen, dstBuff, &bytesDecoded ) ) ) {
                        fprintf(stderr, "Error <%d> while trying backward BWT Transformation. Exiting.\n", st );
                        return st;
                    }

                    /* writes the result of decoding */
                    bytesWrited = writeBlockToFile( fo, dstBuff, bytesDecoded );

                }


                fclose(fi);
                fclose(fo);

            break;

            case 2: /* bwt->git (backward) */

                /* when we got size of block and convert it to int, read the (blocksize) bytes */
                bytesReaded = readBlockFromFile( fi, &header, 4 );

                /* the block (4 bytes) readed contains the blocklength integer
                trying to get it and convert to int */
                blocksize = ((int*)header)[0];

                /* when we got size of block and convert it to int, read the (blocksize) bytes */
                bytesReaded = readBlockFromFile( fi, &header, 4 );

                /* the block (4 bytes) readed contains the blocklength integer
                trying to get it and convert to int */
                strategyHint = (IppGITStrategyHint)(((int*)header)[0]);

                /* allocates memory for GIT */
                if( ippStsNoErr != ( st = ippsDecodeGITInitAlloc_8u( blocksize << 1, blocksize << 1, &pGITState ) ) ) {
                    fprintf( stderr, "Error <%d> while init structure for GIT decoding.\n", st );
                    return st;
                }

                /* allocation of additional buffer for BWT */
                if( ippStsNoErr != ( st = ippsBWTInvGetSize_8u( blocksize, &bwtsize ) ) ) {
                    fprintf( stderr, "Error <%d> while init structure for BWT decoding.\n", st );
                    return st;
                }

                addBuff = (Ipp8u *)ippMalloc( bwtsize * sizeof(Ipp8u) );

                if( NULL == addBuff) {
                    fprintf( stderr, "Error <%d> while allocate memory 4 backward bwt.\n", ippStsNullPtrErr );
                    return ippStsNullPtrErr;
                }

                while( ( bytesReaded = readBlockFromFile( fi, &header, sizeof(int) ) ) > 0 ) {

                        /* the block (4 bytes) readed contains the blocklength integer
                           trying to get it and convert to int */
                        blocksize = ((int*)header)[0];

                        /* when we got size of block and convert it to int, read the (blocksize) bytes */
                        bytesReaded = readBlockFromFile( fi, &srcBuff, blocksize );
                        bytesDecoded = ( blocksize << 1 );

                        /* GIT */
                        if( ippStsNoErr != ( st = DecodeGIT( srcBuff, bytesReaded, dstBuff, &bwtLen ) ) ) {
                            fprintf(stderr, "Error <%d> while trying to decode GIT. Exiting.\n", st );
                            return st;
                        }

                        EXCHANGEBUFFERS( srcBuff, dstBuff );

                        /* BWT */
                        if( ippStsNoErr != ( st = DoTheInverseBWT( srcBuff, bwtLen, dstBuff, &bytesDecoded ) ) ) {
                            fprintf(stderr, "Error <%d> while trying backward BWT Transformation. Exiting.\n", st );
                            return st;
                        }

                        /* writes the result of decoding */
                        bytesWrited = writeBlockToFile( fo, dstBuff, bytesDecoded );
                }

                fclose(fi);
                fclose(fo);

            break;

            case 3:

                /* when we got size of block and convert it to int, read the (blocksize) bytes */
                bytesReaded = readBlockFromFile( fi, &header, 4 );

                /* the block (4 bytes) readed contains the blocklength integer
                trying to get it and convert to int */
                blocksize = ((int*)header)[0];

                if( ippStsNoErr != ( st = ippsDecodeLZSSInitAlloc_8u( &pLZSSState ) ) ) {
                    fprintf( stderr, "Error <%d> while init structure for LZSS decoding.\n", st );
                    return st;
                }

                pSrc = srcBuff;
                pDst = dstBuff;

                dstLen = blocksize;

                bytesReaded = readBlockFromFile( fi, &srcBuff, sizeof(Ipp8u) * blocksize );

                for( ; ; ) {

                    st = ippsDecodeLZSS_8u( &pSrc, &bytesReaded, &pDst, &dstLen, pLZSSState );

                    if( st == ippStsNoErr ) {

                        fwrite( dstBuff, sizeof(Ipp8u), (blocksize - dstLen), fo );
                        pDst    = dstBuff;
                        dstLen  = blocksize;
                        bytesReaded = readBlockFromFile( fi, &srcBuff, sizeof(Ipp8u) * blocksize );
                        pSrc = srcBuff;

                        if( bytesReaded == 0 ) break;

                    } else if( st == ippStsDstSizeLessExpected ) {

                        fwrite( dstBuff, sizeof(Ipp8u), (blocksize - dstLen), fo );
                        pDst    = dstBuff;
                        dstLen  = blocksize;
                    }

                }

                fclose(fi);
                fclose(fo);

            break;

            case 4:

                bytesReaded = 1;

                for( ; bytesReaded != 0 ; ) {

                    bytesReaded = gzread( gzfile, srcBuff, blocksize);

                    if ( ( bytesWrited = writeBlockToFile( fo, srcBuff, bytesReaded ) ) != bytesReaded )
                        return (-11);
                }

                gzclose( gzfile );
                fclose(fo);

            break;
        }

    break;

    /* in other cases outputs the usage string */
    default:

        usage(argv[0]);

    break;

    }

    /* free the structures depending on wayofwork variable's value */
    switch( wayofwork ) {

        case 1:
            ippFree( pMyMTF );
        break;

        case 2:
            ippFree( pGITState );
        break;

        case 3:
            ippsLZSSFree_8u( pLZSSState );
        break;

    }

    /* frees memory */

    ippFree( srcBuff );
    ippFree( dstBuff );
    ippFree( header );

    /* everething is ok, so returns ippStsNoErr (0) */
    return 0;

err:
    usage(argv[0]);

    return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -