mirror of
https://git.huckle.dev/Huckles-Minecraft-Archive/PCK-Studio.git
synced 2026-06-05 18:35:03 +00:00
Core(ImageExtensions) - Add 'GetImageData' & 'CreateMipMap'
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user