347 lines
14 KiB
C#
347 lines
14 KiB
C#
using System.CommandLine;
|
|
using System.CommandLine.Parsing;
|
|
|
|
namespace Atomorph.NET.Demo;
|
|
|
|
|
|
public class Options
|
|
{
|
|
public bool Verbose { get; set; }
|
|
public bool BlendBlobs { get; set; } = false;
|
|
public uint Feather { get; set; } = 1;
|
|
public byte BlobRgbaWeight { get; set; } = 1;
|
|
public byte BlobSizeWeight { get; set; } = 1;
|
|
public byte BlobXyWeight { get; set; } = 1;
|
|
public ulong BlobNumber { get; set; } = 1;
|
|
public bool BlobsAsTexture { get; set; } = true;
|
|
public bool BlobsAsAverage { get; set; } = false;
|
|
public bool BlobsAsDistinct { get; set; } = false;
|
|
public bool BlobsByRgb { get; set; } = false;
|
|
public bool BlobsByHsp { get; set; } = true;
|
|
public ulong BlobMaxSize { get; set; } = ulong.MaxValue;
|
|
public ulong BlobMinSize { get; set; } = 1;
|
|
public ushort BlobBoxGrip { get; set; } = ushort.MaxValue;
|
|
public ulong BlobBoxSamples { get; set; } = 100;
|
|
public double BlobThreshold { get; set; } = 1.0;
|
|
public bool Brief { get; set; } = true;
|
|
public uint CycleLength { get; set; } = 100000;
|
|
public ulong Degenerate { get; set; } = 10000;
|
|
public ushort Density { get; set; } = 2;
|
|
public bool FadingNone { get; set; } = false;
|
|
public bool FadingLinear { get; set; } = false;
|
|
public bool FadingCosine { get; set; } = false;
|
|
public bool FadingPerlin { get; set; } = true;
|
|
public string File { get; set; } = "untitled_morph";
|
|
public bool Finite { get; set; } = false;
|
|
public uint Fluid { get; set; } = 0;
|
|
public uint FramesOut { get; set; } = 0;
|
|
public bool Help { get; set; } = false;
|
|
public string Indir { get; set; } = ".";
|
|
public bool KeepBackground { get; set; } = false;
|
|
public uint MatchTime { get; set; } = 0;
|
|
public uint MorphTime { get; set; } = 0;
|
|
public bool MotionNone { get; set; } = false;
|
|
public bool MotionLinear { get; set; } = false;
|
|
public bool MotionSpline { get; set; } = true;
|
|
public string Outdir { get; set; } = ".";
|
|
public uint Seed { get; set; } = 0;
|
|
public uint Threads { get; set; } = 8;
|
|
public bool Version { get; set; } = false;
|
|
|
|
public ShowBlobs ShowBlobs { get; set; } = ShowBlobs.Texture;
|
|
public ColorMethod DifferBlobs { get; set; } = ColorMethod.HSP;
|
|
public InterpolationMethod Motion { get; set; } = InterpolationMethod.Spline;
|
|
public InterpolationMethod Fading { get; set; } = InterpolationMethod.Perlin;
|
|
public int ExitFlag { get; set; } = 0;
|
|
public string Name { get; set; } = "";
|
|
public int Height { get; set; } = 0;
|
|
public int Width { get; set; } = 0;
|
|
|
|
public bool FramesOutDefined { get; set; } = false;
|
|
|
|
public List<string> Files { get; set; } = [];
|
|
|
|
public static Options Parse(string[] args)
|
|
{
|
|
var options = new Options();
|
|
|
|
var filesArgument = new Argument<string[]>("files")
|
|
{
|
|
Arity = ArgumentArity.ZeroOrMore,
|
|
Description = "Input files to morph."
|
|
};
|
|
|
|
var verboseOption = new Option<bool>("--verbose")
|
|
{
|
|
Description = "Print verbose messages.",
|
|
};
|
|
var blendBlobsOption = new Option<bool>("--blend-blobs")
|
|
{
|
|
Description = "Blob intersections are blended together.",
|
|
};
|
|
var blobFeatherOption = new Option<uint>("--blob-feather", "-E")
|
|
{
|
|
Description = "Number of gradually transparent outer layers.",
|
|
};
|
|
var blobRgbaWeightOption = new Option<byte>("--blob-rgba-weight", "-c")
|
|
{
|
|
Description = "Importance of a blob's color when matching [0-255].",
|
|
};
|
|
var blobSizeWeightOption = new Option<byte>("--blob-size-weight", "-z")
|
|
{
|
|
Description = "Importance of a blob's size when matching [0-255].",
|
|
};
|
|
var blobXyWeightOption = new Option<byte>("--blob-xy-weight", "-p")
|
|
{
|
|
Description = "Importance of a blob's location when matching [0-255].",
|
|
};
|
|
var blobsOption = new Option<ulong>("--blobs", "-b")
|
|
{
|
|
Description = "Preferred number of blobs to keep.",
|
|
};
|
|
var blobsAsTextureOption = new Option<bool>("--blobs-as-texture")
|
|
{
|
|
Description = "A blob is textured by its pixels (default).",
|
|
Arity = ArgumentArity.Zero,
|
|
};
|
|
var blobsAsAverageOption = new Option<bool>("--blobs-as-average")
|
|
{
|
|
Description = "A blob has the average color of its pixels.",
|
|
};
|
|
var blobsAsDistinctOption = new Option<bool>("--blobs-as-distinct")
|
|
{
|
|
Description = "A blob has a random color.",
|
|
};
|
|
var blobsByRgbOption = new Option<bool>("--blobs-by-rgb")
|
|
{
|
|
Description = "Differentiate blobs by RGB distance.",
|
|
};
|
|
var blobsByHspOption = new Option<bool>("--blobs-by-hsp")
|
|
{
|
|
Description = "Differentiate blobs by HSP distance (default).",
|
|
};
|
|
var blobsMaxSizeOption = new Option<ulong>("--blobs-max-size", "-B")
|
|
{
|
|
Description = "Maximum size of a single blob in pixels.",
|
|
CustomParser = HandleNegativeValues,
|
|
};
|
|
var blobsMinSizeOption = new Option<ulong>("--blobs-min-size", "-m")
|
|
{
|
|
Description = "Minimum size of a single blob in pixels.",
|
|
CustomParser = HandleNegativeValues,
|
|
};
|
|
var blobsBoxGripOption = new Option<ushort>("--blobs-box-grip", "-g")
|
|
{
|
|
Description = "Unifying grip bounds for undersized blobs.",
|
|
};
|
|
var blobsBoxSamplesOption = new Option<ulong>("--blobs-box-samples", "-S")
|
|
{
|
|
Description = "Number of samples to take for each dust box.",
|
|
};
|
|
var blobsThresholdOption = new Option<double>("--blobs-threshold", "-t")
|
|
{
|
|
Description = "Maximum color difference for merging blobs [0-255].",
|
|
};
|
|
var briefOption = new Option<bool>("--brief")
|
|
{
|
|
Description = "Print brief messages (default).",
|
|
};
|
|
var cycleLengthOption = new Option<uint>("--cycle-length", "-C")
|
|
{
|
|
Description = "Number of iterations per morph cycle.",
|
|
};
|
|
var degenerateOption = new Option<ulong>("--degenerate", "-d")
|
|
{
|
|
Description = "Degeneration period for energy minimzation.",
|
|
};
|
|
var densityOption = new Option<ushort>("--density", "-D")
|
|
{
|
|
Description = "Number of atoms per pixel at minimum.",
|
|
};
|
|
var fadingNoneOption = new Option<bool>("--fading-none")
|
|
{
|
|
Description = "Colours are not interpolated.",
|
|
};
|
|
var fadingLinearOption = new Option<bool>("--fading-linear")
|
|
{
|
|
Description = "Colours are interpolated linearly.",
|
|
};
|
|
var fadingCosineOption = new Option<bool>("--fading-cosine")
|
|
{
|
|
Description = "Colours are cosine interpolated.",
|
|
};
|
|
var fadingPerlinOption = new Option<bool>("--fading-perlin")
|
|
{
|
|
Description = "Use Perlin noise colour transition (default).",
|
|
};
|
|
var fileOption = new Option<string>("--file", "-f")
|
|
{
|
|
Description = "Output files with prefix.",
|
|
Arity = ArgumentArity.ExactlyOne,
|
|
};
|
|
var finiteOption = new Option<bool>("--finite")
|
|
{
|
|
Description = "Morph will not repeat itself seamlessly.",
|
|
};
|
|
var fluidOption = new Option<uint>("--fluid", "-L")
|
|
{
|
|
Description = "Number of fluid simulation steps per frame.",
|
|
};
|
|
var framesOption = new Option<uint>("--frames", "-F")
|
|
{
|
|
Description = "Number of frames to generate.",
|
|
};
|
|
var helpOption = new Option<bool>("--help", "-h")
|
|
{
|
|
Description = "Display this usage information.",
|
|
};
|
|
var indirOption = new Option<string>("--indir", "-i")
|
|
{
|
|
Description = "Read input from this directory.",
|
|
Arity = ArgumentArity.ExactlyOne,
|
|
};
|
|
var keepBackgroundOption = new Option<bool>("--keep-background")
|
|
{
|
|
Description = "Morph background is cross-dissolved.",
|
|
};
|
|
var matchTimeOption = new Option<uint>("--match-time", "-M")
|
|
{
|
|
Description = "Time in seconds given for blob matching.",
|
|
};
|
|
var morphTimeOption = new Option<uint>("--morph-time", "-O")
|
|
{
|
|
Description = "Time in seconds given for atom morphing.",
|
|
};
|
|
var motionNoneOption = new Option<bool>("--motion-none")
|
|
{
|
|
Description = "Positions are not interpolated.",
|
|
};
|
|
var motionLinearOption = new Option<bool>("--motion-linear")
|
|
{
|
|
Description = "Positions are linearly interpolated.",
|
|
};
|
|
var motionSplineOption = new Option<bool>("--motion-spline")
|
|
{
|
|
Description = "Uses Catmull-Rom splines (default).",
|
|
};
|
|
var outdirOption = new Option<string>("--outdir", "-o")
|
|
{
|
|
Description = "Write output to this directory.",
|
|
};
|
|
var seedOption = new Option<uint>("--seed", "-s")
|
|
{
|
|
Description = "Seed for the random number generator.",
|
|
};
|
|
var threadsOption = new Option<uint>("--threads", "-T")
|
|
{
|
|
Description = "Number of worker threads to spawn.",
|
|
};
|
|
var versionOption = new Option<bool>("--version", "-v")
|
|
{
|
|
Description = "Show version information.",
|
|
};
|
|
|
|
var rootCommand = new RootCommand("Atomorph.NET morphing application")
|
|
{
|
|
filesArgument,
|
|
verboseOption,
|
|
blendBlobsOption,
|
|
blobFeatherOption,
|
|
blobRgbaWeightOption,
|
|
blobSizeWeightOption,
|
|
blobXyWeightOption,
|
|
blobsOption,
|
|
blobsAsTextureOption,
|
|
blobsAsAverageOption,
|
|
blobsAsDistinctOption,
|
|
blobsByRgbOption,
|
|
blobsByHspOption,
|
|
blobsMaxSizeOption,
|
|
blobsMinSizeOption,
|
|
blobsBoxGripOption,
|
|
blobsBoxSamplesOption,
|
|
blobsThresholdOption,
|
|
briefOption,
|
|
cycleLengthOption,
|
|
degenerateOption,
|
|
densityOption,
|
|
fadingNoneOption,
|
|
fadingLinearOption,
|
|
fadingCosineOption,
|
|
fadingPerlinOption,
|
|
fileOption,
|
|
finiteOption,
|
|
fluidOption,
|
|
framesOption,
|
|
helpOption,
|
|
indirOption,
|
|
keepBackgroundOption,
|
|
matchTimeOption,
|
|
morphTimeOption,
|
|
motionNoneOption,
|
|
motionLinearOption,
|
|
motionSplineOption,
|
|
outdirOption,
|
|
seedOption,
|
|
threadsOption,
|
|
versionOption
|
|
};
|
|
|
|
var parseResult = rootCommand.Parse(args);
|
|
options.Files = parseResult.GetValue(filesArgument)?.ToList() ?? [];
|
|
options.Verbose = parseResult.GetValue(verboseOption);
|
|
options.BlendBlobs = parseResult.GetValue(blendBlobsOption);
|
|
options.Feather = parseResult.GetValue(blobFeatherOption);
|
|
options.BlobRgbaWeight = parseResult.GetValue(blobRgbaWeightOption);
|
|
options.BlobSizeWeight = parseResult.GetValue(blobSizeWeightOption);
|
|
options.BlobXyWeight = parseResult.GetValue(blobXyWeightOption);
|
|
options.BlobNumber = parseResult.GetValue(blobsOption);
|
|
options.BlobsAsTexture = parseResult.GetValue(blobsAsTextureOption);
|
|
options.BlobsAsAverage = parseResult.GetValue(blobsAsAverageOption);
|
|
options.BlobsAsDistinct = parseResult.GetValue(blobsAsDistinctOption);
|
|
options.BlobsByRgb = parseResult.GetValue(blobsByRgbOption);
|
|
options.BlobsByHsp = parseResult.GetValue(blobsByHspOption);
|
|
options.BlobMaxSize = parseResult.GetValue(blobsMaxSizeOption);
|
|
options.BlobMinSize = parseResult.GetValue(blobsMinSizeOption);
|
|
options.BlobBoxGrip = parseResult.GetValue(blobsBoxGripOption);
|
|
options.BlobBoxSamples = parseResult.GetValue(blobsBoxSamplesOption);
|
|
options.BlobThreshold = parseResult.GetValue(blobsThresholdOption);
|
|
options.Brief = parseResult.GetValue(briefOption);
|
|
options.CycleLength = parseResult.GetValue(cycleLengthOption);
|
|
options.Degenerate = parseResult.GetValue(degenerateOption);
|
|
options.Density = parseResult.GetValue(densityOption);
|
|
options.FadingNone = parseResult.GetValue(fadingNoneOption);
|
|
options.FadingLinear = parseResult.GetValue(fadingLinearOption);
|
|
options.FadingCosine = parseResult.GetValue(fadingCosineOption);
|
|
options.FadingPerlin = parseResult.GetValue(fadingPerlinOption);
|
|
options.File = parseResult.GetValue(fileOption) ?? "untitled_morph";
|
|
options.Finite = parseResult.GetValue(finiteOption);
|
|
options.Fluid = parseResult.GetValue(fluidOption);
|
|
options.FramesOut = parseResult.GetValue(framesOption);
|
|
options.FramesOutDefined = options.FramesOut > 0;
|
|
options.Help = parseResult.GetValue(helpOption);
|
|
options.Indir = parseResult.GetValue(indirOption) ?? ".";
|
|
options.KeepBackground = parseResult.GetValue(keepBackgroundOption);
|
|
options.MatchTime = parseResult.GetValue(matchTimeOption);
|
|
options.MorphTime = parseResult.GetValue(morphTimeOption);
|
|
options.MotionNone = parseResult.GetValue(motionNoneOption);
|
|
options.MotionLinear = parseResult.GetValue(motionLinearOption);
|
|
options.MotionSpline = parseResult.GetValue(motionSplineOption);
|
|
options.Outdir = parseResult.GetValue(outdirOption) ?? ".";
|
|
options.Seed = parseResult.GetValue(seedOption);
|
|
options.Threads = parseResult.GetValue(threadsOption);
|
|
options.Version = parseResult.GetValue(versionOption);
|
|
|
|
return options;
|
|
}
|
|
|
|
private static ulong HandleNegativeValues(ArgumentResult result)
|
|
{
|
|
// Handle negative values as max size
|
|
if (result.Tokens.Count > 0 && long.TryParse(result.Tokens[0].Value, out var value) && value < 0)
|
|
{
|
|
return (ulong.MaxValue - (ulong)(-value));
|
|
}
|
|
return ulong.TryParse(result.Tokens[0].Value, out var size) ? size : 1;
|
|
}
|
|
} |