public static Rectangle AdjustedToImageEdges(this Rectangle originRect, Rectangle imageRect)
 {
     var rect = new Rectangle(originRect.UpperLeft, originRect.LowerRight);
     if (!imageRect.IsInside(rect.UpperLeft))
     {
         if (rect.UpperLeft.X < imageRect.UpperLeft.X)
         {
             rect.UpperLeft.X = imageRect.UpperLeft.X;
         }
         if (rect.UpperLeft.Y < imageRect.UpperLeft.Y)
         {
             rect.UpperLeft.Y = imageRect.UpperLeft.Y;
         }
     }
     if (!imageRect.IsInside(rect.LowerRight))
     {
         if (rect.LowerRight.X > imageRect.LowerRight.X)
         {
             rect.LowerRight.X = imageRect.LowerRight.X;
         }
         if (rect.LowerRight.Y > imageRect.LowerRight.Y)
         {
             rect.LowerRight.Y = imageRect.LowerRight.Y;
         }
     }
     return rect;
 }
Example #2
0
 public void ConstructorOne()
 {
     var rectangle = new Rectangle(13, 14, 129, 132);
       Assert.AreEqual(13, rectangle.X1);
       Assert.AreEqual(14, rectangle.Y1);
       Assert.AreEqual(129, rectangle.X2);
       Assert.AreEqual(132, rectangle.Y2);
 }
Example #3
0
        private Rectangle _rectangle; // mask bounds

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Create a GSImage backed with an existing Drawable.
        /// </summary>
        /// <param name="drawable"></param>
        public GSImage(Drawable drawable)
        {
            _image = drawable.Image;
            _drawable = drawable;
            _rectangle = _drawable.MaskBounds;
            Tile.CacheDefault(_drawable);
            Progress = new Progress("Halftone Laboratory");
        }
Example #4
0
        public void Equals()
        {
            var rectangle1 = new Rectangle(13, 14, 129, 132);
              var rectangle2 = new Rectangle(23, 24, 129, 132);
              var rectangle3 = new Rectangle(13, 14, 129, 132);

              Assert.IsFalse(rectangle1.Equals(rectangle2));
              Assert.IsTrue(rectangle1.Equals(rectangle3));
        }
Example #5
0
 public void ConstructorTwo()
 {
     var upperLeft = new Coordinate<int>(13, 14);
       var lowerRight = new Coordinate<int>(129, 132);
       var rectangle = new Rectangle(upperLeft, lowerRight);
       Assert.AreEqual(13, rectangle.X1);
       Assert.AreEqual(14, rectangle.Y1);
       Assert.AreEqual(129, rectangle.X2);
       Assert.AreEqual(132, rectangle.Y2);
 }
Example #6
0
 /// <summary>
 /// Create a GSImage backed with a new %Gimp# Image.
 /// </summary>
 /// <param name="width"></param>
 /// <param name="height"></param>
 public GSImage(int width, int height)
 {
     _image = new Gimp.Image(width, height, ImageBaseType.Gray);
     Layer layer = new Layer(_image, "default", width, height,
         ImageType.Gray, 100, LayerModeEffects.Normal);
     _image.AddLayer(layer, 0);
     _drawable = _image.ActiveDrawable;
     _rectangle = _drawable.MaskBounds;
     Tile.CacheDefault(_drawable);
     Progress = new Progress("Halftone Laboratory");
 }
Example #7
0
        public void Operators()
        {
            var rectangle1 = new Rectangle(13, 14, 129, 132);
              var rectangle2 = new Rectangle(23, 24, 129, 132);
              var rectangle3 = new Rectangle(13, 14, 129, 132);

              Assert.IsFalse(rectangle1 == rectangle2);
              Assert.IsTrue(rectangle1 != rectangle2);
              Assert.IsTrue(rectangle1 == rectangle3);
              Assert.IsFalse(rectangle1 != rectangle3);
        }
 public void SetAreaToZero(Rectangle rect)
 {
     for (int i = rect.X1; i < rect.X1 + rect.Width; i++)
     {
         for (int j = rect.Y1; j < rect.Y1 + rect.Height; j++)
         {
             if (!_originBounds.IsInside(i, j) ||
                 !_mask[i - _originBounds.X1, j - _originBounds.Y1])
             {
                 continue;
             }
             _mask[i - _originBounds.X1, j - _originBounds.Y1] = false;
             _area--;
         }
     }
 }
        public Rectangle MinDistanceRegion(Rectangle sourceRect, ImgGradient gradient, Pixel[,] layerPixels, SelectionMask selectionMask)
        {
            Debug.Assert(sourceRect.Width == sourceRect.Height);

            var windowSize = sourceRect.Width;
            var windowDelta = windowSize/2;
            var layerWidth = layerPixels.GetLength(0);
            var layerHeight = layerPixels.GetLength(1);

            Rectangle minRegionRect = null;
            var minDistance = 0.0;
            var adjustedSourceRect = sourceRect.AdjustedToImageEdges(new Rectangle(new IntCoordinate(0, 0),
                                                                                   layerWidth, layerHeight));
            var currentCoordinate = new IntCoordinate(0, 0);
            var orthoGradient = new Coordinate<double>(-gradient.YMagnitude*windowDelta,
                                                       gradient.XMagnitude*windowDelta);
            for (int i = windowDelta; i < layerWidth - windowDelta; i++)
            {
                for (int j = windowDelta; j < layerHeight - 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(adjustedSourceRect, currentRgnRect, layerPixels, selectionMask);
                    if (dist == null)
                    {
                        continue;
                    }
                    if (dist == 0)
                    {
                        return currentRgnRect;
                    }
                    if ((minDistance > dist || minRegionRect == null))
                    {
                        minDistance = (double) dist;
                        minRegionRect = currentRgnRect;
                    }
                }
            }

            return minRegionRect;
        }
        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;
        }
        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;
                    }
                });
        }
Example #12
0
 public void Draw(Rectangle rectangle, ImageType type, byte[] buf, 
     int rowstride)
 {
     Draw(rectangle.X1, rectangle.Y1, rectangle.Width, rectangle.Height,
        type, buf, rowstride);
 }
 public CoordinateGenerator(Rectangle rectangle, Action<int> update = null)
 {
     _rectangle = rectangle;
       _update = update;
 }
Example #14
0
 public void Width()
 {
     var rectangle = new Rectangle(13, 14, 129, 132);
       Assert.AreEqual(129 - 13, rectangle.Width);
 }
Example #15
0
 public void Area()
 {
     var rectangle = new Rectangle(13, 14, 129, 132);
       Assert.AreEqual((129 - 13) * (132 - 14), rectangle.Area);
 }
Example #16
0
 /// <summary>
 /// Scale the image with a given factor and interpolation.
 /// </summary>
 /// <remarks>
 /// If there is an buffer, it gets flushed before scaling.
 /// </remarks>
 /// <param name="factor">Resize factor (< 1.0 = downscale, >1.0 =
 /// upscale)</param>
 /// <param name="interpolation">Interpolation type</param>
 /// <param name="direction">Scale direction (forward = scale by factor,
 /// backward = scale by 1/factor)</param>
 public void scale(double factor, InterpolationType interpolation,
     TransformDirection direction)
 {
     bool restoreBuffer = _imageBuffer != null;
     if (restoreBuffer) {
         flushBuffer();
     }
     double newHeight = Height * factor;
     double newWidth = Width * factor;
     Image.UndoGroupStart();
     Drawable.TransformScale(0, 0, newWidth, newHeight,
         direction, interpolation, false, 1, false);
     Image.ResizeToLayers();
     Image.UndoGroupEnd();
     _drawable = Image.ActiveDrawable;
     _rectangle = Drawable.MaskBounds;
     if (restoreBuffer) {
         initBuffer();
     }
 }
Example #17
0
        public PixelRgn(Drawable drawable, Rectangle rectangle, bool dirty,
		    bool shadow)
            : this(drawable, rectangle.X1, rectangle.Y1, rectangle.Width,
	   rectangle.Height, dirty, shadow)
        {
        }
Example #18
0
 public void UpperLeftLowerRight()
 {
     var upperLeft = new Coordinate<int>(13, 14);
       var lowerRight = new Coordinate<int>(129, 132);
       var rectangle = new Rectangle(upperLeft, lowerRight);
       Assert.IsTrue(rectangle.UpperLeft == upperLeft);
       Assert.IsTrue(rectangle.LowerRight == lowerRight);
 }
Example #19
0
 public byte[] GetRect(Rectangle rectangle)
 {
     return GetRect(rectangle.X1, rectangle.Y1, rectangle.Width,
      rectangle.Height);
 }
Example #20
0
 public void Update(Rectangle rectangle)
 {
     Update(rectangle.X1, rectangle.Y1, rectangle.Width, rectangle.Height);
 }
Example #21
0
        public Pixel[,] GetThumbnailData(Rectangle rectangle,
            Dimensions dimensions)
        {
            int width = dimensions.Width;
              int height = dimensions.Height;
              int bpp;

              IntPtr src = gimp_drawable_get_sub_thumbnail_data(ID, rectangle.X1,
                            rectangle.Y1,
                            rectangle.Width,
                            rectangle.Height,
                            ref width, ref height,
                            out bpp);
              var thumbnail =
            Pixel.ConvertToPixelArray(src, new Dimensions(width, height), bpp);
              Marshaller.Free(src);

              return thumbnail;
        }
Example #22
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;
 }
Example #23
0
 public void CropThree()
 {
     var rectangle = new Rectangle(10, 10, 20, 20);
       _image.Crop(rectangle);
       Assert.AreEqual(rectangle.Width, _image.Width);
       Assert.AreEqual(rectangle.Height, _image.Height);
 }
        public double? RegionDistance(Rectangle sourceRect, Rectangle rgnRect, Pixel[,] layerPixels,
                                      SelectionMask selectionMask)
        {
            var sum = 0.0;
            var sourceWidth = sourceRect.Width;
            var sourceHeight = sourceRect.Height;

            for (int y1 = sourceRect.Y1, y2 = rgnRect.Y1;
                 y1 < sourceRect.Y1 + sourceHeight;
                 y1++, y2++)
            {
                for (int x1 = sourceRect.X1, x2 = rgnRect.X1;
                     x1 < sourceRect.X1 + sourceWidth;
                     x1++, x2++)
                {
                    //if there's selection pixel inside of examined region mark region as useless
                    if (selectionMask[x2, y2])
                    {
                        return null;
                    }
                    //if it's pixel from target region don't examine it
                    if (selectionMask[x1, y1])
                    {
                        continue;
                    }

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

            return sum;
        }
Example #25
0
 public void SetRect(byte[] buf, Rectangle rectangle)
 {
     SetRect(buf, rectangle.X1, rectangle.Y1, rectangle.Width,
       rectangle.Height);
 }
        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;
        }
Example #27
0
 public void Height()
 {
     var rectangle = new Rectangle(13, 14, 129, 132);
       Assert.AreEqual(132 - 14, rectangle.Height);
 }
Example #28
0
 public RgnIterator(Drawable drawable, RunMode runmode)
 {
     _drawable = drawable;
       _runmode = runmode;
       _rectangle = drawable.MaskBounds;
 }
Example #29
0
 /// <summary>
 /// Calculate new confidance value of pixel at given coordinate
 /// </summary>
 /// <param name="coordinate"></param>
 /// <param name="pxConfidenceTerm"></param>
 /// <param name="windowSize"></param>
 private static double NewConfidanceValue(IntCoordinate coordinate, double[,] pxConfidenceTerm, 
                                   int windowSize)
 {
     var imageRect = new Rectangle(new IntCoordinate(0, 0), pxConfidenceTerm.GetLength(0),
                                   pxConfidenceTerm.GetLength(1));
     //TODO: handle boundary regions properly
     return RegionHelper.Grid(coordinate.PointCenteredRectangle(windowSize))
                        .Select(coordPrim => imageRect.IsInside(coordPrim)
                                                 ? pxConfidenceTerm[coordPrim.X, coordPrim.Y]
                                                 : 0)
                        .Sum()/(windowSize*windowSize);
 }