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); }
public static byte[] ToPixelData <T>(this Image image, TextureFormat fmt) where T : unmanaged, IPixel <T> { var img = image.CloneAs <T>(); var stride = fmt.Stride(img.Width); var bytes = new byte[fmt.Size(img.Width, img.Height)]; for (int i = 0; i < img.Height; i++) { MemoryMarshal.AsBytes(img.GetPixelRowSpan(i)) .CopyTo(bytes.AsSpan(stride * i, stride)); } return(bytes); }