Added tiny logging library. Revised logging.

This commit is contained in:
Samuele Lorefice
2025-04-09 03:28:18 +02:00
parent 93002270d5
commit eaf1ced1c3
2 changed files with 31 additions and 28 deletions

View File

@@ -2,6 +2,7 @@
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using CommandLine; using CommandLine;
using LineWalker;
namespace SDFMapCreator; namespace SDFMapCreator;
@@ -41,6 +42,8 @@ public static class Program {
static char PSep => Path.DirectorySeparatorChar; static char PSep => Path.DirectorySeparatorChar;
#endregion #endregion
static Logger logger = Logger.GetInstance();
static Options options = null!; static Options options = null!;
static List<Image> Images = new(); static List<Image> Images = new();
@@ -68,21 +71,21 @@ public static class Program {
string debugPath = $"{Environment.CurrentDirectory}{PSep}Debug"; string debugPath = $"{Environment.CurrentDirectory}{PSep}Debug";
if (options.Debug) { if (options.Debug) {
if (!Directory.Exists(debugPath)) Directory.CreateDirectory(debugPath); if (!Directory.Exists(debugPath)) Directory.CreateDirectory(debugPath);
Console.WriteLine("Debug mode enabled."); logger.Log("Debug mode enabled.", LogLevel.Debug);
} }
Console.WriteLine("Reading images..."); logger.Log("Reading images...");
var imageNames = options.Images.Split(';'); var imageNames = options.Images.Split(';');
for (var i = 0; i < imageNames.GetLength(0); i++) { for (var i = 0; i < imageNames.GetLength(0); i++) {
string imgPath = $"{options.ImgDirectory}{PSep}{imageNames[i]}"; string imgPath = $"{options.ImgDirectory}{PSep}{imageNames[i]}";
ConsoleUpdateLine($"Reading image {imgPath}"); logger.Log($"Reading image {imgPath}", updatePrevious:true);
var pixels = ImageUtil.LoadImage<Vector3>(imgPath); var pixels = ImageUtil.LoadImage<Vector3>(imgPath);
Images.Add(new(pixels, pixels.GetLength(0), pixels.GetLength(1))); Images.Add(new(pixels, pixels.GetLength(0), pixels.GetLength(1)));
} }
Console.WriteLine("\n");
//check if all the images in Images are the same resolution //check if all the images in Images are the same resolution
if (Images.Select(img => (img.Width, img.Height)).Distinct().Count() > 1) { if (Images.Select(img => (img.Width, img.Height)).Distinct().Count() > 1) {
Console.WriteLine("\nError: Not all images have the same resolution."); logger.Log("Error: Not all images have the same resolution.", LogLevel.Critical);
Environment.Exit(1); Environment.Exit(1);
} }
@@ -104,9 +107,9 @@ public static class Program {
if(options.Debug)Images[i].Pixels.SaveImage($"{sumPath}{i}.png"); if(options.Debug)Images[i].Pixels.SaveImage($"{sumPath}{i}.png");
} }
Console.WriteLine("\nCreating masks..."); logger.Log("Creating masks...");
for (var i = 0; i < Images.Count; i++) { //for each image pair, create a mask for (var i = 0; i < Images.Count; i++) { //for each image pair, create a mask
ConsoleUpdateLine($"Creating mask {i}..."); logger.Log($"Creating mask {i}...", updatePrevious:true);
var selfMask = SelfMask(Images[i].Pixels); var selfMask = SelfMask(Images[i].Pixels);
ImageMasks.Add(new(selfMask, Images[i], new())); ImageMasks.Add(new(selfMask, Images[i], new()));
@@ -119,9 +122,8 @@ public static class Program {
} }
//EdgeDetect all masks //EdgeDetect all masks
Console.WriteLine("\nEdge detecting masks..."); logger.Log("Edge detecting masks...");
foreach (var mask in ImageMasks) { foreach (var mask in ImageMasks) {
ConsoleUpdateLine($"Edge detecting mask {ImageMasks.IndexOf(mask)}...");
EdgeDetect(mask); EdgeDetect(mask);
} }
@@ -129,16 +131,15 @@ public static class Program {
for (var i = 0; i < TransitionMasks.Count; i++) for (var i = 0; i < TransitionMasks.Count; i++)
ImageMasks[i].Mask.SaveImage($"{debugPath}{PSep}mask{i}.png"); ImageMasks[i].Mask.SaveImage($"{debugPath}{PSep}mask{i}.png");
Console.WriteLine("Creating SDFs..."); logger.Log("Creating SDFs...");
for (var i = 0; i < ImageMasks.Count; i++) { for (var i = 0; i < ImageMasks.Count; i++) {
var mask = ImageMasks[i]; var mask = ImageMasks[i];
SDFs.Add(SDF(mask)); SDFs.Add(SDF(mask));
if (options.Debug) SDFs[i].SDF.SaveImage($"{debugPath}{PSep}sdf{i}.png"); if (options.Debug) SDFs[i].SDF.SaveImage($"{debugPath}{PSep}sdf{i}.png");
} }
Console.WriteLine("Creating gradients..."); logger.Log("Creating gradients...");
for (var i = 0; i < TransitionMasks.Count; i++) { for (var i = 0; i < TransitionMasks.Count; i++) {
ConsoleUpdateLine($"Generating gradient {i}...");
var gradientData = Gradient(TransitionMasks[i], SDFs[i], SDFs[i + 1]); var gradientData = Gradient(TransitionMasks[i], SDFs[i], SDFs[i + 1]);
Gradients.Add(gradientData); Gradients.Add(gradientData);
if (options.Debug) gradientData.SaveImage($"{debugPath}{PSep}gradient{i}.png"); if (options.Debug) gradientData.SaveImage($"{debugPath}{PSep}gradient{i}.png");
@@ -157,7 +158,7 @@ public static class Program {
for (var i = 0; i < Gradients.Count; i++) { for (var i = 0; i < Gradients.Count; i++) {
var mask = ImageMasks[i + 1]; var mask = ImageMasks[i + 1];
var gradient = Gradients[i]; var gradient = Gradients[i];
Console.WriteLine($"Applying gradient {i}..., Step: {currStep:F2} -> Next: {(currStep + stepIncrement):F2}"); logger.Log($"Applying gradient {i}..., Step: {currStep:F2} -> Next: {(currStep + stepIncrement):F2}");
for (var x = 0; x < mask.Mask.GetLength(0); x++) { for (var x = 0; x < mask.Mask.GetLength(0); x++) {
for (var y = 0; y < mask.Mask.GetLength(1); y++) { for (var y = 0; y < mask.Mask.GetLength(1); y++) {
if (mask.Mask[x,y].X == 0) continue; if (mask.Mask[x,y].X == 0) continue;
@@ -181,13 +182,13 @@ public static class Program {
finalImage = DirectionalBlur(finalImage, totalMask, iterations, radius, step, sigma); finalImage = DirectionalBlur(finalImage, totalMask, iterations, radius, step, sigma);
finalImage.SaveImage(options.OutputFile); finalImage.SaveImage(options.OutputFile);
Console.WriteLine("Done!"); logger.Log("Done!");
Logger.Shutdown();
} }
static void EdgeDetect(MaskData maskData) { static void EdgeDetect(MaskData maskData) {
var sw = new Stopwatch(); var sw = new Stopwatch();
sw.Start(); sw.Start();
Console.WriteLine("Running edge detection...");
kernels.EdgeDetect(maskData.Mask, out var temp); kernels.EdgeDetect(maskData.Mask, out var temp);
maskData.Mask = temp; maskData.Mask = temp;
Parallel.For(0, width * height, parallelOptions, (i) => { Parallel.For(0, width * height, parallelOptions, (i) => {
@@ -197,15 +198,17 @@ public static class Program {
}); });
sw.Stop(); sw.Stop();
Console.WriteLine( string logString = $"Edge detecting mask {ImageMasks.IndexOf(maskData)}..." +
$"\nEdge pixels: {maskData.Edges.Count} | {width * height / (sw.ElapsedMilliseconds + 1)} pixels/s\n Time: {sw.Elapsed.TotalSeconds:F4}s"); Environment.NewLine + $"Edge pixels: {maskData.Edges.Count} | {width * height / (sw.ElapsedMilliseconds + 1)} pixels/s"+
Environment.NewLine + $"Time: {sw.Elapsed.TotalSeconds:F4}s";
logger.Log(logString, updatePrevious:true);
} }
static Vector3[,] Gradient(TransitionMaskData mask, SDFData sdfA, SDFData sdfB) { static Vector3[,] Gradient(TransitionMaskData mask, SDFData sdfA, SDFData sdfB) {
var sw = new Stopwatch(); var sw = new Stopwatch();
sw.Start(); sw.Start();
kernels.Gradient(mask.Mask, sdfA.SDF, sdfB.SDF, out var temp); kernels.Gradient(mask.Mask, sdfA.SDF, sdfB.SDF, out var temp);
Console.WriteLine($"Gradient Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height / (float)sw.Elapsed.TotalSeconds:N0} pixels/s)"); logger.Log($"Gradient Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height / (float)sw.Elapsed.TotalSeconds:N0} pixels/s)");
return temp; return temp;
} }
@@ -213,13 +216,13 @@ public static class Program {
var sw = new Stopwatch(); var sw = new Stopwatch();
sw.Start(); sw.Start();
kernels.Sdf(mask.Edges.ToArray(), (int)width, (int)height, out var temp); kernels.Sdf(mask.Edges.ToArray(), (int)width, (int)height, out var temp);
Console.WriteLine($"\nSDF Generation Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height / sw.Elapsed.TotalSeconds:N0} pixels/s)"); string logString = $"SDF Generation Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height / sw.Elapsed.TotalSeconds:N0} pixels/s)";
sw.Restart(); sw.Restart();
var absMax = 0f; var absMax = 0f;
foreach (var pixel in temp) { Parallel.ForEach(temp.Cast<Vector3>(), pixel => {
if (pixel.X > absMax) absMax = pixel.X; var localMax = pixel.X;
} Interlocked.Exchange(ref absMax, MathF.Max(absMax, localMax));
});
Parallel.For(0, width * height, parallelOptions, (i) => { Parallel.For(0, width * height, parallelOptions, (i) => {
//convert 1D index to 2D index //convert 1D index to 2D index
@@ -228,9 +231,9 @@ public static class Program {
temp[x, y] = new(Remap(temp[x, y].X, 0, absMax, MIN, MAX)); temp[x, y] = new(Remap(temp[x, y].X, 0, absMax, MIN, MAX));
}); });
sw.Stop(); sw.Stop();
logString += Environment.NewLine +
Console.WriteLine( $"SDF Normalization Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height / sw.Elapsed.TotalSeconds:N0} pixels/s)";
$"SDF Normalization Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height / sw.Elapsed.TotalSeconds:N0} pixels/s)"); logger.Log(logString, updatePrevious:true);
return new(temp); return new(temp);
} }
@@ -239,8 +242,7 @@ public static class Program {
var sw = new Stopwatch(); var sw = new Stopwatch();
sw.Start(); sw.Start();
kernels.DirectionalBlur(image, mask, out var temp, iterations, radius, step, sigma); kernels.DirectionalBlur(image, mask, out var temp, iterations, radius, step, sigma);
Console.WriteLine( logger.Log($"Directional Blur Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height * iterations / sw.Elapsed.TotalSeconds:N0} pixels/s)");
$"Directional Blur Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height * iterations / sw.Elapsed.TotalSeconds:N0} pixels/s)");
return temp; return temp;
} }

View File

@@ -16,6 +16,7 @@
<PackageReference Include="CommandLineParser" Version="2.9.1" /> <PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="ILGPU" Version="1.5.2"/> <PackageReference Include="ILGPU" Version="1.5.2"/>
<PackageReference Include="ILGPU.Algorithms" Version="1.5.2"/> <PackageReference Include="ILGPU.Algorithms" Version="1.5.2"/>
<PackageReference Include="LineWalker" Version="0.1.1-alpha" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.7"/> <PackageReference Include="SixLabors.ImageSharp" Version="3.1.7"/>
<PackageReference Include="System.Numerics.Vectors" Version="4.6.1"/> <PackageReference Include="System.Numerics.Vectors" Version="4.6.1"/>
</ItemGroup> </ItemGroup>