예제 #1
0
        public void Unpack()
        {
            m_input.Position = 0x11;
            int ctl_length = (m_input.ReadInt32() + 7) / 8;
            var ctl_bytes  = m_input.ReadBytes(ctl_length);

            m_input.ReadInt32();
            using (var ctl_mem = new MemoryStream(ctl_bytes))
                using (var bits = new LsbBitStream(ctl_mem))
                {
                    int dst = 0;
                    while (dst < m_output.Length)
                    {
                        int bit = bits.GetNextBit();
                        if (-1 == bit)
                        {
                            break;
                        }
                        if (0 == bit)
                        {
                            m_output[dst++] = m_input.ReadUInt8();
                        }
                        else
                        {
                            int offset = m_input.ReadUInt16();
                            int count  = (offset & 7) + 1;
                            offset = (offset >> 3) + 1;
                            Binary.CopyOverlapped(m_output, dst - offset, dst, count);
                            dst += count;
                        }
                    }
                }
        }
예제 #2
0
        public OggBitStream(OggPacket input)
        {
            // certainly an overhead to create a new stream for every packet, but it's so convenient
            var buf = new MemoryStream(input.Packet);

            m_input = new LsbBitStream(buf);
        }
예제 #3
0
        public byte[] UnpackStream(long data_offset, int data_packed, int data_unpacked, int ctl_packed, int ctl_unpacked)
        {
            var ctl_offset = data_offset + data_packed;
            var data       = new byte[data_unpacked];

            using (var z = new StreamRegion(InputStream, data_offset, data_packed, true))
                using (var data_in = new ZLibStream(z, CompressionMode.Decompress))
                    if (data.Length != data_in.Read(data, 0, data.Length))
                    {
                        throw new EndOfStreamException();
                    }

            using (var z = new StreamRegion(InputStream, ctl_offset, ctl_packed, true))
                using (var ctl_in = new ZLibStream(z, CompressionMode.Decompress))
                    using (var bits = new LsbBitStream(ctl_in))
                    {
                        bool copy        = bits.GetNextBit() != 0;
                        int  output_size = GetBitCount(bits);
                        var  output      = new byte[output_size];
                        int  src         = 0;
                        int  dst         = 0;
                        while (dst < output_size)
                        {
                            int count = GetBitCount(bits);
                            if (copy)
                            {
                                Buffer.BlockCopy(data, src, output, dst, count);
                                src += count;
                            }
                            dst += count;
                            copy = !copy;
                        }
                        return(ApplyDelta(output));
                    }
        }
예제 #4
0
파일: ImageGSA.cs 프로젝트: zxc120/GARbro
 public GsaReader(IBinaryStream file, GsaMetaData info)
 {
     m_input  = new LsbBitStream(file.AsStream, true);
     m_type   = info.Type;
     m_width  = (int)info.Width;
     m_height = (int)info.Height;
 }
예제 #5
0
        byte[] UnpackHuffman(Stream input)
        {
            var tree     = CreateHuffmanTree(input);
            var unpacked = new byte[m_huffman_unpacked];

            using (var bits = new LsbBitStream(input, true))
            {
                int dst = 0;
                while (dst < m_huffman_unpacked)
                {
                    int node = RootNodeIndex;
                    while (node > 0xFF)
                    {
                        if (0 != bits.GetNextBit())
                        {
                            node = tree[node].Right;
                        }
                        else
                        {
                            node = tree[node].Left;
                        }
                    }
                    unpacked[dst++] = (byte)node;
                }
            }
            return(unpacked);
        }
예제 #6
0
        public ImgXDecoder(IBinaryStream input)
        {
            input.Position = 4;
            uint unpacked_size = ~input.ReadUInt32();

            m_unpacked_size = unpacked_size >> 16 | unpacked_size << 16;
            m_input         = new LsbBitStream(input.AsStream);
        }
예제 #7
0
파일: AudioNWA.cs 프로젝트: zxc120/GARbro
 public NwaDecoder(IBinaryStream input, NwaMetaData info)
 {
     m_input  = input;
     m_info   = info;
     m_output = new byte[m_info.PcmSize];
     m_sample = new short[2];
     m_bits   = new LsbBitStream(input.AsStream, true);
 }
예제 #8
0
        static int GetBitCount(LsbBitStream bits)
        {
            int n = 0;

            while (0 == bits.GetNextBit())
            {
                ++n;
                if (n >= 0x20)
                {
                    throw new InvalidFormatException("Overflow at HgReader.GetBitCount");
                }
            }
            int value = 1;

            while (n-- > 0)
            {
                value = (value << 1) | bits.GetNextBit();
            }
            return(value);
        }
예제 #9
0
파일: ImageMFC.cs 프로젝트: zxc120/GARbro
        public override ImageData Read(IBinaryStream stream, ImageMetaData info)
        {
            var meta = (MfcMetaData)info;
            // XXX implemented like in reference code, width is expected to be even.
            var alpha = new byte[info.Width * info.Height / 2];

            stream.Position = 0x18;
            RleUnpack(stream.AsStream, meta.AlphaSize - 0x18, alpha);
            byte[] pixels;
            using (var reg = new StreamRegion(stream.AsStream, meta.AlphaSize, true))
                using (var sbi = new BinaryStream(reg, stream.Name))
                    using (var reader = new SbiReader(sbi, meta.BaseInfo))
                    {
                        reader.Unpack();
                        if (meta.BaseInfo.BPP != 32)
                        {
                            var bitmap = BitmapSource.Create((int)info.Width, (int)info.Height, ImageData.DefaultDpiX, ImageData.DefaultDpiY,
                                                             reader.Format, reader.Palette, reader.Data, reader.Stride);
                            bitmap = new FormatConvertedBitmap(bitmap, PixelFormats.Bgra32, null, 0);
                            int stride = (int)info.Width * 4;
                            pixels = new byte[stride * (int)info.Height];
                            bitmap.CopyPixels(pixels, stride, 0);
                        }
                        else
                        {
                            pixels = reader.Data;
                        }
                    }
            using (var mem = new MemoryStream(alpha))
                using (var bits = new LsbBitStream(mem))
                {
                    for (int i = 3; i < pixels.Length; i += 4)
                    {
                        pixels[i] = (byte)(bits.GetBits(4) * 0x11);
                    }
                }
            return(ImageData.Create(info, PixelFormats.Bgra32, null, pixels));
        }
예제 #10
0
        void UnpackV2Alpha()
        {
            m_output = new byte[m_stride * m_height];
            Format   = PixelFormats.Bgra32;

            var palette = ImageFormat.ReadColorMap(m_input.AsStream);
            var index   = new int[m_height];

            for (int i = 0; i < m_height; ++i)
            {
                index[i] = m_input.ReadInt32();
            }
            var data_pos = m_input.Position;
            int dst      = 0;

            using (var bits = new LsbBitStream(m_input.AsStream, true))
            {
                for (int y = 0; y < m_height; ++y)
                {
                    bits.Input.Position = data_pos + index[y];
                    bits.Reset();
                    for (int x = 0; x < m_width;)
                    {
                        int ctl = bits.GetBits(2);
                        int count;
                        if (0 != (ctl & 2))
                        {
                            count = bits.GetBits(10);
                        }
                        else
                        {
                            count = bits.GetBits(2);
                        }
                        count = Math.Min(count, m_width - x);
                        x    += count;
                        if (0 != (ctl & 1))
                        {
                            byte a = (byte)bits.GetBits(4);
                            if (a != 0)
                            {
                                a = (byte)((a << 4) | 0xF);
                            }
                            int c = bits.GetBits(8);
                            if (-1 == c)
                            {
                                throw new EndOfStreamException();
                            }
                            var color = palette[c];
                            for (int i = 0; i < count; ++i)
                            {
                                m_output[dst++] = color.B;
                                m_output[dst++] = color.G;
                                m_output[dst++] = color.R;
                                m_output[dst++] = a;
                            }
                        }
                        else
                        {
                            for (int i = 0; i < count; ++i)
                            {
                                int a = bits.GetBits(4);
                                if (a != 0)
                                {
                                    a = (a << 4) | 0xF;
                                }
                                int c = bits.GetBits(8);
                                if (-1 == c)
                                {
                                    throw new EndOfStreamException();
                                }
                                var color = palette[c];
                                m_output[dst++] = color.B;
                                m_output[dst++] = color.G;
                                m_output[dst++] = color.R;
                                m_output[dst++] = (byte)a;
                            }
                        }
                    }
                }
            }
        }
예제 #11
0
파일: ImageMGO.cs 프로젝트: zxc120/GARbro
 public override void Initialize(Stream input)
 {
     m_input = new LsbBitStream(input, true);
 }
예제 #12
0
        SoundInput Unpack(IBinaryStream input)
        {
            input.Position = 0x40;
            var header = new byte[0x24];

            if (0x14 != input.Read(header, 0, 0x14))
            {
                return(null);
            }
            int fmt_size = LittleEndian.ToInt32(header, 0x10);

            if (fmt_size + input.Position > input.Length)
            {
                return(null);
            }
            int header_size = fmt_size + 0x14;

            if (header_size > header.Length)
            {
                Array.Resize(ref header, header_size);
            }
            if (fmt_size != input.Read(header, 0x14, fmt_size))
            {
                return(null);
            }
            int riff_size = LittleEndian.ToInt32(header, 4) + 8;
            int data_size = riff_size - header_size;
            var pcm       = new MemoryStream(riff_size);

            try
            {
                pcm.Write(header, 0, header_size);
                using (var output = new BinaryWriter(pcm, Encoding.Default, true))
                    using (var bits = new LsbBitStream(input.AsStream, true))
                    {
                        int   written = 0;
                        short sample  = 0;
                        while (written < data_size)
                        {
                            int c = bits.GetBits(4);
                            if (-1 == c)
                            {
                                c = 0;
                            }
                            int code = 0;
                            if (c > 0)
                            {
                                code = bits.GetBits(c) << (32 - c);
                            }
                            code >>= 32 - c;
                            int sign = code >> 31;
                            code   ^= 0x4000 >> (15 - c);
                            code   -= sign;
                            sample += (short)code;
                            output.Write(sample);
                            written += 2;
                        }
                    }
                pcm.Position = 0;
                var sound = Wav.TryOpen(new BinMemoryStream(pcm, input.Name));
                if (sound != null)
                {
                    input.Dispose();
                }
                else
                {
                    pcm.Dispose();
                }
                return(sound);
            }
            catch
            {
                pcm.Dispose();
                throw;
            }
        }
예제 #13
0
        protected override ImageData GetImageData()
        {
            if (m_info.IsMask)
            {
                return(GetMaskData());
            }

            m_input.Position = 6;
            int pixel_size;

            if (1 == m_info.Method || 2 == m_info.Method)
            {
                int colors = m_input.ReadUInt16();
                Palette    = ImageFormat.ReadPalette(m_input.AsStream, colors, PaletteFormat.Bgr);
                pixel_size = 1;
            }
            else if (3 == m_info.Method)
            {
                pixel_size = 3;
            }
            else
            {
                throw new InvalidFormatException();
            }

            if (2 == m_info.Method)
            {
                m_symbol_table = Enumerable.Range(0, 0x100).Select(x => (byte)x).ToArray();
            }
            else if (3 == m_info.Method)
            {
                m_symbol_table = m_input.ReadBytes(0x100);
            }

            int  plane_length  = m_width * m_height;
            var  planes        = new byte[pixel_size * plane_length];
            int  packed_length = m_input.ReadInt32();
            long data_end      = m_input.Position + packed_length;

            if (m_info.Method > 1)
            {
                using (var bits = new LsbBitStream(m_input.AsStream, true))
                    UnpackHuffman(bits, planes);
            }
            else
            {
                UnpackLz(planes);
            }

            byte[] alpha = null;
            if (m_info.HasAlpha)
            {
                m_input.Position = data_end;
                int w      = m_input.ReadInt32();
                int h      = m_input.ReadInt32();
                int method = m_input.ReadByte();
                if (w == m_width && h == m_height)
                {
                    alpha = new byte[plane_length];
                    if (1 == method)
                    {
                        UnpackRle(alpha);
                    }
                    else if (2 == method)
                    {
                        UnpackAlphaV2(alpha);
                    }
                    else
                    {
                        m_input.Seek(-1, SeekOrigin.Current);
                        UnpackRle(alpha);
                        for (int i = 0; i < alpha.Length; ++i)
                        {
                            alpha[i] = (byte)(alpha[i] * 0xFF / 0x64);
                        }
                    }
                    pixel_size = 4;
                }
            }

            byte[] pixels;
            if (3 == m_info.Method)
            {
                int b = 0;
                int g = plane_length;
                int r = plane_length * 2;
                PaethFilter(planes, b);
                PaethFilter(planes, g);
                PaethFilter(planes, r);
                pixels = new byte[pixel_size * plane_length];
                int dst = 0;
                for (int src = 0; src < plane_length; ++src)
                {
                    pixels[dst]     = planes[b + src];
                    pixels[dst + 1] = planes[g + src];
                    pixels[dst + 2] = planes[r + src];
                    if (alpha != null)
                    {
                        pixels[dst + 3] = alpha[src];
                    }
                    dst += pixel_size;
                }
            }
            else if (alpha != null)
            {
                pixels = new byte[pixel_size * plane_length];
                int dst = 0;
                for (int src = 0; src < plane_length; ++src)
                {
                    var color = Palette.Colors[planes[src]];
                    pixels[dst++] = color.B;
                    pixels[dst++] = color.G;
                    pixels[dst++] = color.R;
                    pixels[dst++] = alpha[src];
                }
                Palette = null;
            }
            else
            {
                pixels = planes;
            }

            if (1 == pixel_size)
            {
                Format = PixelFormats.Indexed8;
            }
            else if (3 == pixel_size)
            {
                Format = PixelFormats.Bgr24;
            }
            else
            {
                Format = PixelFormats.Bgra32;
            }

            int stride = m_width * pixel_size;

            return(ImageData.Create(m_info, Format, Palette, pixels, stride));
        }
예제 #14
0
파일: ImageGRP.cs 프로젝트: Casidi/GARbro
 public void Unpack()
 {
     m_input.BaseStream.Position = 0x11;
     int ctl_length = (m_input.ReadInt32() + 7) / 8;
     var ctl_bytes = m_input.ReadBytes (ctl_length);
     m_input.ReadInt32();
     using (var ctl_mem = new MemoryStream (ctl_bytes))
     using (var bits = new LsbBitStream (ctl_mem))
     {
         int dst = 0;
         while (dst < m_output.Length)
         {
             int bit = bits.GetNextBit();
             if (-1 == bit)
                 break;
             if (0 == bit)
             {
                 m_output[dst++] = m_input.ReadByte();
             }
             else
             {
                 int offset = m_input.ReadUInt16();
                 int count = (offset & 7) + 1;
                 offset = (offset >> 3) + 1;
                 Binary.CopyOverlapped (m_output, dst - offset, dst, count);
                 dst += count;
             }
         }
     }
 }
예제 #15
0
 static string DecodeClassName(byte[] enc, int length)
 {
     using (var output = new MemoryStream())
         using (var input = new MemoryStream(enc, 0, length))
             using (var bits = new LsbBitStream(input))
             {
                 if (0 == bits.GetNextBit())
                 {
                     output.WriteByte((byte)'C');
                 }
                 for (;;)
                 {
                     int b = bits.GetNextBit();
                     if (-1 == b)
                     {
                         break;
                     }
                     int c;
                     if (0 == b)
                     {
                         int idx = bits.GetBits(4);
                         if (-1 == idx)
                         {
                             break;
                         }
                         c = CharMap1[idx];
                     }
                     else if (bits.GetNextBit() != 0)
                     {
                         int idx = bits.GetBits(5);
                         if (-1 == idx)
                         {
                             break;
                         }
                         c = CharMap3[idx];
                     }
                     else
                     {
                         int idx = bits.GetBits(4);
                         if (-1 == idx)
                         {
                             break;
                         }
                         if (idx != 0)
                         {
                             c = CharMap2[idx];
                         }
                         else
                         {
                             c = bits.GetBits(8);
                             if (-1 == c)
                             {
                                 break;
                             }
                         }
                     }
                     output.WriteByte((byte)c);
                 }
                 var buf = output.GetBuffer();
                 return(Encodings.cp932.GetString(buf, 0, (int)output.Length));
             }
 }