C++ zlib compression on float in a struct

I have been searching for a long a way to compress, using the zlib library (and the function compress) a struct containing float vars. Every example i saw are showing how to compress a string, specifically a unsigned char*.

My struct is an easy one :

struct Particle{

float x;
float y;
float z;
};

And I am calling the compress function as below :

uLong initSize = sizeof(Particle);
uLongf destSize = initSize * 1.1 + 12;
Bytef dataOriginal = (Bytef)malloc( initSize );
Bytef dataCompressed = (Bytef)malloc( destSize );

Particle p;
memset( &p, 0, sizeof(Particle) );

px = 10.24;
py = 23.5;
pz = 7.4;

memcpy( dataOriginal, &p, sizeof(p) );
compress( dataCompressed, &destSize, dataOriginal, initSize );

But when I try to uncompress my datas to see what inside, i can't get back to my initial float value :

Bytef decomp = (Bytef)malloc( initSize );
uncompress( decomp, &initSize, dataCompressed, destSize );

for( int i = 0 ; i < initSize ; i++ ){

std::cout << (float)decomp[i] << std::endl;
}

If anyone have a solution to this problem, i'm on it since 2 days now... Thanx a lot !


You would need to copy the decompressed data back into the Particle struct, just like you copied it out in the first place. (Or you could just use casts instead of copies). Then you will recover the original floats in the struct. Whatever it is you think you're doing with 'decomp[i]` doesn't make any sense.

However there are several problems with this. First, this is only assured to work on the same machine, with the same compiler, and even then only within the same version of the compiler. If a different compiler or different version chooses to align the structure differently, then the compressed data will not be transferrable between the two. If there is a different representation of floats between different machines, the compressed data will not be transferrable.

Furthermore, you will not get any compression when compressing three floats. I presume that this is just a prelude to compressing a large array of such Particle structs. Then maybe you'll get somewhere with this.

Better would be to first convert the floats to the precision needed as integers. You should know the range and the useful number of bits for your application. This will compress before even using compress() , by using only the number of bits needed as opposed to 32 per float. Then convert those integers portably to a series of bytes with shift operations. You can then also apply differencing to successive Particles (eg x1-x2, y1-y2, z1-z2), which might improve compression if there is a correlation between successive Particles.

By the way, instead of * 1.1 + 12 , you should use compressBound() , which does exactly what you want in a way that is assured by the zlib library for future versions.

链接地址: http://www.djcxy.com/p/86962.html

上一篇: 尝试在Ubuntu上使用pthread时出错

下一篇: C ++ zlib压缩浮点结构