public bool Reduce() { if (height == 0) { return(false); } if (height > 1) { int max = children.Select(x => x).Where(x => x != null).Max(x => x.height); int maxC = children.Select(x => x).Where(x => x != null && x.height == max).Max(x => x.count); Octree o = children.First(x => x != null && x.height == max && x.count == maxC); o.Reduce(); leafCount = children.Select(x => x).Where(x => x != null).Sum(z => z.leafCount); height = children.Select(x => x).Where(x => x != null).Max(z => z.height) + 1; count = children.Select(x => x).Where(x => x != null).Sum(z => z.count); return(true); } (Color? c, int count)[] colors = children.Select(x => x).Where(x => x != null).Select(x => (x.color, x.count)).ToArray();
public void Main(Version v, Bitmap source, Bitmap map, int k) { Octree oct = new Octree(0, null, false, 0); Dictionary <Color, int> colors = new Dictionary <Color, int>(); for (int i = 0; i < source.Width; i++) { for (int j = 0; j < source.Height; j++) { Color c = source.GetPixel(i, j); if (!colors.ContainsKey(c)) { colors.Add(c, 1); } else { colors[c]++; } } } if (v == Version.v1) { foreach (var elem in colors.Keys) { oct.AddColor(elem, colors[elem]); } while (oct.leafCount > k) { oct.Reduce(); } } else if (v == Version.v2) { foreach (var elem in colors.Keys) { oct.AddColor(elem, colors[elem]); while (oct.leafCount > k) { oct.Reduce(); } //if (oct.leafCount > k) // oct.Reduce(); } } else { var col = colors.ToList(); col.Sort((c1, c2) => c2.Value.CompareTo(c1.Value)); for (int i = 0; i < k && i < col.Count; i++) { oct.AddColor(col[i].Key, col[i].Value); } } for (int i = 0; i < source.Width; i++) { for (int j = 0; j < source.Height; j++) { map.SetPixel(i, j, oct.Find(source.GetPixel(i, j))); } } return; }