예제 #1
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
        }
예제 #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 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);
                    }
                }
            }
        }
        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);
        }
예제 #5
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));
        }
        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);
        }
예제 #7
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)
예제 #8
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;
            }
        }
예제 #9
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));
        }
예제 #10
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));
        }
예제 #11
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));
            }
        }
예제 #12
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));
        }
예제 #13
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));
        }
        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);
        }
예제 #15
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));
        }
예제 #16
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);
        }
예제 #19
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);
        }
예제 #20
0
        public BitmapPixelChanges ForestFire(Layer layer, Coordinates startingCoords, Color newColor)
        {
            List <Coordinates> changedCoords = new List <Coordinates>();

            Layer clone  = layer.Clone();
            int   width  = ViewModelMain.Current.BitmapManager.ActiveDocument.Width;
            int   height = ViewModelMain.Current.BitmapManager.ActiveDocument.Height;

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

            var stack = new Stack <Coordinates>();

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

            using (clone.LayerBitmap.GetBitmapContext(ReadWriteMode.ReadWrite))
            {
                while (stack.Count > 0)
                {
                    var cords         = stack.Pop();
                    var relativeCords = clone.GetRelativePosition(cords);

                    if (cords.X < 0 || cords.X > width - 1)
                    {
                        continue;
                    }
                    if (cords.Y < 0 || cords.Y > height - 1)
                    {
                        continue;
                    }
                    if (clone.GetPixel(relativeCords.X, relativeCords.Y) == newColor)
                    {
                        continue;
                    }

                    if (clone.GetPixel(relativeCords.X, relativeCords.Y) == colorToReplace)
                    {
                        changedCoords.Add(new Coordinates(cords.X, cords.Y));
                        clone.SetPixel(new Coordinates(cords.X, cords.Y), newColor);
                        stack.Push(new Coordinates(cords.X, cords.Y - 1));
                        stack.Push(new Coordinates(cords.X + 1, cords.Y));
                        stack.Push(new Coordinates(cords.X, cords.Y + 1));
                        stack.Push(new Coordinates(cords.X - 1, cords.Y));
                    }
                }
            }
            return(BitmapPixelChanges.FromSingleColoredArray(changedCoords, newColor));
        }
예제 #21
0
        public void DeletePixels(Layer[] layers, Coordinates[] pixels)
        {
            var changes   = BitmapPixelChanges.FromSingleColoredArray(pixels, Color.FromArgb(0, 0, 0, 0));
            var oldValues = BitmapUtils.GetPixelsForSelection(layers, pixels);

            LayerChange[] old       = new LayerChange[layers.Length];
            LayerChange[] newChange = new LayerChange[layers.Length];
            for (int i = 0; i < layers.Length; i++)
            {
                old[i] = new LayerChange(
                    BitmapPixelChanges.FromArrays(pixels, oldValues[layers[i]]), i);
                newChange[i] = new LayerChange(changes, i);
                layers[i].SetPixels(changes);
            }

            UndoManager.AddUndoChange(new Change("UndoChanges", old, newChange, "Deleted pixels"));
        }
        public void DeletePixels(Layer[] layers, Coordinates[] pixels)
        {
            if (Manager.ActiveDocument == null)
            {
                return;
            }

            StorageBasedChange change = new StorageBasedChange(Manager.ActiveDocument, layers, true);

            BitmapPixelChanges changes = BitmapPixelChanges.FromSingleColoredArray(pixels, SKColors.Empty);

            for (int i = 0; i < layers.Length; i++)
            {
                Guid guid = layers[i].GuidValue;
                layers[i].SetPixels(changes);
            }

            var args = new object[] { change.Document };

            Manager.ActiveDocument.UndoManager.AddUndoChange(change.ToChange(StorageBasedChange.BasicUndoProcess, args, "Delete selected pixels"));
        }
예제 #23
0
        public override void AfterAddedUndo()
        {
            if (_currentSelection != null && _currentSelection.Length != 0)
            {
                //Inject to default undo system change custom changes made by this tool
                foreach (var item in _startPixelColors)
                {
                    BitmapPixelChanges beforeMovePixels = BitmapPixelChanges.FromArrays(_startSelection, item.Value);
                    Change             changes          = UndoManager.UndoStack.Peek();
                    int layerIndex = ViewModelMain.Current.BitmapManager.ActiveDocument.Layers.IndexOf(item.Key);

                    ((LayerChange[])changes.OldValue).First(x => x.LayerIndex == layerIndex).PixelChanges.ChangedPixels
                    .AddRangeOverride(beforeMovePixels.ChangedPixels);

                    ((LayerChange[])changes.NewValue).First(x => x.LayerIndex == layerIndex).PixelChanges.ChangedPixels
                    .AddRangeNewOnly(BitmapPixelChanges
                                     .FromSingleColoredArray(_startSelection, System.Windows.Media.Colors.Transparent)
                                     .ChangedPixels);
                }
            }
        }
예제 #24
0
        public BitmapPixelChanges Draw(Coordinates startingCoords, Coordinates latestCords, Color color, int toolSize, bool pixelPerfect = false, Layer previewLayer = null)
        {
            if (!pixelPerfect)
            {
                return(BitmapPixelChanges.FromSingleColoredArray(
                           lineTool.CreateLine(startingCoords, latestCords, toolSize), color));
            }

            if (previewLayer != null && previewLayer.GetPixelWithOffset(latestCords.X, latestCords.Y).A > 0)
            {
                confirmedPixels.Add(latestCords);
            }

            var latestPixels = lineTool.CreateLine(startingCoords, latestCords, 1);

            SetPixelToCheck(latestPixels);

            if (changedPixelsindex == 2)
            {
                var changes = ApplyPixelPerfectToPixels(
                    lastChangedPixels[0],
                    lastChangedPixels[1],
                    lastChangedPixels[2],
                    color,
                    toolSize);

                MovePixelsToCheck(changes);

                changes.ChangedPixels.AddRangeNewOnly(
                    BitmapPixelChanges.FromSingleColoredArray(GetThickShape(latestPixels, toolSize), color).ChangedPixels);

                return(changes);
            }

            changedPixelsindex += changedPixelsindex >= 2 ? 0 : 1;

            var result = BitmapPixelChanges.FromSingleColoredArray(GetThickShape(latestPixels, toolSize), color);

            return(result);
        }
예제 #25
0
        public void DeletePixels(Layer[] layers, Coordinates[] pixels)
        {
            if (Manager.ActiveDocument == null)
            {
                return;
            }

            BitmapPixelChanges         changes   = BitmapPixelChanges.FromSingleColoredArray(pixels, Color.FromArgb(0, 0, 0, 0));
            Dictionary <Guid, Color[]> oldValues = BitmapUtils.GetPixelsForSelection(layers, pixels);

            LayerChange[] old       = new LayerChange[layers.Length];
            LayerChange[] newChange = new LayerChange[layers.Length];
            for (int i = 0; i < layers.Length; i++)
            {
                Guid guid = layers[i].LayerGuid;
                old[i] = new LayerChange(
                    BitmapPixelChanges.FromArrays(pixels, oldValues[layers[i].LayerGuid]), guid);
                newChange[i] = new LayerChange(changes, guid);
                layers[i].SetPixels(changes);
            }

            Manager.ActiveDocument.UndoManager.AddUndoChange(new Change("UndoChanges", old, newChange, "Deleted pixels"));
        }
예제 #26
0
 public override LayerChange[] Use(Layer layer, Coordinates[] mouseMove, Color color)
 {
     return(Only(
                BitmapPixelChanges.FromSingleColoredArray(new[] { mouseMove[0] }, color), 0));
 }