Пример #1
0
        public void Extract(string FilePath, int BitmapIndex)
        {
            // Create a new dds file
            FileStream   fs = new FileStream(FilePath, FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);

            BitmapSubmap sm = bitmapList[BitmapIndex];

            // Create a new dds header with our texture info and write it
            new DDS(sm).Write(bw);

            // Setup our new sizes for this level
            int height = sm.vHeight;
            int width  = sm.vWidth;

            // Untile the data
            byte[] data = DXTDecoder.ConvertToLinearTexture(GetBitmapData(BitmapIndex),
                                                            width, height, sm.Format);

            // Now endian swap all the data if its DXT
            if (sm.Format != TextureFormat.A8R8G8B8)
            {
                for (int i = 0; i < data.Length; i += 2)
                {
                    Array.Reverse(data, i, 2);
                }
            }

            // Now lets write it out
            bw.Write(data);

            // Close our new dds
            bw.Close();
        }
Пример #2
0
        public Bitmap GeneratePreview(int BitmapIndex)
        {
            BitmapSubmap bitmapData = bitmapList[BitmapIndex];

            // Make sure this format can be previewed
            if (!bitmapData.IsSupported)
            {
                Bitmap   bitmap = new Bitmap(200, 200);
                Graphics g      = Graphics.FromImage(bitmap);
                g.Clear(Color.CornflowerBlue);
                string drawString = "No Preview Available";
                SizeF  size       = g.MeasureString(drawString, new Font(FontFamily.GenericSerif, 15f));
                g.DrawString(drawString, new Font(FontFamily.GenericSerif, 15f),
                             Brushes.Black,
                             new PointF(100 - (size.Width / 2), 100 - (size.Height / 2)));
                return(bitmap);
            }

            // Setup our new sizes for this level
            int height = bitmapData.vHeight;
            int width  = bitmapData.vWidth;

            // Convert it to a linear texture
            byte[] data = GetBitmapData(BitmapIndex);
            //File.WriteAllBytes("C:\\normal2.bin", data);

            if (bitmapData.IsDefined)
            {
                data = DXTDecoder.ConvertToLinearTexture(data,
                                                         width, height, bitmapData.Format);
            }

            // Decode the dxt
            switch (bitmapData.Format)
            {
            case TextureFormat.A8:
                data = DXTDecoder.DecodeA8(data, width, height);
                break;

            case TextureFormat.Y8:
                data = DXTDecoder.DecodeY8(data, width, height);
                break;

            case TextureFormat.AY8:
                data = DXTDecoder.DecodeAY8(data, width, height);
                break;

            case TextureFormat.A8Y8:
                data = DXTDecoder.DecodeA8Y8(data, width, height);
                break;

            case TextureFormat.R5G6B5:
                data = DXTDecoder.DecodeR5G6B5(data, width, height);
                break;

            case TextureFormat.A1R5G5B5:
                data = DXTDecoder.DecodeA1R5G5B5(data, width, height);
                break;

            case TextureFormat.A4R4G4B4:
                data = DXTDecoder.DecodeA4R4G4B4(data, width, height);
                break;

            case TextureFormat.X8R8G8B8:
            case TextureFormat.A8R8G8B8:
                // Do nothing its fine how it is
                break;

            case TextureFormat.DXT1:
                data = DXTDecoder.DecodeDXT1(data, width, height);
                break;

            case TextureFormat.DXT3:
                data = DXTDecoder.DecodeDXT3(data, width, height);
                break;

            case TextureFormat.DXT5:
                data = DXTDecoder.DecodeDXT5(data, width, height);
                break;

            case TextureFormat.DXN:
                data = DXTDecoder.DecodeDXN(data, width, height);
                break;

            case TextureFormat.CTX1:
                data = DXTDecoder.DecodeCTX1(data, width, height);
                break;

            default:
                return(null);
                // throw new ArgumentOutOfRangeException();
            }

            // Now lets create a bitmap from it
            Bitmap     bmp     = new Bitmap(bitmapData.Width, bitmapData.Height, PixelFormat.Format32bppArgb);
            Rectangle  rect    = new Rectangle(0, 0, bitmapData.Width, bitmapData.Height);
            BitmapData bmpdata = bmp.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

            // Loop and copy what we have removing any padding
            byte[] newData1 = new byte[bitmapData.Width * bitmapData.Height * 4];
            for (int x = 0; x < bitmapData.Height; x++)
            {
                Array.Copy(data, x * width * 4, newData1, x * bitmapData.Width * 4, bitmapData.Width * 4);
            }

            Marshal.Copy(newData1, 0, bmpdata.Scan0, newData1.Length);
            bmp.UnlockBits(bmpdata);

            // Return our image in our pic box now
            return(bmp);
        }