cgohlke / imagecodecs

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

Is this code correct in packbits_encode ? #107

Closed dvht closed 1 month ago

dvht commented 1 month ago

The code for packing packbits is pointing at a literal ( non-repeating block ) and tries to avoid that 2 consecutive bytes in the mid of a series of unique characters interrupt the literal block since it would increase the length with one.
If it is followed by another replication block, it or happens at the end, it can become a replication block of 2 bytes without overhead.

But line https://github.com/cgohlke/imagecodecs/blob/c85049eaca98a28963b3a447a284a74680c794ce/imagecodecs/imcd.c#L759 calculates the position after the replicate block by adding the length to srcptr instead of dupptr.

Or am I missing something here ?

Regards, Dirk

cgohlke commented 1 month ago

Thank you. Good catch. It should be uint8_t* nextsrc = dupptr + replicate;. Otherwise the code fails to skip short replication blocks in most cases.