public SelectionGeometryCache(IDirect2DFactory factory, PaintDotNet.Canvas.SelectionSnapshot selectionSnapshot)
 {
     this.factory           = factory;
     this.selectionSnapshot = selectionSnapshot;
     this.geometry          = LazyResult.New <IGeometry>(() => this.factory.CreateGeometry(selectionSnapshot.GeometryList.Value), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
     this.pixelatedGeometry = LazyResult.New <IGeometry>(() => GeometryHelpers.ToDirect2DGeometryDestructive(this.factory, ScansHelpers.ConvertNonOverlappingScansToPolygons(selectionSnapshot.PixelatedScans.Value), FillMode.Alternate, FigureBegin.Filled, FigureEnd.Closed), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
 }
 public PaintBucketToolContentRenderer(IRenderer <ColorBgra> sampleSource, PaintBucketToolChanges changes) : base(sampleSource.Width, sampleSource.Height, true)
 {
     Validate.IsNotNull <PaintBucketToolChanges>(changes, "changes");
     this.sampleSource  = sampleSource;
     this.changes       = changes;
     this.brush         = new PdnLegacyBrush(this.changes.BrushType, this.changes.HatchStyle, this.changes.ForegroundColor, this.changes.BackgroundColor).EnsureFrozen <PdnLegacyBrush>();
     this.brushRenderer = this.brush.CreateRenderer(base.Width, base.Height);
     this.lazyStencil   = LazyResult.New <IRenderer <ColorAlpha8> >(() => this.CreateMaskRenderer(), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
 }
Ejemplo n.º 3
0
 public BrushStrokeRenderCache(BrushStrokeRenderData renderData, BrushStamp stamp, int tileEdgeLog2)
 {
     Validate.Begin().IsNotNull <BrushStrokeRenderData>(renderData, "renderData").IsNotNull <BrushStamp>(stamp, "stamp").Check();
     this.renderData              = renderData;
     this.stamp                   = stamp;
     this.lazyStampMaskDevBitmap  = LazyResult.New <DeviceBitmap>(() => new DeviceBitmap(this.stamp.MaskBitmap).EnsureFrozen <DeviceBitmap>(), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
     this.tileOffsetToTileDataMap = new ConcurrentDictionary <PointInt32, TileData>();
     this.tileMathHelper          = new TileMathHelper(TransactedToolChanges.MaxMaxRenderBounds.Size, tileEdgeLog2);
 }
Ejemplo n.º 4
0
 public SelectionSnapshot(Result <PaintDotNet.Rendering.GeometryList> lazyGeometryList, Result <IReadOnlyList <RectInt32> > lazyPixelatedScans, Matrix3x2Double interimTransform, RectDouble fastMaxBounds, bool isEmpty, int geometryVersion)
 {
     Validate.Begin().IsNotNull <Result <PaintDotNet.Rendering.GeometryList> >(lazyGeometryList, "lazyGeometryList").IsNotNull <Result <IReadOnlyList <RectInt32> > >(lazyPixelatedScans, "lazyPixelatedScans").Check();
     this.lazyGeometryList   = lazyGeometryList;
     this.lazyPixelatedScans = lazyPixelatedScans;
     this.interimTransform   = interimTransform;
     this.fastMaxBounds      = fastMaxBounds;
     this.isEmpty            = isEmpty;
     this.geometryVersion    = geometryVersion;
     this.isRectilinear      = LazyResult.New <bool>(() => this.GeometryList.Value.IsRectilinear, LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
     this.isPixelated        = LazyResult.New <bool>(() => this.GeometryList.Value.IsPixelated, LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
 }
Ejemplo n.º 5
0
 private void EnsureCachesInitialized()
 {
     if (!this.areCachesInitialized)
     {
         object sync = this.sync;
         lock (sync)
         {
             if (!this.areCachesInitialized)
             {
                 this.lazyFlattenedPathGeometry = this.lazyFlattenedPathGeometry ?? LazyResult.New <PathGeometry>(() => this.geometry.GetFlattenedPathGeometry().EnsureFrozen <PathGeometry>(), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
                 this.lazyFlattenedPolyPoly     = this.lazyFlattenedPolyPoly ?? LazyResult.New <IList <PointDouble[]> >(() => (from p in PathGeometryUtil.EnumeratePathGeometryPolygons(this.lazyFlattenedPathGeometry.Value) select p.ToArrayEx <PointDouble>()).ToArrayEx <PointDouble[]>(), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
                 this.lazyGeometrySource        = this.lazyGeometrySource ?? LazyResult.New <GeometrySource>(() => GeometrySource.Create(this.geometry), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
                 this.perThreadD2DGeometry      = this.perThreadD2DGeometry ?? new ThreadLocal <IGeometry>(() => this.lazyGeometrySource.Value.ToDirect2DGeometry(Direct2DFactory.PerThread));
                 this.lazyGeometryBounds        = this.lazyGeometryBounds ?? LazyResult.New <RectDouble>(new Func <RectDouble>(this.ComputeBounds), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
                 this.areCachesInitialized      = true;
             }
         }
     }
 }
Ejemplo n.º 6
0
        public ShapesToolChanges CloneWithNewTransform(Matrix3x2Double newTransform)
        {
            if (newTransform == this.transform)
            {
                return(this);
            }
            if (!(this.transform.GetScale() == newTransform.GetScale()))
            {
                return(new ShapesToolChanges(base.DrawingSettingsValues, this.ShapeInfoSettingPath, this.AllShapePropertyValues, this.MouseStartPoint, this.MouseEndPoint, this.ShouldApplySnapping, this.ShouldApplyConstraint, this.IsEditingStartPoint, this.IsEditingEndPoint, this.WhichUserColor, newTransform, this.RotationAnchorOffset));
            }
            Matrix3x2Double   relativeTx = this.transform.Inverse * newTransform;
            ShapesToolChanges changes    = (ShapesToolChanges)base.MemberwiseClone();

            using (changes.UseChangeScope())
            {
                changes.transform       = newTransform;
                changes.shapeRenderData = LazyResult.New <PaintDotNet.Shapes.ShapeRenderData>(() => PaintDotNet.Shapes.ShapeRenderData.Transform(this.shapeRenderData.Value, relativeTx), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
                changes.InvalidateCachedMaxRenderBounds();
            }
            return(changes);
        }
Ejemplo n.º 7
0
        private void Initialize()
        {
            if ((this.allShapePropertyValuesMap == null) && (this.allShapePropertyValuesArray != null))
            {
                this.allShapePropertyValuesMap = ShapePropertyValuesUtil.ToReadOnlyMap(this.allShapePropertyValuesArray);
            }
            PaintDotNet.UI.Media.DashStyle style = DashStyleUtil.ToMedia(this.PenDashStyle);
            this.outlinePen = new Pen {
                Brush     = SolidColorBrushCache.Get((ColorRgba128Float)Colors.White),
                Thickness = this.PenWidth,
                LineJoin  = PenLineJoin.Miter,
                DashStyle = style
            }.EnsureFrozen <Pen>();
            RectDouble   baseBounds = this.BaseBounds;
            VectorDouble txScale    = this.Transform.GetScale();
            VectorDouble num3       = (VectorDouble)(this.EndPoint - this.StartPoint);
            PointDouble  endPoint   = new PointDouble(this.StartPoint.X + (num3.X * txScale.X), this.StartPoint.Y + (num3.Y * txScale.Y));
            Dictionary <string, object> settingValues = (from gsp in this.Shape.RenderSettingPaths select KeyValuePairUtil.Create <string, object>(gsp, base.GetDrawingSettingValue(gsp))).ToDictionary <string, object>();

            PaintDotNet.Shapes.ShapeRenderParameters renderParams = new PaintDotNet.Shapes.ShapeRenderParameters(this.StartPoint, endPoint, txScale, settingValues, null);
            this.shapePropertySchema = this.Shape.CreatePropertyCollection(renderParams);
            foreach (Property property in this.shapePropertySchema)
            {
                property.ValueChanged += delegate(object s, ValueEventArgs <object> e) {
                    throw new ReadOnlyException();
                };
                property.ReadOnlyChanged += delegate(object s, ValueEventArgs <bool> e) {
                    throw new ReadOnlyException();
                };
            }
            this.shapeRenderParams = new PaintDotNet.Shapes.ShapeRenderParameters(this.StartPoint, endPoint, txScale, settingValues, this.ShapePropertyValues);
            this.shapeRenderData   = LazyResult.New <PaintDotNet.Shapes.ShapeRenderData>(delegate {
                PaintDotNet.Shapes.ShapeRenderData renderData = this.Shape.CreateRenderData(this.shapeRenderParams);
                Matrix3x2Double matrix = Matrix3x2Double.ScalingAt(1.0 / Math.Abs(txScale.X), 1.0 / Math.Abs(txScale.Y), this.StartPoint.X, this.StartPoint.Y);
                return(PaintDotNet.Shapes.ShapeRenderData.Transform(PaintDotNet.Shapes.ShapeRenderData.Transform(renderData, matrix), this.transform));
            }, LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
        }
Ejemplo n.º 8
0
        public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace)
        {
            GeometryList cachedClippingMask = historyWorkspace.Selection.GetCachedClippingMask();

            if (historyWorkspace.Selection.IsEmpty || (cachedClippingMask.Bounds.Area < 1.0))
            {
                return(null);
            }
            Document  oldDocument   = historyWorkspace.Document;
            RectInt32 b             = cachedClippingMask.Bounds.GetInt32Bound(1E-05);
            RectInt32 oldClipBounds = RectInt32.Intersect(oldDocument.Bounds(), b);
            Document  document      = new Document(oldClipBounds.Width, oldClipBounds.Height);

            document.ReplaceMetadataFrom(oldDocument);
            RectInt32 newClipBounds = new RectInt32(0, 0, oldClipBounds.Width, oldClipBounds.Height);
            SelectionRenderingQuality                   quality = historyWorkspace.ToolSettings.Selection.RenderingQuality.Value;
            Result <IRenderer <ColorAlpha8> >           oldClipMaskRendererLazy = historyWorkspace.Selection.GetCachedLazyClippingMaskRenderer(quality);
            LazyResult <ClippedRenderer <ColorAlpha8> > newClipMaskRendererLazy = LazyResult.New <ClippedRenderer <ColorAlpha8> >(() => new ClippedRenderer <ColorAlpha8>(oldClipMaskRendererLazy.Value, oldClipBounds), LazyThreadSafetyMode.ExecutionAndPublication, new VistaCriticalSection(0));
            SelectionData selectionData = historyWorkspace.Selection.Save();

            System.Threading.Tasks.Task <SelectionHistoryMemento>       task  = System.Threading.Tasks.Task.Factory.StartNew <SelectionHistoryMemento>(() => new SelectionHistoryMemento(null, null, historyWorkspace, selectionData));
            System.Threading.Tasks.Task <ReplaceDocumentHistoryMemento> task2 = System.Threading.Tasks.Task.Factory.StartNew <ReplaceDocumentHistoryMemento>(() => new ReplaceDocumentHistoryMemento(null, null, historyWorkspace, oldDocument), TaskCreationOptions.LongRunning);
            int count = oldDocument.Layers.Count;

            System.Threading.Tasks.Task <BitmapLayer>[] items = new System.Threading.Tasks.Task <BitmapLayer> [count];
            for (int i = 0; i < count; i++)
            {
                int iP = i;
                items[iP] = System.Threading.Tasks.Task.Factory.StartNew <BitmapLayer>(delegate {
                    if (iP == 0)
                    {
                        newClipMaskRendererLazy.EnsureEvaluated();
                    }
                    BitmapLayer layer      = (BitmapLayer)oldDocument.Layers[iP];
                    Surface croppedSurface = layer.Surface.CreateWindow(oldClipBounds);
                    BitmapLayer newLayer   = RetryManager.RunMemorySensitiveOperation <BitmapLayer>(() => new BitmapLayer(croppedSurface));
                    newLayer.LoadProperties(layer.SaveProperties());
                    Parallel.ForEach <RectInt32>(newClipBounds.GetTiles(TilingStrategy.Tiles, 7), delegate(RectInt32 newTileRect) {
                        ISurface <ColorBgra> surface = newLayer.Surface.CreateWindow(newTileRect);
                        IRenderer <ColorAlpha8> mask = new ClippedRenderer <ColorAlpha8>(newClipMaskRendererLazy.Value, newTileRect);
                        surface.MultiplyAlphaChannel(mask);
                    });
                    return(newLayer);
                });
            }
            List <TupleStruct <System.Threading.Tasks.Task, double> > collection = new List <TupleStruct <System.Threading.Tasks.Task, double> >();

            collection.AddTuple <System.Threading.Tasks.Task, double>(task, 0.1);
            collection.AddTuple <System.Threading.Tasks.Task, double>(task2, 1.0);
            for (int j = 0; j < items.Length; j++)
            {
                collection.AddTuple <System.Threading.Tasks.Task, double>(items[j], 0.1);
            }
            PaintDotNet.Threading.Tasks.Task <Unit> task3 = historyWorkspace.TaskManager.CreateFrameworkTasksWrapper(collection);
            historyWorkspace.WaitWithProgress(task3, HistoryMementoImage, HistoryMementoName, PdnResources.GetString("Effects.ApplyingDialog.Description"));
            document.Layers.AddRange <Layer>(items.Select <System.Threading.Tasks.Task <BitmapLayer>, BitmapLayer>(t => t.Result));
            SelectionHistoryMemento       result   = task.Result;
            ReplaceDocumentHistoryMemento memento2 = task2.Result;

            base.EnterCriticalRegion();
            historyWorkspace.Document = document;
            HistoryMemento[] mementos = new HistoryMemento[] { result, memento2 };
            return(HistoryMemento.Combine(HistoryMementoName, HistoryMementoImage, mementos));
        }
Ejemplo n.º 9
0
        protected override GeometryList CreateSelectionGeometry(MagicWandToolChanges changes, AsyncSelectionToolCreateGeometryContext context, CancellationToken cancellationToken)
        {
            GeometryList          list;
            Result <BitVector2D>  lazyBaseStencil;
            IRenderer <ColorBgra> sampleSource = ((MagicWandToolCreateGeometryContext)context).SampleSource;
            byte       x         = (byte)Math.Round((double)(changes.Tolerance * 255.0), MidpointRounding.AwayFromZero);
            byte       tolerance = ByteUtil.FastScale(x, x);
            PointInt32 pt        = changes.OriginPointInt32;

            if (!sampleSource.Bounds <ColorBgra>().Contains(pt))
            {
                switch (changes.SelectionCombineMode)
                {
                case SelectionCombineMode.Replace:
                case SelectionCombineMode.Intersect:
                    return(new GeometryList());

                case SelectionCombineMode.Union:
                case SelectionCombineMode.Exclude:
                case SelectionCombineMode.Xor:
                    return(changes.BaseGeometry);
                }
                throw ExceptionUtil.InvalidEnumArgumentException <SelectionCombineMode>(changes.SelectionCombineMode, "changes.SelectionCombineMode");
            }
            if (cancellationToken.IsCancellationRequested)
            {
                return(null);
            }
            Func <bool>       isCancellationRequestedFn = () => cancellationToken.IsCancellationRequested;
            ColorBgra         basis             = sampleSource.GetPointSlow(pt);
            int               width             = ((sampleSource.Width + 0x1f) / 0x20) * 0x20;
            BitVector2D       newStencil        = new BitVector2D(width, sampleSource.Height);
            BitVector2DStruct newStencilWrapper = new BitVector2DStruct(newStencil);

            if (((changes.SelectionCombineMode != SelectionCombineMode.Replace) && sampleSource.Bounds <ColorBgra>().Contains(changes.BaseGeometry.Bounds.Int32Bound)) && changes.BaseGeometry.IsPixelated)
            {
                lazyBaseStencil = LazyResult.New <BitVector2D>(() => PixelatedGeometryListToBitVector2D(changes.BaseGeometry, newStencil.Width, newStencil.Height, cancellationToken), LazyThreadSafetyMode.ExecutionAndPublication, new SingleUseCriticalSection());
                ThreadPool.QueueUserWorkItem(delegate(object _) {
                    lazyBaseStencil.EnsureEvaluated();
                });
            }
            else
            {
                lazyBaseStencil = null;
            }
            FloodMode floodMode = changes.FloodMode;

            if (floodMode != FloodMode.Local)
            {
                if (floodMode != FloodMode.Global)
                {
                    throw ExceptionUtil.InvalidEnumArgumentException <FloodMode>(changes.FloodMode, "changes.FloodMode");
                }
            }
            else
            {
                RectInt32 num4;
                FloodFillAlgorithm.FillStencilFromPoint <BitVector2DStruct>(sampleSource, newStencilWrapper, pt, tolerance, isCancellationRequestedFn, out num4);
                goto Label_0293;
            }
            TileMathHelper tileMathHelper = new TileMathHelper(sampleSource.Width, sampleSource.Height, 7);

            Work.ParallelForEach <PointInt32>(WaitType.Pumping, tileMathHelper.EnumerateTileOffsets(), delegate(PointInt32 tileOffset) {
                if (!cancellationToken.IsCancellationRequested)
                {
                    RectInt32 clipRect = tileMathHelper.GetTileSourceRect(tileOffset);
                    FloodFillAlgorithm.FillStencilByColor <BitVector2DStruct>(sampleSource, newStencilWrapper, basis, tolerance, isCancellationRequestedFn, clipRect);
                }
            }, WorkItemQueuePriority.Normal, null);
Label_0293:
            if (cancellationToken.IsCancellationRequested)
            {
                return(null);
            }
            if (changes.SelectionCombineMode == SelectionCombineMode.Replace)
            {
                list = GeometryList.FromStencil <BitVector2DStruct>(newStencilWrapper, cancellationToken);
            }
            else if (lazyBaseStencil == null)
            {
                GeometryList rhs = GeometryList.FromStencil <BitVector2DStruct>(newStencilWrapper, cancellationToken);
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }
                list = GeometryList.Combine(changes.BaseGeometry, changes.SelectionCombineMode.ToGeometryCombineMode(), rhs);
            }
            else
            {
                BitVector2D other = lazyBaseStencil.Value;
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }
                switch (changes.SelectionCombineMode)
                {
                case SelectionCombineMode.Replace:
                    throw new InternalErrorException();

                case SelectionCombineMode.Union:
                    newStencil.Or(other);
                    break;

                case SelectionCombineMode.Exclude:
                    newStencil.Invert();
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }
                    newStencil.And(other);
                    break;

                case SelectionCombineMode.Intersect:
                    newStencil.And(other);
                    break;

                case SelectionCombineMode.Xor:
                    newStencil.Xor(other);
                    break;

                default:
                    throw ExceptionUtil.InvalidEnumArgumentException <SelectionCombineMode>(changes.SelectionCombineMode, "changes.SelectionCombineMode");
                }
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }
                list = GeometryList.FromStencil <BitVector2DStruct>(newStencilWrapper, cancellationToken);
            }
            if (cancellationToken.IsCancellationRequested)
            {
                return(null);
            }
            list.Freeze();
            return(list);
        }