示例#1
0
		/// <summary>
		/// Gets the directX pixelformat for the specified pixelformat
		/// </summary>
		/// <param name="e">ePixelFormat</param>
		/// <returns>DirectX PixelFormat</returns>
		public static Microsoft.DirectX.DirectDraw.PixelFormat GetPixelFormat( EPixelFormat e ) {
			Microsoft.DirectX.DirectDraw.PixelFormat pixelFormat = new Microsoft.DirectX.DirectDraw.PixelFormat();

			switch( e ) {
				case EPixelFormat.RGB32:
					pixelFormat.Rgb = true;
					pixelFormat.RgbBitCount = 32;
					pixelFormat.RBitMask = 0xFF0000;
					pixelFormat.GBitMask = 0x00FF00;
					pixelFormat.BBitMask = 0x0000FF;
					break;
				case EPixelFormat.YUY2:
					pixelFormat.FourCC = 0x32595559;
					pixelFormat.FourCcIsValid = true;
					break;
			}

			return pixelFormat;
		}
    public void SetTexture2D(byte[] texture,int width,int height,TextureFormat format)
    {
        m_Texture = texture;
        m_width = width;
        m_height = height;
        if (m_Texture == null) {
            return;
        }

        if (m_Texture.Length != m_lastArrayLength) {
            if(m_lastArrayPtr!=IntPtr.Zero)
                Marshal.FreeHGlobal(m_lastArrayPtr);

            m_lastArrayPtr = Marshal.AllocHGlobal(m_Texture.Length);
        }
        switch (format) {
        case TextureFormat.ARGB32:
        case TextureFormat.RGBA32:
            m_format=EPixelFormat.EPixel_R8G8B8A8;
            break;
        case TextureFormat.Alpha8:
            m_format=EPixelFormat.EPixel_Alpha8;
            break;
        case TextureFormat.RGB24:
            m_format=EPixelFormat.EPixel_R8G8B8;
            break;
        }
    }
示例#3
0
        public static SKImage DecodeImage(byte[] sequence, int width, int height, int depth, EPixelFormat format)
        {
            byte[]      data;
            SKColorType colorType;

            switch (format)
            {
            case EPixelFormat.PF_DXT5:
                data      = DXTDecoder.DecodeDXT5(sequence, width, height, depth);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_DXT1:
                data      = DXTDecoder.DecodeDXT1(sequence, width, height, depth);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_ASTC_8x8:
                var x = (int)TextureFormatHelper.GetBlockWidth(format);
                var y = (int)TextureFormatHelper.GetBlockHeight(format);
                var z = (int)TextureFormatHelper.GetBlockDepth(format);
                data      = ASTCDecoder.DecodeToRGBA8888(sequence, x, y, z, width, height, 1);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_B8G8R8A8:
                data      = sequence;
                colorType = SKColorType.Bgra8888;
                break;

            case EPixelFormat.PF_BC5:
                data      = BCDecoder.DecodeBC5(sequence, width, height);
                colorType = SKColorType.Rgb888x;
                break;

            case EPixelFormat.PF_BC4:
                data      = BCDecoder.DecodeBC4(sequence, width, height);
                colorType = SKColorType.Rgb888x;
                break;

            case EPixelFormat.PF_G8:
                data      = sequence;
                colorType = SKColorType.Gray8;
                break;

            case EPixelFormat.PF_FloatRGBA:
                data      = sequence;
                colorType = SKColorType.RgbaF16;
                break;

            case EPixelFormat.PF_BC7:
                data = Detex.DecodeDetexLinear(sequence, width, height, isFloat: false,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_BPTC,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBA8);
                colorType = SKColorType.Rgb888x;
                break;

            case EPixelFormat.PF_BC6H:
                data = Detex.DecodeDetexLinear(sequence, width, height, isFloat: true,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_BPTC_FLOAT,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBX8); // Not sure whether that works, would actually be DETEX_PIXEL_FORMAT_FLOAT_RGBX32
                data      = Detex.DecodeBC6H(sequence, width, height);
                colorType = SKColorType.Rgb888x;
                break;

            case EPixelFormat.PF_ETC1:
                data = Detex.DecodeDetexLinear(sequence, width, height, isFloat: false,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_ETC1,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBA8);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_ETC2_RGB:
                data = Detex.DecodeDetexLinear(sequence, width, height, isFloat: false,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_ETC2,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBA8);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_ETC2_RGBA:
                data = Detex.DecodeDetexLinear(sequence, width, height, isFloat: false,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_ETC2_EAC,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBA8);
                colorType = SKColorType.Rgba8888;
                break;

            default:
                throw new NotImplementedException($"Cannot decode {format} format");
            }

            using var bitmap = new SKBitmap(new SKImageInfo(width, height, colorType, SKAlphaType.Unpremul));
            unsafe
            {
                fixed(byte *p = data)
                {
                    bitmap.SetPixels(new IntPtr(p));
                }
            }
            return(SKImage.FromBitmap(bitmap));
        }
示例#4
0
		/// <summary>
		/// Initialises the overlay with the specified Boundings
		/// </summary>
		public void Initialise() {
			mDevice = new Device();
			mDevice.SetCooperativeLevel( null, CooperativeLevelFlags.Normal ); //Only a overlay..

			//Create Primary
			SurfaceDescription desc = new SurfaceDescription();
			desc.SurfaceCaps.PrimarySurface = true;
			mPrimary = new Surface( desc, mDevice );

			//Create buffer
			desc = new SurfaceDescription();
			desc.Width = Boundings.Width;
			desc.Height = Boundings.Height;
			desc.BackBufferCount = 1;
			desc.SurfaceCaps.Flip = true;
			desc.SurfaceCaps.Overlay = true;
			desc.SurfaceCaps.Complex = true;
			desc.SurfaceCaps.VideoMemory = true;

			//Try which pixelformat works
			do {
				try {
					desc.PixelFormatStructure = GetPixelFormat( mPixelFormat );
					mBuffer = new Surface( desc, mDevice );
				} catch( InvalidPixelFormatException ) {
					mPixelFormat++;
				} catch( DirectXException ex ) {
					System.Diagnostics.Debug.WriteLine( ex );
					mBuffer = null;
					break;
				}
			} while( mBuffer == null && Enum.IsDefined( typeof( EPixelFormat ), mPixelFormat ) ); //Stop if a valid format is found or no one is left

			if( mBuffer == null || Enum.IsDefined( typeof( EPixelFormat ), mPixelFormat ) == false ) {
				mPixelFormat = EPixelFormat.RGB32; // reset Format
				mBuffer = null;
				throw new GraphicsException( "Could not create overlay - Either your graphic card does not support any of the available pixelformats or overlays in general." );
			}

			//Create backbuffer
			SurfaceCaps caps = new SurfaceCaps();
			caps.BackBuffer = true;
			mBackBuffer = mBuffer.GetAttachedSurface( caps );

			//Create rendertarget
			//Bitmap works on every graphicscard, a RGB-Surface wont
			mRenderTarget = new Bitmap( Boundings.Width, Boundings.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb );

			CreateFlags();
		}
示例#5
0
 public static bool HasFormatTableKey(EPixelFormat Format)
 {
     return(FormatTable.ContainsKey(GetBaseFormat(Format)));
 }
示例#6
0
 public static bool IsBCNCompressed(EPixelFormat Format)
 {
     return(Format.ToString().StartsWith("BC"));
 }
示例#7
0
        private static void DecodeTexture(FTexture2DMipMap mip, EPixelFormat format, out byte[] data, out SKColorType colorType)
        {
            switch (format)
            {
            case EPixelFormat.PF_DXT1:
                data      = DXTDecoder.DXT1(mip.Data.Data, mip.SizeX, mip.SizeY, mip.SizeZ);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_DXT5:
                data      = DXTDecoder.DXT5(mip.Data.Data, mip.SizeX, mip.SizeY, mip.SizeZ);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_ASTC_8x8:
                data = ASTCDecoder.RGBA8888(
                    mip.Data.Data,
                    FormatHelper.GetBlockWidth(format),
                    FormatHelper.GetBlockHeight(format),
                    FormatHelper.GetBlockDepth(format),
                    mip.SizeX, mip.SizeY, mip.SizeZ);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_BC4:
                data      = BCDecoder.BC4(mip.Data.Data, mip.SizeX, mip.SizeY);
                colorType = SKColorType.Rgb888x;
                break;

            case EPixelFormat.PF_BC5:
                data      = BCDecoder.BC5(mip.Data.Data, mip.SizeX, mip.SizeY);
                colorType = SKColorType.Rgb888x;
                break;

            case EPixelFormat.PF_BC6H:
                // BC6H doesn't work no matter the pixel format, the closest we can get is either
                // Rgb565 DETEX_PIXEL_FORMAT_FLOAT_RGBX16 or Rgb565 DETEX_PIXEL_FORMAT_FLOAT_BGRX16

                data = Detex.DecodeDetexLinear(mip.Data.Data, mip.SizeX, mip.SizeY, true,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_BPTC_FLOAT,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_FLOAT_RGBX16);
                colorType = SKColorType.Rgb565;
                break;

            case EPixelFormat.PF_BC7:
                data = Detex.DecodeDetexLinear(mip.Data.Data, mip.SizeX, mip.SizeY, false,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_BPTC,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBA8);
                colorType = SKColorType.Rgb888x;
                break;

            case EPixelFormat.PF_ETC1:
                data = Detex.DecodeDetexLinear(mip.Data.Data, mip.SizeX, mip.SizeY, false,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_ETC1,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBA8);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_ETC2_RGB:
                data = Detex.DecodeDetexLinear(mip.Data.Data, mip.SizeX, mip.SizeY, false,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_ETC2,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBA8);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_ETC2_RGBA:
                data = Detex.DecodeDetexLinear(mip.Data.Data, mip.SizeX, mip.SizeY, false,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_ETC2_EAC,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBA8);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_B8G8R8A8:
                data      = mip.Data.Data;
                colorType = SKColorType.Bgra8888;
                break;

            case EPixelFormat.PF_G8:
                data      = mip.Data.Data;
                colorType = SKColorType.Gray8;
                break;

            case EPixelFormat.PF_FloatRGBA:
                data      = mip.Data.Data;
                colorType = SKColorType.RgbaF16;
                break;

            default: throw new NotImplementedException($"Unknown pixel format: {format}");
            }
        }
示例#8
0
        public static SKImage DecodeImage(byte[] sequence, int width, int height, int depth, EPixelFormat format)
        {
            byte[]      data;
            SKColorType colorType;

            switch (format)
            {
            case EPixelFormat.PF_DXT5:
                data      = DXTDecoder.DecodeDXT5(sequence, width, height, depth);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_DXT1:
                data      = DXTDecoder.DecodeDXT1(sequence, width, height, depth);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_ASTC_8x8:
                var x = (int)TextureFormatHelper.GetBlockWidth(format);
                var y = (int)TextureFormatHelper.GetBlockHeight(format);
                var z = (int)TextureFormatHelper.GetBlockDepth(format);
                data      = ASTCDecoder.DecodeToRGBA8888(sequence, x, y, z, width, height, 1);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_B8G8R8A8:
                data      = sequence;
                colorType = SKColorType.Bgra8888;
                break;

            case EPixelFormat.PF_BC5:
                data      = BCDecoder.DecodeBC5(sequence, width, height);
                colorType = SKColorType.Bgra8888;
                break;

            case EPixelFormat.PF_BC4:
                data      = BCDecoder.DecodeBC4(sequence, width, height);
                colorType = SKColorType.Bgra8888;
                break;

            case EPixelFormat.PF_G8:
                data      = sequence;
                colorType = SKColorType.Gray8;
                break;

            case EPixelFormat.PF_FloatRGBA:
                data      = sequence;
                colorType = SKColorType.RgbaF16;
                break;

            default:
                throw new NotImplementedException($"Cannot decode {format} format");
            }

            using var bitmap = new SKBitmap(new SKImageInfo(width, height, colorType, SKAlphaType.Unpremul));
            unsafe
            {
                fixed(byte *p = data)
                {
                    bitmap.SetPixels(new IntPtr(p));
                }
            }
            return(SKImage.FromBitmap(bitmap));
        }
示例#9
0
        public static void DecodeTextureNSW(FTexture2DMipMap mip, EPixelFormat format, bool isNormalMap, out byte[] data, out SKColorType colorType)
        {
            switch (format)
            {
            case EPixelFormat.PF_DXT5:
            {
                var uBlockSize  = mip.SizeX / 4;
                var vBlockSize  = mip.SizeY / 4;
                var totalBlocks = mip.Data.Data.Length / 16;

                if (uBlockSize * vBlockSize > totalBlocks)
                {
                    throw new ParserException($"Texture unable to be untiled: {format}");
                }

                var d = PlatformDeswizzlers.DesizzleNSW(mip.Data.Data, mip.SizeX, mip.SizeY, 4, 4, 16);
                data      = DXTDecoder.DXT5(d, mip.SizeX, mip.SizeY, mip.SizeZ);
                colorType = SKColorType.Rgba8888;
                break;
            }

            case EPixelFormat.PF_DXT1:
            {
                var uBlockSize  = mip.SizeX / 4;
                var vBlockSize  = mip.SizeY / 4;
                var totalBlocks = mip.Data.Data.Length / 8;

                if (uBlockSize * vBlockSize > totalBlocks)
                {
                    throw new ParserException($"Texture unable to be untiled: {format}");
                }

                var d = PlatformDeswizzlers.DesizzleNSW(mip.Data.Data, mip.SizeX, mip.SizeY, 4, 4, 8);
                data      = DXTDecoder.DXT1(d, mip.SizeX, mip.SizeY, mip.SizeZ);
                colorType = SKColorType.Rgba8888;
                break;
            }

            case EPixelFormat.PF_B8G8R8A8:
            {
                var uBlockSize  = mip.SizeX / 4;
                var vBlockSize  = mip.SizeY / 4;
                var totalBlocks = mip.Data.Data.Length / 4;

                if (uBlockSize * vBlockSize > totalBlocks)
                {
                    throw new ParserException($"Texture unable to be untiled: {format}");
                }

                data      = PlatformDeswizzlers.DesizzleNSW(mip.Data.Data, mip.SizeX, mip.SizeY, 1, 1, 4);
                colorType = SKColorType.Bgra8888;
                break;
            }

            default:
            {
                TextureDecoder.DecodeTexture(mip, format, isNormalMap, out data, out colorType);
                break;
            }
            }
        }