diff --git a/SDFMapCreator/Program.cs b/SDFMapCreator/Program.cs index 3509341..0c7e121 100644 --- a/SDFMapCreator/Program.cs +++ b/SDFMapCreator/Program.cs @@ -2,6 +2,7 @@ using System.Numerics; using System.Runtime.CompilerServices; using CommandLine; +using LineWalker; namespace SDFMapCreator; @@ -41,6 +42,8 @@ public static class Program { static char PSep => Path.DirectorySeparatorChar; #endregion + static Logger logger = Logger.GetInstance(); + static Options options = null!; static List Images = new(); @@ -68,21 +71,21 @@ public static class Program { string debugPath = $"{Environment.CurrentDirectory}{PSep}Debug"; if (options.Debug) { 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(';'); for (var i = 0; i < imageNames.GetLength(0); i++) { string imgPath = $"{options.ImgDirectory}{PSep}{imageNames[i]}"; - ConsoleUpdateLine($"Reading image {imgPath}"); + logger.Log($"Reading image {imgPath}", updatePrevious:true); var pixels = ImageUtil.LoadImage(imgPath); Images.Add(new(pixels, pixels.GetLength(0), pixels.GetLength(1))); } - Console.WriteLine("\n"); + //check if all the images in Images are the same resolution 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); } @@ -104,9 +107,9 @@ public static class Program { 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 - ConsoleUpdateLine($"Creating mask {i}..."); + logger.Log($"Creating mask {i}...", updatePrevious:true); var selfMask = SelfMask(Images[i].Pixels); ImageMasks.Add(new(selfMask, Images[i], new())); @@ -119,9 +122,8 @@ public static class Program { } //EdgeDetect all masks - Console.WriteLine("\nEdge detecting masks..."); + logger.Log("Edge detecting masks..."); foreach (var mask in ImageMasks) { - ConsoleUpdateLine($"Edge detecting mask {ImageMasks.IndexOf(mask)}..."); EdgeDetect(mask); } @@ -129,16 +131,15 @@ public static class Program { for (var i = 0; i < TransitionMasks.Count; i++) 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++) { var mask = ImageMasks[i]; SDFs.Add(SDF(mask)); 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++) { - ConsoleUpdateLine($"Generating gradient {i}..."); var gradientData = Gradient(TransitionMasks[i], SDFs[i], SDFs[i + 1]); Gradients.Add(gradientData); 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++) { var mask = ImageMasks[i + 1]; 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 y = 0; y < mask.Mask.GetLength(1); y++) { 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.SaveImage(options.OutputFile); - Console.WriteLine("Done!"); + logger.Log("Done!"); + Logger.Shutdown(); } static void EdgeDetect(MaskData maskData) { var sw = new Stopwatch(); sw.Start(); - Console.WriteLine("Running edge detection..."); kernels.EdgeDetect(maskData.Mask, out var temp); maskData.Mask = temp; Parallel.For(0, width * height, parallelOptions, (i) => { @@ -197,15 +198,17 @@ public static class Program { }); sw.Stop(); - Console.WriteLine( - $"\nEdge pixels: {maskData.Edges.Count} | {width * height / (sw.ElapsedMilliseconds + 1)} pixels/s\n Time: {sw.Elapsed.TotalSeconds:F4}s"); + string logString = $"Edge detecting mask {ImageMasks.IndexOf(maskData)}..." + + 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) { var sw = new Stopwatch(); sw.Start(); 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; } @@ -213,13 +216,13 @@ public static class Program { var sw = new Stopwatch(); sw.Start(); 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(); var absMax = 0f; - foreach (var pixel in temp) { - if (pixel.X > absMax) absMax = pixel.X; - } + Parallel.ForEach(temp.Cast(), pixel => { + var localMax = pixel.X; + Interlocked.Exchange(ref absMax, MathF.Max(absMax, localMax)); + }); Parallel.For(0, width * height, parallelOptions, (i) => { //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)); }); sw.Stop(); - - Console.WriteLine( - $"SDF Normalization Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height / sw.Elapsed.TotalSeconds:N0} pixels/s)"); + logString += Environment.NewLine + + $"SDF Normalization Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height / sw.Elapsed.TotalSeconds:N0} pixels/s)"; + logger.Log(logString, updatePrevious:true); return new(temp); } @@ -239,8 +242,7 @@ public static class Program { var sw = new Stopwatch(); sw.Start(); kernels.DirectionalBlur(image, mask, out var temp, iterations, radius, step, sigma); - Console.WriteLine( - $"Directional Blur Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height * iterations / sw.Elapsed.TotalSeconds:N0} pixels/s)"); + logger.Log($"Directional Blur Time: {sw.Elapsed.TotalSeconds:N4}s ({width * height * iterations / sw.Elapsed.TotalSeconds:N0} pixels/s)"); return temp; } diff --git a/SDFMapCreator/SDFMapCreator.csproj b/SDFMapCreator/SDFMapCreator.csproj index 17f6004..3b06ab0 100644 --- a/SDFMapCreator/SDFMapCreator.csproj +++ b/SDFMapCreator/SDFMapCreator.csproj @@ -16,6 +16,7 @@ +