/// <summary>
        /// See <see cref="BaseColorCache.OnGetColorPaletteIndex"/> for more details.
        /// </summary>
        protected override void OnGetColorPaletteIndex(TextureBitmap.Color color, out Int32 paletteIndex)
        {
            BucketInfo bucket     = GetBucket(color);
            Int32      colorCount = bucket.Colors.Count();

            paletteIndex = 0;

            if (colorCount == 1)
            {
                paletteIndex = bucket.Colors.First().Key;
            }
            else
            {
                Int32 index      = 0;
                Int32 colorIndex = ColorModelHelper.GetEuclideanDistance(color, ColorModel, bucket.Colors.Values.ToList());

                foreach (Int32 colorPaletteIndex in bucket.Colors.Keys)
                {
                    if (index == colorIndex)
                    {
                        paletteIndex = colorPaletteIndex;
                        break;
                    }

                    index++;
                }
            }
        }
        private BucketInfo GetBucket(TextureBitmap.Color color)
        {
            Int64 bucketIndex = GetColorBucketIndex(color);

            if (bucketIndex < minBucketIndex)
            {
                bucketIndex = minBucketIndex;
            }
            else if (bucketIndex > maxBucketIndex)
            {
                bucketIndex = maxBucketIndex;
            }
            else if (buckets[bucketIndex] == null)
            {
                Boolean bottomFound       = false;
                Boolean topFound          = false;
                Int64   bottomBucketIndex = bucketIndex;
                Int64   topBucketIndex    = bucketIndex;

                while (!bottomFound && !topFound)
                {
                    bottomBucketIndex--;
                    topBucketIndex++;
                    bottomFound = bottomBucketIndex > 0 && buckets[bottomBucketIndex] != null;
                    topFound    = topBucketIndex < quality && buckets[topBucketIndex] != null;
                }

                bucketIndex = bottomFound ? bottomBucketIndex : topBucketIndex;
            }

            return(buckets[bucketIndex]);
        }
Example #3
0
        public static Int32 GetEuclideanDistance(TextureBitmap.Color color, ColorModel colorModel, IList <TextureBitmap.Color> palette)
        {
            // initializes the best difference, set it for worst possible, it can only get better
            Int64 leastDistance = Int64.MaxValue;
            Int32 result        = 0;

            for (Int32 index = 0; index < palette.Count; index++)
            {
                TextureBitmap.Color targetColor = palette[index];
                Int64 distance = GetColorEuclideanDistance(colorModel, color, targetColor);

                // if a difference is zero, we're good because it won't get better
                if (distance == 0)
                {
                    result = index;
                    break;
                }

                // if a difference is the best so far, stores it as our best candidate
                if (distance < leastDistance)
                {
                    leastDistance = distance;
                    result        = index;
                }
            }

            return(result);
        }
Example #4
0
        public static void GetColorComponents(ColorModel colorModel, TextureBitmap.Color color, out Single componentA, out Single componentB, out Single componentC)
        {
            componentA = 0.0f;
            componentB = 0.0f;
            componentC = 0.0f;

            switch (colorModel)
            {
            case ColorModel.RedGreenBlue:
                componentA = color.R;
                componentB = color.G;
                componentC = color.B;
                break;

            case ColorModel.HueSaturationBrightness:
                componentA = color.GetHue();
                componentB = color.GetSaturation();
                componentC = color.GetBrightness();
                break;

            case ColorModel.LabColorSpace:
                RGBtoLab(color.R, color.G, color.B, out componentA, out componentB, out componentC);
                break;

            case ColorModel.XYZ:
                RGBtoXYZ(color.R, color.G, color.B, out componentA, out componentB, out componentC);
                break;
            }
        }
        /// <summary>
        /// Adds the color.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <param name="level">The level.</param>
        /// <param name="parent">The parent.</param>
        public void AddColor(TextureBitmap.Color color, Int32 level, OctreeQuantizer parent)
        {
            // if this node is a leaf, then increase a color amount, and pixel presence
            if (level == 8)
            {
                red   += color.R;
                green += color.G;
                blue  += color.B;
                pixelCount++;
            }
            else if (level < 8) // otherwise goes one level deeper
            {
                // calculates an index for the next sub-branch
                Int32 index = GetColorIndexAtLevel(color, level);

                // if that branch doesn't exist, grows it
                if (nodes[index] == null)
                {
                    nodes[index] = new OctreeNode(level, parent);
                }

                // adds a color to that branch
                nodes[index].AddColor(color, level + 1, parent);
            }
        }
Example #6
0
        public static Int64 GetColorEuclideanDistance(ColorModel colorModel, TextureBitmap.Color requestedColor, TextureBitmap.Color realColor)
        {
            Single componentA, componentB, componentC;

            GetColorComponents(colorModel, requestedColor, realColor, out componentA, out componentB, out componentC);
            return((Int64)(componentA * componentA + componentB * componentB + componentC * componentC));
        }
Example #7
0
        void SpawnFood()
        {
            List <TextureBitmap.Point> freeSpots = new List <TextureBitmap.Point>();

            for (int x = 0; x < Width; x++)
            {
                for (int y = 0; y < Height; y++)
                {
                    bool free = true;
                    for (int i = 0; i < Points.Count; i++)
                    {
                        if (x == Points[i].X && y == Points[i].Y)
                        {
                            free = false;
                            break;
                        }
                    }
                    if (free)
                    {
                        freeSpots.Add(new TextureBitmap.Point(x, y));
                    }
                }
            }

            CurrentFoodColor = new TextureBitmap.Color((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
            FoodPosition     = freeSpots[Random.Range(0, freeSpots.Count)];
        }
    public static TextureBitmap GetBitmap(this DesignPattern pattern)
    {
        int width  = pattern.Width;
        int height = pattern.Height;

        var bitmap = new TextureBitmap(width, height);

        unsafe
        {
            TextureBitmap.Color *ptr = bitmap.GetColors();

            for (var y = 0; y < height; y++)
            {
                for (var x = 0; x < width; x++)
                {
                    var b = pattern.GetPixel(x, y);
                    if (b == 15)
                    {
                        *(ptr + x + (height - 1 - y) * width) = new TextureBitmap.Color(0, 0, 0, 0);
                    }
                    else
                    {
                        var c = pattern.Palette[b];
                        *(ptr + x + (height - 1 - y) * width) = new TextureBitmap.Color(c.R, c.G, c.B, 255);
                    }
                }
            }
        }
        return(bitmap);
    }
Example #9
0
        /// <summary>
        /// See <see cref="IColorQuantizer.AddColor"/> for more details.
        /// </summary>
        public void AddColor(TextureBitmap.Color color, Int32 x, Int32 y)
        {
            Int32 key;

            color = QuantizationHelper.ConvertAlpha(color, out key);
            OnAddColor(color, key, x, y);
        }
Example #10
0
    public void Clear()
    {
        var p = new SimpleDesignPattern();

        p.Type = this.Type;
        //p.IsPro = this.Type != DesignPattern.TypeEnum.SimplePattern;
        p.Image = new byte[p.Width / 2 * p.Height];
        p.Empty();
        var colors = p.GetPixels();

        unsafe
        {
            var bitmapColors = Bitmap.GetColors();
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    var col = new TextureBitmap.Color(
                        (byte)(colors[x + y * Width].r * 255f),
                        (byte)(colors[x + y * Width].g * 255f),
                        (byte)(colors[x + y * Width].b * 255f),
                        (byte)(colors[x + y * Width].a * 255f)
                        );
                    *(bitmapColors + x + y * Width) = col;
                }
            }
        }
        Load();
    }
Example #11
0
        /// <summary>
        /// See <see cref="BaseColorQuantizer.OnAddColor"/> for more details.
        /// </summary>
        protected override void OnAddColor(TextureBitmap.Color color, Int32 key, Int32 x, Int32 y)
        {
            base.OnAddColor(color, key, x, y);
            Int32 index = GetColorIndex(color);

            colorMap.AddOrUpdate(index, colorKey => new PopularityColorSlot(color), (colorKey, slot) => slot.AddValue(color));
        }
Example #12
0
 /// <summary>
 /// Adds the value to the slot.
 /// </summary>
 /// <param name="color">The color to be added.</param>
 public PopularityColorSlot AddValue(TextureBitmap.Color color)
 {
     red   += color.R;
     green += color.G;
     blue  += color.B;
     PixelCount++;
     return(this);
 }
Example #13
0
        /// <summary>
        /// See <see cref="IColorQuantizer.GetPaletteIndex"/> for more details.
        /// </summary>
        public Int32 GetPaletteIndex(TextureBitmap.Color color, Int32 x, Int32 y)
        {
            Int32 result, key;

            color = QuantizationHelper.ConvertAlpha(color, out key);
            OnGetPaletteIndex(color, key, x, y, out result);
            return(result);
        }
Example #14
0
    public Pattern(PatternEditor editor, DesignPattern pattern)
    {
        try
        {
            Logger.Log(Logger.Level.INFO, "[PatternEditor/Pattern] Creating new pattern");
            StartPreviewThread();
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Preview generator thread started!");
            _Type = pattern.Type;
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Pattern type: " + _Type.ToString());
            Bitmap = new TextureBitmap(pattern.Width, pattern.Height);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created TextureBitmap " + pattern.Width + "x" + pattern.Height);
            Bitmap.Clear();
            PreviewBitmap = new TextureBitmap(pattern.Width, pattern.Height);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created preview TextureBitmap " + pattern.Width + "x" + pattern.Height);
            PreviewBitmap.Clear();
            PreviewSprite = UnityEngine.Sprite.Create(PreviewBitmap.Texture, new UnityEngine.Rect(0, 0, PreviewBitmap.Width, PreviewBitmap.Height), new UnityEngine.Vector2(0.5f, 0.5f));
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created preview sprite");

            UpscaledPreviewBitmap = new TextureBitmap(pattern.Width * 4, pattern.Height * 4);
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Created upscaled preview TextureBitmap " + (pattern.Width * 4) + "x" + (pattern.Height * 4));
            UpscaledPreviewBitmap.Clear();

            Quantizer = Quantizers[0];
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Selected Quantizer: " + Quantizer.GetType().ToString());
            ColorCache = ColorCaches[0];
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Selected Color Cache: " + ColorCache.GetType().ToString());

            Editor        = editor;
            DesignPattern = pattern;
            var colors = pattern.GetPixels();

            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Parsing colors of pattern...");
            unsafe
            {
                var bitmapColors = Bitmap.GetColors();
                for (int y = 0; y < Height; y++)
                {
                    for (int x = 0; x < Width; x++)
                    {
                        var col = new TextureBitmap.Color(
                            (byte)(colors[x + y * Width].r * 255f),
                            (byte)(colors[x + y * Width].g * 255f),
                            (byte)(colors[x + y * Width].b * 255f),
                            (byte)(colors[x + y * Width].a * 255f)
                            );
                        *(bitmapColors + x + (Height - 1 - y) * Width) = col;
                    }
                }
            }
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Parsed " + (Width * Height) + " pixels.");
            Info = DesignPatternInformation.Types[pattern.Type];
            Logger.Log(Logger.Level.DEBUG, "[PatternEditor/Pattern] Pattern information obtained.");
        }
        catch (System.Exception e)
        {
            Logger.Log(Logger.Level.ERROR, "[PatternEditor/Pattern] Error while creating pattern: " + e.ToString());
        }
    }
        /// <summary>
        /// Initializes a new instance of the <see cref="DistinctColorInfo"/> struct.
        /// </summary>
        public DistinctColorInfo(TextureBitmap.Color color)
        {
            Color = color.ToARGB();
            Count = 1;

            Hue        = Convert.ToInt32(color.GetHue() * Factor);
            Saturation = Convert.ToInt32(color.GetSaturation() * Factor);
            Brightness = Convert.ToInt32(color.GetBrightness() * Factor);
        }
Example #16
0
        private static Int32 GetColorIndex(TextureBitmap.Color color)
        {
            // determines the index by splitting the RGB cube to 4x4x4 (1 >> 2 = 4)
            Int32 redIndex   = color.R >> 2;
            Int32 greenIndex = color.G >> 2;
            Int32 blueIndex  = color.B >> 2;

            // calculates the whole unique index of the slot: Index = R*4096 + G*64 + B
            return((redIndex << 12) + (greenIndex << 6) + blueIndex);
        }
        /// <summary>
        /// Determines whether the color is in the space of this cube.
        /// </summary>
        /// <param name="color">The color to be checked, if it's contained in this cube.</param>
        /// <returns>if true a color is in the space of this cube, otherwise returns false.</returns>
        public Boolean IsColorIn(TextureBitmap.Color color)
        {
            Int32 red   = ColorModelHelper.GetComponentA(ColorModel, color);
            Int32 green = ColorModelHelper.GetComponentB(ColorModel, color);
            Int32 blue  = ColorModelHelper.GetComponentC(ColorModel, color);

            return((red >= redLowBound && red <= redHighBound) &&
                   (green >= greenLowBound && green <= greenHighBound) &&
                   (blue >= blueLowBound && blue <= blueHighBound));
        }
Example #18
0
        /// <summary>
        /// See <see cref="BaseColorQuantizer.OnGetPaletteIndex"/> for more details.
        /// </summary>
        protected override void OnGetPaletteIndex(TextureBitmap.Color color, Int32 key, Int32 x, Int32 y, out int paletteIndex)
        {
            base.OnGetPaletteIndex(color, key, x, y, out paletteIndex);

            // if not determined, use cache to determine the index
            if (paletteIndex == InvalidIndex)
            {
                GetColorCache().GetColorPaletteIndex(color, out paletteIndex);
            }
        }
Example #19
0
        /// <summary>
        /// See <see cref="BaseColorQuantizer.OnAddColor"/> for more details.
        /// </summary>
        protected override void OnAddColor(TextureBitmap.Color color, Int32 key, Int32 x, Int32 y)
        {
#if (UseDictionary)
            colorMap.AddOrUpdate(key,
                                 colorKey => new DistinctColorInfo(color),
                                 (colorKey, colorInfo) => colorInfo.IncreaseCount());
#else
            color = QuantizationHelper.ConvertAlpha(color);
            rootBucket.StoreColor(color);
#endif
        }
Example #20
0
        /// <summary>
        /// Called when get palette index for a given color should be returned.
        /// </summary>
        protected virtual void OnGetPaletteIndex(TextureBitmap.Color color, Int32 key, Int32 x, Int32 y, out Int32 paletteIndex)
        {
            // by default unknown index is returned
            paletteIndex = InvalidIndex;
            Int16 foundIndex;

            // if we previously found palette quickly (without quantization), use it
            if (paletteFound && UniqueColors.TryGetValue(key, out foundIndex))
            {
                paletteIndex = foundIndex;
            }
        }
    public static void FromBitmap(this DesignPattern pattern, TextureBitmap bitmap)
    {
        Dictionary <TextureBitmap.Color, byte> colorMap = new Dictionary <TextureBitmap.Color, byte>();

        for (int i = 0; i < 15; i++)
        {
            pattern.Palette[i].R = 0;
            pattern.Palette[i].G = 0;
            pattern.Palette[i].B = 0;
        }

        int width  = pattern.Width;
        int height = pattern.Height;

        unsafe
        {
            var colors = bitmap.GetColors();
            pattern.Image = new byte[width * height];
            int bitmapHeight = bitmap.Height;
            int bitmapWidth  = bitmap.Width;
            for (var y = 0; y < width; y++)
            {
                for (var x = 0; x < height; x++)
                {
                    var pixelColor = new TextureBitmap.Color(0, 0, 0, 0);
                    if (x < bitmapWidth && y < bitmapHeight)
                    {
                        pixelColor = colors[x + (bitmapHeight - 1 - y) * bitmapHeight];
                    }

                    byte index = 0xF;
                    if (pixelColor.A == 255)
                    {
                        if (colorMap.ContainsKey(pixelColor))
                        {
                            index = colorMap[pixelColor];
                        }
                        else
                        {
                            index = (byte)colorMap.Count;
                            pattern.Palette[index].R = pixelColor.R;
                            pattern.Palette[index].G = pixelColor.G;
                            pattern.Palette[index].B = pixelColor.B;
                            colorMap.Add(pixelColor, index);
                        }
                    }

                    pattern.SetPixel(x, y, index);
                }
            }
        }
    }
Example #22
0
        /// <summary>
        /// See <see cref="IColorCache.GetColorPaletteIndex"/> for more details.
        /// </summary>
        public void GetColorPaletteIndex(TextureBitmap.Color color, out Int32 paletteIndex)
        {
            Int32 key = color.R << 16 | color.G << 8 | color.B;

            paletteIndex = cache.AddOrUpdate(key,
                                             colorKey =>
            {
                Int32 paletteIndexInside;
                OnGetColorPaletteIndex(color, out paletteIndexInside);
                return(paletteIndexInside);
            },
                                             (colorKey, inputIndex) => inputIndex);
        }
        /// <summary>
        /// See <see cref="IColorQuantizer.GetPaletteIndex"/> for more details.
        /// </summary>
        public void GetPaletteIndex(TextureBitmap.Color color, out Int32 paletteIndex)
        {
            paletteIndex = 0;
            color        = QuantizationHelper.ConvertAlpha(color);

            foreach (MedianCutCube cube in cubeList)
            {
                if (cube.IsColorIn(color))
                {
                    paletteIndex = cube.PaletteIndex;
                    break;
                }
            }
        }
Example #24
0
        public static void GetColorComponents(ColorModel colorModel, TextureBitmap.Color color, TextureBitmap.Color targetColor, out Single componentA, out Single componentB, out Single componentC)
        {
            componentA = 0.0f;
            componentB = 0.0f;
            componentC = 0.0f;

            switch (colorModel)
            {
            case ColorModel.RedGreenBlue:
                componentA = color.R - targetColor.R;
                componentB = color.G - targetColor.G;
                componentC = color.B - targetColor.B;
                break;

            case ColorModel.HueSaturationBrightness:
                componentA = color.GetHue() - targetColor.GetHue();
                componentB = color.GetSaturation() - targetColor.GetSaturation();
                componentC = color.GetBrightness() - targetColor.GetBrightness();
                break;

            case ColorModel.LabColorSpace:

                Single sourceL, sourceA, sourceB;
                Single targetL, targetA, targetB;

                RGBtoLab(color.R, color.G, color.B, out sourceL, out sourceA, out sourceB);
                RGBtoLab(targetColor.R, targetColor.G, targetColor.B, out targetL, out targetA, out targetB);

                componentA = sourceL - targetL;
                componentB = sourceA - targetA;
                componentC = sourceB - targetB;

                break;

            case ColorModel.XYZ:

                Single sourceX, sourceY, sourceZ;
                Single targetX, targetY, targetZ;

                RGBtoXYZ(color.R, color.G, color.B, out sourceX, out sourceY, out sourceZ);
                RGBtoXYZ(targetColor.R, targetColor.G, targetColor.B, out targetX, out targetY, out targetZ);

                componentA = sourceX - targetX;
                componentB = sourceY - targetY;
                componentC = sourceZ - targetZ;

                break;
            }
        }
Example #25
0
        /// <summary>
        /// Gets the index of the palette.
        /// </summary>
        public Dictionary <Int32, TextureBitmap.Color> GetPaletteIndex(TextureBitmap.Color color, Int32 level)
        {
            Dictionary <Int32, TextureBitmap.Color> result = entries;

            if (level < 8)
            {
                Int32 index = GetColorIndexAtLevel(color, level);

                if (nodes[index] != null)
                {
                    result = nodes[index].GetPaletteIndex(color, level + 1);
                }
            }

            return(result);
        }
        /// <summary>
        /// See <see cref="BaseColorQuantizer.AddColor"/> for more details.
        /// </summary>
        protected override void OnAddColor(TextureBitmap.Color color, Int32 key, Int32 x, Int32 y)
        {
            Int32 indexRed   = (color.R >> 3) + 1;
            Int32 indexGreen = (color.G >> 3) + 1;
            Int32 indexBlue  = (color.B >> 3) + 1;

            int index = indexRed + indexGreen * SideSize + indexBlue * SideSize * SideSize;

            weights[index]++;
            momentsRed[index]   += color.R;
            momentsGreen[index] += color.G;
            momentsBlue[index]  += color.B;
            moments[index]      += table[color.R] + table[color.G] + table[color.B];

            quantizedPixels[pixelIndex] = (indexRed << 10) + (indexRed << 6) + indexRed + (indexGreen << 5) + indexGreen + indexBlue;
            pixels[pixelIndex]          = color.ToARGB();
            pixelIndex++;
        }
Example #27
0
        public Snake(TextureBitmap playground)
        {
            CurrentSnakeColor = new TextureBitmap.Color(0, 0, 0, 255);

            Original   = playground.Clone();
            Playground = playground;
            Width      = playground.Width;
            Height     = playground.Height;
            int startX = Width / 2;
            int startY = Height / 2;

            for (int i = 0; i < 3; i++)
            {
                Points.Add(new TextureBitmap.Point(startX, startY + 3 - i));
            }
            SpawnFood();
            CurrentDirection = Direction.UP;
            LastDirection    = Direction.UP;
        }
Example #28
0
        /// <summary>
        /// See <see cref="BaseColorCache.OnGetColorPaletteIndex"/> for more details.
        /// </summary>
        protected override void OnGetColorPaletteIndex(TextureBitmap.Color color, out Int32 paletteIndex)
        {
            Dictionary <Int32, TextureBitmap.Color> candidates = root.GetPaletteIndex(color, 0);

            paletteIndex = 0;
            Int32 index      = 0;
            Int32 colorIndex = ColorModelHelper.GetEuclideanDistance(color, ColorModel, candidates.Values.ToList());

            foreach (Int32 colorPaletteIndex in candidates.Keys)
            {
                if (index == colorIndex)
                {
                    paletteIndex = colorPaletteIndex;
                    break;
                }

                index++;
            }
        }
Example #29
0
        /// <summary>
        /// Adds the color.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <param name="paletteIndex">Index of the palette.</param>
        /// <param name="level">The level.</param>
        public void AddColor(TextureBitmap.Color color, Int32 paletteIndex, Int32 level)
        {
            // if this node is a leaf, then increase a color amount, and pixel presence
            entries.Add(paletteIndex, color);

            if (level < 8) // otherwise goes one level deeper
            {
                // calculates an index for the next sub-branch
                Int32 index = GetColorIndexAtLevel(color, level);

                // if that branch doesn't exist, grows it
                if (nodes[index] == null)
                {
                    nodes[index] = new OctreeCacheNode();
                }

                // adds a color to that branch
                nodes[index].AddColor(color, paletteIndex, level + 1);
            }
        }
        public void StoreColor(TextureBitmap.Color color)
        {
            Int32          redIndex  = color.R >> 5;
            DistinctBucket redBucket = Buckets[redIndex];

            if (redBucket == null)
            {
                redBucket         = new DistinctBucket();
                Buckets[redIndex] = redBucket;
            }

            Int32          greenIndex  = color.G >> 5;
            DistinctBucket greenBucket = redBucket.Buckets[greenIndex];

            if (greenBucket == null)
            {
                greenBucket = new DistinctBucket();
                redBucket.Buckets[greenIndex] = greenBucket;
            }

            Int32          blueIndex  = color.B >> 5;
            DistinctBucket blueBucket = greenBucket.Buckets[blueIndex];

            if (blueBucket == null)
            {
                blueBucket = new DistinctBucket();
                greenBucket.Buckets[blueIndex] = blueBucket;
            }

            DistinctColorInfo colorInfo = blueBucket.ColorInfo;

            if (colorInfo == null)
            {
                colorInfo            = new DistinctColorInfo(color);
                blueBucket.ColorInfo = colorInfo;
            }
            else
            {
                colorInfo.IncreaseCount();
            }
        }