示例#1
0
		public static void Smooth(Bitmap b, int nWeight)
		{
            ConvMatrix m = new ConvMatrix();
			m.SetAll(1);
			m.Pixel = nWeight;
			m.Factor = nWeight + 8;

			Conv3x3(b, m);
		}
		public static bool Smooth(Bitmap b, int nWeight /* default to 1 */)
		{
			ConvMatrix m = new ConvMatrix();
			m.SetAll(1);
			m.Pixel = nWeight;
			m.Factor = nWeight + 8;

			return Conv3x3(b, m);
		}
示例#3
0
            public static bool Smooth(Bitmap bitmap, int nWeight)
            {
                ConvMatrix m = new ConvMatrix();

                m.SetAll(1);
                m.Pixel  = nWeight;
                m.Factor = nWeight + 8;

                return(BitmapFilter.Conv3x3(bitmap, m));
            }
示例#4
0
            public static bool MeanRemoval(Bitmap bitmap, int nWeight)
            {
                ConvMatrix m = new ConvMatrix();

                m.SetAll(-1);
                m.Pixel  = nWeight;
                m.Factor = nWeight - 8;

                return(BitmapFilter.Conv3x3(bitmap, m));
            }
示例#5
0
        private static bool Smooth(Bitmap b, int nWeight /* default to 1 */)
        {
            ConvMatrix m = new ConvMatrix();

            m.SetAll(1);
            m.Pixel  = nWeight;
            m.Factor = nWeight + 8;

            return(Conv3x3(b, m));
        }
示例#6
0
        public static bool GaussianBlur(Bitmap b, int nWeight /* default to 4*/)
        {
            ConvMatrix m = new ConvMatrix();
            m.SetAll(1);
            m.Pixel = nWeight;
            m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 2;
            m.Factor = nWeight + 12;

            return BitmapFilter.Conv3x3(b, m);
        }
示例#7
0
        public static bool MeanRemoval(Bitmap b, int nWeight /* default to 9*/)
        {
            ConvMatrix m = new ConvMatrix();

            m.SetAll(-1);
            m.Pixel  = nWeight;
            m.Factor = nWeight - 8;

            return(Conv3x3(b, m));
        }
        public static bool GaussianBlur(this Bitmap b, int nWeight /* default to 4*/)
        {
            var m = new ConvMatrix();

            m.SetAll(1);
            m.Pixel  = nWeight;
            m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 2;
            m.Factor = nWeight + 30;

            return(b.Conv3X3(m));
        }
        public static bool Sharpen(Bitmap b, int nWeight /* default to 11*/)
        {
            ConvMatrix m = new ConvMatrix();

            m.SetAll(0);
            m.Pixel  = nWeight;
            m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = -2;
            m.Factor = nWeight - 8;

            return(Conv3x3(b, m));
        }
示例#10
0
        public static Bitmap Emboss(Bitmap b, int Offset)
        {
            ConvMatrix m = new ConvMatrix();

            m.SetAll(-1);
            m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 0;
            m.Pixel  = 4;
            m.Offset = Offset;

            return(Conv3x3(b, m));
        }
示例#11
0
            public static bool Sharpen(Bitmap bitmap, int nWeight)
            {
                ConvMatrix m = new ConvMatrix();

                m.SetAll(0);
                m.Pixel  = nWeight;
                m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = -2;
                m.Factor = nWeight - 8;

                return(BitmapFilter.Conv3x3(bitmap, m));
            }
        public static bool EmbossLaplacian(Bitmap b)
        {
            ConvMatrix m = new ConvMatrix();

            m.SetAll(-1);
            m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 0;
            m.Pixel  = 4;
            m.Offset = 127;

            return(Conv3x3(b, m));
        }
示例#13
0
        public static bool GaussianBlur(Bitmap b, int nWeight /* default to 4*/)
        {
            ConvMatrix m = new ConvMatrix();

            m.SetAll(1);
            m.Pixel  = nWeight;
            m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 2;
            m.Factor = nWeight + 12;

            return(BitmapFilter.Conv3x3(b, m));
        }
示例#14
0
        public Image ProcessImage(ImageFactory factory)
        {
            var        nWeight = (int)DynamicParameter;
            ConvMatrix m       = new ConvMatrix();

            m.SetAll(0);
            m.Pixel  = nWeight;
            m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = -2;
            m.Factor = nWeight - 8;
            return(ImageHelper.Conv3x3(factory.Bitmap, m));
        }
示例#15
0
        public static bool GaussianBlur(Bitmap b)
        {
            ConvMatrix m = new ConvMatrix();

            m.TopLeft = m.TopRight = m.BottomLeft = m.BottomRight = 1;
            m.Pixel   = 4;
            m.TopMid  = m.MidLeft = m.MidRight = m.BottomMid = 2;

            m.Factor = 16;

            return(Conv3x3(b, m));
        }
示例#16
0
        void expandSelection(int numVerts, ref byte[] map, int width, int height)
        {
            ConvMatrix filter = new ConvMatrix(1, 1, 1, 1, 0, 1, 1, 1, 1);

            filter.mFactor = 1;
            filter.mOffset = 0;

            for (int i = 0; i < numVerts; i++)
            {
                applyConvMatrix(filter, ref map, width, height);
            }
        }
示例#17
0
        static public void sharpenFilter()
        {
            ConvMatrix filter = new ConvMatrix(0, -2, 0, -2, 11, -2, 0, -2, 0);

            filter.mFactor = 3;
            filter.mOffset = 0;


            applyConvMatrix(filter);

            rebuildVisualsAfterSelection();
        }
        public static bool EdgeDetectQuick(Bitmap b)
        {
            ConvMatrix m = new ConvMatrix();

            m.TopLeft    = m.TopMid = m.TopRight = -1;
            m.MidLeft    = m.Pixel = m.MidRight = 0;
            m.BottomLeft = m.BottomMid = m.BottomRight = 1;

            m.Offset = 127;

            return(Conv3x3(b, m));
        }
示例#19
0
        static public void smoothFilter()
        {
            //use a gaussin matrix
            ConvMatrix filter = new ConvMatrix(1, 2, 1, 2, 4, 2, 1, 2, 1);

            filter.mFactor = 16;
            filter.mOffset = 0;

            applyConvMatrix(filter);

            rebuildVisualsAfterSelection();
        }
示例#20
0
        static public void expandSelection(int numVerts)
        {
            ConvMatrix filter = new ConvMatrix(1, 1, 1, 1, 0, 1, 1, 1, 1);

            filter.mFactor = 1;
            filter.mOffset = 0;

            for (int i = 0; i < numVerts; i++)
            {
                applyConvMatrix(filter);
            }

            rebuildVisualsAfterSelection();
        }
示例#21
0
        public static void Smooth(Bitmap b, int nWeight)
        {
            if (nWeight == 0)
            {
                return;
            }

            var m = new ConvMatrix();

            m.SetAll(1);
            m.Pixel  = nWeight;
            m.Factor = nWeight + 8;

            Conv3X3(b, m);
        }
示例#22
0
        static public void contractSelection(int numVerts)
        {
            Masking.invertSelectionMask();
            ConvMatrix filter = new ConvMatrix(1, 1, 1, 1, 0, 1, 1, 1, 1);

            filter.mFactor = 1;
            filter.mOffset = 0;

            for (int i = 0; i < numVerts; i++)
            {
                applyConvMatrix(filter);
            }


            Masking.invertSelectionMask();
        }
示例#23
0
        public static bool Blur(ref Bitmap b, int intensity = 1)
        {
            int        step0 = intensity;
            int        step1 = intensity / 2;
            int        step2 = intensity / 4;
            ConvMatrix blur  = new ConvMatrix();

            //ConvMatrix blur = new ConvMatrix(
            //    new int[,] {
            //    { step2, step1, step2 },
            //    { step1, step0, step1 },
            //    { step2, step1, step2 }
            //    });
            //blur.Factor = intensity * intensity;
            blur.SetAll(1);
            blur.Pixel  = intensity;
            blur.Factor = intensity + 8;
            return(Conv3x3(ref b, blur));
        }
示例#24
0
        private ZagImage <byte> Convolution(ZagImage <byte> myImage, ConvMatrix Mask)
        {
            int s      = Mask.Size / 2;
            var cImage = myImage.Copy();

            for (int y = s; y < myImage.Height - s; y++)
            {
                for (int x = s; x < myImage.Width - s; x++)
                {
                    int r = 0, g = 0, b = 0;
                    for (int masky = 0; masky < Mask.Size; masky++)
                    {
                        for (int maskx = 0; maskx < Mask.Size; maskx++)
                        {
                            var maskValue = Mask.Matrix[maskx + masky * Mask.Size];

                            b += maskValue * myImage.Data[myImage.Depth * (x + maskx - s) + (y + masky - s) * myImage.Stride];
                            if (myImage.Depth == 3)
                            {
                                r += maskValue * myImage.Data[myImage.Depth * (x + maskx - s) + 2 + (y + masky - s) * myImage.Stride];
                                g += maskValue * myImage.Data[myImage.Depth * (x + maskx - s) + 1 + (y + masky - s) * myImage.Stride];
                            }
                        }
                    }
                    b = Math.Min(Math.Max((b / Mask.Factor) + Mask.Offset, 0), 255);
                    cImage.Data[myImage.Depth * x + y * myImage.Stride] = (byte)b;
                    if (myImage.Depth == 3)
                    {
                        r = Math.Min(Math.Max((r / Mask.Factor) + Mask.Offset, 0), 255);
                        g = Math.Min(Math.Max((g / Mask.Factor) + Mask.Offset, 0), 255);
                        cImage.Data[myImage.Depth * x + 1 + y * myImage.Stride] = (byte)g;
                        cImage.Data[myImage.Depth * x + 2 + y * myImage.Stride] = (byte)r;
                    }
                }
            }
            return(cImage);
        }
示例#25
0
        protected static void Conv3X3(Bitmap b, ConvMatrix m)
        {
            if (0 == m.Factor)
            {
                return;
            }

            using (var bSrc = (Bitmap)b.Clone())
            {
                BitmapData bmData = null;
                try
                {
                    BitmapData bmSrc = null;
                    try
                    {
                        bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, b.PixelFormat);
                        bmSrc  = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, b.PixelFormat);
                        Blur(bmSrc, bmData, m, b.PixelFormat);
                    }
                    finally
                    {
                        if (bmSrc != null)
                        {
                            bSrc.UnlockBits(bmSrc);
                        }
                    }
                }
                finally
                {
                    if (bmData != null)
                    {
                        b.UnlockBits(bmData);
                    }
                }
            }
        }
示例#26
0
		public static bool MeanRemoval(Bitmap b, int nWeight /* default to 9*/ )
		{
			ConvMatrix m = new ConvMatrix();
			m.SetAll(-1);
			m.Pixel = nWeight;
			m.Factor = nWeight - 8;

			return Conv3x3(b, m);
		}
示例#27
0
		public static bool Sharpen(Bitmap b, int nWeight /* default to 11*/ )
		{
			ConvMatrix m = new ConvMatrix();
			m.SetAll(0);
			m.Pixel = nWeight;
			m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = -2;
			m.Factor = nWeight - 8;

			return  Conv3x3(b, m);
		}
示例#28
0
        private static unsafe void Blur(BitmapData bmSrc, BitmapData bmData, ConvMatrix m, PixelFormat pixelFormat)
        {
            int bits;

            switch (pixelFormat)
            {
            case PixelFormat.Format32bppArgb:
                bits = 4;
                break;

            case PixelFormat.Format24bppRgb:
                bits = 3;
                break;

            default:
                throw new NotImplementedException();
            }

            var stride  = bmData.Stride;
            var stride2 = stride * 2;

            var scan0    = bmData.Scan0;
            var srcScan0 = bmSrc.Scan0;

            var p       = (byte *)(void *)scan0;
            var pSrc    = (byte *)(void *)srcScan0;
            var nOffset = stride - bmData.Width * bits;

            for (var y = 0; y < bmData.Height - 2; ++y)
            {
                for (var x = 0; x < bmData.Width - 2; ++x)
                {
                    for (var bit = 0; bit < bits; bit++)
                    {
                        var color = (((
                                          (pSrc[bit + 0 * bits] * m.TopLeft) +
                                          (pSrc[bit + 1 * bits] * m.TopMid) +
                                          (pSrc[bit + 2 * bits] * m.TopRight) +
                                          (pSrc[bit + 0 * bits + stride] * m.MidLeft) +
                                          (pSrc[bit + 1 * bits + stride] * m.Pixel) +
                                          (pSrc[bit + 2 * bits + stride] * m.MidRight) +
                                          (pSrc[bit + 0 * bits + stride2] * m.BottomLeft) +
                                          (pSrc[bit + 1 * bits + stride2] * m.BottomMid) +
                                          (pSrc[bit + 2 * bits + stride2] * m.BottomRight))
                                      / m.Factor) + m.Offset);

                        if (color < 0)
                        {
                            color = 0;
                        }
                        if (color > 255)
                        {
                            color = 255;
                        }
                        p[bits + bit + stride] = (byte)color;
                    }

                    p    += bits;
                    pSrc += bits;
                }

                p    += nOffset;
                pSrc += nOffset;
            }
        }
        private static bool Conv3x3(Bitmap b, ConvMatrix m) {
            // Avoid divide by zero errors
            if (0 == m.Factor) return false;

            Bitmap bSrc = (Bitmap)b.Clone();

            // GDI+ still lies to us - the return format is BGR, NOT RGB.
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            BitmapData bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride = bmData.Stride;
            int stride2 = stride * 2;
            System.IntPtr Scan0 = bmData.Scan0;
            System.IntPtr SrcScan0 = bmSrc.Scan0;

            unsafe {
                byte* p = (byte*)(void*)Scan0;
                byte* pSrc = (byte*)(void*)SrcScan0;

                int nOffset = stride + 6 - b.Width * 3;
                int nWidth = b.Width - 2;
                int nHeight = b.Height - 2;

                int nPixel;

                for (int y = 0; y < nHeight; ++y) {
                    for (int x = 0; x < nWidth; ++x) {
                        nPixel = ((((pSrc[2] * m.TopLeft) + (pSrc[5] * m.TopMid) + (pSrc[8] * m.TopRight) +
                            (pSrc[2 + stride] * m.MidLeft) + (pSrc[5 + stride] * m.Pixel) + (pSrc[8 + stride] * m.MidRight) +
                            (pSrc[2 + stride2] * m.BottomLeft) + (pSrc[5 + stride2] * m.BottomMid) + (pSrc[8 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

                        if (nPixel < 0) nPixel = 0;
                        if (nPixel > 255) nPixel = 255;

                        p[5 + stride] = (byte)nPixel;

                        nPixel = ((((pSrc[1] * m.TopLeft) + (pSrc[4] * m.TopMid) + (pSrc[7] * m.TopRight) +
                            (pSrc[1 + stride] * m.MidLeft) + (pSrc[4 + stride] * m.Pixel) + (pSrc[7 + stride] * m.MidRight) +
                            (pSrc[1 + stride2] * m.BottomLeft) + (pSrc[4 + stride2] * m.BottomMid) + (pSrc[7 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

                        if (nPixel < 0) nPixel = 0;
                        if (nPixel > 255) nPixel = 255;

                        p[4 + stride] = (byte)nPixel;

                        nPixel = ((((pSrc[0] * m.TopLeft) + (pSrc[3] * m.TopMid) + (pSrc[6] * m.TopRight) +
                            (pSrc[0 + stride] * m.MidLeft) + (pSrc[3 + stride] * m.Pixel) + (pSrc[6 + stride] * m.MidRight) +
                            (pSrc[0 + stride2] * m.BottomLeft) + (pSrc[3 + stride2] * m.BottomMid) + (pSrc[6 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

                        if (nPixel < 0) nPixel = 0;
                        if (nPixel > 255) nPixel = 255;

                        p[3 + stride] = (byte)nPixel;

                        p += 3;
                        pSrc += 3;
                    }

                    p += nOffset;
                    pSrc += nOffset;
                }
            }

            b.UnlockBits(bmData);
            bSrc.UnlockBits(bmSrc);

            return true;
        }
        /// see: http://www.codeproject.com/KB/GDI-plus/edge_detection.aspx
        /// <summary>
        /// Sobel Edge Detection
        /// 
        /// [  1   2   1 ]
        /// [  0   0   0 ]
        /// [ -1  -2  -1 ] /1+0
        /// 
        /// Edge detection filters work essentially by looking for contrast in an image.   
        /// 
        /// </summary>
        /// <param name="b">The source bitmap</param>
        public static bool Sobel(Bitmap b)
        {
            ConvMatrix m = new ConvMatrix();

            m.SetAll(0);
            m.TopLeft = m.TopRight = 1;
            m.BottomLeft = m.BottomRight = -1;
            m.TopMid = 2;
            m.BottomMid = -2;
            m.Offset = 0;

            return Conv3x3(b, m);
        }
示例#31
0
		public static bool EmbossLaplacian(Bitmap b)
		{
			ConvMatrix m = new ConvMatrix();
			m.SetAll(-1);
			m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 0;
			m.Pixel = 4;
			m.Offset = 127;

			return  Conv3x3(b, m);
		}	
示例#32
0
        public static void GaussianBlur(Bitmap img, int nWeight)
        {
            Bitmap b = img;
            ConvMatrix m = new ConvMatrix();
            m.SetAll(1);
            m.Pixel = nWeight;
            m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 2;
            m.Factor = nWeight + 12;

            if (Conv3x3(b, m))
            {
                //if (Rounded)                
                //    RoundImage(b, 30);
            }
        }
示例#33
0
        public Bitmap MainFilter(Bitmap b, int nWeight)
        {
            ConvMatrix m = new ConvMatrix();
            frmOptions opcije = new frmOptions();
   
            m.TopLeft = frmOptions.values.topL; 
            m.TopMid = frmOptions.values.topM; 
            m.TopRight = frmOptions.values.topR;

            m.MidLeft = frmOptions.values.midL; 
            m.Pixel = frmOptions.values.midM; 
            m.MidRight = frmOptions.values.midR;

            m.BottomLeft = frmOptions.values.botL;
            m.BottomMid = frmOptions.values.botM;
            m.BottomRight = frmOptions.values.botR;


            m.Factor = 1;
            m.Offset = 0;

            return Conv3x3(b, m);
        }
示例#34
0
        public static Bitmap Sharpen(Bitmap img, int nWeight /* default to 11*/ )
        {
            Bitmap b = (Bitmap)img.Clone();
            ConvMatrix m = new ConvMatrix();
            m.SetAll(0);
            m.Pixel = nWeight;
            m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = -2;
            m.Factor = nWeight - 8;

            if (Conv3x3(b, m))
            {

                if (Rounded)
                    RoundImage(b, 30);
                return b;
            }

            else return null;
        }
示例#35
0
        void applyConvMatrix(ConvMatrix m, ref byte[] map, int mWidth, int mHeight)
        {
            int[] neightbors = new int[] { -1, 1,         //top left
                                           0, 1,          //top center
                                           1, 1,          //top right

                                           -1, 0,         //mid left
                                           0, 0,          //mid center
                                           1, 0,          //mid right

                                           -1, -1,        //bot left
                                           0, -1,         //bot center
                                           1, -1,         //bot right
            };

            int imgWidth = mWidth;

            float[] tempImgArray = new float[mWidth * mHeight];

            for (int x = 0; x < imgWidth; x++)
            {
                for (int y = 0; y < imgWidth; y++)
                {
                    float total = 0;
                    for (int k = 0; k < 9; k++)
                    {
                        int xIndex = x + neightbors[k * 2];
                        int zIndex = y + neightbors[k * 2 + 1];
                        if (xIndex < 0 || xIndex > imgWidth - 1 || zIndex < 0 || zIndex > imgWidth - 1)
                        {
                            continue;
                        }



                        int   index = xIndex * imgWidth + zIndex;
                        float amt   = map[index] / 255.0f;
                        total += m.mFilterCoeffs[k] * amt;
                    }
                    total = total / m.mFactor + m.mOffset;

                    if (total > 1.0f)
                    {
                        total = 1.0f;
                    }
                    if (total < 0)
                    {
                        total = 0;
                    }

                    tempImgArray[x * imgWidth + y] = total;
                }
            }

            //send our mask back
            for (int x = 0; x < mWidth; x++)
            {
                for (int y = 0; y < mHeight; y++)
                {
                    int indx = x * imgWidth + y;
                    if (tempImgArray[indx] != 0)
                    {
                        map[indx] = (byte)(randomMapGen.Clamp(tempImgArray[indx], 0, 1) * 255);
                    }
                }
            }


            tempImgArray = null;
        }
示例#36
0
        public ZagImage <byte> SmoothGaussian(ZagImage <byte> img)
        {
            var gaussMatrix = new ConvMatrix(new[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 }, 3);

            return(Convolution(img, gaussMatrix));
        }
示例#37
0
		public static bool EdgeDetectQuick(Bitmap b)
		{
			ConvMatrix m = new ConvMatrix();
			m.TopLeft = m.TopMid = m.TopRight = -1;
			m.MidLeft = m.Pixel = m.MidRight = 0;
			m.BottomLeft = m.BottomMid = m.BottomRight = 1;
		
			m.Offset = 127;

			return  Conv3x3(b, m);
		}
示例#38
0
        static private void applyConvMatrix(ConvMatrix m)
        {
            int[] neightbors = new int[] { -1, 1,         //top left
                                           0, 1,          //top center
                                           1, 1,          //top right

                                           -1, 0,         //mid left
                                           0, 0,          //mid center
                                           1, 0,          //mid right

                                           -1, -1,        //bot left
                                           0, -1,         //bot center
                                           1, -1,         //bot right
            };

            int imgWidth = TerrainGlobals.getTerrain().getNumXVerts();

            float[] tempImgArray = new float[imgWidth * imgWidth];

            for (int x = 0; x < imgWidth; x++)
            {
                for (int y = 0; y < imgWidth; y++)
                {
                    float total = 0;
                    for (int k = 0; k < 9; k++)
                    {
                        int xIndex = x + neightbors[k * 2];
                        int zIndex = y + neightbors[k * 2 + 1];
                        if (xIndex < 0 || xIndex > imgWidth - 1 || zIndex < 0 || zIndex > imgWidth - 1)
                        {
                            continue;
                        }



                        int   index = xIndex * imgWidth + zIndex;
                        float amt   = 0;
                        isPointSelected(index, ref amt);
                        total += m.mFilterCoeffs[k] * amt;
                    }
                    total = total / m.mFactor + m.mOffset;

                    if (total > 1.0f)
                    {
                        total = 1.0f;
                    }
                    if (total < 0)
                    {
                        total = 0;
                    }

                    tempImgArray[x * imgWidth + y] = total;
                }
            }

            //send our mask back
            mCurrSelectionMask.Clear();
            for (int x = 0; x < imgWidth; x++)
            {
                for (int y = 0; y < imgWidth; y++)
                {
                    int indx = x * imgWidth + y;
                    if (tempImgArray[indx] != 0)
                    {
                        addSelectedVert(x, y, tempImgArray[indx]);
                    }
                }
            }


            tempImgArray = null;
        }
示例#39
0
        override public bool computeOutput(ConnectionPoint connPoint, OutputGenerationParams parms)
        {
            if (!verifyInputConnections())
            {
                return(false);
            }

            if (!gatherInputAndParameters(parms))
            {
                return(false);
            }

            MaskParam mp = ((MaskParam)(connPoint.ParamType));

            mp.Value = InputMask.Clone();
            mp.Value.mConstraintMask = ConstraintMask;


            ConvMatrix filter = new ConvMatrix(1, 2, 1, 2, 4, 2, 1, 2, 1);

            filter.mFactor = 16;
            filter.mOffset = 0;

            for (int i = 0; i < SmoothPower; i++)
            {
                int[] neightbors = new int[] { -1, 1,     //top left
                                               0, 1,      //top center
                                               1, 1,      //top right

                                               -1, 0,     //mid left
                                               0, 0,      //mid center
                                               1, 0,      //mid right

                                               -1, -1,    //bot left
                                               0, -1,     //bot center
                                               1, -1,     //bot right
                };


                DAGMask tempImgArray = new DAGMask(parms.Width, parms.Height);

                for (int x = 0; x < parms.Width; x++)
                {
                    for (int y = 0; y < parms.Height; y++)
                    {
                        float total = 0;
                        for (int k = 0; k < 9; k++)
                        {
                            int xIndex = x + neightbors[k * 2];
                            int zIndex = y + neightbors[k * 2 + 1];
                            if (xIndex < 0 || xIndex > parms.Width - 1 || zIndex < 0 || zIndex > parms.Height - 1)
                            {
                                continue;
                            }


                            total += filter.mFilterCoeffs[k] * mp.Value[xIndex, zIndex];
                        }
                        total = total / filter.mFactor + filter.mOffset;

                        if (total > 1.0f)
                        {
                            total = 1.0f;
                        }
                        if (total < 0)
                        {
                            total = 0;
                        }

                        tempImgArray[x, y] = total;
                    }
                }

                //send our mask back
                for (int x = 0; x < parms.Width; x++)
                {
                    for (int y = 0; y < parms.Height; y++)
                    {
                        mp.Value[x, y] = BMathLib.Clamp(tempImgArray[x, y], 0, 1);
                    }
                }


                tempImgArray = null;
            }


            return(true);
        }
示例#40
0
        private static bool Conv3x3(Bitmap b, ConvMatrix m)
        {
            // Avoid divide by zero errors
            if (0 == m.Factor)
            {
                return(false);
            }

            Bitmap bSrc = (Bitmap)b.Clone();

            // GDI+ still lies to us - the return format is BGR, NOT RGB.
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            BitmapData bmSrc  = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride  = bmData.Stride;
            int stride2 = stride * 2;

            System.IntPtr Scan0    = bmData.Scan0;
            System.IntPtr SrcScan0 = bmSrc.Scan0;

            unsafe
            {
                byte *p    = (byte *)(void *)Scan0;
                byte *pSrc = (byte *)(void *)SrcScan0;

                int nOffset = stride + 6 - b.Width * 3;
                int nWidth  = b.Width - 2;
                int nHeight = b.Height - 2;

                int nPixel;

                for (int y = 0; y < nHeight; ++y)
                {
                    for (int x = 0; x < nWidth; ++x)
                    {
                        nPixel = ((((pSrc[2] * m.TopLeft) + (pSrc[5] * m.TopMid) + (pSrc[8] * m.TopRight) +
                                    (pSrc[2 + stride] * m.MidLeft) + (pSrc[5 + stride] * m.Pixel) + (pSrc[8 + stride] * m.MidRight) +
                                    (pSrc[2 + stride2] * m.BottomLeft) + (pSrc[5 + stride2] * m.BottomMid) + (pSrc[8 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

                        if (nPixel < 0)
                        {
                            nPixel = 0;
                        }
                        if (nPixel > 255)
                        {
                            nPixel = 255;
                        }

                        p[5 + stride] = (byte)nPixel;

                        nPixel = ((((pSrc[1] * m.TopLeft) + (pSrc[4] * m.TopMid) + (pSrc[7] * m.TopRight) +
                                    (pSrc[1 + stride] * m.MidLeft) + (pSrc[4 + stride] * m.Pixel) + (pSrc[7 + stride] * m.MidRight) +
                                    (pSrc[1 + stride2] * m.BottomLeft) + (pSrc[4 + stride2] * m.BottomMid) + (pSrc[7 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

                        if (nPixel < 0)
                        {
                            nPixel = 0;
                        }
                        if (nPixel > 255)
                        {
                            nPixel = 255;
                        }

                        p[4 + stride] = (byte)nPixel;

                        nPixel = ((((pSrc[0] * m.TopLeft) + (pSrc[3] * m.TopMid) + (pSrc[6] * m.TopRight) +
                                    (pSrc[0 + stride] * m.MidLeft) + (pSrc[3 + stride] * m.Pixel) + (pSrc[6 + stride] * m.MidRight) +
                                    (pSrc[0 + stride2] * m.BottomLeft) + (pSrc[3 + stride2] * m.BottomMid) + (pSrc[6 + stride2] * m.BottomRight)) / m.Factor) + m.Offset);

                        if (nPixel < 0)
                        {
                            nPixel = 0;
                        }
                        if (nPixel > 255)
                        {
                            nPixel = 255;
                        }

                        p[3 + stride] = (byte)nPixel;

                        p    += 3;
                        pSrc += 3;
                    }

                    p    += nOffset;
                    pSrc += nOffset;
                }
            }

            b.UnlockBits(bmData);
            bSrc.UnlockBits(bmSrc);
            bSrc   = null;
            bmData = null;
            bmSrc  = null;

            return(true);
        }
示例#41
0
		public static Bitmap Emboss(Bitmap b, int Offset)
		{
			ConvMatrix m = new ConvMatrix();
			m.SetAll(-1);
			m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 0;
			m.Pixel = 4;
			m.Offset = Offset;

			return  Conv3x3(b, m);
		}