Skip to content

Commit

Permalink
Replace "System.Drawing" with "SkiaSharp" in all scripts. (#24)
Browse files Browse the repository at this point in the history
* Restore WIP script changes

* Finish removing/replacing of "System.Drawing" in all scripts (not tested)

* Update MergeImages.csx

* Update ImportMasks.csx

---------

Co-authored-by: Miepee <[email protected]>
  • Loading branch information
VladiStep and Miepee committed Jun 2, 2024
1 parent a8337d0 commit 2ba06eb
Show file tree
Hide file tree
Showing 14 changed files with 274 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.IO;
using System;
using System.Drawing;
using System.Windows.Forms;
using UndertaleModLib.Util;

Expand All @@ -25,10 +24,10 @@ else if (GameName == "deltarune chapter 1&2")
}
if (Data.GeneralInfo.Name.Content == "NXTALE" || Data.GeneralInfo.Name.Content.StartsWith("UNDERTALE"))
{
if (!ScriptQuestion("Would you like to apply this mod?"))
{
return;
}
if (!ScriptQuestion("Would you like to apply this mod?"))
{
return;
}
}
else if (Data.GeneralInfo.DisplayName.Content == "SURVEY_PROGRAM" || Data.GeneralInfo.DisplayName.Content == "DELTARUNE Chapter 1")
{
Expand Down
23 changes: 12 additions & 11 deletions UndertaleModTool/Scripts/Community Scripts/ImportGMS2FontData.csx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using System;
using System.IO;
using System.Drawing;
using SkiaSharp;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -11,6 +11,7 @@ using UndertaleModLib;
using UndertaleModLib.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UndertaleModLib.Util;

EnsureDataLoaded();

Expand Down Expand Up @@ -96,10 +97,10 @@ else if (attemptToFixFontNotAppearing)
fontTexGroup.Fonts.Add(new UndertaleResourceById<UndertaleFont, UndertaleChunkFONT>() { Resource = font });
}

// Prepare font texture
Bitmap textureBitmap = new Bitmap(fontTexturePath);
// Make the DPI exactly 96 for this bitmap
textureBitmap.SetResolution(96.0F, 96.0F);
// Get texture properties
var imgSize = TextureWorker.GetImageSizeFromFile(fontTexturePath);
ushort width = (ushort)imgSize.Width;
ushort height = (ushort)imgSize.Height;

UndertaleEmbeddedTexture texture = new UndertaleEmbeddedTexture();
// ??? Why?
Expand All @@ -115,14 +116,14 @@ texturePageItem.Name = new UndertaleString("PageItem " + Data.TexturePageItems.C
texturePageItem.TexturePage = texture;
texturePageItem.SourceX = 0;
texturePageItem.SourceY = 0;
texturePageItem.SourceWidth = (ushort)textureBitmap.Width;
texturePageItem.SourceHeight = (ushort)textureBitmap.Height;
texturePageItem.SourceWidth = width;
texturePageItem.SourceHeight = height;
texturePageItem.TargetX = 0;
texturePageItem.TargetY = 0;
texturePageItem.TargetWidth = (ushort)textureBitmap.Width;
texturePageItem.TargetHeight = (ushort)textureBitmap.Height;
texturePageItem.BoundingWidth = (ushort)textureBitmap.Width;
texturePageItem.BoundingHeight = (ushort)textureBitmap.Height;
texturePageItem.TargetWidth = width;
texturePageItem.TargetHeight = height;
texturePageItem.BoundingWidth = width;
texturePageItem.BoundingHeight = height;
Data.TexturePageItems.Add(texturePageItem);

font.DisplayName = Data.Strings.MakeString((string)fontData["fontName"]);
Expand Down
26 changes: 6 additions & 20 deletions UndertaleModTool/Scripts/Community Scripts/ScaleAllTextures.csx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using SkiaSharp;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -21,9 +19,9 @@ if (!ScriptQuestion("Visual glitches are very likely to occur in game. Do you ac

TextureWorker worker = new TextureWorker();
double scale = -1;
bool SelectScale = true;
bool selectScale = true;

if (SelectScale)
if (selectScale)
{
bool success = false;
while (scale <= 0 || scale > 10)
Expand Down Expand Up @@ -131,9 +129,8 @@ ChangeSelection(Data.Rooms.ByName("room_ruins1"));

void ScaleEmbeddedTexture(UndertaleEmbeddedTexture tex)
{
Bitmap embImage = worker.GetEmbeddedTexture(tex);
embImage = ResizeBitmap(embImage, (int)(embImage.Width * scale), (int)(embImage.Height * scale));
embImage.SetResolution(96.0F, 96.0F);
SKBitmap embImage = worker.GetEmbeddedTexture(tex);
embImage = TextureWorker.ResizeImage(embImage, (int)(embImage.Width * scale), (int)(embImage.Height * scale), useNearestNeighbor: true);
try
{
var width = (uint)embImage.Width;
Expand All @@ -144,7 +141,7 @@ void ScaleEmbeddedTexture(UndertaleEmbeddedTexture tex)
}
using (var stream = new MemoryStream())
{
embImage.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
embImage.Encode(stream, SKEncodedImageFormat.Png, 100);
tex.TextureData.TextureBlob = stream.ToArray();
}
}
Expand All @@ -153,14 +150,3 @@ void ScaleEmbeddedTexture(UndertaleEmbeddedTexture tex)
//ScriptError("Failed to import file: " + ex.Message, "Failed to import file");
}
}

private Bitmap ResizeBitmap(Bitmap sourceBMP, int width, int height)
{
Bitmap result = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(result))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
g.DrawImage(sourceBMP, 0, 0, width, height);
}
return result;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.IO;
using System.Drawing;
using SkiaSharp;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -105,12 +105,7 @@ await Task.Run(() => {
{
try
{
Bitmap bmp;
using (var ms = new MemoryStream(TextureWorker.ReadTextureBlob(file)))
{
bmp = new Bitmap(ms);
}
bmp.SetResolution(96.0F, 96.0F);
SKBitmap bmp = SKBitmap.Decode(file);
var width = (uint)bmp.Width;
var height = (uint)bmp.Height;
var CheckWidth = (uint)(sprite.Textures[frame].Texture.TargetWidth);
Expand Down
120 changes: 74 additions & 46 deletions UndertaleModTool/Scripts/Resource Repackers/ImportFontData.csx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System;
using System.IO;
using System.Drawing;
using SkiaSharp;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -37,7 +37,6 @@ int atlasCount = 0;
foreach (Atlas atlas in packer.Atlasses)
{
string atlasName = String.Format(prefix + "{0:000}" + ".png", atlasCount);
Bitmap atlasBitmap = new Bitmap(atlasName);
UndertaleEmbeddedTexture texture = new UndertaleEmbeddedTexture();
texture.Name = new UndertaleString("Texture " + ++lastTextPage);
texture.TextureData.TextureBlob = File.ReadAllBytes(atlasName);
Expand Down Expand Up @@ -86,8 +85,6 @@ foreach (Atlas atlas in packer.Atlasses)
atlasCount++;
}



HideProgressBar();
ScriptMessage("Import Complete!");

Expand Down Expand Up @@ -176,10 +173,17 @@ public enum BestFitHeuristic
Area,
MaxOneAxis,
}
public struct Rect
{
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}

public class Node
{
public Rectangle Bounds;
public Rect Bounds;
public TextureInfo Texture;
public SplitType SplitType;
}
Expand All @@ -201,6 +205,26 @@ public class Packer
public bool DebugMode;
public BestFitHeuristic FitHeuristic;
public List<Atlas> Atlasses;
public static readonly SKPaint paintGreen = new()
{
Color = SKColors.Green,
BlendMode = SKBlendMode.Src
};
public static readonly SKPaint paintBlack = new()
{
Color = SKColors.Black,
BlendMode = SKBlendMode.Src
};
public static readonly SKPaint paintWhite = new()
{
Color = SKColors.White,
BlendMode = SKBlendMode.Src
};
public static readonly SKPaint paintDarkMagenta = new()
{
Color = SKColors.DarkMagenta,
BlendMode = SKBlendMode.Src
};

public Packer()
{
Expand Down Expand Up @@ -256,13 +280,8 @@ public class Packer
{
string atlasName = String.Format(prefix + "{0:000}" + ".png", atlasCount);
//1: Save images
Image img = CreateAtlasImage(atlas);
//DPI fix start
Bitmap ResolutionFix = new Bitmap(img);
ResolutionFix.SetResolution(96.0F, 96.0F);
Image img2 = ResolutionFix;
//DPI fix end
img2.Save(atlasName, System.Drawing.Imaging.ImageFormat.Png);
using SKBitmap img = CreateAtlasImage(atlas);
TextureWorker.SaveImageToFile(atlasName, img);
//2: save description in file
foreach (Node n in atlas.Nodes)
{
Expand Down Expand Up @@ -293,25 +312,27 @@ public class Packer
FileInfo[] files = di.GetFiles(_Wildcard, SearchOption.AllDirectories);
foreach (FileInfo fi in files)
{
Image img = Image.FromFile(fi.FullName);
if (img != null)
var imgSize = TextureWorker.GetImageSizeFromFile(fi.FullName);
if (imgSize == default)
continue;
int width = imgSize.Width;
int height = imgSize.Height;

if (width <= AtlasSize && height <= AtlasSize)
{
if (img.Width <= AtlasSize && img.Height <= AtlasSize)
{
TextureInfo ti = new TextureInfo();
TextureInfo ti = new TextureInfo();

ti.Source = fi.FullName;
ti.Width = img.Width;
ti.Height = img.Height;
ti.Source = fi.FullName;
ti.Width = width;
ti.Height = height;

SourceTextures.Add(ti);
SourceTextures.Add(ti);

Log.WriteLine("Added " + fi.FullName);
}
else
{
Error.WriteLine(fi.FullName + " is too large to fix in the atlas. Skipping!");
}
Log.WriteLine("Added " + fi.FullName);
}
else
{
Error.WriteLine(fi.FullName + " is too large to fix in the atlas. Skipping!");
}
}
}
Expand Down Expand Up @@ -404,7 +425,8 @@ public class Packer
_Atlas.Nodes = new List<Node>();
textures = _Textures.ToList();
Node root = new Node();
root.Bounds.Size = new Size(_Atlas.Width, _Atlas.Height);
root.Bounds.Width = _Atlas.Width;
root.Bounds.Height = _Atlas.Height;
root.SplitType = SplitType.Horizontal;
freeList.Add(root);
while (freeList.Count > 0 && textures.Count > 0)
Expand Down Expand Up @@ -432,42 +454,48 @@ public class Packer
return textures;
}

private Image CreateAtlasImage(Atlas _Atlas)
private SKBitmap CreateAtlasImage(Atlas _Atlas)
{
Image img = new Bitmap(_Atlas.Width, _Atlas.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(img);
SKBitmap img = new(_Atlas.Width, _Atlas.Height);
using SKCanvas g = new(img);
if (DebugMode)
{
g.FillRectangle(Brushes.Green, new Rectangle(0, 0, _Atlas.Width, _Atlas.Height));
}
g.DrawRect(0, 0, _Atlas.Width, _Atlas.Height, paintGreen);

foreach (Node n in _Atlas.Nodes)
{
SKRect rect = SKRect.Create(n.Bounds.X, n.Bounds.Y, n.Bounds.Width, n.Bounds.Height);

if (n.Texture != null)
{
Image sourceImg = Image.FromFile(n.Texture.Source);
g.DrawImage(sourceImg, n.Bounds);
using SKBitmap sourceImg = SKBitmap.Decode(n.Texture.Source);
g.DrawBitmap(sourceImg, rect);
if (DebugMode)
{
string label = Path.GetFileNameWithoutExtension(n.Texture.Source);
SizeF labelBox = g.MeasureString(label, SystemFonts.MenuFont, new SizeF(n.Bounds.Size));
RectangleF rectBounds = new Rectangle(n.Bounds.Location, new Size((int)labelBox.Width, (int)labelBox.Height));
g.FillRectangle(Brushes.Black, rectBounds);
g.DrawString(label, SystemFonts.MenuFont, Brushes.White, rectBounds);
SKRect labelBox = default;
paintWhite.MeasureText(label, ref labelBox);
SKRect rectBounds = SKRect.Create(n.Bounds.X, n.Bounds.Y, labelBox.Width, labelBox.Height);
float yOff = paintWhite.FontMetrics.Ascent - paintWhite.FontMetrics.Descent - 1; // I am not sure if it's the correct way, but it works
g.DrawRect(rectBounds, paintBlack);
g.DrawText(label, rectBounds.Left, rectBounds.Top - yOff, paintWhite);
}
}
else
{
g.FillRectangle(Brushes.DarkMagenta, n.Bounds);
g.DrawRect(rect, paintDarkMagenta);
if (DebugMode)
{
string label = n.Bounds.Width.ToString() + "x" + n.Bounds.Height.ToString();
SizeF labelBox = g.MeasureString(label, SystemFonts.MenuFont, new SizeF(n.Bounds.Size));
RectangleF rectBounds = new Rectangle(n.Bounds.Location, new Size((int)labelBox.Width, (int)labelBox.Height));
g.FillRectangle(Brushes.Black, rectBounds);
g.DrawString(label, SystemFonts.MenuFont, Brushes.White, rectBounds);
string label = $"{n.Bounds.Width}x{n.Bounds.Height}";
SKRect labelBox = default;
paintWhite.MeasureText(label, ref labelBox);
SKRect rectBounds = SKRect.Create(n.Bounds.X, n.Bounds.Y, labelBox.Width, labelBox.Height);
float yOff = paintWhite.FontMetrics.Ascent - paintWhite.FontMetrics.Descent - 1;
g.DrawRect(rectBounds, paintBlack);
g.DrawText(label, rectBounds.Left, rectBounds.Top - yOff, paintWhite);
}
}
}

return img;
}
}
Loading

0 comments on commit 2ba06eb

Please sign in to comment.