/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }
/// <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); }