Skip to content

Commit 1823979

Browse files
committed
fix Linux support (move back to ImageSharp)
to update delete: System.Drawing.Common.dll add: SixLabors.ImageSharp.dll SixLabors.ImageSharp.Drawing.dll
1 parent 651110c commit 1823979

File tree

5 files changed

+46
-61
lines changed

5 files changed

+46
-61
lines changed

src/MiNET/MiNET.Console/a.bat

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/MiNET/MiNET.Console/runtimeconfig.template.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"configProperties": {
44
"System.GC.Server": true,
55
"System.GC.Concurrent": true,
6-
"System.Drawing.EnableUnixSupport": true
76
}
87
}
98
}

src/MiNET/MiNET/Entities/ImageProviders/TextMapImageProvider.cs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,33 @@
2323

2424
#endregion
2525

26-
using System.Drawing;
27-
using System.Drawing.Imaging;
2826
using MiNET.Net;
2927
using MiNET.Utils;
28+
using SixLabors.Fonts;
29+
using SixLabors.ImageSharp;
30+
using SixLabors.ImageSharp.Drawing.Processing;
31+
using SixLabors.ImageSharp.PixelFormats;
32+
using SixLabors.ImageSharp.Processing;
33+
using RectangleF = System.Drawing.RectangleF;
3034

3135
namespace MiNET.Entities.ImageProviders
3236
{
3337
public class TextMapImageProvider : IMapImageProvider
3438
{
39+
private static FontCollection _fontCollection;
3540
private static Font _font = null;
36-
41+
3742
static TextMapImageProvider()
3843
{
39-
_font = new Font("Arial", 9);
44+
_fontCollection = new FontCollection();
45+
_fontCollection.AddSystemFonts();
46+
47+
if (_fontCollection.TryGet("Arial", out var family))
48+
{
49+
_font = family.CreateFont(9);
50+
}
4051
}
41-
52+
4253
public string Text { get; set; }
4354

4455
public TextMapImageProvider(string text = "")
@@ -78,39 +89,30 @@ public virtual McpeWrapper GetBatch(MapInfo mapInfo, bool forced)
7889

7990
private static byte[] DrawText(MapInfo map, string text)
8091
{
81-
using (var bitmap = new Bitmap(map.Col, map.Row))
82-
{
83-
using (Graphics graphics = Graphics.FromImage(bitmap))
84-
{
85-
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
86-
graphics.DrawString(text, _font, Brushes.AntiqueWhite, new PointF(0, 0));
87-
}
88-
89-
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
90-
BitmapData bmpData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat);
91-
92-
int bytesPerPixel = Bitmap.GetPixelFormatSize(bitmap.PixelFormat) / 8;
93-
int byteCount = bmpData.Stride * bitmap.Height;
94-
byte[] bytes = new byte[byteCount];
92+
var bitmap = new Image<Rgba32>(map.Col, map.Row);
93+
var rectf = new RectangleF(0, 0, map.Col, map.Row);
9594

96-
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, bytes, 0, byteCount);
95+
bitmap.Mutate(
96+
x =>
97+
{
98+
x.DrawText(text, _font, SixLabors.ImageSharp.Color.AntiqueWhite, new PointF(0, 0));
99+
});
97100

98-
bitmap.UnlockBits(bmpData);
101+
byte[] bytes = new byte[bitmap.Height * bitmap.Width * 4];
99102

100-
for (int i = 0; i < bytes.Length; i += bytesPerPixel)
103+
int i = 0;
104+
for (int y = 0; y < bitmap.Height; y++)
105+
{
106+
for (int x = 0; x < bitmap.Width; x++)
101107
{
102-
byte b = bytes[i];
103-
byte g = bytes[i + 1];
104-
byte r = bytes[i + 2];
105-
byte a = bytes[i + 3];
106-
bytes[i] = r;
107-
bytes[i + 1] = g;
108-
bytes[i + 2] = b;
109-
bytes[i + 3] = a;
108+
var color = bitmap[x, y];
109+
bytes[i++] = color.R;
110+
bytes[i++] = color.G;
111+
bytes[i++] = color.B;
112+
bytes[i++] = 0xff;
110113
}
111-
112-
return bytes;
113114
}
115+
return bytes;
114116
}
115117
}
116118
}

src/MiNET/MiNET/MiNET.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
<PackageReference Include="MiNET.LevelDB" Version="1.0.49" />
4242
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
4343
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
44-
<PackageReference Include="System.Drawing.Common" Version="8.0.6" />
44+
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
45+
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.3" />
4546
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="8.0.0" />
4647
</ItemGroup>
4748
<ItemGroup>

src/MiNET/MiNET/Utils/Skins/Skin.cs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525

2626
using System;
2727
using System.Collections.Generic;
28-
using System.Drawing;
29-
using System.Drawing.Imaging;
3028
using Newtonsoft.Json;
3129
using Newtonsoft.Json.Converters;
3230
using Newtonsoft.Json.Serialization;
31+
using SixLabors.ImageSharp;
32+
using SixLabors.ImageSharp.PixelFormats;
3333

3434
namespace MiNET.Utils.Skins
3535
{
@@ -87,7 +87,7 @@ public SkinResourcePatch SkinResourcePatch
8787
public string GeometryName { get; set; }
8888
public string GeometryData { get; set; }
8989
public string GeometryDataVersion { get; set; }
90-
90+
9191
public string ArmSize { get; set; }
9292

9393
public string SkinColor { get; set; }
@@ -103,11 +103,12 @@ public SkinResourcePatch SkinResourcePatch
103103

104104
public static byte[] GetTextureFromFile(string filename)
105105
{
106-
Bitmap bitmap = new Bitmap(filename);
106+
var bitmap = Image.Load<Rgba32>(filename);// new Image<Rgba32>(filename);
107107

108108
var size = bitmap.Height * bitmap.Width * 4;
109109

110-
if (size != 0x2000 && size != 0x4000 && size != 0x10000) return null;
110+
if (size != 0x2000 && size != 0x4000 && size != 0x10000)
111+
return null;
111112

112113
byte[] bytes = new byte[size];
113114

@@ -116,7 +117,7 @@ public static byte[] GetTextureFromFile(string filename)
116117
{
117118
for (int x = 0; x < bitmap.Width; x++)
118119
{
119-
var color = bitmap.GetPixel(x, y);
120+
var color = bitmap[x, y];
120121
bytes[i++] = color.R;
121122
bytes[i++] = color.G;
122123
bytes[i++] = color.B;
@@ -134,15 +135,7 @@ public static void SaveTextureToFile(string filename, byte[] bytes)
134135
int width = size == 0x10000 ? 128 : 64;
135136
var height = size == 0x2000 ? 32 : (size == 0x4000 ? 64 : 128);
136137

137-
var bitmap = new Bitmap(width, height);
138-
139-
BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
140-
141-
int bytesPerPixel = Bitmap.GetPixelFormatSize(bitmap.PixelFormat) / 8;
142-
int byteCount = bmpData.Stride * bitmap.Height;
143-
byte[] rgbValues = new byte[byteCount];
144-
145-
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, rgbValues, 0, byteCount);
138+
var bitmap = new Image<Rgba32>(width, height);
146139

147140
int i = 0;
148141
for (int y = 0; y < bitmap.Height; y++)
@@ -154,19 +147,10 @@ public static void SaveTextureToFile(string filename, byte[] bytes)
154147
byte b = bytes[i++];
155148
byte a = bytes[i++];
156149

157-
int index = y * bmpData.Stride + x * bytesPerPixel;
158-
159-
rgbValues[index] = r;
160-
rgbValues[index + 1] = g;
161-
rgbValues[index + 2] = b;
162-
if (bytesPerPixel == 4) { rgbValues[index + 3] = a; }
150+
bitmap[x, y] = new Rgba32(r, g, b, a);
163151
}
164152
}
165153

166-
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, bmpData.Scan0, byteCount);
167-
168-
bitmap.UnlockBits(bmpData);
169-
170154
bitmap.Save(filename);
171155
}
172156

@@ -190,7 +174,7 @@ public static string ToJson(GeometryModel geometryModel)
190174
settings.MissingMemberHandling = MissingMemberHandling.Error;
191175
//settings.Formatting = Formatting.Indented;
192176
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
193-
settings.Converters.Add(new StringEnumConverter {NamingStrategy = new CamelCaseNamingStrategy()});
177+
settings.Converters.Add(new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy() });
194178

195179
return JsonConvert.SerializeObject(geometryModel, settings);
196180
}

0 commit comments

Comments
 (0)