示例#1
0
        public static Bitmap DecodeBC3(Texture Tex, int Offset)
        {
            int W = (Tex.Width + 3) / 4;
            int H = (Tex.Height + 3) / 4;

            byte[] Output = new byte[W * H * 64];

            ISwizzle Swizzle = Tex.GetSwizzle();

            for (int Y = 0; Y < H; Y++)
            {
                for (int X = 0; X < W; X++)
                {
                    int IOffs = Offset + Swizzle.GetSwizzleOffset(X, Y);

                    byte[] Tile = BCnDecodeTile(Tex.Data, IOffs + 8, false);

                    byte[] Alpha = new byte[8];

                    Alpha[0] = Tex.Data[IOffs + 0];
                    Alpha[1] = Tex.Data[IOffs + 1];

                    CalculateBC3Alpha(Alpha);

                    int AlphaLow  = IOUtils.Get32(Tex.Data, IOffs + 2);
                    int AlphaHigh = IOUtils.Get16(Tex.Data, IOffs + 6);

                    ulong AlphaCh = (uint)AlphaLow | (ulong)AlphaHigh << 32;

                    int TOffset = 0;

                    for (int TY = 0; TY < 4; TY++)
                    {
                        for (int TX = 0; TX < 4; TX++)
                        {
                            int OOffset = (X * 4 + TX + (Y * 4 + TY) * W * 4) * 4;

                            byte AlphaPx = Alpha[(AlphaCh >> (TY * 12 + TX * 3)) & 7];

                            Output[OOffset + 0] = Tile[TOffset + 0];
                            Output[OOffset + 1] = Tile[TOffset + 1];
                            Output[OOffset + 2] = Tile[TOffset + 2];
                            Output[OOffset + 3] = AlphaPx;

                            TOffset += 4;
                        }
                    }
                }
            }

            return(PixelDecoder.GetBitmap(Output, W * 4, H * 4));
        }
示例#2
0
        public static Bitmap DecodeBC4(Texture Tex, int Offset)
        {
            int W = (Tex.Width + 3) / 4;
            int H = (Tex.Height + 3) / 4;

            byte[] Output = new byte[W * H * 64];

            ISwizzle Swizzle = Tex.GetSwizzle();

            for (int Y = 0; Y < H; Y++)
            {
                for (int X = 0; X < W; X++)
                {
                    int IOffs = Offset + Swizzle.GetSwizzleOffset(X, Y);

                    byte[] Red = new byte[8];

                    Red[0] = Tex.Data[IOffs + 0];
                    Red[1] = Tex.Data[IOffs + 1];

                    CalculateBC3Alpha(Red);

                    int RedLow  = IOUtils.Get32(Tex.Data, IOffs + 2);
                    int RedHigh = IOUtils.Get16(Tex.Data, IOffs + 6);

                    ulong RedCh = (uint)RedLow | (ulong)RedHigh << 32;

                    int TOffset = 0;

                    for (int TY = 0; TY < 4; TY++)
                    {
                        for (int TX = 0; TX < 4; TX++)
                        {
                            int OOffset = (X * 4 + TX + (Y * 4 + TY) * W * 4) * 4;

                            byte RedPx = Red[(RedCh >> (TY * 12 + TX * 3)) & 7];

                            Output[OOffset + 0] = RedPx;
                            Output[OOffset + 1] = RedPx;
                            Output[OOffset + 2] = RedPx;
                            Output[OOffset + 3] = 0xff;

                            TOffset += 4;
                        }
                    }
                }
            }

            return(PixelDecoder.GetBitmap(Output, W * 4, H * 4));
        }
示例#3
0
        public static Bitmap DecodeBC2(Texture Tex, int Offset)
        {
            int W = (Tex.Width + 3) / 4;
            int H = (Tex.Height + 3) / 4;

            byte[] Output = new byte[W * H * 64];

            ISwizzle Swizzle = Tex.GetSwizzle();

            for (int Y = 0; Y < H; Y++)
            {
                for (int X = 0; X < W; X++)
                {
                    int IOffs = Offset + Swizzle.GetSwizzleOffset(X, Y);

                    byte[] Tile = BCnDecodeTile(Tex.Data, IOffs + 8, false);

                    int AlphaLow  = IOUtils.Get32(Tex.Data, IOffs + 0);
                    int AlphaHigh = IOUtils.Get32(Tex.Data, IOffs + 4);

                    ulong AlphaCh = (uint)AlphaLow | (ulong)AlphaHigh << 32;

                    int TOffset = 0;

                    for (int TY = 0; TY < 4; TY++)
                    {
                        for (int TX = 0; TX < 4; TX++)
                        {
                            ulong Alpha = (AlphaCh >> (TY * 16 + TX * 4)) & 0xf;

                            int OOffset = (X * 4 + TX + (Y * 4 + TY) * W * 4) * 4;

                            Output[OOffset + 0] = Tile[TOffset + 0];
                            Output[OOffset + 1] = Tile[TOffset + 1];
                            Output[OOffset + 2] = Tile[TOffset + 2];
                            Output[OOffset + 3] = (byte)(Alpha | (Alpha << 4));

                            TOffset += 4;
                        }
                    }
                }
            }

            return(PixelDecoder.GetBitmap(Output, W * 4, H * 4));
        }
示例#4
0
        public static Bitmap DecodeBC1(Texture Tex, int Offset)
        {
            int W = (Tex.Width + 3) / 4;
            int H = (Tex.Height + 3) / 4;

            byte[] Output = new byte[W * H * 64];

            ISwizzle Swizzle = Tex.GetSwizzle();

            for (int Y = 0; Y < H; Y++)
            {
                for (int X = 0; X < W; X++)
                {
                    int IOffs = Offset + Swizzle.GetSwizzleOffset(X, Y);

                    byte[] Tile = BCnDecodeTile(Tex.Data, IOffs, true);

                    int TOffset = 0;

                    for (int TY = 0; TY < 4; TY++)
                    {
                        for (int TX = 0; TX < 4; TX++)
                        {
                            int OOffset = (X * 4 + TX + (Y * 4 + TY) * W * 4) * 4;

                            Output[OOffset + 0] = Tile[TOffset + 0];
                            Output[OOffset + 1] = Tile[TOffset + 1];
                            Output[OOffset + 2] = Tile[TOffset + 2];
                            Output[OOffset + 3] = Tile[TOffset + 3];

                            TOffset += 4;
                        }
                    }
                }
            }

            return(PixelDecoder.GetBitmap(Output, W * 4, H * 4));
        }
示例#5
0
        static void ExtractTex(Texture Tex, string FileName)
        {
            if (Tex.FormatType >= TextureFormatType.ASTC4x4 &&
                Tex.FormatType <= TextureFormatType.ASTC12x12)
            {
                Console.WriteLine("Extracting " + Tex.Name + " (ASTC)...");

                FileName = FileName.Replace(Path.GetExtension(FileName), ".astc");

                ASTC.Save(Tex, FileName);
            }
            else
            {
                Console.WriteLine("Extracting " + Tex.Name + "...");

                void PrintWarning()
                {
                    Console.ForegroundColor = ConsoleColor.Red;

                    Console.WriteLine("ERROR: Texture " + Tex.Name + " have a unsupported format!");

                    Console.ResetColor();
                }

                if (Tex.Type == TextureType.Cube)
                {
                    int Length = Tex.Data.Length / Tex.ArrayCount;

                    int Offset = 0;

                    string[] CubeFaces = new string[] { "x+", "x-", "y+", "y-", "z+", "z-" };

                    for (int Index = 0; Index < Tex.ArrayCount; Index++)
                    {
                        if (!PixelDecoder.TryDecode(Tex, out Bitmap Img, Offset))
                        {
                            PrintWarning();

                            break;
                        }

                        string Ext = Path.GetExtension(FileName);

                        Img.Save(FileName.Replace(Ext, "." + CubeFaces[Index] + Ext));

                        Offset += Length;
                    }
                }
                else
                {
                    if (Tex.MipmapCount == 1)
                    {
                        if (PixelDecoder.TryDecode(Tex, out Bitmap Img))
                        {
                            Img.Save(FileName);
                        }
                        else
                        {
                            PrintWarning();
                        }
                    }
                    else
                    {
                        for (int Index = 0; Index < Tex.MipmapCount; Index++)
                        {
                            if (!PixelDecoder.TryDecode(Tex, out Bitmap Img, (int)Tex.MipOffsets[Index]))
                            {
                                PrintWarning();

                                break;
                            }

                            string Ext = Path.GetExtension(FileName);

                            Img.Save(FileName.Replace(Ext, "." + Index + "." + Ext));

                            Tex.Width  = Math.Max(Tex.Width >> 1, 1);
                            Tex.Height = Math.Max(Tex.Height >> 1, 1);

                            while (Tex.GetBlockHeight() * 8 > Tex.GetPow2HeightInTexels() && Tex.BlockHeightLog2 > 0)
                            {
                                Tex.BlockHeightLog2--;
                            }
                        }
                    }
                }
            }
        }
示例#6
0
        public static Bitmap DecodeBC5(Texture Tex, int Offset)
        {
            int W = (Tex.Width + 3) / 4;
            int H = (Tex.Height + 3) / 4;

            byte[] Output = new byte[W * H * 64];

            ISwizzle Swizzle = Tex.GetSwizzle();

            for (int Y = 0; Y < H; Y++)
            {
                for (int X = 0; X < W; X++)
                {
                    int IOffs = Offset + Swizzle.GetSwizzleOffset(X, Y);

                    byte[] Red   = new byte[8];
                    byte[] Green = new byte[8];

                    Red[0] = Tex.Data[IOffs + 0];
                    Red[1] = Tex.Data[IOffs + 1];

                    Green[0] = Tex.Data[IOffs + 8];
                    Green[1] = Tex.Data[IOffs + 9];

                    if (Tex.FormatVariant == TextureFormatVar.SNorm)
                    {
                        CalculateBC3AlphaS(Red);
                        CalculateBC3AlphaS(Green);
                    }
                    else
                    {
                        CalculateBC3Alpha(Red);
                        CalculateBC3Alpha(Green);
                    }

                    int RedLow  = IOUtils.Get32(Tex.Data, IOffs + 2);
                    int RedHigh = IOUtils.Get16(Tex.Data, IOffs + 6);

                    int GreenLow  = IOUtils.Get32(Tex.Data, IOffs + 10);
                    int GreenHigh = IOUtils.Get16(Tex.Data, IOffs + 14);

                    ulong RedCh   = (uint)RedLow | (ulong)RedHigh << 32;
                    ulong GreenCh = (uint)GreenLow | (ulong)GreenHigh << 32;

                    int TOffset = 0;

                    if (Tex.FormatVariant == TextureFormatVar.SNorm)
                    {
                        for (int TY = 0; TY < 4; TY++)
                        {
                            for (int TX = 0; TX < 4; TX++)
                            {
                                int Shift = TY * 12 + TX * 3;

                                int OOffset = (X * 4 + TX + (Y * 4 + TY) * W * 4) * 4;

                                byte RedPx   = Red  [(RedCh >> Shift) & 7];
                                byte GreenPx = Green[(GreenCh >> Shift) & 7];

                                if (Tex.FormatVariant == TextureFormatVar.SNorm)
                                {
                                    RedPx   += 0x80;
                                    GreenPx += 0x80;
                                }

                                float NX = (RedPx / 255f) * 2 - 1;
                                float NY = (GreenPx / 255f) * 2 - 1;

                                float NZ = (float)Math.Sqrt(1 - (NX * NX + NY * NY));

                                Output[OOffset + 0] = Clamp((NZ + 1) * 0.5f);
                                Output[OOffset + 1] = Clamp((NY + 1) * 0.5f);
                                Output[OOffset + 2] = Clamp((NX + 1) * 0.5f);
                                Output[OOffset + 3] = 0xff;

                                TOffset += 4;
                            }
                        }
                    }
                    else
                    {
                        for (int TY = 0; TY < 4; TY++)
                        {
                            for (int TX = 0; TX < 4; TX++)
                            {
                                int Shift = TY * 12 + TX * 3;

                                int OOffset = (X * 4 + TX + (Y * 4 + TY) * W * 4) * 4;

                                byte RedPx   = Red  [(RedCh >> Shift) & 7];
                                byte GreenPx = Green[(GreenCh >> Shift) & 7];

                                Output[OOffset + 0] = RedPx;
                                Output[OOffset + 1] = RedPx;
                                Output[OOffset + 2] = RedPx;
                                Output[OOffset + 3] = GreenPx;

                                TOffset += 4;
                            }
                        }
                    }
                }
            }

            return(PixelDecoder.GetBitmap(Output, W * 4, H * 4));
        }