Example #1
0
        public static unsafe byte[] DecompressBlock(Byte[] data, int width, int height, DDS.DXGI_FORMAT format)
        {
            Console.WriteLine(format);
            Console.WriteLine(data.Length);
            Console.WriteLine(width);
            Console.WriteLine(height);

            long inputRowPitch;
            long inputSlicePitch;

            TexHelper.Instance.ComputePitch((DXGI_FORMAT)format, width, height, out inputRowPitch, out inputSlicePitch, CP_FLAGS.NONE);

            DXGI_FORMAT FormatDecompressed;

            if (format.ToString().Contains("SRGB"))
            {
                FormatDecompressed = DXGI_FORMAT.R8G8B8A8_UNORM_SRGB;
            }
            else
            {
                FormatDecompressed = DXGI_FORMAT.R8G8B8A8_UNORM;
            }

            if (data.Length == inputSlicePitch)
            {
                byte *buf;
                buf = (byte *)Marshal.AllocHGlobal((int)inputSlicePitch);
                Marshal.Copy(data, 0, (IntPtr)buf, (int)inputSlicePitch);

                DirectXTexNet.Image inputImage = new DirectXTexNet.Image(
                    width, height, (DXGI_FORMAT)format, inputRowPitch,
                    inputSlicePitch, (IntPtr)buf, null);

                TexMetadata texMetadata = new TexMetadata(width, height, 1, 1, 1, 0, 0,
                                                          (DXGI_FORMAT)format, TEX_DIMENSION.TEXTURE2D);

                ScratchImage scratchImage = TexHelper.Instance.InitializeTemporary(
                    new DirectXTexNet.Image[] { inputImage }, texMetadata, null);

                using (var decomp = scratchImage.Decompress(0, FormatDecompressed))
                {
                    byte[] result = new byte[4 * width * height];
                    Marshal.Copy(decomp.GetImage(0).Pixels, result, 0, result.Length);

                    inputImage = null;
                    scratchImage.Dispose();

                    return(result);
                }
            }
            return(null);
        }
Example #2
0
        private static PixelFormat GetPixelFormatFromDXGI(DDS.DXGI_FORMAT fmt)
        {
            switch (fmt)
            {
            case DDS.DXGI_FORMAT.B5G5R5A1_UNORM:
                return(PixelFormat.B5_G5_R5_A1_UNorm);

            case DDS.DXGI_FORMAT.B8G8R8A8_TYPELESS:
            case DDS.DXGI_FORMAT.B8G8R8A8_UNORM:
            case DDS.DXGI_FORMAT.B8G8R8X8_TYPELESS:
            case DDS.DXGI_FORMAT.B8G8R8X8_UNORM:
                return(PixelFormat.B8_G8_R8_A8_UNorm);

            case DDS.DXGI_FORMAT.B8G8R8A8_UNORM_SRGB:
            case DDS.DXGI_FORMAT.B8G8R8X8_UNORM_SRGB:
                return(PixelFormat.B8_G8_R8_A8_UNorm_SRgb);

            case DDS.DXGI_FORMAT.R8G8B8A8_UNORM_SRGB:
                return(PixelFormat.R8_G8_B8_A8_UNorm_SRgb);

            case DDS.DXGI_FORMAT.R8G8B8A8_UNORM:
            case DDS.DXGI_FORMAT.R8G8B8A8_TYPELESS:
                return(PixelFormat.R8_G8_B8_A8_UNorm);

            case DDS.DXGI_FORMAT.BC1_TYPELESS:
            case DDS.DXGI_FORMAT.BC1_UNORM:
                return(PixelFormat.BC1_Rgba_UNorm);

            case DDS.DXGI_FORMAT.BC1_UNORM_SRGB:
                return(PixelFormat.BC1_Rgba_UNorm_SRgb);

            case DDS.DXGI_FORMAT.BC2_TYPELESS:
            case DDS.DXGI_FORMAT.BC2_UNORM:
                return(PixelFormat.BC2_UNorm);

            case DDS.DXGI_FORMAT.BC2_UNORM_SRGB:
                return(PixelFormat.BC2_UNorm_SRgb);

            case DDS.DXGI_FORMAT.BC3_TYPELESS:
            case DDS.DXGI_FORMAT.BC3_UNORM:
                return(PixelFormat.BC3_UNorm);

            case DDS.DXGI_FORMAT.BC3_UNORM_SRGB:
                return(PixelFormat.BC3_UNorm_SRgb);

            case DDS.DXGI_FORMAT.BC4_TYPELESS:
            case DDS.DXGI_FORMAT.BC4_UNORM:
                return(PixelFormat.BC4_UNorm);

            case DDS.DXGI_FORMAT.BC4_SNORM:
                return(PixelFormat.BC4_SNorm);

            case DDS.DXGI_FORMAT.BC5_TYPELESS:
            case DDS.DXGI_FORMAT.BC5_UNORM:
                return(PixelFormat.BC5_UNorm);

            case DDS.DXGI_FORMAT.BC5_SNORM:
                return(PixelFormat.BC5_SNorm);

            case DDS.DXGI_FORMAT.BC6H_TYPELESS:
            case DDS.DXGI_FORMAT.BC6H_UF16:
                return(PixelFormat.BC6H_UFloat);

            case DDS.DXGI_FORMAT.BC6H_SF16:
                return(PixelFormat.BC6H_SFloat);

            case DDS.DXGI_FORMAT.BC7_TYPELESS:
            case DDS.DXGI_FORMAT.BC7_UNORM:
                return(PixelFormat.BC7_UNorm);

            case DDS.DXGI_FORMAT.BC7_UNORM_SRGB:
                return(PixelFormat.BC7_UNorm_SRgb);

            default:
                throw new Exception($"Unimplemented DXGI Type: {fmt.ToString()}");
            }
        }