Пример #1
0
        public void SaveRegion(GeometryRegion saveMeRegion, Rectangle saveMeBounds)
        {
            var activeLayer = (BitmapLayer)ActiveLayer;

            if (_savedTiles == null)
            {
                _savedTiles = new BitVector2D(
                    (activeLayer.Width + SaveTileGranularity - 1) / SaveTileGranularity,
                    (activeLayer.Height + SaveTileGranularity - 1) / SaveTileGranularity);

                _savedTiles.Clear(false);
            }

            Rectangle regionBounds;

            if (saveMeRegion == null)
            {
                regionBounds = saveMeBounds;
            }
            else
            {
                regionBounds = saveMeRegion.GetBoundsInt();
            }

            Rectangle bounds = Rectangle.Union(regionBounds, saveMeBounds);

            bounds.Intersect(activeLayer.Bounds);

            int leftTile   = bounds.Left / SaveTileGranularity;
            int topTile    = bounds.Top / SaveTileGranularity;
            int rightTile  = (bounds.Right - 1) / SaveTileGranularity;
            int bottomTile = (bounds.Bottom - 1) / SaveTileGranularity;

            for (int tileY = topTile; tileY <= bottomTile; ++tileY)
            {
                Rectangle rowAccumBounds = Rectangle.Empty;

                for (int tileX = leftTile; tileX <= rightTile; ++tileX)
                {
                    if (!_savedTiles.Get(tileX, tileY))
                    {
                        Rectangle tileBounds = new Rectangle(tileX * SaveTileGranularity, tileY * SaveTileGranularity,
                                                             SaveTileGranularity, SaveTileGranularity);

                        tileBounds.Intersect(activeLayer.Bounds);

                        if (rowAccumBounds == Rectangle.Empty)
                        {
                            rowAccumBounds = tileBounds;
                        }
                        else
                        {
                            rowAccumBounds = Rectangle.Union(rowAccumBounds, tileBounds);
                        }

                        _savedTiles.Set(tileX, tileY, true);
                    }
                    else
                    {
                        if (rowAccumBounds != Rectangle.Empty)
                        {
                            using (Surface dst = ScratchSurface.CreateWindow(rowAccumBounds),
                                   src = activeLayer.Surface.CreateWindow(rowAccumBounds))
                            {
                                dst.CopySurface(src);
                            }

                            rowAccumBounds = Rectangle.Empty;
                        }
                    }
                }

                if (rowAccumBounds != Rectangle.Empty)
                {
                    using (Surface dst = ScratchSurface.CreateWindow(rowAccumBounds),
                           src = activeLayer.Surface.CreateWindow(rowAccumBounds))
                    {
                        dst.CopySurface(src);
                    }

                    rowAccumBounds = Rectangle.Empty;
                }
            }

            if (this._saveRegion != null)
            {
                this._saveRegion.Dispose();
                this._saveRegion = null;
            }

            if (saveMeRegion != null)
            {
                this._saveRegion = saveMeRegion.Clone();
            }
        }