Пример #1
0
        void UncompressRgba(byte[] input, byte[] output)
        {
            int src = input.ToInt32(0);

            using (var mem = new MemoryStream(input, 4, src - 4))
                using (var bits = new MsbBitStream(mem))
                {
                    int dst = 0;
                    for (int y = 0; y < m_h; ++y)
                    {
                        int rgb   = 0;
                        int alpha = 0;
                        int x     = 0;
                        while (x < m_w)
                        {
                            int len = input[src++];
                            if (alpha != 0)
                            {
                                for (int i = 0; i < len; ++i)
                                {
                                    if (bits.GetNextBit() != 0)
                                    {
                                        int b = ReadABits(bits, rgb) + 3;
                                        int g = ReadABits(bits, rgb >> 8) + 3;
                                        int r = ReadABits(bits, rgb >> 16) + 3;
                                        rgb = r << 16 | g << 8 | b;
                                    }
                                    LittleEndian.Pack(rgb | alpha << 24, output, dst);
                                    dst += 4;
                                }
                            }
                            else
                            {
                                dst += 4 * len;
                            }
                            x += len;
                            if (x >= m_w)
                            {
                                break;
                            }
                            if (bits.GetNextBit() != 0)
                            {
                                alpha = (bits.GetBits(7) << 1) + 1;
                            }
                            else if (bits.GetNextBit() != 0)
                            {
                                alpha = 0xFF;
                            }
                            else
                            {
                                alpha = 0;
                            }
                        }
                    }
                }
        }
Пример #2
0
        void UnpackGyu(byte[] packed)
        {
            using (var mem = new MemoryStream(packed, 4, packed.Length - 4))
                using (var bits = new MsbBitStream(mem))
                {
                    int dst = 0;
                    m_output[dst++] = (byte)mem.ReadByte();
                    while (dst < m_output.Length)
                    {
                        int b = bits.GetNextBit();
                        if (-1 == b)
                        {
                            throw new EndOfStreamException();
                        }
                        if (1 == b)
                        {
                            m_output[dst++] = (byte)mem.ReadByte();
                            continue;
                        }
                        int count;
                        int offset;
                        if (1 == bits.GetNextBit())
                        {
                            count  = mem.ReadByte() << 8;
                            count |= mem.ReadByte();
                            offset = -1 << 13 | count >> 3;
                            count &= 7;

                            if (0 != count)
                            {
                                ++count;
                            }
                            else
                            {
                                count = mem.ReadByte();
                                if (0 == count)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            count  = 1 + bits.GetBits(2);
                            offset = -1 << 8 | mem.ReadByte();
                        }

                        Binary.CopyOverlapped(m_output, dst + offset, dst, ++count);
                        dst += count;
                    }
                }
        }
Пример #3
0
 void LzUnpack(Stream input, byte[] output)
 {
     using (var bits = new MsbBitStream(input, true))
     {
         int dst = 0;
         while (dst < output.Length)
         {
             int ctl = bits.GetNextBit();
             if (-1 == ctl)
             {
                 break;
             }
             if (0 == ctl)
             {
                 output[dst++] = (byte)bits.GetBits(8);
             }
             else
             {
                 int offset = bits.GetBits(8);
                 int count  = bits.GetBits(8);
                 if (offset <= 0)
                 {
                     break;
                 }
                 Binary.CopyOverlapped(output, dst - offset, dst, count);
                 dst += count;
             }
         }
     }
 }
Пример #4
0
        void LzUnpack(Stream input, byte[] output)
        {
            var frame     = new byte[0x1000];
            int frame_pos = 1;
            int dst       = 0;

            using (var bits = new MsbBitStream(input))
            {
                while (dst < output.Length)
                {
                    if (0 != bits.GetNextBit())
                    {
                        byte b = (byte)bits.GetBits(8);
                        output[dst++] = b;
                        frame[frame_pos++ & 0xFFF] = b;
                    }
                    else
                    {
                        int offset = bits.GetBits(12);
                        int count  = bits.GetBits(4) + 2;
                        for (int i = 0; i < count; ++i)
                        {
                            byte b = frame[(offset + i) & 0xFFF];
                            output[dst++] = b;
                            frame[frame_pos++ & 0xFFF] = b;
                        }
                    }
                }
            }
        }
Пример #5
0
        void LzUnpack(Stream input, byte[] output, int dst)
        {
            var frame     = new byte[0x4000];
            int frame_pos = 1;

            using (var bits = new MsbBitStream(input, true))
            {
                while (dst < output.Length)
                {
                    int ctl = bits.GetNextBit();
                    if (-1 == ctl)
                    {
                        break;
                    }
                    if (ctl != 0)
                    {
                        int v = bits.GetBits(8);
                        output[dst++] = frame[frame_pos++ & 0x3FFF] = (byte)v;
                    }
                    else
                    {
                        int offset = bits.GetBits(14);
                        int count  = bits.GetBits(4) + 3;
                        while (count-- > 0)
                        {
                            byte v = frame[offset++ & 0x3FFF];
                            output[dst++] = frame[frame_pos++ & 0x3FFF] = v;
                        }
                    }
                }
            }
        }
Пример #6
0
        void UnpackHuffman(MsbBitStream input, byte[] output)
        {
            var  root        = BuildHuffmanTree(m_info.FreqTable);
            int  dst         = 0;
            byte last_symbol = 0;

            while (dst < output.Length)
            {
                var node = root;
                while (node.Symbol > 0x1F)
                {
                    if (input.GetNextBit() != 0)
                    {
                        node = node.Left;
                    }
                    else
                    {
                        node = node.Right;
                    }
                }
                byte symbol = (byte)(last_symbol + node.Symbol);
                if (symbol > 0x1F)
                {
                    symbol -= 0x20;
                }
                output[dst++] = symbol;
                last_symbol   = symbol;
            }
        }
Пример #7
0
        internal byte[] Decompress(IBinaryStream input, int unpacked_size)
        {
            input.Position = 0x12;
            int data_pos    = input.ReadInt32();
            int bits_length = Math.Min(data_pos - 0x10, (unpacked_size + 7) / 8);
            var ctl_bits    = input.ReadBytes(bits_length);

            input.Position = 6 + data_pos;
            var output = new byte[unpacked_size];

            using (var mem = new MemoryStream(ctl_bits))
                using (var bits = new MsbBitStream(mem))
                    using (var data = new MsbBitStream(input.AsStream, true))
                    {
                        int dst = 0;
                        while (dst < unpacked_size)
                        {
                            int ctl = bits.GetNextBit();
                            if (-1 == ctl)
                            {
                                break;
                            }
                            if (ctl != 0)
                            {
                                output[dst++] = (byte)data.GetBits(8);
                            }
                            else
                            {
                                int offset, count;
                                if (bits.GetNextBit() != 0)
                                {
                                    offset = data.GetBits(14);
                                    count  = data.GetBits(4) + 3;
                                }
                                else
                                {
                                    offset = data.GetBits(9);
                                    count  = data.GetBits(3) + 2;
                                }
                                count = Math.Min(count, output.Length - dst);
                                Binary.CopyOverlapped(output, dst - offset - 1, dst, count);
                                dst += count;
                            }
                        }
                        return(output);
                    }
        }
Пример #8
0
 public List <Entry> Read()
 {
     byte[] packed;
     using (var input = m_file.CreateStream(12, m_data_offset - 12))
         using (var bits = new MsbBitStream(input))
         {
             bits.GetNextBit();
             m_dict = ReadBytes(bits);
             if (null == m_dict)
             {
                 return(null);
             }
             int packed_size   = ReadInt32(bits);
             int unpacked_size = ReadInt32(bits);
             packed = new byte[packed_size];
             for (int i = 0; i < packed_size; ++i)
             {
                 packed[i] = (byte)bits.GetBits(8);
             }
         }
     using (var bstr = new BinMemoryStream(packed))
         using (var zstr = new ZLibStream(bstr, CompressionMode.Decompress))
             using (var index = new MsbBitStream(zstr))
             {
                 index.GetNextBit();
                 int count = ReadInt32(index);
                 if (!ArchiveFormat.IsSaneCount(count))
                 {
                     return(null);
                 }
                 var dir = new List <Entry> (count);
                 for (int i = 0; i < count; ++i)
                 {
                     if (index.GetBits(2) == -1)
                     {
                         break;
                     }
                     var name_buf = ReadEncryptedChars(index);
                     if (null == name_buf)
                     {
                         return(null);
                     }
                     var name  = DecryptString(name_buf, name_buf.Length);
                     var entry = FormatCatalog.Instance.Create <Entry> (name);
                     ReadInt32(index);
                     ReadInt32(index);
                     entry.Offset = (uint)ReadInt32(index) + m_data_offset;
                     entry.Size   = (uint)ReadInt32(index);
                     if (!entry.CheckPlacement(m_file.MaxOffset))
                     {
                         return(null);
                     }
                     dir.Add(entry);
                 }
                 return(dir);
             }
 }
Пример #9
0
        int GetCount()
        {
            int n = 1;

            while (0 == m_input.GetNextBit())
            {
                ++n;
            }
            return(m_input.GetBits(n) + (1 << n) - 2);
        }
Пример #10
0
 int GetIndex(int count)
 {
     if (0 == --count)
     {
         return(m_bits.GetNextBit());
     }
     if (count < m_index_length_limit)
     {
         return(1 << count | m_bits.GetBits(count));
     }
     while (0 != m_bits.GetNextBit())
     {
         if (count >= 0x10)
         {
             throw new InvalidFormatException("Invalid index count");
         }
         ++count;
     }
     return(1 << count | m_bits.GetBits(count));
 }
Пример #11
0
        protected override IEnumerator <int> Unpack()
        {
            var frame     = new byte[0x1000];
            int dst       = 0;
            int frame_pos = 1;

            while (dst < m_unpacked_size)
            {
                int bit = m_input.GetNextBit();
                if (bit != 0)
                {
                    if (-1 == bit)
                    {
                        yield break;
                    }
                    int v = m_input.GetBits(8);
                    if (-1 == v)
                    {
                        yield break;
                    }
                    frame[frame_pos++ & 0xFFF] = m_buffer[m_pos++] = (byte)v;
                    dst++;
                    if (0 == --m_length)
                    {
                        yield return(m_pos);
                    }
                }
                else
                {
                    int offset = m_input.GetBits(12);
                    if (-1 == offset)
                    {
                        yield break;
                    }
                    int count = m_input.GetBits(4);
                    if (-1 == count)
                    {
                        yield break;
                    }
                    count += 2;
                    dst   += count;
                    while (count-- > 0)
                    {
                        byte v = frame[offset++ & 0xFFF];
                        frame[frame_pos++ & 0xFFF] = v;
                        m_buffer[m_pos++]          = v;
                        if (0 == --m_length)
                        {
                            yield return(m_pos);
                        }
                    }
                }
            }
        }
Пример #12
0
        public override Stream OpenEntry(ArcFile arc, Entry entry)
        {
            if (entry.Size < 0x10 || !arc.File.View.AsciiEqual(entry.Offset, "CRILAYLA"))
            {
                return(base.OpenEntry(arc, entry));
            }

            var unpacked_size = arc.File.View.ReadInt32(entry.Offset + 8);
            var packed_size   = arc.File.View.ReadUInt32(entry.Offset + 12);

            if (unpacked_size < 0 || packed_size > entry.Size - 0x10)
            {
                return(base.OpenEntry(arc, entry));
            }
            uint prefix_size = entry.Size - (0x10 + packed_size);
            var  output      = new byte[unpacked_size + prefix_size];
            var  packed      = arc.File.View.ReadBytes(entry.Offset + 0x10, packed_size);

            Array.Reverse(packed);
            using (var mem = new MemoryStream(packed))
                using (var input = new MsbBitStream(mem))
                {
                    byte[] sizes = { 2, 3, 5, 8 };
                    int    dst   = (int)prefix_size;
                    while (dst < output.Length)
                    {
                        if (0 == input.GetNextBit())
                        {
                            output[dst++] = (byte)input.GetBits(8);
                            continue;
                        }
                        int count = 3;
                        int offset = input.GetBits(13) + 3;
                        int rank = 0;
                        int bits, step;
                        do
                        {
                            bits   = sizes[rank];
                            step   = input.GetBits(bits);
                            count += step;
                            if (rank < 3)
                            {
                                rank++;
                            }
                        }while (((1 << bits) - 1) == step);
                        Binary.CopyOverlapped(output, dst - offset, dst, count);
                        dst += count;
                    }
                }
            Array.Reverse(output, (int)prefix_size, unpacked_size);
            arc.File.View.Read(entry.Offset + 0x10 + packed_size, output, 0, prefix_size);
            return(new BinMemoryStream(output, entry.Name));
        }
Пример #13
0
        void UnpackHuffman()
        {
            m_token = 256;
            ushort root = CreateHuffmanTree();
            int    dst  = 0;

            while (dst < m_output.Length)
            {
                ushort symbol = root;
                while (symbol >= 0x100)
                {
                    int bit = m_input.GetNextBit();
                    if (-1 == bit)
                    {
                        throw new EndOfStreamException();
                    }
                    symbol = m_tree[bit, symbol - 256];
                }
                m_output[dst++] = (byte)symbol;
            }
        }
Пример #14
0
        protected override IEnumerator <int> Unpack()
        {
            var frame     = new byte[0x400];
            int frame_pos = 1;

            for (;;)
            {
                int bit = m_input.GetNextBit();
                if (-1 == bit)
                {
                    yield break;
                }
                if (bit != 0)
                {
                    int v = m_input.GetBits(8);
                    if (-1 == v)
                    {
                        yield break;
                    }
                    m_buffer[m_pos++] = frame[frame_pos++ & 0x3FF] = (byte)v;
                    if (0 == --m_length)
                    {
                        yield return(m_pos);
                    }
                }
                else
                {
                    int offset = m_input.GetBits(10);
                    if (-1 == offset)
                    {
                        yield break;
                    }
                    int count = m_input.GetBits(5);
                    if (-1 == count)
                    {
                        yield break;
                    }
                    count += 2;
                    while (count-- > 0)
                    {
                        byte v = frame[offset++ & 0x3FF];
                        m_buffer[m_pos++] = frame[frame_pos++ & 0x3FF] = v;
                        if (0 == --m_length)
                        {
                            yield return(m_pos);
                        }
                    }
                }
            }
        }
Пример #15
0
        public byte[] Unpack()
        {
            int dst       = 0;
            int frame_pos = 1;

            while (dst < m_output.Length)
            {
                int bit = m_input.GetNextBit();
                if (bit != 0)
                {
                    if (-1 == bit)
                    {
                        break;
                    }
                    int v = m_input.GetBits(8);
                    if (-1 == v)
                    {
                        break;
                    }
                    m_frame[frame_pos++ & 0xFFF] = m_output[dst++] = (byte)v;
                }
                else
                {
                    int offset = m_input.GetBits(12);
                    if (-1 == offset)
                    {
                        break;
                    }
                    int count = m_input.GetBits(4);
                    if (-1 == count)
                    {
                        break;
                    }
                    count += 2;
                    while (count-- > 0)
                    {
                        byte v = m_frame[offset++ & 0xFFF];
                        m_output[dst++] = v;
                        m_frame[frame_pos++ & 0xFFF] = v;
                    }
                }
            }
            return(m_output);
        }
Пример #16
0
 internal static void Unpack(Stream input, byte[] output, int dst = 0)
 {
     using (var bits = new MsbBitStream(input, true))
     {
         bits.GetNextBit();
         while (dst < output.Length)
         {
             int count = bits.GetBits(8);
             if (-1 == count)
             {
                 break;
             }
             if (count > 0x7F)
             {
                 int offset = bits.GetBits(10);
                 if (-1 == offset)
                 {
                     throw new EndOfStreamException();
                 }
                 count = Math.Min(count & 0x7F, output.Length - dst);
                 Binary.CopyOverlapped(output, dst - offset, dst, count);
                 dst += count;
             }
             else
             {
                 if (0 == count)
                 {
                     break;
                 }
                 for (int i = 0; i < count && dst < output.Length; i++)
                 {
                     int v = bits.GetBits(8);
                     if (-1 == v)
                     {
                         throw new EndOfStreamException();
                     }
                     output[dst++] = (byte)v;
                 }
             }
         }
     }
 }
Пример #17
0
        public void Unpack()
        {
            int dst       = 0;
            int frame_pos = 1;

            byte[] frame      = new byte[4096];
            int    frame_mask = frame.Length - 1;

            while (dst < m_output.Length)
            {
                int bit = m_input.GetNextBit();
                if (-1 == bit)
                {
                    break;
                }
                if (0 != bit)
                {
                    int data = m_input.GetBits(8);
                    m_output[dst++]    = (byte)data;
                    frame[frame_pos++] = (byte)data;
                    frame_pos         &= frame_mask;
                }
                else
                {
                    int win_offset = m_input.GetBits(12);
                    if (-1 == win_offset || 0 == win_offset)
                    {
                        break;
                    }

                    int count = m_input.GetBits(4) + 2;
                    for (int i = 0; i < count; i++)
                    {
                        byte data = frame[(win_offset + i) & frame_mask];
                        m_output[dst++]    = data;
                        frame[frame_pos++] = data;
                        frame_pos         &= frame_mask;
                    }
                }
            }
        }
Пример #18
0
        int GetIntV2(out int repeat)
        {
            int count = m_input.GetBits(4);

            repeat = 0;
            switch (count)
            {
            case 0:
                repeat = 1;
                while (repeat < 16 && 1 == m_input.GetNextBit())
                {
                    ++repeat;
                }
                if (16 == repeat)
                {
                    repeat = 0;
                }
                return(0);

            case 1: return(1);

            case 2:
            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                return(m_input.GetBits(count - 1) + (1 << (count - 1)));

            case 8: return(-1);

            case 9: return(-2);

            default:
                return(m_input.GetBits(count - 9) - (2 << (count - 9)));

            case -1: throw new EndOfStreamException();
            }
        }
Пример #19
0
        void Unpack(IBinaryStream input, byte[] output)
        {
            int offset_bits = input.ReadUInt8();
            int count_bits  = input.ReadUInt8();
            int dst         = 0;

            using (var bits = new MsbBitStream(input.AsStream, true))
            {
                while (dst < output.Length)
                {
                    if (bits.GetNextBit() != 0)
                    {
                        int v = bits.GetBits(8);
                        if (v < 0)
                        {
                            break;
                        }
                        output[dst++] = (byte)v;
                    }
                    else
                    {
                        int src = bits.GetBits(offset_bits);
                        if (src < 0)
                        {
                            break;
                        }
                        int count = bits.GetBits(count_bits);
                        if (count < 0)
                        {
                            break;
                        }
                        count = Math.Min(output.Length - dst, count + 1);
                        Binary.CopyOverlapped(output, src, dst, count);
                        dst += count;
                    }
                }
            }
        }
Пример #20
0
        int ReadToken()
        {
            if (m_last_token - 256 >= m_token_limit)
            {
                m_token_limit <<= 1;
                m_token_bits   += 1;
            }
            int token = m_input.GetNextBit();

            if (token > 0)
            {
                token = m_input.GetBits(m_token_bits);
                if (token != -1)
                {
                    token += 256;
                }
            }
            else if (0 == token)
            {
                token = m_input.GetBits(8);
            }
            return(token);
        }
Пример #21
0
        byte[] UnpackEntry(Stream input, uint unpacked_size)
        {
            const int dict_size = 0x10;
            var       output    = new byte[unpacked_size];

            using (var bits = new MsbBitStream(input, true))
            {
                var dict     = new byte[dict_size];
                int dict_pos = 0;
                for (int dst = 0; dst < output.Length; ++dst)
                {
                    byte cur_byte;
                    if (bits.GetNextBit() != 0)
                    {
                        int offset = GetBitLength(bits);
                        int pos    = dict_pos - offset;
                        if (pos < 0)
                        {
                            pos += dict_size;
                        }
                        if (pos < 0 || pos >= dict_size)
                        {
                            throw new InvalidDataException("Invalid compressed data.");
                        }
                        cur_byte = dict[pos];
                    }
                    else
                    {
                        cur_byte = (byte)bits.GetBits(8);
                    }
                    output[dst]      = cur_byte;
                    dict[dict_pos++] = cur_byte;
                    dict_pos        &= 0xF;
                }
            }
            return(output);
        }
Пример #22
0
        protected void Unpack24bpp(MsbBitStream bits, byte[] data, byte[] output)
        {
            int src = 0, dst = 0;

            for (int i = 0; i < m_first_pixel_size; ++i)
            {
                output[dst++] = data[src++];
            }
            while (dst < output.Length)
            {
                int  ctl = bits.GetBits(2);
                byte v;
                if (0 == ctl)
                {
                    v = data[src++];
                }
                else
                {
                    v = output[dst - 3];
                    if (ctl == 2)
                    {
                        if (bits.GetNextBit() != 0)
                        {
                            v -= 1;
                        }
                        else
                        {
                            v += 1;
                        }
                    }
                    else if (ctl == 3)
                    {
                        ctl = bits.GetBits(2);
                        if (ctl == 2)
                        {
                            if (bits.GetNextBit() != 0)
                            {
                                v -= 3;
                            }
                            else
                            {
                                v += 3;
                            }
                        }
                        else if (ctl == 3)
                        {
                            ctl = bits.GetBits(2);
                            if (ctl == 2)
                            {
                                if (bits.GetNextBit() != 0)
                                {
                                    v -= 5;
                                }
                                else
                                {
                                    v += 5;
                                }
                            }
                            else if (ctl == 3)
                            {
                                switch (bits.GetBits(2))
                                {
                                case 3:  v -= 7; break;

                                case 2:  v += 7; break;

                                case 1:  v -= 6; break;

                                default: v += 6; break;
                                }
                            }
                            else if (ctl == 1)
                            {
                                v -= 4;
                            }
                            else
                            {
                                v += 4;
                            }
                        }
                        else if (ctl == 1)
                        {
                            v -= 2;
                        }
                        else
                        {
                            v += 2;
                        }
                    }
                }
                output[dst++] = v;
            }
        }
Пример #23
0
        protected void Unpack8bpp(MsbBitStream bits, byte[] data, byte[] output)
        {
            int  src        = 0;
            int  dst        = 0;
            byte init_value = data[src++];

            output[dst++] = init_value;
            int bit_count = 0;
            int y         = 0;
            int x         = 1;

            while (dst < output.Length)
            {
                int ctl = bits.GetBits(2);
                if (0 == ctl)
                {
                    int count;
                    if (bit_count > 0)
                    {
                        count = bits.GetBits(14 - bit_count);
                    }
                    else
                    {
                        count = bits.GetBits(6);
                    }
                    while (count-- > 0 && dst < output.Length)
                    {
                        if (y == 0 || x + 1 == m_stride)
                        {
                            output[dst] = init_value;
                        }
                        else
                        {
                            output[dst] = output[dst - m_stride + 1];
                        }
                        ++dst;
                        if (++x == m_stride)
                        {
                            x = 0;
                            ++y;
                        }
                    }
                    bit_count = 0;
                    continue;
                }
                else if (1 == ctl)
                {
                    bit_count += 2;
                    if (0 == x)
                    {
                        output[dst] = init_value;
                    }
                    else
                    {
                        output[dst] = output[dst - m_stride - 1];
                    }
                }
                else if (2 == ctl)
                {
                    bit_count  += 2;
                    output[dst] = data[src++];
                }
                else
                {
                    bit_count += 3;
                    if (bits.GetNextBit() != 0)
                    {
                        output[dst] = output[dst - m_stride];
                    }
                    else
                    {
                        output[dst] = output[dst - 1];
                    }
                }
                ++dst;
                if (++x == m_stride)
                {
                    x = 0;
                    ++y;
                }
                if (bit_count >= 8)
                {
                    bit_count -= 8;
                }
            }
            if (8 == Info.BPP)
            {
                byte max = m_max_pixel; // System.Linq.Enumerable.Max (output);
                if (max != 0 && max != 0xFF)
                {
                    for (int i = 0; i < output.Length; ++i)
                    {
                        output[i] = (byte)(output[i] * 0xFF / max);
                    }
                }
            }
        }
Пример #24
0
        public override Stream OpenEntry(ArcFile arc, Entry entry)
        {
            if (entry.Size < 0x10 || !arc.File.View.AsciiEqual (entry.Offset, "CRILAYLA"))
                return base.OpenEntry (arc, entry);

            var unpacked_size = arc.File.View.ReadInt32 (entry.Offset+8);
            var packed_size = arc.File.View.ReadUInt32 (entry.Offset+12);
            if (unpacked_size < 0 || packed_size > entry.Size - 0x10)
                return base.OpenEntry (arc, entry);
            uint prefix_size = entry.Size - (0x10+packed_size);
            var output = new byte[unpacked_size+prefix_size];
            var packed = arc.File.View.ReadBytes (entry.Offset+0x10, packed_size);
            Array.Reverse (packed);
            using (var mem = new MemoryStream (packed))
            using (var input = new MsbBitStream (mem))
            {
                byte[] sizes = { 2, 3, 5, 8 };
                int dst = (int)prefix_size;
                while (dst < output.Length)
                {
                    if (0 == input.GetNextBit())
                    {
                        output[dst++] = (byte)input.GetBits (8);
                        continue;
                    }
                    int count = 3;
                    int offset = input.GetBits (13) + 3;
                    int rank = 0;
                    int bits, step;
                    do
                    {
                        bits = sizes[rank];
                        step = input.GetBits (bits);
                        count += step;
                        if (rank < 3)
                            rank++;
                    }
                    while (((1 << bits) - 1) == step);
                    Binary.CopyOverlapped (output, dst-offset, dst, count);
                    dst += count;
                }
            }
            Array.Reverse (output, (int)prefix_size, unpacked_size);
            arc.File.View.Read (entry.Offset+0x10+packed_size, output, 0, prefix_size);
            return new MemoryStream (output);
        }
Пример #25
0
        void UnpackV0()
        {
            int line = m_width * (m_height - 1);

            for (int h = 0; h < m_height; ++h)
            {
                int dst    = line;
                int column = m_width;
                while (column > 0)
                {
                    if (0 == m_input.GetNextBit())
                    {
                        int src;
                        int count;
                        if (1 == m_input.GetNextBit())
                        {
                            if (0 == m_input.GetNextBit())
                            {
                                src = dst + OffsetTable8[m_input.GetBits(3)];
                            }
                            else
                            {
                                src = m_width + dst + OffsetTable16[m_input.GetBits(4)];
                            }
                        }
                        else
                        {
                            src = dst;
                            if (1 == m_input.GetNextBit())
                            {
                                src += m_width * (m_input.GetNextBit() + 2);
                            }
                            else
                            {
                                src += m_width * (m_input.GetBits(2) + 4);
                            }
                            src += OffsetTable16[m_input.GetBits(4)];
                        }
                        if (1 == m_input.GetNextBit())
                        {
                            count = m_input.GetNextBit() + 2;
                        }
                        else if (1 == m_input.GetNextBit())
                        {
                            count = m_input.GetBits(2) + 4;
                        }
                        else if (1 == m_input.GetNextBit())
                        {
                            count = m_input.GetBits(3) + 8;
                        }
                        else if (1 == m_input.GetNextBit())
                        {
                            count = m_input.GetBits(6) + 16;
                        }
                        else if (1 == m_input.GetNextBit())
                        {
                            count = m_input.GetBits(8) + 80;
                        }
                        else
                        {
                            count = m_input.GetBits(10) + 336;
                        }
                        for (int i = 0; i < count; ++i)
                        {
                            m_output[dst++] = m_output[src++];
                        }
                        column -= count;
                    }
                    else
                    {
                        m_output[dst++] = (byte)m_input.GetBits(8);
                        --column;
                    }
                }
                line -= m_width;
            }
        }
Пример #26
0
        protected override void Unpack()
        {
            m_input.Position = 4;
            var data = ReadDataBytes();

            using (var bits = new MsbBitStream(m_input.AsStream, true))
            {
                int  stride     = (int)Info.Width;
                int  src        = 0;
                int  dst        = 0;
                byte init_value = data[src++];
                m_output[dst++] = init_value;
                int bit_count = 0;
                int y         = 0;
                int x         = 1;
                while (dst < m_output.Length)
                {
                    int ctl = bits.GetBits(2);
                    if (0 == ctl)
                    {
                        int count;
                        if (bit_count > 0)
                        {
                            count = bits.GetBits(14 - bit_count);
                        }
                        else
                        {
                            count = bits.GetBits(6);
                        }
                        while (count-- > 0 && dst < m_output.Length)
                        {
                            if (y == 0 || x + 1 == stride)
                            {
                                m_output[dst] = init_value;
                            }
                            else
                            {
                                m_output[dst] = m_output[dst - stride + 1];
                            }
                            ++dst;
                            if (++x == stride)
                            {
                                x = 0;
                                ++y;
                            }
                        }
                        bit_count = 0;
                        continue;
                    }
                    else if (1 == ctl)
                    {
                        bit_count += 2;
                        if (0 == x)
                        {
                            m_output[dst] = init_value;
                        }
                        else
                        {
                            m_output[dst] = m_output[dst - stride - 1];
                        }
                    }
                    else if (2 == ctl)
                    {
                        bit_count    += 2;
                        m_output[dst] = data[src++];
                    }
                    else
                    {
                        bit_count += 3;
                        if (bits.GetNextBit() != 0)
                        {
                            m_output[dst] = m_output[dst - stride];
                        }
                        else
                        {
                            m_output[dst] = m_output[dst - 1];
                        }
                    }
                    ++dst;
                    if (++x == stride)
                    {
                        x = 0;
                        ++y;
                    }
                    if (bit_count >= 8)
                    {
                        bit_count -= 8;
                    }
                }
            }
            const byte max = 0x20; // System.Linq.Enumerable.Max (m_output);

            if (max != 0 && max != 0xFF)
            {
                for (int i = 0; i < m_output.Length; ++i)
                {
                    m_output[i] = (byte)(m_output[i] * 0xFF / max);
                }
            }
        }
Пример #27
0
        protected virtual void Unpack()
        {
            m_input.Position = 4;
            var data = ReadDataBytes();

            using (var bits = new MsbBitStream(m_input.AsStream, true))
            {
                int src = 0;
                m_output[0] = data[src++];
                for (int dst = 1; dst < m_output.Length; ++dst)
                {
                    int  ctl = bits.GetBits(2);
                    byte v;
                    if (0 == ctl)
                    {
                        v = data[src++];
                    }
                    else
                    {
                        v = m_output[dst - 3];
                        if (ctl == 2)
                        {
                            if (bits.GetNextBit() != 0)
                            {
                                v -= 1;
                            }
                            else
                            {
                                v += 1;
                            }
                        }
                        else if (ctl == 3)
                        {
                            ctl = bits.GetBits(2);
                            if (ctl == 2)
                            {
                                if (bits.GetNextBit() != 0)
                                {
                                    v -= 3;
                                }
                                else
                                {
                                    v += 3;
                                }
                            }
                            else if (ctl == 3)
                            {
                                ctl = bits.GetBits(2);
                                if (ctl == 2)
                                {
                                    if (bits.GetNextBit() != 0)
                                    {
                                        v -= 5;
                                    }
                                    else
                                    {
                                        v += 5;
                                    }
                                }
                                else if (ctl == 3)
                                {
                                    switch (bits.GetBits(2))
                                    {
                                    case 3:  v -= 7; break;

                                    case 2:  v += 7; break;

                                    case 1:  v -= 6; break;

                                    default: v += 6; break;
                                    }
                                }
                                else if (ctl == 1)
                                {
                                    v -= 4;
                                }
                                else
                                {
                                    v += 4;
                                }
                            }
                            else if (ctl == 1)
                            {
                                v -= 2;
                            }
                            else
                            {
                                v += 2;
                            }
                        }
                    }
                    m_output[dst] = v;
                }
            }
        }