C#中马赛克算法

        public static Bitmap KiMosaic(Bitmap b, int val)
        {
            
if (b.Equals(null)) { return null; }

            
int w = b.Width; 
            
int h = b.Height;
            
int stdR, stdG, stdB;
            stdR 
= 0; stdG = 0; stdB = 0;

            BitmapData srcData 
= b.LockBits(new Rectangle(00, w, h), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            
unsafe
            {
                
byte* p = (byte*)srcData.Scan0.ToPointer();
                
for (int y = 0; y < h; y++)
                {
                    
for (int x = 0; x < w; x++)
                    {
                        
if (y % val == 0)
                        {
                            
if (x % val == 0) { stdR = p[2]; stdG = p[1]; stdB = p[0]; }
                            
else { p[0= (byte)stdB; p[1= (byte)stdG; p[2= (byte)stdR; }
                        }
                        
else
                        {  
                            
byte* pTemp = p - srcData.Stride;
                            p[
0= (byte)pTemp[0]; 
                            p[
1= (byte)pTemp[1];
                            p[
2= (byte)pTemp[2];
                        } p 
+= 3;
                    }
                    p 
+= srcData.Stride - w * 3;
                }
                b.UnlockBits(srcData);
            }
            
return b;
        }

 

posted @ 2009-12-04 11:26  李久寧  阅读(580)  评论(0编辑  收藏  举报