网上下载了一个RLE压缩算法的c文件(很多人都用过,说可以的),是别人写好的,然后我想简单的调用一下,结果无法输出帮我看看

winmenaruto 2008-10-29 09:42:28
//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:我觉得我的调用没有问题,怎么就没有东西输出呢?谢谢。
...全文
313 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
winmenaruto 2008-10-30
  • 打赏
  • 举报
回复
问一下,如果只是很简单的文本,用RLE压缩,我能怎么作?上面的代码可能有问题,虽然是外国人写的,可能有问题吧。
难道这种基本的算法,没有什么微软已经封装好的类吗?STL可以吗?具体怎么做?谢谢。
cnzdgs 2008-10-30
  • 打赏
  • 举报
回复
你的调用方法没错,(被调函数有没有问题就不清楚了),压缩之后得到的是二进制数据,不是字符串,不能显示。可以调试来看结果。
另外提一下,#include通常是包含头文件的,你可以把c文件添加到项目里面,自己再写一个函数声明;或者把要调用的代码复制到自己的代码中。
yjgx007 2008-10-30
  • 打赏
  • 举报
回复
试下int f=RLE_Compress((unsigned char *)a,r,2);
至少两个字节。

19,464

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧