예제 #1
0
        void UnpackGyu(byte[] packed)
        {
            using (var mem = new MemoryStream(packed, 4, packed.Length - 4))
                using (var bits = new MsbBitStream(mem))
                {
                    int dst = 0;
                    m_output[dst++] = (byte)mem.ReadByte();
                    while (dst < m_output.Length)
                    {
                        int b = bits.GetNextBit();
                        if (-1 == b)
                        {
                            throw new EndOfStreamException();
                        }
                        if (1 == b)
                        {
                            m_output[dst++] = (byte)mem.ReadByte();
                            continue;
                        }
                        int count;
                        int offset;
                        if (1 == bits.GetNextBit())
                        {
                            count  = mem.ReadByte() << 8;
                            count |= mem.ReadByte();
                            offset = -1 << 13 | count >> 3;
                            count &= 7;

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

                        Binary.CopyOverlapped(m_output, dst + offset, dst, ++count);
                        dst += count;
                    }
                }
        }
예제 #2
0
 public BmrDecoder(Stream input)
 {
     input.Position = 3;
     using (var header = new ArcView.Reader(input))
     {
         m_step       = header.ReadByte();
         m_final_size = header.ReadInt32();
         m_key        = header.ReadInt32();
         int unpacked_size = header.ReadInt32();
         m_output = new byte[unpacked_size];
         m_input  = new MsbBitStream(input, true);
     }
 }
예제 #3
0
파일: ImageMSK.cs 프로젝트: zxc120/GARbro
 public RmskReader(Stream input, ImageMetaData info)
 {
     input.Position = 0xC;
     m_flag         = input.ReadByte();
     if (-1 == m_flag)
     {
         throw new EndOfStreamException();
     }
     input.Position = 0xE;
     m_input        = new MsbBitStream(input, true);
     m_width        = (int)info.Width;
     m_height       = (int)info.Height;
     m_output       = new byte[m_width * m_height];
 }
예제 #4
0
파일: ArcUCA.cs 프로젝트: zxc120/GARbro
        protected override ImageData GetImageData()
        {
            m_input.Position = 0x28;
            int palette_size = m_input.ReadInt32();

            if (24 == Info.BPP)
            {
                var palette = m_input.ReadBytes(palette_size * 3);
                int stride  = 3 * (int)Info.Width;
                var pixels  = new byte[stride * (int)Info.Height];
                int dst     = 0;
                while (dst < pixels.Length)
                {
                    int src = m_input.ReadUInt16();
                    if (src >= palette_size)
                    {
                        throw new InvalidFormatException();
                    }
                    int color = src * 3;
                    pixels[dst++] = palette[color + 2];
                    pixels[dst++] = palette[color + 1];
                    pixels[dst++] = palette[color];
                }
                return(ImageData.CreateFlipped(Info, PixelFormats.Bgr24, null, pixels, stride));
            }
            else
            {
                int bits_length = m_input.ReadInt32();
                var palette     = m_input.ReadBytes(palette_size * 2);
                int color_bits  = GetColorBits(palette_size);
                int stride      = 2 * (int)Info.Width;
                var pixels      = new byte[stride * (int)Info.Height];
                int dst         = 0;
                using (var bits = new MsbBitStream(m_input.AsStream, true))
                {
                    while (dst < pixels.Length)
                    {
                        int src = bits.GetBits(color_bits);
                        if (src >= palette_size)
                        {
                            throw new InvalidFormatException();
                        }
                        int color = src * 2;
                        pixels[dst++] = palette[color];
                        pixels[dst++] = palette[color + 1];
                    }
                    return(ImageData.CreateFlipped(Info, PixelFormats.Bgr555, null, pixels, stride));
                }
            }
        }
예제 #5
0
        public GwdReader(IBinaryStream input, ImageMetaData info)
        {
            m_bpp = info.BPP;
            if (m_bpp != 8 && m_bpp != 24)
            {
                throw new InvalidFormatException();
            }

            m_input    = new MsbBitStream(input.AsStream, true);
            m_width    = (int)info.Width;
            m_height   = (int)info.Height;
            m_stride   = m_width * m_bpp / 8;
            m_output   = new byte[m_stride * m_height];
            m_line_buf = new byte[m_width];
        }
예제 #6
0
 public TrkDecoder(Stream input, byte[] header, int start_offset, int data_length)
 {
     m_format.FormatTag             = 1;
     m_format.Channels              = LittleEndian.ToUInt16(header, 0x1A);
     m_format.SamplesPerSecond      = LittleEndian.ToUInt32(header, 0x14);
     m_format.BitsPerSample         = 16;
     m_format.BlockAlign            = (ushort)(2 * m_format.Channels);
     m_format.AverageBytesPerSecond = m_format.SamplesPerSecond * m_format.BlockAlign;
     m_sample_count = LittleEndian.ToInt32(header, 0x10);
     m_input        = new MsbBitStream(input, true);
     m_start        = start_offset;
     m_end          = start_offset + data_length;
     m_output       = new byte[m_sample_count * m_format.BlockAlign];
     ref_sample     = new int[m_format.Channels];
 }
예제 #7
0
파일: ImageK2.cs 프로젝트: zxc120/GARbro
        internal byte[] Decompress(IBinaryStream input, int unpacked_size)
        {
            input.Position = 0x12;
            int data_pos    = input.ReadInt32();
            int bits_length = Math.Min(data_pos - 0x10, (unpacked_size + 7) / 8);
            var ctl_bits    = input.ReadBytes(bits_length);

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

            using (var mem = new MemoryStream(ctl_bits))
                using (var bits = new MsbBitStream(mem))
                    using (var data = new MsbBitStream(input.AsStream, true))
                    {
                        int dst = 0;
                        while (dst < unpacked_size)
                        {
                            int ctl = bits.GetNextBit();
                            if (-1 == ctl)
                            {
                                break;
                            }
                            if (ctl != 0)
                            {
                                output[dst++] = (byte)data.GetBits(8);
                            }
                            else
                            {
                                int offset, count;
                                if (bits.GetNextBit() != 0)
                                {
                                    offset = data.GetBits(14);
                                    count  = data.GetBits(4) + 3;
                                }
                                else
                                {
                                    offset = data.GetBits(9);
                                    count  = data.GetBits(3) + 2;
                                }
                                count = Math.Min(count, output.Length - dst);
                                Binary.CopyOverlapped(output, dst - offset - 1, dst, count);
                                dst += count;
                            }
                        }
                        return(output);
                    }
        }
예제 #8
0
 byte[] Uncompress(byte[] data)
 {
     using (var input = new MemoryStream(data))
         using (var bits = new MsbBitStream(input))
         {
             var pixels = new byte[4 * m_width * m_height];
             if (HasAlpha)
             {
                 UncompressRgba(bits, pixels);
             }
             else
             {
                 UncompressRgb(bits, pixels);
             }
             return(pixels);
         }
 }
예제 #9
0
        public AdxReader(Stream file, byte[] header)
        {
            FrameSize     = header[1];
            BitsPerSample = header[2];
            if (header[0] != 3 || FrameSize != 0x12 || BitsPerSample != 4)
            {
                throw new InvalidFormatException();
            }
            m_samples_per_frame = (FrameSize - 2) * 8 / BitsPerSample;
            m_format.Channels   = header[3];
            if (0 == m_format.Channels || m_format.Channels > 16)
            {
                throw new InvalidFormatException();
            }
            int encoding = BigEndian.ToInt16(header, 0x0E);

            if (encoding != 0x0400)
            {
                throw new NotSupportedException("Not supported ADX encoding");
            }

            m_format.FormatTag             = 1;
            m_format.SamplesPerSecond      = BigEndian.ToUInt32(header, 4);
            m_format.BitsPerSample         = 16;
            m_format.BlockAlign            = (ushort)(m_format.BitsPerSample * m_format.Channels / 8);
            m_format.AverageBytesPerSecond = m_format.SamplesPerSecond * m_format.BlockAlign;
            SampleCount = BigEndian.ToInt32(header, 8);

            int lowest_freq = BigEndian.ToUInt16(header, 12);
            var sqrt2       = Math.Sqrt(2.0);
            var x           = sqrt2 - Math.Cos(2 * Math.PI * lowest_freq / m_format.SamplesPerSecond);
            var y           = sqrt2 - 1;
            var z           = (x - Math.Sqrt((x + y) * (x - y))) / y;

            m_prev_scale0  = (int)Math.Floor(z * 8192);
            m_prev_scale1  = (int)Math.Floor(z * z * -4096);
            m_prev_samples = new int[m_format.Channels][];
            for (int i = 0; i < m_format.Channels; ++i)
            {
                m_prev_samples[i] = new int[2];
            }

            file.Position = 4 + header.Length;
            m_input       = new MsbBitStream(file, true);
        }
예제 #10
0
        protected override ImageData GetImageData()
        {
            m_input.Position = m_start_pos;
            var data = ReadDataBytes();

            using (var bits = new MsbBitStream(m_input.AsStream, true))
            {
                var pixels = new byte[m_stride * (int)Info.Height];
                if (24 == Info.BPP)
                {
                    Unpack24bpp(bits, data, pixels);
                }
                else
                {
                    Unpack8bpp(bits, data, pixels);
                }
                return(ImageData.Create(Info, Format, null, pixels));
            }
        }
예제 #11
0
 internal static void Unpack(Stream input, byte[] output, int dst = 0)
 {
     using (var bits = new MsbBitStream(input, true))
     {
         bits.GetNextBit();
         while (dst < output.Length)
         {
             int count = bits.GetBits(8);
             if (-1 == count)
             {
                 break;
             }
             if (count > 0x7F)
             {
                 int offset = bits.GetBits(10);
                 if (-1 == offset)
                 {
                     throw new EndOfStreamException();
                 }
                 count = Math.Min(count & 0x7F, output.Length - dst);
                 Binary.CopyOverlapped(output, dst - offset, dst, count);
                 dst += count;
             }
             else
             {
                 if (0 == count)
                 {
                     break;
                 }
                 for (int i = 0; i < count && dst < output.Length; i++)
                 {
                     int v = bits.GetBits(8);
                     if (-1 == v)
                     {
                         throw new EndOfStreamException();
                     }
                     output[dst++] = (byte)v;
                 }
             }
         }
     }
 }
예제 #12
0
        byte[] Uncompress(byte[] data)
        {
            int flags = m_flags & 0xFF;

            if (1 == flags)
            {
                return(UncompressSia(data));
            }
            byte[] pixels = null;
            if (2 == flags)
            {
                using (var mem = new MemoryStream(data))
                    using (var input = new MsbBitStream(mem))
                    {
                        Format = PixelFormats.Bgr32;
                        pixels = new byte[4 * m_width * m_height];
                        switch ((m_flags >> 16) & 0xFF)
                        {
                        case 1: UncompressRgb1(input, pixels); break;

                        case 2: UncompressRgb2(input, pixels); break;

                        case 3: UncompressRgb3(input, pixels); break;
                        }
                    }
            }
            else if (3 == flags)
            {
                if (2 == ((m_flags >> 16) & 0xFF))
                {
                    Format = PixelFormats.Bgra32;
                    pixels = new byte[4 * m_w * m_h];
                    UncompressRgba(data, pixels);
                }
            }
            if (null == pixels)
            {
                throw new InvalidFormatException();
            }
            return(pixels);
        }
예제 #13
0
 public KgReader(IBinaryStream input, KgMetaData info)
 {
     m_input      = input;
     m_bits       = new MsbBitStream(input.AsStream, true);
     m_info       = info;
     m_pixel_size = m_info.BPP / 8;
     m_stride     = m_pixel_size * (int)m_info.Width;
     m_output     = new byte[m_stride * (int)m_info.Height];
     if (0 != m_info.AlphaOffset)
     {
         Format = PixelFormats.Bgra32;
     }
     else if (24 == m_info.BPP)
     {
         Format = PixelFormats.Bgr24;
     }
     else
     {
         Format = PixelFormats.Indexed8;
     }
 }
예제 #14
0
파일: ImageDAT.cs 프로젝트: zxc120/GARbro
        void Unpack(IBinaryStream input, byte[] output)
        {
            int offset_bits = input.ReadUInt8();
            int count_bits  = input.ReadUInt8();
            int dst         = 0;

            using (var bits = new MsbBitStream(input.AsStream, true))
            {
                while (dst < output.Length)
                {
                    if (bits.GetNextBit() != 0)
                    {
                        int v = bits.GetBits(8);
                        if (v < 0)
                        {
                            break;
                        }
                        output[dst++] = (byte)v;
                    }
                    else
                    {
                        int src = bits.GetBits(offset_bits);
                        if (src < 0)
                        {
                            break;
                        }
                        int count = bits.GetBits(count_bits);
                        if (count < 0)
                        {
                            break;
                        }
                        count = Math.Min(output.Length - dst, count + 1);
                        Binary.CopyOverlapped(output, src, dst, count);
                        dst += count;
                    }
                }
            }
        }
예제 #15
0
 public GbcReader(Stream input, GbcMetaData info)
 {
     if (32 == info.BPP)
     {
         Format = PixelFormats.Bgra32;
     }
     else if (24 == info.BPP)
     {
         Format = PixelFormats.Bgr24;
     }
     else if (8 == info.BPP)
     {
         Format = PixelFormats.Gray8;
     }
     else
     {
         throw new InvalidFormatException();
     }
     m_input  = new MsbBitStream(input, true);
     m_info   = info;
     m_stride = (int)m_info.Width * m_info.BPP / 8;
     m_output = new byte[m_stride * (int)m_info.Height];
 }
예제 #16
0
파일: ArcVFS.cs 프로젝트: zxc120/GARbro
        byte[] UnpackEntry(Stream input, uint unpacked_size)
        {
            const int dict_size = 0x10;
            var       output    = new byte[unpacked_size];

            using (var bits = new MsbBitStream(input, true))
            {
                var dict     = new byte[dict_size];
                int dict_pos = 0;
                for (int dst = 0; dst < output.Length; ++dst)
                {
                    byte cur_byte;
                    if (bits.GetNextBit() != 0)
                    {
                        int offset = GetBitLength(bits);
                        int pos    = dict_pos - offset;
                        if (pos < 0)
                        {
                            pos += dict_size;
                        }
                        if (pos < 0 || pos >= dict_size)
                        {
                            throw new InvalidDataException("Invalid compressed data.");
                        }
                        cur_byte = dict[pos];
                    }
                    else
                    {
                        cur_byte = (byte)bits.GetBits(8);
                    }
                    output[dst]      = cur_byte;
                    dict[dict_pos++] = cur_byte;
                    dict_pos        &= 0xF;
                }
            }
            return(output);
        }
예제 #17
0
 int UnpackHuffman(byte[] input, int input_length, byte[] output)
 {
     using (var mem = new MemoryStream(input, 0, input_length))
     {
         var    tree = new HuffmanNode[0x201];
         ushort root = BuildHuffmanTree(tree, mem);
         using (var bits = new MsbBitStream(mem))
         {
             int dst = 0;
             for (;;)
             {
                 ushort symbol = root;
                 while (symbol > 0x100)
                 {
                     int bit = bits.GetBits(1);
                     if (-1 == bit)
                     {
                         return(dst);
                     }
                     if (bit != 0)
                     {
                         symbol = tree[symbol].RChild;
                     }
                     else
                     {
                         symbol = tree[symbol].LChild;
                     }
                 }
                 if (0x100 == symbol)
                 {
                     return(dst);
                 }
                 output[dst++] = (byte)symbol;
             }
         }
     }
 }
예제 #18
0
파일: ImageHHP.cs 프로젝트: zxc120/GARbro
 public HhpReader(IBinaryStream input, ImageMetaData info)
 {
     m_input  = new MsbBitStream(input.AsStream, true);
     m_output = new byte[info.Width * info.Height];
     m_stride = (int)info.Width;
 }
예제 #19
0
파일: ArcFVP.cs 프로젝트: tenyuhuang/GARbro
 public LzwDecoder(Stream input, int unpacked_size)
 {
     m_input  = new MsbBitStream(input, true);
     m_output = new byte[unpacked_size];
 }
예제 #20
0
 public LzReader(Stream input, uint packed_size, uint unpacked_size)
 {
     m_input  = new MsbBitStream(input, true);
     m_output = new byte[unpacked_size];
 }
예제 #21
0
 public override void Initialize(Stream input)
 {
     m_input = new MsbBitStream(input, true);
 }
예제 #22
0
파일: ArcCPK.cs 프로젝트: Casidi/GARbro
        public override Stream OpenEntry(ArcFile arc, Entry entry)
        {
            if (entry.Size < 0x10 || !arc.File.View.AsciiEqual (entry.Offset, "CRILAYLA"))
                return base.OpenEntry (arc, entry);

            var unpacked_size = arc.File.View.ReadInt32 (entry.Offset+8);
            var packed_size = arc.File.View.ReadUInt32 (entry.Offset+12);
            if (unpacked_size < 0 || packed_size > entry.Size - 0x10)
                return base.OpenEntry (arc, entry);
            uint prefix_size = entry.Size - (0x10+packed_size);
            var output = new byte[unpacked_size+prefix_size];
            var packed = arc.File.View.ReadBytes (entry.Offset+0x10, packed_size);
            Array.Reverse (packed);
            using (var mem = new MemoryStream (packed))
            using (var input = new MsbBitStream (mem))
            {
                byte[] sizes = { 2, 3, 5, 8 };
                int dst = (int)prefix_size;
                while (dst < output.Length)
                {
                    if (0 == input.GetNextBit())
                    {
                        output[dst++] = (byte)input.GetBits (8);
                        continue;
                    }
                    int count = 3;
                    int offset = input.GetBits (13) + 3;
                    int rank = 0;
                    int bits, step;
                    do
                    {
                        bits = sizes[rank];
                        step = input.GetBits (bits);
                        count += step;
                        if (rank < 3)
                            rank++;
                    }
                    while (((1 << bits) - 1) == step);
                    Binary.CopyOverlapped (output, dst-offset, dst, count);
                    dst += count;
                }
            }
            Array.Reverse (output, (int)prefix_size, unpacked_size);
            arc.File.View.Read (entry.Offset+0x10+packed_size, output, 0, prefix_size);
            return new MemoryStream (output);
        }
예제 #23
0
파일: AudioADX.cs 프로젝트: Casidi/GARbro
        public AdxReader(Stream file, byte[] header)
        {
            FrameSize = header[1];
            BitsPerSample = header[2];
            if (header[0] != 3 || FrameSize != 0x12 || BitsPerSample != 4)
                throw new InvalidFormatException();
            m_samples_per_frame = (FrameSize - 2) * 8 / BitsPerSample;
            m_format.Channels = header[3];
            if (0 == m_format.Channels || m_format.Channels > 16)
                throw new InvalidFormatException();
            int encoding = BigEndian.ToInt16 (header, 0x0E);
            if (encoding != 0x0400)
                throw new NotSupportedException ("Not supported ADX encoding");

            m_format.FormatTag              = 1;
            m_format.SamplesPerSecond       = BigEndian.ToUInt32 (header, 4);
            m_format.BitsPerSample          = 16;
            m_format.BlockAlign             = (ushort)(m_format.BitsPerSample * m_format.Channels / 8);
            m_format.AverageBytesPerSecond  = m_format.SamplesPerSecond * m_format.BlockAlign;
            SampleCount = BigEndian.ToInt32 (header, 8);

            int lowest_freq = BigEndian.ToUInt16 (header, 12);
            var sqrt2 = Math.Sqrt (2.0);
            var x = sqrt2 - Math.Cos (2 * Math.PI * lowest_freq / m_format.SamplesPerSecond);
            var y = sqrt2 - 1;
            var z = (x - Math.Sqrt ((x + y) * (x - y))) / y;

            m_prev_scale0 = (int)Math.Floor (z * 8192);
            m_prev_scale1 = (int)Math.Floor (z * z * -4096);
            m_prev_samples = new int[m_format.Channels][];
            for (int i = 0; i < m_format.Channels; ++i)
            {
                m_prev_samples[i] = new int[2];
            }

            file.Position = 4+header.Length;
            m_input = new MsbBitStream (file, true);
        }
예제 #24
0
파일: ArcPACK.cs 프로젝트: uboaa/GARbro
        protected override void Unpack()
        {
            m_input.Position = 4;
            var data = ReadDataBytes();

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

            if (max != 0 && max != 0xFF)
            {
                for (int i = 0; i < m_output.Length; ++i)
                {
                    m_output[i] = (byte)(m_output[i] * 0xFF / max);
                }
            }
        }
예제 #25
0
        protected void Unpack24bpp(MsbBitStream bits, byte[] data, byte[] output)
        {
            int src = 0, dst = 0;

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

                                case 2:  v += 7; break;

                                case 1:  v -= 6; break;

                                default: v += 6; break;
                                }
                            }
                            else if (ctl == 1)
                            {
                                v -= 4;
                            }
                            else
                            {
                                v += 4;
                            }
                        }
                        else if (ctl == 1)
                        {
                            v -= 2;
                        }
                        else
                        {
                            v += 2;
                        }
                    }
                }
                output[dst++] = v;
            }
        }
예제 #26
0
파일: ArcFVP.cs 프로젝트: Casidi/GARbro
 public LzwDecoder(Stream input, int unpacked_size)
 {
     m_input = new MsbBitStream (input, true);
     m_output = new byte[unpacked_size];
 }
예제 #27
0
파일: ArcPACK.cs 프로젝트: uboaa/GARbro
        protected virtual void Unpack()
        {
            m_input.Position = 4;
            var data = ReadDataBytes();

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

                                    case 2:  v += 7; break;

                                    case 1:  v -= 6; break;

                                    default: v += 6; break;
                                    }
                                }
                                else if (ctl == 1)
                                {
                                    v -= 4;
                                }
                                else
                                {
                                    v += 4;
                                }
                            }
                            else if (ctl == 1)
                            {
                                v -= 2;
                            }
                            else
                            {
                                v += 2;
                            }
                        }
                    }
                    m_output[dst] = v;
                }
            }
        }
예제 #28
0
        protected void Unpack8bpp(MsbBitStream bits, byte[] data, byte[] output)
        {
            int  src        = 0;
            int  dst        = 0;
            byte init_value = data[src++];

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

            while (dst < output.Length)
            {
                int ctl = bits.GetBits(2);
                if (0 == ctl)
                {
                    int count;
                    if (bit_count > 0)
                    {
                        count = bits.GetBits(14 - bit_count);
                    }
                    else
                    {
                        count = bits.GetBits(6);
                    }
                    while (count-- > 0 && dst < output.Length)
                    {
                        if (y == 0 || x + 1 == m_stride)
                        {
                            output[dst] = init_value;
                        }
                        else
                        {
                            output[dst] = output[dst - m_stride + 1];
                        }
                        ++dst;
                        if (++x == m_stride)
                        {
                            x = 0;
                            ++y;
                        }
                    }
                    bit_count = 0;
                    continue;
                }
                else if (1 == ctl)
                {
                    bit_count += 2;
                    if (0 == x)
                    {
                        output[dst] = init_value;
                    }
                    else
                    {
                        output[dst] = output[dst - m_stride - 1];
                    }
                }
                else if (2 == ctl)
                {
                    bit_count  += 2;
                    output[dst] = data[src++];
                }
                else
                {
                    bit_count += 3;
                    if (bits.GetNextBit() != 0)
                    {
                        output[dst] = output[dst - m_stride];
                    }
                    else
                    {
                        output[dst] = output[dst - 1];
                    }
                }
                ++dst;
                if (++x == m_stride)
                {
                    x = 0;
                    ++y;
                }
                if (bit_count >= 8)
                {
                    bit_count -= 8;
                }
            }
            if (8 == Info.BPP)
            {
                byte max = m_max_pixel; // System.Linq.Enumerable.Max (output);
                if (max != 0 && max != 0xFF)
                {
                    for (int i = 0; i < output.Length; ++i)
                    {
                        output[i] = (byte)(output[i] * 0xFF / max);
                    }
                }
            }
        }
예제 #29
0
파일: ImageMSK.cs 프로젝트: Casidi/GARbro
 public RmskReader(Stream input, ImageMetaData info)
 {
     input.Position = 0xC;
     m_flag = input.ReadByte();
     if (-1 == m_flag)
         throw new EndOfStreamException();
     input.Position = 0xE;
     m_input = new MsbBitStream (input, true);
     m_width = (int)info.Width;
     m_height = (int)info.Height;
     m_output = new byte[m_width*m_height];
 }
예제 #30
0
파일: ImageWCG.cs 프로젝트: Casidi/GARbro
 public Reader(Stream file, uint pixel_size)
 {
     m_data = new byte[pixel_size*4];
     m_input_size = (uint)file.Length;
     m_input = new BinaryReader (file, Encoding.ASCII, true);
     m_bits = new MsbBitStream (file, true);
 }
예제 #31
0
파일: ArcLINK.cs 프로젝트: Casidi/GARbro
 public BmrDecoder(Stream input)
 {
     input.Position = 3;
     using (var header = new ArcView.Reader (input))
     {
         m_step = header.ReadByte();
         m_final_size = header.ReadInt32();
         m_key = header.ReadInt32();
         int unpacked_size = header.ReadInt32();
         m_output = new byte[unpacked_size];
         m_input = new MsbBitStream (input, true);
     }
 }
예제 #32
0
 public LzCompression(IBinaryStream input, int unpacked_size)
 {
     m_input  = new MsbBitStream(input.AsStream);
     m_output = new byte[unpacked_size];
     m_frame  = new byte[0x1000];
 }
예제 #33
0
        public void UnpackBlock(int offset, int length, int dst)
        {
            using (var input = new MemoryStream(this.Input, offset, length))
                using (var reader = new MsbBitStream(input))
                {
                    int block_size = CbgReader.ReadInteger(input);
                    if (-1 == block_size)
                    {
                        return;
                    }
                    var color_data = new short[block_size];
                    int acc        = 0;
                    for (int i = 0; i < block_size && input.Position < input.Length; i += 64)
                    {
                        int count = Tree1.DecodeToken(reader);
                        if (count != 0)
                        {
                            int v = reader.GetBits(count);
                            if (0 == (v >> (count - 1)))
                            {
                                v = (-1 << count | v) + 1;
                            }
                            acc += v;
                        }
                        color_data[i] = (short)acc;
                    }

                    if (0 != (reader.CacheSize & 7))
                    {
                        reader.GetBits(reader.CacheSize & 7);
                    }

                    for (int i = 0; i < block_size && input.Position < input.Length; i += 64)
                    {
                        int index = 1;
                        while (index < 64 && input.Position < input.Length)
                        {
                            int code = Tree2.DecodeToken(reader);
                            if (0 == code)
                            {
                                break;
                            }
                            if (0xF == code)
                            {
                                index += 0x10;
                                continue;
                            }
                            index += code & 0xF;
                            if (index >= block_fill_order.Length)
                            {
                                break;
                            }
                            code >>= 4;
                            int v = reader.GetBits(code);
                            if (code != 0 && 0 == (v >> (code - 1)))
                            {
                                v = (-1 << code | v) + 1;
                            }
                            color_data[i + block_fill_order[index]] = (short)v;
                            ++index;
                        }
                    }
                    if (8 == BPP)
                    {
                        DecodeGrayscale(color_data, dst);
                    }
                    else
                    {
                        DecodeRGB(color_data, dst);
                    }
                }
        }
예제 #34
0
파일: ImageBGI.cs 프로젝트: Casidi/GARbro
        public void UnpackBlock(int offset, int length, int dst)
        {
            using (var input = new MemoryStream (this.Input, offset, length))
            using (var reader = new MsbBitStream (input))
            {
                int block_size = CbgReader.ReadInteger (input);
                if (-1 == block_size)
                    return;
                var color_data = new short[block_size];
                int acc = 0;
                for (int i = 0; i < block_size && input.Position < input.Length; i += 64)
                {
                    int count = Tree1.DecodeToken (reader);
                    if (count != 0)
                    {
                        int v = reader.GetBits (count);
                        if (0 == (v >> (count - 1)))
                            v = (-1 << count | v) + 1;
                        acc += v;
                    }
                    color_data[i] = (short)acc;
                }

                if (0 != (reader.CacheSize & 7))
                    reader.GetBits (reader.CacheSize & 7);

                for (int i = 0; i < block_size && input.Position < input.Length; i += 64)
                {
                    int index = 1;
                    while (index < 64 && input.Position < input.Length)
                    {
                        int code = Tree2.DecodeToken (reader);
                        if (0 == code)
                            break;
                        if (0xF == code)
                        {
                            index += 0x10;
                            continue;
                        }
                        index += code & 0xF;
                        if (index >= block_fill_order.Length)
                            break;
                        code >>= 4;
                        int v = reader.GetBits (code);
                        if (code != 0 && 0 == (v >> (code - 1)))
                            v = (-1 << code | v) + 1;
                        color_data[i + block_fill_order[index]] = (short)v;
                        ++index;
                    }
                }
                if (8 == BPP)
                    DecodeGrayscale (color_data, dst);
                else
                    DecodeRGB (color_data, dst);
            }
        }