Elin Decompiled Documentation EA 23.264 Nightly
Loading...
Searching...
No Matches
ImageLoader.cs
Go to the documentation of this file.
1using System.IO;
2using UnityEngine;
3
4public static class ImageLoader
5{
6 public static Texture2D LoadPNG(string _path, FilterMode filter = FilterMode.Point)
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 }
46
47 public static byte[] ReadImageFile(string _path)
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 }
53
54 private static bool IsPng(byte[] data)
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 }
62
63 private static bool IsJpeg(byte[] data)
64 {
65 if (data.Length >= 2 && data[0] == byte.MaxValue)
66 {
67 return data[1] == 216;
68 }
69 return false;
70 }
71
72 private static bool TryGetPngSize(byte[] data, out int width, out int height)
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 }
87
88 private static bool TryGetJpegSize(byte[] data, out int width, out int height)
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 }
134
135 private static bool IsSofMarker(byte marker)
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 }
157
158 private static int ReadIntBigEndian(byte[] data, int offset)
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 }
166}
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 int ReadIntBigEndian(byte[] data, int offset)
Definition: ImageLoader.cs:158
static Texture2D LoadPNG(string _path, FilterMode filter=FilterMode.Point)
Definition: ImageLoader.cs:6
static bool IsSofMarker(byte marker)
Definition: ImageLoader.cs:135
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