/// <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, 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}!");
            }

            if (PlatformSwizzle == PlatformSwizzle.Platform_3DS && !IsCompressed(Format))
            {
                return(CTR_3DS.DecodeBlock(data, (int)Width, (int)Height, Format));
            }

            if (Format == TEX_FORMAT.R32G8X24_FLOAT)
            {
                return(ConvertBgraToRgba(DDSCompressor.DecodePixelBlock(data, (int)Width, (int)Height, DDS.DXGI_FORMAT.DXGI_FORMAT_R32G8X24_TYPELESS)));
            }

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

            if (IsCompressed(Format))
            {
                return(ConvertBgraToRgba(DDSCompressor.DecompressBlock(data, (int)Width, (int)Height, (DDS.DXGI_FORMAT)Format)));
            }
            else
            {
                //If blue channel becomes first, do not swap them!
                //      if (Format.ToString().StartsWith("B") || Format == TEX_FORMAT.B5G6R5_UNORM)
                //       return DDSCompressor.DecodePixelBlock(data, (int)Width, (int)Height, (DDS.DXGI_FORMAT)Format);
                if (IsAtscFormat(Format))
                {
                    return(ConvertBgraToRgba(ASTCDecoder.DecodeToRGBA8888(data, (int)GetBlockWidth(Format), (int)GetBlockHeight(Format), 1, (int)Width, (int)Height, 1)));
                }
                else
                {
                    return(ConvertBgraToRgba(DDSCompressor.DecodePixelBlock(data, (int)Width, (int)Height, (DDS.DXGI_FORMAT)Format)));
                }
            }
        }
Exemple #2
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)
        {
            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}!");
            }

            if (Format == TEX_FORMAT.BC5)
            {
                return(ConvertBgraToRgba(DDSCompressor.DecompressBC5(data, (int)Width, (int)Height, true, true)));
            }

            if (IsCompressed(Format))
            {
                return(ConvertBgraToRgba(DDSCompressor.DecompressBlock(data, (int)Width, (int)Height, (DDS.DXGI_FORMAT)Format)));
            }
            else
            {
                //If blue channel becomes first, do not swap them
                if (Format.ToString().Contains("FORMAT_B"))
                {
                    return(DDSCompressor.DecodePixelBlock(data, (int)Width, (int)Height, (DDS.DXGI_FORMAT)Format));
                }
                else
                {
                    return(ConvertBgraToRgba(DDSCompressor.DecodePixelBlock(data, (int)Width, (int)Height, (DDS.DXGI_FORMAT)Format)));
                }
            }
        }
        /// <summary>
        /// Gets a <see cref="Bitmap"/> given an array and mip index.
        /// </summary>
        /// <param name="ArrayIndex">The index of the surface/array. Cubemaps will have 6</param>
        /// <param name="MipLevel">The index of the mip level.</param>
        /// <returns></returns>
        public Bitmap GetBitmap(int ArrayLevel = 0, int MipLevel = 0)
        {
            uint width  = Math.Max(1, Width >> MipLevel);
            uint height = Math.Max(1, Height >> MipLevel);

            byte[] data = GetImageData(ArrayLevel, MipLevel);

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

                if (PlatformSwizzle == PlatformSwizzle.Platform_3DS && !IsCompressed(Format))
                {
                    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);
                }

                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:
                    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));

                default:
                    return(BitmapExtension.GetBitmap(DecodeBlock(data, width, height, Format),
                                                     (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb));
                }
            }
            catch (Exception ex)
            {
                Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);

                try
                {
                    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 (Runtime.UseOpenGL)
                        {
                            Runtime.OpenTKInitialized = true;
                            LoadOpenGLTexture();
                            return(RenderableTex.GLTextureToBitmap(RenderableTex));
                        }
                    }
                }
                catch
                {
                    Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);
                }

                return(null);
            }
        }
        /// <summary>
        /// Gets a <see cref="Bitmap"/> given an array and mip index.
        /// </summary>
        /// <param name="ArrayIndex">The index of the surface/array. Cubemaps will have 6</param>
        /// <param name="MipLevel">The index of the mip level.</param>
        /// <returns></returns>
        public Bitmap GetBitmap(int ArrayLevel = 0, int MipLevel = 0)
        {
            uint width  = Math.Max(1, Width >> MipLevel);
            uint height = Math.Max(1, Height >> MipLevel);

            byte[] data = GetImageData(ArrayLevel, MipLevel);

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

                if (Format == TEX_FORMAT.BC5_SNORM)
                {
                    return(DDSCompressor.DecompressBC5(data, (int)width, (int)height, true));
                }

                Bitmap bitmap = BitmapExtension.GetBitmap(DecodeBlock(data, width, height, Format),
                                                          (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                return(bitmap);
            }
            catch (Exception ex)
            {
                Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);

                try
                {
                    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
                    {
                        Runtime.OpenTKInitialized = true;
                        LoadOpenGLTexture();
                        return(RenderableTex.GLTextureToBitmap(RenderableTex));
                    }
                }
                catch
                {
                    Forms.STErrorDialog.Show($"Texture failed to load!", "Texture [GetBitmap({MipLevel},{ArrayLevel})]", DebugInfo() + " \n" + ex);
                }

                return(null);
            }



            /*       try
             *     {
             *         Bitmap bitmap = BitmapExtension.GetBitmap(DecodeBlock(data, width, height, Format),
             *             (int)width, (int)height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
             *
             *         return bitmap;
             *     }
             *     catch (Exception ex)
             *     {
             *        MessageBox.Show($"Failed to texture {Text} \n{DebugInfo()}\n {ex.ToString()}");
             *         return null;
             *     }*/
        }