Esempio n. 1
0
        /// <summary>
        /// Picture filter.
        /// </summary>
        /// <param name="image">Image object.</param>
        /// <param name="filter">3x3 filter.</param>
        /// <returns>Processed image object.</returns>
        public static Image Filter(Image image, int[] filter)
        {
            Bitmap bitmap = image.Clone() as Bitmap;

            // Locks the bitmap into system memory.
            Rectangle  rect    = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
            BitmapData bmpdata = bitmap.LockBits(rect, ImageLockMode.ReadWrite, bitmap.PixelFormat);

            int pixelSize = ImageProcess.GetUnitPixelSize(bitmap);
            int pb = 0, pg = 1, pr = 2;  // BGR

            unsafe {
                byte *start = (byte *)bmpdata.Scan0;

                // Calculate the offset
                Func <int, int, int> offset = (x, y) => {
                    return(x * pixelSize + y * bmpdata.Stride);
                };

                for (int x = 1; x < bitmap.Width - 1; ++x)
                {
                    for (int y = 1; y < bitmap.Height - 1; ++y)
                    {
                        int r = 0, g = 0, b = 0, index = 0;

                        // Convolution calculation
                        for (int col = -1; col <= 1; ++col)
                        {
                            for (int row = -1; row <= 1; ++row)
                            {
                                byte *ptr = start + offset(x + col, y + row);

                                r += ptr[pr] * filter[index];
                                g += ptr[pg] * filter[index];
                                b += ptr[pb] * filter[index];

                                index++;
                            }
                        }

                        // Processing overflow
                        r = Math.Max(0, Math.Min(255, r));
                        g = Math.Max(0, Math.Min(255, g));
                        b = Math.Max(0, Math.Min(255, b));

                        // The pixel to be modified
                        byte *target = start + offset(x - 1, y - 1);

                        target[pr] = (byte)r;
                        target[pg] = (byte)g;
                        target[pb] = (byte)b;
                    }
                }
            }

            bitmap.UnlockBits(bmpdata);
            return(bitmap as Image);
        }
Esempio n. 2
0
 /// <summary>
 /// Scale the image proportionally.
 /// </summary>
 /// <returns>Current factory instance itself.</returns>
 public ImageProcessFactory Scale(float widthScale, float heightScale)
 {
     this.image = ImageProcess.Scale(this.image, widthScale, heightScale);
     return(this);
 }
Esempio n. 3
0
 /// <summary>
 /// Get the grayscale image corresponding to the image.
 /// </summary>
 /// <returns>Current factory instance itself.</returns>
 public ImageProcessFactory Gray()
 {
     this.image = ImageProcess.Gray(this.image);
     return(this);
 }
Esempio n. 4
0
 /// <summary>
 /// Get the inverted image corresponding to the image.
 /// </summary>
 /// <returns>Current factory instance itself.</returns>
 public ImageProcessFactory Invert()
 {
     this.image = ImageProcess.Invert(this.image);
     return(this);
 }
Esempio n. 5
0
 /// <summary>
 /// Swap image rgb color channel.
 /// </summary>
 /// <returns>Current factory instance itself.</returns>
 public ImageProcessFactory SwapRgb(String rgbOrder)
 {
     this.image = ImageProcess.SwapRgb(this.image, rgbOrder);
     return(this);
 }
Esempio n. 6
0
 /// <summary>
 /// Intercept the elliptical portion of the image.
 /// </summary>
 /// <returns>Current factory instance itself.</returns>
 public ImageProcessFactory ClipEllipse(int x, int y, int width, int height,
                                        Color backgroundColor)
 {
     this.image = ImageProcess.ClipEllipse(this.image, x, y, width, height, backgroundColor);
     return(this);
 }
Esempio n. 7
0
 /// <summary>
 /// Intercept the rectangular portion of the image.
 /// </summary>
 /// <returns>Current factory instance itself.</returns>
 public ImageProcessFactory ClipRectangle(int x, int y, int width, int height)
 {
     this.image = ImageProcess.ClipRectangle(this.image, x, y, width, height);
     return(this);
 }
Esempio n. 8
0
 /// <summary>
 /// Flip the image.
 /// </summary>
 /// <returns>Current factory instance itself.</returns>
 public ImageProcessFactory Flip(bool flipX, bool flipY)
 {
     this.image = ImageProcess.Flip(this.image, flipX, flipY);
     return(this);
 }
Esempio n. 9
0
 /// <summary>
 /// Rotates the Image.
 /// </summary>
 /// <returns>Current factory instance itself.</returns>
 public ImageProcessFactory Rotate(int degree)
 {
     this.image = ImageProcess.Rotate(this.image, degree);
     return(this);
 }