private void FixupProvinces(Color col, UnsafeQueueLinearFloodFiller filler, LockBitmap bmp)
        {
            HashSet <Color> done = new HashSet <Color>();
            var             pts  = new List <Point>(filler.pts);

            foreach (var point in pts)
            {
                Color test = bmp.GetPixel(point.X, point.Y);
                if (done.Contains(test))
                {
                    continue;
                }
                if (test.R == 0 && test.B == 0 && test.G == 0)
                {
                    continue;
                }

                Province p = colorProvinceMap[test];

                filler.FillColor = Color.White;
                filler.FloodFill(point);

                Color t = bmp.GetPixel(676, 1935);

                if (t != last)
                {
                }
                last = t;
                bool trimmed = false;

                int minX = 1000000;
                int minY = 1000000;
                int maxX = -1000000;
                int maxY = -1000000;

                foreach (var pt in filler.pts)
                {
                    if (pt.X < minX)
                    {
                        minX = pt.X;
                    }
                    if (pt.Y < minY)
                    {
                        minY = pt.Y;
                    }

                    if (pt.X > maxX)
                    {
                        maxX = pt.X;
                    }
                    if (pt.Y > maxY)
                    {
                        maxY = pt.Y;
                    }
                }

                bool tooSmall = false;

                if (maxX - minX < 16 || maxY - minY < 16)
                {
                    tooSmall = true;
                }

                if (filler.pts.Count < 100 || tooSmall)
                {
                    if (filler.pts.Count == p.points.Count)
                    {
                        done.Add(test);
                    }
                    Color newCol = FindBestNeighbour(filler.pts, test, true);
                    if (newCol == Color.White)
                    {
                        newCol = FindBestNeighbour(filler.pts, test, false);
                    }

                    if (newCol != Color.White)
                    {
                        filler.FillColor = newCol;
                        filler.FloodFill(point);
                        trimmed = true;
                        if (newCol.R == 0 && newCol.B == 0 && newCol.G == 0)
                        {
                            continue;
                        }

                        colorProvinceMap[newCol].points.AddRange(filler.pts);
                    }
                    else
                    {
                        //    filler.FillColor = Color.Black;
                        //   filler.FloodFill(point);
                        //   trimmed = true;
                    }
                    if (!trimmed)
                    {
                    }
                }

                if (!trimmed)
                {
                    if (filler.pts.Count == p.points.Count)
                    {
                        filler.FillColor = test;
                        filler.FloodFill(point);
                        done.Add(test);

                        t = bmp.GetPixel(676, 1935);

                        if (t != last)
                        {
                        }
                        last = t;
                    }
                    else
                    {
                        Province pNew = new Province();
                        pNew.AreaColor = col;
                        do
                        {
                            pNew.Color = colorMap[totalGeneratedProvinces++];
                        } while ((pNew.Color.G == 0 && pNew.Color.B == 0) || pNew.Color == Color.FromArgb(255, 69, 91, 186) || pNew.Color == Color.FromArgb(255, 130, 158, 75) || colorProvinceMap.ContainsKey(pNew.Color));

                        filler.FillColor = pNew.Color;
                        filler.FloodFill(point);
                        t = bmp.GetPixel(676, 1935);

                        if (t != last)
                        {
                        }
                        last = t;

                        provinces.Add(pNew);
                        colorProvinceMap[pNew.Color] = pNew;
                        done.Add(pNew.Color);
                        pNew.points.AddRange(filler.pts);
                        pNew.points.ForEach(o => p.points.Remove(o));
                        pNew.isSea = p.isSea;
                    }
                }
            }
        }
        public void Init(LockBitmap bmp, float sizeDelta, GeneratedTerrainMap generatedMap)
        {
            provincesDelta = sizeDelta;
            if (provincesDelta < 0.5f)
            {
                provincesDelta *= 1.8f;
            }

            LoadProvinceColors();
            Color col = Color.FromArgb(255, 1, 0, 0);

            int   r = 1;
            int   g = 0;
            int   b = 0;
            float highmountainLevel = 0.7f;
            float DivNoise          = 1200.0f * (generatedMap.Height / 2048.0f);

            NoiseGenerator noiseH = new NoiseGenerator();
            NoiseGenerator noiseM = new NoiseGenerator();

            generatedMap.Map.LockBits();
            generatedMap.MoistureMap.LockBits();
            generatedMap.HeatMap.LockBits();
            SolidBrush br = new SolidBrush(Color.Black);

            using (Graphics gg = Graphics.FromImage(bmp.Source))
                for (int x = 0; x < generatedMap.Map.Width; x++)
                {
                    for (int y = 0; y < generatedMap.Map.Height; y++)
                    {
                        float height = generatedMap.Map.GetHeight(x, y);

                        if (height >= highmountainLevel * 255)
                        {
                            gg.FillEllipse(br, new Rectangle(x - 2, y - 2, 4, 4));
                        }
                    }
                }
            LockBitmap bmp2 = new LockBitmap(new Bitmap(bmp.Source));

            bmp2.LockBits();
            using (Graphics gg = Graphics.FromImage(bmp.Source))
                for (int x = 0; x < generatedMap.Map.Width; x++)
                {
                    for (int y = 0; y < generatedMap.Map.Height; y++)
                    {
                        float heat     = generatedMap.HeatMap.GetHeight(x, y) / 255.0f;             // + (Rand.Next(-30, 30) / 8000.0f);
                        float moisture = generatedMap.MoistureMap.GetHeight(x / 4, y / 4) / 255.0f; // + (Rand.Next(-30, 30) / 8000.0f);

                        float no = (float)((float)((noiseH.Noise(x / DivNoise / 8, y / DivNoise / 8)) - 0.5f) * 0.2);
                        heat     += no * 1.4f;
                        moisture -= no * 0.5f;

                        float desertLevel = 0.48f;
                        float desertDry   = 0.28f;
                        bool  hot         = heat > desertLevel;
                        bool  dry         = moisture <= desertDry;

                        if (hot && dry && bmp2.GetPixel(x, y) == Color.FromArgb(255, 130, 158, 75))
                        {
                            gg.FillEllipse(br, new Rectangle(x - 5, y - 5, 10, 10));
                        }
                    }
                }
            bmp2.UnlockBits();
            generatedMap.Map.UnlockBits();
            generatedMap.MoistureMap.UnlockBits();
            generatedMap.HeatMap.UnlockBits();
//            bmp.Save24(Globals.MapOutputTotalDir + "testprovinces.bmp");
            bmp.LockBits();
            UnsafeQueueLinearFloodFiller filler = new UnsafeQueueLinearFloodFiller(null);

            filler.Bitmap = bmp;
            Bitmap        = bmp;
            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    Color pix = bmp.GetPixel(x, y);
                    if (pix == Color.FromArgb(255, 69, 91, 186) || pix == Color.FromArgb(255, 130, 158, 75))
                    {
                        areaColors.Add(col);
                        filler.FillColor = col;
                        filler.FloodFill(new Point(x, y));

                        bool valid = false;
                        foreach (var point in filler.pts)
                        {
                            for (int yy = -1; yy <= 1; yy++)
                            {
                                for (int xx = -1; xx <= 1; xx++)
                                {
                                    if (xx == 0 && yy == 0)
                                    {
                                        continue;
                                    }
                                    if (point.X + xx < 0 || point.Y + yy < 0 || point.X + xx >= bmp.Width || point.Y + yy >= bmp.Height)
                                    {
                                        continue;
                                    }

                                    var ccc = bmp.GetPixel(xx + point.X, yy + point.Y);
                                    if (col.R == ccc.R && ccc.G == col.G && ccc.B == col.B)
                                    {
                                        continue;
                                    }
                                    if (ccc.R > 0 || ccc.G > 0 || ccc.B > 0)
                                    {
                                        valid = true;
                                    }
                                }
                            }
                        }

                        if (!valid)
                        {
                            filler.FillColor = Color.Black;
                            filler.FloodFill(new Point(x, y));
                            continue;
                        }

                        {
                            int numProvinces = (int)(filler.pts.Count / (3000));
                            if (pix == Color.FromArgb(255, 130, 158, 75))
                            {
                                numProvinces = (int)(numProvinces * sizeDelta);
                            }
                            if (numProvinces == 0)
                            {
                                numProvinces = 1;
                            }
                            if (pix != Color.FromArgb(255, 130, 158, 75))
                            {
                                numProvinces /= 6;
                                if (numProvinces < 1)
                                {
                                    numProvinces = 1;
                                }
                            }
                            CreateProvinces(numProvinces, col, filler.pts, bmp.Width, bmp.Height, bmp, pix != Color.FromArgb(255, 130, 158, 75));
                            FixupProvinces(col, filler, bmp);
                        }
                        r += 1;
                        if (r > 255)
                        {
                            r  = 0;
                            g += 1;
                        }
                        if (g > 255)
                        {
                            g  = 0;
                            b += 1;
                        }
                        col = Color.FromArgb(255, r, g, b);
                    }
                }
            }
            provinces = provinces.Distinct().ToList();
            foreach (var province in provinces)
            {
                province.points.Clear();
                colorProvinceMap[province.Color] = province;
            }
            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    Color pix = bmp.GetPixel(x, y);
                    if (colorProvinceMap.ContainsKey(pix))
                    {
                        colorProvinceMap[pix].points.Add(new Point(x, y));
                    }
                    else
                    {
                    }
                }
            }

            CalculateDetails();

            bmp.UnlockDirect();


            //    bmp.Save24("C:\\Users\\LEMMY\\Documents\\terrainNew.bmp");
        }
Example #3
0
        public void Init(LockBitmap bmp, float sizeDelta, GeneratedTerrainMap generatedMap)
        {
            provincesDelta = sizeDelta;
            if (provincesDelta < 0.5f)
            {
                provincesDelta *= 1.8f;
            }

            LoadProvinceColors();
            Color col = FromArgb(255, 1, 0, 0);

            int   r = 1;
            int   g = 0;
            int   b = 0;
            float highmountainLevel = 0.7f;
            float DivNoise          = 1200.0f * (generatedMap.Height / 2048.0f);

            NoiseGenerator noiseH = new NoiseGenerator();

            generatedMap.Map.LockBits();
            generatedMap.MoistureMap.LockBits();
            generatedMap.HeatMap.LockBits();

            SolidBrush br = new SolidBrush(Black);

            using (Graphics gg = Graphics.FromImage(bmp.Source))
            {
                for (int x = 0; x < generatedMap.Map.Width; x++)
                {
                    for (int y = 0; y < generatedMap.Map.Height; y++)
                    {
                        float height = generatedMap.Map.GetHeight(x, y);

                        if (height >= highmountainLevel * 255)
                        {
                            gg.FillEllipse(br, new Rectangle(x - 2, y - 2, 4, 4));
                        }
                    }
                }
            }

            LockBitmap bmp2 = new LockBitmap(new Bitmap(bmp.Source));

            bmp2.LockBits();

            using (Graphics gg = Graphics.FromImage(bmp.Source))
            {
                for (int x = 0; x < generatedMap.Map.Width; x++)
                {
                    for (int y = 0; y < generatedMap.Map.Height; y++)
                    {
                        float heat     = generatedMap.HeatMap.GetHeight(x, y) / 255.0f; // + (Rand.Next(-30, 30) / 8000.0f);
                        float moisture =
                            generatedMap.MoistureMap.GetHeight(x / 4, y / 4) /
                            255.0f; // + (Rand.Next(-30, 30) / 8000.0f);

                        float no = (float)((float)((noiseH.Noise(x / DivNoise / 8, y / DivNoise / 8)) - 0.5f) * 0.2);
                        heat     += no * 1.4f;
                        moisture -= no * 0.5f;

                        float desertLevel = 0.48f;
                        float desertDry   = 0.28f;
                        bool  hot         = heat > desertLevel;
                        bool  dry         = moisture <= desertDry;

                        if (hot && dry && bmp2.GetPixel(x, y) == FromArgb(255, 130, 158, 75))
                        {
                            gg.FillEllipse(br, new Rectangle(x - 5, y - 5, 10, 10));
                        }
                    }
                }
            }

            bmp2.UnlockBits();
            generatedMap.Map.UnlockBits();
            generatedMap.MoistureMap.UnlockBits();
            generatedMap.HeatMap.UnlockBits();
            bmp.LockBits();

            var filler = new UnsafeQueueLinearFloodFiller(null)
            {
                Bitmap = bmp
            };

            Bitmap = bmp;

            for (int x = 0; x < bmp.Width; x++)
            {
                for (int y = 0; y < bmp.Height; y++)
                {
                    Color pix = bmp.GetPixel(x, y);
                    if (pix == FromArgb(255, 69, 91, 186) || pix == FromArgb(255, 130, 158, 75))
                    {
                        col = setColor(bmp, sizeDelta, col, filler, x, y, pix, ref r, ref g, ref b);
                    }
                }
            }

            provinces = provinces.Distinct().ToList();

            foreach (var province in provinces)
            {
                province.points.Clear();
                colorProvinceMap[province.Color] = province;
            }

            CalculateProvinceColor(bmp);

            CalculateDetails();
            bmp.UnlockDirect();
        }
Example #4
0
        //protected static readonly object trimmingLockObject = new System.Object();
        public byte[] ПолучитьФайлПросмотр(object id_file, ImageType type, int width, int height, ImageFormat format, long Качество, КонструкторИзображения Конструктор, string domain)
        {
            #region FileName
            var data     = new DataClient();
            var path     = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["ПросмотрХранилище"], domain);
            var filename = null as string;

            switch (format)
            {
            case ImageFormat.Jpg:
            {
                filename = string.Format("{0:f0}_{1:0}x{2:0}_{3}_{4}_{5}.jpg",
                                         id_file, width, height,
                                         type.ToString(), Качество,
                                         Path.GetFileNameWithoutExtension(data.ПолучитьЗначение <string>(id_file, "НазваниеОбъекта", Хранилище.Оперативное, domain)));
            }
            break;

            case ImageFormat.Png:
            {
                filename = string.Format("{0:f0}_{1:0}x{2:0}_{3}_{4}.png",
                                         id_file, width, height,
                                         type.ToString(),
                                         Path.GetFileNameWithoutExtension(data.ПолучитьЗначение <string>(id_file, "НазваниеОбъекта", Хранилище.Оперативное, domain)));
            }
            break;
            }
            #endregion

            var full_filename = Path.Combine(path, filename);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            if (System.IO.File.Exists(full_filename))
            {
                return(System.IO.File.ReadAllBytes(full_filename));
            }
            else
            {
                var imgOutput = null as Bitmap;
                var canvas    = null as Graphics;
                try
                {
                    using (var output = new MemoryStream())
                    {
                        try
                        {
                            var image      = null as Bitmap;
                            var filestream = ПолучитьФайлПолностью(id_file, Хранилище.Оперативное, domain);
                            if (filestream != null)
                            {
                                using (var mc = new MemoryStream(filestream))
                                    using (image = new Bitmap(mc))
                                    {
                                        #region очистить фон
                                        if (Конструктор != null && Конструктор.ПрозрачныйФон)
                                        {
                                            var firstPixelColor = image.GetPixel(0, 0);
                                            if (firstPixelColor != null && Color.Transparent.ToArgb() != firstPixelColor.ToArgb())
                                            {
                                                var floodFiller = new UnsafeQueueLinearFloodFiller()
                                                {
                                                    Bitmap    = new PictureBoxScroll.EditableBitmap(image, PixelFormat.Format32bppArgb),
                                                    FillColor = Color.Transparent,
                                                    Tolerance = new byte[] { 5, 5, 5 }
                                                };
                                                floodFiller.FloodFill(new Point(0, 0));

                                                if (floodFiller.Bitmap != null && floodFiller.Bitmap.Bitmap != null)
                                                {
                                                    image.Dispose();
                                                    image = new Bitmap(floodFiller.Bitmap.Bitmap);
                                                }
                                            }
                                        }
                                        #endregion

                                        #region trimming
                                        if (Конструктор != null && Конструктор.ОбрезатьПустоеМесто)
                                        {
                                            // Find the min/max non-white/transparent pixels
                                            var min = new Point(int.MaxValue, int.MaxValue);
                                            var max = new Point(int.MinValue, int.MinValue);


                                            var isAlpha = image.PixelFormat.HasFlag(PixelFormat.Alpha);
                                            for (int x = 0; x < image.Width; ++x)
                                            {
                                                for (int y = 0; y < image.Height; ++y)
                                                {
                                                    var pixelColor = image.GetPixel(x, y);

                                                    if ((pixelColor.R < 245 && pixelColor.G < 245 && pixelColor.B < 245 && pixelColor.A > 0) ||
                                                        (isAlpha && pixelColor.A > 0))
                                                    {
                                                        if (x < min.X)
                                                        {
                                                            min.X = x;
                                                        }
                                                        if (y < min.Y)
                                                        {
                                                            min.Y = y;
                                                        }

                                                        if (x > max.X)
                                                        {
                                                            max.X = x;
                                                        }
                                                        if (y > max.Y)
                                                        {
                                                            max.Y = y;
                                                        }
                                                    }
                                                }
                                            }

                                            var cropRectangle = new Rectangle(min.X, min.Y, max.X - min.X, max.Y - min.Y);
                                            var newBitmap     = new Bitmap(cropRectangle.Width, cropRectangle.Height);
                                            newBitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);

                                            var g = Graphics.FromImage(newBitmap);
                                            g.DrawImage(image, 0, 0, cropRectangle, GraphicsUnit.Pixel);
                                            g.Flush();

                                            image.Dispose();
                                            image = newBitmap;
                                        }
                                        #endregion

                                        var newSize = new Size(width, height);
                                        switch (type)
                                        {
                                        case ImageType.Full:
                                        {
                                            newSize                   = new Size(width, height);
                                            imgOutput                 = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppArgb);
                                            canvas                    = Graphics.FromImage(imgOutput);
                                            canvas.SmoothingMode      = SmoothingMode.Default;
                                            canvas.CompositingQuality = CompositingQuality.HighQuality;
                                            canvas.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                                            if (format == ImageFormat.Jpg)
                                            {
                                                canvas.Clear(Color.White);
                                            }
                                            canvas.DrawImage(image, 0, 0, newSize.Width, newSize.Height);
                                            canvas.Flush();
                                        }
                                        break;

                                        case ImageType.Thumbnail:
                                        {
                                            int _width = Math.Min(image.Size.Height, image.Size.Width);
                                            imgOutput                 = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppArgb);
                                            canvas                    = Graphics.FromImage(imgOutput);
                                            canvas.SmoothingMode      = SmoothingMode.Default;
                                            canvas.CompositingQuality = CompositingQuality.HighQuality;
                                            canvas.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                                            if (format == ImageFormat.Jpg)
                                            {
                                                canvas.Clear(Color.White);
                                            }
                                            canvas.DrawImage(image, new Rectangle(0, 0, newSize.Width, newSize.Height), new Rectangle(0, 0, _width, _width), GraphicsUnit.Pixel);
                                            canvas.Flush();
                                        }
                                        break;

                                        case ImageType.Resize:
                                        {
                                            if (newSize.Width > 0 && newSize.Height == 0)                // Сжатие по ширине (принудительное)
                                            {
                                                newSize.Height = (int)(((double)newSize.Width / (double)image.Size.Width) * image.Size.Height);
                                            }
                                            else if (newSize.Width == 0 && newSize.Height > 0)          // Сжатие по высоте (принудительное)
                                            {
                                                newSize.Width = (int)(((double)newSize.Height / (double)image.Size.Height) * image.Size.Width);
                                            }
                                            else
                                            {
                                                int sourceWidth  = image.Size.Width;
                                                int sourceHeight = image.Size.Height;

                                                float nPercent  = 0;
                                                float nPercentW = 0;
                                                float nPercentH = 0;

                                                nPercentW = ((float)newSize.Width / (float)sourceWidth);
                                                nPercentH = ((float)newSize.Height / (float)sourceHeight);

                                                if (nPercentH < nPercentW)
                                                {
                                                    nPercent = nPercentH;
                                                }
                                                else
                                                {
                                                    nPercent = nPercentW;
                                                }

                                                newSize = new Size((int)(sourceWidth * nPercent), (int)(sourceHeight * nPercent));
                                            }

                                            imgOutput                 = new Bitmap(newSize.Width, newSize.Height, PixelFormat.Format32bppArgb);
                                            canvas                    = Graphics.FromImage(imgOutput);
                                            canvas.SmoothingMode      = SmoothingMode.Default;
                                            canvas.CompositingQuality = CompositingQuality.HighQuality;
                                            canvas.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                                            if (format == ImageFormat.Jpg)
                                            {
                                                canvas.Clear(Color.White);
                                            }
                                            canvas.DrawImage(image, 0, 0, newSize.Width, newSize.Height);
                                            canvas.Flush();
                                        }
                                        break;
                                        }

                                        #region Добавить слои из конструктора
                                        if (Конструктор != null && Конструктор.Слои != null && canvas != null)
                                        {
                                            foreach (var item in Конструктор.Слои)
                                            {
                                                if (item.id_file == null || item.id_file.Equals(0m))
                                                {
                                                    continue;
                                                }

                                                using (var mcLayer = new MemoryStream(ПолучитьФайлПолностью(item.id_file, Хранилище.Оперативное, domain)))
                                                    using (var imageLayer = new Bitmap(mcLayer))
                                                    {
                                                        canvas.DrawImage(imageLayer, item.x, item.y,
                                                                         Convert.ToInt32(imageLayer.Width * (item.width > 0 ? item.width : 1.0)),
                                                                         Convert.ToInt32(imageLayer.Height * (item.height > 0 ? item.height : 1.0)));
                                                    }
                                            }
                                        }
                                        #endregion
                                    }
                            }
                            else
                            {
                                imgOutput = new Bitmap(width, height);
                                canvas    = Graphics.FromImage(imgOutput);
                                canvas.Clear(Color.White);
                                canvas.DrawRectangle(Pens.Gray, 0, 0, width - 1, height - 1);
                                canvas.DrawString(
                                    "Not Found " + data.ПолучитьЗначение <string>(id_file, "НазваниеОбъекта", Хранилище.Оперативное, domain),
                                    new Font("Tahoma", 8),
                                    Brushes.Gray,
                                    new RectangleF(0, 0, width, height),
                                    new StringFormat()
                                {
                                    Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center
                                });
                            }
                        }
                        catch (Exception ex)
                        {
                            imgOutput = new Bitmap(width, height);
                            canvas    = Graphics.FromImage(imgOutput);
                            canvas.Clear(Color.White);
                            canvas.DrawRectangle(Pens.Gray, 0, 0, width - 1, height - 1);
                            canvas.DrawString(
                                data.ПолучитьЗначение <string>(id_file, "НазваниеОбъекта", Хранилище.Оперативное, domain),
                                new Font("Tahoma", 8),
                                Brushes.Gray,
                                new RectangleF(0, 0, width, height),
                                new StringFormat()
                            {
                                Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center
                            });

                            ConfigurationClient.WindowsLog("FileService", string.Empty, domain, ex.ToString());
                        }

                        #region Вывод
                        switch (format)
                        {
                        case ImageFormat.Jpg:
                        {
                            var ep = new EncoderParameters(1);
                            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Качество);
                            ImageCodecInfo imageCodecInfo = null;
                            foreach (ImageCodecInfo item in ImageCodecInfo.GetImageEncoders())
                            {
                                if (item.MimeType == "image/jpeg")
                                {
                                    imageCodecInfo = item;
                                    break;
                                }
                            }
                            imgOutput.Save(output, imageCodecInfo, ep);
                        }
                        break;

                        case ImageFormat.Png:
                        {
                            imgOutput.Save(output, System.Drawing.Imaging.ImageFormat.Png);
                        }
                        break;
                        }

                        //сохранить файл
                        System.IO.File.WriteAllBytes(full_filename, output.ToArray());
                        #endregion

                        return(output.ToArray());
                    }
                }
                finally
                {
                    if (canvas != null)
                    {
                        canvas.Dispose();
                    }
                    if (imgOutput != null)
                    {
                        imgOutput.Dispose();
                    }
                }
            }
        }
Example #5
0
        private Color setColor(LockBitmap bmp, float sizeDelta, Color col, UnsafeQueueLinearFloodFiller filler, int x, int y,
                               Color pix, ref int r, ref int g, ref int b)
        {
            areaColors.Add(col);
            filler.FillColor = col;
            filler.FloodFill(new Point(x, y));

            bool valid = false;

            foreach (var point in filler.pts)
            {
                for (int yy = -1; yy <= 1; yy++)
                {
                    for (int xx = -1; xx <= 1; xx++)
                    {
                        if (xx == 0 && yy == 0)
                        {
                            continue;
                        }

                        if (point.X + xx < 0 || point.Y + yy < 0 || point.X + xx >= bmp.Width ||
                            point.Y + yy >= bmp.Height)
                        {
                            continue;
                        }

                        var ccc = bmp.GetPixel(xx + point.X, yy + point.Y);

                        if (col.R == ccc.R && ccc.G == col.G && ccc.B == col.B)
                        {
                            continue;
                        }

                        if (ccc.R > 0 || ccc.G > 0 || ccc.B > 0)
                        {
                            valid = true;
                        }
                    }
                }
            }

            if (!valid)
            {
                filler.FillColor = Black;
                filler.FloodFill(new Point(x, y));
                return(col);
            }

            int numProvinces = (int)(filler.pts.Count / (3000));

            if (pix == FromArgb(255, 130, 158, 75))
            {
                numProvinces = (int)(numProvinces * sizeDelta);
            }
            if (numProvinces == 0)
            {
                numProvinces = 1;
            }

            if (pix != FromArgb(255, 130, 158, 75))
            {
                numProvinces /= 6;
                if (numProvinces < 1)
                {
                    numProvinces = 1;
                }
            }

            CreateProvinces(numProvinces, col, filler.pts, bmp.Width, bmp.Height, bmp,
                            pix != FromArgb(255, 130, 158, 75));
            FixupProvinces(col, filler, bmp);
            r += 1;
            if (r > 255)
            {
                r  = 0;
                g += 1;
            }

            if (g > 255)
            {
                g  = 0;
                b += 1;
            }

            col = FromArgb(255, r, g, b);
            return(col);
        }