diff --git a/README.md b/README.md index 583d71e..6f7e003 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ The algorithms are: -1. Full neighbourhood search algorithm of [Alexei Efros + Thomas Leung](https://www.eecs.berkeley.edu/Research/Projects/CS/vision/papers/efros-iccv99.pdf) and [Li-Yi Wei + Marc Levoy](https://graphics.stanford.edu/papers/texture-synthesis-sig00/texture.pdf) is probably the simplest texture synthesis algorithm imaginable. +1. Full neighbourhood search algorithm of [Scott Draves](http://draves.org/fuse) and [Alexei Efros + Thomas Leung](https://www.eecs.berkeley.edu/Research/Projects/CS/vision/papers/efros-iccv99.pdf) and [Li-Yi Wei + Marc Levoy](https://graphics.stanford.edu/papers/texture-synthesis-sig00/texture.pdf) is probably the simplest texture synthesis algorithm imaginable. 2. K-coherent neighbourhood search of [Michael Ashikhmin](http://www.cs.princeton.edu/courses/archive/fall10/cos526/papers/ashikhmin01a.pdf) and [Xin Tong + Jingdan Zhangz + Ligang Liu + Xi Wangz + Baining Guo + Heung-Yeung Shum](http://research.microsoft.com/pubs/65191/btfsynthesis.pdf) takes computational burden from the synthesis to the analysis part and therefore is better suited for synthesizing large textures. 3. Resynthesis algorithm of [P. F. Harrison](http://logarithmic.net/pfh-files/thesis/dissertation.pdf) is scale-invariant, fast, supports constraints and practically never produces completely unsatisfactory results. diff --git a/Samples/bluehalls.bmp b/Samples/bluehalls.bmp deleted file mode 100644 index 3f11316..0000000 Binary files a/Samples/bluehalls.bmp and /dev/null differ diff --git a/Samples/bluehalls.png b/Samples/bluehalls.png new file mode 100644 index 0000000..a46863a Binary files /dev/null and b/Samples/bluehalls.png differ diff --git a/Samples/brocks.bmp b/Samples/brocks.bmp deleted file mode 100644 index 471ffed..0000000 Binary files a/Samples/brocks.bmp and /dev/null differ diff --git a/Samples/brocks.png b/Samples/brocks.png new file mode 100644 index 0000000..061df07 Binary files /dev/null and b/Samples/brocks.png differ diff --git a/Samples/cobbles48.bmp b/Samples/cobbles48.bmp deleted file mode 100644 index 38c9d93..0000000 Binary files a/Samples/cobbles48.bmp and /dev/null differ diff --git a/Samples/cobbles48.png b/Samples/cobbles48.png new file mode 100644 index 0000000..d0443ce Binary files /dev/null and b/Samples/cobbles48.png differ diff --git a/Samples/forest1.bmp b/Samples/forest1.bmp deleted file mode 100644 index 92df521..0000000 Binary files a/Samples/forest1.bmp and /dev/null differ diff --git a/Samples/forest1.png b/Samples/forest1.png new file mode 100644 index 0000000..4a0c459 Binary files /dev/null and b/Samples/forest1.png differ diff --git a/Samples/forest2.bmp b/Samples/forest2.bmp deleted file mode 100644 index d74a36a..0000000 Binary files a/Samples/forest2.bmp and /dev/null differ diff --git a/Samples/forest2.png b/Samples/forest2.png new file mode 100644 index 0000000..adf38ae Binary files /dev/null and b/Samples/forest2.png differ diff --git a/Samples/grass.bmp b/Samples/grass.bmp deleted file mode 100644 index 80f9e1b..0000000 Binary files a/Samples/grass.bmp and /dev/null differ diff --git a/Samples/grass.png b/Samples/grass.png new file mode 100644 index 0000000..a781a00 Binary files /dev/null and b/Samples/grass.png differ diff --git a/Samples/grass48.bmp b/Samples/grass48.bmp deleted file mode 100644 index c8b3572..0000000 Binary files a/Samples/grass48.bmp and /dev/null differ diff --git a/Samples/grass48.png b/Samples/grass48.png new file mode 100644 index 0000000..dfc5a97 Binary files /dev/null and b/Samples/grass48.png differ diff --git a/Samples/hills.bmp b/Samples/hills.bmp deleted file mode 100644 index 8f3555e..0000000 Binary files a/Samples/hills.bmp and /dev/null differ diff --git a/Samples/hills.png b/Samples/hills.png new file mode 100644 index 0000000..6fd7057 Binary files /dev/null and b/Samples/hills.png differ diff --git a/Samples/lava.bmp b/Samples/lava.bmp deleted file mode 100644 index 2732b55..0000000 Binary files a/Samples/lava.bmp and /dev/null differ diff --git a/Samples/lava.png b/Samples/lava.png new file mode 100644 index 0000000..46a82c0 Binary files /dev/null and b/Samples/lava.png differ diff --git a/Samples/mountains.bmp b/Samples/mountains.bmp deleted file mode 100644 index d2891ed..0000000 Binary files a/Samples/mountains.bmp and /dev/null differ diff --git a/Samples/mountains.png b/Samples/mountains.png new file mode 100644 index 0000000..237f141 Binary files /dev/null and b/Samples/mountains.png differ diff --git a/Samples/redfoam.bmp b/Samples/redfoam.bmp deleted file mode 100644 index 15e129a..0000000 Binary files a/Samples/redfoam.bmp and /dev/null differ diff --git a/Samples/redfoam.png b/Samples/redfoam.png new file mode 100644 index 0000000..40dccac Binary files /dev/null and b/Samples/redfoam.png differ diff --git a/Samples/sand1.bmp b/Samples/sand1.bmp deleted file mode 100644 index 0ed7d23..0000000 Binary files a/Samples/sand1.bmp and /dev/null differ diff --git a/Samples/sand1.png b/Samples/sand1.png new file mode 100644 index 0000000..52bb90e Binary files /dev/null and b/Samples/sand1.png differ diff --git a/Samples/sand2.bmp b/Samples/sand2.bmp deleted file mode 100644 index d9324a1..0000000 Binary files a/Samples/sand2.bmp and /dev/null differ diff --git a/Samples/sand2.png b/Samples/sand2.png new file mode 100644 index 0000000..4bb78d4 Binary files /dev/null and b/Samples/sand2.png differ diff --git a/Samples/shallows.bmp b/Samples/shallows.bmp deleted file mode 100644 index 09cb706..0000000 Binary files a/Samples/shallows.bmp and /dev/null differ diff --git a/Samples/shallows.png b/Samples/shallows.png new file mode 100644 index 0000000..238144f Binary files /dev/null and b/Samples/shallows.png differ diff --git a/Samples/skulls.bmp b/Samples/skulls.bmp deleted file mode 100644 index 6e78aa4..0000000 Binary files a/Samples/skulls.bmp and /dev/null differ diff --git a/Samples/skulls.png b/Samples/skulls.png new file mode 100644 index 0000000..e6d3d65 Binary files /dev/null and b/Samples/skulls.png differ diff --git a/Samples/stones.bmp b/Samples/stones.bmp deleted file mode 100644 index 6072b5d..0000000 Binary files a/Samples/stones.bmp and /dev/null differ diff --git a/Samples/stones.png b/Samples/stones.png new file mode 100644 index 0000000..58fd178 Binary files /dev/null and b/Samples/stones.png differ diff --git a/Samples/strangegrass.bmp b/Samples/strangegrass.bmp deleted file mode 100644 index d4b0889..0000000 Binary files a/Samples/strangegrass.bmp and /dev/null differ diff --git a/Samples/strangegrass.png b/Samples/strangegrass.png new file mode 100644 index 0000000..a6d3b9f Binary files /dev/null and b/Samples/strangegrass.png differ diff --git a/Samples/sword.bmp b/Samples/sword.bmp deleted file mode 100644 index 29034e3..0000000 Binary files a/Samples/sword.bmp and /dev/null differ diff --git a/Samples/sword.png b/Samples/sword.png new file mode 100644 index 0000000..1aa041f Binary files /dev/null and b/Samples/sword.png differ diff --git a/Samples/vines.bmp b/Samples/vines.bmp deleted file mode 100644 index 633d5f9..0000000 Binary files a/Samples/vines.bmp and /dev/null differ diff --git a/Samples/vines.png b/Samples/vines.png new file mode 100644 index 0000000..776f170 Binary files /dev/null and b/Samples/vines.png differ diff --git a/Samples/wall.bmp b/Samples/wall.bmp deleted file mode 100644 index a9de5f9..0000000 Binary files a/Samples/wall.bmp and /dev/null differ diff --git a/Samples/wall.png b/Samples/wall.png new file mode 100644 index 0000000..81b54fd Binary files /dev/null and b/Samples/wall.png differ diff --git a/Samples/water1.bmp b/Samples/water1.bmp deleted file mode 100644 index ba9bfe0..0000000 Binary files a/Samples/water1.bmp and /dev/null differ diff --git a/Samples/water1.png b/Samples/water1.png new file mode 100644 index 0000000..b6a4968 Binary files /dev/null and b/Samples/water1.png differ diff --git a/Samples/water2.bmp b/Samples/water2.bmp deleted file mode 100644 index fb78e32..0000000 Binary files a/Samples/water2.bmp and /dev/null differ diff --git a/Samples/water2.png b/Samples/water2.png new file mode 100644 index 0000000..71ee8ad Binary files /dev/null and b/Samples/water2.png differ diff --git a/Samples/water3.bmp b/Samples/water3.bmp deleted file mode 100644 index 709d837..0000000 Binary files a/Samples/water3.bmp and /dev/null differ diff --git a/Samples/water3.png b/Samples/water3.png new file mode 100644 index 0000000..ddabab0 Binary files /dev/null and b/Samples/water3.png differ diff --git a/Samples/water4.bmp b/Samples/water4.bmp deleted file mode 100644 index 6252915..0000000 Binary files a/Samples/water4.bmp and /dev/null differ diff --git a/Samples/water4.png b/Samples/water4.png new file mode 100644 index 0000000..df5233f Binary files /dev/null and b/Samples/water4.png differ diff --git a/SynTex.cs b/SynTex.cs index 1ad2d4f..9239aae 100644 --- a/SynTex.cs +++ b/SynTex.cs @@ -10,9 +10,9 @@ The above copyright notice and this permission notice shall be included in all c */ using System; -using System.Xml; using System.Linq; using System.Drawing; +using System.Xml.Linq; using System.Diagnostics; using System.ComponentModel; using System.Collections.Generic; @@ -22,46 +22,45 @@ static class Program static void Main() { Stopwatch sw = Stopwatch.StartNew(); - var xdoc = new XmlDocument(); - xdoc.Load("samples.xml"); + XDocument xdoc = XDocument.Load("samples.xml"); int pass = 1; - foreach (XmlNode xnode in xdoc.FirstChild.ChildNodes) + foreach (XElement xelem in xdoc.Root.Elements("sample")) { - string name = xnode.Get("name", ""), method = xnode.Name; - int K = xnode.Get("K", 1), N = xnode.Get("N", 1), M = xnode.Get("M", 20), polish = xnode.Get("polish", 3), OW = xnode.Get("width", 32), OH = xnode.Get("height", 32); - bool indexed = xnode.Get("indexed", true); - double t = xnode.Get("temperature", 1.0); + string name = xelem.Get("name"), method = xelem.Get("method"); + int K = xelem.Get("K", 1), N = xelem.Get("N", 1), M = xelem.Get("M", 20), polish = xelem.Get("polish", 3), OW = xelem.Get("width", 32), OH = xelem.Get("height", 32); + bool indexed = xelem.Get("indexed", true); + double t = xelem.Get("temperature", 1.0); - Bitmap sample = new Bitmap($"Samples/{name}.bmp"); + Bitmap sample = new Bitmap($"Samples/{name}.png"); List[] similaritySets = null; int[] sampleArray = new int[sample.Width * sample.Height]; for (int j = 0; j < sample.Width * sample.Height; j++) sampleArray[j] = sample.GetPixel(j % sample.Width, j / sample.Width).ToArgb(); - if (method == "coherent") + if (method == "Coherent") { Console.WriteLine($"< {name}"); similaritySets = Analysis(sampleArray, sample.Width, sample.Height, K, N, indexed); } - for (int i = 0; i < xnode.Get("screenshots", 1); i++) + for (int i = 0; i < xelem.Get("screenshots", 1); i++) { Console.WriteLine($"> {name} {i}"); string filename = $"{pass} {method} {name} {indexed} N={N} "; int[] outputArray; - if (method == "full") + if (method == "Full") { outputArray = FullSynthesis(sampleArray, sample.Width, sample.Height, N, OW, OH, t, indexed); filename += $"t={t}"; } - else if (method == "coherent") + else if (method == "Coherent") { outputArray = CoherentSynthesis(sampleArray, sample.Width, sample.Height, similaritySets, N, OW, OH, t, indexed); filename += $"K={K} t={t}"; } - else if (method == "re") + else if (method == "Harrison") { outputArray = ReSynthesis(sampleArray, sample.Width, sample.Height, N, M, polish, indexed, OW, OH); filename += $"M={M} polish={polish}"; @@ -70,7 +69,7 @@ static void Main() Bitmap output = new Bitmap(OW, OH); for (int j = 0; j < OW * OH; j++) output.SetPixel(j % OW, j / OW, Color.FromArgb(outputArray[j])); - output.Save($"{filename} {i}.bmp"); + output.Save($"{filename} {i}.png"); } pass++; @@ -246,7 +245,7 @@ static int[] ReSynthesis(int[] sample, int SW, int SH, int N, int M, int polish, int colorsNumber = colors.Count; - Func metric = (c1, c2) => + double metric(int c1, int c2) { Color color1 = Color.FromArgb(c1), color2 = Color.FromArgb(c2); const double lambda = 1.0 / (20.0 * 65536.0); @@ -400,11 +399,10 @@ static int[] ReSynthesis(int[] sample, int SW, int SH, int N, int M, int polish, static class Stuff { - public static T Get(this XmlNode node, string attribute, T defaultT = default(T)) + public static T Get(this XElement xelem, string attribute, T defaultT = default(T)) { - string s = ((XmlElement)node).GetAttribute(attribute); - var converter = TypeDescriptor.GetConverter(typeof(T)); - return s == "" ? defaultT : (T)converter.ConvertFromInvariantString(s); + XAttribute a = xelem.Attribute(attribute); + return a == null ? defaultT : (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(a.Value); } public static int Random(this double[] array, double r) @@ -413,16 +411,16 @@ public static int Random(this double[] array, double r) if (sum <= 0) { - for (int j = 0; j < array.Count(); j++) array[j] = 1; + for (int j = 0; j < array.Length; j++) array[j] = 1; sum = array.Sum(); } - for (int j = 0; j < array.Count(); j++) array[j] /= sum; + for (int j = 0; j < array.Length; j++) array[j] /= sum; int i = 0; double x = 0; - while (i < array.Count()) + while (i < array.Length) { x += array[i]; if (r <= x) return i; @@ -432,5 +430,5 @@ public static int Random(this double[] array, double r) return 0; } - public static int Random(this Dictionary dic, double r) { return dic.Keys.ToArray()[dic.Values.ToArray().Random(r)]; } -} \ No newline at end of file + public static int Random(this Dictionary dic, double r) => dic.Keys.ToArray()[dic.Values.ToArray().Random(r)]; +} diff --git a/samples.xml b/samples.xml index c39657c..cd22d4c 100644 --- a/samples.xml +++ b/samples.xml @@ -1,39 +1,39 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +