Helper Class that gets and sets image pixels using Marshal calls.
예제 #1
0
        /// <summary>
        /// Runs the process of Posterize.
        /// </summary>
        /// <returns>The posterized image.</returns>
        public Bitmap ExecuteFilter(int step)
        {
            if (step == 255)
            {
                return(originalImage);
            }


            var image = new PixelUtil(originalImage);

            image.LockBits();

            int height = originalImage.Height;
            int width  = originalImage.Width;

            #region Loop

            //x - width - sides
            for (int x = 0; x < width; x++)
            {
                //y - height - up/down
                for (int y = 0; y < height; y++)
                {
                    image.SetPixel(x, y, PosterizeCalculus(image.GetPixel(x, y), (step / 100F)));
                }
            }

            #endregion

            image.UnlockBits();
            return(originalImage);
        }
예제 #2
0
        /// <summary>
        /// Runs the process of Posterize.
        /// </summary>
        /// <returns>The posterized image.</returns>
        public Bitmap ExecuteFilter(int step)
        {
            if (step == 255) return originalImage;

            var image = new PixelUtil(originalImage);
            image.LockBits();

            int height = originalImage.Height;
            int width = originalImage.Width;

            #region Loop

            //x - width - sides
            for (int x = 0; x < width; x++)
            {
                //y - height - up/down
                for (int y = 0; y < height; y++)
                {
                    image.SetPixel(x, y, PosterizeCalculus(image.GetPixel(x, y), (step /100F)));
                }
            }

            #endregion

            image.UnlockBits();
            return originalImage;
        }
예제 #3
0
        /// <summary>
        /// Analizes all frames (from the end to the start) and paints all unchanged pixels with a given color.
        /// </summary>
        /// <param name="listBit">The list of frames to analize. This is a parameter by reference.</param>
        /// <param name="transparent">The color to paint the unchanged pixels.</param>
        public static void PaintTransparent(List <Bitmap> listBit, Color transparent)
        {
            for (int index = listBit.Count - 1; index > 0; index--)
            {
                //end to start FOR
                if (index > 0)
                {
                    var image1 = new PixelUtil(listBit[index - 1]); //last
                    var image2 = new PixelUtil(listBit[index]);     //actual

                    image1.LockBits();
                    image2.LockBits();

                    int height = listBit[index - 1].Height;
                    int width  = listBit[index - 1].Width - 1;

                    //Parallelize each column.
                    Parallel.For(0, width, x =>
                    {
                        for (int y = 0; y < height; y++)
                        {
                            if (image1.GetPixel(x, y) == image2.GetPixel(x, y))
                            {
                                image2.SetPixel(x, y, transparent);
                            }
                        }
                    }); //SPEEEEEED, this has alot!

                    #region Old Sequential Code

                    //Benchmark.Start();

                    //for (int x = 0; x < listBit[index - 1].Width; x++)
                    //{
                    //    for (int y = 0; y < listBit[index - 1].Height; y++)
                    //    {
                    //        if (image1.GetPixel(x, y) == image2.GetPixel(x, y))
                    //        {
                    //            image2.SetPixel(x, y, transparent);
                    //        }
                    //    }
                    //}

                    //Benchmark.End();
                    //Console.WriteLine(Benchmark.Span);

                    #endregion

                    image1.UnlockBits();
                    image2.UnlockBits();
                }
            }//); //delete ); if for
        }
예제 #4
0
        /// <summary>
        /// Apply smooth efect on image
        /// </summary>
        /// <param name="image">System.Drawing.Bitmap that will receive blur efect</param>
        /// <param name="rectangle">System.Drawing.Rectangle, the area to apply the efect</param>
        /// <param name="blurSize">System.Int32, the intensity of the blur</param>
        /// <returns>System.Drawing.Bitmap with apllied colors</returns>
        public static Bitmap Blur(Bitmap image, Rectangle rectangle, Int32 blurSize)
        {
            var blurred = new Bitmap(image);

            var pixelUtil = new PixelUtil(blurred);

            pixelUtil.LockBits();

            // look at every pixel in the blur rectangle
            for (var xx = rectangle.X; xx < rectangle.X + rectangle.Width; xx++)
            {
                for (var yy = rectangle.Y; yy < rectangle.Y + rectangle.Height; yy++)
                {
                    Int32 avgR = 0, avgG = 0, avgB = 0;
                    var   blurPixelCount = 0;

                    // average the color of the red, green and blue for each pixel in the
                    // blur size while making sure you don't go outside the image bounds
                    for (var x = xx; (x < xx + blurSize && x < image.Width); x++)
                    {
                        for (var y = yy; (y < yy + blurSize && y < image.Height); y++)
                        {
                            var pixel = pixelUtil.GetPixel(x, y);

                            avgR += pixel.R;
                            avgG += pixel.G;
                            avgB += pixel.B;

                            blurPixelCount++;
                        }
                    }

                    avgR = avgR / blurPixelCount;
                    avgG = avgG / blurPixelCount;
                    avgB = avgB / blurPixelCount;

                    // now that we know the average for the blur size, set each pixel to that color
                    for (var x = xx; x < xx + blurSize && x < image.Width && x < rectangle.Width; x++)
                    {
                        for (var y = yy; y < yy + blurSize && y < image.Height && y < rectangle.Height; y++)
                        {
                            pixelUtil.SetPixel(x, y, Color.FromArgb(avgR, avgG, avgB));
                        }
                    }
                }
            }

            pixelUtil.UnlockBits();

            return(blurred);
        }
예제 #5
0
        /// <summary>
        /// Extracts image pixels into byte array "pixels".
        /// </summary>
        private void GetImagePixels()
        {
            //int w = _image.Width;
            //int h = _image.Height;
            //		int type = image.GetType().;
            //if ((w != _width) || (h != _height))
            //{
            //    // create new image with right size/format
            //    Image temp = new Bitmap(_width, _height);
            //    Graphics g = Graphics.FromImage(temp);
            //    g.DrawImage(_image, 0, 0);
            //    _image = temp;
            //    g.Dispose();
            //}

            //Performance upgrade, now encoding takes half of the time, due to Marshal calls.

            _pixels = new Byte[3 * _image.Width * _image.Height];
            int count      = 0;
            var tempBitmap = new Bitmap(_image);

            var pixelUtil = new PixelUtil(tempBitmap);

            pixelUtil.LockBits();

            //Benchmark.Start();

            for (int th = 0; th < _image.Height; th++)
            {
                for (int tw = 0; tw < _image.Width; tw++)
                {
                    Color color = pixelUtil.GetPixel(tw, th);
                    _pixels[count] = color.R;
                    count++;
                    _pixels[count] = color.G;
                    count++;
                    _pixels[count] = color.B;
                    count++;
                }
            }

            pixelUtil.UnlockBits();

            //Benchmark.End();
            //Console.WriteLine(Benchmark.GetSeconds());

            //pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        }
예제 #6
0
        /// <summary>
        /// Applies the pixelate effect in given frame.
        /// </summary>
        /// <param name="image">The image to pixelate.</param>
        /// <param name="rectangle">The area to pixelate.</param>
        /// <param name="pixelateSize">The size of the pixel.</param>
        /// <returns>A pixelated Bitmap.</returns>
        public static Bitmap Pixelate(Bitmap image, Rectangle rectangle, Int32 pixelateSize)
        {
            var pixelated = new Bitmap(image);

            var pixelUtil = new PixelUtil(pixelated);

            pixelUtil.LockBits();

            // look at every pixel in the rectangle while making sure we're within the image bounds
            for (var xx = rectangle.X; xx < rectangle.X + rectangle.Width && xx < image.Width; xx += pixelateSize)
            {
                for (var yy = rectangle.Y; yy < rectangle.Y + rectangle.Height && yy < image.Height; yy += pixelateSize)
                {
                    var offsetX = pixelateSize / 2;
                    var offsetY = pixelateSize / 2;

                    // make sure that the offset is within the boundry of the image
                    while (xx + offsetX >= image.Width)
                    {
                        offsetX--;
                    }
                    while (yy + offsetY >= image.Height)
                    {
                        offsetY--;
                    }

                    // get the pixel color in the center of the soon to be pixelated area
                    var pixel = pixelUtil.GetPixel(xx + offsetX, yy + offsetY);

                    // for each pixel in the pixelate size, set it to the center color
                    for (var x = xx; x < xx + pixelateSize && x < image.Width; x++)
                    {
                        for (var y = yy; y < yy + pixelateSize && y < image.Height; y++)
                        {
                            pixelUtil.SetPixel(x, y, pixel);
                        }
                    }
                }
            }

            pixelUtil.UnlockBits();

            return(pixelated);
        }
예제 #7
0
        /// <summary>
        /// Extracts image pixels into byte array "pixels".
        /// </summary>
        private void GetImagePixels()
        {
            //Performance upgrade, now encoding takes half of the time, due to Marshal calls.
            int count      = 0;
            var tempBitmap = new Bitmap(_image);

            var pixelUtil = new PixelUtil(tempBitmap);

            pixelUtil.LockBits();

            //Benchmark.Start();
            _pixels = new Byte[3 * _image.Width * _image.Height];

            for (int th = 0; th < _image.Height; th++)
            {
                for (int tw = 0; tw < _image.Width; tw++)
                {
                    Color color = pixelUtil.GetPixel(tw, th);
                    //_pixels[count] = color.R;
                    //count++;
                    //_pixels[count] = color.G;
                    //count++;
                    //_pixels[count] = color.B;
                    //count++;

                    _pixels[count] = color.B;
                    count++;
                    _pixels[count] = color.G;
                    count++;
                    _pixels[count] = color.R;
                    count++;
                }
            }

            pixelUtil.UnlockBits();

            //Benchmark.End();
            //Console.WriteLine(Benchmark.GetSeconds
        }
예제 #8
0
        /// <summary>
        /// Analizes all frames (from the end to the start) and paints all unchanged pixels with a given color,
        /// after, it cuts the image to reduce filesize.
        /// </summary>
        /// <param name="listBit">The list of frames to analize. This is a parameter by reference.</param>
        /// <param name="transparent">The color to paint the unchanged pixels.</param>
        /// <returns>A List contaning all frames and its cut points</returns>
        public static List <FrameInfo> PaintTransparentAndCut(List <string> listBit, Color transparent)
        {
            var listToEncode = new List <FrameInfo>();

            //End to start FOR
            for (var index = listBit.Count - 1; index > 0; index--)
            {
                #region For each Frame, from the end to the start

                Processing.Status(index - 1);

                //First frame is ignored.
                if (index <= 0)
                {
                    continue;
                }

                var imageAux1 = listBit[index - 1].From();
                var imageAux2 = listBit[index].From();

                var startY = new bool[imageAux1.Height];
                var startX = new bool[imageAux1.Width];

                var image1 = new PixelUtil(imageAux1); //Previous image
                var image2 = new PixelUtil(imageAux2); //Actual image

                image1.LockBits();
                image2.LockBits();

                int height = imageAux1.Height;
                int width  = imageAux1.Width;

                //Only use Parallel if the image is big enough.
                if ((width * height) > 150000)
                {
                    #region Parallel Loop

                    //x - width - sides
                    Parallel.For(0, width, x =>
                    {
                        //y - height - up/down
                        for (var y = 0; y < height; y++)
                        {
                            if (image1.GetPixel(x, y) == image2.GetPixel(x, y))
                            {
                                image2.SetPixel(x, y, transparent);
                            }
                            else
                            {
                                #region Get the Changed Pixels

                                startX[x] = true;
                                startY[y] = true;

                                #endregion
                            }
                        }
                    }); //SPEEEEEED, alot!

                    #endregion
                }
                else
                {
                    #region Sequential Loop

                    //x - width - sides
                    for (var x = 0; x < width; x++)
                    {
                        //y - height - up/down
                        for (var y = 0; y < height; y++)
                        {
                            #region For each Pixel

                            if (image1.GetPixel(x, y) == image2.GetPixel(x, y))
                            {
                                image2.SetPixel(x, y, transparent);
                            }
                            else
                            {
                                #region Get the Changed Pixels

                                startX[x] = true;
                                startY[y] = true;

                                #endregion
                            }

                            #endregion
                        }
                    }

                    #endregion
                }

                image1.UnlockBits();
                image2.UnlockBits();

                #region Verify positions

                var firstX = startX.ToList().FindIndex(x => x);
                var lastX  = startX.ToList().FindLastIndex(x => x);

                if (firstX == -1)
                {
                    firstX = 0;
                }
                if (lastX == -1)
                {
                    lastX = imageAux1.Width;
                }

                var firstY = startY.ToList().FindIndex(x => x);
                var lastY  = startY.ToList().FindLastIndex(x => x);

                if (lastY == -1)
                {
                    lastY = imageAux1.Height;
                }
                if (firstY == -1)
                {
                    firstY = 0;
                }

                if (lastX < firstX)
                {
                    var aux = lastX;
                    lastX  = firstX;
                    firstX = aux;
                }

                if (lastY < firstY)
                {
                    var aux = lastY;
                    lastY  = firstY;
                    firstY = aux;
                }

                #endregion

                #region Get the Width and Height

                var heigthCut = Math.Abs(lastY - firstY);
                var widthCut  = Math.Abs(lastX - firstX);

                if (heigthCut != height)
                {
                    heigthCut++;
                }
                else
                {
                    //It means that no pixel got changed.
                    heigthCut = 1;
                    //So i cut to 1 pixel to save the most, 0 can't be.
                }

                if (widthCut != width)
                {
                    widthCut++;
                }
                else
                {
                    widthCut = 1;
                }

                #endregion

                //Cut the images and get the new values.
                var imageSave2 = new Bitmap(imageAux2.Clone(new Rectangle(firstX, firstY, widthCut, heigthCut), imageAux2.PixelFormat));

                imageAux2.Dispose();
                imageAux1.Dispose();

                File.Delete(listBit[index]);

                imageSave2.Save(listBit[index]);

                //Add to listToEncode.
                listToEncode.Insert(0, new FrameInfo(listBit[index], new Point(firstX, firstY)));

                GC.Collect(1);

                #endregion
            }

            //Inserts the first not modified frame.
            listToEncode.Insert(0, new FrameInfo(listBit[0], new Point(0, 0)));

            return(listToEncode);
        }
예제 #9
0
        /// <summary>
        /// Extracts image pixels into byte array "pixels".
        /// </summary>
        private void GetImagePixels()
        {
            //int w = _image.Width;
            //int h = _image.Height;
            //		int type = image.GetType().;
            //if ((w != _width) || (h != _height))
            //{
            //    // create new image with right size/format
            //    Image temp = new Bitmap(_width, _height);
            //    Graphics g = Graphics.FromImage(temp);
            //    g.DrawImage(_image, 0, 0);
            //    _image = temp;
            //    g.Dispose();
            //}

            //Performance upgrade, now encoding takes half of the time, due to Marshal calls.

            _pixels = new Byte[3 * _image.Width * _image.Height];
            int count = 0;
            var tempBitmap = new Bitmap(_image);

            var pixelUtil = new PixelUtil(tempBitmap);
            pixelUtil.LockBits();

            //Benchmark.Start();

            for (int th = 0; th < _image.Height; th++)
            {
                for (int tw = 0; tw < _image.Width; tw++)
                {
                    Color color = pixelUtil.GetPixel(tw, th);
                    _pixels[count] = color.R;
                    count++;
                    _pixels[count] = color.G;
                    count++;
                    _pixels[count] = color.B;
                    count++;
                }
            }

            pixelUtil.UnlockBits();

            //Benchmark.End();
            //Console.WriteLine(Benchmark.GetSeconds());

            //pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        }
예제 #10
0
        /// <summary>
        /// Extracts image pixels into byte array "pixels".
        /// </summary>
        private void GetImagePixels()
        {
            //Performance upgrade, now encoding takes half of the time, due to Marshal calls.
            int count = 0;
            var tempBitmap = new Bitmap(_image);

            var pixelUtil = new PixelUtil(tempBitmap);
            pixelUtil.LockBits();

            //Benchmark.Start();
            _pixels = new Byte[3 * _image.Width * _image.Height];

            for (int th = 0; th < _image.Height; th++)
            {
                for (int tw = 0; tw < _image.Width; tw++)
                {
                    Color color = pixelUtil.GetPixel(tw, th);
                    //_pixels[count] = color.R;
                    //count++;
                    //_pixels[count] = color.G;
                    //count++;
                    //_pixels[count] = color.B;
                    //count++;

                    _pixels[count] = color.B;
                    count++;
                    _pixels[count] = color.G;
                    count++;
                    _pixels[count] = color.R;
                    count++;
                }
            }

            pixelUtil.UnlockBits();

            //Benchmark.End();
            //Console.WriteLine(Benchmark.GetSeconds
        }
예제 #11
0
        /// <summary>
        /// Analizes all frames (from the end to the start) and paints all unchanged pixels with a given color,
        /// after, it cuts the image to reduce filesize.
        /// </summary>
        /// <param name="listBit">The list of frames to analize. This is a parameter by reference.</param>
        /// <param name="transparent">The color to paint the unchanged pixels.</param>
        /// <returns></returns>
        public static List <FrameInfo> PaintTransparentAndCut(List <Bitmap> listBit, Color transparent)
        {
            var listToEncode = new List <FrameInfo>();

            //end to start FOR
            for (int index = listBit.Count - 1; index > 0; index--)
            {
                var startY = new bool[listBit[index - 1].Height];
                var startX = new bool[listBit[index - 1].Width];

                if (index > 0)
                {
                    var image1 = new PixelUtil(listBit[index - 1]); //previous image
                    var image2 = new PixelUtil(listBit[index]);     //actual image

                    image1.LockBits();
                    image2.LockBits();

                    int height = listBit[index - 1].Height;
                    int width  = listBit[index - 1].Width;

                    #region Loop

                    //x - width - sides
                    for (int x = 0; x < width; x++)
                    {
                        //y - height - up/down
                        for (int y = 0; y < height; y++)
                        {
                            if (image1.GetPixel(x, y) == image2.GetPixel(x, y))
                            {
                                image2.SetPixel(x, y, transparent);
                            }
                            else
                            {
                                #region Get the Changed Pixels

                                startX[x] = true;
                                startY[y] = true;

                                #endregion
                            }
                        }
                    }

                    #endregion

                    image1.UnlockBits();
                    image2.UnlockBits();

                    #region Verify positions

                    int firstX = startX.ToList().FindIndex(x => x);
                    int lastX  = startX.ToList().FindLastIndex(x => x);

                    if (firstX == -1)
                    {
                        firstX = 0;
                    }
                    if (lastX == -1)
                    {
                        lastX = listBit[index - 1].Width;
                    }

                    int firstY = startY.ToList().FindIndex(x => x);
                    int lastY  = startY.ToList().FindLastIndex(x => x);

                    if (lastY == -1)
                    {
                        lastY = listBit[index - 1].Height;
                    }
                    if (firstY == -1)
                    {
                        firstY = 0;
                    }

                    if (lastX < firstX)
                    {
                        int aux = lastX;
                        lastX  = firstX;
                        firstX = aux;
                    }

                    if (lastY < firstY)
                    {
                        int aux = lastY;
                        lastY  = firstY;
                        firstY = aux;
                    }

                    #endregion

                    #region Get the Widht and Height

                    int heigthCut = Math.Abs(lastY - firstY);
                    int widthCut  = Math.Abs(lastX - firstX);

                    if (heigthCut != height)
                    {
                        heigthCut++;
                    }
                    else
                    {
                        //It means that no pixel got changed.
                        heigthCut = 1;
                        //So i cut to 1 pixel to save the most, 0 can't be.
                    }

                    if (widthCut != width)
                    {
                        widthCut++;
                    }
                    else
                    {
                        widthCut = 1;
                    }

                    #endregion

                    //Cut the images and get the new values.
                    listBit[index] = new Bitmap(listBit[index].Clone(new Rectangle(firstX, firstY, widthCut, heigthCut), listBit[index].PixelFormat));

                    //Add to listToEncode.
                    //listToEncode.Insert(0, new FrameInfo(listBit[index], new Point(firstX, firstY)));
                }
            }

            //Inserts the first not modified frame.
            //listToEncode.Insert(0, new FrameInfo(listBit[0], new Point(0, 0)));

            return(listToEncode);
        }