public static bool CalculateAlpha(Bitmap bitmap1, Bitmap bitmap2, ConvMatrix m) { // Avoid divide by zero errors // if (0 == m.Factor) return false; // GDI+ still lies to us - the return format is BGR, NOT RGB. BitmapData bm1 = bitmap1.LockBits(new Rectangle(0, 0, bitmap1.Width, bitmap1.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); BitmapData bm2 = bitmap2.LockBits(new Rectangle(0, 0, bitmap2.Width, bitmap2.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); int stride1 = bm1.Stride; int stride2 = bm2.Stride; System.IntPtr Scan1_0 = bm1.Scan0; System.IntPtr Scan2_0 = bm2.Scan0; unsafe { byte* p1 = (byte*)(void*)Scan1_0; byte* p2 = (byte*)(void*)Scan2_0; int nOffset = stride1 + 8 - bitmap1.Width * 4; int nWidth = bitmap1.Width ; int nHeight = bitmap1.Height ; int nSize = nWidth * nHeight; int r1,r2,g1,g2,b1,b2,diff; for (int i = 0; i <= nSize; ++i) { //for (int y = 0; y <= nHeight; ++y) { // for (int x = 0; x <= nWidth; ++x) { r1 = p1[2]; g1 = p1[1]; b1 = p1[0]; r2 = p2[2]; g2 = p2[1]; b2 = p2[0]; diff = Math.Abs((r1 + g1 + b1) / 3 - (r2 + g2 + b2) / 3); p2[0] = (byte)(((int)p1[0] + (int)p2[0]) / 2); p2[1] = (byte)(((int)p1[1] + (int)p2[1]) / 2); p2[2] = (byte)(((int)p1[2] + (int)p2[2]) / 2); p2[3] = (byte)(255 - diff); p1 += 4; p2 += 4; //} //p1 += nOffset; //p2 += nOffset; } } bitmap1.UnlockBits(bm1); bitmap2.UnlockBits(bm2); return true; }
public static bool 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; return BitmapFilter.Conv3x3(b, m); }
//[DllImport("gdi32.dll")] //static extern uint SetPixel(IntPtr hdc, int X, int Y, uint crColor); //[DllImport("gdi32.dll")] //static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos); //public static bool Conv3x3(IntPtr hDC, int nHeight, int nWidth, ConvMatrix m) { // // Avoid divide by zero errors // if (0 == m.Factor) return false; // unsafe { // int nPixel; // byte[] color; // for (int y = 0; y < nHeight; ++y) { // for (int x = 0; x < nWidth; ++x) { // color = GetPixel(hDC, x, y); // 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); // if (nPixel < 0) nPixel = 0; // if (nPixel > 255) nPixel = 255; // p[5 + stride] = (byte)nPixel; // 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); // if (nPixel < 0) nPixel = 0; // if (nPixel > 255) nPixel = 255; // p[4 + stride] = (byte)nPixel; // 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); // if (nPixel < 0) nPixel = 0; // if (nPixel > 255) nPixel = 255; // p[3 + stride] = (byte)nPixel; // p += 3; // pSrc += 3; // } // p += nOffset; // pSrc += nOffset; // } // } // b.UnlockBits(bmData); // bSrc.UnlockBits(bmSrc); // return true; //} public static bool Smooth(Bitmap b, int nWeight /* default to 1 */) { ConvMatrix m = new ConvMatrix(); m.SetAll(1); m.Pixel = nWeight; m.Factor = nWeight + 8; return BitmapFilter.Conv3x3(b, m); }
public static bool MeanRemoval(Bitmap b, int nWeight /* default to 9*/ ) { ConvMatrix m = new ConvMatrix(); m.SetAll(-1); m.Pixel = nWeight; m.Factor = nWeight - 8; return BitmapFilter.Conv3x3(b, m); }
public static bool 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; return BitmapFilter.Conv3x3(b, m); }
public static bool EmbossLaplacian(Bitmap b) { ConvMatrix m = new ConvMatrix(); m.SetAll(-1); m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 0; m.Pixel = 4; m.Offset = 127; return BitmapFilter.Conv3x3(b, m); }
public static bool 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; return BitmapFilter.Conv3x3(b, m); }
public static bool Conv3x3(Bitmap b, ConvMatrix m) { // Avoid divide by zero errors if (0 == m.Factor) return false; Bitmap bSrc = (Bitmap)b.Clone(); // GDI+ still lies to us - the return format is BGR, NOT RGB. 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; System.IntPtr Scan0 = bmData.Scan0; System.IntPtr SrcScan0 = bmSrc.Scan0; unsafe { byte* p = (byte*)(void*)Scan0; byte* pSrc = (byte*)(void*)SrcScan0; int nOffset = stride + 6 - b.Width * 3; int nWidth = b.Width - 2; int nHeight = b.Height - 2; int nPixel; for (int y = 0; y < nHeight; ++y) { for (int x = 0; x < nWidth; ++x) { 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); if (nPixel < 0) nPixel = 0; if (nPixel > 255) nPixel = 255; p[5 + stride] = (byte)nPixel; 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); if (nPixel < 0) nPixel = 0; if (nPixel > 255) nPixel = 255; p[4 + stride] = (byte)nPixel; 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); if (nPixel < 0) nPixel = 0; if (nPixel > 255) nPixel = 255; p[3 + stride] = (byte)nPixel; p += 3; pSrc += 3; } p += nOffset; pSrc += nOffset; } } b.UnlockBits(bmData); bSrc.UnlockBits(bmSrc); return true; }