Initial port
No files matched your search
@@ -0,0 +1,8 @@
|
|||||||
|
bin/
|
||||||
|
obj/
|
||||||
|
/packages/
|
||||||
|
riderModule.iml
|
||||||
|
/_ReSharper.Caches/
|
||||||
|
.idea
|
||||||
|
*.DotSettings.user
|
||||||
|
artifacts
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="BigGustave" />
|
||||||
|
<PackageReference Include="System.CommandLine" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Atomorph.NET\Atomorph.NET.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Tests\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,347 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,274 @@
|
|||||||
|
using System.Text;
|
||||||
|
using Atomorph;
|
||||||
|
using Atomorph.Models;
|
||||||
|
using Atomorph.NET.Demo;
|
||||||
|
using BigGustave;
|
||||||
|
using Pixel = Atomorph.Models.Pixel;
|
||||||
|
|
||||||
|
public static class Program
|
||||||
|
{
|
||||||
|
public static int Main(string[] args)
|
||||||
|
{
|
||||||
|
var options = Options.Parse(args);
|
||||||
|
if (options.ExitFlag != 0)
|
||||||
|
return 0;
|
||||||
|
var programStart = DateTime.UtcNow;
|
||||||
|
|
||||||
|
var morph = new Morph();
|
||||||
|
morph.SetBlobDelimiter(ColorMethod.HSP);
|
||||||
|
morph.SetBlobMaxSize(ulong.MaxValue);
|
||||||
|
morph.SetBlobMinSize(1);
|
||||||
|
morph.SetBlobBoxGrip(ushort.MaxValue);
|
||||||
|
morph.SetBlobBoxSamples(100);
|
||||||
|
morph.SetBlobThreshold(1.0);
|
||||||
|
morph.SetBlobNumber(1);
|
||||||
|
morph.SetSeed(0);
|
||||||
|
morph.SetBlobRgbaWeight(1);
|
||||||
|
morph.SetBlobSizeWeight(0);
|
||||||
|
morph.SetBlobXyWeight(0);
|
||||||
|
morph.SetDegeneration(10000);
|
||||||
|
morph.SetDensity(2); // Higher than 1 sets fluid to 0.
|
||||||
|
morph.SetMotion(InterpolationMethod.Spline);
|
||||||
|
morph.SetFading(InterpolationMethod.Perlin);
|
||||||
|
morph.SetThreads(8);
|
||||||
|
morph.SetCycleLength(100000);
|
||||||
|
morph.SetFeather(0);
|
||||||
|
morph.SetKeepBackground(false);
|
||||||
|
morph.SetFinite(true);
|
||||||
|
morph.SetShowBlobs(ShowBlobs.Texture);
|
||||||
|
morph.SetFluid(0); // Higher than 0 sets density to 1.
|
||||||
|
|
||||||
|
if (!LoadFiles(morph, options))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
MainLoop(morph, options);
|
||||||
|
SaveFiles(morph, options);
|
||||||
|
var programEnd = DateTime.UtcNow;
|
||||||
|
|
||||||
|
if (options.Verbose)
|
||||||
|
{
|
||||||
|
var duration = programEnd - programStart;
|
||||||
|
Console.WriteLine($"Program completed in {duration.TotalSeconds:0.00} seconds.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
// Add your main logic here
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool LoadFiles(Morph morph, Options options)
|
||||||
|
{
|
||||||
|
StringBuilder buf;
|
||||||
|
int width, height, error;
|
||||||
|
int max_width = 0, max_height = 0;
|
||||||
|
|
||||||
|
// Load input image files:
|
||||||
|
for (var i = 0; i < options.Files.Count; ++i)
|
||||||
|
{
|
||||||
|
buf = new(options.Indir);
|
||||||
|
buf.Append('/');
|
||||||
|
buf.Append(options.Files[i]);
|
||||||
|
|
||||||
|
if (options.Verbose)
|
||||||
|
Console.WriteLine($"Loading {buf} ...");
|
||||||
|
using var file = File.OpenRead(buf.ToString());
|
||||||
|
var png = Png.Open(file);
|
||||||
|
width = png.Width;
|
||||||
|
height = png.Height;
|
||||||
|
|
||||||
|
max_width = Math.Max(max_width, width);
|
||||||
|
max_height = Math.Max(max_height, height);
|
||||||
|
|
||||||
|
if (options.Verbose)
|
||||||
|
Console.WriteLine($"{width}x{height} image decoded.");
|
||||||
|
|
||||||
|
if (!FillMorph(morph, (ulong)i, png, (uint)width) && options.Verbose)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Unable to fill {i}th frame.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (morph.GetFrameCount() == 0)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Error. Morph does not contain any key frames.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
width = max_width;
|
||||||
|
height = max_height;
|
||||||
|
morph.SetResolution((ushort)width, (ushort)height);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool FillMorph(Morph morph, ulong frame, Png png, uint width)
|
||||||
|
{
|
||||||
|
ulong pixel = 0;
|
||||||
|
bool empty = true;
|
||||||
|
|
||||||
|
if (width > ushort.MaxValue)
|
||||||
|
return false;
|
||||||
|
for (var x = 0; x < png.Width; ++x)
|
||||||
|
{
|
||||||
|
for (var y = 0; y < png.Height; ++y)
|
||||||
|
{
|
||||||
|
var p = png.GetPixel(x, y);
|
||||||
|
var r = p.R;
|
||||||
|
var g = p.G;
|
||||||
|
var b = p.B;
|
||||||
|
var a = p.A;
|
||||||
|
|
||||||
|
if (a == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
morph.AddPixel(frame,
|
||||||
|
new Pixel()
|
||||||
|
{
|
||||||
|
X = (ushort)(x), Y = (ushort)(y), Color = new Color(r, g, b, a),
|
||||||
|
});
|
||||||
|
pixel++;
|
||||||
|
empty = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty)
|
||||||
|
morph.AddFrame(frame);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void MainLoop(Morph morph, Options options)
|
||||||
|
{
|
||||||
|
if (options.Verbose)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Blobifying {morph.GetWidth()}x{morph.GetHeight()} morph.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var start = DateTime.UtcNow;
|
||||||
|
|
||||||
|
ulong blob_count = 0;
|
||||||
|
ulong largest_frame = 0;
|
||||||
|
double last_energy = 0.0;
|
||||||
|
ulong match_blobs = 0;
|
||||||
|
ulong morph_atoms = 0;
|
||||||
|
var fs = options.Files.Count;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
morph.Suspend();
|
||||||
|
morph.Synchronize();
|
||||||
|
morph.Compute();
|
||||||
|
|
||||||
|
blob_count = 0;
|
||||||
|
for (ulong i = 0; i < (ulong)fs; ++i)
|
||||||
|
{
|
||||||
|
ulong bc = morph.GetBlobCount(i);
|
||||||
|
if (bc >= blob_count)
|
||||||
|
{
|
||||||
|
blob_count = bc;
|
||||||
|
largest_frame = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var morph_state = morph.State;
|
||||||
|
|
||||||
|
var end = DateTime.UtcNow;
|
||||||
|
if ((end - start).TotalMilliseconds > 1000)
|
||||||
|
{
|
||||||
|
if (morph_state is MorphState.BlobDetection or MorphState.BlobUnification)
|
||||||
|
{
|
||||||
|
if (options.Verbose)
|
||||||
|
{
|
||||||
|
Console.WriteLine(
|
||||||
|
$"{blob_count}/{morph.GetBlobCount()} blob{(blob_count == 1 ? "" : "s")} remaining on frame {largest_frame}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (morph_state == MorphState.BlobMatching)
|
||||||
|
{
|
||||||
|
if (options.Verbose)
|
||||||
|
{
|
||||||
|
double e = morph.GetEnergy();
|
||||||
|
if (last_energy < e)
|
||||||
|
Console.WriteLine($"Matching blobs, absolute energy was {e:30.10f}.");
|
||||||
|
else
|
||||||
|
Console.WriteLine($"Matching blobs, energy decreased by {last_energy - e:30.10f}.");
|
||||||
|
last_energy = e;
|
||||||
|
}
|
||||||
|
if (++match_blobs >= options.MatchTime)
|
||||||
|
{
|
||||||
|
morph.NextState();
|
||||||
|
last_energy = 0.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (morph_state == MorphState.AtomMorphing)
|
||||||
|
{
|
||||||
|
if (options.Verbose)
|
||||||
|
{
|
||||||
|
double e = morph.GetEnergy();
|
||||||
|
if (last_energy < e)
|
||||||
|
Console.WriteLine($"Matching atoms, absolute energy was {e:30.10f}.");
|
||||||
|
else
|
||||||
|
Console.WriteLine($"Matching atoms, energy decreased by {last_energy - e:30.10f}.");
|
||||||
|
last_energy = e;
|
||||||
|
}
|
||||||
|
if (++morph_atoms >= options.MorphTime)
|
||||||
|
{
|
||||||
|
morph.NextState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (morph_state == MorphState.Done)
|
||||||
|
{
|
||||||
|
if (options.Verbose)
|
||||||
|
Console.WriteLine("All done!\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (options.Verbose)
|
||||||
|
Console.WriteLine("Unknown state!\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
start = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
Thread.Sleep(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.Verbose)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Frame %lu had the most blobs (%lu).\n", largest_frame, blob_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void SaveFiles(Morph morph, Options options)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < options.FramesOut; ++i)
|
||||||
|
{
|
||||||
|
WriteImage(morph, options, (ulong)i, morph.GetWidth(), morph.GetHeight());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void WriteImage(Morph morph, Options options, ulong frame_out, uint width, uint height)
|
||||||
|
{
|
||||||
|
double t = morph.GetTime(frame_out, options.FramesOut);
|
||||||
|
|
||||||
|
var buf = $"{options.Outdir}/{options.File}_{frame_out + 1:0000}.png";
|
||||||
|
if (options.Verbose)
|
||||||
|
Console.WriteLine($"Rendering {frame_out + 1:0000}. frame ...");
|
||||||
|
|
||||||
|
var png = PngBuilder.Create((int)width, (int)height, true);
|
||||||
|
|
||||||
|
var pixels = new List<Pixel>();
|
||||||
|
morph.GetPixels(t, pixels);
|
||||||
|
foreach (var pixel in pixels)
|
||||||
|
{
|
||||||
|
png.SetPixel(new BigGustave.Pixel(pixel.Color.R, pixel.Color.G, pixel.Color.B, pixel.Color.A, false),
|
||||||
|
pixel.X, pixel.Y);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.Verbose)
|
||||||
|
Console.WriteLine($"Writing {buf} ... ");
|
||||||
|
using var file = File.Create(buf);
|
||||||
|
png.Save(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
After Width: | Height: | Size: 628 B |
|
After Width: | Height: | Size: 402 B |
|
After Width: | Height: | Size: 385 B |
|
After Width: | Height: | Size: 417 B |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 1.7 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
|
After Width: | Height: | Size: 1.7 KiB |
|
After Width: | Height: | Size: 1.1 KiB |
|
After Width: | Height: | Size: 1.8 KiB |
|
After Width: | Height: | Size: 2.2 KiB |
|
After Width: | Height: | Size: 2.3 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 4.4 KiB |
|
After Width: | Height: | Size: 4.2 KiB |
|
After Width: | Height: | Size: 5.2 KiB |
|
After Width: | Height: | Size: 4.6 KiB |
|
After Width: | Height: | Size: 5.4 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 942 B |
|
After Width: | Height: | Size: 925 B |
|
After Width: | Height: | Size: 932 B |
|
After Width: | Height: | Size: 999 B |
|
After Width: | Height: | Size: 1.0 KiB |
|
After Width: | Height: | Size: 1.0 KiB |
|
After Width: | Height: | Size: 1.4 KiB |
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.5 KiB |
|
After Width: | Height: | Size: 1.9 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 3.7 KiB |
|
After Width: | Height: | Size: 4.3 KiB |
|
After Width: | Height: | Size: 2.6 KiB |
|
After Width: | Height: | Size: 2.3 KiB |
|
After Width: | Height: | Size: 2.2 KiB |
|
After Width: | Height: | Size: 2.6 KiB |
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 2.8 KiB |
|
After Width: | Height: | Size: 4.2 KiB |
|
After Width: | Height: | Size: 3.5 KiB |
|
After Width: | Height: | Size: 3.3 KiB |
|
After Width: | Height: | Size: 4.0 KiB |
|
After Width: | Height: | Size: 4.3 KiB |
|
After Width: | Height: | Size: 4.4 KiB |
|
After Width: | Height: | Size: 2.8 KiB |
|
After Width: | Height: | Size: 2.4 KiB |
|
After Width: | Height: | Size: 2.3 KiB |
|
After Width: | Height: | Size: 2.7 KiB |
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 18 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 721 B |
|
After Width: | Height: | Size: 730 B |
|
After Width: | Height: | Size: 710 B |
|
After Width: | Height: | Size: 725 B |
|
After Width: | Height: | Size: 713 B |
|
After Width: | Height: | Size: 704 B |
|
After Width: | Height: | Size: 5.5 KiB |
|
After Width: | Height: | Size: 5.4 KiB |
|
After Width: | Height: | Size: 5.3 KiB |
|
After Width: | Height: | Size: 5.5 KiB |
|
After Width: | Height: | Size: 443 B |
|
After Width: | Height: | Size: 452 B |
|
After Width: | Height: | Size: 476 B |
|
After Width: | Height: | Size: 462 B |
|
After Width: | Height: | Size: 136 B |
|
After Width: | Height: | Size: 129 KiB |
|
After Width: | Height: | Size: 19 KiB |
|
After Width: | Height: | Size: 27 KiB |