Example #1
0
        public void Clear(Color c)
        {
            _bitmap.Lock();

            for (Int32 y = 0; y < _bitmap.Height; y++)
            {
                for (Int32 x = 0; x < _bitmap.Width; x++)
                {
                    _bitmap.SetPixel(x, y, c);
                }
            }

            _bitmap.Unlock();
        }
Example #2
0
        public void CopyTo(FastBitmap bitmap, Int32 destx, Int32 desty, Int32 srcx, Int32 srcy, Int32 width, Int32 height)
        {
            try
            {
                Lock();
                bitmap.Lock();

                for (Int32 y = 0; y < height; y++)
                {
                    for (Int32 x = 0; x < width; x++)
                    {
                        Color c = GetPixel(srcx + x, srcy + y);
                        bitmap.SetPixel(destx + x, desty + y, c);
                    }
                }
            }
            finally
            {
                Unlock();
                bitmap.Unlock();
            }
        }
Example #3
0
        internal FastBitmap Flatten()
        {
            // create a bitmap for result image
            FastBitmap final = new FastBitmap(_width, _height, PixelFormat.Format24bppRgb);

            // lock all bitmaps
            final.Lock();

            for (Int32 i = 0; i < _layers.Count; i++)
            {
                Layer l = _layers[i];
                l._bitmap.Lock();

                if (l.Mask != null)
                {
                    l.Mask.Lock();
                }
            }

            // calculate colors of flattened image
            // 1. take offsetx, offsety into consideration
            // 2. calculate alpha of color (alpha, opacity, mask)
            // 3. mix colors of current layer and layer below
            for (Int32 y = 0; y < _height; y++)
            {
                for (Int32 x = 0; x < _width; x++)
                {
                    Color c0 = _layers[0]._bitmap.GetPixel(x, y);

                    for (Int32 i = 1; i < _layers.Count; i++)
                    {
                        Layer layer = _layers[i];
                        Color c1    = Color.Transparent;

                        if (x >= layer.OffsetX &&
                            x <= layer.OffsetX + layer._bitmap.Width - 1 &&
                            y >= layer.OffsetY &&
                            y <= layer.OffsetY + layer._bitmap.Height - 1)
                        {
                            c1 = layer._bitmap.GetPixel(x - layer.OffsetX,
                                                        y - layer.OffsetY);
                        }

                        if (c1.A == 255 && layer.Opacity == 1.0 && layer.Mask == null)
                        {
                            c0 = c1;
                        }
                        else
                        {
                            Double tr, tg, tb, a;
                            a = c1.A / 255.0 * layer.Opacity;

                            if (layer.Mask != null)
                            {
                                a *= layer.Mask.GetIntensity(x, y) / 255.0;
                            }

                            tr = c1.R * a + c0.R * (1.0 - a);
                            tg = c1.G * a + c0.G * (1.0 - a);
                            tb = c1.B * a + c0.B * (1.0 - a);
                            tr = Math.Round(tr);
                            tg = Math.Round(tg);
                            tb = Math.Round(tb);
                            tr = Math.Min(tr, 255);
                            tg = Math.Min(tg, 255);
                            tb = Math.Min(tb, 255);
                            c0 = Color.FromArgb((Byte)tr, (Byte)tg, (Byte)tb);
                        }
                    }
                    final.SetPixel(x, y, c0);
                }
            }

            // unlock all bitmaps
            for (Int32 i = 0; i < _layers.Count; i++)
            {
                Layer l = _layers[i];
                l._bitmap.Unlock();

                if (l.Mask != null)
                {
                    l.Mask.Unlock();
                }
            }

            final.Unlock();

            return(final);
        }
Example #4
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();
        }