-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColormap.cs
141 lines (123 loc) · 4.23 KB
/
Colormap.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
// Author: Tomáš Karella
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace _051colormap
{
class Colormap
{
struct Vector3
{
public float X;
public float Y;
public float Z;
public static double DistanceSquared(Vector3 a, Vector3 b)
{
return (Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y,2) + Math.Pow(a.Z - b.Z,2));
}
public static double Distance(Vector3 a, Vector3 b)
{
return Math.Sqrt(DistanceSquared(a,b));
}
}
class KeyValueComparer : Comparer<KeyValuePair<Color,long>>
{
public override int Compare(KeyValuePair<Color, long> x, KeyValuePair<Color, long> y)
{
if (x.Value == y.Value) return 0;
else if (x.Value > y.Value) return -1;
else return 1;
}
}
class PairComparer:IComparer<Pair>
{
public int Compare(Pair x, Pair y)
{
if (x.value == y.value) return 0;
else if (x.value > y.value) return -1;
else return 1;
}
}
class Pair
{
public Color color;
public long value;
public Pair(Color c, long v)
{
color = c;
value = v;
}
}
/// <summary>
/// Generate a colormap based on input image.
/// </summary>
/// <param name="input">Input raster image.</param>
/// <param name="numCol">Required colormap size (ignore it if you must)</param>
/// <param name="colors">Output palette (array of colors).</param>
/// <param name="colors">Output palette (array of colors).f</param>
public static void Generate(Bitmap input, int numCol, out Color[] colors)
{
int width = input.Width;
int height = input.Height;
colors = new Color[numCol];
Dictionary<Color, long> dic = new Dictionary<Color, long>();
//hard loop every color,
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
var c = input.GetPixel(j, i);
var cl = Color.FromArgb((c.R/16)*16, (c.G/16)*16, (c.B/16)*16);
if (dic.ContainsKey(cl))
checked {dic[cl]++;}
else dic.Add(cl, 1);
}
}
var l = dic.ToList();
l.Sort(new KeyValueComparer());
var myList = new List<Pair>(l.Count);
foreach (var item in l)
{
myList.Add(new Pair(item.Key,item.Value));
}
int index = 0;
while (index < myList.Count)
{
var selectItem = myList[index];
Vector3 v = new Vector3() {X = selectItem.color.R, Y = selectItem.color.G, Z = selectItem.color.B};
var nList = new List<Pair>();
for (int i = 0; i <= index;i++)
{
nList.Add(myList[i]);
}
for (int i = index+1; i < myList.Count; i++)
{
Vector3 c = new Vector3() {X = myList[i].color.R, Y = myList[i].color.G, Z = myList[i].color.B};
if (Vector3.DistanceSquared(v, c) < 5000)
{
checked
{
nList[index].value += myList[i].value;
}
}
else nList.Add(new Pair(myList[i].color,myList[i].value));
}
index++;
myList = nList;
}
myList.Sort(new PairComparer());
for (int i = 0; i < numCol; i++)
{
if (myList.Count <= i)
{
colors[i] = Color.Black;
}
else
{
colors[i] = myList[i].color;
}
}
}
}
}