using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
namespace RegionBuilder
{
    public static class Tools
    {
        public static Region NewRegionForm(Color cTrans, string filename, Rectangle clientRec)
        {
            if (File.Exists(filename))
            {
                Bitmap bmp = new Bitmap(filename);
                return NewRegionForm(cTrans,bmp,clientRec);
            }
            return new Region(clientRec);
        }
        public static Region NewRegionForm(Color cTrans, Bitmap b, Rectangle clientRec)
        {
            int ct = cTrans.ToArgb();
            Rectangle r = new Rectangle(0, 0, 1, 1);
            GraphicsPath gpRemoved = new GraphicsPath(FillMode.Winding);
            BitmapData bd = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            int h = b.Height;
            int w = b.Width;
            int[,] mat = new int[h, w];
            int curj = 0;
            int nb = 0;
            bool canAdd = false;
            unsafe
            {
                int* pi = (int*)bd.Scan0.ToInt32() - 1;
                for (int i = 0; i < h; i += 1)
                {
                    for (int j = 0; j < w; j += 1)
                    {
                        pi++;
                        if (*pi == ct)
                        {
                            r.X = j;
                            r.Y = i;
                            mat[i, j] = 1;
                            if (canAdd)
                            {
                                mat[i, j] = 0;
                                nb++;
                            }
                            else
                            {
                                mat[i, curj] = nb;
                                curj = j;
                                canAdd = true;
                                nb = 1;
                            }
                        }
                        else
                        {
                            mat[i, j] = -1;
                            if (canAdd)
                            {
                                canAdd = false;
                                mat[i, curj] = nb;
                                nb = 0;
                            }
                            curj = j;
                        }
                    }
                    if (canAdd)
                    {
                        canAdd = false;
                        mat[i, curj] = nb;
                        nb = 0;
                        curj = 0;
                    }
                }
            }
            for (int i = 0; i < h; i += 1)
            {
                for (int j = 0; j < w; j += 1)
                {
                    if (mat[i, j] > 1)
                    {
                        r.Width = mat[i, j];
                        r.X = j;
                        r.Y = i;
                        j += (mat[i, j] - 1);
                        gpRemoved.AddRectangle(r);
                    }
                    else if (mat[i, j] == 1)
                    {
                        r.Width = 1;
                        r.X = j;
                        r.Y = i;
                        gpRemoved.AddRectangle(r);
                    }
                }
            }
            b.UnlockBits(bd);
            Region a = null;
            a = new Region(clientRec);
            a.Exclude(gpRemoved);
            gpRemoved.Dispose();
            return a;
        }
    }
}