Skip to content

Commit

Permalink
Use ImageSharp instead for image decoding/resizing in dotnet build
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Aug 16, 2022
1 parent e36b969 commit 522260d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Expand Up @@ -5,6 +5,6 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\MCGalaxy\MCGalaxy_core.csproj" />
<ProjectReference Include="..\MCGalaxy\MCGalaxy_dotnet.csproj" />
</ItemGroup>
</Project>
Expand Up @@ -5,7 +5,7 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="4.5.0" />
<PackageReference Include="MySql.Data" Version="6.7.9" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
</ItemGroup>
</Project>
54 changes: 54 additions & 0 deletions MCGalaxy/util/ImageUtils.cs
Expand Up @@ -16,9 +16,16 @@
permissions and limitations under the Licenses.
*/
using System;
#if !NETSTANDARD
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
#else
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Transforms;
#endif
using System.IO;

namespace MCGalaxy.Util
Expand All @@ -41,9 +48,14 @@ public abstract class IBitmap2D : IDisposable

public abstract void Dispose();

#if !NETSTANDARD
public static IBitmap2D Create() { return new GDIPlusBitmap(); }
#else
public static IBitmap2D Create() { return new ImageSharpBitmap(); }
#endif
}

#if !NETSTANDARD
unsafe sealed class GDIPlusBitmap : IBitmap2D
{
Image img;
Expand Down Expand Up @@ -138,4 +150,46 @@ unsafe sealed class GDIPlusBitmap : IBitmap2D
data = null;
}
}
#else
unsafe sealed class ImageSharpBitmap : IBitmap2D
{
Image<Rgba32> img;

public override void Decode(byte[] data) {
img = Image.Load<Rgba32>(data);
UpdateDimensions();
Get = GetPixel;
}

public override void Resize(int width, int height, bool hq) {
IResampler resampler = hq ? KnownResamplers.Bicubic : KnownResamplers.NearestNeighbor;
img.Mutate(x => x.Resize(width, height, resampler));
UpdateDimensions();
}

void UpdateDimensions() {
Width = img.Width;
Height = img.Height;
}

Pixel GetPixel(int x, int y) {
Pixel pixel;
Rgba32 src = img[x, y];
pixel.A = src.A;
pixel.R = src.R;
pixel.G = src.G;
pixel.B = src.B; // TODO avoid overhead by direct blit??
return pixel;
}

public override void Dispose() {
if (img != null) img.Dispose();
img = null;
}


public override void LockBits() { }
public override void UnlockBits() { }
}
#endif
}

0 comments on commit 522260d

Please sign in to comment.