Beispiel #1
0
        public HashSet<Location> GetByColor(SxzColor color)
        {
            HashSet<Location> result = new HashSet<Location>();
            int width = locations.GetLength(0);
            int height = locations.GetLength(1);

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    Location location = locations[j, i];
                    if (location == null)
                    {
                        continue;
                    }

                    if (!location.Marked && location.Color.Equals(color))
                    {
                        result.Add(location);
                    }
                }
            }

            return result;
        }
Beispiel #2
0
        public override void Add(Location location)
        {
            if (Color == null)
            {
                Color = location.Color;
            }

            base.Add(location);
        }
Beispiel #3
0
        public static double GetColorDistance(SxzColor source, SxzColor target)
        {
            if (source.Equals(target)) {
                return 0.0d;
            }

            double red = source.Red - target.Red;
            double green = source.Green - target.Green;
            double blue = source.Blue - target.Blue;
            return Math.Sqrt(red * red + blue * blue + green * green);
        }
Beispiel #4
0
        public void Add(SxzColor color)
        {
            if (Colors.Contains(color))
            {
                throw new Exception("Duplicate color in palette");
            }

            if (Colors.Count >= 256)
            {
                throw new Exception("Palette too large");
            }

            Colors.Add(color);
        }
Beispiel #5
0
        //For background chunk
        public static void AddChunk(List<PaletteContainer> paletteContainers, ChunkContainer chunkContainer, SxzColor color)
        {
            foreach (PaletteContainer paletteContainer in paletteContainers)
            {
                if (paletteContainer.Contains(color))
                {
                    paletteContainer.Add(chunkContainer);
                    return;
                }
            }

            foreach (PaletteContainer paletteContainer in paletteContainers)
            {
                if (paletteContainer.HasRoom(color))
                {
                    paletteContainer.Add(chunkContainer);
                    return;
                }
            }
        }
Beispiel #6
0
        private double FindNearestDistance(SxzColor target)
        {
            double result = double.MaxValue;
            foreach (SxzColor color in Colors)
            {
                if (target.Equals(color))
                {
                    continue;
                }

                double distance = SxzColor.GetColorDistance(target, color);
                if (distance < result)
                {
                    result = distance;
                }
            }

            return result;
        }
Beispiel #7
0
        public static bool IsInPalette(List<PaletteContainer> paletteContainers, SxzColor color)
        {
            foreach (PaletteContainer paletteContainer in paletteContainers)
            {
                if (paletteContainer.Contains(color))
                {
                    return true;
                }
            }

            foreach (PaletteContainer paletteContainer in paletteContainers)
            {
                if (paletteContainer.HasRoom(color))
                {
                    return true;
                }
            }

            return false;
        }
Beispiel #8
0
        public Container Parse(string filename, SxzColor backgroundColor, bool useMostCommonColor, bool parseBackground, BackgroundType backgroundType, double bitsPerPixel, double maximumDistance)
        {
            int width = 0;
            int height = 0;

            //now we can act on our pixel byte array to get pixels out
            //and also histogram because it is informative if nothing else
            LocationPool locationPool = null;

            Dictionary<SxzColor, int> histogram = new Dictionary<SxzColor, int>();

            TransparentRegion transparentRegion = new TransparentRegion();

            //Parse all the pixels into the locationpool, histogram and remove the fully transparent pixels into the transparentregion
            using (System.Drawing.Bitmap bitMap = new System.Drawing.Bitmap(filename))
            {
                width = bitMap.Width;
                height = bitMap.Height;
                locationPool = new LocationPool(width, height);
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        System.Drawing.Color pixel = bitMap.GetPixel(x, y);

                        Location location = new Location();
                        location.Color = new SxzColor(pixel.R, pixel.G, pixel.B);
                        location.Point.X = x;
                        location.Point.Y = y;

                        //Don't put transparent colors into the histogram
                        if (pixel.A == 0)
                        {
                            transparentRegion.Add(location);
                            locationPool.SetLocation(location, x, y);
                            locationPool.SetMarked(location);
                            continue;
                        }

                        if (histogram.ContainsKey(location.Color))
                        {
                            int count = histogram[location.Color];
                            histogram[location.Color] = count + 1;
                        }
                        else
                        {
                            histogram.Add(location.Color, 1);
                        }

                        locationPool.SetLocation(location, x, y);
                    }
                }
            }

            Console.WriteLine("Total pixel count " + (width * height));
            Console.WriteLine("Total transparent pixel count " + transparentRegion.Locations.Count);

            //Remove all the transparent locations
            //foreach (Location location in transparentRegion.Locations)
            //{
            //    locationPool.SetMarked(location);
            //}

            //set the neighbors after removing transparent pixels to save time since those aren't needed anymore
            locationPool.SetNeighbors();

            Console.WriteLine("Done removing transparent pixels");
            //Sort the colors by frequency
            List<KeyValuePair<SxzColor, int>> colorList = histogram.ToList();
            colorList.Sort((first, second) =>
            {
                return first.Value == second.Value ? (int)(SxzColor.GetColorDistance(second.Key, new SxzColor(0, 0, 0)) - SxzColor.GetColorDistance(first.Key, new SxzColor(0, 0, 0)))
                        : second.Value.CompareTo(first.Value);
            });

            //Find the most commonly used color
            SxzColor mostCommonColor = colorList[0].Key;
            if (useMostCommonColor)
            {
                backgroundColor = mostCommonColor;
            }

            //always start with a palette, register empty palette and fill as we go

            Console.WriteLine("Processing the most common color");

            DefaultPaletteChunk defaultPaletteChunk = new DefaultPaletteChunk();
            Palette defaultPalette = new Palette(defaultPaletteChunk);

            //Initialization overhead
            List<PaletteContainer> paletteContainers = new List<PaletteContainer>();

            PaletteContainer initialPaletteContainer = new PaletteContainer();
            initialPaletteContainer.Initial = true;
            initialPaletteContainer.Palette = defaultPalette;
            paletteContainers.Add(initialPaletteContainer);

            HashSet<Location> locations = locationPool.GetByColor(backgroundColor);
            if (parseBackground && locations.Count > 0)
            {

                if (backgroundType == BackgroundType.Background)
                {
                    Console.WriteLine("Creating background chunk");
                    BackgroundChunk backgroundChunk = new BackgroundChunk();
                    BackgroundChunkContainer backgroundChunkContainer = new BackgroundChunkContainer();

                    backgroundChunkContainer.Chunk = backgroundChunk;
                    backgroundChunkContainer.Color = backgroundColor;

                    //pull out all the pixels with the most common color and throw them into a backgroundchunk
                    if (!initialPaletteContainer.Contains(backgroundColor))
                    {
                        Console.WriteLine("Background chunk in it's own palette");
                        //not in default palette so create a new palette
                        Palette palette = new Palette();
                        palette.Add(backgroundColor);
                        PaletteContainer paletteContainer = new PaletteContainer();
                        paletteContainer.Palette = palette;
                        paletteContainers.Add(paletteContainer);
                        paletteContainer.Add(backgroundChunkContainer);
                        backgroundChunk.Palette = palette.GetPaletteChunk();
                    }
                    else
                    {
                        backgroundChunk.Palette = defaultPaletteChunk;
                        initialPaletteContainer.Add(backgroundChunkContainer);
                    }

                    //AddChunk(paletteContainers, backgroundContainer, mostCommonColor);
                    backgroundChunkContainer.Locations = locations;
                }
                else if (backgroundType == BackgroundType.Rectangle)
                {
                    MonoRectangleChunk rectangleChunk = new MonoRectangleChunk();
                    rectangleChunk.Origin = new SxzPoint(0, 0);
                    rectangleChunk.Width = width;
                    rectangleChunk.Height = height;
                    MonoRectangleChunkContainer rectangleContainer = new MonoRectangleChunkContainer();
                    rectangleContainer.Color = backgroundColor;
                    rectangleContainer.Chunk = rectangleChunk;

                    if (!initialPaletteContainer.Contains(backgroundColor))
                    {
                        Console.WriteLine("Background chunk in it's own palette");
                        //not in default palette so create a new palette
                        Palette palette = new Palette();
                        palette.Add(backgroundColor);
                        PaletteContainer paletteContainer = new PaletteContainer();
                        paletteContainer.Palette = palette;
                        paletteContainers.Add(paletteContainer);
                        paletteContainer.Add(rectangleContainer);
                        rectangleChunk.Palette = palette.GetPaletteChunk();
                    }
                    else
                    {
                        rectangleChunk.Palette = defaultPaletteChunk;
                        initialPaletteContainer.Add(rectangleContainer);
                    }

                    //AddChunk(paletteContainers, backgroundContainer, mostCommonColor);
                    rectangleContainer.Locations = locations;
                }

                //Remove the pixels with the background color from the location pool
                foreach (Location location in locations)
                {
                    locationPool.SetMarked(location);
                }
            }

            Console.WriteLine("Done processing the most common color");

            Console.WriteLine("Parsing monocolor regions");
            //Pull out all regions with the same color in mono regions that were not pulled by the most common color if any
            HashSet<Region> monoRegions = Process(locationPool, maximumDistance, RegionType.MonoRegion);
            Console.WriteLine("Have mono region count " + monoRegions.Count);
            foreach (Region region in monoRegions)
            {
                HashSet<ChunkContainer> potentialContainers = region.GetChunks(locationPool);
                PaletteContainer paletteContainer = GetPaletteContainer(paletteContainers, region.GetColors());

                foreach (ChunkContainer chunkContainer in potentialContainers)
                {
                    //filter out chunks that are not storage sufficient, putting the unused locations
                    //back into the location pool
                    //...
                    if (!chunkContainer.IsValid(bitsPerPixel))
                    {
                        PrintTossedChunk(chunkContainer);
                        foreach (Location location in chunkContainer.Locations)
                        {
                            locationPool.Unmark(location);
                        }

                        continue;
                    }

                    if (paletteContainer == null)
                    {
                        paletteContainer = new PaletteContainer();
                        paletteContainer.Palette = new Palette();
                        paletteContainers.Add(paletteContainer);
                    }

                    paletteContainer.Add(chunkContainer);
                    paletteContainer.AddColors(region.GetColors());
                }
            }

            Console.WriteLine("Done processing monocolor regions");

            Console.WriteLine("Parsing multicolor regions");
            HashSet<Region> colorRegions = Process(locationPool, maximumDistance, RegionType.MultiColorRegion);
            Console.WriteLine("Have multi color region count " + colorRegions.Count);
            foreach (Region region in colorRegions)
            {
                HashSet<ChunkContainer> potentialContainers = region.GetChunks(locationPool);
                PaletteContainer paletteContainer = GetPaletteContainer(paletteContainers, region.GetColors());
                if (paletteContainer == null)
                {
                    paletteContainer = new PaletteContainer();
                    paletteContainer.Palette = new Palette();
                    paletteContainers.Add(paletteContainer);
                }

                foreach (ChunkContainer chunkContainer in potentialContainers)
                {
                    paletteContainer.Add(chunkContainer);
                    paletteContainer.AddColors(region.GetColors());
                }
            }

            Console.WriteLine("Done processing multicolor regions");
            Console.WriteLine("Histogram count " + colorList.Count);
            Console.WriteLine("Majority color " + mostCommonColor + " with count " + colorList[0].Value);
            //alrighty, let's get some output going
            Container container = new Container();
            Frame frame = new Frame();
            container.Frames.Add(frame);

            foreach (PaletteContainer paletteContainer in paletteContainers)
            {
                PaletteChunk paletteChunk = paletteContainer.Palette.GetPaletteChunk();
                if (paletteChunk == null)
                {
                    paletteChunk = defaultPaletteChunk;
                }

                if (!paletteContainer.Initial)
                {
                    paletteChunk.Colors.Sort(new PaletteChunk.ColorSorter());
                    frame.Chunks.Add(paletteChunk);
                }

                foreach (ChunkContainer chunkContainer in paletteContainer.ChunkContainers)
                {
                    int count = chunkContainer.Chunk.GetData().Count;
                    PrintKeptChunk(chunkContainer);
                    chunkContainer.SetIndex(paletteChunk);
                    chunkContainer.Chunk.Palette = paletteChunk;

                    frame.Chunks.Add(chunkContainer.Chunk);
                }
            }

            return container;
        }
Beispiel #9
0
 public bool HasRoom(SxzColor color)
 {
     return Palette.HasRoom(color);
 }
Beispiel #10
0
        public void SetColor(SxzColor color, int x, int y)
        {
            if (color.Red != color.Green || color.Red != color.Blue || color.Green != color.Blue)
            {
                throw new Exception("This is not a gray scale color " + color);
            }

            BitPlane.DrawLocation(x - Origin.X, y - Origin.Y);
            SetByteIndex(color.Red, x - Origin.X, y - Origin.Y);
        }
Beispiel #11
0
        public void SetMarked(SxzColor color)
        {
            int width = locations.GetLength(0);
            int height = locations.GetLength(1);

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    Location location = locations[j, i];
                    if (location == null)
                    {
                        continue;
                    }

                    if (location.Color.Equals(color))
                    {
                        location.Marked = true;
                        Unmarked.Remove(location);
                    }
                }
            }
        }
Beispiel #12
0
 public static Color GetColor(SxzColor color)
 {
     return Color.FromArgb(color.Red, color.Green, color.Blue);
 }
 public void SetColor(SxzColor color, int x, int y)
 {
     if (color.Equals(Black))
     {
         BitPlane.UnDrawLocation(x - Origin.X, y - Origin.Y);
     }
     else
     {
         BitPlane.DrawLocation(x - Origin.X, y - Origin.Y);
     }
 }
Beispiel #14
0
 public void SetColor(SxzColor color, int x, int y)
 {
     SetByteIndex((byte)Palette.Colors.IndexOf(color), x - Origin.X, y - Origin.Y);
 }
Beispiel #15
0
 public bool Contains(SxzColor color)
 {
     return Palette.Contains(color);
 }
Beispiel #16
0
        public bool HasRoom(SxzColor color)
        {
            if (Default)
            {
                return false;
            }

            double nearestDistance = double.MaxValue;
            foreach (SxzColor alreadyMember in Colors)
            {
                double distance = SxzColor.GetColorDistance(color, alreadyMember);
                if (distance < nearestDistance)
                {
                    nearestDistance = distance;
                }
            }

            //TODO: Make the distance configurable
            if (nearestDistance > Distance)
            {
                return false;
            }

            return Colors.Count < 256;
        }
Beispiel #17
0
 public bool Contains(SxzColor color)
 {
     return Colors.Contains(color);
 }
Beispiel #18
0
 public void SetColor(SxzColor color, int x, int y)
 {
     BitPlane.DrawLocation(x - Origin.X, y - Origin.Y);
     SetByteIndex((byte)Palette.Colors.IndexOf(color), x - Origin.X, y - Origin.Y);
 }
Beispiel #19
0
 public static void AddBytes(List<byte> list, SxzColor color)
 {
     list.Add(color.Red);
     list.Add(color.Green);
     list.Add(color.Blue);
 }