Beispiel #1
0
        public void AddColor(Color c, int counter)
        {
            var R   = (c.R & (1 << (7 - position))) != 0;
            var G   = (c.G & (1 << (7 - position))) != 0;
            var B   = (c.B & (1 << (7 - position))) != 0;
            int RGB = (R ? 4 : 0) + (G ? 2 : 0) + (B ? 0 : 1);

            if (isLeaf)
            {
                count += counter;
                return;
            }
            Octree o = children[RGB];

            if (o == null)
            {
                if (position == 7)
                {
                    children[RGB] = new Octree(position + 1, c, true, counter);
                }
                else
                {
                    children[RGB] = new Octree(position + 1, null, false, 0);
                    children[RGB].AddColor(c, counter);
                }
                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);
            }
            else
            {
                o.AddColor(c, counter);
                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);
            }
        }
Beispiel #2
0
        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;
        }