static void gbp_Done(Bitmap Bmp, BitmapData BmpData, ColorStructure rgbValues) { timer1.Stop(); Console.WriteLine("> Processing completed in {0}.", timer1.Elapsed); Marshal.Copy(ConvertToSingle(rgbValues, Bmp.Height, Bmp.Width, BmpData.Stride), 0, BmpData.Scan0, Math.Abs(BmpData.Height * BmpData.Stride)); Bmp.UnlockBits(BmpData); Bmp.Save(outputFileAddr); }
public static byte[] ConvertToSingle(ColorStructure colors, int height, int width, int stride) { byte[] Res = new byte[Math.Abs(stride) * height]; int redOffset = 2; int greenOffset = 1; int blueOffset = 0; int paddingOffset; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Res[redOffset] = colors.Red[i, j]; redOffset += 3; } for (int j = 0; j < width; j++) { Res[greenOffset] = colors.Green[i, j]; greenOffset += 3; } for (int j = 0; j < width; j++) { Res[blueOffset] = colors.Blue[i, j]; blueOffset += 3; } paddingOffset = (stride * i) + width * 3; for (int j = 0; j < colors.GenericPadding.GetLength(1); j++) { Res[paddingOffset] = colors.GenericPadding[i, j]; paddingOffset++; } redOffset += colors.GenericPadding.GetLength(1); greenOffset += colors.GenericPadding.GetLength(1); blueOffset += colors.GenericPadding.GetLength(1); } return Res; }
public static ColorStructure ConvertTo2D(ref BitmapData bmpData, ref Bitmap bmp) { ColorStructure colors = new ColorStructure(); byte[] commonRGB = new byte[bmpData.Stride]; colors.Red = new byte[bmpData.Height, bmpData.Width]; colors.Green = new byte[bmpData.Height, bmpData.Width]; colors.Blue = new byte[bmpData.Height, bmpData.Width]; colors.GenericPadding = new byte[bmpData.Height, Math.Abs(bmpData.Stride - (bmpData.Width * 3))]; int commonOffset = 0; for (int i = 0; i < bmpData.Height; i++) { Marshal.Copy(bmpData.Scan0 + (i * Math.Abs(bmpData.Stride)), commonRGB, 0, Math.Abs(bmpData.Stride)); commonOffset = 2; for (int j = 0; j < bmpData.Width; j++) { colors.Red[i, j] = commonRGB[commonOffset]; commonOffset += 3; } commonOffset = 1; for (int j = 0; j < bmpData.Width; j++) { colors.Green[i, j] = commonRGB[commonOffset]; commonOffset += 3; } commonOffset = 0; for (int j = 0; j < bmpData.Width; j++) { colors.Blue[i, j] = commonRGB[commonOffset]; commonOffset += 3; } commonOffset = 0; for (int j = bmpData.Stride - (bmpData.Stride - (bmpData.Width * 3)); j < bmpData.Stride; j++) { colors.GenericPadding[i, commonOffset] = commonRGB[j]; commonOffset++; } } return colors; }
private void __ProcessRedV(ColorStructure rgbValues, GaussianDistribution gd, Rectangle blurRect) { #region Up -> Down for (int j = blurRect.X; j <= blurRect.X + blurRect.Width; j++) { for (int i = blurRect.Y; i <= blurRect.Y + blurRect.Height; i++) { double newValue = 0.0; double divisor = 0.0; int kernelAddr = 0; for (int rad = -gd.GetRadius; rad <= gd.GetRadius; rad++) { kernelAddr = rad + gd.GetRadius; if ((i + rad >= blurRect.Y) && (i + rad <= blurRect.Y + blurRect.Height)) { newValue += rgbValues.Red[i + rad, j] * gd.GetKernel[kernelAddr]; divisor += gd.GetKernel[kernelAddr]; } } rgbValues.Red[i, j] = Convert.ToByte(newValue / divisor); newValue = 0.0; } } #endregion }
private void __ProcessRedH(ColorStructure rgbValues, GaussianDistribution gd, Rectangle blurRect) { #region Left -> Right for (int i = blurRect.Y; i <= blurRect.Y + blurRect.Height; i++) { for (int j = blurRect.X; j <= blurRect.X + blurRect.Width; j++) { double newValue = 0.0; double divisor = 0.0; int kernelAddr; for (int rad = -gd.GetRadius; rad <= gd.GetRadius; rad++) { kernelAddr = rad + gd.GetRadius; if (j + rad >= blurRect.X && j + rad <= blurRect.X + blurRect.Width) { newValue += rgbValues.Red[i, j + rad] * gd.GetKernel[kernelAddr]; divisor += gd.GetKernel[kernelAddr]; } } rgbValues.Red[i, j] = Convert.ToByte(newValue / divisor); newValue = 0.0; if (rgbValues.Red[i, j] + gd.GetOffset <= 255 && rgbValues.Red[i, j] + gd.GetOffset >= 0) rgbValues.Red[i, j] = (byte)(rgbValues.Red[i, j] + gd.GetOffset); else if (rgbValues.Red[i, j] + gd.GetOffset <= 0) rgbValues.Red[i, j] = 0; else if (rgbValues.Red[i, j] + gd.GetOffset >= 255) rgbValues.Red[i, j] = 255; } } #endregion }
public GaussianBlurProcessing(GaussianDistribution gaussianKernel, ref ColorStructure rgbValues, Rectangle blurRectangle, Bitmap bmp, BitmapData bmpData) { this._GaussianKernel = gaussianKernel; this._RGBValues = rgbValues; this._BlurRect = blurRectangle; this._Bitmap = bmp; this._BitmapData = bmpData; }