Exemplo n.º 1
0
        private IEnumerable <Region> Isolate(Int32Rect zone, int blanksBetweenRegions, bool horizontal, Color?drawColor)
        {
            // ------ initialisation -----

            // on etudie dans le cadre délimité par la zone
            // le pixel en (right, bot) est le dernier pixel de la zone
            int right = zone.X + zone.Width - 1;
            int bot   = zone.Y + zone.Height - 1;

            int    spaces        = 0;
            bool   inRegion      = false;
            Region currentRegion = null;

            int index     = horizontal ? zone.Y : zone.X;
            int lastIndex = (horizontal ? bot : right);

            // -------------------------

            // ----- boucle sur chaque ligne/colonne ----
            for (; index <= lastIndex; index++) // dernier indice compris
            {
                var start = horizontal ? zone.X : zone.Y;
                var end   = horizontal ? right : bot;

                // line delimite la zones de pixels noirs
                Line line;
                var  isBlank = ScanLineOrColumn(horizontal, index, start, end, out line);

                if (isBlank) // c'est un espace blanc
                {
                    spaces++;
                }
                else
                {
                    spaces = 0; // ligne noire on réinitialise le compteur
                }
                // réadapte la taille de la zone si on a rencontré une ligne non blanche
                if (inRegion && !isBlank)
                {
                    currentRegion.AdjustSizeToFit(line);
                }

                // on sort d'une region ou c'est la derniere region
                if (inRegion && (spaces >= blanksBetweenRegions || index == lastIndex))
                {
                    // on retourne cette region
                    yield return(currentRegion);

                    // dessine la region
                    if (drawColor.HasValue)
                    {
                        Output.DrawRectangle(currentRegion.X, currentRegion.Y, currentRegion.X + currentRegion.Width - 1, currentRegion.Y + currentRegion.Height - 1, drawColor.Value);
                    }

                    // on reinitialise le compteur d'espaces blancs
                    spaces        = 0;
                    inRegion      = false; // on n'est plus dans une region
                    currentRegion = null;
                }

                if (!inRegion && !isBlank) // on entre dans une nouvelle region
                {
                    inRegion      = true;
                    currentRegion = new Region(line); // nouvelle region qui continent la ligne
                }
            }
        }