From ef5a0f0e57c146db7da703d926ecee7090555e5d Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Wed, 26 Nov 2025 06:51:14 +0100 Subject: [PATCH] Core(ImageExtensions) - Add 'GetImageData' & 'CreateMipMap' --- PCK-Studio/Controls/PckAssetBrowserEditor.cs | 13 +------- PckStudio.Core/Extensions/ImageExtensions.cs | 33 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/PCK-Studio/Controls/PckAssetBrowserEditor.cs b/PCK-Studio/Controls/PckAssetBrowserEditor.cs index 671edc65..47a51432 100644 --- a/PCK-Studio/Controls/PckAssetBrowserEditor.cs +++ b/PCK-Studio/Controls/PckAssetBrowserEditor.cs @@ -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); diff --git a/PckStudio.Core/Extensions/ImageExtensions.cs b/PckStudio.Core/Extensions/ImageExtensions.cs index 36563243..6f9549f4 100644 --- a/PckStudio.Core/Extensions/ImageExtensions.cs +++ b/PckStudio.Core/Extensions/ImageExtensions.cs @@ -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; + } + /// /// Creates an IEnumerable by reading in horizontal order /// @@ -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 sources, ImageLayoutDirection layoutDirection) { bool horizontal = layoutDirection == ImageLayoutDirection.Horizontal;