1using Microsoft.Xna.Framework.Graphics;
4using System.Drawing.Imaging;
6using System.Runtime.InteropServices;
8using System.Threading.Tasks;
9using Terraria.ModLoader.Engine;
17 public static bool ToRaw(Stream src, Stream dst) {
18 using (var img =
new Bitmap(src)) {
26 var bitmapData = img.LockBits(
new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
27 var rawdata =
new int[img.Width * img.Height];
28 Marshal.Copy(bitmapData.Scan0, rawdata, 0, rawdata.Length);
33 foreach (
int c
in rawdata) {
35 int a = c >> 24 & 0xFF;
36 int b = c >> 16 & 0xFF;
37 int g = c >> 8 & 0xFF;
38 int r = c >> 0 & 0xFF;
60 using (var ms =
new MemoryStream()) {
61 return ToRaw(src, ms) ? ms.ToArray() :
null;
65 public static Texture2D
RawToTexture2D(GraphicsDevice graphicsDevice, Stream src) =>
68 public static void RawToPng(Stream src, Stream dst) {
69 using (var img =
RawToTexture2D(Main.instance.GraphicsDevice, src))
70 img.SaveAsPng(dst, img.Width, img.Height);
73 public static Tuple<int, int, byte[]>
ReadRaw(Stream src) =>
77 int v = r.ReadInt32();
79 throw new Exception(
"Unknown RawImg Format Version: " + v);
81 int width = r.ReadInt32();
82 int height = r.ReadInt32();
83 var rawdata = r.ReadBytes(width * height * 4);
84 return new Tuple<int, int, byte[]>(width, height, rawdata);
89 var tex =
new Texture2D(graphicsDevice, rawData.Item1, rawData.Item2);
90 tex.SetData(rawData.Item3);
96 return GLCallLocker.InvokeAsync(() => {
97 var tex =
new Texture2D(graphicsDevice, rawData.Item1, rawData.Item2);
98 tex.SetData(rawData.Item3);
105 if (!(stream is MemoryStream)) {
106 var ms =
new MemoryStream((
int)stream.Length);
111 return GLCallLocker.InvokeAsync(() => Texture2D.FromStream(graphicsDevice, stream));
113 Texture2D.TextureDataFromStreamEXT(stream, out
int width, out
int height, out
byte[] rawdata, -1, -1,
false);
114 return GLCallLocker.InvokeAsync(() => {
115 var tex =
new Texture2D(graphicsDevice, width, height);
116 tex.SetData(rawdata);
static bool ToRaw(Stream src, Stream dst)
static Tuple< int, int, byte[]> ReadRaw(Stream src)
static void RawToPng(Stream src, Stream dst)
static Task< Texture2D > RawToTexture2DAsync(GraphicsDevice graphicsDevice, BinaryReader r)
static byte[] ToRawBytes(Stream src)
static Texture2D RawToTexture2D(GraphicsDevice graphicsDevice, Stream src)
static Tuple< int, int, byte[]> ReadRaw(BinaryReader r)
static Texture2D RawToTexture2D(GraphicsDevice graphicsDevice, BinaryReader r)
static Task< Texture2D > PngToTexture2DAsync(GraphicsDevice graphicsDevice, Stream stream)