Skip to content

Commit

Permalink
Don't use ImageSharp for texture processing
Browse files Browse the repository at this point in the history
- returned to System.Drawing
(ImageSharp is a good lib, but too slow for such app, IMO)
  • Loading branch information
aelurum committed Nov 24, 2021
1 parent 9cbe91d commit 571ea2d
Show file tree
Hide file tree
Showing 6 changed files with 6,039 additions and 70 deletions.
10 changes: 4 additions & 6 deletions AssetStudioGUI/AssetStudioGUIForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -763,10 +763,9 @@ private void PreviewAsset(AssetItem assetItem)

private void PreviewTexture2D(AssetItem assetItem, Texture2D m_Texture2D)
{
var stream = m_Texture2D.ConvertToStream(ImageFormat.Png, true);
if (stream != null)
var bitmap = m_Texture2D.ConvertToBitmap(true);
if (bitmap != null)
{
var bitmap = new Bitmap(stream);
assetItem.InfoText = $"Width: {m_Texture2D.m_Width}\nHeight: {m_Texture2D.m_Height}\nFormat: {m_Texture2D.m_TextureFormat}";
switch (m_Texture2D.m_TextureSettings.m_FilterMode)
{
Expand Down Expand Up @@ -1174,10 +1173,9 @@ private void PreviewMesh(Mesh m_Mesh)

private void PreviewSprite(AssetItem assetItem, Sprite m_Sprite)
{
var stream = m_Sprite.GetImage(ImageFormat.Png);
if (stream != null)
var bitmap = m_Sprite.GetImage();
if (bitmap != null)
{
var bitmap = new Bitmap(stream);
assetItem.InfoText = $"Width: {bitmap.Width}\nHeight: {bitmap.Height}\n";
PreviewTexture(bitmap);
}
Expand Down
2 changes: 1 addition & 1 deletion AssetStudioUtility/AssetStudioUtility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.11.3" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta13" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
25 changes: 9 additions & 16 deletions AssetStudioUtility/ImageExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Tga;
using System.Drawing;
using System.IO;
using TGASharpLib;

namespace AssetStudio
{
public static class ImageExtensions
{
public static MemoryStream ConvertToStream(this Image image, ImageFormat imageFormat)
public static MemoryStream ConvertToStream(this Bitmap image, ImageFormat imageFormat)
{
var outputStream = new MemoryStream();
switch (imageFormat)
{
case ImageFormat.Jpeg:
image.SaveAsJpeg(outputStream);
image.Save(outputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case ImageFormat.Png:
image.SaveAsPng(outputStream);
image.Save(outputStream, System.Drawing.Imaging.ImageFormat.Png);
break;
case ImageFormat.Bmp:
image.Save(outputStream, new BmpEncoder
{
BitsPerPixel = BmpBitsPerPixel.Pixel32,
SupportTransparency = true
});
image.Save(outputStream, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case ImageFormat.Tga:
image.Save(outputStream, new TgaEncoder
{
BitsPerPixel = TgaBitsPerPixel.Pixel32,
Compression = TgaCompression.None
});
var tga = new TGA(image);
tga.Save(outputStream);
break;
}
image.Dispose();
return outputStream;
}
}
Expand Down
90 changes: 51 additions & 39 deletions AssetStudioUtility/SpriteHelper.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;

namespace AssetStudio
{
Expand All @@ -26,7 +23,7 @@ public static MemoryStream GetImage(this Sprite m_Sprite, ImageFormat imageForma
return null;
}

public static Image GetImage(this Sprite m_Sprite)
public static Bitmap GetImage(this Sprite m_Sprite)
{
if (m_Sprite.m_SpriteAtlas != null && m_Sprite.m_SpriteAtlas.TryGet(out var m_SpriteAtlas))
{
Expand All @@ -45,9 +42,9 @@ public static Image GetImage(this Sprite m_Sprite)
return null;
}

private static Image CutImage(Texture2D m_Texture2D, Sprite m_Sprite, Rectf textureRect, Vector2 textureRectOffset, SpriteSettings settingsRaw)
private static Bitmap CutImage(Texture2D m_Texture2D, Sprite m_Sprite, Rectf textureRect, Vector2 textureRectOffset, SpriteSettings settingsRaw)
{
var originalImage = m_Texture2D.ConvertToImage(false);
var originalImage = m_Texture2D.ConvertToBitmap(false);
if (originalImage != null)
{
using (originalImage)
Expand All @@ -59,23 +56,29 @@ private static Image CutImage(Texture2D m_Texture2D, Sprite m_Sprite, Rectf text
rectRight = Math.Min(rectRight, m_Texture2D.m_Width);
rectBottom = Math.Min(rectBottom, m_Texture2D.m_Height);
var rect = new Rectangle(rectX, rectY, rectRight - rectX, rectBottom - rectY);
var spriteImage = originalImage.Clone(x => x.Crop(rect));

var spriteImage = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
var destRect = new Rectangle(0, 0, rect.Width, rect.Height);
using (var graphic = Graphics.FromImage(spriteImage))
{
graphic.DrawImage(originalImage, destRect, rect, GraphicsUnit.Pixel);
}
if (settingsRaw.packed == 1)
{
//RotateAndFlip
switch (settingsRaw.packingRotation)
{
case SpritePackingRotation.kSPRFlipHorizontal:
spriteImage.Mutate(x => x.Flip(FlipMode.Horizontal));
spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipX);
break;
case SpritePackingRotation.kSPRFlipVertical:
spriteImage.Mutate(x => x.Flip(FlipMode.Vertical));
spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
break;
case SpritePackingRotation.kSPRRotate180:
spriteImage.Mutate(x => x.Rotate(180));
spriteImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
break;
case SpritePackingRotation.kSPRRotate90:
spriteImage.Mutate(x => x.Rotate(270));
spriteImage.RotateFlip(RotateFlipType.Rotate270FlipNone);
break;
}
}
Expand All @@ -86,32 +89,41 @@ private static Image CutImage(Texture2D m_Texture2D, Sprite m_Sprite, Rectf text
try
{
var triangles = GetTriangles(m_Sprite.m_RD);
var polygons = triangles.Select(x => new Polygon(new LinearLineSegment(x.Select(y => new PointF(y.X, y.Y)).ToArray()))).ToArray();
IPathCollection path = new PathCollection(polygons);
var matrix = Matrix3x2.CreateScale(m_Sprite.m_PixelsToUnits);
var version = m_Sprite.version;
if (version[0] < 5
|| (version[0] == 5 && version[1] < 4)
|| (version[0] == 5 && version[1] == 4 && version[2] <= 1)) //5.4.1p3 down
{
matrix *= Matrix3x2.CreateTranslation(m_Sprite.m_Rect.width * 0.5f - textureRectOffset.X, m_Sprite.m_Rect.height * 0.5f - textureRectOffset.Y);
}
else
var points = triangles.Select(x => x.Select(y => new PointF(y.X, y.Y)).ToArray());
using (var path = new GraphicsPath())
{
matrix *= Matrix3x2.CreateTranslation(m_Sprite.m_Rect.width * m_Sprite.m_Pivot.X - textureRectOffset.X, m_Sprite.m_Rect.height * m_Sprite.m_Pivot.Y - textureRectOffset.Y);
}
path = path.Transform(matrix);
var options = new DrawingOptions
{
GraphicsOptions = new GraphicsOptions()
foreach (var p in points)
{
AlphaCompositionMode = PixelAlphaCompositionMode.DestOut
path.AddPolygon(p);
}
};
var rectP = new RectangularPolygon(0, 0, rect.Width, rect.Height);
spriteImage.Mutate(x => x.Fill(options, SixLabors.ImageSharp.Color.Red, rectP.Clip(path)));
spriteImage.Mutate(x => x.Flip(FlipMode.Vertical));
return spriteImage;
using (var matr = new Matrix())
{
var version = m_Sprite.version;
if (version[0] < 5
|| (version[0] == 5 && version[1] < 4)
|| (version[0] == 5 && version[1] == 4 && version[2] <= 1)) //5.4.1p3 down
{
matr.Translate(m_Sprite.m_Rect.width * 0.5f - textureRectOffset.X, m_Sprite.m_Rect.height * 0.5f - textureRectOffset.Y);
}
else
{
matr.Translate(m_Sprite.m_Rect.width * m_Sprite.m_Pivot.X - textureRectOffset.X, m_Sprite.m_Rect.height * m_Sprite.m_Pivot.Y - textureRectOffset.Y);
}
matr.Scale(m_Sprite.m_PixelsToUnits, m_Sprite.m_PixelsToUnits);
path.Transform(matr);
var bitmap = new Bitmap(rect.Width, rect.Height);
using (var graphic = Graphics.FromImage(bitmap))
{
using (var brush = new TextureBrush(spriteImage))
{
graphic.FillPath(brush, path);
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
spriteImage.Dispose();
return bitmap;
}
}
}
}
}
catch
{
Expand All @@ -120,7 +132,7 @@ private static Image CutImage(Texture2D m_Texture2D, Sprite m_Sprite, Rectf text
}

//Rectangle
spriteImage.Mutate(x => x.Flip(FlipMode.Vertical));
spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
return spriteImage;
}
}
Expand Down
Loading

0 comments on commit 571ea2d

Please sign in to comment.