Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
LegacyImage.cs
浏览该文件的文档.
1namespace Engine.Media {
2 public class LegacyImage {
3 public readonly int Width;
4
5 public readonly int Height;
6
7 public readonly Color[] Pixels;
8
9 public LegacyImage(LegacyImage image) {
10 ArgumentNullException.ThrowIfNull(image);
11 Width = image.Width;
12 Height = image.Height;
13 Pixels = (Color[])image.Pixels.Clone();
14 }
15
16 public LegacyImage(Image image) {
17 ArgumentNullException.ThrowIfNull(image);
18 Width = image.Width;
19 Height = image.Height;
20 Pixels = (Color[])image.Pixels.Clone();
21 }
22
23 public LegacyImage(int width, int height) {
24 ArgumentOutOfRangeException.ThrowIfNegative(width);
25 ArgumentOutOfRangeException.ThrowIfNegative(height);
26 Width = width;
27 Height = height;
28 Pixels = new Color[width * height];
29 }
30
31 public Color GetPixel(int x, int y) => x < 0 || x >= Width ? throw new ArgumentOutOfRangeException(nameof(x)) :
32 y < 0 || y >= Height ? throw new ArgumentOutOfRangeException(nameof(y)) : Pixels[x + y * Width];
33
34 public void SetPixel(int x, int y, Color color) {
35 if (x < 0
36 || x >= Width) {
37 throw new ArgumentOutOfRangeException(nameof(x));
38 }
39 if (y < 0
40 || y >= Height) {
41 throw new ArgumentOutOfRangeException(nameof(y));
42 }
43 Pixels[x + y * Width] = color;
44 }
45
46 public static void PremultiplyAlpha(LegacyImage image) {
47 for (int i = 0; i < image.Pixels.Length; i++) {
48 image.Pixels[i] = Color.PremultiplyAlpha(image.Pixels[i]);
49 }
50 }
51
52 public static IEnumerable<LegacyImage> GenerateMipmaps(LegacyImage image, int maxLevelsCount = int.MaxValue) {
53 ArgumentNullException.ThrowIfNull(image);
54 ArgumentOutOfRangeException.ThrowIfNegative(maxLevelsCount);
55 if (maxLevelsCount == 0) {
56 yield break;
57 }
58 int mipWidth = image.Width;
59 int mipHeight = image.Height;
60 yield return image;
61 int level = 1;
62 while (true) {
63 if (level >= maxLevelsCount) {
64 yield break;
65 }
66 if ((mipWidth > 1 && mipWidth % 2 != 0)
67 || (mipHeight > 1 && mipHeight % 2 != 0)) {
68 break;
69 }
70 int num = mipWidth;
71 int num2 = mipHeight;
72 mipWidth = MathUtils.Max(num / 2, 1);
73 mipHeight = MathUtils.Max(num2 / 2, 1);
74 LegacyImage mipImage = new(mipWidth, mipHeight);
75 int num3 = num / mipWidth;
76 int num4 = num2 / mipHeight;
77 if (num3 == 2
78 && num4 == 2) {
79 int i = 0;
80 int num5 = 0;
81 for (; i < mipHeight; i++) {
82 int num6 = i * 2 * num;
83 int num7 = 0;
84 while (num7 < mipWidth) {
85 Color color = image.Pixels[num6];
86 Color color2 = image.Pixels[num6 + 1];
87 Color color3 = image.Pixels[num6 + num];
88 Color color4 = image.Pixels[num6 + num + 1];
89 byte r = (byte)((color.R + color2.R + color3.R + color4.R + 2) / 4);
90 byte g = (byte)((color.G + color2.G + color3.G + color4.G + 2) / 4);
91 byte b = (byte)((color.B + color2.B + color3.B + color4.B + 2) / 4);
92 byte a = (byte)((color.A + color2.A + color3.A + color4.A + 2) / 4);
93 mipImage.Pixels[num5] = new Color(r, g, b, a);
94 num7++;
95 num6 += 2;
96 num5++;
97 }
98 }
99 }
100 else if (num3 == 2
101 && num4 == 1) {
102 int j = 0;
103 int num8 = 0;
104 for (; j < mipHeight; j++) {
105 int num9 = j * num;
106 int num10 = 0;
107 while (num10 < mipWidth) {
108 Color color5 = image.Pixels[num9];
109 Color color6 = image.Pixels[num9 + 1];
110 byte r2 = (byte)((color5.R + color6.R + 1) / 2);
111 byte g2 = (byte)((color5.G + color6.G + 1) / 2);
112 byte b2 = (byte)((color5.B + color6.B + 1) / 2);
113 byte a2 = (byte)((color5.A + color6.A + 1) / 2);
114 mipImage.Pixels[num8] = new Color(r2, g2, b2, a2);
115 num10++;
116 num9 += 2;
117 num8++;
118 }
119 }
120 }
121 else {
122 if (num3 != 1
123 || num4 != 2) {
124 yield break;
125 }
126 int k = 0;
127 int num11 = 0;
128 for (; k < mipHeight; k++) {
129 int num12 = k * 2 * num;
130 int num13 = 0;
131 while (num13 < mipWidth) {
132 Color color7 = image.Pixels[num12];
133 Color color8 = image.Pixels[num12 + num];
134 byte r3 = (byte)((color7.R + color8.R + 1) / 2);
135 byte g3 = (byte)((color7.G + color8.G + 1) / 2);
136 byte b3 = (byte)((color7.B + color8.B + 1) / 2);
137 byte a3 = (byte)((color7.A + color8.A + 1) / 2);
138 mipImage.Pixels[num11] = new Color(r3, g3, b3, a3);
139 num13++;
140 num12++;
141 num11++;
142 }
143 }
144 }
145 yield return mipImage;
146 image = mipImage;
147 int num14 = level + 1;
148 level = num14;
149 }
150 throw new InvalidOperationException(
151 "Generating mipmaps with not 2:1 scaling is not supported. Limit mipmap levels count using maxLevelsCount parameter."
152 );
153 }
154
155 public static ImageFileFormat DetermineFileFormat(string extension) => Image.DetermineFileFormat(extension);
156
157 public static ImageFileFormat DetermineFileFormat(Stream stream) => Image.DetermineFileFormat(stream);
158
159 public static LegacyImage Load(Stream stream, ImageFileFormat format) => new(Image.Load(stream, format));
160
161 public static LegacyImage Load(string fileName, ImageFileFormat format) {
162 using (Stream stream = Storage.OpenFile(fileName, OpenFileMode.Read)) {
163 return Load(stream, format);
164 }
165 }
166
167 public static LegacyImage Load(Stream stream) => new(Image.Load(stream));
168
169 public static LegacyImage Load(string fileName) {
170 using (Stream stream = Storage.OpenFile(fileName, OpenFileMode.Read)) {
171 return Load(stream);
172 }
173 }
174
175 public static void Save(LegacyImage image, Stream stream, ImageFileFormat format, bool saveAlpha) =>
176 Image.Save(new Image(image), stream, format, saveAlpha);
177
178 public static void Save(LegacyImage image, string fileName, ImageFileFormat format, bool saveAlpha) {
179 using (Stream stream = Storage.OpenFile(fileName, OpenFileMode.Create)) {
180 Save(image, stream, format, saveAlpha);
181 }
182 }
183 }
184}
Engine.Color Color
Engine.Media.Image Image
static int Max(int x1, int x2)
static ImageFileFormat DetermineFileFormat(string extension)
static Image Load(Stream stream, ImageFileFormat format)
static void Save(Image image, Stream stream, ImageFileFormat format, bool saveAlpha, bool sync=false)
void SetPixel(int x, int y, Color color)
static LegacyImage Load(string fileName)
static void Save(LegacyImage image, Stream stream, ImageFileFormat format, bool saveAlpha)
static ImageFileFormat DetermineFileFormat(Stream stream)
Color GetPixel(int x, int y)
static void Save(LegacyImage image, string fileName, ImageFileFormat format, bool saveAlpha)
static void PremultiplyAlpha(LegacyImage image)
static LegacyImage Load(Stream stream)
LegacyImage(LegacyImage image)
static LegacyImage Load(Stream stream, ImageFileFormat format)
static IEnumerable< LegacyImage > GenerateMipmaps(LegacyImage image, int maxLevelsCount=int.MaxValue)
static ImageFileFormat DetermineFileFormat(string extension)
LegacyImage(int width, int height)
static LegacyImage Load(string fileName, ImageFileFormat format)
static Stream OpenFile(string path, OpenFileMode openFileMode)
static Color PremultiplyAlpha(Color c)