예제 #1
0
                public Bitmap DisplayImage(int mipLevel = 0, int arrayLevel = 0)
                {
                    LoadTexture();

                    Bitmap decomp;

                    if (Format == XTXFormats.XTXImageFormat.BC5S)
                    {
                        return(DDSCompressor.DecompressBC5(mipmaps[0], (int)Width, (int)Height, true));
                    }

                    byte[] d = null;
                    if (IsCompressedFormat(Format))
                    {
                        d = DDSCompressor.DecompressBlock(mipmaps[0], (int)Width, (int)Height, GetCompressedDXGI_FORMAT(Format));
                    }
                    else
                    {
                        d = DDSCompressor.DecodePixelBlock(mipmaps[0], (int)Width, (int)Height, GetUncompressedDXGI_FORMAT(Format));
                    }

                    if (d != null)
                    {
                        decomp = BitmapExtension.GetBitmap(d, (int)Width, (int)Height);
                        return(TextureData.SwapBlueRedChannels(decomp));
                    }
                    return(null);
                }
예제 #2
0
        public static Bitmap DecodeBlock(byte[] data, uint Width, uint Height, GX2SurfaceFormat Format)
        {
            Bitmap decomp;

            try
            {
                if (Format == GX2SurfaceFormat.T_BC5_SNorm)
                {
                    return(DDSCompressor.DecompressBC5(data, (int)Width, (int)Height, true));
                }

                byte[] d = null;
                if (IsCompressedFormat(Format))
                {
                    d = DDSCompressor.DecompressBlock(data, (int)Width, (int)Height, GetCompressedDXGI_FORMAT(Format));
                }
                else
                {
                    d = DDSCompressor.DecodePixelBlock(data, (int)Width, (int)Height, GetUncompressedDXGI_FORMAT(Format));
                }

                if (d != null)
                {
                    decomp = BitmapExtension.GetBitmap(d, (int)Width, (int)Height);
                    return(SwapBlueRedChannels(decomp));
                }
                return(BitmapExtension.GetBitmap(d, (int)Width, (int)Height));;
            }
            catch
            {
                throw new Exception($"Bad size from format {Format}");
            }
        }
예제 #3
0
            public static Bitmap DecodeBlock(byte[] data, uint Width, uint Height, NUTEXImageFormat Format)
            {
                Bitmap decomp;

                if (Format == NUTEXImageFormat.BC5_SNORM)
                {
                    return(DDSCompressor.DecompressBC5(data, (int)Width, (int)Height, true));
                }

                byte[] d = null;
                if (IsCompressedFormat(Format))
                {
                    d = DDSCompressor.DecompressBlock(data, (int)Width, (int)Height, GetCompressedDXGI_FORMAT(Format));
                }
                else
                {
                    d = DDSCompressor.DecodePixelBlock(data, (int)Width, (int)Height, GetUncompressedDXGI_FORMAT(Format));
                }

                if (d != null)
                {
                    decomp = BitmapExtension.GetBitmap(d, (int)Width, (int)Height);
                    return(TextureData.SwapBlueRedChannels(decomp));
                }
                return(null);
            }
예제 #4
0
        private byte[] DecodeWithoutOpenGLDecoder(byte[] ImageData, uint width, uint height, STGenericTexture GenericTexture)
        {
            if (!UseOpenGLDecoder)
            {
                return(STGenericTexture.ConvertBgraToRgba(
                           STGenericTexture.DecodeBlock(ImageData,
                                                        width,
                                                        height,
                                                        GenericTexture.Format,
                                                        GenericTexture.GetPaletteData(),
                                                        GenericTexture.Parameters,
                                                        GenericTexture.PaletteFormat,
                                                        GenericTexture.PlatformSwizzle)));
            }

            switch (GenericTexture.Format)
            {
            case TEX_FORMAT.BC1_UNORM:
            case TEX_FORMAT.BC1_UNORM_SRGB:
            case TEX_FORMAT.BC2_UNORM:
            case TEX_FORMAT.BC2_UNORM_SRGB:
            case TEX_FORMAT.BC3_UNORM:
            case TEX_FORMAT.BC3_UNORM_SRGB:
            case TEX_FORMAT.BC5_UNORM:
            case TEX_FORMAT.BC6H_SF16:
            case TEX_FORMAT.BC6H_UF16:
            case TEX_FORMAT.BC7_UNORM:
            case TEX_FORMAT.BC7_UNORM_SRGB:
            case TEX_FORMAT.R8G8B8A8_UNORM:
            case TEX_FORMAT.R8G8B8A8_UNORM_SRGB:
                return(ImageData);

            case TEX_FORMAT.BC5_SNORM:
                return(DDSCompressor.DecompressBC5(ImageData,
                                                   (int)width, (int)height, true, true));

            default:
                if (Runtime.UseDirectXTexDecoder)
                {
                    return(STGenericTexture.ConvertBgraToRgba(
                               STGenericTexture.DecodeBlock(ImageData,
                                                            width,
                                                            height,
                                                            GenericTexture.Format,
                                                            GenericTexture.GetPaletteData(),
                                                            GenericTexture.Parameters,
                                                            GenericTexture.PaletteFormat,
                                                            GenericTexture.PlatformSwizzle)));
                }
                else
                {
                    return(ImageData);
                }
            }
        }
        public void SetupSettings()
        {
            if (SelectedTexSettings.Format == SurfaceFormat.Invalid)
            {
                return;
            }


            WidthLabel.Text  = $"Width {SelectedTexSettings.TexWidth}";
            HeightLabel.Text = $"Height {SelectedTexSettings.TexHeight}";


            if (formatComboBox.SelectedItem is SurfaceFormat)
            {
                SelectedTexSettings.Format = (SurfaceFormat)formatComboBox.SelectedItem;
                listViewCustom1.SelectedItems[0].SubItems[1].Text = SelectedTexSettings.Format.ToString();
            }
            Bitmap bitmap = Switch_Toolbox.Library.Imaging.GetLoadingImage();


            Thread = new Thread((ThreadStart)(() =>
            {
                ToggleOkButton(false);

                pictureBox1.Image = bitmap;
                SelectedTexSettings.Compress();

                ToggleOkButton(true);

                if (SelectedTexSettings.Format == SurfaceFormat.BC5_SNORM)
                {
                    bitmap = DDSCompressor.DecompressBC5(SelectedTexSettings.DataBlockOutput[0],
                                                         (int)SelectedTexSettings.TexWidth, (int)SelectedTexSettings.TexHeight, true);
                }
                else
                {
                    bitmap = TextureData.DecodeBlockGetBitmap(SelectedTexSettings.DataBlockOutput[0],
                                                              SelectedTexSettings.TexWidth, SelectedTexSettings.TexHeight, TextureData.ConvertFormat(SelectedTexSettings.Format));
                }

                if (pictureBox1.InvokeRequired)
                {
                    pictureBox1.Invoke((MethodInvoker) delegate {
                        pictureBox1.Image = bitmap;
                        pictureBox1.Refresh();
                    });
                }
            }));
            Thread.Start();
        }
예제 #6
0
        public void SetBitmap(Bitmap bitmap)
        {
            Header.Width  = (ushort)bitmap.Width;
            Header.Height = (ushort)(bitmap.Height / Header.TileCount);

            var ddsPF = DDSHelper.ConvertFromDDSAtlus(Header.PixelFormat);

            if (ddsPF == DDSFourCC.NONE)
            {
                var PF = DDSHelper.DDSAtlusToPixelFormat(Header.PixelFormat);
                if (bitmap.PixelFormat != PF)
                {
                    bitmap = bitmap.ConvertTo(PF, null);
                }
                dataList[0] = bitmap.CopyData();

                if (dataList.Count > 1)
                {
                }
            }
            else
            {
                DDSCompressor.DDSCompress(bitmap, ddsPF, out byte[] newData);
                dataList[0] = newData;

                if (dataList.Count > 1)
                {
                    AuxiliaryLibraries.Media.Processing.Scale.Lanczos lanczos = new AuxiliaryLibraries.Media.Processing.Scale.Lanczos();
                    Bitmap temp = bitmap;
                    for (int i = 1; i < dataList.Count; i++)
                    {
                        temp = lanczos.imageScale(temp, 0.5f, 0.5f);
                        DDSCompressor.DDSCompress(temp.Width, temp.Height, temp.CopyData(), ddsPF, out newData);
                        dataList[i] = newData;
                    }
                }
            }

            Header.SizeTexture  = dataList.Sum(x => x.Length);
            Header.SizeWOHeader = Header.SizeTexture + (LastBlock == null ? 0 : LastBlock.Length);

            this.bitmap = null;
        }
        public void SetupSettings(TextureImporterSettings setting)
        {
            if (setting.Format == SurfaceFormat.Invalid || SelectedIndex == -1)
            {
                return;
            }


            WidthLabel.Text  = $"Width: {setting.TexWidth}";
            HeightLabel.Text = $"Height: {setting.TexHeight}";

            if (Thread != null && Thread.IsAlive)
            {
                Thread.Abort();
            }

            if (formatComboBox.SelectedItem is SurfaceDim)
            {
                setting.SurfaceDim = (SurfaceDim)formatComboBox.SelectedItem;
            }


            if (formatComboBox.SelectedItem is SurfaceFormat)
            {
                setting.Format = (SurfaceFormat)formatComboBox.SelectedItem;

                listViewCustom1.Items[SelectedIndex].SubItems[1].Text = setting.Format.ToString();
            }

            if (setting.Format == SurfaceFormat.BC7_UNORM ||
                setting.Format == SurfaceFormat.BC7_SRGB)
            {
                compressionModeCB.Visible = true;
                compModeLbl.Visible       = true;
            }
            else
            {
                compressionModeCB.Visible = false;
                compModeLbl.Visible       = false;
            }

            Bitmap bitmap = Toolbox.Library.Imaging.GetLoadingImage();

            if (compressionModeCB.SelectedIndex == 0)
            {
                CompressionMode = STCompressionMode.Fast;
            }
            else
            {
                CompressionMode = STCompressionMode.Normal;
            }

            Thread = new Thread((ThreadStart)(() =>
            {
                setting.IsFinishedCompressing = false;
                ToggleOkButton(false);

                pictureBox1.Image = bitmap;

                var mips = setting.GenerateMipList(CompressionMode);
                setting.DataBlockOutput.Clear();
                setting.DataBlockOutput.Add(Utils.CombineByteArray(mips.ToArray()));

                ToggleOkButton(true);
                setting.IsFinishedCompressing = true;

                if (setting.DataBlockOutput.Count > 0)
                {
                    if (setting.Format == SurfaceFormat.BC5_SNORM)
                    {
                        bitmap = DDSCompressor.DecompressBC5(mips[0],
                                                             (int)setting.TexWidth, (int)setting.TexHeight, true);
                    }
                    else
                    {
                        bitmap = STGenericTexture.DecodeBlockGetBitmap(mips[0],
                                                                       setting.TexWidth, setting.TexHeight, TextureData.ConvertFormat(setting.Format), new byte[0]);
                    }
                }

                if (pictureBox1.InvokeRequired)
                {
                    pictureBox1.Invoke((MethodInvoker) delegate {
                        pictureBox1.Image = bitmap;
                        pictureBox1.Refresh();

                        int size = Utils.GetSizeInBytes(mips);
                        dataSizeLbl.Text = $"Data Size: {STMath.GetFileSize(size, 5)}";
                    });
                }

                mips.Clear();
            }));
            Thread.Start();
        }
예제 #8
0
        public void SetupSettings()
        {
            if (SelectedTexSettings.Format == TEX_FORMAT.UNKNOWN || SelectedIndex == -1)
            {
                return;
            }


            WidthLabel.Text  = $"Width {SelectedTexSettings.TexWidth}";
            HeightLabel.Text = $"Height {SelectedTexSettings.TexHeight}";

            if (Thread != null && Thread.IsAlive)
            {
                Thread.Abort();
            }

            if (formatComboBox.SelectedItem is TEX_FORMAT)
            {
                SelectedTexSettings.Format = (TEX_FORMAT)formatComboBox.SelectedItem;

                listViewCustom1.Items[SelectedIndex].SubItems[1].Text = SelectedTexSettings.Format.ToString();
            }

            if (SelectedTexSettings.Format == TEX_FORMAT.BC7_UNORM ||
                SelectedTexSettings.Format == TEX_FORMAT.BC7_TYPELESS ||
                SelectedTexSettings.Format == TEX_FORMAT.BC7_UNORM_SRGB)
            {
                compressionModeCB.Visible = true;
            }
            else
            {
                compressionModeCB.Visible = false;
            }

            Bitmap bitmap = Toolbox.Library.Imaging.GetLoadingImage();

            if (compressionModeCB.SelectedIndex == 0)
            {
                CompressionMode = STCompressionMode.Fast;
            }
            else
            {
                CompressionMode = STCompressionMode.Normal;
            }

            Thread = new Thread((ThreadStart)(() =>
            {
                SelectedTexSettings.IsFinishedCompressing = false;
                ToggleOkButton(false);

                pictureBox1.Image = bitmap;

                var mips = SelectedTexSettings.GenerateMipList(CompressionMode, MultiThreading);
                SelectedTexSettings.DataBlockOutput.Clear();
                SelectedTexSettings.DataBlockOutput.Add(Utils.CombineByteArray(mips.ToArray()));

                ToggleOkButton(true);
                SelectedTexSettings.IsFinishedCompressing = true;

                if (SelectedTexSettings.DataBlockOutput.Count > 0)
                {
                    if (SelectedTexSettings.Format == TEX_FORMAT.BC5_SNORM)
                    {
                        bitmap = DDSCompressor.DecompressBC5(mips[0],
                                                             (int)SelectedTexSettings.TexWidth, (int)SelectedTexSettings.TexHeight, true);
                    }
                    else
                    {
                        bitmap = STGenericTexture.DecodeBlockGetBitmap(mips[0],
                                                                       SelectedTexSettings.TexWidth, SelectedTexSettings.TexHeight, SelectedTexSettings.Format, new byte[0]);
                    }
                }

                if (pictureBox1.InvokeRequired)
                {
                    pictureBox1.Invoke((MethodInvoker) delegate {
                        pictureBox1.Image = bitmap;
                        pictureBox1.Refresh();

                        int size = Utils.GetSizeInBytes(mips);
                        dataSizeLbl.Text = $"Data Size: {STMath.GetFileSize(size, 5)}";
                    });
                }

                mips.Clear();
            }));
            Thread.Start();
        }
        public void LoadOpenGLTexture(STGenericTexture GenericTexture)
        {
            if (!Runtime.OpenTKInitialized || GLInitialized || Runtime.UseLegacyGL)
            {
                return;
            }

            if (GenericTexture.ArrayCount <= 0)
            {
                throw new Exception($"No texture data found with texture {GenericTexture.Text}");
            }

            width  = (int)GenericTexture.Width;
            height = (int)GenericTexture.Height;

            data = GenericTexture.GetImageData(0, 0);

            if (data.Length <= 0)
            {
                throw new Exception("Data is empty!");
            }

            switch (GenericTexture.Format)
            {
            case TEX_FORMAT.BC1_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                break;

            case TEX_FORMAT.BC1_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                break;

            case TEX_FORMAT.BC2_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                break;

            case TEX_FORMAT.BC2_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                break;

            case TEX_FORMAT.BC3_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                break;

            case TEX_FORMAT.BC3_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                break;

            case TEX_FORMAT.BC4_UNORM:
            case TEX_FORMAT.BC4_SNORM:
                //Convert to rgb to prevent red output
                //While shaders could prevent this, converting is easier and works fine across all editors
                data = STGenericTexture.DecodeBlock(data,
                                                    GenericTexture.Width,
                                                    GenericTexture.Height,
                                                    GenericTexture.Format,
                                                    GenericTexture.PlatformSwizzle);

                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;

                // pixelInternalFormat = PixelInternalFormat.CompressedRedRgtc1;
                //     pixelInternalFormat = PixelInternalFormat.CompressedSignedRedRgtc1;
                break;

            case TEX_FORMAT.BC5_SNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgRgtc2;

                data = DDSCompressor.DecompressBC5(GenericTexture.GetImageData(0, 0),
                                                   (int)GenericTexture.Width, (int)GenericTexture.Height, true, true);
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            case TEX_FORMAT.BC5_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgRgtc2;
                break;

            case TEX_FORMAT.BC6H_UF16:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbBptcUnsignedFloat;
                break;

            case TEX_FORMAT.BC6H_SF16:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbBptcSignedFloat;
                break;

            case TEX_FORMAT.BC7_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaBptcUnorm;
                break;

            case TEX_FORMAT.BC7_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedSrgbAlphaBptcUnorm;
                break;

            case TEX_FORMAT.R8G8B8A8_UNORM:
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            case TEX_FORMAT.R8G8B8A8_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            default:
                data = STGenericTexture.DecodeBlock(data,
                                                    GenericTexture.Width,
                                                    GenericTexture.Height,
                                                    GenericTexture.Format,
                                                    GenericTexture.PlatformSwizzle);

                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;
            }
            GLInitialized = true;

            TexID = loadImage(this);
        }
예제 #10
0
        private Bitmap DecodeNotDirectXTex(byte[] data, uint Width, uint Height, TEX_FORMAT Format)
        {
            if (Format == TEX_FORMAT.R8G8B8A8_UNORM)
            {
                return(BitmapExtension.GetBitmap(ConvertBgraToRgba(data), (int)Width, (int)Height));
            }
            else if (Format == TEX_FORMAT.R8G8B8A8_UNORM_SRGB)
            {
                return(BitmapExtension.GetBitmap(ConvertBgraToRgba(data), (int)Width, (int)Height));
            }
            else if (Format == TEX_FORMAT.BC1_UNORM)
            {
                return(DDSCompressor.DecompressBC1(data, (int)Width, (int)Height, false));
            }
            else if (Format == TEX_FORMAT.BC1_UNORM_SRGB)
            {
                return(DDSCompressor.DecompressBC1(data, (int)Width, (int)Height, true));
            }
            else if (Format == TEX_FORMAT.BC3_UNORM_SRGB)
            {
                return(DDSCompressor.DecompressBC3(data, (int)Width, (int)Height, false));
            }
            else if (Format == TEX_FORMAT.BC3_UNORM)
            {
                return(DDSCompressor.DecompressBC3(data, (int)Width, (int)Height, true));
            }
            else if (Format == TEX_FORMAT.BC4_UNORM)
            {
                return(DDSCompressor.DecompressBC4(data, (int)Width, (int)Height, false));
            }
            else if (Format == TEX_FORMAT.BC4_SNORM)
            {
                return(DDSCompressor.DecompressBC4(data, (int)Width, (int)Height, true));
            }
            else if (Format == TEX_FORMAT.BC5_UNORM)
            {
                return(DDSCompressor.DecompressBC5(data, (int)Width, (int)Height, false));
            }
            else if (Format == TEX_FORMAT.BC7_UNORM)
            {
                return(BitmapExtension.GetBitmap(CSharpImageLibrary.DDS.Dxt.DecompressBc7(data, (int)Width, (int)Height), (int)Width, (int)Height));
            }
            else if (Format == TEX_FORMAT.BC7_UNORM_SRGB)
            {
                return(BitmapExtension.GetBitmap(CSharpImageLibrary.DDS.Dxt.DecompressBc7(data, (int)Width, (int)Height), (int)Width, (int)Height));
            }
            else
            {
                if (Runtime.UseOpenGL)
                {
                    Runtime.OpenTKInitialized = true;
                    if (RenderableTex == null || !RenderableTex.GLInitialized)
                    {
                        LoadOpenGLTexture();
                    }

                    return(RenderableTex.ToBitmap());
                }
            }
            return(null);
        }
예제 #11
0
        /// <summary>
        /// Decodes a byte array of image data given the source image in bytes, width, height, and DXGI format.
        /// </summary>
        /// <param name="byte[]">The byte array of the image</param>
        /// <param name="Width">The width of the image in pixels.</param>
        /// <param name="Height">The height of the image in pixels.</param>
        /// <param name=" DDS.DXGI_FORMAT">The image format.</param>
        /// <returns>Returns a byte array of decoded data. </returns>
        public static byte[] DecodeBlock(byte[] data, uint Width, uint Height, TEX_FORMAT Format, byte[] paletteData, ImageParameters parameters, PALETTE_FORMAT PaletteFormat = PALETTE_FORMAT.None, PlatformSwizzle PlatformSwizzle = PlatformSwizzle.None)
        {
            if (data == null)
            {
                throw new Exception($"Data is null!");
            }
            if (Format <= 0)
            {
                throw new Exception($"Invalid Format!");
            }
            if (data.Length <= 0)
            {
                throw new Exception($"Data is empty!");
            }
            if (Width <= 0)
            {
                throw new Exception($"Invalid width size {Width}!");
            }
            if (Height <= 0)
            {
                throw new Exception($"Invalid height size {Height}!");
            }

            byte[] imageData  = new byte[0];
            bool   DontSwapRG = false;

            if (PlatformSwizzle == PlatformSwizzle.Platform_3DS)
            {
                imageData  = CTR_3DS.DecodeBlock(data, (int)Width, (int)Height, Format);
                DontSwapRG = true;
            }
            else if (PlatformSwizzle == PlatformSwizzle.Platform_Gamecube)
            {
                imageData = Decode_Gamecube.DecodeData(data, paletteData, Width, Height, Format, PaletteFormat);
            }
            else
            {
                if (Format == TEX_FORMAT.R32G8X24_FLOAT)
                {
                    imageData = DDSCompressor.DecodePixelBlock(data, (int)Width, (int)Height, DDS.DXGI_FORMAT.DXGI_FORMAT_R32G8X24_TYPELESS);
                }

                if (Format == TEX_FORMAT.BC5_SNORM)
                {
                    imageData = DDSCompressor.DecompressBC5(data, (int)Width, (int)Height, true, true);
                }

                if (Format == TEX_FORMAT.L8)
                {
                    return(RGBAPixelDecoder.Decode(data, (int)Width, (int)Height, Format));
                }
                if (Format == TEX_FORMAT.LA8)
                {
                    return(RGBAPixelDecoder.Decode(data, (int)Width, (int)Height, Format));
                }
                if (Format == TEX_FORMAT.R5G5B5A1_UNORM)
                {
                    return(RGBAPixelDecoder.Decode(data, (int)Width, (int)Height, Format));
                }

                if (IsCompressed(Format))
                {
                    imageData = DDSCompressor.DecompressBlock(data, (int)Width, (int)Height, (DDS.DXGI_FORMAT)Format);
                }
                else
                {
                    if (IsAtscFormat(Format))
                    {
                        imageData = ASTCDecoder.DecodeToRGBA8888(data, (int)GetBlockWidth(Format), (int)GetBlockHeight(Format), 1, (int)Width, (int)Height, 1);
                    }
                    else
                    {
                        imageData = DDSCompressor.DecodePixelBlock(data, (int)Width, (int)Height, (DDS.DXGI_FORMAT)Format);
                    }

                    //    imageData = RGBAPixelDecoder.Decode(data, (int)Width, (int)Height, Format);
                }
            }

            if (parameters.DontSwapRG || DontSwapRG)
            {
                return(imageData);
            }
            else
            {
                return(ConvertBgraToRgba(imageData));
            }
        }
예제 #12
0
        public Bitmap GetBitmap(int ArrayLevel = 0, int MipLevel = 0, int DepthLevel = 0)
        {
            uint width  = Math.Max(1, Width >> MipLevel);
            uint height = Math.Max(1, Height >> MipLevel);

            byte[] data        = GetImageData(ArrayLevel, MipLevel, DepthLevel);
            byte[] paletteData = GetPaletteData();
            if (Format == TEX_FORMAT.R8G8B8A8_UNORM)
            {
                return(BitmapExtension.GetBitmap(ConvertBgraToRgba(data), (int)width, (int)height));
            }

            try
            {
                if (data == null)
                {
                    throw new Exception("Data is null!");
                }

                if (PlatformSwizzle == PlatformSwizzle.Platform_3DS)
                {
                    var Image = BitmapExtension.GetBitmap(ConvertBgraToRgba(CTR_3DS.DecodeBlock(data, (int)width, (int)height, Format)),
                                                          (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    Image.RotateFlip(RotateFlipType.RotateNoneFlipY); //It's upside down for some reason so flip it
                    return(Image);
                }

                if (PlatformSwizzle == PlatformSwizzle.Platform_Gamecube)
                {
                    return(BitmapExtension.GetBitmap(Decode_Gamecube.DecodeData(data, paletteData, width, height, Format, PaletteFormat),
                                                     (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));
                }

                switch (Format)
                {
                case TEX_FORMAT.R4G4_UNORM:
                    return(BitmapExtension.GetBitmap(R4G4.Decompress(data, (int)width, (int)height, false),
                                                     (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));

                case TEX_FORMAT.BC5_SNORM:
                    return(DDSCompressor.DecompressBC5(data, (int)width, (int)height, true));

                case TEX_FORMAT.ETC1_UNORM:
                    return(BitmapExtension.GetBitmap(ETC1.ETC1Decompress(data, (int)width, (int)height, false),
                                                     (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));

                case TEX_FORMAT.ETC1_A4:
                    return(BitmapExtension.GetBitmap(ETC1.ETC1Decompress(data, (int)width, (int)height, true),
                                                     (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));

                case TEX_FORMAT.R5G5B5A1_UNORM:
                case TEX_FORMAT.LA8:
                case TEX_FORMAT.L8:
                    return(BitmapExtension.GetBitmap(RGBAPixelDecoder.Decode(data, (int)width, (int)height, Format),
                                                     (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));
                }

                if (Runtime.UseDirectXTexDecoder)
                {
                    return(BitmapExtension.GetBitmap(DecodeBlock(data, width, height, Format, new byte[0], Parameters),
                                                     (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));
                }
                else
                {
                    return(DecodeNotDirectXTex(data, width, height, Format));
                }
            }
            catch (Exception ex)
            {
                /*       Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);
                 *
                 *     try
                 *     {
                 *         return DecodeNotDirectXTex(data, width, height, Format);
                 *     }
                 *     catch
                 *     {
                 *         Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);
                 *     }*/

                return(null);
            }
        }
        public void SetupSettings()
        {
            if (SelectedTexSettings.Format == SurfaceFormat.Invalid || SelectedIndex == -1)
            {
                return;
            }

            WidthLabel.Text  = $"Width {SelectedTexSettings.TexWidth}";
            HeightLabel.Text = $"Height {SelectedTexSettings.TexHeight}";

            if (Thread != null && Thread.IsAlive)
            {
                Thread.Abort();
            }

            if (formatComboBox.SelectedItem is SurfaceFormat)
            {
                SelectedTexSettings.Format = (SurfaceFormat)formatComboBox.SelectedItem;

                listViewCustom1.Items[SelectedIndex].SubItems[1].Text = SelectedTexSettings.Format.ToString();
            }

            if (SelectedTexSettings.Format == SurfaceFormat.BC7_UNORM ||
                SelectedTexSettings.Format == SurfaceFormat.BC7_SRGB)
            {
                compressionModeCB.Visible = true;
            }
            else
            {
                compressionModeCB.Visible = false;
            }

            Bitmap bitmap = Switch_Toolbox.Library.Imaging.GetLoadingImage();

            if (compressionModeCB.SelectedIndex == 0)
            {
                CompressionMode = STCompressionMode.Fast;
            }
            else
            {
                CompressionMode = STCompressionMode.Normal;
            }

            Thread = new Thread((ThreadStart)(() =>
            {
                SelectedTexSettings.IsFinishedCompressing = false;
                ToggleOkButton(false);

                pictureBox1.Image = bitmap;

                var mips = SelectedTexSettings.GenerateMipList(CompressionMode);
                SelectedTexSettings.DataBlockOutput.Clear();
                SelectedTexSettings.DataBlockOutput.Add(Utils.CombineByteArray(mips.ToArray()));

                ToggleOkButton(true);
                SelectedTexSettings.IsFinishedCompressing = true;

                if (SelectedTexSettings.DataBlockOutput.Count > 0)
                {
                    if (SelectedTexSettings.Format == SurfaceFormat.BC5_SNORM)
                    {
                        bitmap = DDSCompressor.DecompressBC5(mips[0],
                                                             (int)SelectedTexSettings.TexWidth, (int)SelectedTexSettings.TexHeight, true);
                    }
                    else
                    {
                        bitmap = STGenericTexture.DecodeBlockGetBitmap(mips[0],
                                                                       SelectedTexSettings.TexWidth, SelectedTexSettings.TexHeight, TextureData.ConvertFormat(SelectedTexSettings.Format));
                    }
                }

                mips.Clear();

                if (pictureBox1.InvokeRequired)
                {
                    pictureBox1.Invoke((MethodInvoker) delegate {
                        pictureBox1.Image = bitmap;
                        pictureBox1.Refresh();
                    });
                }
            }));
            Thread.Start();
        }
예제 #14
0
        public void LoadOpenGLTexture()
        {
            if (OpenTKSharedResources.SetupStatus == OpenTKSharedResources.SharedResourceStatus.Unitialized)
            {
                return;
            }

            switch (format)
            {
            case ((int)GTX.GX2SurfaceFormat.GX2_SURFACE_FORMAT_T_BC1_UNORM):
                renderedTex.pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                break;

            case ((int)GTX.GX2SurfaceFormat.GX2_SURFACE_FORMAT_T_BC1_SRGB):
                renderedTex.pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                break;

            case ((int)GTX.GX2SurfaceFormat.GX2_SURFACE_FORMAT_T_BC2_UNORM):
                renderedTex.pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                break;

            case ((int)GTX.GX2SurfaceFormat.GX2_SURFACE_FORMAT_T_BC2_SRGB):
                renderedTex.pixelInternalFormat = PixelInternalFormat.CompressedSrgbAlphaS3tcDxt3Ext;
                break;

            case ((int)GTX.GX2SurfaceFormat.GX2_SURFACE_FORMAT_T_BC3_UNORM):
                renderedTex.pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                break;

            case ((int)GTX.GX2SurfaceFormat.GX2_SURFACE_FORMAT_T_BC3_SRGB):
                renderedTex.pixelInternalFormat = PixelInternalFormat.CompressedSrgbAlphaS3tcDxt5Ext;
                break;

            case ((int)GTX.GX2SurfaceFormat.GX2_SURFACE_FORMAT_T_BC4_UNORM):
                renderedTex.pixelInternalFormat = PixelInternalFormat.CompressedRedRgtc1;
                break;

            case ((int)GTX.GX2SurfaceFormat.GX2_SURFACE_FORMAT_T_BC4_SNORM):
                renderedTex.pixelInternalFormat = PixelInternalFormat.CompressedSignedRedRgtc1;
                break;

            case ((int)GTX.GX2SurfaceFormat.GX2_SURFACE_FORMAT_T_BC5_UNORM):
                renderedTex.pixelInternalFormat = PixelInternalFormat.CompressedRgRgtc2;
                break;

            case ((int)GTX.GX2SurfaceFormat.GX2_SURFACE_FORMAT_T_BC5_SNORM):
                //OpenTK doesn't load BC5 SNORM textures right so I'll use the same decompress method bntx has
                byte[] fixBC5 = DDSCompressor.DecompressBC5(renderedTex.data, renderedTex.width, renderedTex.height, true, true);
                renderedTex.data = fixBC5;
                renderedTex.pixelInternalFormat = PixelInternalFormat.Rgba;
                renderedTex.pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            case ((int)GTX.GX2SurfaceFormat.GX2_SURFACE_FORMAT_TCS_R8_G8_B8_A8_UNORM):
                renderedTex.pixelInternalFormat = PixelInternalFormat.Rgba;
                renderedTex.pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            case ((int)GTX.GX2SurfaceFormat.GX2_SURFACE_FORMAT_TCS_R8_G8_B8_A8_SRGB):
                renderedTex.pixelInternalFormat = PixelInternalFormat.Rgba;
                renderedTex.pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;
            }
            renderedTex.display = loadImage(renderedTex);
        }
예제 #15
0
        public void LoadOpenGLTexture(STGenericTexture GenericTexture)
        {
            if (!Runtime.OpenTKInitialized || GLInitialized)
            {
                return;
            }

            if (GenericTexture.ArrayCount <= 0)
            {
                throw new Exception($"No texture data found with texture {GenericTexture.Text}");
            }

            width  = (int)GenericTexture.Width;
            height = (int)GenericTexture.Height;

            data = GenericTexture.GetImageData(0, 0);

            if (data.Length <= 0)
            {
                throw new Exception("Data is empty!");
            }

            switch (GenericTexture.Format)
            {
            case TEX_FORMAT.BC1_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                break;

            case TEX_FORMAT.BC1_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                break;

            case TEX_FORMAT.BC2_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                break;

            case TEX_FORMAT.BC2_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                break;

            case TEX_FORMAT.BC3_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                break;

            case TEX_FORMAT.BC3_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                break;

            case TEX_FORMAT.BC4_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRedRgtc1;
                break;

            case TEX_FORMAT.BC4_SNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedSignedRedRgtc1;
                break;

            case TEX_FORMAT.BC5_SNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgRgtc2;

                data = DDSCompressor.DecompressBC5(GenericTexture.GetImageData(0, 0),
                                                   (int)GenericTexture.Width, (int)GenericTexture.Height, true, true);
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            case TEX_FORMAT.BC5_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgRgtc2;
                break;

            case TEX_FORMAT.BC6H_UF16:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbBptcUnsignedFloat;
                break;

            case TEX_FORMAT.BC6H_SF16:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbBptcSignedFloat;
                break;

            case TEX_FORMAT.BC7_UNORM:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaBptcUnorm;
                break;

            case TEX_FORMAT.BC7_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.CompressedSrgbAlphaBptcUnorm;
                break;

            case TEX_FORMAT.R8G8B8A8_UNORM:
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;

            case TEX_FORMAT.R8G8B8A8_UNORM_SRGB:
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;
            }
            GLInitialized = true;

            TexID = loadImage(this);
        }
예제 #16
0
        public void LoadOpenGLTexture(STGenericTexture GenericTexture)
        {
            if (OpenTKSharedResources.SetupStatus == OpenTKSharedResources.SharedResourceStatus.Unitialized)
            {
                return;
            }

            bool IsSNORM = (GenericTexture.FormatType == TEX_FORMAT_TYPE.SNORM);
            bool IsSRGB  = (GenericTexture.FormatType == TEX_FORMAT_TYPE.SRGB);
            bool IsFLOAT = (GenericTexture.FormatType == TEX_FORMAT_TYPE.FLOAT);
            bool IsSINT  = (GenericTexture.FormatType == TEX_FORMAT_TYPE.SINT);

            if (GenericTexture.Surfaces.Count <= 0)
            {
                throw new Exception($"No texture data found with texture {GenericTexture.Text}");
            }

            data   = GenericTexture.Surfaces[0].mipmaps[0];
            width  = (int)GenericTexture.Width;
            height = (int)GenericTexture.Height;

            switch (GenericTexture.Format)
            {
            case TEX_FORMAT.BC1:
                if (GenericTexture.FormatType == TEX_FORMAT_TYPE.SRGB)
                {
                    pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                }
                else
                {
                    pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt1Ext;
                }
                break;

            case TEX_FORMAT.BC2:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt3Ext;
                break;

            case TEX_FORMAT.BC3:
                pixelInternalFormat = PixelInternalFormat.CompressedRgbaS3tcDxt5Ext;
                break;

            case TEX_FORMAT.BC4:
                pixelInternalFormat = PixelInternalFormat.CompressedRedRgtc1;
                break;

            case TEX_FORMAT.BC5:
                if (IsSNORM)
                {
                    pixelInternalFormat = PixelInternalFormat.CompressedRgRgtc2;

                    data = DDSCompressor.DecompressBC5(GenericTexture.Surfaces[0].mipmaps[0],
                                                       (int)GenericTexture.Width, (int)GenericTexture.Height, true, true);
                    pixelInternalFormat = PixelInternalFormat.Rgba;
                    pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                }
                else
                {
                    pixelInternalFormat = PixelInternalFormat.CompressedRgRgtc2;
                }
                break;

            case TEX_FORMAT.BC6:
                if (IsFLOAT)
                {
                    pixelInternalFormat = PixelInternalFormat.CompressedRgbBptcSignedFloat;
                }
                else
                {
                    pixelInternalFormat = PixelInternalFormat.CompressedRgbBptcUnsignedFloat;
                }
                break;

            case TEX_FORMAT.BC7:
                if (IsSRGB)
                {
                    pixelInternalFormat = PixelInternalFormat.CompressedRgbaBptcUnorm;
                }
                else
                {
                    pixelInternalFormat = PixelInternalFormat.CompressedSrgbAlphaBptcUnorm;
                }
                break;

            case TEX_FORMAT.R8_G8_B8_A8:
                pixelInternalFormat = PixelInternalFormat.Rgba;
                pixelFormat         = OpenTK.Graphics.OpenGL.PixelFormat.Rgba;
                break;
            }
            display = loadImage(this);

            GLInitialized = true;
        }