Example #1
0
        public static Bitmap AddNoise2(Bitmap OriginalImage, int Amount)
        {
            Bitmap     NewBitmap    = new Bitmap(OriginalImage.Width, OriginalImage.Height);
            BitmapData NewData      = NewBitmap.LockImage();     //Image.LockImage(NewBitmap);
            BitmapData OldData      = OriginalImage.LockImage(); //Image.LockImage(OriginalImage);
            int        NewPixelSize = NewData.GetPixelSize();    //Image.GetPixelSize(NewData);
            int        OldPixelSize = OldData.GetPixelSize();    //Image.GetPixelSize(OldData);

            Utilities.Random.Random TempRandom = new Utilities.Random.Random();
            for (int x = 0; x < NewBitmap.Width; ++x)
            {
                for (int y = 0; y < NewBitmap.Height; ++y)
                {
                    Color CurrentPixel = OldData.GetPixel(x, y, OldPixelSize); //Image.GetPixel(OldData, x, y, OldPixelSize);
                    int   noise        = (int)AWGN() * 255;
                    int   R            = CurrentPixel.R + noise;               //(int)Statistic.GaussianRandom(0, 1);//(int)Statistic.NextGaussian(0f, 1f, -Amount, Amount + 1);//TempRandom.Next(-Amount, Amount + 1);
                    int   G            = CurrentPixel.G + noise;               //(int)Statistic.GaussianRandom(0, 1);//(int)Statistic.NextGaussian(0f, 1f, -Amount, Amount + 1);//TempRandom.Next(-Amount, Amount + 1);
                    int   B            = CurrentPixel.B + noise;               //(int)Statistic.GaussianRandom(0, 1);//(int)Statistic.NextGaussian(0f, 1f, -Amount, Amount + 1);//TempRandom.Next(-Amount, Amount + 1);
                    R = R > 255 ? 255 : R;
                    R = R < 0 ? 0 : R;
                    G = G > 255 ? 255 : G;
                    G = G < 0 ? 0 : G;
                    B = B > 255 ? 255 : B;
                    B = B < 0 ? 0 : B;
                    Color TempValue = Color.FromArgb(R, G, B);
                    NewData.SetPixel(x, y, TempValue, NewPixelSize);//Image.SetPixel(NewData, x, y, TempValue, NewPixelSize);
                }
            }
            NewBitmap.UnlockImage(NewData);     //Image.UnlockImage(NewBitmap, NewData);
            OriginalImage.UnlockImage(OldData); //Image.UnlockImage(OriginalImage, OldData);
            return(NewBitmap);
        }
        /// <summary>
        /// Copies the rectangle to point.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="sourceRectangle">The source rectangle.</param>
        /// <param name="destination">The destination.</param>
        /// <param name="destinationPoint">The destination point.</param>
        /// <param name="flip">if set to <c>true</c> [flip].</param>
        public static void CopyRectangleToPoint(this Bitmap source, Rectangle sourceRectangle, Bitmap destination, Point destinationPoint, Palette palette, bool reverseX, bool reverseY)
        {
            BitmapData bmdSource = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);
            BitmapData bmdDest   = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, destination.PixelFormat);

            int        width  = sourceRectangle.Width;
            int        height = sourceRectangle.Height;
            int        x      = destinationPoint.X;
            int        y      = destinationPoint.Y;
            CalcOffset calcX  = reverseX ?
                                (CalcOffset)(col => (width - col - 1)) :
                                (CalcOffset)(col => col);
            CalcOffset calcY = reverseY ?
                               (CalcOffset)(row => (height - row - 1)) :
                               (CalcOffset)(row => row);

            for (int col = 0; col < sourceRectangle.Width; col++)
            {
                for (int row = 0; row < sourceRectangle.Height; row++)
                {
                    int index = bmdSource.GetPixel(col + sourceRectangle.X, row + sourceRectangle.Y);
                    if (palette.Colors[index % 16].A != 0)
                    {
                        bmdDest.SetPixel8bpp(
                            x + calcX(col),
                            y + calcY(row),
                            index);
                    }
                }
            }

            source.UnlockBits(bmdSource);
            destination.UnlockBits(bmdDest);
        }
Example #3
0
        /// <summary>
        /// Convert this Texture into a block of raw, uncompressed pixel data.
        /// </summary>
        /// <param name="format">The pixel format of the returned data.</param>
        /// <param name="flip">Whether to flip the image vertically, since RGB is often "upside down" in memory.</param>
        /// <returns>The uncompressed pixel data for this Texture as an array of bytes.</returns>
        public byte[] ToUncompressed(PixelFormat format = PixelFormat.Format24bppRgb, bool flip = false)
        {
            int components = 3;

            if (format == PixelFormat.Format32bppRgb || format == PixelFormat.Format32bppRgba)
            {
                components++;
            }

            byte[] bytes = new byte[Bitmap.Width * Bitmap.Height * components];

            int pitch = Bitmap.Width * components;

            // An unsafe block is necessary for speed; Eto's Bitmap GetPixel is
            // too slow for grabbing every pixel of an image, but the BitmapData
            // version of GetPixel flies. Direct access to the data buffer by
            // pointer is also possible, but exposes each platform's underlying
            // pixel formats, e.g. RGB, BGR, RGBA, ARGB, and would then demand
            // per-platform branching to reorder the components properly.
            unsafe
            {
                using (BitmapData raw = Bitmap.Lock())
                {
                    for (int y = 0; y < Bitmap.Height; y++)
                    {
                        int line = pitch;
                        if (flip)
                        {
                            line *= y;
                        }
                        else
                        {
                            line *= Bitmap.Height - 1 - y;
                        }

                        for (int x = 0; x < Bitmap.Width; x++)
                        {
                            Color pixel = raw.GetPixel(x, y);

                            int offset = x * components;

                            bytes[line + offset + 0] = Convert.ToByte(pixel.Rb);
                            bytes[line + offset + 1] = Convert.ToByte(pixel.Gb);
                            bytes[line + offset + 2] = Convert.ToByte(pixel.Bb);

                            if (format == PixelFormat.Format32bppRgba)
                            {
                                bytes[line + offset + 3] = Convert.ToByte(pixel.Ab);
                            }
                            else if (format == PixelFormat.Format32bppRgb)
                            {
                                bytes[line + offset + 3] = 0;
                            }
                        }
                    }

                    return(bytes);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Loads an image
        /// </summary>
        /// <param name="ImageUsing">Image to load</param>
        public virtual void LoadImage(Bitmap ImageUsing)
        {
            if (ImageUsing == null)
            {
                throw new ArgumentNullException("ImageUsing");
            }
            BitmapData OldData   = ImageUsing.LockImage();
            int        PixelSize = OldData.GetPixelSize();

            Width  = ImageUsing.Width;
            Height = ImageUsing.Height;
            R.Clear();
            G.Clear();
            B.Clear();
            for (int x = 0; x < ImageUsing.Width; ++x)
            {
                for (int y = 0; y < ImageUsing.Height; ++y)
                {
                    Color TempColor = OldData.GetPixel(x, y, PixelSize);
                    ++R[(int)TempColor.R];
                    ++G[(int)TempColor.G];
                    ++B[(int)TempColor.B];
                }
            }
            ImageUsing.UnlockImage(OldData);
        }
Example #5
0
        public static Bitmap AdjustGamma(Bitmap OriginalImage, float Value)
        {
            Bitmap     NewBitmap    = new Bitmap(OriginalImage.Width, OriginalImage.Height);
            BitmapData NewData      = NewBitmap.LockImage();     //Image.LockImage(NewBitmap);
            BitmapData OldData      = OriginalImage.LockImage(); //Image.LockImage(OriginalImage);
            int        NewPixelSize = NewData.GetPixelSize();    //Image.GetPixelSize(NewData);
            int        OldPixelSize = OldData.GetPixelSize();    //Image.GetPixelSize(OldData);

            int[] RedRamp   = new int[256];
            int[] GreenRamp = new int[256];
            int[] BlueRamp  = new int[256];
            for (int x = 0; x < 256; ++x)
            {
                RedRamp[x]   = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
                GreenRamp[x] = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
                BlueRamp[x]  = Clamp((int)((255.0 * System.Math.Pow(x / 255.0, 1.0 / Value)) + 0.5), 255, 0);
            }

            for (int x = 0; x < NewBitmap.Width; ++x)
            {
                for (int y = 0; y < NewBitmap.Height; ++y)
                {
                    Color Pixel = OldData.GetPixel(x, y, OldPixelSize);//Image.GetPixel(OldData, x, y, OldPixelSize);
                    int   Red   = RedRamp[Pixel.R];
                    int   Green = GreenRamp[Pixel.G];
                    int   Blue  = BlueRamp[Pixel.B];
                    NewData.SetPixel(x, y, Color.FromArgb(Red, Green, Blue), NewPixelSize);//Image.SetPixel(NewData, x, y, Color.FromArgb(Red, Green, Blue), NewPixelSize);
                }
            }

            NewBitmap.UnlockImage(NewData);     //Image.UnlockImage(NewBitmap, NewData);
            OriginalImage.UnlockImage(OldData); //Image.UnlockImage(OriginalImage, OldData);
            return(NewBitmap);
        }
Example #6
0
        public Color DetectBackground()
        {
            if (BackgroundColor == null)
            {
                var top    = Enumerable.Range(0, image.Width).Select(x => image.GetPixel(x, 0)).ToList();
                var bottom = Enumerable.Range(0, image.Width).Select(x => image.GetPixel(x, image.Height - 1)).ToList();
                var left   = Enumerable.Range(0, image.Height).Select(y => image.GetPixel(0, y)).ToList();
                var right  = Enumerable.Range(0, image.Height).Select(y => image.GetPixel(image.Width - 1, y)).ToList();

                BackgroundColor = (from c in top.Concat(bottom).Concat(left).Concat(right)
                                   group c by c into g
                                   orderby g.Count() descending
                                   select g.Key).First();
            }

            return(BackgroundColor.Value);
        }
Example #7
0
        /// <summary>
        /// Copies the rectangle to point.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="sourceRectangle">The source rectangle.</param>
        /// <param name="destination">The destination.</param>
        /// <param name="destinationPoint">The destination point.</param>
        /// <param name="flip">if set to <c>true</c> [flip].</param>
        public static void CopyRectangleToPoint(this Bitmap source, Rectangle sourceRectangle, Bitmap destination, Point destinationPoint, Palette palette, bool flip)
        {
            BitmapData bmdSource = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);
            BitmapData bmdDest   = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, destination.PixelFormat);

            if (flip)
            {
                for (int col = 0; col < sourceRectangle.Width; col++)
                {
                    for (int row = 0; row < sourceRectangle.Height; row++)
                    {
                        int index = bmdSource.GetPixel(col + sourceRectangle.X, row + sourceRectangle.Y);
                        if (palette.Colors[index % 16].A != 0)
                        {
                            bmdDest.SetPixel8bpp(
                                destinationPoint.X + (sourceRectangle.Width - col - 1), destinationPoint.Y + row,
                                index);
                        }
                    }
                }
            }
            else
            {
                for (int col = 0; col < sourceRectangle.Width; col++)
                {
                    for (int row = 0; row < sourceRectangle.Height; row++)
                    {
                        int index = bmdSource.GetPixel(col + sourceRectangle.X, row + sourceRectangle.Y);
                        if (palette.Colors[index % 16].A != 0)
                        {
                            bmdDest.SetPixel8bpp(
                                destinationPoint.X + col, destinationPoint.Y + row,
                                index);
                        }
                    }
                }
            }

            source.UnlockBits(bmdSource);
            destination.UnlockBits(bmdDest);
        }
        private void SetupImage()
        {
            BitmapData ImageData      = _Image.LockImage();
            int        ImagePixelSize = ImageData.GetPixelSize();
            int        Width          = _Image.Width;
            int        Height         = _Image.Height;

            Parallel.For(0, _NumberOfPoints, i =>
            {
                int Red     = 0;
                int Green   = 0;
                int Blue    = 0;
                int Counter = 0;
                for (int x = 0; x < Width; ++x)
                {
                    for (int y = 0; y < Height; ++y)
                    {
                        if (Map.ClosestPoint[x, y] == i)
                        {
                            Color Pixel = ImageData.GetPixel(x, y, ImagePixelSize);
                            Red        += Pixel.R;
                            Green      += Pixel.G;
                            Blue       += Pixel.B;
                            ++Counter;
                        }
                    }
                }
                int Counter2 = 0;
                for (int x = 0; x < Width; ++x)
                {
                    for (int y = 0; y < Height; ++y)
                    {
                        if (Map.ClosestPoint[x, y] == i)
                        {
                            ImageData.SetPixel(x, y, Color.FromArgb(Red / Counter, Green / Counter, Blue / Counter), ImagePixelSize);
                            ++Counter2;
                            if (Counter2 == Counter)
                            {
                                break;
                            }
                        }
                    }
                    if (Counter2 == Counter)
                    {
                        break;
                    }
                }
            });
            _Image.UnlockImage(ImageData);
        }
Example #9
0
        /// <summary>
        /// Imports a bitmap and tries to convert it to a FFT sprite.
        /// </summary>
        public virtual void ImportBitmap(Bitmap bmp, out bool foundBadPixels)
        {
            foundBadPixels = false;

            if (bmp.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                throw new BadImageFormatException("Image is not an 8bpp paletted bitmap!");
            }
            if (bmp.Width != 256)
            {
                throw new BadImageFormatException("Image is not 256 pixels wide!");
            }

            Palettes = new Palette[16];

            for (int i = 0; i < 16; i++)
            {
                Palettes[i] = Palette.EmptyPalette;
            }

            for (int i = 0; i < bmp.Palette.Entries.Length; i++)
            {
                Color c = bmp.Palette.Entries[i];
                Palettes[i / 16][i % 16] = Color.FromArgb(c.R & 0xF8, c.G & 0xF8, c.B & 0xF8);
                if (i % 16 == 0 && c.ToArgb() == Color.Black.ToArgb())
                {
                    Palettes[i / 16][i % 16] = Color.Transparent;
                }
            }

            Pixels.InitializeElements();

            BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

            for (int i = 0; (i < Pixels.Count) && (i / 256 < bmp.Height); i++)
            {
                Pixels[i] = (byte)bmd.GetPixel(i % 256, i / 256);
                if (Pixels[i] >= 16)
                {
                    foundBadPixels = true;
                }
            }

            bmp.UnlockBits(bmd);

            BitmapDirty = true;

            FirePixelsChanged();
        }
Example #10
0
 /// <summary>
 /// Creates the bump map
 /// </summary>
 public virtual Bitmap Create(Bitmap ImageUsing)
 {
     if (ImageUsing == null)
     {
         throw new ArgumentNullException("ImageUsing");
     }
     CreateFilter();
     using (Bitmap TempImageX = FilterX.Create(ImageUsing))
     {
         using (Bitmap TempImageY = FilterY.Create(ImageUsing))
         {
             Bitmap       ReturnImage          = new Bitmap(TempImageX.Width, TempImageX.Height);
             Math.Vector3 TempVector           = new Utilities.Math.Vector3(0.0, 0.0, 0.0);
             BitmapData   TempImageXData       = TempImageX.LockImage();
             BitmapData   TempImageYData       = TempImageY.LockImage();
             BitmapData   ReturnImageData      = ReturnImage.LockImage();
             int          TempImageXPixelSize  = TempImageXData.GetPixelSize();
             int          TempImageYPixelSize  = TempImageYData.GetPixelSize();
             int          ReturnImagePixelSize = ReturnImageData.GetPixelSize();
             for (int y = 0; y < TempImageX.Height; ++y)
             {
                 for (int x = 0; x < TempImageX.Width; ++x)
                 {
                     Color TempPixelX = TempImageXData.GetPixel(x, y, TempImageXPixelSize);
                     Color TempPixelY = TempImageYData.GetPixel(x, y, TempImageYPixelSize);
                     TempVector.X = (double)(TempPixelX.R) / 255.0;
                     TempVector.Y = (double)(TempPixelY.R) / 255.0;
                     TempVector.Z = 1.0;
                     TempVector.Normalize();
                     TempVector.X = ((TempVector.X + 1.0) / 2.0) * 255.0;
                     TempVector.Y = ((TempVector.Y + 1.0) / 2.0) * 255.0;
                     TempVector.Z = ((TempVector.Z + 1.0) / 2.0) * 255.0;
                     ReturnImageData.SetPixel(x, y,
                                              Color.FromArgb((int)TempVector.X,
                                                             (int)TempVector.Y,
                                                             (int)TempVector.Z),
                                              ReturnImagePixelSize);
                 }
             }
             TempImageX.UnlockImage(TempImageXData);
             TempImageY.UnlockImage(TempImageYData);
             ReturnImage.UnlockImage(ReturnImageData);
             return(ReturnImage);
         }
     }
 }
Example #11
0
 /// <summary>
 /// Creates the bump map
 /// </summary>
 public virtual Bitmap Create(Bitmap ImageUsing)
 {
     ImageUsing.ThrowIfNull("ImageUsing");
     CreateFilter();
     using (Bitmap TempImageX = FilterX.Create(ImageUsing))
     {
         using (Bitmap TempImageY = FilterY.Create(ImageUsing))
         {
             Bitmap     ReturnImage          = new Bitmap(TempImageX.Width, TempImageX.Height);
             BitmapData TempImageXData       = TempImageX.LockImage();
             BitmapData TempImageYData       = TempImageY.LockImage();
             BitmapData ReturnImageData      = ReturnImage.LockImage();
             int        TempImageXPixelSize  = TempImageXData.GetPixelSize();
             int        TempImageYPixelSize  = TempImageYData.GetPixelSize();
             int        ReturnImagePixelSize = ReturnImageData.GetPixelSize();
             int        Width  = TempImageX.Width;
             int        Height = TempImageX.Height;
             Parallel.For(0, Height, y =>
             {
                 Math.Vector3 TempVector = new Utilities.Math.Vector3(0.0, 0.0, 0.0);
                 for (int x = 0; x < Width; ++x)
                 {
                     Color TempPixelX = TempImageXData.GetPixel(x, y, TempImageXPixelSize);
                     Color TempPixelY = TempImageYData.GetPixel(x, y, TempImageYPixelSize);
                     TempVector.X     = (double)(TempPixelX.R) / 255.0;
                     TempVector.Y     = (double)(TempPixelY.R) / 255.0;
                     TempVector.Z     = 1.0;
                     TempVector.Normalize();
                     TempVector.X = ((TempVector.X + 1.0) / 2.0) * 255.0;
                     TempVector.Y = ((TempVector.Y + 1.0) / 2.0) * 255.0;
                     TempVector.Z = ((TempVector.Z + 1.0) / 2.0) * 255.0;
                     ReturnImageData.SetPixel(x, y,
                                              Color.FromArgb((int)TempVector.X,
                                                             (int)TempVector.Y,
                                                             (int)TempVector.Z),
                                              ReturnImagePixelSize);
                 }
             });
             TempImageX.UnlockImage(TempImageXData);
             TempImageY.UnlockImage(TempImageYData);
             ReturnImage.UnlockImage(ReturnImageData);
             return(ReturnImage);
         }
     }
 }
Example #12
0
        /// <summary>
        /// Imports a bitmap and tries to convert it to a FFT sprite.
        /// </summary>
        public virtual void ImportBitmap(Bitmap bmp, out bool foundBadPixels)
        {
            foundBadPixels = false;

            if (bmp.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                throw new BadImageFormatException();
            }
            if (bmp.Width != 256)
            {
                throw new BadImageFormatException();
            }

            Palettes = new Palette[16];
            for (int i = 0; i < 16; i++)
            {
                Palettes[i] = new Palette(bmp.Palette.Entries.Sub(16 * i, 16 * (i + 1) - 1));
            }

            Pixels.InitializeElements();

            BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);

            for (int i = 0; (i < Pixels.Count) && (i / 256 < bmp.Height); i++)
            {
                Pixels[i] = (byte)bmd.GetPixel(i % 256, i / 256);
                if (Pixels[i] >= 16)
                {
                    foundBadPixels = true;
                }
            }

            bmp.UnlockBits(bmd);

            ThumbnailDirty = true;
            BitmapDirty    = true;

            FirePixelsChanged();
        }
Example #13
0
        /// <summary>
        /// Loads an image
        /// </summary>
        /// <param name="ImageUsing">Image to load</param>
        public virtual void LoadImage(Bitmap ImageUsing)
        {
            Contract.Requires <ArgumentNullException>(ImageUsing != null, "ImageUsing");
            BitmapData OldData   = ImageUsing.LockImage();
            int        PixelSize = OldData.GetPixelSize();

            Width  = ImageUsing.Width;
            Height = ImageUsing.Height;
            R.Clear();
            G.Clear();
            B.Clear();
            for (int x = 0; x < Width; ++x)
            {
                for (int y = 0; y < Height; ++y)
                {
                    Color TempColor = OldData.GetPixel(x, y, PixelSize);
                    ++R[(int)TempColor.R];
                    ++G[(int)TempColor.G];
                    ++B[(int)TempColor.B];
                }
            }
            ImageUsing.UnlockImage(OldData);
        }
        /// <summary>
        /// Loads an image
        /// </summary>
        /// <param name="ImageUsing">Image to load</param>
        public virtual void LoadImage(Bitmap ImageUsing)
        {
            ImageUsing.ThrowIfNull("ImageUsing");
            BitmapData OldData   = ImageUsing.LockImage();
            int        PixelSize = OldData.GetPixelSize();

            Width  = ImageUsing.Width;
            Height = ImageUsing.Height;
            R.Clear();
            G.Clear();
            B.Clear();
            for (int x = 0; x < Width; ++x)
            {
                for (int y = 0; y < Height; ++y)
                {
                    Color TempColor = OldData.GetPixel(x, y, PixelSize);
                    ++R[(int)TempColor.R];
                    ++G[(int)TempColor.G];
                    ++B[(int)TempColor.B];
                }
            }
            ImageUsing.UnlockImage(OldData);
        }
Example #15
0
        /// <summary>
        /// Converts an image to ASCII art
        /// </summary>
        /// <param name="Input">The image you wish to convert</param>
        /// <returns>A string containing the art</returns>
        public static string ConvertToASCII(Bitmap Input)
        {
            if (Input == null)
            {
                throw new ArgumentNullException("Input");
            }
            bool ShowLine = true;

            using (Bitmap TempImage = Input.BlackAndWhite())
            {
                BitmapData    OldData      = TempImage.LockImage();
                int           OldPixelSize = OldData.GetPixelSize();
                StringBuilder Builder      = new StringBuilder();
                for (int x = 0; x < TempImage.Height; ++x)
                {
                    for (int y = 0; y < TempImage.Width; ++y)
                    {
                        if (ShowLine)
                        {
                            Color CurrentPixel = OldData.GetPixel(y, x, OldPixelSize);
                            Builder.Append(_ASCIICharacters[((CurrentPixel.R * _ASCIICharacters.Length) / 255)]);
                        }
                    }
                    if (ShowLine)
                    {
                        Builder.Append(System.Environment.NewLine);
                        ShowLine = false;
                    }
                    else
                    {
                        ShowLine = true;
                    }
                }
                TempImage.UnlockImage(OldData);
                return(Builder.ToString());
            }
        }
Example #16
0
        public virtual Bitmap ApplyFilter(Bitmap Input)
        {
            Contract.Requires <ArgumentNullException>(Input != null, "Input");
            Bitmap     NewBitmap    = new Bitmap(Input.Width, Input.Height);
            BitmapData NewData      = NewBitmap.LockImage();
            BitmapData OldData      = Input.LockImage();
            int        NewPixelSize = NewData.GetPixelSize();
            int        OldPixelSize = OldData.GetPixelSize();
            int        Width2       = Input.Width;
            int        Height2      = Input.Height;

            Parallel.For(0, Width2, x =>
            {
                for (int y = 0; y < Height2; ++y)
                {
                    int RValue   = 0;
                    int GValue   = 0;
                    int BValue   = 0;
                    int Weight   = 0;
                    int XCurrent = -Width / 2;
                    for (int x2 = 0; x2 < Width; ++x2)
                    {
                        if (XCurrent + x < Width2 && XCurrent + x >= 0)
                        {
                            int YCurrent = -Height / 2;
                            for (int y2 = 0; y2 < Height; ++y2)
                            {
                                if (YCurrent + y < Height2 && YCurrent + y >= 0)
                                {
                                    Color Pixel = OldData.GetPixel(XCurrent + x, YCurrent + y, OldPixelSize);
                                    RValue     += MyFilter[x2, y2] * Pixel.R;
                                    GValue     += MyFilter[x2, y2] * Pixel.G;
                                    BValue     += MyFilter[x2, y2] * Pixel.B;
                                    Weight     += MyFilter[x2, y2];
                                }
                                ++YCurrent;
                            }
                        }
                        ++XCurrent;
                    }
                    Color MeanPixel = OldData.GetPixel(x, y, OldPixelSize);
                    if (Weight == 0)
                    {
                        Weight = 1;
                    }
                    if (Weight > 0)
                    {
                        if (Absolute)
                        {
                            RValue = System.Math.Abs(RValue);
                            GValue = System.Math.Abs(GValue);
                            BValue = System.Math.Abs(BValue);
                        }
                        RValue    = (RValue / Weight) + Offset;
                        RValue    = RValue.Clamp(255, 0);
                        GValue    = (GValue / Weight) + Offset;
                        GValue    = GValue.Clamp(255, 0);
                        BValue    = (BValue / Weight) + Offset;
                        BValue    = BValue.Clamp(255, 0);
                        MeanPixel = Color.FromArgb(RValue, GValue, BValue);
                    }
                    NewData.SetPixel(x, y, MeanPixel, NewPixelSize);
                }
            });
            NewBitmap.UnlockImage(NewData);
            Input.UnlockImage(OldData);
            return(NewBitmap);
        }
 /// <summary>
 /// Runs the motion detection algorithm
 /// </summary>
 /// <param name="NewImage">The "new" frame</param>
 /// <param name="OldImage">The "old" frame</param>
 /// <param name="Threshold">The threshold used to detect changes in the image</param>
 /// <param name="DetectionColor">Color to display changes in the images as</param>
 /// <returns>A bitmap indicating where changes between frames have occurred overlayed on top of the new image.</returns>
 public static Bitmap Process(Bitmap NewImage, Bitmap OldImage, int Threshold, Color DetectionColor)
 {
     NewImage.ThrowIfNull("NewImage");
     OldImage.ThrowIfNull("OldImage");
     DetectionColor.ThrowIfNull("DetectionColor");
     using (Bitmap NewImage1 = NewImage.BlackAndWhite())
     {
         using (Bitmap OldImage1 = OldImage.BlackAndWhite())
         {
             using (Bitmap NewImage2 = NewImage1.SNNBlur(5))
             {
                 using (Bitmap OldImage2 = OldImage1.SNNBlur(5))
                 {
                     using (Bitmap OutputImage = new Bitmap(NewImage2, NewImage2.Width, NewImage2.Height))
                     {
                         using (Bitmap Overlay = new Bitmap(NewImage, NewImage.Width, NewImage.Height))
                         {
                             BitmapData NewImage2Data      = NewImage2.LockImage();
                             int        NewImage2PixelSize = NewImage2Data.GetPixelSize();
                             BitmapData OldImage2Data      = OldImage2.LockImage();
                             int        OldImage2PixelSize = OldImage2Data.GetPixelSize();
                             BitmapData OverlayData        = Overlay.LockImage();
                             int        OverlayPixelSize   = OverlayData.GetPixelSize();
                             int        Width  = OutputImage.Width;
                             int        Height = OutputImage.Height;
                             Parallel.For(0, Width, x =>
                             {
                                 for (int y = 0; y < Height; ++y)
                                 {
                                     Color NewPixel = NewImage2Data.GetPixel(x, y, NewImage2PixelSize);
                                     Color OldPixel = OldImage2Data.GetPixel(x, y, OldImage2PixelSize);
                                     if (System.Math.Pow((double)(NewPixel.R - OldPixel.R), 2.0) > Threshold)
                                     {
                                         OverlayData.SetPixel(x, y, Color.FromArgb(100, 0, 100), OverlayPixelSize);
                                     }
                                     else
                                     {
                                         OverlayData.SetPixel(x, y, Color.FromArgb(200, 0, 200), OverlayPixelSize);
                                     }
                                 }
                             });
                             Overlay.UnlockImage(OverlayData);
                             NewImage2.UnlockImage(NewImage2Data);
                             OldImage2.UnlockImage(OldImage2Data);
                             using (Bitmap Overlay2 = Overlay.EdgeDetection(25, DetectionColor))
                             {
                                 BitmapData Overlay2Data      = Overlay2.LockImage();
                                 int        Overlay2PixelSize = Overlay2Data.GetPixelSize();
                                 Width  = OutputImage.Width;
                                 Height = OutputImage.Height;
                                 Parallel.For(0, Width, x =>
                                 {
                                     for (int y = 0; y < Height; ++y)
                                     {
                                         Color Pixel1 = Overlay2Data.GetPixel(x, y, Overlay2PixelSize);
                                         if (Pixel1.R != DetectionColor.R || Pixel1.G != DetectionColor.G || Pixel1.B != DetectionColor.B)
                                         {
                                             Overlay2Data.SetPixel(x, y, Color.FromArgb(200, 0, 200), Overlay2PixelSize);
                                         }
                                     }
                                 });
                                 Overlay2.UnlockImage(Overlay2Data);
                                 return(OutputImage.Watermark(Overlay2, 1.0f, 0, 0, Color.FromArgb(200, 0, 200)));
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Example #18
0
 public static Color GetPixel(this BitmapData bd, int x, int y)
 {
     return(bd.GetPixel((uint)x, (uint)y));
 }