Skip to content

Commit 777167d

Browse files
committed
migration to System.Drawing
1 parent d5f0142 commit 777167d

File tree

4 files changed

+53
-284
lines changed

4 files changed

+53
-284
lines changed

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

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,20 @@
2323

2424
#endregion
2525

26-
using System.Drawing.Drawing2D;
26+
using System.Drawing;
27+
using System.Drawing.Imaging;
2728
using MiNET.Net;
2829
using MiNET.Utils;
29-
using SixLabors.Fonts;
30-
using SixLabors.ImageSharp;
31-
using SixLabors.ImageSharp.Drawing.Processing;
32-
using SixLabors.ImageSharp.PixelFormats;
33-
using SixLabors.ImageSharp.Processing;
34-
using Color = System.Drawing.Color;
35-
using RectangleF = System.Drawing.RectangleF;
3630

3731
namespace MiNET.Entities.ImageProviders
3832
{
3933
public class TextMapImageProvider : IMapImageProvider
4034
{
41-
private static FontCollection _fontCollection;
4235
private static Font _font = null;
4336

4437
static TextMapImageProvider()
4538
{
46-
_fontCollection = new FontCollection();
47-
_fontCollection.AddSystemFonts();
48-
49-
if (_fontCollection.TryGet("Arial", out var family))
50-
{
51-
_font = family.CreateFont(9);
52-
}
39+
_font = new Font("Arial", 9);
5340
}
5441

5542
public string Text { get; set; }
@@ -91,36 +78,39 @@ public virtual McpeWrapper GetBatch(MapInfo mapInfo, bool forced)
9178

9279
private static byte[] DrawText(MapInfo map, string text)
9380
{
94-
var bitmap = new Image<Rgba32>(map.Col, map.Row);
95-
var rectf = new RectangleF(0, 0, map.Col, map.Row);
96-
/*var g = Graphics.FromImage(bitmap);
97-
g.SmoothingMode = SmoothingMode.AntiAlias;
98-
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
99-
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
100-
g.DrawString(text, new Font("SketchFlow Print", 10), Brushes.AntiqueWhite, rectf);
101-
g.Flush();*/
102-
103-
bitmap.Mutate(
104-
x =>
81+
using (var bitmap = new Bitmap(map.Col, map.Row))
82+
{
83+
using (Graphics graphics = Graphics.FromImage(bitmap))
10584
{
106-
x.DrawText(text, _font, SixLabors.ImageSharp.Color.AntiqueWhite, new PointF(0, 0));
107-
});
85+
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
86+
graphics.DrawString(text, _font, Brushes.AntiqueWhite, new PointF(0, 0));
87+
}
10888

109-
byte[] bytes = new byte[bitmap.Height * bitmap.Width * 4];
89+
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
90+
BitmapData bmpData = bitmap.LockBits(rect, ImageLockMode.ReadOnly, bitmap.PixelFormat);
11091

111-
int i = 0;
112-
for (int y = 0; y < bitmap.Height; y++)
113-
{
114-
for (int x = 0; x < bitmap.Width; x++)
92+
int bytesPerPixel = Bitmap.GetPixelFormatSize(bitmap.PixelFormat) / 8;
93+
int byteCount = bmpData.Stride * bitmap.Height;
94+
byte[] bytes = new byte[byteCount];
95+
96+
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, bytes, 0, byteCount);
97+
98+
bitmap.UnlockBits(bmpData);
99+
100+
for (int i = 0; i < bytes.Length; i += bytesPerPixel)
115101
{
116-
var color = bitmap[x, y];
117-
bytes[i++] = color.R;
118-
bytes[i++] = color.G;
119-
bytes[i++] = color.B;
120-
bytes[i++] = 0xff;
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;
121110
}
111+
112+
return bytes;
122113
}
123-
return bytes;
124114
}
125115
}
126116
}

src/MiNET/MiNET/MiNET.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
<PackageReference Include="MiNET.LevelDB" Version="1.0.49" />
5050
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
5151
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
52-
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.3" />
53-
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta14" />
5452
<PackageReference Include="System.Drawing.Common" Version="8.0.3" />
5553
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="8.0.0" />
5654
</ItemGroup>

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@
2525

2626
using System;
2727
using System.Collections.Generic;
28+
using System.Drawing;
2829
using System.Drawing.Imaging;
2930
using Newtonsoft.Json;
3031
using Newtonsoft.Json.Converters;
3132
using Newtonsoft.Json.Serialization;
32-
using SixLabors.ImageSharp;
33-
using SixLabors.ImageSharp.Formats.Png;
34-
using SixLabors.ImageSharp.PixelFormats;
35-
using Color = System.Drawing.Color;
3633

3734
namespace MiNET.Utils.Skins
3835
{
@@ -106,7 +103,7 @@ public SkinResourcePatch SkinResourcePatch
106103

107104
public static byte[] GetTextureFromFile(string filename)
108105
{
109-
var bitmap = Image.Load<Rgba32>(filename);// new Image<Rgba32>(filename);
106+
Bitmap bitmap = new Bitmap(filename);
110107

111108
var size = bitmap.Height * bitmap.Width * 4;
112109

@@ -119,7 +116,7 @@ public static byte[] GetTextureFromFile(string filename)
119116
{
120117
for (int x = 0; x < bitmap.Width; x++)
121118
{
122-
var color = bitmap[x, y];
119+
var color = bitmap.GetPixel(x, y);
123120
bytes[i++] = color.R;
124121
bytes[i++] = color.G;
125122
bytes[i++] = color.B;
@@ -137,7 +134,15 @@ public static void SaveTextureToFile(string filename, byte[] bytes)
137134
int width = size == 0x10000 ? 128 : 64;
138135
var height = size == 0x2000 ? 32 : (size == 0x4000 ? 64 : 128);
139136

140-
var bitmap = new Image<Rgba32>(width, height);
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);
141146

142147
int i = 0;
143148
for (int y = 0; y < bitmap.Height; y++)
@@ -149,10 +154,19 @@ public static void SaveTextureToFile(string filename, byte[] bytes)
149154
byte b = bytes[i++];
150155
byte a = bytes[i++];
151156

152-
bitmap[x, y] = new Rgba32(r, g, b, a);
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; }
153163
}
154164
}
155-
165+
166+
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, bmpData.Scan0, byteCount);
167+
168+
bitmap.UnlockBits(bmpData);
169+
156170
bitmap.Save(filename);
157171
}
158172

0 commit comments

Comments
 (0)