C++ LZO压缩字符*
我已经在我的Ubuntu机器上安装了LZO,我想用ti压缩一个char*类型的字符串 在示例文件中,我发现了以下代码片段,我已经为我的应用程序对其进行了一些编辑:C++ LZO压缩字符*,c++,lzo,C++,Lzo,我已经在我的Ubuntu机器上安装了LZO,我想用ti压缩一个char*类型的字符串 在示例文件中,我发现了以下代码片段,我已经为我的应用程序对其进行了一些编辑: int r; lzo_bytep in; lzo_bytep out; lzo_voidp wrkmem; lzo_uint in_len; lzo_uint out_len; lzo_uint new_len; int strToCompressLen; // I have added this
int r;
lzo_bytep in;
lzo_bytep out;
lzo_voidp wrkmem;
lzo_uint in_len;
lzo_uint out_len;
lzo_uint new_len;
int strToCompressLen; // I have added this
/*
* Step 1: initialize the LZO library
*/
if (lzo_init() != LZO_E_OK)
{
cout << "internal error - lzo_init() failed !!!" << endl;
cout << "(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)" << endl;
//return 4;
}
// here I get the data I want to compress
char* imageData = (char*)imageIn->getFrame();
/*
* Step 2: allocate blocks and the work-memory
*/
strToCompressLen = strlen(imageData);
in = (lzo_bytep) xmalloc(strToCompressLen);
out = (lzo_bytep) xmalloc((strToCompressLen + strToCompressLen / 16 + 64 + 3));
wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
if (in == NULL || out == NULL || wrkmem == NULL)
{
cout << "out of memory" << endl;
//return 3;
}
/*
* Step 3: prepare the input block that will get compressed.
* We just fill it with zeros in this example program,
* but you would use your real-world data here.
*/
in_len = strToCompressLen;
lzo_memset(in,0,in_len);
/*
* Step 4: compress from 'in' to 'out' with LZO1X-1
*/
r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem);
if (r != LZO_E_OK)
{
/* this should NEVER happen */
cout << "internal error - compression failed: " << r << endl;
//return 2;
}
/* check for an incompressible block */
if (out_len >= in_len)
{
cout << "This block contains incompressible data." << endl;
//return 0;
}
我在这个变量中有一个要压缩的字符串:
char* imageData = (char*)imageIn->getFrame();
我需要将它转换为其他类型吗
LZO的文档不是很有用,或者我就是不能正确使用它。getFrame返回什么类型?为什么需要将其转换为char*?最明显的问题是在二进制数据上使用strlen-strlen在遇到第一个零字节时停止 getFrame返回什么类型?为什么需要将其转换为char*?最明显的问题是在二进制数据上使用strlen-strlen在遇到第一个零字节时停止 有三个主要的混淆点: 当 指针指向的缓冲区为 不是以null结尾的字符串,因为它是 令人困惑 斯特伦只会给你 以null结尾的字符串的长度, 它不会给你一个 内存中的任意缓冲区。你需要 从别处得到这些信息。 传递到的缓冲区 lzo1x_1_压缩实际上需要 包含您要删除的数据 压缩而不是空缓冲区 满是零。 假设您可以使用imageIn->getFrameSizeBytes之类的方法从imageIn获取图像的大小,请尝试以下操作:
int r;
lzo_bytep out;
lzo_voidp wrkmem;
lzo_uint out_len;
lzo_uint new_len;
/*
* Step 1: initialize the LZO library
*/
if (lzo_init() != LZO_E_OK)
{
cout << "internal error - lzo_init() failed !!!" << endl;
cout << "(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)" << endl;
//return 4;
}
// here I get the data I want to compress
lzo_bytep imageData = (lzo_bytep) imageIn->getFrame();
size_t uncompressedImageSize = imageIn->getFrameSizeBytes();
/*
* Step 2: allocate blocks and the work-memory
*/
out = (lzo_bytep) xmalloc((uncompressedImageSize + uncompressedImageSize / 16 + 64 + 3));
wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
if (out == NULL || wrkmem == NULL)
{
cout << "out of memory" << endl;
//return 3;
}
/*
* Step 4: compress from 'imageData' to 'out' with LZO1X-1
*/
r = lzo1x_1_compress(imageData,uncompressedImageSize,out,&out_len,wrkmem);
if (r != LZO_E_OK)
{
/* this should NEVER happen */
cout << "internal error - compression failed: " << r << endl;
//return 2;
}
/* check for an incompressible block */
if (out_len >= uncompressedImageSize)
{
cout << "This block contains incompressible data." << endl;
//return 0;
}
别忘了释放wrkmem。更好的是,使用C++和STD:工作记忆向量,自动释放。
有三个主要的困惑点:
当 指针指向的缓冲区为 不是以null结尾的字符串,因为它是 令人困惑 斯特伦只会给你 以null结尾的字符串的长度, 它不会给你一个 内存中的任意缓冲区。你需要 从别处得到这些信息。 传递到的缓冲区 lzo1x_1_压缩实际上需要 包含您要删除的数据 压缩而不是空缓冲区 满是零。 假设您可以使用imageIn->getFrameSizeBytes之类的方法从imageIn获取图像的大小,请尝试以下操作: int r;
lzo_bytep out;
lzo_voidp wrkmem;
lzo_uint out_len;
lzo_uint new_len;
/*
* Step 1: initialize the LZO library
*/
if (lzo_init() != LZO_E_OK)
{
cout << "internal error - lzo_init() failed !!!" << endl;
cout << "(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)" << endl;
//return 4;
}
// here I get the data I want to compress
lzo_bytep imageData = (lzo_bytep) imageIn->getFrame();
size_t uncompressedImageSize = imageIn->getFrameSizeBytes();
/*
* Step 2: allocate blocks and the work-memory
*/
out = (lzo_bytep) xmalloc((uncompressedImageSize + uncompressedImageSize / 16 + 64 + 3));
wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
if (out == NULL || wrkmem == NULL)
{
cout << "out of memory" << endl;
//return 3;
}
/*
* Step 4: compress from 'imageData' to 'out' with LZO1X-1
*/
r = lzo1x_1_compress(imageData,uncompressedImageSize,out,&out_len,wrkmem);
if (r != LZO_E_OK)
{
/* this should NEVER happen */
cout << "internal error - compression failed: " << r << endl;
//return 2;
}
/* check for an incompressible block */
if (out_len >= uncompressedImageSize)
{
cout << "This block contains incompressible data." << endl;
//return 0;
}
别忘了释放wrkmem。更好的是,使用C++和STD::工作内存的向量,自动释放。好,以后我用OpenCV编辑框架。但没关系,不用压缩就可以很好地工作。对char的转换不会导致任何问题。我只需要压缩它,因为我将通过wifi将帧发送到另一台电脑,我想节省带宽。下面是压缩问题的答案,但就我个人而言,我不会担心压缩的额外复杂性,除非分析显示网络已饱和,传输速度不够快。现代wifi有相当多的带宽@实际上问题是wifi不够快。现在我们的速度是每秒15帧,每秒15帧。我们需要更快的速度,这就是为什么我认为压缩可以帮助我们。好吧,稍后我用OpenCV编辑帧。但没关系,不用压缩就可以很好地工作。对char的转换不会导致任何问题。我只需要压缩它,因为我将通过wifi将帧发送到另一台电脑,我想节省带宽。下面是压缩问题的答案,但就我个人而言,我不会担心压缩的额外复杂性,除非分析显示网络已饱和,传输速度不够快。现代wifi有相当多的带宽@实际上问题是wifi不够快。现在我们的速度是每秒15帧,每秒15帧。我们需要它跑得更快,这就是为什么我认为压缩可以帮助我们。我有一个图像的高度和宽度。cvSizewidth,height能给我LZO的正确字节大小吗?只要每像素有一个字节,并且行的末尾没有填充,那么WITH*height就可以了。如果不知道getFrame是什么,就很难知道!这是一个640x480px的图像,具有YUV颜色空间。sizeofimageData不起作用吗?抱歉,如果这是一个愚蠢的问题,我是C++新手。sisioFixDeDATA只会给你的指针大小,即4或8,这取决于内存模型。在C++中没有找到内存块大小的真正支持,返回指针的对象也需要提供一种方法来找出指针的大小。cvSizewidth,height能给我LZO的正确字节大小吗?只要每像素有一个字节,并且行的末尾没有填充,那么WITH*height就可以了。如果不知道getFrame是什么,就很难知道!这是一个640x480px的图像,具有YUV颜色空间。sizeofimageData不起作用吗?抱歉,如果这是一个愚蠢的问题,我是C++新手。sisioFixDeDATA只会给你的指针大小,即4或8,这取决于内存模型。在C++中没有找到内存块大小的真正支持,返回指针的对象也需要提供一种方法来找出指针的大小。