Esempio n. 1
0
        public static BMAP Load(string path, bool dump = false)
        {
            BMAP bmap = new BMAP();

            byte[] buff = new byte[536870912];
            int    size = 0;

            using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(path)))
                using (BinaryReader br = new BinaryReader(ms))
                {
                    if (!IsBMAP(br))
                    {
                        return(null);
                    }

                    while (br.BaseStream.Position < br.BaseStream.Length)
                    {
                        int length = (int)br.ReadUInt32();

                        using (MemoryStream lzs = new MemoryStream(br.ReadBytes(length)))
                            using (LZ4Decompress lz4 = new LZ4Decompress(lzs))
                            {
                                size += lz4.Read(buff, size, buff.Length);
                            }
                    }
                }

            if (dump)
            {
                using (BinaryWriter bw = new BinaryWriter(new FileStream(path.Replace(".bmap", ".raw"), FileMode.Create)))
                {
                    bw.Write(buff, 0, size);
                }
            }

            using (MemoryStream ms = new MemoryStream(buff, 0, size))
                using (BinaryReader br = new BinaryReader(ms))
                {
                    bmap.Mode = (int)br.ReadUInt32();
                    bmap.Path = br.ReadString((int)br.ReadUInt32());
                    int dataSize = (int)br.ReadUInt32();

                    switch (bmap.Mode)
                    {
                    case 0:
                        bmap.DDS = DDS.Load(br.ReadBytes(dataSize));
                        break;

                    case 1:
                        br.ReadUInt16();
                        br.ReadUInt16();
                        br.ReadUInt32();
                        br.ReadUInt32();
                        bmap.Raw = new Bitmap(br.ReadUInt16(), br.ReadUInt16(), PixelFormat.Format32bppArgb);
                        br.ReadNullTerminatedString();

                        BitmapData bmpdata = bmap.Raw.LockBits(new Rectangle(0, 0, bmap.Raw.Width, bmap.Raw.Height), ImageLockMode.ReadWrite, bmap.Raw.PixelFormat);
                        dataSize = (int)(br.BaseStream.Length - br.BaseStream.Position);
                        Marshal.Copy(br.ReadBytes(dataSize), 0, bmpdata.Scan0, dataSize);
                        break;
                    }
                }

            return(bmap);
        }
Esempio n. 2
0
        public static DDS Load(byte[] data)
        {
            DDS dds = new DDS();

            using (MemoryStream ms = new MemoryStream(data))
                using (BinaryReader br = new BinaryReader(ms))
                {
                    if (!IsDDS(br))
                    {
                        return(null);
                    }

                    dds.pixelFormat = new DDSPixelFormat();

                    br.ReadUInt32(); // header length
                    dds.flags  = (Flags)br.ReadUInt32();
                    dds.height = (int)br.ReadUInt32();
                    dds.width  = (int)br.ReadUInt32();
                    dds.pitch  = (int)br.ReadUInt32();
                    dds.depth  = (int)br.ReadUInt32();
                    int mipCount = (int)br.ReadUInt32();
                    for (int i = 0; i < 11; i++)
                    {
                        br.ReadUInt32();
                    }
                    br.ReadUInt32(); // pixel format length
                    dds.pixelFormat.Flags       = (PixelFormatFlags)br.ReadUInt32();
                    dds.pixelFormat.FourCC      = (PixelFormatFourCC)br.ReadUInt32();
                    dds.pixelFormat.RGBBitCount = (int)br.ReadUInt32();
                    dds.pixelFormat.RBitMask    = br.ReadUInt32();
                    dds.pixelFormat.GBitMask    = br.ReadUInt32();
                    dds.pixelFormat.BBitMask    = br.ReadUInt32();
                    dds.pixelFormat.ABitMask    = br.ReadUInt32();
                    dds.caps  = (DDSCaps)br.ReadUInt32();
                    dds.caps2 = (DDSCaps2)br.ReadUInt32();
                    br.ReadUInt32();
                    br.ReadUInt32();
                    br.ReadUInt32();

                    if (dds.pixelFormat.Flags.HasFlag(PixelFormatFlags.DDPF_FOURCC))
                    {
                        dds.format = (D3DFormat)dds.pixelFormat.FourCC;
                    }
                    else if (dds.pixelFormat.Flags.HasFlag(PixelFormatFlags.DDPF_RGB) & dds.pixelFormat.Flags.HasFlag(PixelFormatFlags.DDPF_ALPHAPIXELS))
                    {
                        dds.format = D3DFormat.A8R8G8B8;
                    }

                    for (int i = 0; i < Math.Max(1, mipCount); i++)
                    {
                        var mip = new MipMap
                        {
                            Width  = dds.width >> i,
                            Height = dds.height >> i
                        };

                        switch (dds.format)
                        {
                        case D3DFormat.A8R8G8B8:
                            mip.Data = br.ReadBytes(mip.Width * mip.Height * 4);
                            break;

                        case D3DFormat.DXT1:
                            mip.Data = br.ReadBytes((((mip.Width + 3) / 4) * ((mip.Height + 3) / 4)) * 8);
                            break;

                        case D3DFormat.DXT3:
                        case D3DFormat.DXT5:
                            mip.Data = br.ReadBytes((((mip.Width + 3) / 4) * ((mip.Height + 3) / 4)) * 16);
                            break;
                        }

                        dds.mipMaps.Add(mip);
                    }
                }

            return(dds);
        }
Esempio n. 3
0
        public static void Save(BinaryWriter bw, DDS dds)
        {
            Flags flags = (Flags.Caps | Flags.Height | Flags.Width | Flags.PixelFormat | Flags.MipMapCount);
            flags |= (dds.format == D3DFormat.A8R8G8B8 ? Flags.Pitch : Flags.LinearSize);

            bw.Write(new byte[] { 0x44, 0x44, 0x53, 0x20 });    // 'DDS '
            bw.Write(124);
            bw.Write((int)flags);
            bw.Write(dds.Height);
            bw.Write(dds.Width);
            bw.Write((flags.HasFlag(Flags.Pitch) ? dds.width * 4 : dds.MipMaps[0].Data.Length));
            bw.Write(dds.Depth);
            bw.Write(dds.MipMaps.Count);

            for (int i = 0; i < 11; i++) { bw.Write(0); }

            // PixelFormat
            bw.Write(32);

            switch (dds.Format)
            {
                case D3DFormat.DXT1:
                case D3DFormat.DXT3:
                case D3DFormat.DXT5:
                    bw.Write(4);        // fourCC length
                    bw.Write(dds.Format.ToString().ToCharArray());
                    bw.Write(0);
                    bw.Write(0);
                    bw.Write(0);
                    bw.Write(0);
                    bw.Write(0);
                    break;

                default:
                    bw.Write(0);    // fourCC length
                    bw.Write(0);
                    bw.Write(32);   //  RGB bit count
                    bw.Write(255 << 16);    // R mask
                    bw.Write(255 << 8);     // G mask
                    bw.Write(255 << 0);     // B mask
                    bw.Write(255 << 24);    // A mask
                    break;
            }

            bw.Write((int)DDSCaps.DDSCAPS_TEXTURE);
            bw.Write(0);    // Caps 2
            bw.Write(0);    // Caps 3
            bw.Write(0);    // Caps 4
            bw.Write(0);    // Reserved

            for (int i = 0; i < dds.mipMaps.Count; i++)
            {
                bw.Write(dds.mipMaps[i].Data);
            }
        }
Esempio n. 4
0
 public static DDS Load(string path)
 {
     return(DDS.Load(File.ReadAllBytes(path)));
 }
Esempio n. 5
0
        public static DDS Load(byte[] data)
        {
            DDS dds = new DDS();

            using (MemoryStream ms = new MemoryStream(data))
            using (BinaryReader br = new BinaryReader(ms))
            {
                if (!IsDDS(br)) { return null; }

                dds.pixelFormat = new DDSPixelFormat();

                br.ReadUInt32();    // header length
                dds.flags = (Flags)br.ReadUInt32();
                dds.height = (int)br.ReadUInt32();
                dds.width = (int)br.ReadUInt32();
                dds.pitch = (int)br.ReadUInt32();
                dds.depth = (int)br.ReadUInt32();
                int mipCount = (int)br.ReadUInt32();
                for (int i = 0; i < 11; i++) { br.ReadUInt32(); }
                br.ReadUInt32();    // pixel format length
                dds.pixelFormat.Flags = (PixelFormatFlags)br.ReadUInt32();
                dds.pixelFormat.FourCC = (PixelFormatFourCC)br.ReadUInt32();
                dds.pixelFormat.RGBBitCount = (int)br.ReadUInt32();
                dds.pixelFormat.RBitMask = br.ReadUInt32();
                dds.pixelFormat.GBitMask = br.ReadUInt32();
                dds.pixelFormat.BBitMask = br.ReadUInt32();
                dds.pixelFormat.ABitMask = br.ReadUInt32();
                dds.caps = (DDSCaps)br.ReadUInt32();
                dds.caps2 = (DDSCaps2)br.ReadUInt32();
                br.ReadUInt32();
                br.ReadUInt32();
                br.ReadUInt32();

                if (dds.pixelFormat.Flags.HasFlag(PixelFormatFlags.DDPF_FOURCC))
                {
                    dds.format = (D3DFormat)dds.pixelFormat.FourCC;
                }
                else if (dds.pixelFormat.Flags.HasFlag(PixelFormatFlags.DDPF_RGB) & dds.pixelFormat.Flags.HasFlag(PixelFormatFlags.DDPF_ALPHAPIXELS))
                {
                    dds.format = D3DFormat.A8R8G8B8;
                }

                for (int i = 0; i < Math.Max(1, mipCount); i++)
                {
                    var mip = new MipMap
                    {
                        Width = dds.width >> i,
                        Height = dds.height >> i
                    };

                    switch (dds.format)
                    {
                        case D3DFormat.A8R8G8B8:
                            mip.Data = br.ReadBytes(mip.Width * mip.Height * 4);
                            break;

                        case D3DFormat.DXT1:
                            mip.Data = br.ReadBytes((((mip.Width + 3) / 4) * ((mip.Height + 3) / 4)) * 8);
                            break;

                        case D3DFormat.DXT3:
                        case D3DFormat.DXT5:
                            mip.Data = br.ReadBytes((((mip.Width + 3) / 4) * ((mip.Height + 3) / 4)) * 16);
                            break;
                    }

                    dds.mipMaps.Add(mip);
                }
            }

            return dds;
        }