forked from mxgmn/TextureSynthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHarrison.cs
321 lines (273 loc) · 11.8 KB
/
Harrison.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using BumpKit;
public class Harrison : Program.SynTex.ITextureSynthesisAlgorithm
{
public class Parameters
{
public string SampleFilename;
public string OutputFilename;
public int Neighborhood;
public int M;
public int Polish;
public int OutputWidth;
public int OutputHeight;
public int Seed;
}
private Parameters _parameters;
private Bitmap _sample;
private long _elapsedTime;
private int _seed;
public void ParseCommandLine(string[] commandLineStrings)
{
_parameters = new Parameters();
if (commandLineStrings[0] != GetAlgorithmShortName())
{
throw new Exception("Wrong algorithm name.");
}
_parameters.SampleFilename = commandLineStrings[1];
_parameters.OutputFilename = commandLineStrings[2];
_parameters.Neighborhood = Convert.ToInt32(commandLineStrings[3]);
_parameters.M = Convert.ToInt32(commandLineStrings[4]);
_parameters.Polish = Convert.ToInt32(commandLineStrings[5]);
_parameters.OutputWidth = Convert.ToInt32(commandLineStrings[6]);
_parameters.OutputHeight = Convert.ToInt32(commandLineStrings[7]);
_parameters.Seed = Convert.ToInt32(commandLineStrings[8]);
}
public string GetAlgorithmName()
{
return "Harrison";
}
public string GetAlgorithmShortName()
{
return "HAR";
}
public void PrintHelp()
{
Console.WriteLine("HAR SampleFileName OutputFileName Neighborhood OutputWidth OutputHeight Seed");
Console.WriteLine(" HAR - short name of algorithm to use");
Console.WriteLine(" SampleFileName - sample file name including extension to use");
Console.WriteLine(" OutputFileName - output file name including extension");
Console.WriteLine(" Neighborhood - Neighborhood around the pixel to consider");
Console.WriteLine(" M - ");
Console.WriteLine(" Polish - number of polishing frames. ");
Console.WriteLine(" OutputWidth - output picture width in pixels");
Console.WriteLine(" OutputHeight - output picture width in pixels");
Console.WriteLine(" Seed - random number generator seed. If seed == -1 then seed will be randomized");
Console.WriteLine("");
Console.WriteLine("Example:");
Console.WriteLine(" syntex.exe verbose HAR Samples/water.png Output/watergen.png 3 48 48 42");
}
public void Synthesize()
{
Debug.Assert(_parameters != null);
_sample = new Bitmap($"{_parameters.SampleFilename}");
int[] sampleArray = Utils.BitmapToARGBArray(_sample);
Stopwatch sw = Stopwatch.StartNew();
int[] result = ReSynthesis(sampleArray, _sample.Width, _sample.Height, _parameters);
_elapsedTime = sw.ElapsedMilliseconds;
if (Program.Log.Normal())
Console.WriteLine($"Synthesis duration = {_elapsedTime}");
if(result == null)
return;
var outputBitmap = Utils.ARGBArrayToBitmap(result, _parameters.OutputWidth, _parameters.OutputHeight);
outputBitmap.Save(_parameters.OutputFilename);
}
public string GetCSVRecord()
{
var seed = _parameters.Seed == -1 ? $"-1({_seed})" : _seed.ToString();
return $"{GetAlgorithmShortName()};{_parameters.SampleFilename};{_sample.Width}x{_sample.Height};{_parameters.OutputFilename};{_parameters.OutputWidth}x{_parameters.OutputHeight};{_elapsedTime};{seed};neighborhood={_parameters.Neighborhood}, M={_parameters.M}, Polish={_parameters.Polish}";
}
int[] ReSynthesis(int[] sample, int SW, int SH, Parameters p)
{
List<int> colors = new List<int>();
int[] indexedSample = new int[sample.Length];
var isGif = Path.GetExtension(p.OutputFilename) == ".gif";
for (int j = 0; j < SW * SH; j++)
{
int color = sample[j];
int i = 0;
foreach (var c in colors)
{
if (c == color) break;
i++;
}
if (i == colors.Count)
colors.Add(color);
indexedSample[j] = i;
}
int colorsNumber = colors.Count;
double metric(int c1, int c2)
{
Color color1 = Color.FromArgb(c1), color2 = Color.FromArgb(c2);
const double lambda = 1.0 / (20.0 * 65536.0);
double r = 1.0 + lambda * (double)((color1.R - color2.R) * (color1.R - color2.R));
double g = 1.0 + lambda * (double)((color1.G - color2.G) * (color1.G - color2.G));
double b = 1.0 + lambda * (double)((color1.B - color2.B) * (color1.B - color2.B));
return -Math.Log(r * g * b);
};
double[][] colorMetric = null;
if (colorsNumber <= 1024)
{
colorMetric = new double[colorsNumber][];
for (int x = 0; x < colorsNumber; x++)
{
colorMetric[x] = new double[colorsNumber];
for (int y = 0; y < colorsNumber; y++)
{
int cx = colors[x], cy = colors[y];
colorMetric[x][y] = metric(cx, cy);
}
}
}
int[] origins = new int[p.OutputWidth * p.OutputHeight];
for (int i = 0; i < origins.Length; i++)
origins[i] = -1;
_seed = p.Seed == -1 ? DateTime.Now.Millisecond : p.Seed;
Random random = new Random(_seed);
int[] shuffle = new int[p.OutputWidth * p.OutputHeight];
for (int i = 0; i < shuffle.Length; i++)
{
int j = random.Next(i + 1);
if (j != i)
shuffle[i] = shuffle[j];
shuffle[j] = i;
}
List<Bitmap> bitmaps = new List<Bitmap>();
for (int round = 0; round <= p.Polish; round++)
{
for (int counter = 0; counter < shuffle.Length; counter++)
{
int f = shuffle[counter];
int fx = f % p.OutputWidth, fy = f / p.OutputWidth;
int neighborsNumber = round > 0 ? 8 : Math.Min(8, counter);
int neighborsFound = 0;
int[] candidates = new int[neighborsNumber + p.M];
if (neighborsNumber > 0)
{
int[] neighbors = new int[neighborsNumber];
int[] x = new int[4], y = new int[4];
for (int radius = 1; neighborsFound < neighborsNumber; radius++)
{
x[0] = fx - radius;
y[0] = fy - radius;
x[1] = fx - radius;
y[1] = fy + radius;
x[2] = fx + radius;
y[2] = fy + radius;
x[3] = fx + radius;
y[3] = fy - radius;
for (int k = 0; k < 2 * radius; k++)
{
for (int d = 0; d < 4; d++)
{
x[d] = (x[d] + 10 * p.OutputWidth) % p.OutputWidth;
y[d] = (y[d] + 10 * p.OutputHeight) % p.OutputHeight;
if (neighborsFound >= neighborsNumber)
continue;
int point = x[d] + y[d] * p.OutputWidth;
if (origins[point] != -1)
{
neighbors[neighborsFound] = point;
neighborsFound++;
}
}
y[0]++;
x[1]++;
y[2]--;
x[3]--;
}
}
for (int n = 0; n < neighborsNumber; n++)
{
int cx = (origins[neighbors[n]] + (f - neighbors[n]) % p.OutputWidth + 100 * SW) % SW;
int cy = (origins[neighbors[n]] / SW + f / p.OutputWidth - neighbors[n] / p.OutputWidth +
100 * SH) % SH;
candidates[n] = cx + cy * SW;
}
}
for (int m = 0; m < p.M; m++)
candidates[neighborsNumber + m] = random.Next(SW * SH);
double max = -1E+10;
int argmax = -1;
for (int c = 0; c < candidates.Length; c++)
{
double sum = 1E-6 * random.NextDouble();
int ix = candidates[c] % SW,
iy = candidates[c] / SW,
jx = f % p.OutputWidth,
jy = f / p.OutputWidth;
int SX, SY, FX, FY, S, F;
int origin;
for (int dy = -p.Neighborhood; dy <= p.Neighborhood; dy++)
for (int dx = -p.Neighborhood; dx <= p.Neighborhood; dx++)
if (dx != 0 || dy != 0)
{
SX = ix + dx;
if (SX < 0)
SX += SW;
else if (SX >= SW)
SX -= SW;
SY = iy + dy;
if
(SY < 0) SY += SH;
else if (SY >= SH)
SY -= SH;
FX = jx + dx;
if (FX < 0) FX += p.OutputWidth;
else if (FX >= p.OutputWidth)
FX -= p.OutputWidth;
FY = jy + dy;
if (FY < 0) FY += p.OutputHeight;
else if (FY >= p.OutputHeight) FY -= p.OutputHeight;
S = SX + SY * SW;
F = FX + FY * p.OutputWidth;
origin = origins[F];
if (origin != -1)
{
if (colorMetric != null)
sum += colorMetric[indexedSample[origin]][indexedSample[S]];
else
sum += metric(sample[origin], sample[S]);
}
}
if (sum >= max)
{
max = sum;
argmax = candidates[c];
}
}
origins[f] = argmax;
}
if (isGif)
{
int[] r = new int[p.OutputWidth * p.OutputHeight];
for (int i = 0; i < r.Length; i++)
r[i] = sample[origins[i]];
bitmaps.Add(Utils.ARGBArrayToBitmap(r, _parameters.OutputWidth, _parameters.OutputHeight));
}
}
if (isGif)
{
var gifStream = new MemoryStream();
var encoder = new GifEncoder(gifStream);
foreach (var bitmap in bitmaps)
{
encoder.AddFrame(bitmap,0,0, TimeSpan.FromSeconds(0.2f));
}
gifStream.Position = 0;
using (FileStream file = new FileStream(_parameters.OutputFilename, FileMode.Create, FileAccess.Write))
{
gifStream.WriteTo(file);
}
return null;
}
int[] result = new int[p.OutputWidth * p.OutputHeight];
for (int i = 0; i < result.Length; i++)
result[i] = sample[origins[i]];
return result;
}
}