Ejemplo n.º 1
0
        public void Rowstride()
        {
            var rgn = new PixelRgn(_drawable, false, false);

            PixelRgn.Register(rgn);
            Assert.AreEqual(_drawable.Bpp * Gimp.TileWidth, rgn.Rowstride);
        }
Ejemplo n.º 2
0
        public void IterateDest(Func<Pixel> func)
        {
            int total_area = _rectangle.Area;
              int area_so_far = 0;

              var destPR = new PixelRgn(_drawable, _rectangle, true, true);
              for (IntPtr pr = PixelRgn.Register(destPR); pr != IntPtr.Zero;
               pr = PixelRgn.Process(pr))
            {
              for (int y = destPR.Y; y < destPR.Y + destPR.H; y++)
            {
              for (int x = destPR.X; x < destPR.X + destPR.W; x++)
            {
              destPR[y, x] = func();
            }
            }
              if (_runmode != RunMode.Noninteractive)
            {
              area_so_far += destPR.W * destPR.H;
              Progress.Update((double) area_so_far / (double) total_area);
            }
            }
              _drawable.Flush();
              _drawable.MergeShadow(true);
              _drawable.Update(_rectangle);
        }
Ejemplo n.º 3
0
        public void IterateDest(Func <int, int, Pixel> func)
        {
            int total_area  = _rectangle.Area;
            int area_so_far = 0;

            var destPR = new PixelRgn(_drawable, _rectangle, true, true);

            for (IntPtr pr = PixelRgn.Register(destPR); pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                for (int y = destPR.Y; y < destPR.Y + destPR.H; y++)
                {
                    for (int x = destPR.X; x < destPR.X + destPR.W; x++)
                    {
                        destPR[y, x] = func(x, y);
                    }
                }
                if (_runmode != RunMode.Noninteractive)
                {
                    area_so_far += destPR.W * destPR.H;
                    Progress.Update((double)area_so_far / (double)total_area);
                }
            }
            _drawable.Flush();
            _drawable.MergeShadow(true);
            _drawable.Update(_rectangle);
        }
Ejemplo n.º 4
0
        public void DirectAccessRgba()
        {
            var image = new Image(_width, _height, ImageBaseType.Rgb)
            {
                { new Layer("test", ImageType.Rgba), 0 }
            };

            var drawable = image.ActiveDrawable;
            var pixel    = new Pixel(13, 24, 35, 128);

            FillDrawable(drawable, pixel);

            var rgn = new PixelRgn(drawable, false, false);

            for (IntPtr pr = PixelRgn.Register(rgn); pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                for (int y = rgn.Y; y < rgn.Y + rgn.H; y++)
                {
                    for (int x = rgn.X; x < rgn.X + rgn.W; x++)
                    {
                        Assert.AreEqual(pixel.Color, rgn[y, x].Color);
                    }
                }
            }
            image.Delete();
        }
Ejemplo n.º 5
0
        public void Draw(Drawable drawable)
        {
            var rgn = new PixelRgn(drawable, false, false);

              var rectangle = drawable.Bounds;
              var buf = rgn.GetRect(rectangle);
              Draw(rectangle, drawable.Type, buf, rectangle.Width * drawable.Bpp);
        }
Ejemplo n.º 6
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.º 7
0
        public void Draw(Drawable drawable)
        {
            var rgn = new PixelRgn(drawable, false, false);

            var rectangle = drawable.Bounds;
            var buf       = rgn.GetRect(rectangle);

            Draw(rectangle, drawable.Type, buf, rectangle.Width * drawable.Bpp);
        }
Ejemplo n.º 8
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.º 9
0
        public void GetSetPixel()
        {
            var rgn   = new PixelRgn(_drawable, false, false);
            var pixel = new byte[] { 13, 24, 35 };

            rgn.SetPixel(pixel, 13, 14);

            var result = new byte[_drawable.Bpp];

            rgn.GetPixel(result, 13, 14);

            Assert.AreEqual(pixel, result);
        }
Ejemplo n.º 10
0
        public void CopyRgb2Rgba()
        {
            // Fill src region

            var pixel = new Pixel(13, 24, 35);

            FillDrawable(_drawable, pixel);

            // Copy to dest region
            var image = new Image(_width, _height, ImageBaseType.Rgb)
            {
                { new Layer("test", ImageType.Rgba), 0 }
            };
            var drawable = image.ActiveDrawable;

            var srcRgn  = new PixelRgn(_drawable, false, false);
            var destRgn = new PixelRgn(drawable, true, false);

            for (IntPtr pr = PixelRgn.Register(srcRgn, destRgn); pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                for (int y = srcRgn.Y; y < srcRgn.Y + srcRgn.H; y++)
                {
                    for (int x = srcRgn.X; x < srcRgn.X + srcRgn.W; x++)
                    {
                        var tmp = srcRgn[y, x];
                        destRgn[y, x] = new Pixel(tmp.Red, tmp.Green, tmp.Blue, 255);
                    }
                }
            }

            // Check results

            var pixel2 = new Pixel(13, 24, 35, 255);

            srcRgn  = new PixelRgn(_drawable, false, false);
            destRgn = new PixelRgn(drawable, false, false);
            for (IntPtr pr = PixelRgn.Register(srcRgn, destRgn); pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                for (int y = srcRgn.Y; y < srcRgn.Y + srcRgn.H; y++)
                {
                    for (int x = srcRgn.X; x < srcRgn.X + srcRgn.W; x++)
                    {
                        Assert.AreEqual(pixel.Color, srcRgn[y, x].Color);
                        Assert.AreEqual(pixel2.Color, destRgn[y, x].Color);
                    }
                }
            }
            image.Delete();
        }
Ejemplo n.º 11
0
        void FillDrawable(Drawable drawable, Pixel pixel)
        {
            var rgn = new PixelRgn(drawable, true, false);

            for (IntPtr pr = PixelRgn.Register(rgn); pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                for (int y = rgn.Y; y < rgn.Y + rgn.H; y++)
                {
                    for (int x = rgn.X; x < rgn.X + rgn.W; x++)
                    {
                        rgn[y, x] = pixel;
                    }
                }
            }
        }
Ejemplo n.º 12
0
        public void GetSetRect()
        {
            var rgn  = new PixelRgn(_drawable, true, false);
            var rect = new byte[_drawable.Bpp * _width * _height];

            for (int i = 0; i < _drawable.Bpp * _width * _height; i++)
            {
                rect[i] = 13;
            }

            rgn.SetRect(rect, 0, 0, _width, _height);

            var result = rgn.GetRect(0, 0, _width, _height);

            Assert.AreEqual(rect, result);
        }
Ejemplo n.º 13
0
        public IEnumerator <Pixel> GetEnumerator()
        {
            var srcPR = new PixelRgn(_drawable, _drawable.MaskBounds, false, false);

            for (IntPtr pr = PixelRgn.Register(srcPR); pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                for (int y = srcPR.Y; y < srcPR.Y + srcPR.H; y++)
                {
                    for (int x = srcPR.X; x < srcPR.X + srcPR.W; x++)
                    {
                        yield return(srcPR[y, x]);
                    }
                }
            }
        }
Ejemplo n.º 14
0
        // Fix me: there could be some overhead in reading from PixelRgn's that
        // are write-only! This could be solved by introducing Read/Write/ReadWrite
        // regions

        public void ForEach(Action <Pixel> func)
        {
            var rgn = _regions[0];

            for (IntPtr pr = PixelRgn.Register(rgn); pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                for (int y = rgn.Y; y < rgn.Y + rgn.H; y++)
                {
                    for (int x = rgn.X; x < rgn.X + rgn.W; x++)
                    {
                        func(rgn[y, x]);
                    }
                }
            }
        }
        public IEnumerator<Pixel> GetEnumerator()
        {
            var srcPR = new PixelRgn(_drawable, _drawable.MaskBounds, false, false);

              for (IntPtr pr = PixelRgn.Register(srcPR); pr != IntPtr.Zero;
               pr = PixelRgn.Process(pr))
            {
              for (int y = srcPR.Y; y < srcPR.Y + srcPR.H; y++)
            {
              for (int x = srcPR.X; x < srcPR.X + srcPR.W; x++)
            {
              yield return srcPR[y, x];
            }
            }
            }
        }
Ejemplo n.º 16
0
        public void CopyRgb2Rgb()
        {
            // Fill src region

              var pixel = new Pixel(13, 24, 35);
              FillDrawable(_drawable, pixel);

              // Copy to dest region
              var image = new Image(_width, _height, ImageBaseType.Rgb) {
            {new Layer("test", ImageType.Rgb), 0}};
              var drawable = image.ActiveDrawable;

              var srcRgn = new PixelRgn(_drawable, false, false);
              var destRgn = new PixelRgn(drawable, true, false);

              for (IntPtr pr = PixelRgn.Register(srcRgn, destRgn); pr != IntPtr.Zero;
               pr = PixelRgn.Process(pr))
            {
              for (int y = srcRgn.Y; y < srcRgn.Y + srcRgn.H; y++)
            {
              for (int x = srcRgn.X; x < srcRgn.X + srcRgn.W; x++)
            {
              destRgn[y, x] = srcRgn[y, x];
            }
            }
            }

              // Check results

              srcRgn = new PixelRgn(_drawable, false, false);
              destRgn = new PixelRgn(drawable, false, false);
              for (IntPtr pr = PixelRgn.Register(srcRgn, destRgn); pr != IntPtr.Zero;
               pr = PixelRgn.Process(pr))
            {
              for (int y = srcRgn.Y; y < srcRgn.Y + srcRgn.H; y++)
            {
              for (int x = srcRgn.X; x < srcRgn.X + srcRgn.W; x++)
            {
              Assert.AreEqual(pixel.Color, srcRgn[y, x].Color);
              Assert.AreEqual(pixel.Color, destRgn[y, x].Color);
              Assert.AreEqual(srcRgn[y, x].Color, destRgn[y, x].Color);
            }
            }
            }

              image.Delete();
        }
Ejemplo n.º 17
0
        public void CountTiles()
        {
            var rgn = new PixelRgn(_drawable, false, false);

            int tw     = (int)Gimp.TileWidth;
            int th     = (int)Gimp.TileHeight;
            int htiles = (_width + tw - 1) / tw;
            int vtiles = (_height + th - 1) / th;

            int count = 0;

            for (IntPtr pr = PixelRgn.Register(rgn); pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                count++;
            }
            Assert.AreEqual(htiles * vtiles, count);
        }
Ejemplo n.º 18
0
        public void ForEach(Action <Pixel, Pixel> func)
        {
            var rgn1 = _regions[0];
            var rgn2 = _regions[1];

            for (IntPtr pr = PixelRgn.Register(rgn1, rgn2); pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                for (int y1 = rgn1.Y, y2 = rgn2.Y; y1 < rgn1.Y + rgn1.H; y1++, y2++)
                {
                    for (int x1 = rgn1.X, x2 = rgn2.X; x1 < rgn1.X + rgn1.W;
                         x1++, x2++)
                    {
                        func(rgn1[y1, x1], rgn2[y2, x2]);
                    }
                }
            }
        }
        public Rectangle MinDistanceRegion(Rectangle sourceRect, Drawable drawable, SelectionMask selectionMask)
        {
            Debug.Assert(sourceRect.Width == sourceRect.Height);

            var windowSize = sourceRect.Width;
            var windowDelta = windowSize/2;
            Rectangle minRegionRect = null;

            var minDistance = 0.0;

            var sourceRgn = new PixelRgn(drawable, sourceRect.AdjustedToImageEdges(drawable.Bounds), false, false);
            var sourceArray = sourceRgn.ToArrayWithoutSelection(selectionMask);

            var currentCoordinate = new IntCoordinate(0, 0);
            for (int i = windowDelta; i < drawable.Width - windowDelta; i++)
            {
                for (int j = windowDelta; j < drawable.Height - windowDelta; j++)
                {
                    if (j == sourceRect.Y1 + windowDelta && i == sourceRect.X1 + windowDelta)
                    {
                        continue;
                    }
                    currentCoordinate.X = i;
                    currentCoordinate.Y = j;
                    var currentRgnRect = currentCoordinate.PointCenteredRectangle(windowSize);
                    var dist = RegionDistance(sourceArray, currentRgnRect, drawable, selectionMask);
                    if (dist == null)
                    {
                        continue;
                    }
                    if (dist == 0)
                    {
                        return currentRgnRect;
                    }
                    if ((minDistance > dist || minRegionRect == null))
                    {
                        minDistance = (double) dist;
                        minRegionRect = currentRgnRect;
                    }
                }
            }

            return minRegionRect;
        }
Ejemplo n.º 20
0
        public void CountPixels()
        {
            var rgn = new PixelRgn(_drawable, false, false);

            int count = 0;

            for (IntPtr pr = PixelRgn.Register(rgn); pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                for (int y = rgn.Y; y < rgn.Y + rgn.H; y++)
                {
                    for (int x = rgn.X; x < rgn.X + rgn.W; x++)
                    {
                        count++;
                    }
                }
            }
            Assert.AreEqual(_width * _height, count);
        }
Ejemplo n.º 21
0
        public void GetSetRow()
        {
            var rgn = new PixelRgn(_drawable, false, false);
            var row = new Pixel[_width];

            for (int i = 0; i < _width; i++)
            {
                row[i] = new Pixel(13, 13, 13);
            }

            rgn.SetRow(row, 0, 13);

            var result = rgn.GetRow(0, 13, _width);

            for (int i = 0; i < _width; i++)
            {
                Assert.IsTrue(row[i].IsSameColor(result[i]));
            }
        }
Ejemplo n.º 22
0
        public void DirectAccessRgb()
        {
            var rgn   = new PixelRgn(_drawable, true, false);
            var pixel = new Pixel(13, 24, 35);

            FillDrawable(_drawable, pixel);

            rgn = new PixelRgn(_drawable, false, false);
            for (IntPtr pr = PixelRgn.Register(rgn); pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                for (int y = rgn.Y; y < rgn.Y + rgn.H; y++)
                {
                    for (int x = rgn.X; x < rgn.X + rgn.W; x++)
                    {
                        Assert.AreEqual(pixel.Color, rgn[y, x].Color);
                    }
                }
            }
        }
Ejemplo n.º 23
0
        public void IterateSrcDest(Func <Pixel, Pixel> func)
        {
            var srcPR  = new PixelRgn(_drawable, _rectangle, false, false);
            var destPR = new PixelRgn(_drawable, _rectangle, true, true);

            for (IntPtr pr = PixelRgn.Register(srcPR, destPR); pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                for (int y = srcPR.Y; y < srcPR.Y + srcPR.H; y++)
                {
                    for (int x = srcPR.X; x < srcPR.X + srcPR.W; x++)
                    {
                        destPR[y, x] = func(srcPR[y, x]);
                    }
                }
            }
            _drawable.Flush();
            _drawable.MergeShadow(true);
            _drawable.Update(_rectangle);
        }
Ejemplo n.º 24
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.º 25
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.º 26
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.º 27
0
        public void GetSetRect()
        {
            var rgn = new PixelRgn(_drawable, true, false);
              var rect = new byte[_drawable.Bpp * _width * _height];
              for (int i = 0; i < _drawable.Bpp * _width * _height; i++)
            {
              rect[i] = 13;
            }

              rgn.SetRect(rect, 0, 0, _width, _height);

              var result = rgn.GetRect(0, 0, _width, _height);

              Assert.AreEqual(rect, result);
        }
Ejemplo n.º 28
0
 public static IntPtr Register(PixelRgn rgn) =>
 gimp_pixel_rgns_register(1, ref *rgn.pr);
Ejemplo n.º 29
0
 public override void flushBuffer()
 {
     if (_imageBuffer != null) {
         PixelRgn destPR = new PixelRgn(_drawable, true, true);
         destPR.SetRect(_imageBuffer, _rectangle.X1, _rectangle.Y1,
             _rectangle.Width, _rectangle.Height);
         _drawable.Flush();
         _drawable.MergeShadow(true);
         _drawable.Update(_rectangle);
     }
     _imageBuffer = null;
 }
Ejemplo n.º 30
0
 internal Pixel(PixelRgn rgn, byte[] rgb)
     : this(rgb)
 {
     _rgn = rgn;
 }
Ejemplo n.º 31
0
        public void CountPixels()
        {
            var rgn = new PixelRgn(_drawable, false, false);

              int count = 0;
              for (IntPtr pr = PixelRgn.Register(rgn); pr != IntPtr.Zero;
               pr = PixelRgn.Process(pr))
            {
              for (int y = rgn.Y; y < rgn.Y + rgn.H; y++)
            {
              for (int x = rgn.X; x < rgn.X + rgn.W; x++)
            {
              count++;
            }
            }
            }
              Assert.AreEqual(_width * _height, count);
        }
Ejemplo n.º 32
0
 public static IntPtr Register(PixelRgn rgn)
 {
     return gimp_pixel_rgns_register(1, ref *rgn.pr);
 }
Ejemplo n.º 33
0
        public void DirectAccessRgb()
        {
            var rgn = new PixelRgn(_drawable, true, false);
              var pixel = new Pixel(13, 24, 35);

              FillDrawable(_drawable, pixel);

              rgn = new PixelRgn(_drawable, false, false);
              for (IntPtr pr = PixelRgn.Register(rgn); pr != IntPtr.Zero;
               pr = PixelRgn.Process(pr))
            {
              for (int y = rgn.Y; y < rgn.Y + rgn.H; y++)
            {
              for (int x = rgn.X; x < rgn.X + rgn.W; x++)
            {
              Assert.AreEqual(pixel.Color, rgn[y, x].Color);
            }
            }
            }
        }
Ejemplo n.º 34
0
        public void GetSetRow()
        {
            var rgn = new PixelRgn(_drawable, false, false);
              var row = new Pixel[_width];

              for (int i = 0; i < _width; i++)
            {
              row[i] = new Pixel(13, 13, 13);
            }

              rgn.SetRow(row, 0, 13);

              var result = rgn.GetRow(0, 13, _width);

              for (int i = 0; i < _width; i++)
            {
              Assert.IsTrue(row[i].IsSameColor(result[i]));
            }
        }
Ejemplo n.º 35
0
 internal Pixel(PixelRgn rgn, byte[] rgb) : this(rgb)
 {
     _rgn = rgn;
 }
Ejemplo n.º 36
0
 public void SetBuffer(byte[] buffer)
 {
   var rgn = new PixelRgn(this, true, false);
   rgn.SetRect(buffer, Bounds);
 }
Ejemplo n.º 37
0
 public void Rowstride()
 {
     var rgn = new PixelRgn(_drawable, false, false);
       PixelRgn.Register(rgn);
       Assert.AreEqual(_drawable.Bpp * Gimp.TileWidth, rgn.Rowstride);
 }
Ejemplo n.º 38
0
        public void CountTiles()
        {
            var rgn = new PixelRgn(_drawable, false, false);

              int tw = (int) Gimp.TileWidth;
              int th = (int) Gimp.TileHeight;
              int htiles = (_width + tw - 1) / tw;
              int vtiles = (_height + th - 1) / th;

              int count = 0;
              for (IntPtr pr = PixelRgn.Register(rgn); pr != IntPtr.Zero;
               pr = PixelRgn.Process(pr))
            {
              count++;
            }
              Assert.AreEqual(htiles * vtiles, count);
        }
        public double? RegionDistance(Pixel[,] source, Rectangle rgnRect, Drawable drawable, SelectionMask selectionMask)
        {
            var sum = 0.0;
            var sourceWidth = source.GetLength(0);
            var sourceHeight = source.GetLength(1);
            var rgn = new PixelRgn(drawable, rgnRect, false, false);
            for (IntPtr pr = PixelRgn.Register(rgn);
                 pr != IntPtr.Zero;
                 pr = PixelRgn.Process(pr))
            {
                for (int y1 = rgn.Y - rgnRect.UpperLeft.Y, y2 = rgn.Y;
                     y2 < rgn.Y + rgn.H;
                     y1++, y2++)
                {
                    for (int x1 = rgn.X - rgnRect.UpperLeft.X, x2 = rgn.X;
                         x2 < rgn.X + rgn.W;
                         x1++, x2++)
                    {
                        if (selectionMask[x2, y2])
                        {
                            return null;
                        }
                        if (x1 >= sourceWidth ||
                            x2 >= sourceHeight ||
                            source[x1, y1] == null)
                        {
                            continue;
                        }

                        sum += DistanceSqr(source[x1, y1], rgn[y2, x2]);
                    }
                }
            }

            return sum;
        }
Ejemplo n.º 40
0
        public void DrawRegion(PixelRgn region)
        {
            var pr = region.PR;

            gimp_drawable_preview_draw_region(Handle, ref pr);
        }
Ejemplo n.º 41
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.º 42
0
        public static IntPtr Register(PixelRgn rgn1, PixelRgn rgn2, 
				  PixelRgn rgn3)
        {
            return gimp_pixel_rgns_register(3, ref *rgn1.pr, ref *rgn2.pr,
              ref *rgn3.pr);
        }
Ejemplo n.º 43
0
        public void GetSetPixel()
        {
            var rgn = new PixelRgn(_drawable, false, false);
              var pixel = new byte[]{13, 24, 35};

              rgn.SetPixel(pixel, 13, 14);

              var result = new byte[_drawable.Bpp];
              rgn.GetPixel(result, 13, 14);

              Assert.AreEqual(pixel, result);
        }
Ejemplo n.º 44
0
 public override void initBuffer()
 {
     PixelRgn rgn = new PixelRgn(_drawable, false, false);
     _imageBuffer = rgn.GetRect(_rectangle.X1, _rectangle.Y1,
         _rectangle.Width, _rectangle.Height);
 }
Ejemplo n.º 45
0
        public void IterateSrcDest(Func<Pixel, Pixel> func)
        {
            var srcPR = new PixelRgn(_drawable, _rectangle, false, false);
              var destPR = new PixelRgn(_drawable, _rectangle, true, true);

              for (IntPtr pr = PixelRgn.Register(srcPR, destPR); pr != IntPtr.Zero;
               pr = PixelRgn.Process(pr))
            {
              for (int y = srcPR.Y; y < srcPR.Y + srcPR.H; y++)
            {
              for (int x = srcPR.X; x < srcPR.X + srcPR.W; x++)
            {
              destPR[y, x] = func(srcPR[y, x]);
            }
            }
            }
              _drawable.Flush();
              _drawable.MergeShadow(true);
              _drawable.Update(_rectangle);
        }
Ejemplo n.º 46
0
 public static IntPtr Register(PixelRgn rgn1, PixelRgn rgn2)
 {
     return(gimp_pixel_rgns_register(2, ref *rgn1.pr, ref *rgn2.pr));
 }
Ejemplo n.º 47
0
        public void DirectAccessRgba()
        {
            var image = new Image(_width, _height, ImageBaseType.Rgb) {
            {new Layer("test", ImageType.Rgba), 0}};

              var drawable = image.ActiveDrawable;
              var pixel = new Pixel(13, 24, 35, 128);
              FillDrawable(drawable, pixel);

              var rgn = new PixelRgn(drawable, false, false);
              for (IntPtr pr = PixelRgn.Register(rgn); pr != IntPtr.Zero;
               pr = PixelRgn.Process(pr))
            {
              for (int y = rgn.Y; y < rgn.Y + rgn.H; y++)
            {
              for (int x = rgn.X; x < rgn.X + rgn.W; x++)
            {
              Assert.AreEqual(pixel.Color, rgn[y, x].Color);
            }
            }
            }
              image.Delete();
        }
Ejemplo n.º 48
0
 public static IntPtr Register(PixelRgn rgn1, PixelRgn rgn2,
                               PixelRgn rgn3) =>
 gimp_pixel_rgns_register(3, ref *rgn1.pr, ref *rgn2.pr, ref *rgn3.pr);
Ejemplo n.º 49
0
 public void DrawRegion(PixelRgn region)
 {
     var pr = region.PR;
       gimp_drawable_preview_draw_region(Handle, ref pr);
 }
Ejemplo n.º 50
0
 public static IntPtr Register(PixelRgn rgn)
 {
     return(gimp_pixel_rgns_register(1, ref *rgn.pr));
 }
Ejemplo n.º 51
0
        void FillDrawable(Drawable drawable, Pixel pixel)
        {
            var rgn = new PixelRgn(drawable, true, false);

              for (IntPtr pr = PixelRgn.Register(rgn); pr != IntPtr.Zero;
               pr = PixelRgn.Process(pr))
            {
              for (int y = rgn.Y; y < rgn.Y + rgn.H; y++)
            {
              for (int x = rgn.X; x < rgn.X + rgn.W; x++)
            {
              rgn[y, x] = pixel;
            }
            }
            }
        }
Ejemplo n.º 52
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;
 }
Ejemplo n.º 53
0
 public void SetBuffer(byte[] buffer)
 {
     var rgn = new PixelRgn(this, true, false);
       rgn.SetRect(buffer, Bounds);
 }