LZ4: Compression of compressed image formats

2.4k views Asked by At

I am using LZ4 compressor C lib. Some of the files I compress are JPG and PNGs. For some reason, which I fail to understand, the method

 int compressedSize =   LZ4_compress_default((char*)data,
            compressedData, uncompressedSize, uncompressedSize);

Sometimes returns negative values for images(or zero), which I am compressing.

For example, I am compressing two standard JPG images. One returns with a value '-236', while the second returns fine the number of compressed bytes, which means the first has failed to compress. Zero return means LZ4 failed to compress. I have no idea what -236 means. Is it because the source data is already compressed to some extent?

1

There are 1 answers

1
Clifford On BEST ANSWER

The function return definition is:

  return : the number of bytes written into buffer 'dest' (necessarily <= maxOutputSize)
           or 0 if compression fails

If the the compression resulted in a size larger than uncompressedSize, which is highly likely with an already compressed file, the function will fail. However that does not explain why it has returned a negative value - that is not documented. But it remains the case that if you need to accommodate compressed files, you need to allow for the file size to increase by providing a larger buffer.

Looking at the source code, the return value is generated in LZ4_compress_generic() by:

return (int) (((char*)op)-dest);

The pointer arithmetic would return a negative value if op < dest, though looking at the code, it is hard to see how that might occur. If it is a concern (and certainly if just providing a larger destination buffer does not resolve the issue), I suggest that you step through the code in your debugger to determine what the problem is.