示例#1
0
文件: ImageGP8.cs 项目: Casidi/GARbro
 public override ImageData Read(Stream stream, ImageMetaData info)
 {
     stream.Position = 8;
     var palette = ReadPalette (stream);
     var pixels = new byte[info.Width * info.Height];
     using (var reader = new LzssStream (stream, LzssMode.Decompress, true))
     {
         if (pixels.Length != reader.Read (pixels, 0, pixels.Length))
             throw new InvalidFormatException();
         return ImageData.CreateFlipped (info, PixelFormats.Indexed8, palette, pixels, (int)info.Width);
     }
 }
示例#2
0
文件: ImageGFB.cs 项目: Casidi/GARbro
        public override ImageData Read(Stream stream, ImageMetaData info)
        {
            var meta = (GfbMetaData)info;
            BitmapPalette palette = null;
            if (8 == meta.BPP && meta.DataOffset != 0x40)
            {
                stream.Position = 0x40;
                palette = ReadPalette (stream, meta.DataOffset - 0x40);
            }

            stream.Position = meta.DataOffset;
            byte[] pixels = new byte[meta.UnpackedSize];
            if (0 != meta.PackedSize)
            {
                using (var lzss = new LzssStream (stream, LzssMode.Decompress, true))
                    lzss.Read (pixels, 0, pixels.Length);
            }
            else
                stream.Read (pixels, 0, pixels.Length);

            PixelFormat format;
            switch (meta.BPP)
            {
            case 32:
                if (HasAlphaChannel (pixels))
                    format = PixelFormats.Bgra32;
                else
                    format = PixelFormats.Bgr32;
                break;

            case 24:
                format = PixelFormats.Bgr24;
                break;

            case 16:
                format = PixelFormats.Bgr565;
                break;

            case 8:
                if (null != palette)
                    format = PixelFormats.Indexed8;
                else
                    format = PixelFormats.Gray8;
                break;

            default:
                throw new NotSupportedException ("Not supported GFB color depth");
            }
            int stride = pixels.Length / (int)info.Height;

            return ImageData.CreateFlipped (info, format, palette, pixels, stride);
        }
示例#3
0
文件: ImageGS.cs 项目: Casidi/GARbro
        public override ImageData Read(Stream stream, ImageMetaData info)
        {
            var meta = (PicMetaData)info;
            stream.Position = meta.HeaderSize;
            using (var input = new LzssStream (stream, LzssMode.Decompress, true))
            {
                BitmapPalette palette = null;
                PixelFormat format;
                if (8 == meta.BPP) // read palette
                {
                    format = PixelFormats.Indexed8;
                    palette = ReadPalette (input);
                }
                else if (24 == meta.BPP)
                    format = PixelFormats.Bgr24;
                else if (16 == meta.BPP)
                    format = PixelFormats.Bgr565;
                else
                    format = PixelFormats.Bgr32;

                int stride = (int)meta.Width*((info.BPP+7)/8);
                var pixels = new byte[stride*meta.Height];
                input.Read (pixels, 0, pixels.Length);
                if (32 == meta.BPP)
                {
                    for (int i = 3; i < pixels.Length; i += 4)
                    {
                        if (0 != pixels[i])
                        {
                            format = PixelFormats.Bgra32;
                            break;
                        }
                    }
                }
                return ImageData.Create (meta, format, palette, pixels);
            }
        }
示例#4
0
文件: ImageAKB.cs 项目: Casidi/GARbro
        public byte[] Unpack()
        {
            if (0 == m_info.InnerWidth || 0 == m_info.InnerHeight)
                return CreateBackground();

            m_input.Position = 0x20;
            int inner_stride = m_info.InnerWidth * m_pixel_size;
            var pixels = new byte[m_info.InnerHeight * inner_stride];
            using (var lz = new LzssStream (m_input, LzssMode.Decompress, true))
            {
                for (int pos = pixels.Length - inner_stride; pos >= 0; pos -= inner_stride)
                {
                    if (inner_stride != lz.Read (pixels, pos, inner_stride))
                        throw new InvalidFormatException();
                }
            }
            RestoreDelta (pixels, inner_stride);
            if (m_info.InnerWidth == m_info.Width && m_info.InnerHeight == m_info.Height)
                return pixels;

            var image = CreateBackground();
            int src = 0;
            int dst = m_info.OffsetY * Stride + m_info.OffsetX * m_pixel_size;
            for (int y = 0; y < m_info.InnerHeight; ++y)
            {
                Buffer.BlockCopy (pixels, src, image, dst, inner_stride);
                dst += Stride;
                src += inner_stride;
            }
            return image;
        }
示例#5
0
文件: ImageHIZ.cs 项目: Casidi/GARbro
        public override ImageData Read(Stream stream, ImageMetaData info)
        {
            var meta = info as HizMetaData;
            if (null == meta)
                throw new ArgumentException ("HizFormat.Read should be supplied with HizMetaData", "info");

            var pixels = new byte[meta.UnpackedSize];
            stream.Position = meta.DataOffset;
            using (var lzss = new LzssStream (stream, LzssMode.Decompress, true))
            {
                var channel = new byte[info.Width*info.Height];
                for (int p = 0; p < 4; ++p)
                {
                    if (channel.Length != lzss.Read (channel, 0, channel.Length))
                        throw new InvalidFormatException ("Unexpected end of file");
                    int src = 0;
                    for (int i = p; i < pixels.Length; i += 4)
                        pixels[i] = channel[src++];
                }
                return ImageData.Create (info, PixelFormats.Bgra32, null, pixels);
            }
        }
示例#6
0
            bool ReadV1(Stream input)
            {
                // NOTE CryptoStream will close an input stream
                using (var xored = new CryptoStream (input, new NotTransform(), CryptoStreamMode.Read))
                using (var lzss = new LzssStream (xored))
                    lzss.Read (m_index, 0, m_index.Length);

                int index_offset = Array.IndexOf (m_index, (byte)0);
                if (-1 == index_offset || 0 == index_offset)
                    return false;
                Password = m_index.Take (index_offset++).ToArray();
                long base_offset = 0x20 + m_packed_size;

                for (int i = 0; i < m_count; ++i)
                {
                    var entry = new PackedEntry();
                    entry.Offset = LittleEndian.ToUInt32 (m_index, index_offset) + base_offset;
                    entry.Size   = LittleEndian.ToUInt32 (m_index, index_offset + 4);
                    entry.UnpackedSize = LittleEndian.ToUInt32 (m_index, index_offset + 8);
                    entry.IsPacked = entry.UnpackedSize != 0;
                    if (!entry.CheckPlacement (m_file.MaxOffset))
                        return false;
                    int name_len = LittleEndian.ToInt32 (m_index, index_offset + 0xC);
                    entry.Name = Encodings.cp932.GetString (m_index, index_offset+0x18, name_len);
                    entry.Type = FormatCatalog.Instance.GetTypeFromName (entry.Name);
                    m_dir.Value.Add (entry);
                    index_offset += 0x18 + name_len;
                }
                return true;
            }
示例#7
0
            bool ReadV0(Stream input)
            {
                long current_offset = 0x20 + m_packed_size;
                uint offset_table_size = (uint)m_count * 0x10;
                if (offset_table_size > m_file.View.Reserve (current_offset, offset_table_size))
                    return false;

                using (var lzss = new LzssStream (input, LzssMode.Decompress, true))
                    lzss.Read (m_index, 0, m_index.Length);

                for (int i = 0; i < m_index.Length; ++i)
                {
                    m_index[i] = (byte)(~m_index[i] - 5);
                }
                int index_offset = Array.IndexOf (m_index, (byte)0);
                if (-1 == index_offset || 0 == index_offset)
                    return false;
                index_offset++;
                //                Password = m_index.Take (index_offset++).ToArray();

                for (int i = 0; i < m_count && index_offset < m_index.Length; ++i)
                {
                    int name_end = Array.IndexOf (m_index, (byte)0, index_offset);
                    if (-1 == name_end)
                        name_end = m_index.Length;
                    if (index_offset == name_end)
                        return false;
                    var entry = new PackedEntry();
                    entry.Offset = m_file.View.ReadUInt32 (current_offset);
                    entry.Size   = m_file.View.ReadUInt32 (current_offset+4);
                    entry.UnpackedSize = m_file.View.ReadUInt32 (current_offset+8);
                    entry.IsPacked = entry.UnpackedSize != 0;
                    if (!entry.CheckPlacement (m_file.MaxOffset))
                        return false;
                    entry.Name = Encodings.cp932.GetString (m_index, index_offset, name_end-index_offset);
                    entry.Type = FormatCatalog.Instance.GetTypeFromName (entry.Name);
                    m_dir.Value.Add (entry);
                    index_offset = name_end+1;
                    current_offset += 0x10;
                }
                return true;
            }
示例#8
0
文件: ImageGPS.cs 项目: Casidi/GARbro
 Stream OpenGpsStream(Stream input, byte compression, int unpacked_size)
 {
     if (0 == compression)
         return new StreamRegion (input, 0x29, true);
     else if (1 == compression)
         return OpenRLEStream (input, unpacked_size);
     else if (2 == compression)
         return new LzssStream (input, LzssMode.Decompress, true);
     else if (3 == compression)
     {
         using (var lzss = new LzssStream (input, LzssMode.Decompress, true))
             return OpenRLEStream (lzss, unpacked_size);
     }
     else
         throw new InvalidFormatException();
 }
示例#9
0
文件: ImageGRP.cs 项目: Casidi/GARbro
        // Exile ~Blood Royal 2~      : flipped == true
        // Gakuen ~Nerawareta Chitai~ : flipped == false
        private ImageData ReadV3(bool flipped)
        {
            using (var lzs = new LzssStream (m_input, LzssMode.Decompress, true))
            {
                if (8 == m_info.BPP)
                {
                    var palette_data = new byte[0x400];
                    if (palette_data.Length != lzs.Read (palette_data, 0, palette_data.Length))
                        throw new InvalidFormatException ("Unexpected end of file");
                    SetPalette (palette_data);
                }
                m_image_data = new byte[m_stride * (int)m_info.Height];
                if (m_image_data.Length != lzs.Read (m_image_data, 0, m_image_data.Length))
                    throw new InvalidFormatException();

                if (flipped)
                    return ImageData.CreateFlipped (m_info, Format, Palette, m_image_data, m_stride);
                else
                    return ImageData.Create (m_info, Format, Palette, m_image_data, m_stride);
            }
        }
示例#10
0
文件: ArcEME.cs 项目: Casidi/GARbro
 Stream OpenScript(EmeArchive arc, EmEntry entry)
 {
     var header = arc.File.View.ReadBytes (entry.Offset, 12);
     Decrypt (header, 0, 12, arc.Key);
     if (0 == entry.LzssFrameSize)
     {
         var input = arc.File.CreateStream (entry.Offset+12, entry.Size);
         return new PrefixStream (header, input);
     }
     int unpacked_size = LittleEndian.ToInt32 (header, 4);
     if (0 != unpacked_size)
     {
         uint packed_size = LittleEndian.ToUInt32 (header, 0);
         int part1_size = (int)entry.UnpackedSize - unpacked_size;
         var data = new byte[entry.UnpackedSize];
         using (var input = arc.File.CreateStream (entry.Offset+12+packed_size, entry.Size))
         using (var lzss = new LzssStream (input))
         {
             lzss.Config.FrameSize = entry.LzssFrameSize;
             lzss.Config.FrameInitPos = entry.LzssInitPos;
             lzss.Read (data, 0, part1_size);
         }
         using (var input = arc.File.CreateStream (entry.Offset+12, packed_size))
         using (var lzss = new LzssStream (input))
         {
             lzss.Config.FrameSize = entry.LzssFrameSize;
             lzss.Config.FrameInitPos = entry.LzssInitPos;
             lzss.Read (data, part1_size, unpacked_size);
         }
         return new MemoryStream (data);
     }
     else
     {
         var input = arc.File.CreateStream (entry.Offset+12, entry.Size);
         var lzss = new LzssStream (input);
         lzss.Config.FrameSize = entry.LzssFrameSize;
         lzss.Config.FrameInitPos = entry.LzssInitPos;
         return lzss;
     }
 }