www.pudn.com > Gaussian_Smoothing.rar > ConvOperation.cs


using System; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
 
 
namespace Gaussian_Smoothing 
{ 
	///  
	/// Summary description for ConvOperation. 
	///  
	public class ConvOperation 
	{ 
		public ConvOperation() 
		{ 
			// 
			// TODO: Add constructor logic here 
			// 
		} 
 
		public static Bitmap GaussianSmoothing(Bitmap b,int nWidth) 
		{ 
			CSharpMask m=new CSharpMask(); 
			m.setAll(2); 
			m.Pixel=nWidth; 
			m.Factor=16; 
			m.Offset=0; 
			m.BottomLeft=1; 
			m.BottomRight=1; 
			m.TopLeft=1; 
			m.TopRight=1; 
			 
			return simpleConv(b,m); 
		} 
 
		public static Bitmap simpleConv(Bitmap b,CSharpMask m) 
		{ 
			if (m.Factor==0) 
				return b; 
			 
			Bitmap OutPutImage; 
			OutPutImage=(Bitmap)b.Clone(); 
			BitmapData bData=b.LockBits(new Rectangle(0,0,b.Width,b.Height),System.Drawing.Imaging.ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
			BitmapData copyData=b.LockBits(new Rectangle(0,0,OutPutImage.Width,OutPutImage.Height),ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb); 
			int stride=bData.Stride; 
			int stride2=stride*2; 
             
			System.IntPtr ptr=bData.Scan0; 
			System.IntPtr ptrOutPut=copyData.Scan0; 
 
			unsafe 
			{ 
				byte *p=(byte*)(void*)ptr; 
				byte *pOutPut=(byte*)(void*)ptrOutPut; 
 
				int nOffset=stride-b.Width*3; 
				int nWidth=b.Width-2; 
				int nHeight=b.Height-2; 
				int nPixel; 
					 
				for (int x=0;x255) nPixel=255; 
						pOutPut[5+stride]=(byte)nPixel; 
						nPixel = ( ( ( (pOutPut[1] * m.TopLeft) + (pOutPut[4] * m.TopMiddle) +  
							(pOutPut[7] * m.TopRight) + (pOutPut[1 + stride] * m.MiddleLeft) +  
							(pOutPut[4 + stride] * m.Pixel) + (pOutPut[7 + stride] * m.MiddleRight) +  
							(pOutPut[1 + stride2] * m.BottomLeft) +  
							(pOutPut[4 + stride2] * m.BottomMiddle) +  
							(pOutPut[7 + stride2] * m.BottomRight))  
							/ m.Factor) + m.Offset);  
                     
						if (nPixel < 0) nPixel = 0;  
						if (nPixel > 255) nPixel = 255;  
						p[4 + stride] = (byte)nPixel;  
                 
						nPixel = ( ( ( (pOutPut[0] * m.TopLeft) + (pOutPut[3] * m.TopMiddle) +  
							(pOutPut[6] * m.TopRight) + (pOutPut[0 + stride] * m.MiddleLeft) +  
							(pOutPut[3 + stride] * m.Pixel) +  
							(pOutPut[6 + stride] * m.MiddleRight) +  
							(pOutPut[0 + stride2] * m.BottomLeft) +  
							(pOutPut[3 + stride2] * m.BottomMiddle) +  
							(pOutPut[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;  
						pOutPut += 3;  
					}  
             
				p += nOffset;  
				pOutPut += nOffset;  
			} 
			 
			b.UnlockBits(bData);  
			OutPutImage.UnlockBits(copyData); 			 
 
			return OutPutImage;  
		} 
 
	} 
}