Core(ImageExtensions) - Add 'GetImageData' & 'CreateMipMap'

This commit is contained in:
miku-666
2025-11-26 06:51:14 +01:00
parent 42594e7029
commit ef5a0f0e57
2 changed files with 34 additions and 12 deletions

View File

@@ -1514,18 +1514,7 @@ namespace PckStudio.Controls
PckAsset mipMappedAsset = new PckAsset(mippedPath, PckAssetType.TextureFile);
Image originalTexture = asset.GetTexture();
int newWidth = Math.Max(originalTexture.Width / (int)Math.Pow(2, i - 1), 1);
int newHeight = Math.Max(originalTexture.Height / (int)Math.Pow(2, i - 1), 1);
Rectangle tileArea = new Rectangle(0, 0, newWidth, newHeight);
Image mippedTexture = new Bitmap(newWidth, newHeight);
using (Graphics gfx = Graphics.FromImage(mippedTexture))
{
gfx.SmoothingMode = SmoothingMode.None;
gfx.InterpolationMode = InterpolationMode.NearestNeighbor;
gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
gfx.DrawImage(originalTexture, tileArea);
}
Image mippedTexture = originalTexture.CreateMipMap(i);
mipMappedAsset.SetTexture(mippedTexture);

View File

@@ -22,6 +22,7 @@ using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -49,6 +50,29 @@ namespace PckStudio.Core.Extensions
return result;
}
public static byte[] GetImageData(this Image source, ImageFormat format = default)
{
format ??= ImageFormat.Png;
var stream = new MemoryStream();
source.Save(stream, format);
return stream.ToArray();
}
public static Image CreateMipMap(this Image image, int level)
{
int factor = (int)Math.Pow(2, level - 1);
int newWidth = Math.Max(image.Width / factor, 1);
int newHeight = Math.Max(image.Height / factor, 1);
Image mippedTexture = new Bitmap(newWidth, newHeight);
using (Graphics gfx = Graphics.FromImage(mippedTexture))
{
gfx.ApplyConfig(GraphicsConfig.PixelPerfect());
gfx.DrawImage(image, new Rectangle(0, 0, newWidth, newHeight));
}
return mippedTexture;
}
/// <summary>
/// Creates an IEnumerable by reading in horizontal order
/// </summary>
@@ -92,6 +116,15 @@ namespace PckStudio.Core.Extensions
yield break;
}
public static Image Combine(this Image first, Image second, ImageLayoutDirection layoutDirection)
{
bool horizontal = layoutDirection == ImageLayoutDirection.Horizontal;
int imgCount = 2;
int rows = horizontal ? imgCount : 1;
int columns = horizontal ? 1 : imgCount;
return new Image[] {first, second }.Combine(rows, columns, layoutDirection);
}
public static Image Combine(this IEnumerable<Image> sources, ImageLayoutDirection layoutDirection)
{
bool horizontal = layoutDirection == ImageLayoutDirection.Horizontal;