Compression

BlitzMax Forums/BlitzMax Beginners Area/Compression

JBR(Posted 2011) [#1]
Hi,

I see BMax has compress, compress2 and uncompress.

There is not much info.

Is the compression lossless?, and why are there levels of compression?

A little demo would be great :-)

Thanks
Jim


CS_TBL(Posted 2011) [#2]
Euhm.. apart from media such as images, movies and audio, why would generic data be compressed lossy? Where's the joy in that?


JBR(Posted 2011) [#3]
Aye, this is true.

What about the levels in compress2 ?

Jim


xlsior(Posted 2011) [#4]
As far as why there are levels: high compression tends to be much more CPU intensive than mediocre compression, since it takes more time & effort to look at various options and calculate the most efficient compression form for whatever data tyou happen to through at it.

Ergo, a quick and dirty 'fast' compression that saves a little space but doesn't optimize as well, and a more thourough compression that can take significantly longer to finish. Each have their own use.


TomToad(Posted 2011) [#5]
From the zlib.html file located in the pub.mod/zlib.mod folder


int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
Compresses the source buffer into the destination buffer. sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size of the destination buffer, which must be at least 0.1% larger than sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.

This function can be used to compress a whole file at once if the input file is mmap'ed.

compress returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if there was not enough room in the output buffer.

int compress2 (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level);
Compresses the source buffer into the destination buffer. The level parameter has the same meaning as in deflateInit. sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size of the destination buffer, which must be at least 0.1% larger than sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.

compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if there was not enough room in the output buffer, Z_STREAM_ERROR if the level parameter is invalid.

int uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen);
Decompresses the source buffer into the destination buffer. sourceLen is the byte length of the source buffer. Upon entry, destLen is the total size of the destination buffer, which must be large enough to hold the entire uncompressed data. (The size of the uncompressed data must have been saved previously by the compressor and transmitted to the decompressor by some mechanism outside the scope of this compression library.) Upon exit, destLen is the actual size of the compressed buffer.

This function can be used to decompress a whole file at once if the input file is mmap'ed.

uncompress returns Z_OK if success, Z_MEM_ERROR if there was not enough memory, Z_BUF_ERROR if there was not enough room in the output buffer, or Z_DATA_ERROR if the input data was corrupted.



The compression level is -1 to 9. 0 is uncompressed, 9 is highest compression. There is a speed tradeoff for higher compression levels. -1 is default compression.


Czar Flavius(Posted 2011) [#6]
You have to provide a destination buffer large enough to hold the worst case compression first. And then you can write it to a file etc the right size.


ImaginaryHuman(Posted 2011) [#7]
I highly recommend you get the LZMA module in the code archives. LZMA compresses faster, decompresses faster, and compresses smaller than zlib. It's really superior to zip/zlib in the same way that Jpeg2000 is superior to Jpeg.

And it is lossless. And I only ever use maximum compression. Why not?


Sub_Zero(Posted 2011) [#8]
I encountered the same problem a while ago, found the solution here:

http://www.blitzmax.com/bmdocs/command.php?name=compress&ref=2d_a-z