-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
SynTex.cs
434 lines (356 loc) · 12.3 KB
/
SynTex.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
The MIT License(MIT)
Copyright(c) mxgmn 2016.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.
*/
using System;
using System.Linq;
using System.Drawing;
using System.Xml.Linq;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Generic;
static class Program
{
static void Main()
{
Stopwatch sw = Stopwatch.StartNew();
XDocument xdoc = XDocument.Load("samples.xml");
int pass = 1;
foreach (XElement xelem in xdoc.Root.Elements("sample"))
{
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}.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")
{
Console.WriteLine($"< {name}");
similaritySets = Analysis(sampleArray, sample.Width, sample.Height, K, N, indexed);
}
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")
{
outputArray = FullSynthesis(sampleArray, sample.Width, sample.Height, N, OW, OH, t, indexed);
filename += $"t={t}";
}
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 == "Harrison")
{
outputArray = ReSynthesis(sampleArray, sample.Width, sample.Height, N, M, polish, indexed, OW, OH);
filename += $"M={M} polish={polish}";
}
else continue;
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}.png");
}
pass++;
}
Console.WriteLine($"time = {sw.ElapsedMilliseconds}");
}
static List<int>[] Analysis(int[] bitmap, int width, int height, int K, int N, bool indexed)
{
int area = width * height;
var result = new List<int>[area];
var points = new List<int>();
for (int i = 0; i < area; i++) points.Add(i);
double[] similarities = new double[area * area];
for (int i = 0; i < area; i++) for (int j = 0; j < area; j++)
similarities[i * area + j] = similarities[j * area + i] != 0 ? similarities[j * area + i] :
Similarity(i, bitmap, width, height, j, bitmap, width, height, N, null, indexed);
for (int i = 0; i < area; i++)
{
result[i] = new List<int>();
var copy = new List<int>(points);
result[i].Add(i);
copy.Remove(i);
for (int k = 1; k < K; k++)
{
double max = -1E-4;
int argmax = -1;
foreach (int p in copy)
{
double s = similarities[i * area + p];
if (s > max)
{
max = s;
argmax = p;
}
}
result[i].Add(argmax);
copy.Remove(argmax);
}
}
return result;
}
static int[] CoherentSynthesis(int[] sample, int SW, int SH, List<int>[] sets, int N, int OW, int OH, double t, bool indexed)
{
int[] result = new int[OW * OH];
int?[] origins = new int?[OW * OH];
Random random = new Random();
for (int i = 0; i < result.Length; i++)
{
int x = i % OW, y = i / OW;
var candidates = new Dictionary<int, double>();
bool[,] mask = new bool[SW, SH];
for (int dy = -1; dy <= 1; dy++) for (int dx = -1; dx <= 1; dx++)
{
int sx = (x + dx + OW) % OW, sy = (y + dy + OH) % OH;
int? origin = origins[sy * OW + sx];
if ((dx != 0 || dy != 0) && origin != null)
{
foreach (int p in sets[(int)origin])
{
int ox = (p % SW - dx + SW) % SW, oy = (p / SW - dy + SH) % SH;
double s = Similarity(oy * SW + ox, sample, SW, SH, i, result, OW, OH, N, origins, indexed);
if (!mask[ox, oy]) candidates.Add(ox + oy * SW, Math.Pow(1E+2, s / t));
mask[ox, oy] = true;
}
}
}
int shifted = candidates.Any() ? candidates.Random(random.NextDouble()) : random.Next(SW) + random.Next(SH) * SW;
origins[i] = shifted;
result[i] = sample[shifted];
}
return result;
}
static int[] FullSynthesis(int[] sample, int SW, int SH, int N, int OW, int OH, double t, bool indexed)
{
int[] result = new int[OW * OH];
int?[] origins = new int?[OW * OH];
Random random = new Random();
if (!indexed) for (int y = 0; y < OH; y++) for (int x = 0; x < OW; x++) if (y + N >= OH)
{
result[x + y * OW] = sample[random.Next(SW * SH)];
origins[x + y * OW] = -1;
}
for (int i = 0; i < result.Length; i++)
{
double[] candidates = new double[SW * SH];
double max = -1E+4;
int argmax = -1;
for (int j = 0; j < SW * SH; j++)
{
double s = Similarity(j, sample, SW, SH, i, result, OW, OH, N, origins, indexed);
if (s > max)
{
max = s;
argmax = j;
}
if (indexed) candidates[j] = Math.Pow(1E+2, s / t);
}
if (indexed) argmax = candidates.Random(random.NextDouble());
result[i] = sample[argmax];
origins[i] = -1;
}
return result;
}
static double Similarity(int i1, int[] b1, int w1, int h1, int i2, int[] b2, int w2, int h2, int N, int?[] origins, bool indexed)
{
double sum = 0;
int x1 = i1 % w1, y1 = i1 / w1, x2 = i2 % w2, y2 = i2 / w2;
for (int dy = -N; dy <= 0; dy++) for (int dx = -N; (dy < 0 && dx <= N) || (dy == 0 && dx < 0); dx++)
{
int sx1 = (x1 + dx + w1) % w1, sy1 = (y1 + dy + h1) % h1;
int sx2 = (x2 + dx + w2) % w2, sy2 = (y2 + dy + h2) % h2;
int c1 = b1[sx1 + sy1 * w1];
int c2 = b2[sx2 + sy2 * w2];
if (origins == null || origins[sy2 * w2 + sx2] != null)
{
if (indexed) sum += c1 == c2 ? 1 : -1;
else
{
Color C1 = Color.FromArgb(c1), C2 = Color.FromArgb(c2);
sum -= (double)((C1.R - C2.R) * (C1.R - C2.R) + (C1.G - C2.G) * (C1.G - C2.G) + (C1.B - C2.B) * (C1.B - C2.B)) / 65536.0;
}
}
}
return sum;
}
static int[] ReSynthesis(int[] sample, int SW, int SH, int N, int M, int polish, bool indexed, int OW, int OH)
{
List<int> colors = new List<int>();
int[] indexedSample = new int[sample.Length];
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 (!indexed && 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[OW * OH];
for (int i = 0; i < origins.Length; i++) origins[i] = -1;
Random random = new Random();
int[] shuffle = new int[OW * OH];
for (int i = 0; i < shuffle.Length; i++)
{
int j = random.Next(i + 1);
if (j != i) shuffle[i] = shuffle[j];
shuffle[j] = i;
}
for (int round = 0; round <= polish; round++) for (int counter = 0; counter < shuffle.Length; counter++)
{
int f = shuffle[counter];
int fx = f % OW, fy = f / OW;
int neighborsNumber = round > 0 ? 8 : Math.Min(8, counter);
int neighborsFound = 0;
int[] candidates = new int[neighborsNumber + 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 * OW) % OW;
y[d] = (y[d] + 10 * OH) % OH;
if (neighborsFound >= neighborsNumber) continue;
int point = x[d] + y[d] * OW;
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]) % OW + 100 * SW) % SW;
int cy = (origins[neighbors[n]] / SW + f / OW - neighbors[n] / OW + 100 * SH) % SH;
candidates[n] = cx + cy * SW;
}
}
for (int m = 0; m < 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 % OW, jy = f / OW;
int SX, SY, FX, FY, S, F;
int origin;
for (int dy = -N; dy <= N; dy++) for (int dx = -N; dx <= N; 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 += OW;
else if (FX >= OW) FX -= OW;
FY = jy + dy;
if (FY < 0) FY += OH;
else if (FY >= OH) FY -= OH;
S = SX + SY * SW;
F = FX + FY * OW;
origin = origins[F];
if (origin != -1)
{
if (indexed) sum += sample[origin] == sample[S] ? 1 : -1;
else 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;
}
int[] result = new int[OW * OH];
for (int i = 0; i < result.Length; i++) result[i] = sample[origins[i]];
return result;
}
}
static class Stuff
{
public static T Get<T>(this XElement xelem, string attribute, T defaultT = default(T))
{
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)
{
double sum = array.Sum();
if (sum <= 0)
{
for (int j = 0; j < array.Length; j++) array[j] = 1;
sum = array.Sum();
}
for (int j = 0; j < array.Length; j++) array[j] /= sum;
int i = 0;
double x = 0;
while (i < array.Length)
{
x += array[i];
if (r <= x) return i;
i++;
}
return 0;
}
public static int Random(this Dictionary<int, double> dic, double r) => dic.Keys.ToArray()[dic.Values.ToArray().Random(r)];
}