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

Static Public Member Functions

static Texture2D LoadPNG (string _path, FilterMode filter=FilterMode.Point)
 
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 63 of file ImageLoader.cs.

64 {
65 if (data.Length >= 2 && data[0] == byte.MaxValue)
66 {
67 return data[1] == 216;
68 }
69 return false;
70 }

Referenced by LoadPNG().

◆ IsPng()

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

Definition at line 54 of file ImageLoader.cs.

55 {
56 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)
57 {
58 return data[7] == 10;
59 }
60 return false;
61 }

Referenced by LoadPNG().

◆ IsSofMarker()

static bool ImageLoader.IsSofMarker ( byte  marker)
inlinestaticprivate

Definition at line 135 of file ImageLoader.cs.

136 {
137 switch (marker)
138 {
139 case 192:
140 case 193:
141 case 194:
142 case 195:
143 case 197:
144 case 198:
145 case 199:
146 case 201:
147 case 202:
148 case 203:
149 case 205:
150 case 206:
151 case 207:
152 return true;
153 default:
154 return false;
155 }
156 }

Referenced by TryGetJpegSize().

◆ LoadPNG()

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

Definition at line 6 of file ImageLoader.cs.

7 {
8 if (!File.Exists(_path))
9 {
10 return null;
11 }
12 byte[] array = ReadImageFile(_path);
13 if (array == null || array.Length < 8)
14 {
15 return null;
16 }
17 int width;
18 int height;
19 if (IsPng(array))
20 {
21 if (!TryGetPngSize(array, out width, out height))
22 {
23 return null;
24 }
25 }
26 else
27 {
28 if (!IsJpeg(array))
29 {
30 return null;
31 }
32 if (!TryGetJpegSize(array, out width, out height))
33 {
34 return null;
35 }
36 }
37 TextureImportSetting.Data data = (TextureImportSetting.Instance ? TextureImportSetting.Instance.data : IO.importSetting);
38 Texture2D texture2D = new Texture2D(width, height, data.format, data.mipmap, data.linear);
39 texture2D.LoadImage(array);
40 texture2D.wrapMode = data.wrapMode;
41 texture2D.filterMode = filter;
42 texture2D.anisoLevel = data.anisoLevel;
43 texture2D.mipMapBias = data.mipmapBias;
44 return texture2D;
45 }
Definition: IO.cs:11
static TextureImportSetting.Data importSetting
Definition: IO.cs:51
static bool TryGetJpegSize(byte[] data, out int width, out int height)
Definition: ImageLoader.cs:88
static bool TryGetPngSize(byte[] data, out int width, out int height)
Definition: ImageLoader.cs:72
static bool IsPng(byte[] data)
Definition: ImageLoader.cs:54
static byte[] ReadImageFile(string _path)
Definition: ImageLoader.cs:47
static bool IsJpeg(byte[] data)
Definition: ImageLoader.cs:63

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.LoadPNG().

◆ ReadImageFile()

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

Definition at line 47 of file ImageLoader.cs.

48 {
49 using FileStream input = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
50 using BinaryReader binaryReader = new BinaryReader(input);
51 return binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
52 }

Referenced by LoadPNG().

◆ ReadIntBigEndian()

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

Definition at line 158 of file ImageLoader.cs.

159 {
160 if (offset + 3 >= data.Length)
161 {
162 return 0;
163 }
164 return ((data[offset] & 0xFF) << 24) | ((data[offset + 1] & 0xFF) << 16) | ((data[offset + 2] & 0xFF) << 8) | (data[offset + 3] & 0xFF);
165 }

Referenced by TryGetPngSize().

◆ TryGetJpegSize()

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

Definition at line 88 of file ImageLoader.cs.

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

References IsSofMarker().

Referenced by LoadPNG().

◆ TryGetPngSize()

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

Definition at line 72 of file ImageLoader.cs.

73 {
74 width = (height = 0);
75 if (data.Length < 24)
76 {
77 return false;
78 }
79 width = ReadIntBigEndian(data, 16);
80 height = ReadIntBigEndian(data, 20);
81 if (width > 0)
82 {
83 return height > 0;
84 }
85 return false;
86 }
static int ReadIntBigEndian(byte[] data, int offset)
Definition: ImageLoader.cs:158

References ReadIntBigEndian().

Referenced by LoadPNG().


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