Exemple #1
0
        public static Bitmap OffsetFilterAbs(Bitmap image, int xOffset, int yOffset)
        {
            LockBitmap lockImage = new LockBitmap(image);

            lockImage.LockBits();

            Bitmap     result     = new Bitmap(width: image.Width, height: image.Height);
            LockBitmap lockResult = new LockBitmap(result);

            lockResult.LockBits();

            for (int y = 0; y < lockImage.Height; y++)
            {
                for (int x = 0; y < lockImage.Width; x++)
                {
                    int newY = y + yOffset;
                    int newX = x + xOffset;

                    if (newY < lockImage.Height && newX < lockImage.Width)
                    {
                        Color cl = lockImage.GetPixel(x, y);
                        lockResult.SetPixel(newX, newY, cl);
                    }
                }
            }

            lockImage.UnlockBits();
            lockResult.UnlockBits();
            return(result);
        }
Exemple #2
0
        public static Bitmap GrayScale(Bitmap image)
        {
            LockBitmap lockImage = new LockBitmap(image);

            lockImage.LockBits();

            Bitmap     result     = (Bitmap)image.Clone();
            LockBitmap lockResult = new LockBitmap(result);

            lockResult.LockBits();

            int nWidth  = lockImage.Width;
            int nHeight = lockImage.Height;

            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    Color originalColor = lockImage.GetPixel(x, y);
                    int   red           = originalColor.R;
                    int   green         = originalColor.G;
                    int   blue          = originalColor.B;
                    int   avg           = (red + green + blue) / 3;
                    Color newColor      = Color.FromArgb(red: avg, blue: avg, green: avg);
                    lockResult.SetPixel(x, y, newColor);
                }
            }


            lockImage.UnlockBits();
            lockResult.UnlockBits();
            return(result);
        }
Exemple #3
0
 public Histogram(Bitmap sourceImage)
 {
     this.sourceImage  = sourceImage;
     lockedSourceImage = new LockBitmap(this.sourceImage);
     lockedSourceImage.LockBits();
     height = lockedSourceImage.Height;
     width  = lockedSourceImage.Width;
 }
Exemple #4
0
        public static Bitmap GrayWorld(Bitmap image)
        {
            LockBitmap lockImage = new LockBitmap(image);

            lockImage.LockBits();

            Bitmap     result     = (Bitmap)image.Clone();
            LockBitmap lockResult = new LockBitmap(result);

            lockResult.LockBits();

            int nWidth  = lockImage.Width;
            int nHeight = lockImage.Height;

            int redSum = 0, greenSum = 0, blueSum = 0;


            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    Color color = lockImage.GetPixel(x, y);
                    redSum   += color.R;
                    greenSum += color.G;
                    blueSum  += color.B;
                }
            }

            int size = nWidth * nHeight;

            double redGlobal   = 1f / size * redSum;
            double greenGlobal = 1f / size * greenSum;
            double blueGlobal  = 1f / size * blueSum;
            double avg         = (redGlobal + greenGlobal + blueGlobal) / 3;

            double redDiv   = avg / redGlobal;
            double greenDiv = avg / greenGlobal;
            double blueDiv  = avg / blueGlobal;

            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    Color color    = lockImage.GetPixel(x, y);
                    int   newRed   = (int)(color.R * redDiv);
                    int   newGreen = (int)(color.G * greenDiv);
                    int   newBlue  = (int)(color.B * blueDiv);
                    lockResult.SetPixel(x, y, Color.FromArgb((newRed < 255) ? newRed: 254, (newGreen < 255) ? newGreen : 254, (newBlue < 255) ? newBlue : 254));
                }
            }
            lockImage.UnlockBits();
            lockResult.UnlockBits();
            return(result);
        }
Exemple #5
0
        //public static bool Swirl(Bitmap b, double fDegree, bool bSmoothing /* default fDegree to .05 */)
        //{
        //    int nWidth = b.Width;
        //    int nHeight = b.Height;

        //    FloatPoint[,] fp = new FloatPoint[nWidth, nHeight];
        //    Point[,] pt = new Point[nWidth, nHeight];

        //    Point mid = new Point();
        //    mid.X = nWidth / 2;
        //    mid.Y = nHeight / 2;

        //    double theta, radius;
        //    double newX, newY;

        //    for (int x = 0; x < nWidth; ++x)
        //        for (int y = 0; y < nHeight; ++y)
        //        {
        //            int trueX = x - mid.X;
        //            int trueY = y - mid.Y;
        //            theta = Math.Atan2((trueY), (trueX));

        //            radius = Math.Sqrt(trueX * trueX + trueY * trueY);

        //            newX = mid.X + (radius * Math.Cos(theta + fDegree * radius));
        //            if (newX > 0 && newX < nWidth)
        //            {
        //                fp[x, y].X = newX;
        //                pt[x, y].X = (int)newX;
        //            }
        //            else
        //                fp[x, y].X = pt[x, y].X = x;

        //            newY = mid.Y + (radius * Math.Sin(theta + fDegree * radius));
        //            if (newY > 0 && newY < nHeight)
        //            {
        //                fp[x, y].Y = newY;
        //                pt[x, y].Y = (int)newY;
        //            }
        //            else
        //                fp[x, y].Y = pt[x, y].Y = y;
        //        }
        //}

        //if (bSmoothing)
        //    OffsetFilterAntiAlias(b, fp);
        //else
        //    OffsetFilterAbs(b, pt);


        public static Bitmap RandomJitter(Bitmap image, short nDegree)
        {
            LockBitmap lockImage = new LockBitmap(image);

            lockImage.LockBits();

            Bitmap     result     = (Bitmap)image.Clone();
            LockBitmap lockResult = new LockBitmap(result);

            lockResult.LockBits();

            int nWidth  = lockImage.Width;
            int nHeight = lockImage.Height;

            int newX, newY;

            short  nHalf = (short)Math.Floor(nDegree / 2.0);
            Random rnd   = new Random();

            for (int x = 0; x < nWidth; x++)
            {
                for (int y = 0; y < nHeight; y++)
                {
                    newX = rnd.Next(nDegree) - nHalf;

                    if (x + newX < 0 || x + newX >= nWidth)
                    {
                        newX = 0;
                    }
                    else
                    {
                        newX = newX + x;
                    }

                    newY = rnd.Next(nDegree) - nHalf;

                    if (y + newY < 0 || y + newY >= nHeight)
                    {
                        newY = 0;
                    }
                    else
                    {
                        newY = newY + y;
                    }

                    Color cl = lockImage.GetPixel(x, y);
                    lockResult.SetPixel(newX, newY, cl);
                }
            }

            lockImage.UnlockBits();
            lockResult.UnlockBits();
            return(result);
        }
Exemple #6
0
        static public Bitmap rotate(Bitmap image, int angle)
        {
            LockBitmap lockImage  = new LockBitmap(image);
            Bitmap     result     = new Bitmap(width: image.Width, height: image.Height);
            LockBitmap lockResult = new LockBitmap(result);

            lockImage.LockBits();
            lockResult.LockBits();

            // center of image
            int y0 = image.Height / 2;
            int x0 = image.Width / 2;

            double degree = Math.PI * angle / 180.0;

            for (int y = 0; y < image.Height; y++)
            {
                for (int x = 0; x < image.Width; x++)
                {
                    int newX = Convert.ToInt32(Math.Cos(degree) * (x - x0) - Math.Sin(degree) * (y - y0) + x0);
                    if (newX >= image.Width || newX < 0)
                    {
                        continue;
                    }
                    int newY = Convert.ToInt32(Math.Sin(degree) * (x - x0) + Math.Cos(degree) * (y - y0) + y0);
                    if (newY >= image.Height || newY < 0)
                    {
                        continue;
                    }
                    Color color = lockImage.GetPixel(x, y);
                    lockResult.SetPixel(newX, newY, color);
                }
            }
            lockImage.UnlockBits();
            lockResult.UnlockBits();
            return(result);
        }
Exemple #7
0
        static public Bitmap wind(Bitmap image, int strength, Direction direction)
        {
            int        pixelStrenght = strengthToPixels(strength, image, direction.orientation());
            Random     randShift     = new Random(pixelStrenght);
            Bitmap     result        = new Bitmap(width: image.Width, height: image.Height);
            LockBitmap lockResult    = new LockBitmap(result);
            LockBitmap lockBitmap    = new LockBitmap(image);

            lockBitmap.LockBits();
            lockResult.LockBits();

            switch (direction)
            {
            case Direction.Left:

                for (int y = lockBitmap.Height - 1; y >= 0; y--)
                {
                    for (int x = 0; x < lockBitmap.Width; x++)
                    {
                        Color cl    = lockBitmap.GetPixel(x, y);
                        int   shift = x - randShift.Next(0, strength);

                        for (int i = shift; (i <= x && i >= 0); i++)
                        {
                            lockResult.SetPixel(i, y, cl);
                        }
                    }
                }
                break;

            case Direction.Right:
                for (int y = lockBitmap.Height - 1; y >= 0; y--)
                {
                    for (int x = lockBitmap.Width - 1; x >= 0; x--)
                    {
                        Color cl    = lockBitmap.GetPixel(x, y);
                        int   shift = x + randShift.Next(0, strength);

                        for (int i = x; (i <= shift && i < lockBitmap.Width); i++)
                        {
                            lockResult.SetPixel(i, y, cl);
                        }
                    }
                }
                break;

            case Direction.Down:
                for (int x = 0; x < lockBitmap.Width; x++)
                {
                    for (int y = lockBitmap.Height - 1; y >= 0; y--)
                    {
                        Color cl    = lockBitmap.GetPixel(x, y);
                        int   shift = y + randShift.Next(0, strength);

                        for (int i = y; (i <= shift && i < lockBitmap.Height); i++)
                        {
                            lockResult.SetPixel(x, i, cl);
                        }
                    }
                }
                break;

            case Direction.Up:
                for (int x = 0; x < lockBitmap.Width; x++)
                {
                    for (int y = 0; y < lockBitmap.Height; y++)
                    {
                        Color cl    = lockBitmap.GetPixel(x, y);
                        int   shift = y - randShift.Next(0, strength);

                        for (int i = shift; (i <= y && i >= 0); i++)
                        {
                            lockResult.SetPixel(x, i, cl);
                        }
                    }
                }
                break;
            }
            lockBitmap.UnlockBits();
            lockResult.UnlockBits();
            return(result);
        }