Пример #1
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();
            }
        }
Пример #2
0
        public static void CreateShadowImage(string fileName, int swidth)
        {
            Int32   shadowwidth = swidth;
            Int32   borderwidth = 0;
            Int32   margin      = shadowwidth;
            Int32   shadowdir   = 0;
            Double  shadowtrans = 0.0;
            Color   bkcolor     = Color.FromArgb(255, 255, 255);
            Color   shadowcolor = Color.FromArgb(0, 0, 0);
            Color   bordercolor = Color.FromArgb(0, 0, 0);
            Boolean softshadow  = true;

            String outputfile = fileName;
            String inputfile  = fileName;

            FastBitmap tmp, bmp;

            if (System.IO.File.Exists(inputfile))
            {
                tmp = new FastBitmap(inputfile);
            }
            else
            {
                Console.WriteLine("Error: Could not find file '{0}'", inputfile);
                return;
            }

            bmp = new FastBitmap(tmp.Width + borderwidth * 2, tmp.Height + borderwidth * 2, PixelFormat.Format32bppArgb);

            // add border if necessary
            if (borderwidth > 0)
            {
                SolidBrush br = new SolidBrush(bordercolor);
                Graphics   g  = Graphics.FromImage(bmp._bitmap);
                g.FillRectangle(br, 0, 0, borderwidth * 2 + tmp.Width, borderwidth * 2 + tmp.Height);
                g.Dispose();
                br.Dispose();
            }

            tmp.CopyTo(bmp, borderwidth, borderwidth, 0, 0, tmp.Width, tmp.Height);
            tmp.Dispose();

            // create image
            Int32        width  = bmp.Width + shadowwidth + margin * 2;
            Int32        height = bmp.Height + shadowwidth + margin * 2;
            LayeredImage image  = new LayeredImage(width, height);

            Int32 shadowx = 0, shadowy = 0, imgx = 0, imgy = 0;

            if (softshadow)
            {
                switch (shadowdir)
                {
                case 0:
                {
                    shadowx = margin - shadowwidth / 2;
                    shadowy = margin - shadowwidth / 2;
                    imgx    = margin;
                    imgy    = margin;
                    break;
                }

                case 1:
                {
                    shadowx = margin + shadowwidth - 3 * (shadowwidth / 2);
                    shadowy = margin - shadowwidth / 2;
                    imgx    = margin + shadowwidth;
                    imgy    = margin;
                    break;
                }

                case 2:
                {
                    shadowx = margin + shadowwidth - 3 * (shadowwidth / 2);
                    shadowy = margin + shadowwidth - 3 * (shadowwidth / 2);
                    imgx    = margin + shadowwidth;
                    imgy    = margin + shadowwidth;
                    break;
                }

                case 3:
                {
                    shadowx = margin - shadowwidth / 2;
                    shadowy = margin + shadowwidth - 3 * (shadowwidth / 2);
                    imgx    = margin;
                    imgy    = margin + shadowwidth;
                    break;
                }
                }
            }
            else
            {
                switch (shadowdir)
                {
                case 0:
                {
                    shadowx = margin;
                    shadowy = margin;
                    imgx    = margin;
                    imgy    = margin;
                    break;
                }

                case 1:
                {
                    shadowx = margin - shadowwidth;
                    shadowy = margin;
                    imgx    = margin + shadowwidth;
                    imgy    = margin;
                    break;
                }

                case 2:
                {
                    shadowx = margin - shadowwidth;
                    shadowy = margin - shadowwidth;
                    imgx    = margin + shadowwidth;
                    imgy    = margin + shadowwidth;
                    break;
                }

                case 3:
                {
                    shadowx = margin;
                    shadowy = margin - shadowwidth;
                    imgx    = margin;
                    imgy    = margin + shadowwidth;
                    break;
                }
                }
            }

            // background
            Layer bg = image.Layers.Add();

            bg.Clear(bkcolor);

            // shadow -- layer must be larger because of blur
            Layer      shadow = image.Layers.Add(width + shadowwidth, height + shadowwidth);
            SolidBrush brush  = new SolidBrush(shadowcolor);

            shadow.FillRectangle(shadowwidth, shadowwidth, bmp.Width, bmp.Height, brush);

            if (softshadow)
            {
                shadow.Blur(shadowwidth, shadowwidth);
            }

            brush.Dispose();
            shadow.OffsetX = shadowx;
            shadow.OffsetY = shadowy;
            shadow.Opacity = 1.0 - shadowtrans;

            // image
            Layer img = image.Layers.Add(bmp);

            img.OffsetX = imgx;
            img.OffsetY = imgy;

            // result
            FastBitmap result = image.Flatten();

            // save
            String filename = outputfile != "" ? outputfile : inputfile;
            String ext      = Path.GetExtension(filename);

            if (ext == "")
            {
                ext = ".bmp";
            }

            ext = ext.ToLower();
            ImageFormat imgf = ImageFormat.Bmp;

            switch (ext)
            {
            case ".bmp":
                ext  = ".bmp";
                imgf = ImageFormat.Bmp;
                break;

            case ".jpg":
                ext  = ".jpg";
                imgf = ImageFormat.Jpeg;
                break;

            case ".jpeg":
                ext  = ".jpeg";
                imgf = ImageFormat.Jpeg;
                break;

            case ".png":
                ext  = ".png";
                imgf = ImageFormat.Png;
                break;

            case ".gif":
                ext  = ".gif";
                imgf = ImageFormat.Gif;
                break;

            default:
                ext  = ".bmp";
                imgf = ImageFormat.Bmp;
                break;
            }

            result.Save(filename, imgf);
        }
Пример #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);
        }
Пример #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();
        }
Пример #5
0
 public Layer(FastBitmap bitmap)
 {
     _bitmap  = bitmap;
     _opacity = 1.0;
 }
Пример #6
0
 public Layer(Int32 width, Int32 height)
 {
     _bitmap = new FastBitmap(width, height, PixelFormat.Format32bppArgb);
     Clear(Color.Transparent);
     _opacity = 1.0;
 }