Ejemplo n.º 1
0
        int ReadABits(IBitStream input, int prev)
        {
            prev &= 0xFC;
            if (input.GetNextBit() == 0)
            {
                if (input.GetNextBit() == 0)
                {
                    return(prev);
                }
                else
                {
                    return(input.GetBits(6) << 2);
                }
            }
            else if (input.GetNextBit() == 0)
            {
                if (input.GetNextBit() == 0)
                {
                    return(prev + 4);
                }
                else
                {
                    return(prev - 4);
                }
            }
            else if (input.GetNextBit() == 0)
            {
                if (input.GetNextBit() == 0)
                {
                    return(prev + 8);
                }
                else
                {
                    return(prev - 8);
                }
            }
            else
            {
                switch (input.GetBits(2))
                {
                case 0:  return(Math.Min(prev + 16, 0xFC));

                case 1:  return(Math.Max(prev - 16, 0));

                case 2:  return(Math.Min(prev + 24, 0xFC));

                default: return(Math.Max(prev - 24, 0));
                }
            }
        }
Ejemplo n.º 2
0
        int ReadShort(IBitStream input, int prev)
        {
            prev &= 0xFE;
            if (input.GetNextBit() == 0)
            {
                if (input.GetNextBit() == 0)
                {
                    return(prev);
                }
                else
                {
                    return(input.GetBits(6) << 2);
                }
            }
            else if (input.GetNextBit() == 0)
            {
                if (input.GetNextBit() == 0)
                {
                    return(prev + 2);
                }
                else
                {
                    return(prev - 2);
                }
            }
            else if (input.GetNextBit() == 0)
            {
                if (input.GetNextBit() == 0)
                {
                    return(prev + 4);
                }
                else
                {
                    return(prev - 4);
                }
            }
            else
            {
                switch (input.GetBits(2))
                {
                case 0:  return(Math.Min(prev + 8, 0xFE));

                case 1:  return(Math.Max(prev - 8, 0));

                case 2:  return(Math.Min(prev + 12, 0xFE));

                default: return(Math.Max(prev - 12, 0));
                }
            }
        }
Ejemplo n.º 3
0
        void UnpackL(IBitStream input)
        {
            int dst       = 0;
            var frame     = new byte[0x10000];
            int frame_pos = 1;

            while (dst < m_output.Length)
            {
                int bit = input.GetNextBit();
                if (-1 == bit)
                {
                    break;
                }
                if (0 != bit)
                {
                    byte v = (byte)input.GetBits(8);
                    m_output[dst++]             = v;
                    frame[frame_pos++ & 0xFFFF] = v;
                }
                else
                {
                    int offset = input.GetBits(16);
                    int count  = input.GetBits(4);
                    if (-1 == offset || -1 == count)
                    {
                        break;
                    }
                    count += 3;
                    while (count-- > 0)
                    {
                        byte v = frame[offset++ & 0xFFFF];
                        m_output[dst++]             = v;
                        frame[frame_pos++ & 0xFFFF] = v;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        void UnpackZeChunk(IBitStream bits, byte[] output, int dst, int unpacked_size)
        {
            int output_end = dst + unpacked_size;

            while (dst < output_end)
            {
                int count = LzeGetInteger(bits);
                if (-1 == count)
                {
                    break;
                }

                while (--count > 0)
                {
                    int data = bits.GetBits(8);
                    if (-1 == data)
                    {
                        break;
                    }

                    if (dst < output_end)
                    {
                        output[dst++] = (byte)data;
                    }
                }
                if (count > 0 || dst >= output_end)
                {
                    break;
                }

                int offset = LzeGetInteger(bits);
                if (-1 == offset)
                {
                    break;
                }
                count = LzeGetInteger(bits);
                if (-1 == count)
                {
                    break;
                }

                Binary.CopyOverlapped(output, dst - offset, dst, count);
                dst += count;
            }
            if (dst < output_end)
            {
                throw new EndOfStreamException("Premature end of compressed stream");
            }
        }
Ejemplo n.º 5
0
        static int GetBitLength(IBitStream input)
        {
            int count = 0;

            while (0 == input.GetNextBit())
            {
                ++count;
            }
            int n = 1 << count;

            if (count > 0)
            {
                n |= input.GetBits(count);
            }
            return(n);
        }
Ejemplo n.º 6
0
        int LzeGetInteger(IBitStream bits)
        {
            int length = 0;

            for (int i = 0; i < 16; ++i)
            {
                if (0 != bits.GetNextBit())
                {
                    break;
                }
                ++length;
            }
            int v = 1 << length;

            if (length > 0)
            {
                v |= bits.GetBits(length);
            }
            return(v);
        }
Ejemplo n.º 7
0
        void UnpackP(IBitStream input)
        {
            int dst = 0;

            for (int i = 0; i < m_output.Length; ++i)
            {
                m_output[i] = 0xFF;
            }
            int width = (int)m_info.Width;

            while (dst < m_output.Length)
            {
                int count = input.GetBits(2);
                if (-1 == count)
                {
                    break;
                }
                if (2 == count)
                {
                    count = input.GetBits(2) + 2;
                }
                else if (3 == count)
                {
                    int n = 3;
                    while (input.GetNextBit() > 0)
                    {
                        ++n;
                    }
                    if (n >= 24)
                    {
                        break;
                    }
                    count = (1 << n | input.GetBits(n)) - 2;
                }
                dst               += 3 * count;
                m_output [dst]     = (byte)input.GetBits(8);
                m_output [dst + 1] = (byte)input.GetBits(8);
                m_output [dst + 2] = (byte)input.GetBits(8);
                if (input.GetNextBit() > 0)
                {
                    int copy_dst = dst;
                    for (;;)
                    {
                        int ctl = input.GetBits(2);
                        if (0 == ctl)
                        {
                            if (input.GetNextBit() <= 0)
                            {
                                break;
                            }
                            if (input.GetNextBit() > 0)
                            {
                                copy_dst += (width + 2) * 3;
                            }
                            else
                            {
                                copy_dst += (width - 2) * 3;
                            }
                        }
                        else if (1 == ctl)
                        {
                            copy_dst += (width - 1) * 3;
                        }
                        else if (2 == ctl)
                        {
                            copy_dst += width * 3;
                        }
                        else if (3 == ctl)
                        {
                            copy_dst += (width + 1) * 3;
                        }
                        else if (-1 == ctl)
                        {
                            break;
                        }
                        m_output[copy_dst]     = m_output[dst];
                        m_output[copy_dst + 1] = m_output[dst + 1];
                        m_output[copy_dst + 2] = m_output[dst + 2];
                    }
                }
                dst += 3;
            }
            byte b = 0, g = 0, r = 0;

            for (dst = 0; dst < m_output.Length; dst += 3)
            {
                if (0xFF == m_output[dst] && 0xFF == m_output[dst + 1] && 0xFF == m_output[dst + 2])
                {
                    m_output[dst]     = b;
                    m_output[dst + 1] = g;
                    m_output[dst + 2] = r;
                }
                else
                {
                    b = m_output[dst];
                    g = m_output[dst + 1];
                    r = m_output[dst + 2];
                }
            }
        }
Ejemplo n.º 8
0
        int ReadLong(IBitStream input, int prev)
        {
            prev &= 0xFC;
            if ((prev >> 2) == 0)
            {
                if (input.GetNextBit() == 0)
                {
                    return(prev);
                }
                else if (input.GetNextBit() == 0)
                {
                    return(input.GetBits(6) << 2);
                }
                else if (input.GetNextBit() == 0)
                {
                    return(4);
                }
                else if (input.GetNextBit() == 0)
                {
                    return(8);
                }
                else
                {
                    return(12);
                }
            }
            else if ((prev >> 2) == 1)
            {
                if (input.GetNextBit() == 0)
                {
                    return(input.GetNextBit() << 2);
                }
                else if (input.GetNextBit() == 0)
                {
                    return(input.GetBits(6) << 2);
                }
                else if (input.GetNextBit() == 0)
                {
                    return(8);
                }
                else if (input.GetNextBit() == 0)
                {
                    return(12);
                }
                else
                {
                    return(16);
                }
            }
            else if ((prev >> 2) == 0x3F)
            {
                if (input.GetNextBit() == 0)
                {
                    return(0xFC);
                }
                else if (input.GetNextBit() == 0)
                {
                    return(input.GetBits(6) << 2);
                }
                else if (input.GetNextBit() != 0)
                {
                    return(0xF4 + (-input.GetNextBit() & 0xFC));
                }
                else
                {
                    return(0xF8);
                }
            }
            else
            {
                if (input.GetNextBit() == 0)
                {
                    if (input.GetNextBit() == 0)
                    {
                        return(prev);
                    }
                    return(input.GetBits(6) << 2);
                }
                if (input.GetNextBit() == 0)
                {
                    if (input.GetNextBit() != 0)
                    {
                        return(prev - 4);
                    }
                    else
                    {
                        return(prev + 4);
                    }
                }
                if (input.GetNextBit() == 0)
                {
                    if (input.GetNextBit() != 0)
                    {
                        return(prev - 8);
                    }
                    else
                    {
                        return(prev + 8);
                    }
                }
                switch (input.GetBits(2))
                {
                case 0:  return(Math.Min(prev + 16, 0xFC));

                case 1:  return(Math.Max(prev - 16, 0));

                case 2:  return(Math.Min(prev + 24, 0xFC));

                default: return(Math.Max(prev - 24, 0));
                }
            }
        }