Terraria ModLoader  0.11.4
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 13 of file ImageIO.cs.

Member Function Documentation

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

Definition at line 103 of file ImageIO.cs.

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

103  {
104 #if XNA
105  if (!(stream is MemoryStream)) {
106  var ms = new MemoryStream((int)stream.Length);
107  stream.CopyTo(ms);
108  ms.Position = 0;
109  stream = ms;
110  }
111  return GLCallLocker.InvokeAsync(() => Texture2D.FromStream(graphicsDevice, stream));
112 #else
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);
117  return tex;
118  });
119 #endif
120  }

+ Here is the caller graph for this function:

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

Definition at line 68 of file ImageIO.cs.

68  {
69  using (var img = RawToTexture2D(Main.instance.GraphicsDevice, src))
70  img.SaveAsPng(dst, img.Width, img.Height);
71  }
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 87 of file ImageIO.cs.

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

Definition at line 94 of file ImageIO.cs.

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

94  {
95  var rawData = ReadRaw(r);
96  return GLCallLocker.InvokeAsync(() => {
97  var tex = new Texture2D(graphicsDevice, rawData.Item1, rawData.Item2);
98  tex.SetData(rawData.Item3);
99  return tex;
100  });
101  }
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 76 of file ImageIO.cs.

76  {
77  int v = r.ReadInt32();
78  if (v != VERSION)
79  throw new Exception("Unknown RawImg Format Version: " + v);
80 
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);
85  }
static bool Terraria.ModLoader.IO.ImageIO.ToRaw ( Stream  src,
Stream  dst 
)
static

Definition at line 17 of file ImageIO.cs.

17  {
18  using (var img = new Bitmap(src)) {
19  // now that the hi-def profile is always enabled, the max texture size is 4096
20  // if we get bug reports with old graphics cards forcing fallback to Reach and failing to load
21  // large textures, we can implement a slower path where the rawimg is converted to png before loading
22 
23  //if (img.Width > 2048 || img.Height > 2048)
24  // return false;
25 
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);
29  var w = new BinaryWriter(dst);
30  w.Write(VERSION);
31  w.Write(img.Width);
32  w.Write(img.Height);
33  foreach (int c in rawdata) {
34  //Bitmap is in ABGR
35  int a = c >> 24 & 0xFF;
36  int b = c >> 16 & 0xFF;
37  int g = c >> 8 & 0xFF;
38  int r = c >> 0 & 0xFF;
39 
40  //special note, mirror XNA behaviour of zeroing out textures with full alpha zero
41  //this means that an author doesn't have to set their fully transparent pixels to black
42  //if they want additive blending they need to use alpha 1/255
43  if (a == 0) {
44  w.Write(0);
45  continue;
46  }
47 
48  //write ARGB, note that the texture is assumed pre-multiplied, allowing for extra blending effects
49  w.Write((byte)b);
50  w.Write((byte)g);
51  w.Write((byte)r);
52  w.Write((byte)a);
53  }
54 
55  return true;
56  }
57  }
static byte [] Terraria.ModLoader.IO.ImageIO.ToRawBytes ( Stream  src)
static

Definition at line 59 of file ImageIO.cs.

59  {
60  using (var ms = new MemoryStream()) {
61  return ToRaw(src, ms) ? ms.ToArray() : null;
62  }
63  }
static bool ToRaw(Stream src, Stream dst)
Definition: ImageIO.cs:17

Member Data Documentation

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

Definition at line 15 of file ImageIO.cs.