Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
PngReader.cs
Go to the documentation of this file.
1using System;
2using System.IO;
7
9
11{
13
15
16 private bool _disposedValue;
17
18 public PngReader(GraphicsDevice graphicsDevice)
19 {
20 _graphicsDevice = graphicsDevice;
22 }
23
25 {
26 return new Type[1] { typeof(Texture2D) };
27 }
28
29 public async ValueTask<T> FromStream<T>(Stream stream, MainThreadCreationContext mainThreadCtx) where T : class
30 {
31 if (typeof(T) != typeof(Texture2D))
32 {
33 throw AssetLoadException.FromInvalidReader<PngReader, T>();
34 }
35 int width = default(int);
36 int height = default(int);
37 int len = default(int);
38 IntPtr img = FNA3D.ReadImageStream(stream, ref width, ref height, ref len, -1, -1, false);
40 await mainThreadCtx;
41 Texture2D val = new Texture2D(_graphicsDevice, width, height);
42 val.SetDataPointerEXT(0, (Rectangle?)null, img, len);
43 FNA3D.FNA3D_Image_Free(img);
44 return val as T;
45 }
46
47 protected virtual void Dispose(bool disposing)
48 {
49 if (!_disposedValue)
50 {
51 if (disposing)
52 {
54 }
55 _disposedValue = true;
56 }
57 }
58
59 public void Dispose()
60 {
61 Dispose(disposing: true);
62 }
63
64 private unsafe static void PreMultiplyAlpha(IntPtr img, int len)
65 {
66 byte* colors = (byte*)img.ToPointer();
67 for (int i = 0; i < len; i += 4)
68 {
69 int a = colors[i + 3];
70 colors[i] = (byte)(colors[i] * a / 255);
71 colors[i + 1] = (byte)(colors[i + 1] * a / 255);
72 colors[i + 2] = (byte)(colors[i + 2] * a / 255);
73 }
74 }
75}
async ValueTask< T > FromStream< T >(Stream stream, MainThreadCreationContext mainThreadCtx)
Definition PngReader.cs:29
virtual void Dispose(bool disposing)
Definition PngReader.cs:47
readonly ThreadLocal< Color[]> _colorProcessingCache
Definition PngReader.cs:14
static unsafe void PreMultiplyAlpha(IntPtr img, int len)
Definition PngReader.cs:64
PngReader(GraphicsDevice graphicsDevice)
Definition PngReader.cs:18
readonly GraphicsDevice _graphicsDevice
Definition PngReader.cs:12
unsafe void * ToPointer()
Definition IntPtr.cs:210