int output_code(int code, ref StateLZW st)
        {
            int bits;

            if (st.maxCode > 2048)
            {
                bits = 12;
            }
            else
            if (st.maxCode > 1024)
            {
                bits = 11;
            }
            else
            if (st.maxCode > 512)
            {
                bits = 10;
            }
            else
            {
                bits = 9;
            }
            st.buf[st.p++] |= (byte)(code << st.bs);
            code          >>= (8 - st.bs);
            bits           -= 8 - st.bs;
            if (bits > 8)
            {
                st.buf[st.p++] = (byte)code;
                bits          -= 8;
                code         >>= 8;
            }
            st.buf[st.p] = (byte)code;
            st.bs        = bits;
            return(st.p);
        }
        int input_code(ref StateLZW st)
        {
            int bits;
            int maxCode = st.maxCode + 1;

            if (maxCode > 2048)
            {
                bits = 12;
            }
            else
            if (maxCode > 1024)
            {
                bits = 11;
            }
            else
            if (maxCode > 512)
            {
                bits = 10;
            }
            else
            {
                bits = 9;
            }
            int code = st.buf[st.p++] >> st.bs;
            int bs2  = (8 - st.bs);

            bits -= bs2;
            if (bits > 8)
            {
                code |= st.buf[st.p++] << bs2;
                bs2  += 8;
                bits -= 8;
            }
            code |= (st.buf[st.p] & ((1 << bits) - 1)) << bs2;
            st.bs = bits;
            return(code);
        }