Exemplo n.º 1
0
        private void MonohromeFilter(Bitmap source, Bitmap destination, Color firstColor, Color secondColor, int coefficient)
        {
            var initData   = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);
            var destData   = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, source.PixelFormat);
            int byteLength = 4;

            if (source.PixelFormat.ToString().Contains("24"))
            {
                byteLength = 3;
            }

            unsafe
            {
                byte *sourcePtr = (byte *)initData.Scan0.ToPointer();
                byte *destPtr   = (byte *)destData.Scan0.ToPointer();

                for (int i = 0; i < initData.Stride; i += byteLength)
                {
                    for (int j = 0; j < source.Height; j++)
                    {
                        var pixel = PixelOperations.GetPixelUnsafe(sourcePtr, initData.Stride, i, j, byteLength);
                        pixel = GetMonohromePixel(pixel, byteLength, coefficient);
                        PixelOperations.SetPixelUnsafe(destPtr, pixel, initData.Stride, i, j, byteLength);
                    }
                }
            }

            source.UnlockBits(initData);
            destination.UnlockBits(destData);
        }
Exemplo n.º 2
0
        private void AdditiveMultiplicativeFilter(Bitmap source, Bitmap destination, double[] matrix)
        {
            var initData   = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);
            var destData   = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, source.PixelFormat);
            int byteLength = 4;

            if (source.PixelFormat.ToString().Contains("24"))
            {
                byteLength = 3;
            }


            var pixelCount = 3;

            unsafe
            {
                byte *sourcePtr = (byte *)initData.Scan0.ToPointer();
                byte *destPtr   = (byte *)destData.Scan0.ToPointer();

                int pixelOffset = pixelCount / 2;
                int byteOffset  = pixelOffset * byteLength;
                for (int i = byteOffset; i < (initData.Stride - byteOffset); i += byteLength)
                {
                    for (int j = pixelOffset; j < source.Height - pixelOffset; j++)
                    {
                        byte[][] window = PixelOperations.GetWindowOfPixels(sourcePtr, initData.Stride, i, j, byteLength, pixelCount);
                        byte[]   pixel  = GetAdditiveMultiplicatedPixel(Transform2Dto1DArray(window, byteLength * pixelCount * pixelCount), byteLength, matrix);
                        PixelOperations.SetPixelUnsafe(destPtr, pixel, initData.Stride, i, j, byteLength);
                    }
                }
            }

            source.UnlockBits(initData);
            destination.UnlockBits(destData);
        }
Exemplo n.º 3
0
        private Bitmap RotateImage(Bitmap source, double angle)
        {
            double radAngle = angle * Math.PI / 180;
            var    cos      = Math.Cos(radAngle);
            var    sin      = Math.Sin(radAngle);

            var widthAndHeight = CalculateNewBoundaries(bitmap.Width, bitmap.Height, radAngle);
            int newWidth       = widthAndHeight.width;
            int newHeight      = widthAndHeight.height;

            Bitmap destination = new Bitmap((int)newWidth, (int)newHeight);

            var SrcData  = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);
            var DestData = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, source.PixelFormat);

            int bytelength = 4;

            if (source.PixelFormat.ToString().Contains("24"))
            {
                bytelength = 3;
            }

            int x0 = (int)(newWidth / 2.0);
            int y0 = (int)(newHeight / 2.0);

            unsafe
            {
                byte *SrcPtr  = (byte *)SrcData.Scan0.ToPointer();
                byte *DestPtr = (byte *)DestData.Scan0.ToPointer();

                var offset = GetOffsets(source.Width, source.Height, cos, sin, x0, y0, angle);

                for (double x = 0; x < source.Width; x += 1)
                {
                    for (int y = 0; y < source.Height; y++)
                    {
                        var X = CalculateNewX((int)x, y, cos, sin, x0, y0);
                        var Y = CalculateNewY((int)x, y, cos, sin, x0, y0);
                        X += offset.xOffset;
                        Y += offset.yOffset;

                        if (Y >= 0 && Y < newHeight && X >= 0 && X < newWidth)
                        {
                            var pixel = PixelOperations.GetPixelUnsafe(SrcPtr, SrcData.Stride, (int)Math.Round(x * 4), y, bytelength);
                            PixelOperations.SetPixelUnsafe(DestPtr, pixel, DestData.Stride, X * 4, Y, bytelength);
                            PixelOperations.SetPixelUnsafe(DestPtr, pixel, DestData.Stride, (X + 1) * 4, Y, bytelength);
                        }
                    }
                }
            }
            source.UnlockBits(SrcData);
            destination.UnlockBits(DestData);
            return(destination);
        }
Exemplo n.º 4
0
        private void ZoomPicture(Bitmap source, Bitmap destination, double xZoom, double yZoom)
        {
            var initData   = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);
            var destData   = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, source.PixelFormat);
            int byteLength = 4;

            if (source.PixelFormat.ToString().Contains("24"))
            {
                byteLength = 3;
            }
            else if (source.PixelFormat.ToString().Contains("16"))
            {
                byteLength = 2;
            }
            // Get the address of the first line
            IntPtr ptr1 = initData.Scan0;
            IntPtr ptr2 = destData.Scan0;

            unsafe
            {
                byte *sourcePtr = (byte *)ptr1.ToPointer();
                byte *destPtr   = (byte *)ptr2.ToPointer();

                for (int i = 0; i < destData.Stride; i += 4)
                {
                    for (int j = 0; j < destination.Height; j++)
                    {
                        int newX = (int)(i / xZoom);
                        int newY = (int)(j / yZoom);
                        if (newX % byteLength != 0)
                        {
                            newX -= newX % byteLength;                         //offset to the first byte of four
                        }
                        var pixel = PixelOperations.GetPixelUnsafe(sourcePtr, initData.Stride, newX, newY, byteLength);
                        PixelOperations.SetPixelUnsafe(destPtr, pixel, destData.Stride, i, j, byteLength);
                    }
                }
            }

            source.UnlockBits(initData);
            destination.UnlockBits(destData);
        }
Exemplo n.º 5
0
        private void DrawSegment(int x0, int x1, int y0, int y1, Color color1, Color color2)
        {
            var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            unsafe
            {
                byte *ptr = (byte *)data.Scan0.ToPointer();


                int dx = (x1 > x0) ? (x1 - x0) : (x0 - x1);
                int dy = (y1 > y0) ? (y1 - y0) : (y0 - y1);
                //Направление приращения
                int sx = (x1 >= x0) ? (1) : (-1);
                int sy = (y1 >= y0) ? (1) : (-1);

                if (dy < dx)
                {
                    int   d     = (dy << 1) - dx;
                    int   d1    = dy << 1;
                    int   d2    = (dy - dx) << 1;
                    Color color = GetInterpolateColor(color1, color2, 0);
                    PixelOperations.SetPixelUnsafe(ptr, new byte[] { color.B, color.G, color.R, 255 }, data.Stride, x0 * 4, y0, 4);
                    int x = x0 + sx;
                    int y = y0;
                    for (int i = 1; i <= dx; i++)
                    {
                        if (d > 0)
                        {
                            d += d2;
                            y += sy;
                        }
                        else
                        {
                            d += d1;
                        }
                        color = GetInterpolateColor(color1, color2, (i * 1.0 / dx));
                        PixelOperations.SetPixelUnsafe(ptr, new byte[] { color.B, color.G, color.R, 255 }, data.Stride, x * 4, y, 4);
                        x += sx;
                    }
                }
                else
                {
                    int   d     = (dx << 1) - dy;
                    int   d1    = dx << 1;
                    int   d2    = (dx - dy) << 1;
                    Color color = GetInterpolateColor(color1, color2, 0);
                    PixelOperations.SetPixelUnsafe(ptr, new byte[] { color.B, color.G, color.R, 255 }, data.Stride, x0 * 4, y0, 4);
                    int x = x0;
                    int y = y0 + sy;
                    for (int i = 1; i <= dy; i++)
                    {
                        if (d > 0)
                        {
                            d += d2;
                            x += sx;
                        }
                        else
                        {
                            d += d1;
                        }
                        color = GetInterpolateColor(color1, color2, (i * 1.0 / dy));
                        PixelOperations.SetPixelUnsafe(ptr, new byte[] { color.B, color.G, color.R, 255 }, data.Stride, x * 4, y, 4);
                        y += sy;
                    }
                }
            }

            bitmap.UnlockBits(data);
            pictureBox1.Image = bitmap;
        }