protected sealed override HistoryMemento OnCommitChanges(TChanges changes)
        {
            string commitChangesHistoryMementoName = this.GetCommitChangesHistoryMementoName(changes);

            if (this.isCreatingSelection && (changes != this.createSelectionChanges))
            {
                this.InvalidateSelection();
            }
            DateTime utcNow = DateTime.UtcNow;
            TimeSpan span   = TimeSpan.FromSeconds(1.0);
            bool     flag   = false;

            do
            {
                if (this.PollForSelection(1))
                {
                    flag = true;
                    break;
                }
            }while ((DateTime.UtcNow - utcNow) < span);
            if (!flag)
            {
                VirtualTask <Unit> selectionTask = base.DocumentWorkspace.TaskManager.CreateVirtualTask(TaskState.Running);
                selectionTask.Progress = null;
                using (TaskProgressDialog dialog = new TaskProgressDialog())
                {
                    dialog.Task             = selectionTask;
                    dialog.CloseOnFinished  = true;
                    dialog.ShowCancelButton = false;
                    dialog.Text             = base.Name;
                    dialog.Icon             = base.Image.Reference.ToIcon();
                    dialog.HeaderText       = PdnResources.GetString("SaveConfigDialog.Finishing.Text");
                    using (System.Windows.Forms.Timer pollTimer = new System.Windows.Forms.Timer())
                    {
Beispiel #2
0
        public override HistoryMemento PerformAction(DocumentWorkspace docWorkspace)
        {
            int                 newWidth;
            int                 newHeight;
            MeasurementUnit     newDpuUnit;
            double              newDpu;
            ResamplingAlgorithm algorithm = AppSettings.Instance.Workspace.LastResamplingMethod.Value;

            if (algorithm == ResamplingAlgorithm.SuperSampling)
            {
                algorithm = ResamplingAlgorithm.Fant;
            }
            bool flag = AppSettings.Instance.Workspace.LastMaintainAspectRatio.Value;

            using (ResizeDialog dialog = new ResizeDialog())
            {
                dialog.OriginalSize        = docWorkspace.Document.Size;
                dialog.OriginalDpuUnit     = docWorkspace.Document.DpuUnit;
                dialog.OriginalDpu         = docWorkspace.Document.DpuX;
                dialog.ImageHeight         = docWorkspace.Document.Height;
                dialog.ImageWidth          = docWorkspace.Document.Width;
                dialog.ResamplingAlgorithm = algorithm;
                dialog.LayerCount          = docWorkspace.Document.Layers.Count;
                dialog.Units             = dialog.OriginalDpuUnit;
                dialog.Resolution        = docWorkspace.Document.DpuX;
                dialog.Units             = AppSettings.Instance.Workspace.LastNonPixelUnits.Value;
                dialog.ConstrainToAspect = flag;
                if (dialog.ShowDialog(docWorkspace) == DialogResult.Cancel)
                {
                    return(null);
                }
                AppSettings.Instance.Workspace.LastResamplingMethod.Value    = dialog.ResamplingAlgorithm;
                AppSettings.Instance.Workspace.LastMaintainAspectRatio.Value = dialog.ConstrainToAspect;
                newDpuUnit = dialog.Units;
                newWidth   = dialog.ImageWidth;
                newHeight  = dialog.ImageHeight;
                newDpu     = dialog.Resolution;
                algorithm  = dialog.ResamplingAlgorithm;
                if (newDpuUnit != MeasurementUnit.Pixel)
                {
                    AppSettings.Instance.Workspace.LastNonPixelUnits.Value = newDpuUnit;
                    if (docWorkspace.AppWorkspace.Units != MeasurementUnit.Pixel)
                    {
                        docWorkspace.AppWorkspace.Units = newDpuUnit;
                    }
                }
                if (((docWorkspace.Document.Size == new Size(dialog.ImageWidth, dialog.ImageHeight)) && (docWorkspace.Document.DpuX == newDpu)) && (docWorkspace.Document.DpuUnit == newDpuUnit))
                {
                    return(null);
                }
            }
            if ((newWidth == docWorkspace.Document.Width) && (newHeight == docWorkspace.Document.Height))
            {
                MetadataHistoryMemento memento2 = new MetadataHistoryMemento(StaticName, PdnResources.GetImageResource(StaticImageName), docWorkspace);
                docWorkspace.Document.DpuUnit = newDpuUnit;
                docWorkspace.Document.DpuX    = newDpu;
                docWorkspace.Document.DpuY    = newDpu;
                return(memento2);
            }
            docWorkspace.FlushTool();
            using (TaskProgressDialog progressDialog = new TaskProgressDialog())
            {
Beispiel #3
0
        public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace)
        {
            if (historyWorkspace.Selection.IsEmpty)
            {
                return(null);
            }
            SelectionHistoryMemento             memento      = new SelectionHistoryMemento(StaticName, StaticImage, historyWorkspace);
            GeometryList                        selectedPath = historyWorkspace.Selection.GetCachedGeometryList();
            SelectionRenderingQuality           selectionRenderingQuality = historyWorkspace.ToolSettings.Selection.RenderingQuality.Value;
            Result <IReadOnlyList <RectInt32> > selectedPathScansLazy     = historyWorkspace.Selection.GetCachedLazyClippingMaskScans();
            RectInt32           documentBounds = historyWorkspace.Document.Bounds();
            Func <GeometryList> invertedPathFn = delegate {
                if ((selectionRenderingQuality == SelectionRenderingQuality.Aliased) || selectedPath.IsPixelated)
                {
                    GeometryList list2 = GeometryList.FromNonOverlappingSortedScans(selectedPathScansLazy.Value);
                    list2.AddRect(documentBounds);
                    SegmentedList <RectInt32> scans = new SegmentedList <RectInt32>();
                    foreach (RectInt32 num in list2.EnumerateInteriorScans())
                    {
                        if (documentBounds.Contains(num))
                        {
                            scans.Add(num);
                        }
                        else if (documentBounds.IntersectsWith(num))
                        {
                            scans.Add(RectInt32.Intersect(documentBounds, num));
                        }
                    }
                    return(GeometryList.FromNonOverlappingScans(scans));
                }
                GeometryList lhs = documentBounds.Contains(selectedPath.Bounds) ? selectedPath : GeometryList.ClipToRect(selectedPath, documentBounds);
                return(GeometryList.Combine(lhs, GeometryCombineMode.Xor, documentBounds));
            };
            ThreadTask <GeometryList> task = historyWorkspace.TaskManager.StartNewThreadTask <GeometryList>(task => invertedPathFn(), ApartmentState.MTA);
            ManualResetEvent          taskFinishedEvent = new ManualResetEvent(false);

            task.ResultAsync <GeometryList>().Receive(delegate(Result <GeometryList> r) {
                taskFinishedEvent.Set();
            }).Observe();
            if (!taskFinishedEvent.WaitOne(0x3e8))
            {
                using (TaskProgressDialog dialog = new TaskProgressDialog())
                {
                    dialog.Task       = task;
                    dialog.Text       = StaticName;
                    dialog.Icon       = StaticImage.Reference.ToIcon();
                    dialog.HeaderText = PdnResources.GetString("SaveConfigDialog.Finishing.Text");
                    dialog.ShowDialog(historyWorkspace.Window);
                }
            }
            Result <GeometryList> taskResult = task.TaskResult;

            if (taskResult.IsError)
            {
                if (taskResult.Error is OutOfMemoryException)
                {
                    throw new OutOfMemoryException(null, taskResult.Error);
                }
                throw new AggregateException(null, taskResult.Error);
            }
            GeometryList geometry = task.TaskResult.Value;

            base.EnterCriticalRegion();
            using (historyWorkspace.Selection.UseChangeScope())
            {
                historyWorkspace.Selection.Reset();
                geometry.Freeze();
                historyWorkspace.Selection.SetContinuation(geometry, SelectionCombineMode.Replace);
                historyWorkspace.Selection.CommitContinuation();
            }
            return(memento);
        }
Beispiel #4
0
        public override HistoryMemento OnExecute(IHistoryWorkspace historyWorkspace)
        {
            historyWorkspace.VerifyAccess();
            BitmapLayer       layer               = (BitmapLayer)historyWorkspace.Document.Layers[this.layerIndex];
            Surface           layerSurface        = layer.Surface;
            TileMathHelper    tileMathHelper      = new TileMathHelper(layerSurface.Width, layerSurface.Height, this.rendererTileEdgeLog2);
            TileMathHelper    tileGroupMathHelper = new TileMathHelper(layerSurface.Width, layerSurface.Height, this.rendererTileEdgeLog2 + 3);
            HistoryMemento    actionHM            = null;
            ManualResetEvent  taskFinishedEvent   = new ManualResetEvent(false);
            ThreadTask <Unit> commitTask          = historyWorkspace.TaskManager.StartNewThreadTask(delegate(Task task) {
                SegmentedList <RectInt32> list = tileGroupMathHelper.EnumerateTilesClippedToSourceRect(this.rendererClipRect).ToSegmentedList <RectInt32>();
                ListUtil.FisherYatesShuffle <RectInt32>(list);
                ConcurrentQueue <ISurface <ColorBgra> > queue = new ConcurrentQueue <ISurface <ColorBgra> >();
                ConcurrentQueue <RectInt32> changedTiles      = new ConcurrentQueue <RectInt32>();
                ConcurrentQueue <TupleStruct <RectInt32, ISurface <ColorBgra> > > changedTileGroups = new ConcurrentQueue <TupleStruct <RectInt32, ISurface <ColorBgra> > >();
                double progressIncrement = 1.0 / ((double)Math.Max(1, list.Count));
                task.Progress            = 0.0;
                Work.ParallelForEach <RectInt32>(WaitType.Blocking, list, delegate(RectInt32 tileGroupRect) {
                    ISurface <ColorBgra> dst = RetryManager.RunMemorySensitiveOperation <ISurfaceRef <ColorBgra> >(() => SurfaceAllocator.Bgra.Allocate <ColorBgra>(tileGroupRect.Size, AllocationOptions.ZeroFillNotRequired));
                    this.renderer.Render(dst, tileGroupRect.Location);
                    bool flag = false;
                    foreach (RectInt32 num in tileMathHelper.EnumerateTilesClippedToSourceRect(tileGroupRect))
                    {
                        RectInt32 bounds = RectInt32.Offset(num, -tileGroupRect.Location);
                        using (ISurface <ColorBgra> surface2 = layerSurface.CreateWindow(num))
                        {
                            using (ISurface <ColorBgra> surface3 = dst.CreateWindow <ColorBgra>(bounds))
                            {
                                if (!SurfaceBgraUtil.ArePixelsEqual(surface2, surface3))
                                {
                                    flag = true;
                                    changedTiles.Enqueue(num);
                                }
                            }
                        }
                    }
                    if (flag)
                    {
                        changedTileGroups.Enqueue(TupleStruct.Create <RectInt32, ISurface <ColorBgra> >(tileGroupRect, dst));
                    }
                    else
                    {
                        DisposableUtil.Free <ISurface <ColorBgra> >(ref dst);
                    }
                    task.IncrementProgressBy(progressIncrement);
                }, WorkItemQueuePriority.Normal, null);
                task.Progress = null;
                if (changedTiles.Count == 0)
                {
                    actionHM = null;
                }
                else
                {
                    SegmentedList <RectInt32> scans = new SegmentedList <RectInt32>(changedTiles.Count, 7);
                    scans.AddRange(changedTiles);
                    ScansHelpers.SortScansByTopLeft(scans);
                    ScansHelpers.ConsolidateSortedScansInPlace(scans);
                    actionHM = new BitmapHistoryMemento(this.historyMementoName, this.historyMementoImage, historyWorkspace, this.layerIndex, scans);
                    this.EnterCriticalRegion();
                    Work.ParallelForEach <TupleStruct <RectInt32, ISurface <ColorBgra> > >(WaitType.Blocking, changedTileGroups, delegate(TupleStruct <RectInt32, ISurface <ColorBgra> > tileInfo) {
                        using (ISurface <ColorBgra> surface = layerSurface.CreateWindow(tileInfo.Item1))
                        {
                            tileInfo.Item2.Render(surface, PointInt32.Zero);
                        }
                        tileInfo.Item2.Dispose();
                    }, WorkItemQueuePriority.Normal, null);
                    foreach (RectInt32 num in scans)
                    {
                        layer.Invalidate(num);
                    }
                }
            }, ApartmentState.MTA);

            commitTask.ResultAsync <Unit>().Receive(delegate(Result <Unit> r) {
                taskFinishedEvent.Set();
            }).Observe();
            if (!taskFinishedEvent.WaitOne(this.delayUntilProgressDialogMs))
            {
                string headerText       = PdnResources.GetString("ApplyRendererToBitmapLayerHistoryFunction.ProgressDialog.HeaderText");
                string headerTextFormat = PdnResources.GetString("ApplyRendererToBitmapLayerHistoryFunction.ProgressDialog.HeaderText.Format");
                using (TaskProgressDialog progressDialog = new TaskProgressDialog())
                {
                    Action updateHeaderText = delegate {
                        progressDialog.VerifyAccess();
                        if (!progressDialog.IsDisposed)
                        {
                            string text1;
                            double?progress = commitTask.Progress;
                            if (!progress.HasValue)
                            {
                                text1 = headerText;
                            }
                            else
                            {
                                text1 = string.Format(headerTextFormat, (progress.Value * 100.0).ToString("N0"));
                            }
                            progressDialog.HeaderText = text1;
                        }
                    };
                    progressDialog.Text             = this.historyMementoName;
                    progressDialog.Icon             = this.historyMementoImage.Reference.ToIcon();
                    progressDialog.CloseOnFinished  = true;
                    progressDialog.ShowCancelButton = false;
                    progressDialog.Task             = commitTask;
                    updateHeaderText();
                    commitTask.ProgressChanged += delegate(object s, ValueEventArgs <double?> e) {
                        PdnSynchronizationContext.Instance.EnsurePosted(updateHeaderText);
                    };
                    progressDialog.ShowDialog(historyWorkspace.Window);
                }
            }
            if (!commitTask.TaskResult.IsError)
            {
                return(actionHM);
            }
            if (commitTask.TaskResult.Error is OutOfMemoryException)
            {
                throw new OutOfMemoryException(null, commitTask.TaskResult.Error);
            }
            throw new AggregateException(null, commitTask.TaskResult.Error);
        }