BitMiracle.LibJpeg.Classic.Internal.huff_entropy_encoder.emit_bits_s C# (CSharp) Method

emit_bits_s() private method

Only the right 24 bits of put_buffer are used; the valid bits are left-justified in this part. At most 16 bits can be passed to emit_bits in one call, and we never retain more than 7 bits in put_buffer between calls, so 24 bits are sufficient.
private emit_bits_s ( savable_state state, int code, int size ) : bool
state savable_state
code int
size int
return bool
        private bool emit_bits_s(savable_state state, int code, int size)
        {
            /* This routine is heavily used, so it's worth coding tightly. */

            /* if size is 0, caller used an invalid Huffman table entry */
            if (size == 0)
                m_cinfo.ERREXIT(J_MESSAGE_CODE.JERR_HUFF_MISSING_CODE);

            /* mask off any extra bits in code */
            int put_buffer = code & ((1 << size) - 1);

            /* new number of bits in buffer */
            int put_bits = size + state.put_bits;

            put_buffer <<= 24 - put_bits; /* align incoming bits */

            /* and merge with old buffer contents */
            put_buffer |= state.put_buffer;

            while (put_bits >= 8)
            {
                int c = (put_buffer >> 16) & 0xFF;
                if (!emit_byte_s(c))
                    return false;

                if (c == 0xFF)
                {
                    /* need to stuff a zero byte? */
                    if (!emit_byte_s(0))
                        return false;
                }

                put_buffer <<= 8;
                put_bits -= 8;
            }

            state.put_buffer = put_buffer; /* update state variables */
            state.put_bits = put_bits;

            return true;
        }