Пример #1
0
        void ParseEncryptedIndex(int count)
        {
            int header_length = m_index.ReadUInt8();

            if (0 != header_length)
            {
                m_info.Prefix = m_index.ReadBytes(header_length);
            }
            m_wide_offset = m_index.ReadByte() != 0;
            int name_tree_length   = m_index.ReadInt32();
            var entry_table_offset = m_index.Position + name_tree_length;

            m_name_buf = new byte[0x110];
            TraverseIndex(m_index.Position, 0);
            if (m_dir.Count != count)
            {
                throw new InvalidFormatException();
            }

            foreach (EpkEntry entry in m_dir)
            {
                m_index.Position = entry_table_offset + entry.DirIndex * 12;
                ReadEntry(entry);
            }
        }
Пример #2
0
        }                                                               // 'YGP'

        public override ImageMetaData ReadMetaData(IBinaryStream stream)
        {
            stream.Position = 4;
            int  mask_pos = stream.ReadUInt16();        // 04
            byte type     = stream.ReadUInt8();         // 06

            if (type != 1 && type != 2)
            {
                return(null);
            }
            var info = new YgpMetaData {
                Type = type, BPP = 32
            };

            info.Flags = stream.ReadUInt8();            // 07
            int header_size = stream.ReadInt32();       // 08

            stream.Position = header_size;
            info.DataSize   = stream.ReadInt32();       // XX+00
            info.Width      = stream.ReadUInt16();      // XX+04
            info.Height     = stream.ReadUInt16();      // XX+06
            info.DataOffset = header_size + 8;
            if (0 != (info.Flags & 4))
            {
                stream.Position = 0x14;
                info.OffsetX    = stream.ReadInt16();
                info.OffsetY    = stream.ReadInt16();
            }
            return(info);
        }
Пример #3
0
        public override ImageData Read(IBinaryStream stream, ImageMetaData info)
        {
            var pixels = new byte[info.Width * info.Height * 4];

            stream.Position = 0x18;
            int dst = 0;

            for (uint y = 0; y < info.Height; ++y)
            {
                while (dst < pixels.Length)
                {
                    byte a = stream.ReadUInt8();
                    if (0 == a)
                    {
                        byte count = stream.ReadUInt8();
                        if (0 == count)
                        {
                            break;
                        }
                        dst += count * 4;
                    }
                    else
                    {
                        stream.Read(pixels, dst, 3);
                        pixels[dst + 3] = (byte)a;
                        dst            += 4;
                    }
                }
            }
            return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels));
        }
Пример #4
0
 void ReadRleLine(int dst)
 {
     while (dst < m_output.Length)
     {
         int ctl = m_input.ReadUInt8();
         if (ctl != 0)
         {
             int  count = ctl;
             byte v     = m_input.ReadUInt8();
             while (count-- > 0)
             {
                 m_output[dst] = v;
                 dst          += 3;
             }
         }
         else
         {
             int count = m_input.ReadUInt8();
             if (0 == count)
             {
                 break;
             }
             while (count-- > 0)
             {
                 m_output[dst] = m_input.ReadUInt8();
                 dst          += 3;
             }
         }
     }
 }
Пример #5
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            file.Position = 0x1C;
            var palette = ReadPalette(file.AsStream);

            if (info.BPP != 32)
            {
                var pixels = new byte[info.iWidth * info.iHeight];
                file.Read(pixels, 0, pixels.Length);
                return(ImageData.CreateFlipped(info, PixelFormats.Indexed8, palette, pixels, info.iWidth));
            }
            else
            {
                int stride   = info.iWidth * 4;
                var pixels   = new byte[stride * info.iHeight];
                var colormap = palette.Colors;
                for (int dst = 0; dst < pixels.Length; dst += 4)
                {
                    byte c = file.ReadUInt8();
                    pixels[dst]     = colormap[c].B;
                    pixels[dst + 1] = colormap[c].G;
                    pixels[dst + 2] = colormap[c].R;
                    pixels[dst + 3] = file.ReadUInt8();
                }
                return(ImageData.CreateFlipped(info, PixelFormats.Bgra32, null, pixels, stride));
            }
        }
Пример #6
0
        byte[] UnpackStream24(IBinaryStream input, byte[] output, int total_length)
        {
            int dst = 0;

            while (dst < total_length)
            {
                int v = input.ReadUInt8();
                if (0 == v)
                {
                    int count = input.ReadUInt8();
                    if (0 == count)
                    {
                        continue;
                    }
                    dst += count;
                }
                else if (0xff == v)
                {
                    int count = input.ReadUInt8();
                    if (0 == count)
                    {
                        continue;
                    }
                    count = Math.Min(count, total_length - dst);
                    input.Read(output, dst, count);
                    dst += count;
                }
                else
                {
                    output[dst++] = input.ReadUInt8();
                }
            }
            return(output);
        }
Пример #7
0
        public byte[] Unpack()
        {
            long next_pos = m_info.DataOffset;
            int  height = (int)m_info.Height;
            int  dst = 0;
            byte b = 0, g = 0, r = 0;

            for (int y = 0; y < height; ++y)
            {
                int start_pos = dst;
                if (0 == (y % m_info.BlockSize))
                {
                    m_input.Position = next_pos;
                    next_pos        += m_input.ReadUInt32();
                    b = m_input.ReadUInt8();
                    g = m_input.ReadUInt8();
                    r = m_input.ReadUInt8();
                    m_input.ReadUInt8();
                    mask1 = mask2 = mask3 = mask4 = mask5 = 1;
                }
                for (uint x = 0; x < m_info.Width; ++x)
                {
                    b = GetNextByte(b);
                    g = GetNextByte(g);
                    r = GetNextByte(r);
                    m_output[dst++] = b;
                    m_output[dst++] = g;
                    m_output[dst++] = r;
                }
                b = m_output[start_pos];
                g = m_output[start_pos + 1];
                r = m_output[start_pos + 2];
            }
            return(m_output);
        }
Пример #8
0
        byte[] Unpack8()
        {
            var output = new byte[m_info.Width * m_info.Height];
            int dst    = 0;
            int bits   = 0;
            int mask   = 0;

            while (dst < output.Length)
            {
                mask >>= 1;
                if (0 == mask)
                {
                    bits = m_input.ReadUInt8();
                    mask = 0x80;
                }
                if (0 != (bits & mask))
                {
                    output[dst++] = m_input.ReadUInt8();
                }
                else
                {
                    int count  = 2 + m_input.ReadUInt8();
                    int offset = 1 + m_input.ReadUInt8();
                    Binary.CopyOverlapped(output, dst - offset, dst, count);
                    dst += count;
                }
            }
            return(output);
        }
Пример #9
0
 internal static void DecompressRle(IBinaryStream input, byte[] output, int step)
 {
     for (int i = 0; i < step; ++i)
     {
         sbyte ctl = input.ReadInt8();
         int   dst = i;
         while (ctl != 0)
         {
             if (ctl < 0)
             {
                 int count = -ctl;
                 while (count-- > 0)
                 {
                     output[dst] = input.ReadUInt8();
                     dst        += step;
                 }
             }
             else
             {
                 byte v     = input.ReadUInt8();
                 int  count = ctl;
                 while (count-- > 0)
                 {
                     output[dst] = v;
                     dst        += step;
                 }
             }
             ctl = input.ReadInt8();
         }
     }
 }
Пример #10
0
        ImageData ReadAlpha(IBinaryStream alp, ImageMetaData info, byte[] image)
        {
            var header = alp.ReadHeader(0x10);

            if (!header.AsciiEqual("ALP1"))
            {
                throw new InvalidFormatException();
            }
            int unpacked_size = header.ToInt32(8);
            var alpha         = new byte[unpacked_size];
            int dst           = 0;

            while (dst < alpha.Length)
            {
                byte a     = alp.ReadUInt8();
                int  count = alp.ReadUInt8();
                for (int i = 0; i < count; ++i)
                {
                    alpha[dst++] = a;
                }
            }
            int dst_stride = (int)info.Width * 4;
            var pixels     = new byte[dst_stride * (int)info.Height];
            int a_src      = 0;
            int src        = 0;

            for (dst = 0; dst < pixels.Length; dst += 4)
            {
                pixels[dst]     = image[src++];
                pixels[dst + 1] = image[src++];
                pixels[dst + 2] = image[src++];
                pixels[dst + 3] = alpha[a_src++];
            }
            return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels, dst_stride));
        }
Пример #11
0
        internal static void LzDecompress(IBinaryStream input, byte[] output)
        {
            int dst  = 0;
            int bits = 0;
            int mask = 0;

            while (dst < output.Length)
            {
                mask >>= 1;
                if (0 == mask)
                {
                    bits = input.ReadUInt8();
                    mask = 0x80;
                }
                if (0 != (bits & mask))
                {
                    output[dst++] = input.ReadUInt8();
                }
                else
                {
                    int offset = input.ReadUInt16();
                    int count  = (offset & 0xF) + 2;
                    offset >>= 4;
                    Binary.CopyOverlapped(output, dst - offset, dst, count);
                    dst += count;
                }
            }
        }
Пример #12
0
        bool UnpackAlpha(int length)
        {
            bool has_alpha = false;
            int  dst       = 3;

            while (length > 0)
            {
                int count = m_input.ReadInt16();
                length -= 2;
                if (count < 0)
                {
                    count = (count & 0x7FFF) + 1;
                    byte a = m_input.ReadUInt8();
                    has_alpha = has_alpha || a != 0;
                    length--;
                    for (int i = 0; i < count; ++i)
                    {
                        m_output[dst] = a;
                        dst          += 4;
                    }
                }
                else
                {
                    for (int i = 0; i < count; ++i)
                    {
                        byte a = m_input.ReadUInt8();
                        has_alpha     = has_alpha || a != 0;
                        m_output[dst] = a;
                        dst          += 4;
                    }
                    length -= count;
                }
            }
            return(has_alpha);
        }
Пример #13
0
        int GetInt()
        {
            byte a = m_input.ReadUInt8();

            if (a == m_scheme.Value3)
            {
                a = 0;
            }
            int d = 0;
            int c = 0;

            for (;;)
            {
                byte a1 = m_input.ReadUInt8();
                if (a1 == m_scheme.Value2)
                {
                    break;
                }
                if (a1 != m_scheme.Value1)
                {
                    c = (a1 == m_scheme.Value3) ? 0 : a1;
                }
                else
                {
                    ++d;
                }
            }
            return(a + (c + d * m_scheme.Value1) * m_scheme.Value1);
        }
Пример #14
0
        void UnpackRle()
        {
            int dst = 0;

            while (dst < m_output.Length)
            {
                int count = m_input.ReadByte();
                if (-1 == count)
                {
                    break;
                }
                if (0 != (count & 0x80))
                {
                    byte v = m_input.ReadUInt8();
                    count = (count & 0x7F) + 1;
                    while (count-- > 0)
                    {
                        m_output[dst++] = v;
                    }
                }
                else
                {
                    count++;
                    m_input.Read(m_output, dst, count);
                    dst += count;
                }
            }
        }
Пример #15
0
        void RestorePixels(IBinaryStream input, byte[] output, BgiMetaData info)
        {
            int bpp    = info.BPP / 8;
            int stride = (int)info.Width * bpp;

            for (int i = 0; i < bpp; ++i)
            {
                int  dst  = i;
                byte incr = 0;
                for (int h = (int)info.Height; h > 0; --h)
                {
                    for (uint w = 0; w < info.Width; ++w)
                    {
                        incr       += input.ReadUInt8();
                        output[dst] = incr;
                        dst        += bpp;
                    }
                    if (--h == 0)
                    {
                        break;
                    }
                    dst += stride;
                    int pos = dst;
                    for (uint w = 0; w < info.Width; ++w)
                    {
                        pos        -= bpp;
                        incr       += input.ReadUInt8();
                        output[pos] = incr;
                    }
                }
            }
        }
Пример #16
0
        void UnpackAlpha(int length)
        {
            int dst = 3;

            while (length > 0)
            {
                int count = m_input.ReadInt16();
                length -= 2;
                if (count < 0)
                {
                    count = (count & 0x7FFF) + 1;
                    byte a = (byte)~m_input.ReadUInt8();
                    length--;
                    for (int i = 0; i < count; ++i)
                    {
                        m_output[dst] = a;
                        dst          += 4;
                    }
                }
                else
                {
                    for (int i = 0; i < count; ++i)
                    {
                        m_output[dst] = (byte)~m_input.ReadUInt8();
                        dst          += 4;
                    }
                    length -= count;
                }
            }
        }
Пример #17
0
 private void Decode(IBinaryStream input, int count, Stream output)
 {
     using (var buffer = new BinaryWriter(output, Encoding.ASCII, true))
     {
         ushort sampleL = 0;
         ushort sampleR = 0;
         for (int i = 0; i < count; ++i)
         {
             byte v = input.ReadUInt8();
             if (0 != (v & 0x80))
             {
                 sampleL = (ushort)(v << 9);
             }
             else
             {
                 sampleL += (ushort)(MulValue * SampleTable[v]);
             }
             buffer.Write(sampleL);
             if (1 != Format.Channels)
             {
                 ++i;
                 v = input.ReadUInt8();
                 if (0 != (v & 0x80))
                 {
                     sampleR = (ushort)(v << 9);
                 }
                 else
                 {
                     sampleR += (ushort)(MulValue * SampleTable[v]);
                 }
                 buffer.Write(sampleR);
             }
         }
     }
 }
Пример #18
0
        void UnpackStream8(IBinaryStream input, byte[] output, byte[] alpha)
        {
            int alpha_dst = 0;
            int dst       = 0;

            while (dst < output.Length)
            {
                byte rle = input.ReadUInt8();
                if (0 == rle)
                {
                    int skip = input.ReadUInt8();
                    dst       += skip;
                    alpha_dst += skip;
                }
                else if (rle != 0xFF)
                {
                    output[dst++]      = input.ReadUInt8();
                    alpha[alpha_dst++] = rle;
                }
                else
                {
                    int count = input.ReadUInt8();
                    input.Read(output, dst, count);
                    dst += count;
                    for (int i = 0; i < count; ++i)
                    {
                        alpha[alpha_dst++] = 0xFF;
                    }
                }
            }
        }
Пример #19
0
        void UnpackBlock_4_9(PxBlock block_info)
        {
            var  output    = NewBlock(block_info);
            int  dst       = 0;
            bool has_alpha = true;

            for (;;)
            {
                int code = m_input.ReadInt32();
                if (-1 == code)
                {
                    break;
                }
                if (0 != (code & 0x180000))
                {
                    has_alpha = !has_alpha;
                }
                dst += (code & 0x1FF) * MaxBlockSize;
                dst += code >> 21;

                int count = (code >> 9) & 0x3FF;
                for (int i = 0; i < count; ++i)
                {
                    byte alpha     = (byte)(has_alpha ? (m_input.ReadUInt8() << 1) - 1 : 0xFF);
                    int  color_idx = m_input.ReadByte();
                    if (dst < output.Length)
                    {
                        var color = Palette.Colors[color_idx];
                        output[dst] = (uint)(color.B | color.G << 8 | color.R << 16 | alpha << 24);
                    }
                    dst++;
                }
            }
            PutBlock(block_info);
        }
Пример #20
0
        DelphiObject DeserializeNode()
        {
            int type_len = m_input.ReadByte();

            if (type_len <= 0)
            {
                return(null);
            }
            var node = new DelphiObject();

            node.Type = ReadString(type_len);
            node.Name = ReadString();
            int key_length;

            while ((key_length = m_input.ReadUInt8()) > 0)
            {
                var key = ReadString(key_length);
                node.Props[key] = ReadValue();
            }
            DelphiObject child;

            while ((child = DeserializeNode()) != null)
            {
                node.Contents.Add(child);
            }
            return(node);
        }
Пример #21
0
 protected override IEnumerator <int> Unpack()
 {
     for (int i = 0; i < Chunks; ++i)
     {
         int ctl = m_input.ReadByte();
         if (-1 == ctl)
         {
             yield break;
         }
         int count;
         if (ctl <= 1)
         {
             if (0 == ctl)
             {
                 count = m_input.ReadUInt8();
             }
             else
             {
                 count = 0x100;
             }
             while (count > 0)
             {
                 int avail = Math.Min(count, m_length);
                 int read  = m_input.Read(m_buffer, m_pos, avail);
                 if (0 == read)
                 {
                     yield break;
                 }
                 count    -= read;
                 m_pos    += read;
                 m_length -= read;
                 if (0 == m_length)
                 {
                     yield return(m_pos);
                 }
             }
         }
         else
         {
             if (3 == ctl)
             {
                 count = m_input.ReadUInt16();
             }
             else
             {
                 count = ctl;
             }
             byte v = m_input.ReadUInt8();
             while (count-- > 0)
             {
                 m_buffer[m_pos++] = v;
                 if (0 == --m_length)
                 {
                     yield return(m_pos);
                 }
             }
         }
     }
 }
Пример #22
0
        byte[] UnpackCfp0(IBinaryStream input)
        {
            input.Position = 8;
            int unpacked_size = input.ReadInt32();
            var output        = new byte[unpacked_size];
            int dst           = 0;

            while (dst < output.Length)
            {
                int cmd   = input.ReadByte();
                int count = 0;
                switch (cmd)
                {
                case 0:
                    count = input.ReadUInt8();
                    input.Read(output, dst, count);
                    break;

                case 1:
                    count = input.ReadInt32();
                    input.Read(output, dst, count);
                    break;

                case 2:
                {
                    count = input.ReadUInt8();
                    byte v = input.ReadUInt8();
                    for (int i = 0; i < count; ++i)
                    {
                        output[dst + i] = v;
                    }
                    break;
                }

                case 3:
                {
                    count = input.ReadInt32();
                    byte v = input.ReadUInt8();
                    for (int i = 0; i < count; ++i)
                    {
                        output[dst + i] = v;
                    }
                    break;
                }

                case 6:
                    int offset = input.ReadUInt16();
                    count = input.ReadUInt16();
                    Binary.CopyOverlapped(output, dst - offset, dst, count);
                    break;

                case 15:
                case -1:
                    return(output);
                }
                dst += count;
            }
            return(output);
        }
Пример #23
0
            public void Unpack()
            {
                int dst = 0;

                while (dst < m_output.Length)
                {
                    int code = m_input.ReadUInt8();
                    switch (code)
                    {
                    case 0:
                    {
                        int count = m_input.ReadUInt8();
                        if (dst + count > m_output.Length)
                        {
                            count = m_output.Length - dst;
                        }
                        m_input.Read(m_output, dst, count);
                        dst += count;
                        break;
                    }

                    case 1:
                    {
                        int count = m_input.ReadUInt8() * m_chunk_size;
                        if (dst + count > m_output.Length)
                        {
                            count = m_output.Length - dst;
                        }
                        m_input.Read(m_output, dst, count);
                        dst += count;
                        break;
                    }

                    default:
                    {
                        if (dst + m_chunk_size > m_output.Length)
                        {
                            return;
                        }
                        m_input.Read(m_output, dst, m_chunk_size);
                        int src = dst;
                        dst += m_chunk_size;
                        for (int i = 1; i < code; ++i)
                        {
                            if (dst + m_chunk_size > m_output.Length)
                            {
                                return;
                            }
                            System.Buffer.BlockCopy(m_output, src, m_output, dst, m_chunk_size);
                            dst += m_chunk_size;
                        }
                        break;
                    }
                    }
                }
            }
Пример #24
0
        void Decompress(IBinaryStream input, byte[] output)
        {
            int  bit_mask = 0;
            int  dst      = 0;
            byte ctl_bits = 0;

            while (dst < output.Length)
            {
                bit_mask >>= 1;
                if (0 == bit_mask)
                {
                    ctl_bits = input.ReadUInt8();
                    bit_mask = 0x80;
                }
                if (0 != (bit_mask & ctl_bits))
                {
                    output[dst++] = input.ReadUInt8();
                }
                else
                {
                    int next   = input.ReadUInt16();
                    int offset = next >> 4;
                    int count  = next & 0xF;
                    if (0 == offset)
                    {
                        if (0xF == count)
                        {
                            count = input.ReadUInt8() + 0x1F;
                        }
                        else
                        {
                            count += 0x10;
                        }

                        count = Math.Min(count, output.Length - dst);
                        input.Read(output, dst, count);
                    }
                    else
                    {
                        if (0xF == count)
                        {
                            count = input.ReadUInt8() + 0x12;
                        }
                        else
                        {
                            count += 3;
                        }

                        count = Math.Min(count, output.Length - dst);
                        Binary.CopyOverlapped(output, dst - offset, dst, count);
                    }
                    dst += count;
                }
            }
        }
Пример #25
0
        private void Decode3(IBinaryStream input, Stream output, int step)
        {
            input.ReadInt32(); // unpacked_size
            int count = input.ReadInt32();

            using (var buffer = new BinaryWriter(output, Encoding.ASCII, true))
            {
                short sample = input.ReadInt16();
                buffer.Write(sample);
                for (int i = 0; i < count; ++i)
                {
                    if (count - 300 == i)
                    {
                        sample = 0;
                    }
                    ushort v = input.ReadUInt8();
                    if (0 != (v & 1))
                    {
                        ushort v14 = (ushort)((v >> 1) & 0x7F);
                        if (0 != (v14 & 0x40))
                        {
                            sample = (short)(v14 << 10);
                        }
                        else
                        {
                            sample += (short)SampleTable2[v14];
                        }
                        buffer.Write(sample);
                        if (step != 0)
                        {
                            buffer.BaseStream.Seek(step, SeekOrigin.Current);
                        }
                    }
                    else
                    {
                        v |= (ushort)(input.ReadUInt8() << 8);
                        int    repeat = SizeTable[(v >> 1) & 7];
                        short  end    = (short)(v & 0xFFF0);
                        double inc    = (end - sample) / (double)repeat;
                        double v8     = sample;
                        for (int j = 0; j < repeat; ++j)
                        {
                            v8 += inc;
                            buffer.Write((short)v8);
                            if (step != 0)
                            {
                                buffer.BaseStream.Seek(step, SeekOrigin.Current);
                            }
                        }
                        sample = end;
                    }
                }
            }
        }
Пример #26
0
        byte[] UnpackStream32(IBinaryStream input, byte[] output, int total_length)
        {
            int dst       = 0;
            int component = 0;

            while (dst < total_length)
            {
                byte v = input.ReadUInt8();
                if (0 == v)
                {
                    int count = input.ReadUInt8();
                    if (0 == count)
                    {
                        continue;
                    }
                    for (int i = 0; i < count; ++i)
                    {
                        ++dst;
                        if (++component == 3)
                        {
                            ++dst;
                            component = 0;
                        }
                    }
                }
                else if (0xff == v)
                {
                    int count = input.ReadUInt8();
                    if (0 == count)
                    {
                        continue;
                    }
                    for (int i = 0; i < count && dst < total_length; ++i)
                    {
                        output[dst++] = input.ReadUInt8();
                        if (++component == 3)
                        {
                            output[dst++] = 0xff;
                            component     = 0;
                        }
                    }
                }
                else
                {
                    output[dst++] = input.ReadUInt8();
                    if (++component == 3)
                    {
                        output[dst++] = v;
                        component     = 0;
                    }
                }
            }
            return(output);
        }
Пример #27
0
        public byte[] Unpack()
        {
            int chmask = Format.Channels - 1;

            m_input.Position = 0x12;
            var sample = new short[Format.Channels];
            int chn    = 0;
            int dst    = 0;

            for (int i = 0; i < m_chunk_count; ++i)
            {
                int  count;
                byte ctl = m_input.ReadUInt8();
                if (ctl < 2)
                {
                    if (0 == ctl)
                    {
                        count = m_input.ReadUInt8();
                    }
                    else
                    {
                        count = 256;
                    }
                    while (count-- > 0)
                    {
                        byte s = m_input.ReadUInt8();
                        int  n = chn++ & chmask;
                        sample[n] += PcmTable[s];
                        LittleEndian.Pack(sample[n], m_output, dst);
                        dst += 2;
                    }
                }
                else
                {
                    if (3 == ctl)
                    {
                        count = m_input.ReadUInt16();
                    }
                    else
                    {
                        count = ctl;
                    }
                    byte s = m_input.ReadUInt8();
                    while (count-- > 0)
                    {
                        int n = chn++ & chmask;
                        sample[n] += PcmTable[s];
                        LittleEndian.Pack(sample[n], m_output, dst);
                        dst += 2;
                    }
                }
            }
            return(m_output);
        }
Пример #28
0
        byte[] UnpackLzss(IBinaryStream input, uint unpacked_size)
        {
            var output     = new byte[unpacked_size];
            var frame      = new byte[0x100];
            int frame_pos  = 0xEF;
            int dst        = 0;
            int ctl        = 0;
            int bit        = 0;
            int prev_count = -1;

            while (dst < output.Length)
            {
                bit >>= 1;
                if (0 == bit)
                {
                    ctl = input.ReadByte();
                    if (-1 == ctl)
                    {
                        break;
                    }
                    bit = 0x80;
                }
                if (0 != (ctl & bit))
                {
                    byte v = input.ReadUInt8();
                    frame[frame_pos++ & 0xFF] = v;
                    output[dst++]             = v;
                }
                else
                {
                    int offset = input.ReadUInt8();
                    int count;
                    if (-1 == prev_count)
                    {
                        prev_count = input.ReadUInt8();
                        count      = prev_count & 0xF;
                    }
                    else
                    {
                        count      = prev_count >> 4;
                        prev_count = -1;
                    }
                    count += 2;
                    while (count-- > 0 && dst < output.Length)
                    {
                        byte v = frame[offset++ & 0xFF];
                        frame[frame_pos++ & 0xFF] = v;
                        output[dst++]             = v;
                    }
                }
            }
            return(output);
        }
Пример #29
0
        void ReadV1(int size)
        {
            int row_count = 0;

            while (size > 0)
            {
                int chunk_size = Binary.BigEndian(m_input.ReadUInt16());
                if (row_count < m_height)
                {
                    m_rows_sizes[row_count++] = chunk_size;
                }
                size -= chunk_size + 2;
            }
            if (size < 0)
            {
                throw new InvalidFormatException();
            }
            int dst = 0;

            for (int y = 0; y < row_count; ++y)
            {
                int width      = m_width;
                int chunk_size = m_rows_sizes[y];
                while (chunk_size-- > 0)
                {
                    byte rle = m_input.ReadUInt8();
                    if (rle < 0x81)
                    {
                        int count = rle + 1;
                        width      -= count;
                        chunk_size -= count;
                        m_input.Read(m_channel, dst, count);
                        dst += count;
                    }
                    else
                    {
                        int count = 0x101 - rle;
                        width -= count;
                        --chunk_size;
                        byte v = m_input.ReadUInt8();
                        while (count-- > 0)
                        {
                            m_channel[dst++] = v;
                        }
                    }
                }
                while (width-- > 0)
                {
                    m_channel[dst++] = 0;
                }
            }
        }
Пример #30
0
        void Unpack8bpp()
        {
            Format   = PixelFormats.Indexed8;
            m_output = new byte[m_height * m_width];

            int  packed_size = m_input.ReadInt32();
            byte index       = 0;
            byte ctl         = m_input.ReadUInt8();

            if (0xF5 == ctl)
            {
                index = m_input.ReadUInt8();
            }
            var input = m_input.ReadBytes(packed_size);

            if (input.Length != packed_size)
            {
                throw new InvalidFormatException();
            }
            int src = 0;
            int dst = 0;

            while (src < packed_size)
            {
                if (input[src] != ctl)
                {
                    m_output[dst++] = input[src++];
                }
                else if (ctl != 0xF5)
                {
                    int count = input[src + 1];
                    for (int i = 0; i < count; ++i)
                    {
                        m_output[dst++] = input[src - 1];
                    }
                    src += 2;
                }
                else if (input[src + 1] == index)
                {
                    int count = input[src + 2];
                    for (int i = 0; i < count; ++i)
                    {
                        m_output[dst++] = input[src - 1];
                    }
                    src += 3;
                }
                else
                {
                    m_output[dst++] = input[src++];
                }
            }
        }