马赛克算法

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(0, 0, 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;
        }

你可能感兴趣的:(马赛克算法)