/// <summary>
        ///     Applies pixels from preview layer to selected layer.
        /// </summary>
        public void ApplyPreviewLayer()
        {
            if (previewLayerChanges == null)
            {
                return;
            }

            foreach (var modifiedLayer in previewLayerChanges)
            {
                Layer layer = Manager.ActiveDocument.Layers.FirstOrDefault(x => x.LayerGuid == modifiedLayer.LayerGuid);

                if (layer != null)
                {
                    BitmapPixelChanges oldValues = ApplyToLayer(layer, modifiedLayer).PixelChanges;

                    BitmapChanged?.Invoke(this, new BitmapChangedEventArgs(
                                              modifiedLayer.PixelChanges,
                                              oldValues,
                                              modifiedLayer.LayerGuid));
                    Manager.ActiveDocument.GeneratePreviewLayer();
                }
            }

            previewLayerChanges = null;
        }
示例#2
0
        public BitmapPixelChanges Draw(Coordinates startingCoords, Coordinates latestCords, Color color, int toolSize)
        {
            LineTool line = new LineTool();

            return(BitmapPixelChanges.FromSingleColoredArray(
                       line.CreateLine(startingCoords, latestCords, toolSize), color));
        }
示例#3
0
        public LayerChange[] Erase(Layer layer, Coordinates[] coordinates, int toolSize)
        {
            Coordinates        startingCords = coordinates.Length > 1 ? coordinates[1] : coordinates[0];
            BitmapPixelChanges pixels        = pen.Draw(startingCords, coordinates[0], System.Windows.Media.Colors.Transparent, toolSize);

            return(Only(pixels, layer));
        }
示例#4
0
        public BitmapPixelChanges ChangeBrightness(Layer layer, Coordinates coordinates, int toolSize,
                                                   float correctionFactor)
        {
            DoubleCords centeredCoords = CoordinatesCalculator.CalculateThicknessCenter(coordinates, toolSize);

            Coordinates[] rectangleCoordinates = CoordinatesCalculator.RectangleToCoordinates(centeredCoords.Coords1.X,
                                                                                              centeredCoords.Coords1.Y,
                                                                                              centeredCoords.Coords2.X, centeredCoords.Coords2.Y);
            BitmapPixelChanges changes = new BitmapPixelChanges(new Dictionary <Coordinates, Color>());

            for (int i = 0; i < rectangleCoordinates.Length; i++)
            {
                if (Mode == BrightnessMode.Default)
                {
                    if (_pixelsVisited.Contains(rectangleCoordinates[i]))
                    {
                        continue;
                    }
                    _pixelsVisited.Add(rectangleCoordinates[i]);
                }

                Color pixel    = layer.GetPixelWithOffset(rectangleCoordinates[i].X, rectangleCoordinates[i].Y);
                Color newColor = ExColor.ChangeColorBrightness(Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B),
                                                               correctionFactor);
                changes.ChangedPixels.Add(new Coordinates(rectangleCoordinates[i].X, rectangleCoordinates[i].Y),
                                          newColor);
            }

            return(changes);
        }
示例#5
0
        public override void AfterAddedUndo(UndoManager undoManager)
        {
            if (currentSelection != null && currentSelection.Length > 0)
            {
                Change changes = undoManager.UndoStack.Peek();

                // Inject to default undo system change custom changes made by this tool
                foreach (var item in startPixelColors)
                {
                    BitmapPixelChanges beforeMovePixels = BitmapPixelChanges.FromArrays(startSelection, item.Value);
                    BitmapPixelChanges afterMovePixels  = BitmapPixelChanges.FromArrays(currentSelection, endPixelColors[item.Key]);
                    Guid layerGuid = item.Key;
                    var  oldValue  = (LayerChange[])changes.OldValue;

                    if (oldValue.Any(x => x.LayerGuid == layerGuid))
                    {
                        var layer = oldValue.First(x => x.LayerGuid == layerGuid);
                        layer.PixelChanges.ChangedPixels.AddRangeOverride(afterMovePixels.ChangedPixels);
                        layer.PixelChanges.ChangedPixels
                        .AddRangeOverride(beforeMovePixels.ChangedPixels);

                        ((LayerChange[])changes.NewValue).First(x => x.LayerGuid == layerGuid).PixelChanges.ChangedPixels
                        .AddRangeNewOnly(BitmapPixelChanges
                                         .FromSingleColoredArray(startSelection, System.Windows.Media.Colors.Transparent)
                                         .ChangedPixels);
                    }
                }
            }
        }
示例#6
0
 public BitmapChangedEventArgs(BitmapPixelChanges pixelsChanged, BitmapPixelChanges oldPixelsValues,
                               int changedLayerIndex)
 {
     PixelsChanged     = pixelsChanged;
     OldPixelsValues   = oldPixelsValues;
     ChangedLayerIndex = changedLayerIndex;
 }
示例#7
0
        public void TestThatGetPixelsForSelectionReturnsCorrectPixels()
        {
            Coordinates[] cords = { new Coordinates(0, 0),
                                    new Coordinates(1, 1),new Coordinates(0,  1), new Coordinates(1, 0) };
            Layer[]       layers = { new Layer("test", 2, 2), new Layer("test2", 2, 2) };

            layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new [] { cords[0] }, Colors.Green));
            layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new [] { cords[1] }, Colors.Red));

            var output = BitmapUtils.GetPixelsForSelection(layers, cords);

            List <Color> colors = new List <Color>();

            foreach (var layerColor in output.ToArray())
            {
                foreach (var color in layerColor.Value)
                {
                    colors.Add(color);
                }
            }
            Assert.Single(colors.Where(x => x == Colors.Green));
            Assert.Single(colors.Where(x => x == Colors.Red));
            Assert.Equal(6, colors.Count(x => x.A == 0)); //6 because layer is 4 pixels,
                                                          //2 * 4 = 8, 2 other color pixels, so 8 - 2 = 6
        }
        public void TestThatFromSingleColoredArrayCreatesCorrectArray()
        {
            Coordinates[]      cords      = { new Coordinates(0, 0), new Coordinates(1, 0), new Coordinates(3, 2) };
            BitmapPixelChanges bmpChanges = BitmapPixelChanges.FromSingleColoredArray(cords, SKColors.Lime);

            Assert.All(bmpChanges.ChangedPixels.Values, changeColor => Assert.Equal(SKColors.Lime, changeColor));
            Assert.True(bmpChanges.WasBuiltAsSingleColored);
        }
示例#9
0
        public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)
        {
            var pixels =
                BitmapPixelChanges.FromSingleColoredArray(
                    CreateLine(coordinates,
                               (int)Toolbar.GetSetting("ToolSize").Value, CapType.Square, CapType.Square), color);

            return(Only(pixels, layer));
        }
示例#10
0
        private BitmapPixelChanges RemoveTransparentPixels(BitmapPixelChanges pixels)
        {
            foreach (var item in pixels.ChangedPixels.Where(x => x.Value.A == 0).ToList())
            {
                pixels.ChangedPixels.Remove(item.Key);
            }

            return(pixels);
        }
        private static PixelChangesController CreateBasicController()
        {
            Coordinates[]          cords      = { new Coordinates(0, 0), new Coordinates(1, 1) };
            PixelChangesController controller = new PixelChangesController();

            controller.AddChanges(new LayerChange(
                                      BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Black), 0),
                                  new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Transparent), 0));
            return(controller);
        }
示例#12
0
        public override LayerChange[] Use(Layer layer, List <Coordinates> coordinates, Color color)
        {
            int thickness             = Toolbar.GetSetting <SizeSetting>("ToolSize").Value;
            BitmapPixelChanges pixels =
                BitmapPixelChanges.FromSingleColoredArray(CreateRectangle(coordinates, thickness), color);

            if (Toolbar.GetSetting <BoolSetting>("Fill").Value)
            {
                Color fillColor = Toolbar.GetSetting <ColorSetting>("FillColor").Value;
                pixels.ChangedPixels.AddRangeOverride(
                    BitmapPixelChanges.FromSingleColoredArray(
                        CalculateFillForRectangle(coordinates[^ 1], coordinates[0], thickness), fillColor)
示例#13
0
        private void ClearSelectedPixels(Layer layer, Coordinates[] selection)
        {
            Guid layerGuid = layer.LayerGuid;

            if (!clearedPixels.ContainsKey(layerGuid) || clearedPixels[layerGuid] == false)
            {
                ViewModelMain.Current.BitmapManager.ActiveDocument.Layers.First(x => x == layer)
                .SetPixels(BitmapPixelChanges.FromSingleColoredArray(selection, System.Windows.Media.Colors.Transparent));

                clearedPixels[layerGuid] = true;
            }
        }
示例#14
0
        private BitmapPixelChanges ForestFire(Layer layer, Coordinates startingCoords, Color newColor)
        {
            List <Coordinates> changedCoords = new List <Coordinates>();

            int width  = ViewModelMain.Current.BitmapManager.ActiveDocument.Width;
            int height = ViewModelMain.Current.BitmapManager.ActiveDocument.Height;

            var visited = new bool[width, height];

            Color colorToReplace = layer.GetPixelWithOffset(startingCoords.X, startingCoords.Y);

            var stack = new Stack <Coordinates>();

            stack.Push(new Coordinates(startingCoords.X, startingCoords.Y));

            while (stack.Count > 0)
            {
                var cords         = stack.Pop();
                var relativeCords = layer.GetRelativePosition(cords);

                if (cords.X < 0 || cords.X > width - 1)
                {
                    continue;
                }

                if (cords.Y < 0 || cords.Y > height - 1)
                {
                    continue;
                }

                if (visited[cords.X, cords.Y])
                {
                    continue;
                }

                if (layer.GetPixel(relativeCords.X, relativeCords.Y) == newColor)
                {
                    continue;
                }

                if (layer.GetPixel(relativeCords.X, relativeCords.Y) == colorToReplace)
                {
                    changedCoords.Add(new Coordinates(cords.X, cords.Y));
                    visited[cords.X, cords.Y] = true;
                    stack.Push(new Coordinates(cords.X, cords.Y - 1));
                    stack.Push(new Coordinates(cords.X, cords.Y + 1));
                    stack.Push(new Coordinates(cords.X - 1, cords.Y));
                    stack.Push(new Coordinates(cords.X + 1, cords.Y));
                }
            }

            return(BitmapPixelChanges.FromSingleColoredArray(changedCoords, newColor));
        }
示例#15
0
        public void ApplyPixels(BitmapPixelChanges pixels, Color color)
        {
            LayerBitmap.Lock();

            foreach (var coords in pixels.ChangedCoordinates)
            {
                LayerBitmap.SetPixel(Math.Clamp(coords.X, 0, Width - 1), Math.Clamp(coords.Y, 0, Height - 1),
                                     color);
            }

            LayerBitmap.Unlock();
        }
示例#16
0
        public override LayerChange[] Use(Layer layer, Coordinates[] coordinates, Color color)
        {
            Coordinates        startingCords = coordinates.Length > 1 ? coordinates[1] : coordinates[0];
            BitmapPixelChanges pixels        = Draw(
                startingCords,
                coordinates[0],
                color,
                toolSizeSetting.Value,
                pixelPerfectSetting.Value,
                ViewModelMain.Current.BitmapManager.ActiveDocument.PreviewLayer);

            return(Only(pixels, layer));
        }
示例#17
0
        private static Tuple <Guid, PixelChangesController> CreateBasicController()
        {
            Coordinates[]          cords      = { new Coordinates(0, 0), new Coordinates(1, 1) };
            PixelChangesController controller = new PixelChangesController();

            Guid guid = Guid.NewGuid();

            controller.AddChanges(
                new LayerChange(
                    BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Black), guid),
                new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Transparent), guid));
            return(new Tuple <Guid, PixelChangesController>(guid, controller));
        }
示例#18
0
        public override LayerChange[] Use(Layer layer, List <Coordinates> coordinates, Color color)
        {
            Coordinates        startingCords = coordinates.Count > 1 ? coordinates[1] : coordinates[0];
            BitmapPixelChanges pixels        = Draw(
                startingCords,
                coordinates[0],
                color,
                toolSizeSetting.Value,
                pixelPerfectSetting.Value,
                BitmapManager.ActiveDocument.PreviewLayer);

            return(Only(pixels, layer));
        }
示例#19
0
        public void TestThatFormArraysWorks()
        {
            Coordinates[] coordinatesArray = { new Coordinates(0, 0), new Coordinates(2, 3), new Coordinates(5, 5) };
            Color[]       colorsArray      = { Colors.Red, Colors.Green, Colors.Blue };
            var           result           = BitmapPixelChanges.FromArrays(coordinatesArray, colorsArray);

            for (int i = 0; i < coordinatesArray.Length; i++)
            {
                var cords = coordinatesArray[i];
                Assert.Equal(colorsArray[i], result.ChangedPixels[cords]);
            }
            Assert.False(result.WasBuiltAsSingleColored);
        }
示例#20
0
        public BitmapPixelChanges MoveSelection(Layer layer, Coordinates[] mouseMove)
        {
            Coordinates end = mouseMove[0];

            currentSelection = TranslateSelection(end);
            if (updateViewModelSelection)
            {
                ViewModelMain.Current.BitmapManager.ActiveDocument.ActiveSelection.SetSelection(currentSelection, SelectionType.New);
            }

            lastMouseMove = end;
            return(BitmapPixelChanges.FromArrays(currentSelection, startPixelColors[layer.LayerGuid]));
        }
示例#21
0
        public void TestThatCombineLayersReturnsCorrectBitmapWithSamePixels()
        {
            Coordinates[] cords  = { new Coordinates(0, 0) };
            Layer[]       layers = { new Layer("test", 2, 2), new Layer("test2", 2, 2) };

            layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Green));

            layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Red));

            var outputBitmap = BitmapUtils.CombineLayers(layers, 2, 2);

            Assert.Equal(Colors.Red, outputBitmap.GetPixel(0, 0));
        }
示例#22
0
        public void TestThatSetPixelsSetsPixels() //This also tests if Dynamic Resize works
        {
            Coordinates[] pixels = { new Coordinates(4, 2), new Coordinates(0, 0), new Coordinates(15, 2), };

            Layer layer = new Layer("layer");

            layer.SetPixels(BitmapPixelChanges.FromSingleColoredArray(pixels, Colors.Green));

            for (int i = 0; i < pixels.Length; i++)
            {
                Assert.Equal(Colors.Green, layer.GetPixelWithOffset(pixels[i].X, pixels[i].Y));
            }
        }
示例#23
0
        public void TestThatCombineLayersReturnsCorrectBitmap()
        {
            Coordinates[] cords  = { new Coordinates(0, 0), new Coordinates(1, 1) };
            Layer[]       layers = { new Layer("test", 2, 2), new Layer("test2", 2, 2) };

            layers[0].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { cords[0] }, Colors.Green));

            layers[1].SetPixels(BitmapPixelChanges.FromSingleColoredArray(new[] { cords[1] }, Colors.Red));

            WriteableBitmap outputBitmap = BitmapUtils.CombineLayers(2, 2, layers);

            Assert.Equal(Colors.Green, outputBitmap.GetPixel(0, 0));
            Assert.Equal(Colors.Red, outputBitmap.GetPixel(1, 1));
        }
示例#24
0
 private void MovePixelsToCheck(BitmapPixelChanges changes)
 {
     if (changes.ChangedPixels[lastChangedPixels[1]].A != 0)
     {
         lastChangedPixels[0] = lastChangedPixels[1];
         lastChangedPixels[1] = lastChangedPixels[2];
         changedPixelsindex   = 2;
     }
     else
     {
         lastChangedPixels[0] = lastChangedPixels[2];
         changedPixelsindex   = 1;
     }
 }
示例#25
0
        private BitmapPixelChanges ApplyPixelPerfectToPixels(Coordinates p1, Coordinates p2, Coordinates p3, Color color, int toolSize)
        {
            if (Math.Abs(p3.X - p1.X) == 1 && Math.Abs(p3.Y - p1.Y) == 1 && !confirmedPixels.Contains(p2))
            {
                var changes = BitmapPixelChanges.FromSingleColoredArray(GetThickShape(new Coordinates[] { p1, p3 }, toolSize), color);
                changes.ChangedPixels.AddRangeNewOnly(
                    BitmapPixelChanges.FromSingleColoredArray(
                        GetThickShape(new[] { p2 }, toolSize),
                        System.Windows.Media.Colors.Transparent).ChangedPixels);
                return(changes);
            }

            return(BitmapPixelChanges.FromSingleColoredArray(GetThickShape(new Coordinates[] { p2, p3 }.Distinct(), toolSize), color));
        }
        public void TestThatAddChangesAddsAsNewChange()
        {
            var controller = CreateBasicController();

            Coordinates[] cords = { new Coordinates(5, 3), new Coordinates(7, 2) };

            controller.AddChanges(new LayerChange(
                                      BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Black), 1),
                                  new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Transparent), 1));

            var changes = controller.PopChanges();

            Assert.Equal(2, changes.Length);
        }
示例#27
0
        public void TestThatCombineCombineOverrideCombinesValues()
        {
            Coordinates[]      cords1   = { new Coordinates(0, 0), new Coordinates(1, 0), new Coordinates(3, 2) };
            Coordinates[]      cords2   = { new Coordinates(3, 2), new Coordinates(0, 0), new Coordinates(5, 5) };
            BitmapPixelChanges changes  = BitmapPixelChanges.FromSingleColoredArray(cords1, Colors.Green);
            BitmapPixelChanges changes2 = BitmapPixelChanges.FromSingleColoredArray(cords2, Colors.Red);

            var output = BitmapPixelChanges.CombineOverride(new[] { changes, changes2 });

            Assert.Equal(4, output.ChangedPixels.Count);
            Assert.Equal(Colors.Red, output.ChangedPixels[new Coordinates(3, 2)]);
            Assert.Equal(Colors.Red, output.ChangedPixels[new Coordinates(0, 0)]);
            Assert.Equal(Colors.Green, output.ChangedPixels[new Coordinates(1, 0)]);
        }
        public void TestThatAddChangesAddsToExistingChange()
        {
            Coordinates[] cords2     = { new Coordinates(2, 2), new Coordinates(5, 5) };
            var           controller = CreateBasicController();

            controller.AddChanges(new LayerChange(
                                      BitmapPixelChanges.FromSingleColoredArray(cords2, Colors.Black), 0),
                                  new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords2, Colors.Transparent), 0));

            var changes = controller.PopChanges();

            Assert.Single(changes);
            Assert.Equal(4, changes[0].Item1.PixelChanges.ChangedPixels.Count);
            Assert.Equal(4, changes[0].Item2.PixelChanges.ChangedPixels.Count);
        }
        public void TestThatBitmapOperationsUtilityDeletesPixels()
        {
            BitmapOperationsUtility util = new BitmapOperationsUtility(new BitmapManager());

            Layer testLayer = new Layer("test layer", 10, 10);

            Coordinates[]      cords  = { new Coordinates(0, 0), new Coordinates(1, 1) };
            BitmapPixelChanges pixels = BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Black);

            testLayer.SetPixels(pixels);

            util.DeletePixels(new [] { testLayer }, cords);

            Assert.Equal(0, testLayer.GetPixel(0, 0).A);
            Assert.Equal(0, testLayer.GetPixel(1, 1).A);
        }
示例#30
0
        public void TestThatAddChangesAddsAsNewChange()
        {
            var data = CreateBasicController();
            PixelChangesController controller = data.Item2;

            Coordinates[] cords = { new Coordinates(5, 3), new Coordinates(7, 2) };
            Guid          guid  = Guid.NewGuid();

            controller.AddChanges(
                new LayerChange(
                    BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Black), guid),
                new LayerChange(BitmapPixelChanges.FromSingleColoredArray(cords, Colors.Transparent), guid));

            System.Tuple <LayerChange, LayerChange>[] changes = controller.PopChanges();
            Assert.Equal(2, changes.Length);
        }