/// <summary> /// Implements the Apply code for the Brightness Filet /// </summary> /// <param name="graphics"></param> /// <param name="applyBitmap"></param> /// <param name="rect"></param> /// <param name="renderMode"></param> public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode) { Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert); if (applyRect.Width == 0 || applyRect.Height == 0) { // nothing to do return; } GraphicsState state = graphics.Save(); if (Invert) { graphics.SetClip(applyRect); graphics.ExcludeClip(rect); } using (IFastBitmap fastBitmap = FastBitmap.CreateCloneOf(applyBitmap, applyRect)) { Color highlightColor = GetFieldValueAsColor(FieldType.FILL_COLOR); for (int y = fastBitmap.Top; y < fastBitmap.Bottom; y++) { for (int x = fastBitmap.Left; x < fastBitmap.Right; x++) { Color color = fastBitmap.GetColorAt(x, y); color = Color.FromArgb(color.A, Math.Min(highlightColor.R, color.R), Math.Min(highlightColor.G, color.G), Math.Min(highlightColor.B, color.B)); fastBitmap.SetColorAt(x, y, color); } } fastBitmap.DrawTo(graphics, applyRect.Location); } graphics.Restore(state); }
public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode) { int pixelSize = GetFieldValueAsInt(FieldType.PIXEL_SIZE); Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert); if (pixelSize <= 1 || rect.Width == 0 || rect.Height == 0) { // Nothing to do return; } if (rect.Width < pixelSize) { pixelSize = rect.Width; } if (rect.Height < pixelSize) { pixelSize = rect.Height; } using (IFastBitmap dest = FastBitmap.CreateCloneOf(applyBitmap, rect)) { using (IFastBitmap src = FastBitmap.Create(applyBitmap, rect)) { List <Color> colors = new List <Color>(); int halbPixelSize = pixelSize / 2; for (int y = src.Top - halbPixelSize; y < src.Bottom + halbPixelSize; y = y + pixelSize) { for (int x = src.Left - halbPixelSize; x <= src.Right + halbPixelSize; x = x + pixelSize) { colors.Clear(); for (int yy = y; yy < y + pixelSize; yy++) { if (yy >= src.Top && yy < src.Bottom) { for (int xx = x; xx < x + pixelSize; xx++) { if (xx >= src.Left && xx < src.Right) { colors.Add(src.GetColorAt(xx, yy)); } } } } Color currentAvgColor = Colors.Mix(colors); for (int yy = y; yy <= y + pixelSize; yy++) { if (yy >= src.Top && yy < src.Bottom) { for (int xx = x; xx <= x + pixelSize; xx++) { if (xx >= src.Left && xx < src.Right) { dest.SetColorAt(xx, yy, currentAvgColor); } } } } } } } dest.DrawTo(graphics, rect.Location); } }
private static void BoxBlurVerticalAlpha(IFastBitmap targetFastBitmap, int range) { if (!targetFastBitmap.hasAlphaChannel) { throw new NotSupportedException("BoxBlurVerticalAlpha should be called for bitmaps with alpha channel"); } int w = targetFastBitmap.Width; int halfRange = range / 2; Color[] newColors = new Color[targetFastBitmap.Height]; int oldPixelOffset = -(halfRange + 1) * w; int newPixelOffset = (halfRange) * w; byte[] tmpColor = new byte[4]; for (int x = targetFastBitmap.Left; x < targetFastBitmap.Right; x++) { int hits = 0; int a = 0; int r = 0; int g = 0; int b = 0; for (int y = targetFastBitmap.Top - halfRange; y < targetFastBitmap.Bottom; y++) { int oldPixel = y - halfRange - 1; if (oldPixel >= targetFastBitmap.Top) { targetFastBitmap.GetColorAt(x, oldPixel, tmpColor); a -= tmpColor[FastBitmap.COLOR_INDEX_A]; r -= tmpColor[FastBitmap.COLOR_INDEX_R]; g -= tmpColor[FastBitmap.COLOR_INDEX_G]; b -= tmpColor[FastBitmap.COLOR_INDEX_B]; hits--; } int newPixel = y + halfRange; if (newPixel < targetFastBitmap.Bottom) { //int colorg = pixels[index + newPixelOffset]; targetFastBitmap.GetColorAt(x, newPixel, tmpColor); a += tmpColor[FastBitmap.COLOR_INDEX_A]; r += tmpColor[FastBitmap.COLOR_INDEX_R]; g += tmpColor[FastBitmap.COLOR_INDEX_G]; b += tmpColor[FastBitmap.COLOR_INDEX_B]; hits++; } if (y >= targetFastBitmap.Top) { newColors[y - targetFastBitmap.Top] = Color.FromArgb((byte)(a / hits), (byte)(r / hits), (byte)(g / hits), (byte)(b / hits)); } } for (int y = targetFastBitmap.Top; y < targetFastBitmap.Bottom; y++) { targetFastBitmap.SetColorAt(x, y, newColors[y - targetFastBitmap.Top]); } } }
/// <summary> /// BoxBlurVertical is a private helper method for the BoxBlur /// </summary> /// <param name="targetFastBitmap">BitmapBuffer which previously was created with BoxBlurHorizontal</param> /// <param name="range">Range must be odd!</param> private static void BoxBlurVerticalAlpha(IFastBitmap targetFastBitmap, int range) { if (!targetFastBitmap.HasAlphaChannel) { throw new NotSupportedException("BoxBlurVerticalAlpha should be called for bitmaps with alpha channel"); } var halfRange = range / 2; var newColors = new Color[targetFastBitmap.Height]; var tmpColor = new byte[4]; for (var x = targetFastBitmap.Left; x < targetFastBitmap.Right; x++) { var hits = 0; var a = 0; var r = 0; var g = 0; var b = 0; for (var y = targetFastBitmap.Top - halfRange; y < targetFastBitmap.Bottom; y++) { var oldPixel = y - halfRange - 1; if (oldPixel >= targetFastBitmap.Top) { targetFastBitmap.GetColorAt(x, oldPixel, tmpColor); a -= tmpColor[FastBitmapBase.ColorIndexA]; r -= tmpColor[FastBitmapBase.ColorIndexR]; g -= tmpColor[FastBitmapBase.ColorIndexG]; b -= tmpColor[FastBitmapBase.ColorIndexB]; hits--; } var newPixel = y + halfRange; if (newPixel < targetFastBitmap.Bottom) { //int colorg = pixels[index + newPixelOffset]; targetFastBitmap.GetColorAt(x, newPixel, tmpColor); a += tmpColor[FastBitmapBase.ColorIndexA]; r += tmpColor[FastBitmapBase.ColorIndexR]; g += tmpColor[FastBitmapBase.ColorIndexG]; b += tmpColor[FastBitmapBase.ColorIndexB]; hits++; } if (y >= targetFastBitmap.Top) { newColors[y - targetFastBitmap.Top] = Color.FromArgb((byte)(a / hits), (byte)(r / hits), (byte)(g / hits), (byte)(b / hits)); } } for (var y = targetFastBitmap.Top; y < targetFastBitmap.Bottom; y++) { targetFastBitmap.SetColorAt(x, y, ref newColors[y - targetFastBitmap.Top]); } } }
/// <summary> /// BoxBlurHorizontal is a private helper method for the BoxBlur, only for IFastBitmaps with alpha channel /// </summary> /// <param name="targetFastBitmap">Target BitmapBuffer</param> /// <param name="range">Range must be odd!</param> private static void BoxBlurHorizontalAlpha(IFastBitmap targetFastBitmap, int range) { if (!targetFastBitmap.HasAlphaChannel) { throw new NotSupportedException("BoxBlurHorizontalAlpha should be called for bitmaps with alpha channel"); } var halfRange = range / 2; var newColors = new Color[targetFastBitmap.Width]; var tmpColor = new byte[4]; for (var y = targetFastBitmap.Top; y < targetFastBitmap.Bottom; y++) { var hits = 0; var a = 0; var r = 0; var g = 0; var b = 0; for (var x = targetFastBitmap.Left - halfRange; x < targetFastBitmap.Right; x++) { var oldPixel = x - halfRange - 1; if (oldPixel >= targetFastBitmap.Left) { targetFastBitmap.GetColorAt(oldPixel, y, tmpColor); a -= tmpColor[FastBitmapBase.ColorIndexA]; r -= tmpColor[FastBitmapBase.ColorIndexR]; g -= tmpColor[FastBitmapBase.ColorIndexG]; b -= tmpColor[FastBitmapBase.ColorIndexB]; hits--; } var newPixel = x + halfRange; if (newPixel < targetFastBitmap.Right) { targetFastBitmap.GetColorAt(newPixel, y, tmpColor); a += tmpColor[FastBitmapBase.ColorIndexA]; r += tmpColor[FastBitmapBase.ColorIndexR]; g += tmpColor[FastBitmapBase.ColorIndexG]; b += tmpColor[FastBitmapBase.ColorIndexB]; hits++; } if (x >= targetFastBitmap.Left) { newColors[x - targetFastBitmap.Left] = Color.FromArgb((byte)(a / hits), (byte)(r / hits), (byte)(g / hits), (byte)(b / hits)); } } for (var x = targetFastBitmap.Left; x < targetFastBitmap.Right; x++) { targetFastBitmap.SetColorAt(x, y, ref newColors[x - targetFastBitmap.Left]); } } }
private static void BoxBlurHorizontalAlpha(IFastBitmap targetFastBitmap, int range) { if (!targetFastBitmap.hasAlphaChannel) { throw new NotSupportedException("BoxBlurHorizontalAlpha should be called for bitmaps with alpha channel"); } int halfRange = range / 2; Color[] newColors = new Color[targetFastBitmap.Width]; byte[] tmpColor = new byte[4]; for (int y = targetFastBitmap.Top; y < targetFastBitmap.Bottom; y++) { int hits = 0; int a = 0; int r = 0; int g = 0; int b = 0; for (int x = targetFastBitmap.Left - halfRange; x < targetFastBitmap.Right; x++) { int oldPixel = x - halfRange - 1; if (oldPixel >= targetFastBitmap.Left) { targetFastBitmap.GetColorAt(oldPixel, y, tmpColor); a -= tmpColor[FastBitmap.COLOR_INDEX_A]; r -= tmpColor[FastBitmap.COLOR_INDEX_R]; g -= tmpColor[FastBitmap.COLOR_INDEX_G]; b -= tmpColor[FastBitmap.COLOR_INDEX_B]; hits--; } int newPixel = x + halfRange; if (newPixel < targetFastBitmap.Right) { targetFastBitmap.GetColorAt(newPixel, y, tmpColor); a += tmpColor[FastBitmap.COLOR_INDEX_A]; r += tmpColor[FastBitmap.COLOR_INDEX_R]; g += tmpColor[FastBitmap.COLOR_INDEX_G]; b += tmpColor[FastBitmap.COLOR_INDEX_B]; hits++; } if (x >= targetFastBitmap.Left) { newColors[x - targetFastBitmap.Left] = Color.FromArgb((byte)(a / hits), (byte)(r / hits), (byte)(g / hits), (byte)(b / hits)); } } for (int x = targetFastBitmap.Left; x < targetFastBitmap.Right; x++) { targetFastBitmap.SetColorAt(x, y, newColors[x - targetFastBitmap.Left]); } } }
public static Bitmap Pixelate(Bitmap sourceImage, int pixelSize) { pixelSize = Math.Min(pixelSize, sourceImage.Width); pixelSize = Math.Min(pixelSize, sourceImage.Height); Bitmap result = sourceImage.CreateEmptyBitmap(); using (IFastBitmap src = FastBitmap.Create(sourceImage, new Rectangle(0, 0, sourceImage.Width, sourceImage.Height))) using (IFastBitmap dest = FastBitmap.Create(result)) { List <Color> colors = new List <Color>(); int halbPixelSize = pixelSize / 2; for (int y = src.Top - halbPixelSize; y < src.Bottom + halbPixelSize; y = y + pixelSize) { for (int x = src.Left - halbPixelSize; x <= src.Right + halbPixelSize; x = x + pixelSize) { colors.Clear(); for (int yy = y; yy < y + pixelSize; yy++) { if (yy >= src.Top && yy < src.Bottom) { for (int xx = x; xx < x + pixelSize; xx++) { if (xx >= src.Left && xx < src.Right) { colors.Add(src.GetColorAt(xx, yy)); } } } } Color currentAvgColor = ColorHelpers.Mix(colors); for (int yy = y; yy <= y + pixelSize; yy++) { if (yy >= src.Top && yy < src.Bottom) { for (int xx = x; xx <= x + pixelSize; xx++) { if (xx >= src.Left && xx < src.Right) { dest.SetColorAt(xx, yy, currentAvgColor); } } } } } } } return(result); }
public override bool ProcessCapture(ISurface surface, ICaptureDetails captureDetails) { LOG.DebugFormat("Changing surface to grayscale!"); using (IFastBitmap bbb = FastBitmap.Create(surface.Image as Bitmap)) { bbb.Lock(); for (int y = 0; y < bbb.Height; y++) { for (int x = 0; x < bbb.Width; x++) { Color color = bbb.GetColorAt(x, y); int luma = (int)((0.3 * color.R) + (0.59 * color.G) + (0.11 * color.B)); color = Color.FromArgb(luma, luma, luma); bbb.SetColorAt(x, y, color); } } } return(true); }
public static void MixColors(IFastBitmap dest, IFastBitmap src, int pixelSize) { List <Color> colors = new List <Color>(); int halbPixelSize = pixelSize / 2; for (int y = src.Top - halbPixelSize; y < src.Bottom + halbPixelSize; y = y + pixelSize) { for (int x = src.Left - halbPixelSize; x <= src.Right + halbPixelSize; x = x + pixelSize) { colors.Clear(); for (int yy = y; yy < y + pixelSize; yy++) { if (yy >= src.Top && yy < src.Bottom) { for (int xx = x; xx < x + pixelSize; xx++) { if (xx >= src.Left && xx < src.Right) { colors.Add(src.GetColorAt(xx, yy)); } } } } Color currentAvgColor = Colors.Mix(colors); for (int yy = y; yy <= y + pixelSize; yy++) { if (yy >= src.Top && yy < src.Bottom) { for (int xx = x; xx <= x + pixelSize; xx++) { if (xx >= src.Left && xx < src.Right) { dest.SetColorAt(xx, yy, currentAvgColor); } } } } } } }
/// <summary> /// BoxBlurVertical is a private helper method for the BoxBlur /// </summary> /// <param name="targetFastBitmap">BitmapBuffer which previously was created with BoxBlurHorizontal</param> /// <param name="range">Range must be odd!</param> private static void BoxBlurVerticalAlpha(IFastBitmap targetFastBitmap, int range) { if (!targetFastBitmap.hasAlphaChannel) { throw new NotSupportedException("BoxBlurVerticalAlpha should be called for bitmaps with alpha channel"); } int halfRange = range / 2; Color[] newColors = new Color[targetFastBitmap.Height]; byte[] tmpColor = new byte[4]; for (int x = targetFastBitmap.Left; x < targetFastBitmap.Right; x++) { int hits = 0; int a = 0; int r = 0; int g = 0; int b = 0; for (int y = targetFastBitmap.Top - halfRange; y < targetFastBitmap.Bottom; y++) { int oldPixel = y - halfRange - 1; if (oldPixel >= targetFastBitmap.Top) { targetFastBitmap.GetColorAt(x, oldPixel, tmpColor); a -= tmpColor[FastBitmap.COLOR_INDEX_A]; r -= tmpColor[FastBitmap.COLOR_INDEX_R]; g -= tmpColor[FastBitmap.COLOR_INDEX_G]; b -= tmpColor[FastBitmap.COLOR_INDEX_B]; hits--; } int newPixel = y + halfRange; if (newPixel < targetFastBitmap.Bottom) { //int colorg = pixels[index + newPixelOffset]; targetFastBitmap.GetColorAt(x, newPixel, tmpColor); a += tmpColor[FastBitmap.COLOR_INDEX_A]; r += tmpColor[FastBitmap.COLOR_INDEX_R]; g += tmpColor[FastBitmap.COLOR_INDEX_G]; b += tmpColor[FastBitmap.COLOR_INDEX_B]; hits++; } if (y >= targetFastBitmap.Top) { newColors[y - targetFastBitmap.Top] = Color.FromArgb((byte)(a / hits), (byte)(r / hits), (byte)(g / hits), (byte)(b / hits)); } } for (int y = targetFastBitmap.Top; y < targetFastBitmap.Bottom; y++) { targetFastBitmap.SetColorAt(x, y, newColors[y - targetFastBitmap.Top]); } } }
/// <summary> /// BoxBlurHorizontal is a private helper method for the BoxBlur, only for IFastBitmaps with alpha channel /// </summary> /// <param name="targetFastBitmap">Target BitmapBuffer</param> /// <param name="range">Range must be odd!</param> private static void BoxBlurHorizontalAlpha(IFastBitmap targetFastBitmap, int range) { if (!targetFastBitmap.hasAlphaChannel) { throw new NotSupportedException("BoxBlurHorizontalAlpha should be called for bitmaps with alpha channel"); } int halfRange = range / 2; Color[] newColors = new Color[targetFastBitmap.Width]; byte[] tmpColor = new byte[4]; for (int y = targetFastBitmap.Top; y < targetFastBitmap.Bottom; y++) { int hits = 0; int a = 0; int r = 0; int g = 0; int b = 0; for (int x = targetFastBitmap.Left - halfRange; x < targetFastBitmap.Right; x++) { int oldPixel = x - halfRange - 1; if (oldPixel >= targetFastBitmap.Left) { targetFastBitmap.GetColorAt(oldPixel, y, tmpColor); a -= tmpColor[FastBitmap.COLOR_INDEX_A]; r -= tmpColor[FastBitmap.COLOR_INDEX_R]; g -= tmpColor[FastBitmap.COLOR_INDEX_G]; b -= tmpColor[FastBitmap.COLOR_INDEX_B]; hits--; } int newPixel = x + halfRange; if (newPixel < targetFastBitmap.Right) { targetFastBitmap.GetColorAt(newPixel, y, tmpColor); a += tmpColor[FastBitmap.COLOR_INDEX_A]; r += tmpColor[FastBitmap.COLOR_INDEX_R]; g += tmpColor[FastBitmap.COLOR_INDEX_G]; b += tmpColor[FastBitmap.COLOR_INDEX_B]; hits++; } if (x >= targetFastBitmap.Left) { newColors[x - targetFastBitmap.Left] = Color.FromArgb((byte)(a / hits), (byte)(r / hits), (byte)(g / hits), (byte)(b / hits)); } } for (int x = targetFastBitmap.Left; x < targetFastBitmap.Right; x++) { targetFastBitmap.SetColorAt(x, y, newColors[x - targetFastBitmap.Left]); } } }
private static void BoxBlurHorizontal(IFastBitmap targetFastBitmap, int range) { var halfRange = range / 2; Parallel.For(targetFastBitmap.Top, targetFastBitmap.Bottom, y => { unsafe { var averages = stackalloc byte[range << 2]; var readColor = stackalloc byte[4]; var a = 0; var r = 0; var g = 0; var b = 0; var hits = halfRange; for (var x = targetFastBitmap.Left; x < targetFastBitmap.Left + halfRange; x++) { targetFastBitmap.GetColorAt(x, y, readColor); a += readColor[FastBitmapBase.ColorIndexA]; r += readColor[FastBitmapBase.ColorIndexR]; g += readColor[FastBitmapBase.ColorIndexG]; b += readColor[FastBitmapBase.ColorIndexB]; } for (var x = targetFastBitmap.Left; x < targetFastBitmap.Right; x++) { var leftSide = x - halfRange - 1; if (leftSide >= targetFastBitmap.Left) { // Get value at the left side of range targetFastBitmap.GetColorAt(leftSide, y, readColor); a -= readColor[FastBitmapBase.ColorIndexA]; r -= readColor[FastBitmapBase.ColorIndexR]; g -= readColor[FastBitmapBase.ColorIndexG]; b -= readColor[FastBitmapBase.ColorIndexB]; hits--; } var rightSide = x + halfRange; if (rightSide < targetFastBitmap.Right) { targetFastBitmap.GetColorAt(rightSide, y, readColor); a += readColor[FastBitmapBase.ColorIndexA]; r += readColor[FastBitmapBase.ColorIndexR]; g += readColor[FastBitmapBase.ColorIndexG]; b += readColor[FastBitmapBase.ColorIndexB]; hits++; } var writeLocation = (x % range) << 2; averages[writeLocation + FastBitmapBase.ColorIndexA] = (byte)(a / hits); averages[writeLocation + FastBitmapBase.ColorIndexR] = (byte)(r / hits); averages[writeLocation + FastBitmapBase.ColorIndexG] = (byte)(g / hits); averages[writeLocation + FastBitmapBase.ColorIndexB] = (byte)(b / hits); if (leftSide >= targetFastBitmap.Left) { // Now we can write the value from the calculated avarages var readLocation = (leftSide % range) << 2; targetFastBitmap.SetColorAt(leftSide, y, averages, readLocation); } } } }); }
private static void BoxBlurVertical(IFastBitmap targetFastBitmap, int range) { var halfRange = range / 2; Parallel.For(targetFastBitmap.Left, targetFastBitmap.Right, x => { unsafe { var readColor = stackalloc byte[4]; var averages = stackalloc byte[range << 2]; var hits = 0; var a = 0; var r = 0; var g = 0; var b = 0; for (var y = targetFastBitmap.Top; y < targetFastBitmap.Top + halfRange; y++) { targetFastBitmap.GetColorAt(x, y, readColor); a += readColor[FastBitmapBase.ColorIndexA]; r += readColor[FastBitmapBase.ColorIndexR]; g += readColor[FastBitmapBase.ColorIndexG]; b += readColor[FastBitmapBase.ColorIndexB]; hits++; } for (var y = targetFastBitmap.Top; y < targetFastBitmap.Bottom; y++) { var topSide = y - halfRange - 1; if (topSide >= targetFastBitmap.Top) { // Get value at the top side of range targetFastBitmap.GetColorAt(x, topSide, readColor); a -= readColor[FastBitmapBase.ColorIndexA]; r -= readColor[FastBitmapBase.ColorIndexR]; g -= readColor[FastBitmapBase.ColorIndexG]; b -= readColor[FastBitmapBase.ColorIndexB]; hits--; } var bottomSide = y + halfRange; if (bottomSide < targetFastBitmap.Bottom) { targetFastBitmap.GetColorAt(x, bottomSide, readColor); a += readColor[FastBitmapBase.ColorIndexA]; r += readColor[FastBitmapBase.ColorIndexR]; g += readColor[FastBitmapBase.ColorIndexG]; b += readColor[FastBitmapBase.ColorIndexB]; hits++; } var writeLocation = (y % range) << 2; averages[writeLocation + FastBitmapBase.ColorIndexA] = (byte)(a / hits); averages[writeLocation + FastBitmapBase.ColorIndexR] = (byte)(r / hits); averages[writeLocation + FastBitmapBase.ColorIndexG] = (byte)(g / hits); averages[writeLocation + FastBitmapBase.ColorIndexB] = (byte)(b / hits); if (topSide >= targetFastBitmap.Top) { // Write the value from the calculated avarages var readLocation = (topSide % range) << 2; targetFastBitmap.SetColorAt(x, topSide, averages, readLocation); } } } }); }