Skip to content

Commit

Permalink
Direct copy pixel memory from image instead of saving to memory stream
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Mar 25, 2022
1 parent 5ee2f46 commit 44420e8
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions EOLib.Graphics/NativeGraphicsManager.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using AutomaticTypeMapper;
using Microsoft.Xna.Framework.Graphics;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace EOLib.Graphics
Expand Down Expand Up @@ -45,12 +47,21 @@ public Texture2D TextureFromResource(GFXTypes file, int resourceVal, bool transp
}
}

using (var mem = new System.IO.MemoryStream())
using (var i = BitmapFromResource(file, resourceVal, transparent))
{
using (var i = BitmapFromResource(file, resourceVal, transparent))
((Image)i).Save(mem, new PngEncoder());

ret = Texture2D.FromStream(_graphicsDeviceProvider.GraphicsDevice, mem);
if (!i.DangerousTryGetSinglePixelMemory(out var mem))
{
using (var ms = new MemoryStream())
{
i.SaveAsPng(ms);
ret = Texture2D.FromStream(_graphicsDeviceProvider.GraphicsDevice, ms);
}
}
else
{
ret = new Texture2D(_graphicsDeviceProvider.GraphicsDevice, i.Width, i.Height);
ret.SetData(mem.ToArray());
}
}

lock (__cachelock__)
Expand All @@ -61,9 +72,9 @@ public Texture2D TextureFromResource(GFXTypes file, int resourceVal, bool transp
return ret;
}

private IImage BitmapFromResource(GFXTypes file, int resourceVal, bool transparent)
private Image<Rgba32> BitmapFromResource(GFXTypes file, int resourceVal, bool transparent)
{
var ret = (Image)_gfxLoader.LoadGFX(file, resourceVal);
var ret = (Image<Rgba32>)_gfxLoader.LoadGFX(file, resourceVal);

if (transparent)
{
Expand Down

0 comments on commit 44420e8

Please sign in to comment.