private void ReduceAllRegionUnderCountPixel(CanvasPixel canvasPixel, int maxCountPixel, CanvasPixel canvasPixelOriginal)
        {
            int origRegionCount = 0;

            do
            {
                origRegionCount = _listRegion.Count;
                for (int i = 0; i < _listRegion.Count; i++)
                {
                    RegionVO region = _listRegion[i];

                    if (region == null)
                    {
                        break;
                    }
                    if (region.NotValid)
                    {
                        continue;
                    }

                    if (region.Pixels.Length <= maxCountPixel)
                    {
                        RegionVO bestForFuse = BestRegionToFuse2(region, canvasPixel, int.MaxValue, true);
                        if (bestForFuse == null)
                        {
                            continue;
                        }

                        if (bestForFuse != region)
                        {
                            //// swap for faster merge
                            if (region.Pixels.Length > bestForFuse.Pixels.Length)
                            {
                                RegionVO tmp = region;
                                region      = bestForFuse;
                                bestForFuse = tmp;
                            }

                            FuseRegions(bestForFuse, region, canvasPixel, canvasPixelOriginal);

                            region.NotValid = true;
                            region.Clear();
                        }
                    }
                }

                List <RegionVO> kk = new List <RegionVO>(_listRegion.Count);
                for (int i = 0; i < _listRegion.Count; i++)
                {
                    if (!_listRegion[i].NotValid)
                    {
                        kk.Add(_listRegion[i]);
                    }
                }

                _listRegion = kk;
            } while (origRegionCount > _listRegion.Count);
        }
        private void ReduceAllRegionUnderColorTolerance(CanvasPixel canvasPixel, int maxColorDiff, CanvasPixel canvasPixelOriginal)
        {
            int origRegionCount = 0;

            //List<RegionVO> tmpRegions = new List<RegionVO>(_listRegion.Count);

            //HashSet<RegionVO> tmpRegions = new HashSet<RegionVO>();

            do
            {
                //  tmpRegions.Clear();

                //if (!Check_ValidRegionData(_regionPixelLookup, _listRegion))
                //{
                //    System.Diagnostics.Trace.WriteLine($"Start Invalid region data {_listRegion.Count}");
                //}



                origRegionCount = _listRegion.Count;
                for (int i = 0; i < _listRegion.Count; i++)
                {
                    RegionVO region = _listRegion[i];

                    if (region == null)
                    {
                        break;
                    }
                    if (region.NotValid)
                    {
                        continue;
                    }


                    RegionVO bestForFuse = BestRegionToFuse2(region, canvasPixel, maxColorDiff);
                    if (bestForFuse == null)
                    {
                        continue;
                    }


                    if (bestForFuse != region)
                    {
                        //// swap for faster merge
                        if (region.Pixels.Length > bestForFuse.Pixels.Length)
                        {
                            RegionVO tmp = region;
                            region      = bestForFuse;
                            bestForFuse = tmp;
                        }

                        FuseRegions(bestForFuse, region, canvasPixel, canvasPixelOriginal);

                        region.NotValid = true;
                        region.Clear();
                    }



                    //if (!Check_ValidRegionData(_regionPixelLookup, _listRegion))
                    //{
                    //    throw new Exception();
                    //}
                }

                //   _listRegion = _listRegion.Where(x => !x.NotValid ).ToList();
                List <RegionVO> kk = new List <RegionVO>(_listRegion.Count);
                for (int i = 0; i < _listRegion.Count; i++)
                {
                    if (!_listRegion[i].NotValid)
                    {
                        kk.Add(_listRegion[i]);
                    }
                }

                _listRegion = kk;
                //if (!Check_ValidRegionData(_regionPixelLookup, _listRegion))
                //{
                //    System.Diagnostics.Trace.WriteLine($"End Invalid region data {_listRegion.Count}");
                //}

                //if (tmpRegions.Count == 0)
                //{
                //    throw new Exception();
                //}
            } while (origRegionCount > _listRegion.Count);
        }