team-charls/charls

ITU T.87 JPEG-LS section A.9 contains an error? Or please explain intent and implementation.

likewise opened this issue · 1 comments

I am trying to understand a detail in the standard, that seems buggy or unclear to me.

Can someone explain if ITU T.87 JPEG-LS section A.9 contains an error? Quote from standard:

The error shall be reduced to the range relevant for coding, (– RANGE / 2 ... ///RANGE / 2\\ − 1 ) . This is achieved with
the steps detailed in Code segment A.9 (function ModRange()). ///x\\ = round-up(x).

Code segment A.9 – Modulo reduction of the error
if (Errval < 0)
Errval = Errval + RANGE;
if (Errval >= ((RANGE + 1) / 2))
Errval = Errval – RANGE;

However, suppose Errval == -1 and RANGE=1024, then clearly the above will result in Errval == 1023, out of the range relevant for coding.

lossless_traits.h implementation seems to use an optimization, not helping understand what should be done.

    FORCE_INLINE constexpr static int32_t
    modulo_range(const int32_t error_value) noexcept
    {
        return (static_cast<int32_t>(static_cast<uint32_t>(error_value) << (int32_t_bit_count - bits_per_pixel))) >>
               (int32_t_bit_count - bits_per_pixel);
    }

Thanks, Leon.

Erhm, I seem to have completely missed that the second if is not proceeded by else.