public static void Conv3x3(Bitmap b, ConvMatrix m) { if (0 == m.Factor) return; Bitmap bSrc = (Bitmap)b.Clone(); BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); BitmapData bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); int stride = bmData.Stride; int stride2 = stride * 2; unsafe { byte* pData = (byte*)bmData.Scan0; byte* pSrc = (byte*)bmSrc.Scan0; int nOffset = stride + 6 - b.Width * 3; int nWidth = b.Width - 2; int nHeight = b.Height - 2; for (int y = 0; y < nHeight; ++y) { for (int x = 0; x < nWidth; ++x) { int nPixel = ((((pSrc[2] * m.TopLeft) + (pSrc[5] * m.TopMid) + (pSrc[8] * m.TopRight) + (pSrc[2 + stride] * m.MidLeft) + (pSrc[5 + stride] * m.Pixel) + (pSrc[8 + stride] * m.MidRight) + (pSrc[2 + stride2] * m.BottomLeft) + (pSrc[5 + stride2] * m.BottomMid) + (pSrc[8 + stride2] * m.BottomRight)) / m.Factor) + m.Offset); pData[5 + stride] = (byte)Math.Max(0, Math.Min(nPixel, 255)); nPixel = ((((pSrc[1] * m.TopLeft) + (pSrc[4] * m.TopMid) + (pSrc[7] * m.TopRight) + (pSrc[1 + stride] * m.MidLeft) + (pSrc[4 + stride] * m.Pixel) + (pSrc[7 + stride] * m.MidRight) + (pSrc[1 + stride2] * m.BottomLeft) + (pSrc[4 + stride2] * m.BottomMid) + (pSrc[7 + stride2] * m.BottomRight)) / m.Factor) + m.Offset); pData[4 + stride] = (byte)Math.Max(0, Math.Min(nPixel, 255)); nPixel = ((((pSrc[0] * m.TopLeft) + (pSrc[3] * m.TopMid) + (pSrc[6] * m.TopRight) + (pSrc[0 + stride] * m.MidLeft) + (pSrc[3 + stride] * m.Pixel) + (pSrc[6 + stride] * m.MidRight) + (pSrc[0 + stride2] * m.BottomLeft) + (pSrc[3 + stride2] * m.BottomMid) + (pSrc[6 + stride2] * m.BottomRight)) / m.Factor) + m.Offset); pData[3 + stride] = (byte)Math.Max(0, Math.Min(nPixel, 255)); pData += 3; pSrc += 3; } pData += nOffset; pSrc += nOffset; } } b.UnlockBits(bmData); bSrc.UnlockBits(bmSrc); }
public static void EdgeDetectQuick(Bitmap b) { ConvMatrix m = new ConvMatrix(); m.TopLeft = m.TopMid = m.TopRight = -1; m.MidLeft = m.Pixel = m.MidRight = 0; m.BottomLeft = m.BottomMid = m.BottomRight = 1; m.Offset = 127; Conv3x3(b, m); }
public static void Sharpen(Bitmap b, int nWeight /* default to 11*/ ) { ConvMatrix m = new ConvMatrix(); m.SetAll(0); m.Pixel = nWeight; m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = -2; m.Factor = nWeight - 8; Conv3x3(b, m); }
public static void Smooth(Bitmap b, int nWeight /* default to 1 */) { ConvMatrix m = new ConvMatrix(); m.SetAll(1); m.Pixel = nWeight; m.Factor = nWeight + 8; Conv3x3(b, m); }
public static void GaussianBlur(Bitmap b, int nWeight /* default to 4*/) { ConvMatrix m = new ConvMatrix(); m.SetAll(1); m.Pixel = nWeight; m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 2; m.Factor = nWeight + 12; Conv3x3(b, m); }