public ImageEnhancement Clone() { bool was_locked = this.IsLocked; this.LockBitmap(); ImageEnhancement result = (ImageEnhancement)this.MemberwiseClone(); result.Bitmap = new Bitmap(this.Bitmap.Width, this.Bitmap.Height); result._IsLocked = false; if (!was_locked) { this.UnlockBitmap(); } return(result); }
public ImageEnhancement ApplyFilter(Filter filter, bool lock_result) { ImageEnhancement result = this.Clone(); bool was_locked = this.IsLocked; this.LockBitmap(); result.LockBitmap(); int xoffset = -(int)(filter.Kernel.GetUpperBound(1) / 2); int yoffset = -(int)(filter.Kernel.GetUpperBound(0) / 2); int xmin = -xoffset; int xmax = Bitmap.Width - filter.Kernel.GetUpperBound(1); int ymin = -yoffset; int ymax = Bitmap.Height - filter.Kernel.GetUpperBound(0); int row_max = filter.Kernel.GetUpperBound(0); int col_max = filter.Kernel.GetUpperBound(1); for (int x = xmin; x <= xmax; x++) { for (int y = ymin; y <= ymax; y++) { bool skip_pixel = false; float red = 0, green = 0, blue = 0; for (int row = 0; row <= row_max; row++) { for (int col = 0; col <= col_max; col++) { int ix = x + col + xoffset; int iy = y + row + yoffset; byte new_red, new_green, new_blue, new_alpha; this.GetPixel(ix, iy, out new_red, out new_green, out new_blue, out new_alpha); if (new_alpha == 0) { skip_pixel = true; break; } red += new_red * filter.Kernel[row, col]; green += new_green * filter.Kernel[row, col]; blue += new_blue * filter.Kernel[row, col]; } if (skip_pixel) { break; } } if (!skip_pixel) { red = filter.Offset + red / filter.Weight; if (red < 0) { red = 0; } if (red > 255) { red = 255; } green = filter.Offset + green / filter.Weight; if (green < 0) { green = 0; } if (green > 255) { green = 255; } blue = filter.Offset + blue / filter.Weight; if (blue < 0) { blue = 0; } if (blue > 255) { blue = 255; } result.SetPixel(x, y, (byte)red, (byte)green, (byte)blue, this.GetAlpha(x, y)); } } } if (!lock_result) { result.UnlockBitmap(); } if (!was_locked) { this.UnlockBitmap(); } return(result); #endregion }