网上下载了一个RLE压缩算法的c文件(很多人都用过,说可以的),是别人写好的,然后我想简单的调用一下,结果无法输出帮我看看
//RLE.C
static void _RLE_WriteRep( unsigned char *out, unsigned int *outpos,
unsigned char marker, unsigned char symbol, unsigned int count )
{
unsigned int i, idx;
idx = *outpos;
if( count <= 3 )
{
if( symbol == marker )
{
out[ idx ++ ] = marker;
out[ idx ++ ] = count-1;
}
else
{
for( i = 0; i < count; ++ i )
{
out[ idx ++ ] = symbol;
}
}
}
else
{
out[ idx ++ ] = marker;
-- count;
if( count >= 128 )
{
out[ idx ++ ] = (count >> 8) | 0x80;
}
out[ idx ++ ] = count & 0xff;
out[ idx ++ ] = symbol;
}
*outpos = idx;
}
static void _RLE_WriteNonRep( unsigned char *out, unsigned int *outpos,
unsigned char marker, unsigned char symbol )
{
unsigned int idx;
idx = *outpos;
if( symbol == marker )
{
out[ idx ++ ] = marker;
out[ idx ++ ] = 0;
}
else
{
out[ idx ++ ] = symbol;
}
*outpos = idx;
}
/*************************************************************************
* PUBLIC FUNCTIONS *
*************************************************************************/
/*************************************************************************
* RLE_Compress() - Compress a block of data using an RLE coder.
* in - Input (uncompressed) buffer.
* out - Output (compressed) buffer. This buffer must be 0.4% larger
* than the input buffer, plus one byte.
* insize - Number of input bytes.
* The function returns the size of the compressed data.
*************************************************************************/
int RLE_Compress( unsigned char *in, unsigned char *out,
unsigned int insize )
{
unsigned char byte1, byte2, marker;
unsigned int inpos, outpos, count, i, histogram[ 256 ];
/* Do we have anything to compress? */
if( insize < 1 )
{
return 0;
}
/* Create histogram */
for( i = 0; i < 256; ++ i )
{
histogram[ i ] = 0;
}
for( i = 0; i < insize; ++ i )
{
++ histogram[ in[ i ] ];
}
/* Find the least common byte, and use it as the repetition marker */
marker = 0;
for( i = 1; i < 256; ++ i )
{
if( histogram[ i ] < histogram[ marker ] )
{
marker = i;
}
}
/* Remember the repetition marker for the decoder */
out[ 0 ] = marker;
outpos = 1;
/* Start of compression */
byte1 = in[ 0 ];
inpos = 1;
count = 1;
/* Are there at least two bytes? */
if( insize >= 2 )
{
byte2 = in[ inpos ++ ];
count = 2;
/* Main compression loop */
do
{
if( byte1 == byte2 )
{
/* Do we meet only a sequence of identical bytes? */
while( (inpos < insize) && (byte1 == byte2) &&
(count < 32768) )
{
byte2 = in[ inpos ++ ];
++ count;
}
if( byte1 == byte2 )
{
_RLE_WriteRep( out, &outpos, marker, byte1, count );
if( inpos < insize )
{
byte1 = in[ inpos ++ ];
count = 1;
}
else
{
count = 0;
}
}
else
{
_RLE_WriteRep( out, &outpos, marker, byte1, count-1 );
byte1 = byte2;
count = 1;
}
}
else
{
/* No, then don't handle the last byte */
_RLE_WriteNonRep( out, &outpos, marker, byte1 );
byte1 = byte2;
count = 1;
}
if( inpos < insize )
{
byte2 = in[ inpos ++ ];
count = 2;
}
}
while( (inpos < insize) || (count >= 2) );
}
/* One byte left? */
if( count == 1 )
{
_RLE_WriteNonRep( out, &outpos, marker, byte1 );
}
return outpos;
}
下面是我的调用:
#include"rle.c"
#include "iostream.h"
#include "string.h"
void main()
{
unsigned char w[300]="jg";
char *a="dddddddfasd584876";
unsigned char *r=w;
unsigned int l=strlen(a);
int f=RLE_Compress((unsigned char *)a,r,l);
cout<<r;
}
结果社么也没有输出来。
问题1:我就是想看看,"dddddddfasd584876"压缩一下会变成什么样子,大家能帮我实现这个简单的问题吗?
问题2:我觉得我的调用没有问题,怎么就没有东西输出呢?谢谢。