Skip to content

Commit c31ded5

Browse files
Stop using system.drawing/imagesharp
1 parent 9a431bd commit c31ded5

File tree

13 files changed

+136
-345
lines changed

13 files changed

+136
-345
lines changed

MCGalaxy/Commands/building/CmdImageprint.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ permissions and limitations under the Licenses.
2323
using MCGalaxy.Maths;
2424
using MCGalaxy.Network;
2525
using MCGalaxy.Util;
26+
using MCGalaxy.Util.Imaging;
2627
using BlockID = System.UInt16;
2728

2829
namespace MCGalaxy.Commands.Building {
@@ -121,7 +122,7 @@ void DoDrawImage(Player p, Vec3S32[] m, DrawArgs dArgs) {
121122
}
122123

123124
void DoDrawImageCore(Player p, Vec3S32[] marks, DrawArgs dArgs) {
124-
IBitmap2D bmp = ImageUtils.DecodeImage(dArgs.Data, p);
125+
Bitmap2D bmp = ImageUtils.DecodeImage(dArgs.Data, p);
125126
if (bmp == null) return;
126127

127128
ImagePrintDrawOp op = dArgs.Dithered ? new ImagePrintDitheredDrawOp() : new ImagePrintDrawOp();
@@ -133,7 +134,7 @@ void DoDrawImageCore(Player p, Vec3S32[] marks, DrawArgs dArgs) {
133134
Clamp(p, marks, op, ref width, ref height);
134135

135136
if (width < bmp.Width || height < bmp.Height) {
136-
bmp.Resize(width, height, true);
137+
bmp = ImageUtils.ResizeBilinear(bmp, width, height);
137138
}
138139

139140
op.Source = bmp; op.Palette = dArgs.Pal;

MCGalaxy/Drawing/Image/IPaletteMatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ permissions and limitations under the Licenses.
1717
*/
1818
using System;
1919
using BlockID = System.UInt16;
20-
using MCGalaxy.Util;
20+
using MCGalaxy.Util.Imaging;
2121

2222
namespace MCGalaxy.Drawing
2323
{

MCGalaxy/Drawing/Image/ImagePrintDrawOp.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ permissions and limitations under the Licenses.
1919
using MCGalaxy.Drawing.Brushes;
2020
using MCGalaxy.Maths;
2121
using MCGalaxy.Util;
22+
using MCGalaxy.Util.Imaging;
2223
using BlockID = System.UInt16;
2324

2425
namespace MCGalaxy.Drawing.Ops
@@ -31,7 +32,7 @@ public override long BlocksAffected(Level lvl, Vec3S32[] marks) {
3132
return Source.Width * Source.Height;
3233
}
3334

34-
internal IBitmap2D Source;
35+
internal Bitmap2D Source;
3536
internal bool DualLayer, LayerMode;
3637
public ImagePalette Palette;
3738

@@ -42,12 +43,7 @@ public override void Perform(Vec3S32[] marks, Brush brush, DrawOpOutput output)
4243
selector = new RgbPaletteMatcher();
4344
CalcLayerColors();
4445

45-
try {
46-
Source.LockBits();
47-
OutputPixels(output);
48-
} finally {
49-
Source.UnlockBits();
50-
}
46+
OutputPixels(output);
5147
selector = null;
5248

5349
// Put all the blocks in shadow
@@ -63,7 +59,6 @@ public override void Perform(Vec3S32[] marks, Brush brush, DrawOpOutput output)
6359
}
6460
}
6561

66-
Source.Dispose();
6762
Source = null;
6863
Player.Message("Finished printing image using {0} palette.", Palette.Name);
6964
}

MCGalaxy/Generator/HeightmapGen.cs

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ permissions and limitations under the Licenses.
1818
using System;
1919
using MCGalaxy.Network;
2020
using MCGalaxy.Util;
21+
using MCGalaxy.Util.Imaging;
2122

2223
namespace MCGalaxy.Generator
2324
{
@@ -37,49 +38,47 @@ public static bool Generate(Player p, Level lvl, MapGenArgs args) {
3738

3839
byte[] data = HttpUtil.DownloadImage(url, p);
3940
if (data == null) return false;
40-
IBitmap2D bmp = ImageUtils.DecodeImage(data, p);
41+
42+
Bitmap2D bmp = ImageUtils.DecodeImage(data, p);
4143
if (bmp == null) return false;
4244

4345
int index = 0, oneY = lvl.Width * lvl.Length;
4446
int lvlWidth = lvl.Width;
4547
int lvlLength = lvl.Length;
4648

47-
using (bmp) {
48-
bool resized = bmp.Width != lvlWidth || bmp.Height != lvlLength;
49-
50-
if (resized) {
51-
p.Message("&cHeightmap size ({0}x{1}) does not match Width x Length ({2}x{3}) of the level",
52-
bmp.Width, bmp.Height, lvlWidth, lvlLength);
53-
p.Message("&cAs such, the map may not look accurate.");
54-
}
55-
bmp.LockBits();
56-
57-
byte[] hmap = resized ? ResizeHeightmap(bmp, lvlWidth, lvlLength)
58-
: ComputeHeightmap(bmp);
49+
bool resized = bmp.Width != lvlWidth || bmp.Height != lvlLength;
50+
51+
if (resized) {
52+
p.Message("&cHeightmap size ({0}x{1}) does not match Width x Length ({2}x{3}) of the level",
53+
bmp.Width, bmp.Height, lvlWidth, lvlLength);
54+
p.Message("&cAs such, the map may not look accurate.");
55+
}
56+
57+
byte[] hmap = resized ? ResizeHeightmap(bmp, lvlWidth, lvlLength)
58+
: ComputeHeightmap(bmp);
5959

60-
for (int z = 0; z < lvlLength; z++)
61-
for (int x = 0; x < lvlWidth; x++)
60+
for (int z = 0; z < lvlLength; z++)
61+
for (int x = 0; x < lvlWidth; x++)
62+
{
63+
int height = hmap[index];
64+
byte layer = biome.Ground, top = biome.Surface;
65+
66+
if (
67+
IsCliff(height, hmap, lvl, x - 1, z) ||
68+
IsCliff(height, hmap, lvl, x + 1, z) ||
69+
IsCliff(height, hmap, lvl, x, z - 1) ||
70+
IsCliff(height, hmap, lvl, x, z + 1))
6271
{
63-
int height = hmap[index];
64-
byte layer = biome.Ground, top = biome.Surface;
65-
66-
if (
67-
IsCliff(height, hmap, lvl, x - 1, z) ||
68-
IsCliff(height, hmap, lvl, x + 1, z) ||
69-
IsCliff(height, hmap, lvl, x, z - 1) ||
70-
IsCliff(height, hmap, lvl, x, z + 1))
71-
{
72-
layer = biome.Cliff; top = biome.Cliff;
73-
}
74-
75-
// remap from 0..255 to 0..lvl.Height
76-
height = height * lvl.Height / 255;
77-
for (int y = 0; y < height - 1; y++)
78-
lvl.blocks[index + oneY * y] = layer;
79-
if (height > 0)
80-
lvl.blocks[index + oneY * (height - 1)] = top;
81-
index++;
72+
layer = biome.Cliff; top = biome.Cliff;
8273
}
74+
75+
// remap from 0..255 to 0..lvl.Height
76+
height = height * lvl.Height / 255;
77+
for (int y = 0; y < height - 1; y++)
78+
lvl.blocks[index + oneY * y] = layer;
79+
if (height > 0)
80+
lvl.blocks[index + oneY * (height - 1)] = top;
81+
index++;
8382
}
8483
return true;
8584
}
@@ -91,20 +90,21 @@ static bool IsCliff(int height, byte[] hmap, Level lvl, int x, int z) {
9190
return height >= neighbourHeight + 2;
9291
}
9392

94-
static byte[] ComputeHeightmap(IBitmap2D bmp) {
93+
static byte[] ComputeHeightmap(Bitmap2D bmp) {
9594
byte[] hmap = new byte[bmp.Width * bmp.Height];
9695
int i = 0;
9796

9897
for (int y = 0; y < bmp.Height; y++)
9998
for (int x = 0; x < bmp.Width; x++)
10099
{
101-
hmap[i++] = bmp.Get(x, y).R;
100+
hmap[i] = bmp.Pixels[i].R;
101+
i++;
102102
}
103103
return hmap;
104104
}
105105

106106
// Calculates adjusted X/Y coordinates using nearest neighbour resizing
107-
static byte[] ResizeHeightmap(IBitmap2D bmp, int dstWidth, int dstHeight) {
107+
static byte[] ResizeHeightmap(Bitmap2D bmp, int dstWidth, int dstHeight) {
108108
byte[] hmap = new byte[dstWidth * dstHeight];
109109
int i = 0;
110110

MCGalaxy/MCGalaxy_.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
</Reference>
6060
<Reference Include="System" />
6161
<Reference Include="System.Data" />
62-
<Reference Include="System.Drawing" />
6362
</ItemGroup>
6463
<ItemGroup>
6564
<Compile Include="Blocks\Behaviour\BlockBehaviour.cs" />

MCGalaxy/MCGalaxy_dotnet.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@
88
</PropertyGroup>
99
<ItemGroup>
1010
<PackageReference Include="MySql.Data" Version="8.1.0" />
11-
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.11" />
1211
</ItemGroup>
1312
</Project>

MCGalaxy/MCGalaxy_standalone.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,4 @@
55
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
66
<DefineConstants>$(DefineConstants);MCG_DOTNET;MCG_STANDALONE</DefineConstants>
77
</PropertyGroup>
8-
<ItemGroup>
9-
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.11" />
10-
</ItemGroup>
118
</Project>

0 commit comments

Comments
 (0)