Exemplo n.º 1
0
        public byte[] Decode(IBinaryStream input)
        {
            m_dst = 0;
            if (1 == Channels)
            {
                var adp = new AdpDecoder();
                while (input.PeekByte() != -1)
                {
                    short sample = input.ReadInt16();
                    PutSample(sample);
                    int quant_idx = input.ReadUInt16() & 0xFF;
                    adp.Reset(sample, quant_idx);

                    for (int j = 0; j < BytesPerChunk - 4; ++j)
                    {
                        byte octet = input.ReadUInt8();
                        PutSample(adp.DecodeSample(octet));
                        PutSample(adp.DecodeSample(octet >> 4));
                    }
                }
            }
            else
            {
                var first             = new AdpDecoder();
                var second            = new AdpDecoder();
                int samples_per_chunk = (BytesPerChunk - 8) / 8;
                while (input.PeekByte() != -1)
                {
                    short sample = input.ReadInt16();
                    PutSample(sample);
                    int quant_idx = input.ReadUInt16() & 0xFF;
                    first.Reset(sample, quant_idx);

                    sample = input.ReadInt16();
                    PutSample(sample);
                    quant_idx = input.ReadUInt16() & 0xFF;
                    second.Reset(sample, quant_idx);

                    for (int j = 0; j < samples_per_chunk; ++j)
                    {
                        uint first_code  = input.ReadUInt32();
                        uint second_code = input.ReadUInt32();
                        for (int i = 0; i < 8; ++i)
                        {
                            PutSample(first.DecodeSample((byte)first_code));
                            PutSample(second.DecodeSample((byte)second_code));
                            first_code  >>= 4;
                            second_code >>= 4;
                        }
                    }
                }
            }
            return(m_output);
        }
Exemplo n.º 2
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            file.Position = 4;
            var palette = ReadPalette(file.AsStream, 0x100, PaletteFormat.Bgr);
            int stride  = (info.iWidth + 3) & ~3;
            var pixels  = new byte[info.iHeight * stride];
            int length  = info.iHeight * info.iWidth;
            int dst     = 0;

            while (dst < length)
            {
                byte px   = file.ReadUInt8();
                int  next = file.PeekByte();
                if (px == next)
                {
                    file.ReadByte();
                    int count = file.ReadByte() + 1;
                    while (count-- > 0)
                    {
                        pixels[dst++] = px;
                    }
                }
                else
                {
                    pixels[dst++] = px;
                }
            }
            return(ImageData.CreateFlipped(info, PixelFormats.Indexed8, palette, pixels, stride));
        }
Exemplo n.º 3
0
        bool ConvertToMp3(IBinaryStream input, Stream mp3, ushort schema)
        {
            byte[] frame_buffer = null;
            using (var output = new BinaryWriter(mp3, System.Text.Encoding.Default, true))
            {
                while (input.PeekByte() != -1)
                {
                    ushort rha_header = Binary.BigEndian(input.ReadUInt16());
                    uint   header;
                    int    add_len   = 0;
                    byte   add_value = 0;
                    if (0 == schema)
                    {
                        header = 0xFFFB0000u | rha_header;
                    }
                    else
                    {
                        if (0 != (rha_header & 0x1000)) // RHAF_LASTZEROADD
                        {
                            add_len   = input.ReadUInt16();
                            add_value = 0;
                        }
                        else if (0 != (rha_header & 0x2000)) // RHAF_LASTFULLADD
                        {
                            add_len   = input.ReadUInt16();
                            add_value = 0xFF;
                        }
                        header = RhaToMp3Header(rha_header);
                    }
                    int frame_length = GetFrameLength(header);
                    if (0 == frame_length || add_len > frame_length)
                    {
                        return(false);
                    }
                    if (null == frame_buffer || frame_length > frame_buffer.Length)
                    {
                        frame_buffer = new byte[frame_length];
                    }

                    int read_length = frame_length - add_len;
                    if (read_length != input.Read(frame_buffer, 0, read_length))
                    {
                        break;
                    }
                    for (int i = 0; i < add_len; ++i)
                    {
                        frame_buffer[read_length + i] = add_value;
                    }

                    output.Write(Binary.BigEndian(header));
                    output.Write(frame_buffer, 0, frame_length);

                    if (0 == (header & (1 << 16))) // CRC bit
                    {
                        output.Write(input.ReadUInt16());
                    }
                }
            }
            return(mp3.Length > 0);
        }
Exemplo n.º 4
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            var pixels = new short[info.Width * info.Height];

            file.Position = 0x20;
            int dst = 0;

            while (dst < pixels.Length && file.PeekByte() != -1)
            {
                short px = file.ReadInt16();
                if (px < 0)
                {
                    px &= 0x7FFF;
                    int count = file.ReadUInt8();
                    for (int i = 0; i < count; ++i)
                    {
                        pixels[dst++] = px;
                    }
                }
                else
                {
                    pixels[dst++] = px;
                }
            }
            return(ImageData.Create(info, PixelFormats.Bgr555, null, pixels));
        }
Exemplo n.º 5
0
 public override ImageMetaData ReadMetaData(IBinaryStream file)
 {
     if (0xFF != file.ReadByte() || 0xD8 != file.ReadByte())
     {
         return(null);
     }
     while (-1 != file.PeekByte())
     {
         ushort marker = Binary.BigEndian(file.ReadUInt16());
         if ((marker & 0xff00) != 0xff00)
         {
             break;
         }
         int length = Binary.BigEndian(file.ReadUInt16());
         if ((marker & 0x00f0) == 0xc0 && marker != 0xffc4)
         {
             if (length < 8)
             {
                 break;
             }
             int  bits       = file.ReadByte();
             uint height     = Binary.BigEndian(file.ReadUInt16());
             uint width      = Binary.BigEndian(file.ReadUInt16());
             int  components = file.ReadByte();
             return(new ImageMetaData {
                 Width = width,
                 Height = height,
                 BPP = bits * components,
             });
         }
         file.Seek(length - 2, SeekOrigin.Current);
     }
     return(null);
 }
Exemplo n.º 6
0
        List <Entry> ReadIndexV1(IBinaryStream file)
        {
            var max_offset = file.Length;

            file.Position = 8;
            var key_gen = new KeyGenerator(0xDEADCAFE);
            var dir     = new List <Entry>();

            while (file.PeekByte() != -1)
            {
                uint name_length = file.ReadUInt32() ^ key_gen.GetNext();
                var  name_bytes  = file.ReadBytes((int)name_length);
                var  name        = DecryptName(name_bytes, key_gen);
                var  entry       = FormatCatalog.Instance.Create <RgssEntry> (name);
                entry.Size   = file.ReadUInt32() ^ key_gen.GetNext();
                entry.Offset = file.Position;
                entry.Key    = key_gen.Current;
                if (!entry.CheckPlacement(max_offset))
                {
                    return(null);
                }
                dir.Add(entry);
                file.Seek(entry.Size, SeekOrigin.Current);
            }
            return(dir);
        }
Exemplo n.º 7
0
        List <Entry> ReadIndexV3(IBinaryStream file)
        {
            var max_offset = file.Length;

            file.Position = 8;
            uint key = file.ReadUInt32() * 9 + 3;
            var  dir = new List <Entry>();

            while (file.PeekByte() != -1)
            {
                uint offset = file.ReadUInt32() ^ key;
                if (0 == offset)
                {
                    break;
                }
                uint size        = file.ReadUInt32() ^ key;
                uint entry_key   = file.ReadUInt32() ^ key;
                uint name_length = file.ReadUInt32() ^ key;
                var  name_bytes  = file.ReadBytes((int)name_length);
                var  name        = DecryptName(name_bytes, key);
                var  entry       = FormatCatalog.Instance.Create <RgssEntry> (name);
                entry.Offset = offset;
                entry.Size   = size;
                entry.Key    = entry_key;
                if (!entry.CheckPlacement(max_offset))
                {
                    return(null);
                }
                dir.Add(entry);
            }
            return(dir);
        }
Exemplo n.º 8
0
 bool UnpackSVariant(byte[] output, int dst, int channels, GrpVariant method)
 {
     if (GrpVariant.Default == method)
     {
         UnpackSv2(output, dst, channels);
     }
     else
     {
         UnpackSv1(output, dst, channels);
     }
     return(m_input.PeekByte() == -1); // rather loose test, but whatever
 }
Exemplo n.º 9
0
        void Unpack8BPP(IBinaryStream input, byte[] output)
        {
            int dst = 0;

            while (dst < output.Length && input.PeekByte() != -1)
            {
                byte v     = input.ReadUInt8();
                int  count = input.ReadByte();
                for (int i = 0; i < count; ++i)
                {
                    output[dst++] = v;
                }
            }
        }
Exemplo n.º 10
0
        public override ImageMetaData ReadMetaData(IBinaryStream stream)
        {
            int first = stream.PeekByte() ^ 0x21;

            if (first != 0x78) // doesn't look like zlib stream
            {
                return(null);
            }

            using (var input = new XoredStream(stream.AsStream, 0x21, true))
                using (var zstream = new ZLibStream(input, CompressionMode.Decompress))
                    using (var bmp = new BinaryStream(zstream, stream.Name))
                        return(Bmp.ReadMetaData(bmp));
        }
Exemplo n.º 11
0
        void Unpack24BPP(IBinaryStream input, byte[] output)
        {
            int dst = 0;

            while (dst < output.Length && input.PeekByte() != -1)
            {
                input.Read(output, dst, 3);
                int count = input.ReadByte();
                if (count > 0)
                {
                    count *= 3;
                    Binary.CopyOverlapped(output, dst, dst + 3, count - 3);
                    dst += count;
                }
            }
        }
Exemplo n.º 12
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            file.Position = 0xC;
            var palette = ReadColorMap(file.AsStream, 256, PaletteFormat.Bgr);
            int stride  = (int)info.Width * 3;
            var pixels  = new byte[stride * (int)info.Height];
            int dst     = 0;

            while (dst < pixels.Length)
            {
                int ctl = file.PeekByte();
                if (-1 == ctl)
                {
                    break;
                }
                if (1 == ctl)
                {
                    file.ReadByte();
                    int idx   = file.ReadByte();
                    var color = palette[idx];
                    pixels[dst]     = color.B;
                    pixels[dst + 1] = color.G;
                    pixels[dst + 2] = color.R;
                    dst            += 3;
                }
                else if (2 == ctl)
                {
                    file.ReadByte();
                    int idx   = file.ReadByte();
                    int count = file.ReadByte();
                    var color = palette[idx];
                    for (int i = 0; i < count; ++i)
                    {
                        pixels[dst]     = color.B;
                        pixels[dst + 1] = color.G;
                        pixels[dst + 2] = color.R;
                        dst            += 3;
                    }
                }
                else
                {
                    file.Read(pixels, dst, 3);
                    dst += 3;
                }
            }
            return(ImageData.CreateFlipped(info, PixelFormats.Bgr24, null, pixels, stride));
        }
Exemplo n.º 13
0
        void Unpack(IBinaryStream input, Stream output)
        {
            var buffer = new byte[16];
            int ctl;

            while ((ctl = input.ReadByte()) != -1)
            {
                if (0 == ctl)
                {
                    input.Read(buffer, 0, 16);
                    output.Write(buffer, 0, 16);
                }
                else if (1 == ctl)
                {
                    for (int i = 0; i < 16; i += 2)
                    {
                        buffer[i]     = input.ReadUInt8();
                        buffer[i + 1] = 0;
                    }
                    output.Write(buffer, 0, 16);
                }
                else if (8 == ctl)
                {
                    for (int i = 0; i < 16; i += 2)
                    {
                        buffer[i]     = input.ReadUInt8();
                        buffer[i + 1] = 0xFF;
                    }
                    output.Write(buffer, 0, 16);
                }
                else if (15 == ctl)
                {
                    int count = input.ReadUInt8();
                    if (count > buffer.Length)
                    {
                        buffer = new byte[count];
                    }
                    input.Read(buffer, 0, count);
                    output.Write(buffer, 0, count);
                }
                else if (input.PeekByte() != -1)
                {
                    throw new InvalidFormatException();
                }
            }
        }
Exemplo n.º 14
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            var meta = (RadMetaData)info;

            file.Position = 4;
            int stride = info.iWidth * 3;
            var pixels = new byte[stride * info.iHeight];

            if (meta.IsCompressed)
            {
                UnpackRgb(file, pixels);
            }
            else
            {
                file.Read(pixels, 0, pixels.Length);
            }
            if (file.PeekByte() == -1)
            {
                return(ImageData.CreateFlipped(info, PixelFormats.Bgr24, null, pixels, stride));
            }

            var alpha = new byte[info.iWidth * info.iHeight];

            if (meta.IsCompressed)
            {
                UnpackAlpha(file, alpha);
            }
            else
            {
                file.Read(alpha, 0, alpha.Length);
            }

            stride = info.iWidth * 4;
            var output = new byte[stride * info.iHeight];
            int src    = 0;
            int asrc   = 0;

            for (int dst = 0; dst < output.Length; dst += 4)
            {
                output[dst]     = pixels[src++];
                output[dst + 1] = pixels[src++];
                output[dst + 2] = pixels[src++];
                output[dst + 3] = alpha[asrc++];
            }
            return(ImageData.CreateFlipped(info, PixelFormats.Bgra32, null, output, stride));
        }
Exemplo n.º 15
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            ImageData base_img;

            using (var input = new StreamRegion(file.AsStream, 0x24, true))
                using (var bmp = new BinaryStream(input, file.Name))
                {
                    var bmp_info = Bmp.ReadMetaData(bmp) as BmpMetaData;
                    if (null == bmp_info)
                    {
                        throw new InvalidFormatException();
                    }
                    bmp.Position  = 0;
                    base_img      = Bmp.Read(bmp, bmp_info);
                    file.Position = 0x24 + bmp_info.ImageLength;
                    if (file.PeekByte() == -1)
                    {
                        return(base_img);
                    }
                }
            using (var input = new StreamRegion(file.AsStream, file.Position, true))
                using (var bmp = new BinaryStream(input, file.Name))
                {
                    var bmp_info = Bmp.ReadMetaData(bmp) as BmpMetaData;
                    if (null == bmp_info)
                    {
                        return(base_img);
                    }
                    bmp.Position = 0;
                    var alpha_img = Bmp.Read(bmp, bmp_info);
                    var alpha_bmp = new FormatConvertedBitmap(alpha_img.Bitmap, PixelFormats.Gray8, null, 0);
                    var alpha     = new byte[alpha_bmp.PixelWidth * alpha_bmp.PixelHeight];
                    alpha_bmp.CopyPixels(alpha, alpha_bmp.PixelWidth, 0);

                    var base_bmp = new FormatConvertedBitmap(base_img.Bitmap, PixelFormats.Bgr32, null, 0);
                    int stride   = base_bmp.PixelWidth * 4;
                    var pixels   = new byte[stride * base_bmp.PixelHeight];
                    base_bmp.CopyPixels(pixels, stride, 0);
                    int asrc = 0;
                    for (int dst = 3; dst < pixels.Length; dst += 4)
                    {
                        pixels[dst] = alpha[asrc++];
                    }
                    return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels, stride));
                }
        }
Exemplo n.º 16
0
        IEnumerator <int> ReadSeq()
        {
            var stack = new byte[256];

            while (m_input.PeekByte() != -1)
            {
                int packed_size = UnpackDict();
                int src         = 0;
                int stack_pos   = 0;
                for (;;)
                {
                    byte s;
                    if (stack_pos > 0)
                    {
                        s = stack[--stack_pos];
                    }
                    else if (src < packed_size)
                    {
                        s = m_input.ReadUInt8();
                        src++;
                    }
                    else
                    {
                        break;
                    }
                    if (m_dict[s, 0] == s)
                    {
                        while (m_output_pos == m_output_end)
                        {
                            yield return(m_output_pos);
                        }
                        m_output[m_output_pos++] = s;
                    }
                    else
                    {
                        stack[stack_pos++] = m_dict[s, 1];
                        stack[stack_pos++] = m_dict[s, 0];
                    }
                }
            }
            yield return(m_output_pos);
        }
Exemplo n.º 17
0
        void UnpackRle(IBinaryStream input, byte[] output)
        {
            var  header      = input.ReadBytes(0x18);
            byte count_limit = header[5];
            int  dst         = 0;

            while (dst < output.Length)
            {
                int v = input.ReadByte();
                if (-1 == v)
                {
                    break;
                }
                int count = 1;
                while (count < count_limit)
                {
                    if (input.PeekByte() != v)
                    {
                        break;
                    }
                    ++count;
                    input.ReadByte();
                }
                if (count == count_limit)
                {
                    byte ctl = input.ReadUInt8();
                    if (ctl > 0x7F)
                    {
                        byte lo = input.ReadUInt8();
                        count += lo + ((ctl & 0x7F) << 8) + 128;
                    }
                    else
                    {
                        count += ctl;
                    }
                }
                for (int i = 0; i < count; ++i)
                {
                    output[dst++] = (byte)v;
                }
            }
        }
Exemplo n.º 18
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            file.Position = 4;
            var pixels = new uint[info.Width * info.Height];
            int dst    = 0;

            while (dst < pixels.Length && file.PeekByte() != -1)
            {
                uint px = file.ReadUInt32();
                if (px != 0)
                {
                    pixels[dst++] = px;
                }
                else
                {
                    dst += file.ReadUInt8();
                }
            }
            return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels));
        }
Exemplo n.º 19
0
 bool ConvertToMp3(IBinaryStream input, Stream mp3, ushort schema)
 {
     byte[] frame_buffer = null;
     using (var output = new BinaryWriter(mp3, System.Text.Encoding.Default, true))
     {
         while (input.PeekByte() != -1)
         {
             ushort rha_header = Binary.BigEndian(input.ReadUInt16());
             uint   header;
             if (0 == schema)
             {
                 header = 0xFFFB0000u | rha_header;
             }
             else
             {
                 if (0 != (rha_header & 0x3000))
                 {
                     input.ReadUInt16();
                 }
                 header = RhaToMp3Header(rha_header);
             }
             int frame_length = GetFrameLength(header);
             if (0 == frame_length)
             {
                 return(false);
             }
             if (null == frame_buffer || frame_length > frame_buffer.Length)
             {
                 frame_buffer = new byte[frame_length];
             }
             if (frame_length != input.Read(frame_buffer, 0, frame_length))
             {
                 break;
             }
             output.Write(Binary.BigEndian(header));
             output.Write(frame_buffer, 0, frame_length);
         }
     }
     return(mp3.Length > 0);
 }
Exemplo n.º 20
0
        void FillBuffer()
        {
            if (m_input.PeekByte() == -1)
            {
                m_eof = true;
                return;
            }
            int chunk_size = m_input.ReadInt32();
            int type       = m_input.ReadByte();

            if (type != 2)
            {
                m_eof = true;
                return;
            }
            if (chunk_size > m_buffer.Length)
            {
                m_buffer = new byte[chunk_size];
            }
            m_buffer_size = m_input.Read(m_buffer, 0, chunk_size);
            m_buffer_pos  = 0;
        }
Exemplo n.º 21
0
        void UnpackRle(byte[] output)
        {
            int dst = 0;

            while (m_input.PeekByte() != -1 && dst < output.Length)
            {
                byte ctl = m_input.ReadUInt8();
                if (ctl != 0xF0)
                {
                    output[dst++] = (byte)ctl;
                }
                else
                {
                    byte count = m_input.ReadUInt8();
                    if (0 == count)
                    {
                        output[dst++] = (byte)ctl;
                    }
                    else if (1 == count)
                    {
                        output[dst++] = 0xF0;
                    }
                    else if (2 == count)
                    {
                        output[dst++] = 0xF0;
                        output[dst++] = 0xF0;
                    }
                    else
                    {
                        byte v = m_input.ReadUInt8();
                        while (count-- > 0)
                        {
                            output[dst++] = v;
                        }
                    }
                }
            }
        }
Exemplo n.º 22
0
        void FillBuffer()
        {
            m_buffer_pos = m_buffer_size = 0;
            if (m_eof)
            {
                return;
            }
            if (m_input.PeekByte() == -1)
            {
                m_eof = true;
                BigEndian.Pack(0, m_buffer, 0);
                BigEndian.Pack(0x49454E44, m_buffer, 4);  // 'IEND'
                BigEndian.Pack(0xAE426082, m_buffer, 8);
                m_buffer_size = 12;
                return;
            }
            int chunk_size = m_input.ReadInt32();
            int type       = m_input.ReadByte();

            if (type != 2)
            {
                m_eof = true;
                return;
            }
            if (chunk_size + 12 > m_buffer.Length)
            {
                m_buffer = new byte[chunk_size + 12];
            }
            chunk_size = m_input.Read(m_buffer, 8, chunk_size);
            BigEndian.Pack(chunk_size, m_buffer, 0);
            BigEndian.Pack(0x49444154, m_buffer, 4);  // 'IDAT'
            uint checksum = Adler32.Compute(m_buffer, 8, chunk_size);

            BigEndian.Pack(checksum, m_buffer, 8 + chunk_size);
            m_buffer_size = 12 + chunk_size;
            return;
        }
Exemplo n.º 23
0
        void LL5Decompress(IBinaryStream input, Stream output)
        {
            var buffer = new byte[0x100];

            while (input.PeekByte() != -1)
            {
                int count = input.ReadInt8();
                if (count < 0)
                {
                    count = -count;
                    input.Read(buffer, 0, count);
                    output.Write(buffer, 0, count);
                }
                else
                {
                    byte v = input.ReadUInt8();
                    for (int i = 0; i < count; ++i)
                    {
                        buffer[i] = v;
                    }
                    output.Write(buffer, 0, count);
                }
            }
        }
Exemplo n.º 24
0
 public static long FindChunk(IBinaryStream file, string chunk)
 {
     try
     {
         var buf = new byte[4];
         file.Position = 8;
         while (-1 != file.PeekByte())
         {
             long chunk_offset = file.Position;
             uint chunk_size   = Binary.BigEndian(file.ReadUInt32());
             if (4 != file.Read(buf, 0, 4))
             {
                 break;
             }
             if (Binary.AsciiEqual(buf, chunk))
             {
                 return(chunk_offset);
             }
             file.Position += chunk_size + 4;
         }
     }
     catch { /* ignore errors */ }
     return(-1L);
 }
Exemplo n.º 25
0
        void QlzUnpack(IBinaryStream input, byte[] output)
        {
            int  dst         = 0;
            uint bits        = 1;
            int  output_last = output.Length - 11;

            while (dst < output.Length)
            {
                if (1 == bits)
                {
                    bits = input.ReadUInt32();
                }
                if ((bits & 1) == 1)
                {
                    int ctl = input.PeekByte();
                    int offset, count = 3;
                    if ((ctl & 3) == 0)
                    {
                        offset = input.ReadUInt8() >> 2;
                    }
                    else if ((ctl & 2) == 0)
                    {
                        offset = input.ReadUInt16() >> 2;
                    }
                    else if ((ctl & 1) == 0)
                    {
                        offset = input.ReadUInt16() >> 6;
                        count += ((ctl >> 2) & 0xF);
                    }
                    else if ((ctl & 0x7F) != 3)
                    {
                        offset = (input.ReadInt24() >> 7) & 0x1FFFF;
                        count += ((ctl >> 2) & 0x1F) - 1;
                    }
                    else
                    {
                        uint v = input.ReadUInt32();
                        offset = (int)(v >> 15);
                        count += (int)((v >> 7) & 0xFF);
                    }
                    Binary.CopyOverlapped(output, dst - offset, dst, count);
                    dst += count;
                }
                else
                {
                    if (dst > output_last)
                    {
                        break;
                    }
                    output[dst++] = input.ReadUInt8();
                }
                bits >>= 1;
            }
            while (dst < output.Length)
            {
                if (1 == bits)
                {
                    input.Seek(4, SeekOrigin.Current);
                    bits = 0x80000000u;
                }
                output[dst++] = input.ReadUInt8();
                bits        >>= 1;
            }
        }
Exemplo n.º 26
0
        public byte[] Unpack(int unpacked_size)
        {
            var output = new byte[unpacked_size];
            int dst    = 0;
            var table  = new byte[2, 256];

            while (dst < unpacked_size && m_input.PeekByte() != -1)
            {
                for (int i = 0; i < 256; ++i)
                {
                    table[0, i] = (byte)i;
                }
                int count;
                int t_idx = 0;
                do
                {
                    byte ctl = m_input.ReadUInt8();
                    if (ctl > 127)
                    {
                        t_idx += ctl - 127;
                        ctl    = 0;
                    }
                    if (t_idx != 256)
                    {
                        count = ctl + 1;
                        while (count-- > 0)
                        {
                            ctl             = (byte)t_idx;
                            table[0, t_idx] = m_input.ReadUInt8();
                            if (t_idx != table[0, t_idx])
                            {
                                table[1, t_idx] = m_input.ReadUInt8();
                            }
                            ++t_idx;
                        }
                    }
                }while (t_idx != 256);
                byte hi = m_input.ReadUInt8();
                byte lo = m_input.ReadUInt8();
                count = hi << 8 | lo;
                int pos = 0;
                for (;;)
                {
                    byte b;
                    if (pos != 0)
                    {
                        b = m_filename[--pos];
                    }
                    else
                    {
                        if (0 == count--)
                        {
                            break;
                        }
                        b = m_input.ReadUInt8();
                    }
                    if (b == table[0, b])
                    {
                        output[dst++] = b;
                        if (dst >= output.Length)
                        {
                            break;
                        }
                    }
                    else
                    {
                        m_filename[pos++] = table[1, b];
                        m_filename[pos++] = table[0, b];
                    }
                }
            }
            return(output);
        }
Exemplo n.º 27
0
            void UnpackV2()
            {
                int  dst = 0;
                byte b = 0, g = 0, r = 0;

                while (dst < m_output.Length)
                {
                    if (GetBit() == 0)
                    {
                        if (GetBit() != 0)
                        {
                            if (m_input.PeekByte() != -1)
                            {
                                b = m_input.ReadUInt8();
                                g = m_input.ReadUInt8();
                                r = m_input.ReadUInt8();
                            }
                        }
                        else if (GetBit() != 0)
                        {
                            byte v = (byte)(GetBits(2));
                            if (3 == v)
                            {
                                b = m_output[dst - m_stride];
                                g = m_output[dst - m_stride + 1];
                                r = m_output[dst - m_stride + 2];
                            }
                            else
                            {
                                b += (byte)(v - 1);
                                v  = (byte)GetBits(2);
                                if (3 == v)
                                {
                                    if (GetBit() != 0)
                                    {
                                        b = m_output[dst - m_stride - 3];
                                        g = m_output[dst - m_stride - 2];
                                        r = m_output[dst - m_stride - 1];
                                    }
                                    else
                                    {
                                        b = m_output[dst - m_stride + 3];
                                        g = m_output[dst - m_stride + 4];
                                        r = m_output[dst - m_stride + 5];
                                    }
                                }
                                else
                                {
                                    g += (byte)(v - 1);
                                    r += (byte)(GetBits(2) - 1);
                                }
                            }
                        }
                        else if (GetBit() != 0)
                        {
                            byte v = (byte)(GetBits(3));
                            if (7 == v)
                            {
                                b  = m_output[dst - m_stride];
                                g  = m_output[dst - m_stride + 1];
                                r  = m_output[dst - m_stride + 2];
                                b += (byte)(GetBits(3) - 3);
                                g += (byte)(GetBits(3) - 3);
                                r += (byte)(GetBits(3) - 3);
                            }
                            else
                            {
                                b += (byte)(v - 3);
                                g += (byte)(GetBits(3) - 3);
                                r += (byte)(GetBits(3) - 3);
                            }
                        }
                        else if (GetBit() != 0)
                        {
                            b += (byte)(GetBits(4) - 7);
                            g += (byte)(GetBits(4) - 7);
                            r += (byte)(GetBits(4) - 7);
                        }
                        else
                        {
                            b += (byte)(GetBits(5) - 15);
                            g += (byte)(GetBits(5) - 15);
                            r += (byte)(GetBits(5) - 15);
                        }
                    }
                    m_output[dst++] = b;
                    m_output[dst++] = g;
                    m_output[dst++] = r;
                }
            }