Example #1
0
    public void UpdateColors()
    {
        int desiredWidth  = _ObjectWidth;
        int desiredHeight = _ObjectHeight;

        if (Crop != null)
        {
            Crop.SetImage(Bitmap, desiredWidth, desiredHeight);
            desiredWidth  = Crop.GetWidth();
            desiredHeight = Crop.GetHeight();
        }
        var resampled = Resampler.Resample(Bitmap, desiredWidth, desiredHeight);

        var colors = resampled.ToColors();

        for (int i = 0; i < Colors.Length; i++)
        {
            Colors[i] = new UnityEngine.Color(0f, 0f, 0f, 0f);
        }

        var startX = (int)(_ObjectX + (_ObjectWidth - desiredWidth) / 2f);
        var startY = (int)(_ObjectY + (_ObjectHeight - desiredHeight) / 2f);

        for (int y = _ObjectY; y < _ObjectY + _ObjectHeight; y++)
        {
            for (int x = _ObjectX; x < _ObjectX + _ObjectWidth; x++)
            {
                if (x >= startX && y >= startY && x < startX + desiredWidth && y < startY + desiredHeight)
                {
                    int px = x;
                    int py = y;
                    if (px >= 0 && px < _Width && py >= 0 && py < _Height)
                    {
                        Colors[px + py * _Width] = colors[(x - startX) + (y - startY) * desiredWidth];
                    }
                }
            }
        }

        UpdateTexture();
    }
Example #2
0
    public void ParsePattern(ICrop crop, ISampling sampling, IColorQuantizer quantizer, IColorCache colorCache)
    {
        if (!this.IsParsing)
        {
            IsParsing = true;
            Thread thread = new Thread(() =>
            {
                var bmp = new Bitmap((System.Drawing.Image)Image.Clone());

                int desiredWidth  = 32;
                int desiredHeight = 32;
                if (crop != null)
                {
                    crop.SetImage(bmp);
                    desiredWidth  = crop.GetWidth();
                    desiredHeight = crop.GetHeight();
                }

                if (quantizer is BaseColorCacheQuantizer colorCacheQuantizer)
                {
                    colorCacheQuantizer.ChangeCacheProvider(colorCache);
                }

                var sampledBmp = sampling.Resample(bmp, desiredWidth, desiredHeight);
                bmp.Dispose();
                bmp = sampledBmp;

                Bitmap croppedBmp = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(croppedBmp))
                {
                    graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    graphics.DrawImage(bmp, (32 - bmp.Width) / 2, (32 - bmp.Height) / 2, bmp.Width, bmp.Height);
                }

                bmp.Dispose();
                bmp = croppedBmp;

                var transparentPixels = new bool[bmp.Width * bmp.Height];
                for (var y = 0; y < bmp.Height; y++)
                {
                    for (var x = 0; x < bmp.Width; x++)
                    {
                        transparentPixels[x + y * bmp.Width] = bmp.GetPixel(x, y).A != 255;
                    }
                }
                var targetImage = ImageBuffer.QuantizeImage(bmp, quantizer, null, 15, 1);

                bmp.Dispose();
                bmp = new Bitmap(targetImage);
                for (var y = 0; y < bmp.Height; y++)
                {
                    for (var x = 0; x < bmp.Width; x++)
                    {
                        if (transparentPixels[x + y * bmp.Width])
                        {
                            bmp.SetPixel(x, y, System.Drawing.Color.FromArgb(0, 0, 0, 0));
                        }
                    }
                }
                Result    = bmp;
                IsReady   = true;
                IsParsing = false;
            });
            thread.Start();
        }
    }