Skip to content

Commit

Permalink
bmp -> png, xml.linq, C# 7.0, fuse
Browse files Browse the repository at this point in the history
  • Loading branch information
mxgmn committed Jul 24, 2017
1 parent f95aa77 commit f686268
Show file tree
Hide file tree
Showing 51 changed files with 62 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Binary file removed Samples/bluehalls.bmp
Binary file not shown.
Binary file added Samples/bluehalls.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/brocks.bmp
Binary file not shown.
Binary file added Samples/brocks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/cobbles48.bmp
Binary file not shown.
Binary file added Samples/cobbles48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/forest1.bmp
Binary file not shown.
Binary file added Samples/forest1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/forest2.bmp
Binary file not shown.
Binary file added Samples/forest2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/grass.bmp
Binary file not shown.
Binary file added Samples/grass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/grass48.bmp
Binary file not shown.
Binary file added Samples/grass48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/hills.bmp
Binary file not shown.
Binary file added Samples/hills.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/lava.bmp
Binary file not shown.
Binary file added Samples/lava.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/mountains.bmp
Binary file not shown.
Binary file added Samples/mountains.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/redfoam.bmp
Binary file not shown.
Binary file added Samples/redfoam.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/sand1.bmp
Binary file not shown.
Binary file added Samples/sand1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/sand2.bmp
Binary file not shown.
Binary file added Samples/sand2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/shallows.bmp
Binary file not shown.
Binary file added Samples/shallows.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/skulls.bmp
Binary file not shown.
Binary file added Samples/skulls.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/stones.bmp
Binary file not shown.
Binary file added Samples/stones.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/strangegrass.bmp
Binary file not shown.
Binary file added Samples/strangegrass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/sword.bmp
Binary file not shown.
Binary file added Samples/sword.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/vines.bmp
Binary file not shown.
Binary file added Samples/vines.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/wall.bmp
Binary file not shown.
Binary file added Samples/wall.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/water1.bmp
Binary file not shown.
Binary file added Samples/water1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/water2.bmp
Binary file not shown.
Binary file added Samples/water2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/water3.bmp
Binary file not shown.
Binary file added Samples/water3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Samples/water4.bmp
Binary file not shown.
Binary file added Samples/water4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 23 additions & 25 deletions SynTex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string>("name"), method = xelem.Get<string>("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<int>[] 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}";
Expand All @@ -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++;
Expand Down Expand Up @@ -246,7 +245,7 @@ static int[] ReSynthesis(int[] sample, int SW, int SH, int N, int M, int polish,

int colorsNumber = colors.Count;

Func<int, int, double> 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);
Expand Down Expand Up @@ -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<T>(this XmlNode node, string attribute, T defaultT = default(T))
public static T Get<T>(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)
Expand All @@ -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;
Expand All @@ -432,5 +430,5 @@ public static int Random(this double[] array, double r)
return 0;
}

public static int Random(this Dictionary<int, double> dic, double r) { return dic.Keys.ToArray()[dic.Values.ToArray().Random(r)]; }
}
public static int Random(this Dictionary<int, double> dic, double r) => dic.Keys.ToArray()[dic.Values.ToArray().Random(r)];
}
76 changes: 38 additions & 38 deletions samples.xml
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
<samples>
<re name="lava" N="2" width="100" height="100" indexed="True"/>
<re name="water1" N="3"/>
<re name="forest1" N="1" width="400" height="400" screenshots="3"/>
<re name="forest1" N="2" width="400" height="400" M="5"/>
<re name="forest1" N="3" polish="3" width="400" height="400"/>
<re name="cobbles48" N="3" polish="6" width="400" height="400"/>
<re name="water3" N="3"/>
<re name="water4" N="6"/>
<re name="brocks" N="1"/>
<re name="brocks" N="2"/>
<re name="forest2" N="1"/>
<re name="forest2" N="2"/>
<re name="forest2" N="4"/>
<re name="bluehalls" N="1" polish="5"/>
<re name="skulls" N="3"/>
<re name="strangegrass" N="3"/>
<re name="grass48" N="1"/>
<re name="sword" N="6"/>
<re name="redfoam" N="1" width="300" height="300" screenshots="3"/>
<re name="redfoam" N="2" width="300" height="300" screenshots="3"/>
<re name="redfoam" N="3" width="300" height="300" screenshots="3"/>
<coherent name="mountains" indexed="True" K="3" N="2" screenshots="2" width="400" height="400"/>
<coherent name="hills" indexed="True" K="3" N="2" screenshots="2" width="400" height="400"/>
<coherent name="grass" indexed="True" K="3" N="2" screenshots="2" width="400" height="400"/>
<coherent name="forest1" indexed="True" K="4" N="2" screenshots="2" width="400" height="400"/>
<coherent name="water3" indexed="True" K="4" N="3" screenshots="2" width="400" height="400"/>
<coherent name="shallows" indexed="True" K="4" N="3" screenshots="2" width="400" height="400"/>
<full name="sand1" indexed="False" N="3" screenshots="2" width="46" height="46"/>
<full name="sand2" indexed="False" N="3" screenshots="2" width="46" height="46"/>
<full name="lava" indexed="False" N="3" screenshots="2" width="32" height="32"/>
<full name="stones" indexed="False" N="3" screenshots="2" width="32" height="32"/>
<full name="wall" indexed="False" N="2" screenshots="2" width="32" height="32"/>
<full name="vines" indexed="True" N="3" screenshots="2" width="32" height="32"/>
<full name="vines" indexed="False" N="2" screenshots="2" width="32" height="32"/>
<full name="redfoam" indexed="False" N="3" screenshots="2" width="32" height="32"/>
<full name="redfoam" indexed="False" N="2" screenshots="2" width="32" height="32"/>
<full name="water2" indexed="False" N="3" screenshots="4" width="48" height="48"/>
</samples>
<sample method="Harrison" name="lava" N="2" width="100" height="100" indexed="True"/>
<sample method="Harrison" name="water1" N="3"/>
<sample method="Harrison" name="forest1" N="1" width="400" height="400" screenshots="3"/>
<sample method="Harrison" name="forest1" N="2" width="400" height="400" M="5"/>
<sample method="Harrison" name="forest1" N="3" polish="3" width="400" height="400"/>
<sample method="Harrison" name="cobbles48" N="3" polish="6" width="400" height="400"/>
<sample method="Harrison" name="water3" N="3"/>
<sample method="Harrison" name="water4" N="6"/>
<sample method="Harrison" name="brocks" N="1"/>
<sample method="Harrison" name="brocks" N="2"/>
<sample method="Harrison" name="forest2" N="1"/>
<sample method="Harrison" name="forest2" N="2"/>
<sample method="Harrison" name="forest2" N="4"/>
<sample method="Harrison" name="bluehalls" N="1" polish="5"/>
<sample method="Harrison" name="skulls" N="3"/>
<sample method="Harrison" name="strangegrass" N="3"/>
<sample method="Harrison" name="grass48" N="1"/>
<sample method="Harrison" name="sword" N="6"/>
<sample method="Harrison" name="redfoam" N="1" width="300" height="300" screenshots="3"/>
<sample method="Harrison" name="redfoam" N="2" width="300" height="300" screenshots="3"/>
<sample method="Harrison" name="redfoam" N="3" width="300" height="300" screenshots="3"/>
<sample method="Coherent" name="mountains" indexed="True" K="3" N="2" screenshots="2" width="400" height="400"/>
<sample method="Coherent" name="hills" indexed="True" K="3" N="2" screenshots="2" width="400" height="400"/>
<sample method="Coherent" name="grass" indexed="True" K="3" N="2" screenshots="2" width="400" height="400"/>
<sample method="Coherent" name="forest1" indexed="True" K="4" N="2" screenshots="2" width="400" height="400"/>
<sample method="Coherent" name="water3" indexed="True" K="4" N="3" screenshots="2" width="400" height="400"/>
<sample method="Coherent" name="shallows" indexed="True" K="4" N="3" screenshots="2" width="400" height="400"/>
<sample method="Full" name="sand1" indexed="False" N="3" screenshots="2" width="46" height="46"/>
<sample method="Full" name="sand2" indexed="False" N="3" screenshots="2" width="46" height="46"/>
<sample method="Full" name="lava" indexed="False" N="3" screenshots="2" width="32" height="32"/>
<sample method="Full" name="stones" indexed="False" N="3" screenshots="2" width="32" height="32"/>
<sample method="Full" name="wall" indexed="False" N="2" screenshots="2" width="32" height="32"/>
<sample method="Full" name="vines" indexed="True" N="3" screenshots="2" width="32" height="32"/>
<sample method="Full" name="vines" indexed="False" N="2" screenshots="2" width="32" height="32"/>
<sample method="Full" name="redfoam" indexed="False" N="3" screenshots="2" width="32" height="32"/>
<sample method="Full" name="redfoam" indexed="False" N="2" screenshots="2" width="32" height="32"/>
<sample method="Full" name="water2" indexed="False" N="3" screenshots="4" width="48" height="48"/>
</samples>

0 comments on commit f686268

Please sign in to comment.