示例#1
0
        public void Blur(Int32 horz, Int32 vert)
        {
            Single weightsum;

            Single[] weights;

            FastBitmap t = (FastBitmap)_bitmap.Clone();

            _bitmap.Lock();
            t.Lock();

            // horizontal blur

            weights = new Single[horz * 2 + 1];

            for (Int32 i = 0; i < horz * 2 + 1; i++)
            {
                Single y = Gauss(-horz + i, 0, horz);
                weights[i] = y;
            }

            for (Int32 row = 0; row < _bitmap.Height; row++)
            {
                for (Int32 col = 0; col < _bitmap.Width; col++)
                {
                    Double r = 0;
                    Double g = 0;
                    Double b = 0;
                    Double a = 0;
                    weightsum = 0;

                    for (Int32 i = 0; i < horz * 2 + 1; i++)
                    {
                        Int32 x = col - horz + i;

                        if (x < 0)
                        {
                            i += -x;
                            x  = 0;
                        }

                        if (x > _bitmap.Width - 1)
                        {
                            break;
                        }

                        Color c = _bitmap.GetPixel(x, row);
                        r         += c.R * weights[i] / 255.0 * c.A;
                        g         += c.G * weights[i] / 255.0 * c.A;
                        b         += c.B * weights[i] / 255.0 * c.A;
                        a         += c.A * weights[i];
                        weightsum += weights[i];
                    }

                    r /= weightsum;
                    g /= weightsum;
                    b /= weightsum;
                    a /= weightsum;
                    Byte br = (Byte)Math.Round(r);
                    Byte bg = (Byte)Math.Round(g);
                    Byte bb = (Byte)Math.Round(b);
                    Byte ba = (Byte)Math.Round(a);

                    if (br > 255)
                    {
                        br = 255;
                    }

                    if (bg > 255)
                    {
                        bg = 255;
                    }

                    if (bb > 255)
                    {
                        bb = 255;
                    }

                    if (ba > 255)
                    {
                        ba = 255;
                    }

                    t.SetPixel(col, row, Color.FromArgb(ba, br, bg, bb));
                }
            }

            // vertical blur
            weights = new Single[vert * 2 + 1];

            for (Int32 i = 0; i < vert * 2 + 1; i++)
            {
                Single y = Gauss(-vert + i, 0, vert);
                weights[i] = y;
            }

            for (Int32 col = 0; col < _bitmap.Width; col++)
            {
                for (Int32 row = 0; row < _bitmap.Height; row++)
                {
                    Double r = 0;
                    Double g = 0;
                    Double b = 0;
                    Double a = 0;
                    weightsum = 0;

                    for (Int32 i = 0; i < vert * 2 + 1; i++)
                    {
                        Int32 y = row - vert + i;

                        if (y < 0)
                        {
                            i += -y;
                            y  = 0;
                        }

                        if (y > _bitmap.Height - 1)
                        {
                            break;
                        }

                        Color c = t.GetPixel(col, y);
                        r         += c.R * weights[i] / 255.0 * c.A;
                        g         += c.G * weights[i] / 255.0 * c.A;
                        b         += c.B * weights[i] / 255.0 * c.A;
                        a         += c.A * weights[i];
                        weightsum += weights[i];
                    }

                    r /= weightsum;
                    g /= weightsum;
                    b /= weightsum;
                    a /= weightsum;
                    Byte br = (Byte)Math.Round(r);
                    Byte bg = (Byte)Math.Round(g);
                    Byte bb = (Byte)Math.Round(b);
                    Byte ba = (Byte)Math.Round(a);

                    if (br > 255)
                    {
                        br = 255;
                    }

                    if (bg > 255)
                    {
                        bg = 255;
                    }

                    if (bb > 255)
                    {
                        bb = 255;
                    }

                    if (ba > 255)
                    {
                        ba = 255;
                    }

                    _bitmap.SetPixel(col, row, Color.FromArgb(ba, br, bg, bb));
                }
            }

            t.Dispose();             // will unlock
            _bitmap.Unlock();
        }
示例#2
0
 public Color GetPixel(Int32 x, Int32 y)
 {
     return(_bitmap.GetPixel(x, y));
 }