public static Image ToImage(this ITexture tex) { var d = tex.GetTextureData(); int w = tex.Width, h = tex.Height; switch (tex.Format) { case D3DFMT_A8R8G8B8: return(Image.LoadPixelData <Bgra32>(d, w, h)); case D3DFMT_L8: return(Image.LoadPixelData <L8>(d, w, h)); case D3DFMT_A8: return(Image.LoadPixelData <A8>(d, w, h)); case D3DFMT_A1R5G5B5: return(Image.LoadPixelData <Bgra5551>(d, w, h)); case D3DFMT_A8B8G8R8: return(Image.LoadPixelData <Rgba32>(d, w, h)); } var uncompressed = new byte[D3DFMT_A8B8G8R8.Size(w, h)]; if (!FuckDX.decode(d, tex.Format, w, h, uncompressed)) { throw new NotSupportedException( $"Failed to decode {tex.Name}: I guess {tex.Format} is not supported :( Mention @Kng if you care" ); } return(Image.LoadPixelData <Rgba32>(uncompressed, w, h)); }
public static byte[] ToPixelData(this Image image, TextureFormat fmt) { switch (fmt) { case D3DFMT_A8R8G8B8: return(image.ToPixelData <Bgra32>(fmt)); case D3DFMT_L8: return(image.ToPixelData <L8>(fmt)); case D3DFMT_A8: return(image.ToPixelData <A8>(fmt)); case D3DFMT_A1R5G5B5: return(image.ToPixelData <Bgra5551>(fmt)); case D3DFMT_A8B8G8R8: return(image.ToPixelData <Rgba32>(fmt)); } var uncompressed = image.ToPixelData <Rgba32>(D3DFMT_A8B8G8R8); var compressed = new byte[fmt.Size(image.Width, image.Height)]; if (!FuckDX.encode(uncompressed, fmt, 2, image.Width, image.Height, compressed)) { throw new NotSupportedException( $"Failed to encode: I guess {fmt} is not supported :( Mention @Kng if you care" ); } return(compressed); }