Elin Decompiled Documentation EA 23.356.1 Nightly
Loading...
Searching...
No Matches
ImageLoader Class Reference

Static Public Member Functions

static Texture2D LoadPNG (string _path, FilterMode filter=FilterMode.Point)
 
static Texture2D Load (string _path, FilterMode filter, Vector2Int? mipmap)
 
static byte[] ReadImageFile (string _path)
 

Static Private Member Functions

static bool IsPng (byte[] data)
 
static bool IsJpeg (byte[] data)
 
static bool TryGetPngSize (byte[] data, out int width, out int height)
 
static bool TryGetJpegSize (byte[] data, out int width, out int height)
 
static bool IsSofMarker (byte marker)
 
static int ReadIntBigEndian (byte[] data, int offset)
 

Detailed Description

Definition at line 4 of file ImageLoader.cs.

Member Function Documentation

◆ IsJpeg()

static bool ImageLoader.IsJpeg ( byte[]  data)
inlinestaticprivate

Definition at line 69 of file ImageLoader.cs.

70 {
71 if (data.Length >= 2 && data[0] == byte.MaxValue)
72 {
73 return data[1] == 216;
74 }
75 return false;
76 }

Referenced by Load().

◆ IsPng()

static bool ImageLoader.IsPng ( byte[]  data)
inlinestaticprivate

Definition at line 60 of file ImageLoader.cs.

61 {
62 if (data.Length >= 8 && data[0] == 137 && data[1] == 80 && data[2] == 78 && data[3] == 71 && data[4] == 13 && data[5] == 10 && data[6] == 26)
63 {
64 return data[7] == 10;
65 }
66 return false;
67 }

Referenced by Load().

◆ IsSofMarker()

static bool ImageLoader.IsSofMarker ( byte  marker)
inlinestaticprivate

Definition at line 141 of file ImageLoader.cs.

142 {
143 switch (marker)
144 {
145 case 192:
146 case 193:
147 case 194:
148 case 195:
149 case 197:
150 case 198:
151 case 199:
152 case 201:
153 case 202:
154 case 203:
155 case 205:
156 case 206:
157 case 207:
158 return true;
159 default:
160 return false;
161 }
162 }

Referenced by TryGetJpegSize().

◆ Load()

static Texture2D ImageLoader.Load ( string  _path,
FilterMode  filter,
Vector2Int?  mipmap 
)
inlinestatic

Definition at line 11 of file ImageLoader.cs.

12 {
13 if (!File.Exists(_path))
14 {
15 return null;
16 }
17 byte[] array = ReadImageFile(_path);
18 if (array == null || array.Length < 8)
19 {
20 return null;
21 }
22 int width;
23 int height;
24 if (IsPng(array))
25 {
26 if (!TryGetPngSize(array, out width, out height))
27 {
28 return null;
29 }
30 }
31 else
32 {
33 if (!IsJpeg(array))
34 {
35 return null;
36 }
37 if (!TryGetJpegSize(array, out width, out height))
38 {
39 return null;
40 }
41 }
42 TextureImportSetting.Data data = (TextureImportSetting.Instance ? TextureImportSetting.Instance.data : IO.importSetting);
43 bool flag = mipmap.HasValue && (width > mipmap.Value.x || height > mipmap.Value.y);
44 Texture2D texture2D = new Texture2D(width, height, data.format, data.mipmap || flag, data.linear);
45 texture2D.LoadImage(array);
46 texture2D.wrapMode = data.wrapMode;
47 texture2D.filterMode = (flag ? FilterMode.Trilinear : filter);
48 texture2D.anisoLevel = data.anisoLevel;
49 texture2D.mipMapBias = data.mipmapBias;
50 return texture2D;
51 }
Definition: IO.cs:10
static TextureImportSetting.Data importSetting
Definition: IO.cs:50
static bool TryGetJpegSize(byte[] data, out int width, out int height)
Definition: ImageLoader.cs:94
static bool TryGetPngSize(byte[] data, out int width, out int height)
Definition: ImageLoader.cs:78
static bool IsPng(byte[] data)
Definition: ImageLoader.cs:60
static byte[] ReadImageFile(string _path)
Definition: ImageLoader.cs:53
static bool IsJpeg(byte[] data)
Definition: ImageLoader.cs:69

References TextureImportSetting.Data.anisoLevel, TextureImportSetting.Data.format, IsJpeg(), IsPng(), TextureImportSetting.Data.linear, TextureImportSetting.Data.mipmap, TextureImportSetting.Data.mipmapBias, ReadImageFile(), TryGetJpegSize(), TryGetPngSize(), and TextureImportSetting.Data.wrapMode.

Referenced by IO.LoadObject< T >(), and LoadPNG().

◆ LoadPNG()

static Texture2D ImageLoader.LoadPNG ( string  _path,
FilterMode  filter = FilterMode::Point 
)
inlinestatic

Definition at line 6 of file ImageLoader.cs.

7 {
8 return Load(_path, filter, null);
9 }
static Texture2D Load(string _path, FilterMode filter, Vector2Int? mipmap)
Definition: ImageLoader.cs:11

References Load().

Referenced by IO.LoadPNG().

◆ ReadImageFile()

static byte[] ImageLoader.ReadImageFile ( string  _path)
inlinestatic

Definition at line 53 of file ImageLoader.cs.

54 {
55 using FileStream input = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
56 using BinaryReader binaryReader = new BinaryReader(input);
57 return binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
58 }

Referenced by Load().

◆ ReadIntBigEndian()

static int ImageLoader.ReadIntBigEndian ( byte[]  data,
int  offset 
)
inlinestaticprivate

Definition at line 164 of file ImageLoader.cs.

165 {
166 if (offset + 3 >= data.Length)
167 {
168 return 0;
169 }
170 return ((data[offset] & 0xFF) << 24) | ((data[offset + 1] & 0xFF) << 16) | ((data[offset + 2] & 0xFF) << 8) | (data[offset + 3] & 0xFF);
171 }

Referenced by TryGetPngSize().

◆ TryGetJpegSize()

static bool ImageLoader.TryGetJpegSize ( byte[]  data,
out int  width,
out int  height 
)
inlinestaticprivate

Definition at line 94 of file ImageLoader.cs.

95 {
96 width = (height = 0);
97 int i = 2;
98 while (i + 1 < data.Length)
99 {
100 if (data[i] != byte.MaxValue)
101 {
102 i++;
103 continue;
104 }
105 for (; i < data.Length && data[i] == byte.MaxValue; i++)
106 {
107 }
108 if (i >= data.Length)
109 {
110 break;
111 }
112 byte b = data[i++];
113 if (b == 218 || b == 217 || i + 1 >= data.Length)
114 {
115 break;
116 }
117 int num = (data[i] << 8) | data[i + 1];
118 if (num < 2)
119 {
120 return false;
121 }
122 if (IsSofMarker(b))
123 {
124 if (i + 7 >= data.Length)
125 {
126 return false;
127 }
128 height = (data[i + 3] << 8) | data[i + 4];
129 width = (data[i + 5] << 8) | data[i + 6];
130 if (width > 0)
131 {
132 return height > 0;
133 }
134 return false;
135 }
136 i += num;
137 }
138 return false;
139 }
static bool IsSofMarker(byte marker)
Definition: ImageLoader.cs:141

References IsSofMarker().

Referenced by Load().

◆ TryGetPngSize()

static bool ImageLoader.TryGetPngSize ( byte[]  data,
out int  width,
out int  height 
)
inlinestaticprivate

Definition at line 78 of file ImageLoader.cs.

79 {
80 width = (height = 0);
81 if (data.Length < 24)
82 {
83 return false;
84 }
85 width = ReadIntBigEndian(data, 16);
86 height = ReadIntBigEndian(data, 20);
87 if (width > 0)
88 {
89 return height > 0;
90 }
91 return false;
92 }
static int ReadIntBigEndian(byte[] data, int offset)
Definition: ImageLoader.cs:164

References ReadIntBigEndian().

Referenced by Load().


The documentation for this class was generated from the following file: