Skip to content

Commit

Permalink
Finalize Change the size if necessary to the next possible size.
Browse files Browse the repository at this point in the history
  • Loading branch information
Venomalia committed Oct 3, 2023
1 parent c0217b3 commit f578501
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
43 changes: 43 additions & 0 deletions TextureExtraction tool/Scan/Finalize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System.Reflection.Emit;
using System.Security.Principal;

namespace DolphinTextureExtraction.Scan
{
Expand Down Expand Up @@ -56,6 +59,13 @@ protected override void Scan(ScanObjekt so)
int height = image.Height;
PngEncoder encoder = new() { ColorType = PngColorType.GrayscaleWithAlpha };

if (FixImageResolutionIfNeeded(image, hashRG))
{
Log.WriteNotification(NotificationType.Info, $"\"{name}\" Resize {new Size(width, height)} => {image.Size}.");
width = image.Width;
height = image.Height;
}

// Create two new images.
using (Image<La16> imageRG = new(width, height))
using (Image<La16> imageBA = new(width, height))
Expand Down Expand Up @@ -138,6 +148,12 @@ protected override void Scan(ScanObjekt so)
}

image ??= Image.Load(so.Stream);

if (FixImageResolutionIfNeeded(image, dolphinHash))
{
Log.WriteNotification(NotificationType.Info, $"\"{name}\" Resize {info.Size} => {image.Size}.");
}

string imagePath = Path.Combine(savePath, dolphinHash.Build() + ".png");
image.SaveAsPng(imagePath, encoder);
return;
Expand All @@ -155,6 +171,33 @@ protected override void Scan(ScanObjekt so)
Save(so);
}

public static bool FixImageResolutionIfNeeded(Image image, DolphinTextureHashInfo hash)
{
Size hSize = hash.GetImageSize();

int dividend = image.Width % hSize.Width;
if (dividend != 0 || image.Height % hSize.Height != 0)
{
int width = image.Width;
if (dividend > hSize.Width / 2)
{
width += hSize.Width - dividend;
}
else
{
width -= dividend;
}
width = Math.Max(width, hSize.Width);
int height = hSize.Height * (width / hSize.Width);

ResizeOptions options = new() { Size = new(width, height), Mode = ResizeMode.Stretch, Sampler = KnownResamplers.Lanczos3 };
image.Mutate(x => x.Resize(options));

return true;
}
return false;
}

private static void FixIntensityTexturs(Image<La16> image)
{
var mem = image.GetPixelMemoryGroup();
Expand Down
6 changes: 6 additions & 0 deletions lib/AuroraLip/Texture/DolphinTextureHashInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public DolphinTextureHashInfo(int imageWidth, int imageHeight, ulong hash, GXIma

public override string ToString() => Build();

/// <summary>
/// Determines the image size with taking the mipmap level into account.
/// </summary>
/// <returns>The size of the image.</returns>
public Size GetImageSize() => new(Math.Max(Width >> Mipmap, 1), Math.Max(Height >> Mipmap, 1));

/// <summary>
/// Builds a Dolphin texture hash.
/// </summary>
Expand Down

0 comments on commit f578501

Please sign in to comment.