Esempio n. 1
0
        public static Tuple <SurfaceFormat, SurfaceFormatType> GetSurfaceFormat(TEX_FORMAT format, TEX_FORMAT_TYPE type)
        {
            var surfaceFormat = SurfaceFormat.Invalid;
            var surfaceType   = SurfaceFormatType.UNORM;

            Enum.TryParse(format.ToString(), out surfaceFormat);
            Enum.TryParse(type.ToString(), out surfaceType);

            return(Tuple.Create(surfaceFormat, surfaceType));
        }
Esempio n. 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 (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 (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));
                }
                else 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)));
                }
            }
        }
Esempio n. 3
0
 public static bool IsAtscFormat(TEX_FORMAT Format)
 {
     if (Format.ToString().Contains("ASTC"))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Esempio n. 4
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)));
                }
            }
        }