Compare commits

...

2 Commits

Author SHA1 Message Date
Samuele Lorefice
f342a2158b Command line argument parsing added, general logging cleanup. Buildchain improvements 2025-04-07 09:45:34 +02:00
Samuele Lorefice
1de6ee2127 Started pre-release cleanup 2025-04-07 08:24:54 +02:00
18 changed files with 148 additions and 105 deletions

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="LocationsWithSilentDeleteHolder">
<option name="locations">
<list>
<option value="$PROJECT_DIR$/SDFMapCreator/publish/win-x64" />
</list>
</option>
</component>
</project>

21
.run/Run SDF Tool.run.xml Normal file
View File

@@ -0,0 +1,21 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run SDF Tool" type="DotNetProject" factoryName=".NET Project" focusToolWindowBeforeRun="true">
<option name="EXE_PATH" value="$PROJECT_DIR$/SDFMapCreator/bin/Release/net8.0/SDFMapTool" />
<option name="PROGRAM_PARAMETERS" value="-d -D &quot;./images&quot; -o &quot;./sdf.png&quot; -i &quot;01.png;02.png;03.png;04.png;05.png;06.png;07.png;08.png&quot;" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/SDFMapCreator/bin/Release/net8.0" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/SDFMapCreator/SDFMapCreator.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="Build" default="false" projectName="SDFMapCreator" projectPath="$PROJECT_DIR$/SDFMapCreator/SDFMapCreator.csproj" />
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Copy Images To TestDirs" run_configuration_type="ShConfigurationType" />
</method>
</configuration>
</component>

View File

@@ -12,5 +12,6 @@ Global
{915A479D-55CC-4B48-B7C0-75E0B8978698}.Debug|Any CPU.Build.0 = Debug|Any CPU
{915A479D-55CC-4B48-B7C0-75E0B8978698}.Release|Any CPU.ActiveCfg = Release|Any CPU
{915A479D-55CC-4B48-B7C0-75E0B8978698}.Release|Any CPU.Build.0 = Release|Any CPU
{915A479D-55CC-4B48-B7C0-75E0B8978698}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
EndGlobalSection
EndGlobal

View File

@@ -1,15 +1,40 @@
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using CommandLine;
namespace SDFMapCreator;
public class Options {
[Option('d', "debug", Required = false, HelpText = "Enable debug mode.")]
public bool Debug { get; set; } = false;
[Option('D', "imgDirectory", Required = true, HelpText = "Input Images directory.")]
public string ImgDirectory { get; set; } = "images";
[Option('i', "images", Required = true, HelpText = "Images file names separated by ';' and in the correct order.")]
public string Images { get; set; } = "";
[Option('o', "output", Required = true, HelpText = "Output file path.")]
public string OutputFile { get; set; } = "output.png";
[Option('b', "blur", Required = false, HelpText = "How many blur iterations to run.")]
public int BlurIterations { get; set; } = 10;
[Option('r', "radius", Required = false, HelpText = "Blur radius.")]
public float BlurRadius { get; set; } = 100f;
[Option('s', "step", Required = false, HelpText = "Blur step size.")]
public float BlurStep { get; set; } = 0.5f;
[Option("sigma", Required = false, HelpText = "Blur sigma value (weighting).")]
public float BlurSigma { get; set; } = 1f;
}
public static class Program {
#region Magic Values
const float MAX = 1f;
const float MIN = 0f;
static readonly int MAX_THREADS = Environment.ProcessorCount - 2;
static bool debug = true;
static readonly ParallelOptions parallelOptions = new() { MaxDegreeOfParallelism = MAX_THREADS };
static char PSep => Path.DirectorySeparatorChar;
#endregion
static Options options = null!;
static List<Image> Images = new();
static List<MaskData> ImageMasks = new();
static List<TransitionMaskData> TransitionMasks = new();
@@ -22,34 +47,32 @@ public static class Program {
static void ConsoleUpdateLine(string s) => Console.Write("\r" + s);
public static void Main(string[] args) {
Console.WriteLine("Reading images...");
var parser = Parser.Default;
if (debug) {
if (!Directory.Exists("Debug")) Directory.CreateDirectory("Debug");
parser.ParseArguments<Options>(args)
.WithParsed(o => options = o)
.WithNotParsed(o => {
Environment.Exit(1);
});
string debugPath = $"{Environment.CurrentDirectory}{PSep}Debug";
if (options.Debug) {
if (!Directory.Exists(debugPath)) Directory.CreateDirectory(debugPath);
Console.WriteLine("Debug mode enabled.");
}
var imagesPath = "images";
for (var i = 0; i < 8; i++) {
var pixels = ImageUtil.LoadImage<Vector3>($"./{imagesPath}{Path.DirectorySeparatorChar}{i + 1:00}.png");
Console.WriteLine("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}");
var pixels = ImageUtil.LoadImage<Vector3>(imgPath);
Images.Add(new(pixels, pixels.GetLength(0), pixels.GetLength(1)));
}
/*
var pixels = ImageUtil.LoadImage<Vector3>($"./sphereempty.png");
Images.Add(new(pixels, pixels.GetLength(0), pixels.GetLength(1)));
pixels = ImageUtil.LoadImage<Vector3>($"./spherehalf.png");
Images.Add(new(pixels, pixels.GetLength(0), pixels.GetLength(1)));
pixels = ImageUtil.LoadImage<Vector3>($"./spherecut.png");
Images.Add(new(pixels, pixels.GetLength(0), pixels.GetLength(1)));
pixels = ImageUtil.LoadImage<Vector3>($"./spherefull.png");
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("Error: Not all images have the same resolution.");
Console.WriteLine("\nError: Not all images have the same resolution.");
Environment.Exit(1);
}
@@ -57,7 +80,8 @@ public static class Program {
height = (uint)Images[0].Height;
// sum all images together
if(debug) Images[0].Pixels.SaveImage("Debug/Sum0.png");
var sumPath = $"{debugPath}{PSep}Sum";
if(options.Debug) Images[0].Pixels.SaveImage($"{sumPath}0.png");
for (int i = 1; i < Images.Count; i++) {
for (int x = 0; x < Images[i].Width; x++) {
@@ -67,18 +91,19 @@ public static class Program {
Images[i].Pixels[x, y].Z = MathF.Min(Images[i - 1].Pixels[x, y].Z + Images[i].Pixels[x, y].X, MAX);
}
}
if(debug)Images[i].Pixels.SaveImage($"Debug/Sum{i}.png");
if(options.Debug)Images[i].Pixels.SaveImage($"{sumPath}{i}.png");
}
Console.WriteLine("Creating masks...");
Console.WriteLine("\nCreating masks...");
for (var i = 0; i < Images.Count; i++) { //for each image pair, create a mask
ConsoleUpdateLine($"Creating mask {i}...");
var selfMask = SelfMask(Images[i].Pixels);
ImageMasks.Add(new(selfMask, Images[i], new()));
if (debug) selfMask.SaveImage($"Debug/selfMask{i}.png");
if (options.Debug) selfMask.SaveImage($"{debugPath}{PSep}selfMask{i}.png");
if (i >= Images.Count - 1) continue;
ConsoleUpdateLine($"Creating mask {i}...");
var mask = GetABMask(Images[i].Pixels, Images[i + 1].Pixels);
TransitionMasks.Add(new(mask, Images[i], Images[i + 1]));
}
@@ -89,15 +114,16 @@ public static class Program {
ConsoleUpdateLine($"Edge detecting mask {ImageMasks.IndexOf(mask)}...");
EdgeDetect(mask);
}
if (debug)
if (options.Debug)
for (var i = 0; i < TransitionMasks.Count; i++)
ImageMasks[i].Mask.SaveImage($"Debug/mask{i}.png");
ImageMasks[i].Mask.SaveImage($"{debugPath}{PSep}mask{i}.png");
Console.WriteLine("Creating SDFs...");
for (var i = 0; i < ImageMasks.Count; i++) {
var mask = ImageMasks[i];
SDFs.Add(SDF(mask));
if (debug) SDFs[i].SDF.SaveImage($"Debug/sdf{i}.png");
if (options.Debug) SDFs[i].SDF.SaveImage($"{debugPath}{PSep}sdf{i}.png");
}
Console.WriteLine("Creating gradients...");
@@ -105,7 +131,7 @@ public static class Program {
ConsoleUpdateLine($"Generating gradient {i}...");
var gradientData = Gradient(TransitionMasks[i], SDFs[i], SDFs[i + 1]);
Gradients.Add(gradientData);
if (debug) gradientData.SaveImage($"Debug/gradient{i}.png");
if (options.Debug) gradientData.SaveImage($"{debugPath}{PSep}gradient{i}.png");
}
// generate final image
@@ -121,7 +147,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}..., {currStep} -> {currStep + stepIncrement}");
Console.WriteLine($"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;
@@ -133,21 +159,21 @@ public static class Program {
currStep += stepIncrement;
}
finalImage.SaveImage("Debug/Final.png");
finalImage.SaveImage($"{options.OutputFile}.png");
// apply directional blur
var iterations = 1;
var radius = 100f;
var step = .5f;
var sigma = 1f;
var iterations = options.BlurIterations;
var radius = options.BlurRadius;
var step = options.BlurStep;
var sigma = options.BlurSigma;
var totalMask = SelfMask(Images[^1].Pixels);
totalMask.SaveImage("Debug/TotalMask.png");
totalMask.SaveImage($"{debugPath}{PSep}TotalMask.png");
for (var i = 0; i < iterations; i++) {
Console.WriteLine($"Applying directional blur {i + 1}/{iterations}...");
finalImage = DirectionalBlur(finalImage, totalMask, radius, step, sigma);
}
finalImage.SaveImage("finalBlur.png");
finalImage.SaveImage(options.OutputFile);
Console.WriteLine("Done!");
}
@@ -171,29 +197,6 @@ public static class Program {
static Vector3[,] Gradient(TransitionMaskData mask, SDFData sdfA, SDFData sdfB) {
var sw = new Stopwatch();
sw.Start();
/*Vector3[,] temp = new Vector3[width, height];
var min = MAX;
var max = MIN;
Console.WriteLine("Running gradient generation...");
Parallel.For(0, width * height, parallelOptions, (i) =>
{
int x = (int)(i % width);
int y = (int)(i / width);
if (mask.Mask[x, y].X == 0 || mask.Mask[x, y].Y > 0) return;
var a = sdfA.SDF[x, y].X;
var b = sdfB.SDF[x, y].X;
var gradient = a / (a + b);
temp[x, y] = new(Remap(gradient, 0, 1, MIN, MAX));
if (gradient < min) min = gradient;
if (gradient > max) max = gradient;
});*/
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)");
return temp;
@@ -235,8 +238,7 @@ public static class Program {
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static float EuclideanDistance(Vector2 a, Vector2 b) =>
MathF.Sqrt(MathF.Pow(a.X - b.X, 2) + MathF.Pow(a.Y - b.Y, 2));
static float EuclideanDistance(Vector2 a, Vector2 b) => MathF.Sqrt(MathF.Pow(a.X - b.X, 2) + MathF.Pow(a.Y - b.Y, 2));
static Vector3[,] GetABMask(Vector3[,] A, Vector3[,] B) {
kernels.ABMask(A, B, out var temp);

View File

@@ -5,9 +5,15 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>SDFMapTool</AssemblyName>
<Company>Circle2Labs</Company>
<Product>SDFMapTool</Product>
<AssemblyVersion>1.0</AssemblyVersion>
<FileVersion>1.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="ILGPU" Version="1.5.2"/>
<PackageReference Include="ILGPU.Algorithms" Version="1.5.2"/>
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.7"/>
@@ -15,59 +21,59 @@
</ItemGroup>
<ItemGroup>
<None Update="1.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="2.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="TestPattern.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="images\01.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="images\02.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="images\03.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="images\04.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="images\05.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="images\06.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="images\07.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="images\08.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="spherecut.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<None Update="images\spherecut.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="spherefull.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<None Update="images\spherecut32.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="spherehalf.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<None Update="images\sphereempty.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="spherehalf32.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<None Update="images\spherefull.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="spherefull32.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<None Update="images\spherefull32.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="spherecut32.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<None Update="images\spherehalf.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="sphereempty.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<None Update="images\spherehalf32.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="images\TestPattern.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="images\1.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="images\2.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

View File

@@ -133,12 +133,6 @@ public partial class SdfKernels {
gradient.Y += i * image[x, y + i].X;
}
/*
output[x, y] = new Vector3(float.Abs((gradient.X * 0.5f) + 0.5f),
float.Abs((gradient.Y * 0.5f) + 0.5f), 0.5f);
return;
*/
if (gradient == Vector2.Zero) {
output[x, y] = value;
return;

View File

@@ -25,19 +25,19 @@ public partial class SdfKernels {
Console.WriteLine("Reading available accelerators (CUDA only)...");
foreach (var device in gpuContext.GetCudaDevices()) {
using var accelerator = device.CreateAccelerator(gpuContext);
Console.WriteLine($"{GetInfoString(accelerator)}");
Console.WriteLine($"Found accelerator: {accelerator.Name} {accelerator.AcceleratorType}");
}
Console.WriteLine("Reading available accelerators (OpenCL only)...");
foreach (var device in gpuContext.GetCLDevices()) {
using var accelerator = device.CreateAccelerator(gpuContext);
Console.WriteLine($"{GetInfoString(accelerator)}");
Console.WriteLine($"Found accelerator: {accelerator.Name} {accelerator.AcceleratorType}");
}
Console.WriteLine("Reading available accelerators (CPU only)...");
foreach (var device in gpuContext.GetCPUDevices()) {
using var accelerator = device.CreateAccelerator(gpuContext);
Console.WriteLine($"{GetInfoString(accelerator)}");
Console.WriteLine($"Found accelerator: {accelerator.Name} {accelerator.AcceleratorType}");
}
}

View File

@@ -0,0 +1,9 @@
if [ -d ./bin/Debug/net8.0/ ]; then
echo "Copying images to bin/Debug"
cp -r ./images ./bin/Debug/net8.0/
fi
if [ -d ./bin/Release/net8.0/ ]; then
echo "Copying images to bin/Release"
cp -r ./images ./bin/Release/net8.0/
fi

View File

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

Before

Width:  |  Height:  |  Size: 8.4 KiB

After

Width:  |  Height:  |  Size: 8.4 KiB

View File

Before

Width:  |  Height:  |  Size: 680 B

After

Width:  |  Height:  |  Size: 680 B

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 693 B

After

Width:  |  Height:  |  Size: 693 B

View File

Before

Width:  |  Height:  |  Size: 6.7 KiB

After

Width:  |  Height:  |  Size: 6.7 KiB

View File

Before

Width:  |  Height:  |  Size: 540 B

After

Width:  |  Height:  |  Size: 540 B