cgohlke / imagecodecs

Image transformation, compression, and decompression codecs
https://pypi.org/project/imagecodecs
BSD 3-Clause "New" or "Revised" License
111 stars 21 forks source link

Possible issue with lzf codec #103

Closed sharkinsspatial closed 3 weeks ago

sharkinsspatial commented 3 weeks ago

Environment

Python 3.11.8 liblzf 3.6 imagecodecs 2024.6.1

Expected Behavior

lzf_encode and lzf_decode can compress and decompress a numpy array.

Actual Behavior

lzf_decode throws an error with float data

Example Code

import imagecodecs
import numpy as np

array = np.arange(10, dtype=np.float32)

compressed_array = imagecodecs.lzf_encode(array)

decompressed_array = imagecodecs.lzf_decode(compressed_array)

Returns

File "imagecodecs/_lzf.pyx", line 162, in imagecodecs._lzf.lzf_decode
imagecodecs._lzf.LzfError: lzf_decompress returned 0
cgohlke commented 3 weeks ago

If lzf_encode and lzf_decode are not used with header=True, the output size has to be passed to lzf_decode, for example lzf_decode(compressed_array, out=array.nbytes).

The error handling should be improved, including checking the errno variable...

cgohlke commented 3 weeks ago

The next version will check errno and raise LzfError: lzf_decompress detected the output buffer is not large enough to hold the decompressed data in such cases. The default output buffer size is raised to 8 times the input size.