Terraria ModLoader  0.10.1.5
A framework for Terraria mods
Terraria.ModLoader.IO.ImageIO Class Reference
+ Collaboration diagram for Terraria.ModLoader.IO.ImageIO:

Static Public Member Functions

static Task< Texture2D > PngToTexture2DAsync (GraphicsDevice graphicsDevice, Stream stream)
 
static void RawToPng (Stream src, Stream dst)
 
static Texture2D RawToTexture2D (GraphicsDevice graphicsDevice, Stream src)
 
static Texture2D RawToTexture2D (GraphicsDevice graphicsDevice, BinaryReader r)
 
static Task< Texture2D > RawToTexture2DAsync (GraphicsDevice graphicsDevice, BinaryReader r)
 
static Tuple< int, int, byte[]> ReadRaw (Stream src)
 
static Tuple< int, int, byte[]> ReadRaw (BinaryReader r)
 
static bool ToRaw (Stream src, Stream dst)
 
static byte[] ToRawBytes (Stream src)
 

Public Attributes

const int VERSION = 1
 

Detailed Description

Definition at line 12 of file ImageIO.cs.

Member Function Documentation

static Task<Texture2D> Terraria.ModLoader.IO.ImageIO.PngToTexture2DAsync ( GraphicsDevice  graphicsDevice,
Stream  stream 
)
static

Definition at line 99 of file ImageIO.cs.

Referenced by Terraria.ModLoader.Mod.LoadTexture().

99  {
100 #if WINDOWS
101  if (!(stream is MemoryStream)) {
102  var ms = new MemoryStream((int)stream.Length);
103  stream.CopyTo(ms);
104  ms.Position = 0;
105  stream = ms;
106  }
107  return GLCallLocker.InvokeAsync(() => Texture2D.FromStream(graphicsDevice, stream));
108 #else
109  Texture2D.TextureDataFromStreamEXT(stream, out int width, out int height, out byte[] rawdata, -1, -1, false);
110  return GLCallLocker.InvokeAsync(() => {
111  var tex = new Texture2D(graphicsDevice, width, height);
112  tex.SetData(rawdata);
113  return tex;
114  });
115 #endif
116  }

+ Here is the caller graph for this function:

static void Terraria.ModLoader.IO.ImageIO.RawToPng ( Stream  src,
Stream  dst 
)
static

Definition at line 64 of file ImageIO.cs.

64  {
65  using (var img = RawToTexture2D(Main.instance.GraphicsDevice, src))
66  img.SaveAsPng(dst, img.Width, img.Height);
67  }
static Texture2D RawToTexture2D(GraphicsDevice graphicsDevice, Stream src)
static Texture2D Terraria.ModLoader.IO.ImageIO.RawToTexture2D ( GraphicsDevice  graphicsDevice,
Stream  src 
)
static
static Texture2D Terraria.ModLoader.IO.ImageIO.RawToTexture2D ( GraphicsDevice  graphicsDevice,
BinaryReader  r 
)
static

Definition at line 83 of file ImageIO.cs.

83  {
84  var rawData = ReadRaw(r);
85  var tex = new Texture2D(graphicsDevice, rawData.Item1, rawData.Item2);
86  tex.SetData(rawData.Item3);
87  return tex;
88  }
static Tuple< int, int, byte[]> ReadRaw(Stream src)
static Task<Texture2D> Terraria.ModLoader.IO.ImageIO.RawToTexture2DAsync ( GraphicsDevice  graphicsDevice,
BinaryReader  r 
)
static

Definition at line 90 of file ImageIO.cs.

Referenced by Terraria.ModLoader.Mod.LoadTexture().

90  {
91  var rawData = ReadRaw(r);
92  return GLCallLocker.InvokeAsync(() => {
93  var tex = new Texture2D(graphicsDevice, rawData.Item1, rawData.Item2);
94  tex.SetData(rawData.Item3);
95  return tex;
96  });
97  }
static Tuple< int, int, byte[]> ReadRaw(Stream src)

+ Here is the caller graph for this function:

static Tuple<int, int, byte[]> Terraria.ModLoader.IO.ImageIO.ReadRaw ( Stream  src)
static
static Tuple<int, int, byte[]> Terraria.ModLoader.IO.ImageIO.ReadRaw ( BinaryReader  r)
static

Definition at line 72 of file ImageIO.cs.

72  {
73  int v = r.ReadInt32();
74  if (v != VERSION)
75  throw new Exception("Unknown RawImg Format Version: " + v);
76 
77  int width = r.ReadInt32();
78  int height = r.ReadInt32();
79  var rawdata = r.ReadBytes(width * height * 4);
80  return new Tuple<int, int, byte[]>(width, height, rawdata);
81  }
static bool Terraria.ModLoader.IO.ImageIO.ToRaw ( Stream  src,
Stream  dst 
)
static

Definition at line 16 of file ImageIO.cs.

16  {
17  using (var img = new Bitmap(src)) {
18  //XNA has a strange interaction where large size PNGs can be loaded, but not created via any other means
19  if (img.Width > 2048 || img.Height > 2048)
20  return false;
21 
22  var bitmapData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
23  var rawdata = new int[img.Width * img.Height];
24  Marshal.Copy(bitmapData.Scan0, rawdata, 0, rawdata.Length);
25  var w = new BinaryWriter(dst);
26  w.Write(VERSION);
27  w.Write(img.Width);
28  w.Write(img.Height);
29  foreach (int c in rawdata) {
30  //Bitmap is in ABGR
31  int a = c >> 24 & 0xFF;
32  int b = c >> 16 & 0xFF;
33  int g = c >> 8 & 0xFF;
34  int r = c >> 0 & 0xFF;
35 
36  //special note, mirror XNA behaviour of zeroing out textures with full alpha zero
37  //this means that an author doesn't have to set their fully transparent pixels to black
38  //if they want additive blending they need to use alpha 1/255
39  if (a == 0) {
40  w.Write(0);
41  continue;
42  }
43 
44  //write ARGB, note that the texture is assumed pre-multiplied, allowing for extra blending effects
45  w.Write((byte)b);
46  w.Write((byte)g);
47  w.Write((byte)r);
48  w.Write((byte)a);
49  }
50 
51  return true;
52  }
53  }
static byte [] Terraria.ModLoader.IO.ImageIO.ToRawBytes ( Stream  src)
static

Definition at line 55 of file ImageIO.cs.

55  {
56  using (var ms = new MemoryStream()) {
57  return ToRaw(src, ms) ? ms.ToArray() : null;
58  }
59  }
static bool ToRaw(Stream src, Stream dst)
Definition: ImageIO.cs:16

Member Data Documentation

const int Terraria.ModLoader.IO.ImageIO.VERSION = 1

Definition at line 14 of file ImageIO.cs.