Пример #1
0
        private static ColorBgra GetBasisColor(RecolorToolChanges changes, IRenderer <ColorBgra> sampleSource)
        {
            PointInt32 pt = PointDouble.Truncate(changes.InputPoints.First <BrushInputPoint>().Location);

            if (sampleSource.Bounds <ColorBgra>().Contains(pt))
            {
                return(sampleSource.GetPointSlow(pt));
            }
            return(ColorBgra.TransparentBlack);
        }
Пример #2
0
        private static ISurface <ColorAlpha8> UseTileOrToSurfaceWithEdgePadding(IRenderer <ColorAlpha8> source, RectInt32 bounds, ColorAlpha8 padding)
        {
            if (source.Bounds <ColorAlpha8>().Contains(bounds))
            {
                return(source.UseTileOrToSurface(bounds));
            }
            ISurface <ColorAlpha8> surface = SurfaceAllocator.Alpha8.Allocate <ColorAlpha8>(bounds.Size, AllocationOptions.ZeroFillNotRequired);

            surface.Clear(padding);
            RectInt32  num2     = RectInt32.Intersect(source.Bounds <ColorAlpha8>(), bounds);
            PointInt32 location = new PointInt32(num2.Location.X - bounds.Location.X, num2.Location.Y - bounds.Location.Y);
            RectInt32  num4     = new RectInt32(location, num2.Size);

            using (ISurfaceWindow <ColorAlpha8> window = surface.CreateWindow <ColorAlpha8>(num4))
            {
                source.Render(window, num2.Location);
            }
            return(surface);
        }
Пример #3
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);
        }
        public bool PerformAction()
        {
            bool flag = true;

            if (!this.QueryCanPerformAction())
            {
                return(false);
            }
            try
            {
                using (new WaitCursorChanger(this.documentWorkspace))
                {
                    IRenderer <ColorBgra>   source             = this.GetSource();
                    RectInt32               num                = source.Bounds <ColorBgra>();
                    GeometryList            cachedClippingMask = this.documentWorkspace.Selection.GetCachedClippingMask();
                    RectInt32               a = cachedClippingMask.Bounds.Int32Bound;
                    IRenderer <ColorAlpha8> cachedClippingMaskRenderer = this.documentWorkspace.Selection.GetCachedClippingMaskRenderer(this.documentWorkspace.ToolSettings.Selection.RenderingQuality.Value);
                    RectInt32               sourceRect = RectInt32.Intersect(a, source.Bounds <ColorBgra>());
                    IRenderer <ColorBgra>   renderer3  = new ClippedRenderer <ColorBgra>(source, sourceRect);
                    IRenderer <ColorAlpha8> alpha      = new ClippedRenderer <ColorAlpha8>(cachedClippingMaskRenderer, sourceRect);
                    IRenderer <ColorBgra>   sourceRHS  = new MultiplyAlphaChannelRendererBgra32(renderer3, alpha);
                    IRenderer <ColorBgra>   sourceLHS  = new SolidColorRendererBgra(sourceRHS.Width, sourceRHS.Height, ColorBgra.White);
                    IRenderer <ColorBgra>   renderer   = new BlendRendererBgra(sourceLHS, CompositionOps.Normal.Static, sourceRHS);
                    if ((a.Width > 0) && (a.Height > 0))
                    {
                        int num5 = 10;
                        while (num5 >= 0)
                        {
                            try
                            {
                                try
                                {
                                    using (Clipboard.Transaction transaction = Clipboard.Open(this.documentWorkspace))
                                    {
                                        transaction.Empty();
                                        using (MaskedSurface surface = MaskedSurface.CopyFrom(source, cachedClippingMask))
                                        {
                                            transaction.AddManagedData(surface);
                                            using (Surface surface2 = surface.Surface.CreateWindow(new Rectangle(0, 0, a.Width, a.Height)))
                                            {
                                                sourceRHS.Parallelize <ColorBgra>(TilingStrategy.Tiles, 7, WorkItemQueuePriority.Normal).Render <ColorBgra>(surface2);
                                                using (Bitmap copyBitmap = surface2.CreateAliasedBitmap(true))
                                                {
                                                    transaction.AddRawNativeData("PNG", delegate(Stream dstStream) {
                                                        copyBitmap.Save(dstStream, ImageFormat.Png);
                                                    });
                                                }
                                                renderer.Parallelize <ColorBgra>(TilingStrategy.Tiles, 7, WorkItemQueuePriority.Normal).Render <ColorBgra>(surface2);
                                                using (Bitmap bitmap = surface2.CreateAliasedBitmap(false))
                                                {
                                                    transaction.AddDibV5(bitmap);
                                                }
                                            }
                                            goto Label_0292;
                                        }
                                    }
                                }
                                catch (Exception exception)
                                {
                                    if (num5 == 0)
                                    {
                                        flag = false;
                                        ExceptionDialog.ShowErrorDialog(this.documentWorkspace, PdnResources.GetString("CopyAction.Error.TransferToClipboard"), exception.ToString());
                                    }
                                    else
                                    {
                                        CleanupManager.RequestCleanup();
                                        CleanupManager.WaitForPendingCleanup(50);
                                        Thread.Sleep(50);
                                    }
                                }
                                continue;
                            }
                            finally
                            {
                                num5--;
                            }
                        }
                    }
                }
            }
            catch (OutOfMemoryException exception2)
            {
                flag = false;
                ExceptionDialog.ShowErrorDialog(this.documentWorkspace, PdnResources.GetString("CopyAction.Error.OutOfMemory"), exception2.ToString());
            }
            catch (Exception exception3)
            {
                flag = false;
                ExceptionDialog.ShowErrorDialog(this.documentWorkspace, PdnResources.GetString("CopyAction.Error.Generic"), exception3.ToString());
            }
Label_0292:
            CleanupManager.RequestCleanup();
            return(flag);
        }