mirror of
https://github.com/aelurum/AssetStudio.git
synced 2025-05-25 05:40:21 -04:00
Don't use ImageSharp for texture processing
- returned to System.Drawing (ImageSharp is a good lib, but too slow for such app, IMO)
This commit is contained in:
parent
9cbe91decb
commit
571ea2da4a
@ -763,10 +763,9 @@ namespace AssetStudioGUI
|
||||
|
||||
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)
|
||||
{
|
||||
@ -1174,10 +1173,9 @@ namespace AssetStudioGUI
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -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>
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
{
|
||||
@ -26,7 +23,7 @@ namespace AssetStudio
|
||||
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))
|
||||
{
|
||||
@ -45,9 +42,9 @@ namespace AssetStudio
|
||||
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)
|
||||
@ -59,23 +56,29 @@ namespace AssetStudio
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -86,32 +89,41 @@ namespace AssetStudio
|
||||
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 points = triangles.Select(x => x.Select(y => new PointF(y.X, y.Y)).ToArray());
|
||||
using (var path = new GraphicsPath())
|
||||
{
|
||||
foreach (var p in points)
|
||||
{
|
||||
path.AddPolygon(p);
|
||||
}
|
||||
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
|
||||
{
|
||||
matrix *= Matrix3x2.CreateTranslation(m_Sprite.m_Rect.width * 0.5f - textureRectOffset.X, m_Sprite.m_Rect.height * 0.5f - textureRectOffset.Y);
|
||||
matr.Translate(m_Sprite.m_Rect.width * 0.5f - textureRectOffset.X, m_Sprite.m_Rect.height * 0.5f - textureRectOffset.Y);
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
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);
|
||||
}
|
||||
path = path.Transform(matrix);
|
||||
var options = new DrawingOptions
|
||||
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))
|
||||
{
|
||||
GraphicsOptions = new GraphicsOptions()
|
||||
using (var brush = new TextureBrush(spriteImage))
|
||||
{
|
||||
AlphaCompositionMode = PixelAlphaCompositionMode.DestOut
|
||||
graphic.FillPath(brush, path);
|
||||
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
|
||||
spriteImage.Dispose();
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
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;
|
||||
}
|
||||
catch
|
||||
{
|
||||
@ -120,7 +132,7 @@ namespace AssetStudio
|
||||
}
|
||||
|
||||
//Rectangle
|
||||
spriteImage.Mutate(x => x.Flip(FlipMode.Vertical));
|
||||
spriteImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
|
||||
return spriteImage;
|
||||
}
|
||||
}
|
||||
|
5963
AssetStudioUtility/TGASharpLib.cs
Normal file
5963
AssetStudioUtility/TGASharpLib.cs
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,31 +1,34 @@
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.IO;
|
||||
|
||||
namespace AssetStudio
|
||||
{
|
||||
public static class Texture2DExtensions
|
||||
{
|
||||
public static Image ConvertToImage(this Texture2D m_Texture2D, bool flip)
|
||||
public static Bitmap ConvertToBitmap(this Texture2D m_Texture2D, bool flip)
|
||||
{
|
||||
var converter = new Texture2DConverter(m_Texture2D);
|
||||
var bytes = converter.DecodeTexture2D();
|
||||
if (bytes != null && bytes.Length > 0)
|
||||
{
|
||||
var image = Image.LoadPixelData<Bgra32>(bytes, m_Texture2D.m_Width, m_Texture2D.m_Height);
|
||||
var bitmap = new Bitmap(m_Texture2D.m_Width, m_Texture2D.m_Height, PixelFormat.Format32bppArgb);
|
||||
var bmpData = bitmap.LockBits(new Rectangle(0, 0, m_Texture2D.m_Width, m_Texture2D.m_Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
||||
Marshal.Copy(bytes, 0, bmpData.Scan0, bytes.Length);
|
||||
bitmap.UnlockBits(bmpData);
|
||||
if (flip)
|
||||
{
|
||||
image.Mutate(x => x.Flip(FlipMode.Vertical));
|
||||
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
|
||||
}
|
||||
return image;
|
||||
return bitmap;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static MemoryStream ConvertToStream(this Texture2D m_Texture2D, ImageFormat imageFormat, bool flip)
|
||||
{
|
||||
var image = ConvertToImage(m_Texture2D, flip);
|
||||
var image = ConvertToBitmap(m_Texture2D, flip);
|
||||
if (image != null)
|
||||
{
|
||||
using (image)
|
||||
|
Loading…
Reference in New Issue
Block a user