Ejemplo n.º 1
0
 public void Clear(Color c)
 {
     m_bmpImage.Lock();
     for (Int32 y = 0; y < m_bmpImage.Height; y++)
     {
         for (Int32 x = 0; x < m_bmpImage.Width; x++)
         {
             m_bmpImage.SetPixel(x, y, c);
         }
     }
     m_bmpImage.Unlock();
 }
Ejemplo n.º 2
0
        internal FastBitmap Flatten(Color crBack)
        {
            // create a bitmap for result image

            FastBitmap bmpFinal = new FastBitmap(m_nWidth, m_nHeight, PixelFormat.Format24bppRgb);

            // lock all bitmaps
            bmpFinal.Lock();
            for (Int32 i = 0; i < m_arLayers.Count; i++)
            {
                Layer layer = m_arLayers[i];
                layer.m_bmpImage.Lock();
                if (layer.Mask != null)
                {
                    layer.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 < m_nHeight; y++)
            {
                for (Int32 x = 0; x < m_nWidth; x++)
                {
                    Color c0 = m_arLayers[0].m_bmpImage.GetPixel(x, y);
                    for (Int32 i = 1; i < m_arLayers.Count; i++)
                    {
                        Layer layer = m_arLayers[i];
                        Color c1    = Color.Transparent;
                        if (x >= layer.OffsetX &&
                            x <= layer.OffsetX + layer.m_bmpImage.Width - 1 &&
                            y >= layer.OffsetY &&
                            y <= layer.OffsetY + layer.m_bmpImage.Height - 1)
                        {
                            c1 = layer.m_bmpImage.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);

                            if (i == 4) // shadow layer
                            {
                                tb = Math.Round(tb);
                                tr = Math.Min(tr, crBack.R);
                                tg = Math.Min(tg, crBack.G);
                                tb = Math.Min(tb, crBack.B);
                            }
                            else
                            {
                                tr = Math.Min(tr, 255);
                                tg = Math.Min(tg, 255);
                                tb = Math.Min(tb, 255);
                            }

                            c0 = Color.FromArgb((Byte)tr, (Byte)tg, (Byte)tb);
                        }
                    }
                    bmpFinal.SetPixel(x, y, c0);
                }
            }

            // unlock all bitmaps
            for (Int32 i = 0; i < m_arLayers.Count; i++)
            {
                Layer layer = m_arLayers[i];
                layer.m_bmpImage.Unlock();
                if (layer.Mask != null)
                {
                    layer.Mask.Unlock();
                }
            }

            bmpFinal.Unlock();

            return(bmpFinal);
        }
Ejemplo n.º 3
0
        public void Blur(Int32 nHorz, Int32 nVert)
        {
            Single nWeightSum;

            Single[] nWeights;

            FastBitmap bmpBlur = (FastBitmap)m_bmpImage.Clone();

            m_bmpImage.Lock();
            bmpBlur.Lock();

            // horizontal blur

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

            for (Int32 row = 0; row < m_bmpImage.Height; row++)
            {
                for (Int32 col = 0; col < m_bmpImage.Width; col++)
                {
                    Double r = 0;
                    Double g = 0;
                    Double b = 0;
                    nWeightSum = 0;
                    for (Int32 i = 0; i < nHorz * 2 + 1; i++)
                    {
                        Int32 x = col - nHorz + i;
                        if (x < 0)
                        {
                            i += -x;
                            x  = 0;
                        }
                        if (x > m_bmpImage.Width - 1)
                        {
                            break;
                        }
                        Color c = m_bmpImage.GetPixel(x, row);
                        r          += c.R * nWeights[i];
                        g          += c.G * nWeights[i];
                        b          += c.B * nWeights[i];
                        nWeightSum += nWeights[i];
                    }
                    r /= nWeightSum;
                    g /= nWeightSum;
                    b /= nWeightSum;
                    Byte br = (Byte)Math.Round(r);
                    Byte bg = (Byte)Math.Round(g);
                    Byte bb = (Byte)Math.Round(b);
                    if (br > 255)
                    {
                        br = 255;
                    }
                    if (bg > 255)
                    {
                        bg = 255;
                    }
                    if (bb > 255)
                    {
                        bb = 255;
                    }
                    bmpBlur.SetPixel(col, row, Color.FromArgb(br, bg, bb));
                }
            }

            // vertical blur

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

            for (Int32 col = 0; col < m_bmpImage.Width; col++)
            {
                for (Int32 row = 0; row < m_bmpImage.Height; row++)
                {
                    Double r = 0;
                    Double g = 0;
                    Double b = 0;
                    nWeightSum = 0;
                    for (Int32 i = 0; i < nVert * 2 + 1; i++)
                    {
                        Int32 y = row - nVert + i;
                        if (y < 0)
                        {
                            i += -y;
                            y  = 0;
                        }
                        if (y > m_bmpImage.Height - 1)
                        {
                            break;
                        }
                        Color c = bmpBlur.GetPixel(col, y);
                        r          += c.R * nWeights[i];
                        g          += c.G * nWeights[i];
                        b          += c.B * nWeights[i];
                        nWeightSum += nWeights[i];
                    }
                    r /= nWeightSum;
                    g /= nWeightSum;
                    b /= nWeightSum;
                    Byte br = (Byte)Math.Round(r);
                    Byte bg = (Byte)Math.Round(g);
                    Byte bb = (Byte)Math.Round(b);
                    if (br > 255)
                    {
                        br = 255;
                    }
                    if (bg > 255)
                    {
                        bg = 255;
                    }
                    if (bb > 255)
                    {
                        bb = 255;
                    }
                    m_bmpImage.SetPixel(col, row, Color.FromArgb(br, bg, bb));
                }
            }

            bmpBlur.Dispose();          // will unlock
            m_bmpImage.Unlock();
        }