Ejemplo n.º 1
0
        public void TestCount()
        {
            var srcPR = new PixelRgn(_drawable, false, false);
              var iterator = new RegionIterator(srcPR);

              int count = 0;
              iterator.ForEach(src => {count++;});
              Assert.AreEqual(_width * _height, count);
        }
Ejemplo n.º 2
0
        public void TestCount()
        {
            var srcPR    = new PixelRgn(_drawable, false, false);
            var iterator = new RegionIterator(srcPR);

            int count = 0;

            iterator.ForEach(src => { count++; });
            Assert.AreEqual(_width * _height, count);
        }
Ejemplo n.º 3
0
        public new void Update(Func<Pixel, Pixel> func)
        {
            var rectangle = Bounds;

              int rowStride = rectangle.Width * _bpp;
              byte[] buffer = new byte[rectangle.Area * _bpp];

              var srcPR = new PixelRgn(Drawable, rectangle, false, false);

              var iterator = new RegionIterator(srcPR);
              iterator.ForEach(src =>
            {
              int x = src.X;
              int y = src.Y;
              var pixel = func(src);

              int index =
              (y - rectangle.Y1) * rowStride + (x - rectangle.X1) * _bpp;
              pixel.CopyTo(buffer, index);
            });
              DrawBuffer(buffer, rowStride);
        }
Ejemplo n.º 4
0
        public SelectionMask(Selection selection)
        {
            bool selectionNonEmpty;
            _originBounds = selection.Bounds(out selectionNonEmpty);
            if (!selectionNonEmpty)
            {
                _mask = null;
                return;
            }

            _mask = new bool[_originBounds.Width,_originBounds.Height];
            var selRgn = new PixelRgn(selection, _originBounds, false, false);
            var iterator = new RegionIterator(selRgn);

            iterator.ForEach(pixel =>
                {
                    if (pixel.Red > 128)
                    {
                        _area++;
                        _mask[pixel.X - _originBounds.X1, pixel.Y - _originBounds.Y1] = true;
                    }
                });
        }
Ejemplo n.º 5
0
        public new void Update(Func <Pixel, Pixel> func)
        {
            var rectangle = Bounds;

            int rowStride = rectangle.Width * _bpp;

            byte[] buffer = new byte[rectangle.Area * _bpp];

            var srcPR = new PixelRgn(Drawable, rectangle, false, false);

            var iterator = new RegionIterator(srcPR);

            iterator.ForEach(src =>
            {
                int x     = src.X;
                int y     = src.Y;
                var pixel = func(src);

                int index = (y - rectangle.Y1) * rowStride + (x - rectangle.X1) * _bpp;
                pixel.CopyTo(buffer, index);
            });
            DrawBuffer(buffer, rowStride);
        }
Ejemplo n.º 6
0
        public void Render(Image image, Drawable drawable)
        {
            if (image.Selection.Empty)
            {
                return;
            }
            image.UndoGroupStart();
            Tile.CacheDefault(drawable);

            var temp = image.Duplicate();
            var imageRect = image.Bounds;

            var layerPixels = LayerToArray(imageRect, drawable);

            //temp.Selection.Border(1);
            bool selectionNonEmpty;
            var selectionBounds = temp.Selection.Bounds(out selectionNonEmpty);
            var selectionMask = new SelectionMask(temp.Selection);

            //var progress = new Progress(_("Filling the region..."));
            var initArea = selectionMask.Area;

            //init confidance term
            //TODO throw out rgniterator from here
            var pxConfidenceTerm = RegionHelper.FilledMatrix(image.Width, image.Height, 1.0);
            var iter = new RgnIterator(image.Selection, RunMode.Noninteractive);
            iter.IterateSrc(pixel => pxConfidenceTerm[pixel.X, pixel.Y] = selectionMask[pixel.X, pixel.Y] ? 0 : 1);

            int iteration = 0;
            while (!selectionMask.Empty && iteration < _iterations)
            {
                //  initial selection border / target region
                var selectionBorder =
                    RegionHelper.TraceBorder(selectionBounds,
                                             coord => selectionMask[coord.X, coord.Y])
                                .ToList();

                var borderConfidanceValues = NewConfidanceValue(selectionBorder, pxConfidenceTerm, _windowSize);
                var borderGradientValues = GradientValues(selectionBorder, layerPixels, selectionMask, _windowSize).ToList();
                var borderNormals = NormalVectors.CalculateNormals(selectionBorder);
                var borderDataTerms = borderGradientValues.Zip(borderNormals, ComputeDataTerm);
                var borderPriorities = borderConfidanceValues.Zip(borderDataTerms, (c, d) => c * d)
                                                             .ToArray();

                var currentIndex = borderPriorities.MaxIndex();
                var currentPoint = selectionBorder[currentIndex];
                var currentRect = currentPoint.PointCenteredRectangle(_windowSize);
                var currentRegion = new PixelRgn(drawable, currentRect, true, true);

                var minRgnRect = _pixelDistanceCalc.MinDistanceRegion(currentRect, borderGradientValues[currentIndex], layerPixels,
                                                                      selectionMask);
                var minRgn = new PixelRgn(drawable, minRgnRect, false, false);

                var iterator = new RegionIterator(currentRegion, minRgn);
                iterator.ForEach((targetPixel, srcPixel) =>
                    {
                        if (!selectionMask[targetPixel.X, targetPixel.Y])
                        {
                            return;
                        }
                        layerPixels[targetPixel.X, targetPixel.Y] = srcPixel;
                        targetPixel.Set(srcPixel);
                    });
                drawable.Flush();
                drawable.MergeShadow(true);
                drawable.Update(temp.Selection.MaskBounds);

                //update confidance values
                var currentCoords =
                    RegionHelper.Grid(currentRect).Where(coord => selectionMask[coord.X, coord.Y]).ToArray();
                var newConfidanceValues = NewConfidanceValue(currentCoords, pxConfidenceTerm, _windowSize).ToArray();
                foreach (var point in
                    currentCoords.Zip(newConfidanceValues,
                                      (coordinate, confidance) =>
                                      new {Coordinate = coordinate, Confidance = confidance}))
                {
                    pxConfidenceTerm[point.Coordinate.X, point.Coordinate.Y] = point.Confidance;
                }
                // exclude current region pixels from selection
                selectionMask.SetAreaToZero(currentRect);
                //progress.Update((initArea - selectionMask.Area)/(double) initArea);

                iteration++;
                if (iteration == _iterations)
                {
                    selectionBorder =
                    RegionHelper.TraceBorder(selectionBounds,
                                             coord => selectionMask[coord.X, coord.Y])
                                .ToList();
                    var rgn = new PixelRgn(drawable, selectionBounds, true, true);
                    for (int i = 0; i < selectionBorder.Count; i++)
                    {
                        var coord = selectionBorder[i];
                        rgn.SetPixel(new byte[] { (byte)(255), 0, 0, 255 }, coord.X, coord.Y);
                    }

                    drawable.Flush();
                    drawable.MergeShadow(false);
                    drawable.Update(temp.Selection.MaskBounds);
                }
            }
            temp.Delete();
            image.UndoGroupEnd();
        }
Ejemplo n.º 7
0
 private Pixel[,] LayerToArray(Rectangle imageRect, Drawable drawable)
 {
     var pixels = new Pixel[imageRect.Width, imageRect.Height];
     var rgn = new PixelRgn(drawable, imageRect, false, false);
     var iterator = new RegionIterator(rgn);
     iterator.ForEach(pixel =>
                         pixels[pixel.X, pixel.Y] = pixel);
     return pixels;
 }