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(16); morph.SetCycleLength(10000); 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:F2} seconds."); } return 0; } 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 AtomorphColor(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; var stepsWithoutEnergyChange = 0; 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:F2}."); else Console.WriteLine($"Matching blobs, energy decreased by {last_energy - e:F2}."); last_energy = e; } if (++match_blobs >= options.MatchTime) { morph.NextState(); last_energy = 0.0; } } else if (morph_state == MorphState.AtomMorphing) { double e = morph.GetEnergy(); if (options.Verbose) { if (last_energy < e) Console.WriteLine($"Matching atoms, absolute energy was {e:F2}."); else Console.WriteLine( $"Matching atoms, energy decreased by {last_energy - e:F2}"); } if (last_energy == e) { stepsWithoutEnergyChange++; if (stepsWithoutEnergyChange > 3) { if (options.Verbose) Console.WriteLine("No energy change for 3 steps, moving to next state."); morph.NextState(); } } 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 {largest_frame} had the most blobs {blob_count}.\n"); } 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(); 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); } }