Beispiel #1
0
        protected unsafe override void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            int   width   = destinationData.Width;
            int   height  = destinationData.Height;
            int   stride  = sourceData.Stride;
            int   stride2 = destinationData.Stride;
            int   num     = width;
            byte *ptr     = (byte *)sourceData.ImageData.ToPointer();
            byte *ptr2    = (byte *)destinationData.ImageData.ToPointer();

            ptr += (long)minY * (long)stride;
            if (destinationData.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                ptr += minX;
            }
            else
            {
                ptr += (long)minX * 3L;
                num *= 3;
            }
            for (int i = 0; i < height; i++)
            {
                SystemTools.CopyUnmanagedMemory(ptr2, ptr, num);
                ptr2 += stride2;
                ptr  += stride;
            }
        }
Beispiel #2
0
        public unsafe void Apply(UnmanagedImage sourceImage, UnmanagedImage destinationImage)
        {
            CheckSourceFormat(sourceImage.PixelFormat);
            if (destinationImage.PixelFormat != sourceImage.PixelFormat)
            {
                throw new InvalidImagePropertiesException("Destination pixel format must be the same as pixel format of source image.");
            }
            if (destinationImage.Width != sourceImage.Width || destinationImage.Height != sourceImage.Height)
            {
                throw new InvalidImagePropertiesException("Destination image must have the same width and height as source image.");
            }
            int   stride  = destinationImage.Stride;
            int   stride2 = sourceImage.Stride;
            int   count   = System.Math.Min(stride2, stride);
            byte *ptr     = (byte *)destinationImage.ImageData.ToPointer();
            byte *ptr2    = (byte *)sourceImage.ImageData.ToPointer();
            int   i       = 0;

            for (int height = sourceImage.Height; i < height; i++)
            {
                SystemTools.CopyUnmanagedMemory(ptr, ptr2, count);
                ptr  += stride;
                ptr2 += stride2;
            }
            ProcessFilter(destinationImage, new Rectangle(0, 0, destinationImage.Width, destinationImage.Height));
        }
        /// <summary>
        /// Create managed image from the unmanaged.
        /// </summary>
        ///
        /// <returns>Returns managed copy of the unmanaged image.</returns>
        ///
        /// <remarks><para>The method creates a managed copy of the unmanaged image with the
        /// same size and pixel format.</para></remarks>
        ///
        public Bitmap ToManagedImage()
        {
            // create new image of required format
            Bitmap dstImage = (pixelFormat == PixelFormat.Format8bppIndexed) ?
                              NComputerVision.Common.Image.CreateGrayscaleImage(width, height) :
                              new Bitmap(width, height, pixelFormat);

            // lock destination bitmap data
            BitmapData dstData = dstImage.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, pixelFormat);

            int dstStride = dstData.Stride;
            int lineSize  = System.Math.Min(stride, dstStride);

            unsafe
            {
                byte *dst = (byte *)dstData.Scan0.ToPointer();
                byte *src = (byte *)imageData.ToPointer();

                // copy image
                for (int y = 0; y < height; y++)
                {
                    SystemTools.CopyUnmanagedMemory(dst, src, lineSize);
                    dst += dstStride;
                    src += stride;
                }
            }

            // unlock destination images
            dstImage.UnlockBits(dstData);

            return(dstImage);
        }
        /// <summary>
        /// Copy unmanaged image.
        /// </summary>
        ///
        /// <param name="destImage">Destination image to copy this image to.</param>
        ///
        /// <remarks><para>The method copies current unmanaged image to the specified image.
        /// Size and pixel format of the destination image must be exactly the same.</para></remarks>
        ///
        /// <exception cref="InvalidImagePropertiesException">Destination image has different size or pixel format.</exception>
        ///
        public void Copy(UnmanagedImage destImage)
        {
            if (
                (width != destImage.width) || (height != destImage.height) ||
                (pixelFormat != destImage.pixelFormat))
            {
                throw new InvalidImagePropertiesException("Destination image has different size or pixel format.");
            }

            if (stride == destImage.stride)
            {
                // copy entire image
                SystemTools.CopyUnmanagedMemory(destImage.imageData, imageData, stride * height);
            }
            else
            {
                unsafe
                {
                    int dstStride  = destImage.stride;
                    int copyLength = (stride < dstStride) ? stride : dstStride;

                    byte *src = (byte *)imageData.ToPointer();
                    byte *dst = (byte *)destImage.imageData.ToPointer();

                    // copy line by line
                    for (int i = 0; i < height; i++)
                    {
                        SystemTools.CopyUnmanagedMemory(dst, src, copyLength);

                        dst += dstStride;
                        src += stride;
                    }
                }
            }
        }
        public unsafe void Apply(UnmanagedImage sourceImage, UnmanagedImage destinationImage)
        {
            this.CheckSourceFormat(sourceImage.PixelFormat);
            if (destinationImage.PixelFormat != sourceImage.PixelFormat)
            {
                throw new InvalidImagePropertiesException("Destination pixel format must be the same as pixel format of source image.");
            }
            if ((destinationImage.Width != sourceImage.Width) || (destinationImage.Height != sourceImage.Height))
            {
                throw new InvalidImagePropertiesException("Destination image must have the same width and height as source image.");
            }
            int   stride = destinationImage.Stride;
            int   num2   = sourceImage.Stride;
            int   count  = Math.Min(num2, stride);
            byte *dst    = (byte *)destinationImage.ImageData.ToPointer();
            byte *src    = (byte *)sourceImage.ImageData.ToPointer();
            int   num4   = 0;
            int   height = sourceImage.Height;

            while (num4 < height)
            {
                SystemTools.CopyUnmanagedMemory(dst, src, count);
                dst += stride;
                src += num2;
                num4++;
            }
            this.ProcessFilter(destinationImage);
        }
Beispiel #6
0
        protected unsafe override void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            Rectangle rectangle = rect;

            rectangle.Intersect(new Rectangle(0, 0, sourceData.Width, sourceData.Height));
            int   left    = rectangle.Left;
            int   top     = rectangle.Top;
            int   num     = rectangle.Bottom - 1;
            int   width   = rectangle.Width;
            int   stride  = sourceData.Stride;
            int   stride2 = destinationData.Stride;
            int   num2    = System.Drawing.Image.GetPixelFormatSize(sourceData.PixelFormat) / 8;
            int   count   = width * num2;
            byte *ptr     = (byte *)sourceData.ImageData.ToPointer() + (long)top * (long)stride + (long)left * (long)num2;
            byte *ptr2    = (byte *)destinationData.ImageData.ToPointer();

            if (rect.Top < 0)
            {
                ptr2 -= (long)stride2 * (long)rect.Top;
            }
            if (rect.Left < 0)
            {
                ptr2 -= (long)num2 * (long)rect.Left;
            }
            for (int i = top; i <= num; i++)
            {
                SystemTools.CopyUnmanagedMemory(ptr2, ptr, count);
                ptr  += stride;
                ptr2 += stride2;
            }
        }
Beispiel #7
0
        protected override unsafe void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData)
        {
            System.Drawing.Rectangle rect = this.rect;
            rect.Intersect(new System.Drawing.Rectangle(0, 0, sourceData.Width, sourceData.Height));
            int   left   = rect.Left;
            int   top    = rect.Top;
            int   num3   = rect.Bottom - 1;
            int   width  = rect.Width;
            int   stride = sourceData.Stride;
            int   num6   = destinationData.Stride;
            int   num7   = Image.GetPixelFormatSize(sourceData.PixelFormat) / 8;
            int   count  = width * num7;
            byte *src    = (byte *)((sourceData.ImageData.ToPointer() + (top * stride)) + (left * num7));
            byte *dst    = (byte *)destinationData.ImageData.ToPointer();

            if (this.rect.Top < 0)
            {
                dst -= num6 * this.rect.Top;
            }
            if (this.rect.Left < 0)
            {
                dst -= num7 * this.rect.Left;
            }
            for (int i = top; i <= num3; i++)
            {
                SystemTools.CopyUnmanagedMemory(dst, src, count);
                src += stride;
                dst += num6;
            }
        }
        /// <summary>
        /// Create unmanaged image from the specified managed image.
        /// </summary>
        ///
        /// <param name="imageData">Source locked image data.</param>
        ///
        /// <returns>Returns new unmanaged image, which is a copy of source managed image.</returns>
        ///
        /// <remarks><para>The method creates an exact copy of specified managed image, but allocated
        /// in unmanaged memory. This means that managed image may be unlocked right after call to this
        /// method.</para></remarks>
        ///
        /// <exception cref="UnsupportedImageFormatException">Unsupported pixel format of source image.</exception>
        ///
        public static UnmanagedImage FromManagedImage(BitmapData imageData)
        {
            PixelFormat pixelFormat = imageData.PixelFormat;

            // check source pixel format
            if (
                (pixelFormat != PixelFormat.Format8bppIndexed) &&
                (pixelFormat != PixelFormat.Format16bppGrayScale) &&
                (pixelFormat != PixelFormat.Format24bppRgb) &&
                (pixelFormat != PixelFormat.Format32bppRgb) &&
                (pixelFormat != PixelFormat.Format32bppArgb) &&
                (pixelFormat != PixelFormat.Format48bppRgb) &&
                (pixelFormat != PixelFormat.Format64bppArgb))
            {
                throw new UnsupportedImageFormatException("Unsupported pixel format of the source image.");
            }

            // allocate memory for the image
            IntPtr dstImageData = System.Runtime.InteropServices.Marshal.AllocHGlobal(imageData.Stride * imageData.Height);

            UnmanagedImage image = new UnmanagedImage(dstImageData, imageData.Width, imageData.Height, imageData.Stride, pixelFormat);

            SystemTools.CopyUnmanagedMemory(dstImageData, imageData.Scan0, imageData.Stride * imageData.Height);
            image.mustBeDisposed = true;

            return(image);
        }
Beispiel #9
0
        public unsafe void ProcessFrame(UnmanagedImage videoFrame)
        {
            lock (sync)
            {
                if (previousFrame == null)
                {
                    width  = videoFrame.Width;
                    height = videoFrame.Height;

                    previousFrame = UnmanagedImage.Create(width, height, PixelFormat.Format8bppIndexed);
                    motionFrame   = UnmanagedImage.Create(width, height, PixelFormat.Format8bppIndexed);

                    frameSize = motionFrame.Stride * height;

                    if (suppressNoise)
                    {
                        tempFrame = UnmanagedImage.Create(width, height, PixelFormat.Format8bppIndexed);
                    }

                    Tools.ConvertToGrayscale(videoFrame, previousFrame);

                    return;
                }

                if ((videoFrame.Width != width) || (videoFrame.Height != height))
                {
                    return;
                }

                Tools.ConvertToGrayscale(videoFrame, motionFrame);

                byte *prevFrame = (byte *)previousFrame.ImageData.ToPointer();
                byte *currFrame = (byte *)motionFrame.ImageData.ToPointer();

                int diff;

                for (int i = 0; i < frameSize; i++, prevFrame++, currFrame++)
                {
                    diff = (int)*currFrame - (int)*prevFrame;
                    *prevFrame = *currFrame;

                    *currFrame = ((diff >= differenceThreshold) || (diff <= differenceThresholdNeg)) ? (byte)255 : (byte)0;
                }

                if (suppressNoise)
                {
                    SystemTools.CopyUnmanagedMemory(tempFrame.ImageData, motionFrame.ImageData, frameSize);
                    erosionFilter.Apply(tempFrame, motionFrame);
                }

                pixelsChanged = 0;
                byte *motion = (byte *)motionFrame.ImageData.ToPointer();

                for (int i = 0; i < frameSize; i++, motion++)
                {
                    pixelsChanged += (*motion & 1);
                }
            }
        }
Beispiel #10
0
        protected override unsafe void ProcessFilter(UnmanagedImage source, UnmanagedImage destination)
        {
            int   num    = Image.GetPixelFormatSize(source.PixelFormat) / 8;
            int   width  = source.Width;
            int   height = source.Height;
            int   num4   = Math.Min(width, this.warpMap.GetLength(1));
            int   num5   = Math.Min(height, this.warpMap.GetLength(0));
            int   stride = source.Stride;
            int   num7   = destination.Stride;
            int   num8   = num7 - (num4 * num);
            byte *numPtr = (byte *)source.ImageData.ToPointer();
            byte *dst    = (byte *)destination.ImageData.ToPointer();

            for (int i = 0; i < num5; i++)
            {
                for (int j = 0; j < num4; j++)
                {
                    int num9  = j + this.warpMap[i, j].X;
                    int num10 = i + this.warpMap[i, j].Y;
                    if (((num9 >= 0) && (num10 >= 0)) && ((num9 < width) && (num10 < height)))
                    {
                        byte *numPtr3 = (numPtr + (num10 * stride)) + (num9 * num);
                        int   num13   = 0;
                        while (num13 < num)
                        {
                            dst[0] = numPtr3[0];
                            num13++;
                            dst++;
                            numPtr3++;
                        }
                    }
                    else
                    {
                        int num14 = 0;
                        while (num14 < num)
                        {
                            dst[0] = 0;
                            num14++;
                            dst++;
                        }
                    }
                }
                if (width != num4)
                {
                    SystemTools.CopyUnmanagedMemory(dst, (numPtr + (i * stride)) + (num4 * num), (width - num4) * num);
                }
                dst += num8;
            }
            int num15 = num5;

            while (num15 < height)
            {
                SystemTools.CopyUnmanagedMemory(dst, numPtr + (num15 * stride), width * num);
                num15++;
                dst += num7;
            }
        }
Beispiel #11
0
        protected unsafe override void ProcessFilter(UnmanagedImage source, UnmanagedImage destination)
        {
            int   num     = System.Drawing.Image.GetPixelFormatSize(source.PixelFormat) / 8;
            int   width   = source.Width;
            int   height  = source.Height;
            int   num2    = System.Math.Min(width, warpMap.GetLength(1));
            int   num3    = System.Math.Min(height, warpMap.GetLength(0));
            int   stride  = source.Stride;
            int   stride2 = destination.Stride;
            int   num4    = stride2 - num2 * num;
            byte *ptr     = (byte *)source.ImageData.ToPointer();
            byte *ptr2    = (byte *)destination.ImageData.ToPointer();

            for (int i = 0; i < num3; i++)
            {
                for (int j = 0; j < num2; j++)
                {
                    int num5 = j + warpMap[i, j].X;
                    int num6 = i + warpMap[i, j].Y;
                    if (num5 >= 0 && num6 >= 0 && num5 < width && num6 < height)
                    {
                        byte *ptr3 = ptr + (long)num6 * (long)stride + (long)num5 * (long)num;
                        int   num7 = 0;
                        while (num7 < num)
                        {
                            *ptr2 = *ptr3;
                            num7++;
                            ptr2++;
                            ptr3++;
                        }
                    }
                    else
                    {
                        int num8 = 0;
                        while (num8 < num)
                        {
                            *ptr2 = 0;
                            num8++;
                            ptr2++;
                        }
                    }
                }
                if (width != num2)
                {
                    SystemTools.CopyUnmanagedMemory(ptr2, ptr + (long)i * (long)stride + (long)num2 * (long)num, (width - num2) * num);
                }
                ptr2 += num4;
            }
            int num9 = num3;

            while (num9 < height)
            {
                SystemTools.CopyUnmanagedMemory(ptr2, ptr + (long)num9 * (long)stride, width * num);
                num9++;
                ptr2 += stride2;
            }
        }
Beispiel #12
0
        protected override unsafe void ProcessFilter(UnmanagedImage source, UnmanagedImage destination, Rectangle rect)
        {
            int   num      = Image.GetPixelFormatSize(source.PixelFormat) / 8;
            int   left     = rect.Left;
            int   top      = rect.Top;
            int   num4     = left + rect.Width;
            int   num5     = top + rect.Height;
            int   stride   = source.Stride;
            int   num7     = destination.Stride;
            int   num8     = num7 - (rect.Width * num);
            int   maxValue = (this.radius * 2) + 1;
            byte *src      = (byte *)source.ImageData.ToPointer();
            byte *dst      = (byte *)destination.ImageData.ToPointer();

            if (stride == num7)
            {
                SystemTools.CopyUnmanagedMemory(dst, src, stride * source.Height);
            }
            else
            {
                int count  = source.Width * num;
                int num13  = 0;
                int height = source.Height;
                while (num13 < height)
                {
                    SystemTools.CopyUnmanagedMemory(dst + (num7 * num13), src + (stride * num13), count);
                    num13++;
                }
            }
            dst += (top * num7) + (left * num);
            for (int i = top; i < num5; i++)
            {
                for (int j = left; j < num4; j++)
                {
                    int num9  = (j + this.rand.Next(maxValue)) - this.radius;
                    int num10 = (i + this.rand.Next(maxValue)) - this.radius;
                    if (((num9 >= left) && (num10 >= top)) && ((num9 < num4) && (num10 < num5)))
                    {
                        byte *numPtr3 = (src + (num10 * stride)) + (num9 * num);
                        int   num17   = 0;
                        while (num17 < num)
                        {
                            dst[0] = numPtr3[0];
                            num17++;
                            dst++;
                            numPtr3++;
                        }
                    }
                    else
                    {
                        dst += num;
                    }
                }
                dst += num8;
            }
        }
Beispiel #13
0
        public UnmanagedImage Clone()
        {
            IntPtr dst = Marshal.AllocHGlobal(stride * height);

            GC.AddMemoryPressure(stride * height);
            UnmanagedImage unmanagedImage = new UnmanagedImage(dst, width, height, stride, pixelFormat);

            unmanagedImage.mustBeDisposed = true;
            SystemTools.CopyUnmanagedMemory(dst, imageData, stride * height);
            return(unmanagedImage);
        }
Beispiel #14
0
        protected unsafe override void ProcessFilter(UnmanagedImage source, UnmanagedImage destination, Rectangle rect)
        {
            int   num      = System.Drawing.Image.GetPixelFormatSize(source.PixelFormat) / 8;
            int   left     = rect.Left;
            int   top      = rect.Top;
            int   num2     = left + rect.Width;
            int   num3     = top + rect.Height;
            int   stride   = source.Stride;
            int   stride2  = destination.Stride;
            int   num4     = stride2 - rect.Width * num;
            int   maxValue = radius * 2 + 1;
            byte *ptr      = (byte *)source.ImageData.ToPointer();
            byte *ptr2     = (byte *)destination.ImageData.ToPointer();

            if (stride == stride2)
            {
                SystemTools.CopyUnmanagedMemory(ptr2, ptr, stride * source.Height);
            }
            else
            {
                int count = source.Width * num;
                int i     = 0;
                for (int height = source.Height; i < height; i++)
                {
                    SystemTools.CopyUnmanagedMemory(ptr2 + (long)stride2 * (long)i, ptr + (long)stride * (long)i, count);
                }
            }
            ptr2 += top * stride2 + left * num;
            for (int j = top; j < num3; j++)
            {
                for (int k = left; k < num2; k++)
                {
                    int num5 = k + rand.Next(maxValue) - radius;
                    int num6 = j + rand.Next(maxValue) - radius;
                    if (num5 >= left && num6 >= top && num5 < num2 && num6 < num3)
                    {
                        byte *ptr3 = ptr + (long)num6 * (long)stride + (long)num5 * (long)num;
                        int   num7 = 0;
                        while (num7 < num)
                        {
                            *ptr2 = *ptr3;
                            num7++;
                            ptr2++;
                            ptr3++;
                        }
                    }
                    else
                    {
                        ptr2 += num;
                    }
                }
                ptr2 += num4;
            }
        }
        public UnmanagedImage Clone()
        {
            IntPtr         imageData = Marshal.AllocHGlobal((int)(this.stride * this.height));
            UnmanagedImage image     = new UnmanagedImage(imageData, this.width, this.height, this.stride, this.pixelFormat)
            {
                mustBeDisposed = true
            };

            SystemTools.CopyUnmanagedMemory(imageData, this.imageData, this.stride * this.height);
            return(image);
        }
Beispiel #16
0
        public static Bitmap Clone(BitmapData sourceData)
        {
            int        width      = sourceData.Width;
            int        height     = sourceData.Height;
            Bitmap     bitmap     = new Bitmap(width, height, sourceData.PixelFormat);
            BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bitmap.PixelFormat);

            SystemTools.CopyUnmanagedMemory(bitmapdata.Scan0, sourceData.Scan0, height * sourceData.Stride);
            bitmap.UnlockBits(bitmapdata);
            return(bitmap);
        }
        /// <summary>
        /// Clone the unmanaged images.
        /// </summary>
        ///
        /// <returns>Returns clone of the unmanaged image.</returns>
        ///
        /// <remarks><para>The method does complete cloning of the object.</para></remarks>
        ///
        public UnmanagedImage Clone()
        {
            // allocate memory for the image
            IntPtr newImageData = System.Runtime.InteropServices.Marshal.AllocHGlobal(stride * height);

            UnmanagedImage newImage = new UnmanagedImage(newImageData, width, height, stride, pixelFormat);

            newImage.mustBeDisposed = true;

            SystemTools.CopyUnmanagedMemory(newImageData, imageData, stride * height);

            return(newImage);
        }
 public void ApplyInPlace(UnmanagedImage image, Rectangle rect)
 {
     this.CheckSourceFormat(image.PixelFormat);
     rect.Intersect(new Rectangle(0, 0, image.Width, image.Height));
     if ((rect.Width | rect.Height) != 0)
     {
         int    size = image.Stride * image.Height;
         IntPtr dst  = MemoryManager.Alloc(size);
         SystemTools.CopyUnmanagedMemory(dst, image.ImageData, size);
         this.ProcessFilter(new UnmanagedImage(dst, image.Width, image.Height, image.Stride, image.PixelFormat), image, rect);
         MemoryManager.Free(dst);
     }
 }
        public static UnmanagedImage FromManagedImage(BitmapData imageData)
        {
            System.Drawing.Imaging.PixelFormat pixelFormat = imageData.PixelFormat;
            if (((((pixelFormat != System.Drawing.Imaging.PixelFormat.Format8bppIndexed) && (pixelFormat != System.Drawing.Imaging.PixelFormat.Format16bppGrayScale)) && ((pixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb) && (pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppRgb))) && (((pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb) && (pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppPArgb)) && ((pixelFormat != System.Drawing.Imaging.PixelFormat.Format48bppRgb) && (pixelFormat != System.Drawing.Imaging.PixelFormat.Format64bppArgb)))) && (pixelFormat != System.Drawing.Imaging.PixelFormat.Format64bppPArgb))
            {
                throw new UnsupportedImageFormatException("Unsupported pixel format of the source image.");
            }
            IntPtr         ptr   = Marshal.AllocHGlobal((int)(imageData.Stride * imageData.Height));
            UnmanagedImage image = new UnmanagedImage(ptr, imageData.Width, imageData.Height, imageData.Stride, pixelFormat);

            SystemTools.CopyUnmanagedMemory(ptr, imageData.Scan0, imageData.Stride * imageData.Height);
            image.mustBeDisposed = true;
            return(image);
        }
Beispiel #20
0
        public static UnmanagedImage FromManagedImage(BitmapData imageData)
        {
            PixelFormat pixelFormat = imageData.PixelFormat;

            if (pixelFormat != PixelFormat.Format8bppIndexed && pixelFormat != PixelFormat.Format16bppGrayScale && pixelFormat != PixelFormat.Format24bppRgb && pixelFormat != PixelFormat.Format32bppRgb && pixelFormat != PixelFormat.Format32bppArgb && pixelFormat != PixelFormat.Format32bppPArgb && pixelFormat != PixelFormat.Format48bppRgb && pixelFormat != PixelFormat.Format64bppArgb && pixelFormat != PixelFormat.Format64bppPArgb)
            {
                throw new UnsupportedImageFormatException("Unsupported pixel format of the source image.");
            }
            IntPtr dst = Marshal.AllocHGlobal(imageData.Stride * imageData.Height);

            GC.AddMemoryPressure(imageData.Stride * imageData.Height);
            UnmanagedImage unmanagedImage = new UnmanagedImage(dst, imageData.Width, imageData.Height, imageData.Stride, pixelFormat);

            SystemTools.CopyUnmanagedMemory(dst, imageData.Scan0, imageData.Stride * imageData.Height);
            unmanagedImage.mustBeDisposed = true;
            return(unmanagedImage);
        }
        private static Bitmap CloneImage(BitmapData sourceData)
        {
            int width  = sourceData.Width;
            int height = sourceData.Height;

            Bitmap destination = new Bitmap(width, height, sourceData.PixelFormat);

            BitmapData destinationData = destination.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, destination.PixelFormat);

            SystemTools.CopyUnmanagedMemory(destinationData.Scan0, sourceData.Scan0, height * sourceData.Stride);

            destination.UnlockBits(destinationData);

            return(destination);
        }
Beispiel #22
0
        public unsafe Bitmap ToManagedImage(bool makeCopy)
        {
            Bitmap bitmap = null;

            try
            {
                if (!makeCopy)
                {
                    bitmap = new Bitmap(width, height, stride, pixelFormat, imageData);
                    if (pixelFormat == PixelFormat.Format8bppIndexed)
                    {
                        Image.SetGrayscalePalette(bitmap);
                    }
                }
                else
                {
                    bitmap = ((pixelFormat == PixelFormat.Format8bppIndexed) ? Image.CreateGrayscaleImage(width, height) : new Bitmap(width, height, pixelFormat));
                    BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, pixelFormat);
                    int        num        = bitmapData.Stride;
                    int        count      = System.Math.Min(stride, num);
                    byte *     ptr        = (byte *)bitmapData.Scan0.ToPointer();
                    byte *     ptr2       = (byte *)imageData.ToPointer();
                    if (stride != num)
                    {
                        for (int i = 0; i < height; i++)
                        {
                            SystemTools.CopyUnmanagedMemory(ptr, ptr2, count);
                            ptr  += num;
                            ptr2 += stride;
                        }
                    }
                    else
                    {
                        SystemTools.CopyUnmanagedMemory(ptr, ptr2, stride * height);
                    }
                    bitmap.UnlockBits(bitmapData);
                }
                return(bitmap);
            }
            catch (Exception)
            {
                bitmap?.Dispose();
                throw new InvalidImagePropertiesException("The unmanaged image has some invalid properties, which results in failure of converting it to managed image.");
            }
        }
        public unsafe Bitmap ToManagedImage()
        {
            Bitmap     bitmap     = (this.pixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) ? Image.CreateGrayscaleImage(this.width, this.height) : new Bitmap(this.width, this.height, this.pixelFormat);
            BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, this.width, this.height), ImageLockMode.ReadWrite, this.pixelFormat);
            int        stride     = bitmapdata.Stride;
            int        count      = Math.Min(this.stride, stride);
            byte *     dst        = (byte *)bitmapdata.Scan0.ToPointer();
            byte *     src        = (byte *)this.imageData.ToPointer();

            for (int i = 0; i < this.height; i++)
            {
                SystemTools.CopyUnmanagedMemory(dst, src, count);
                dst += stride;
                src += this.stride;
            }
            bitmap.UnlockBits(bitmapdata);
            return(bitmap);
        }
        public Bitmap Apply(BitmapData imageData)
        {
            PixelFormat pixelFormat = imageData.PixelFormat;

            this.CheckSourceFormat(pixelFormat);
            int        width      = imageData.Width;
            int        height     = imageData.Height;
            Bitmap     bitmap     = (pixelFormat == PixelFormat.Format8bppIndexed) ? Image.CreateGrayscaleImage(width, height) : new Bitmap(width, height, pixelFormat);
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, pixelFormat);

            SystemTools.CopyUnmanagedMemory(bitmapData.Scan0, imageData.Scan0, imageData.Stride * height);
            try
            {
                this.ProcessFilter(new UnmanagedImage(bitmapData));
            }
            finally
            {
                bitmap.UnlockBits(bitmapData);
            }
            return(bitmap);
        }
Beispiel #25
0
        public unsafe void Copy(UnmanagedImage destImage)
        {
            if (width != destImage.width || height != destImage.height || pixelFormat != destImage.pixelFormat)
            {
                throw new InvalidImagePropertiesException("Destination image has different size or pixel format.");
            }
            if (stride == destImage.stride)
            {
                SystemTools.CopyUnmanagedMemory(destImage.imageData, imageData, stride * height);
                return;
            }
            int   num   = destImage.stride;
            int   count = (stride < num) ? stride : num;
            byte *ptr   = (byte *)imageData.ToPointer();
            byte *ptr2  = (byte *)destImage.imageData.ToPointer();

            for (int i = 0; i < height; i++)
            {
                SystemTools.CopyUnmanagedMemory(ptr2, ptr, count);
                ptr2 += num;
                ptr  += stride;
            }
        }
 public unsafe void Copy(UnmanagedImage destImage)
 {
     if (((this.width != destImage.width) || (this.height != destImage.height)) || (this.pixelFormat != destImage.pixelFormat))
     {
         throw new InvalidImagePropertiesException("Destination image has different size or pixel format.");
     }
     if (this.stride == destImage.stride)
     {
         SystemTools.CopyUnmanagedMemory(destImage.imageData, this.imageData, this.stride * this.height);
     }
     else
     {
         int   stride = destImage.stride;
         int   count  = (this.stride < stride) ? this.stride : stride;
         byte *src    = (byte *)this.imageData.ToPointer();
         byte *dst    = (byte *)destImage.imageData.ToPointer();
         for (int i = 0; i < this.height; i++)
         {
             SystemTools.CopyUnmanagedMemory(dst, src, count);
             dst += stride;
             src += this.stride;
         }
     }
 }
Beispiel #27
0
        private static Bitmap Copy(this BitmapData sourceData, Bitmap destination)
        {
            if (destination == null)
            {
                // create new image
                destination = new Bitmap(sourceData.Width, sourceData.Height, sourceData.PixelFormat);
            }

            // get source image size
            int width  = sourceData.Width;
            int height = sourceData.Height;

            // lock destination bitmap data
            BitmapData destinationData = destination.LockBits(ImageLockMode.WriteOnly);

            System.Diagnostics.Debug.Assert(destinationData.Stride == sourceData.Stride);

            SystemTools.CopyUnmanagedMemory(destinationData.Scan0, sourceData.Scan0, height * sourceData.Stride);

            // unlock destination image
            destination.UnlockBits(destinationData);

            return(destination);
        }
        public unsafe void ProcessFrame(UnmanagedImage videoFrame)
        {
            lock (sync)
            {
                if (backgroundFrame == null)
                {
                    lastTimeMeasurment = DateTime.Now;

                    width  = videoFrame.Width;
                    height = videoFrame.Height;

                    backgroundFrame = UnmanagedImage.Create(width, height, PixelFormat.Format8bppIndexed);
                    motionFrame     = UnmanagedImage.Create(width, height, PixelFormat.Format8bppIndexed);

                    frameSize = motionFrame.Stride * height;

                    if (suppressNoise)
                    {
                        tempFrame = UnmanagedImage.Create(width, height, PixelFormat.Format8bppIndexed);
                    }

                    Tools.ConvertToGrayscale(videoFrame, backgroundFrame);

                    return;
                }

                if ((videoFrame.Width != width) || (videoFrame.Height != height))
                {
                    return;
                }

                Tools.ConvertToGrayscale(videoFrame, motionFrame);

                byte *backFrame;
                byte *currFrame;
                int   diff;

                if (millisecondsPerBackgroundUpdate == 0)
                {
                    if (++framesCounter == framesPerBackgroundUpdate)
                    {
                        framesCounter = 0;

                        backFrame = (byte *)backgroundFrame.ImageData.ToPointer();
                        currFrame = (byte *)motionFrame.ImageData.ToPointer();

                        for (int i = 0; i < frameSize; i++, backFrame++, currFrame++)
                        {
                            diff = *currFrame - *backFrame;
                            if (diff > 0)
                            {
                                (*backFrame)++;
                            }
                            else if (diff < 0)
                            {
                                (*backFrame)--;
                            }
                        }
                    }
                }
                else
                {
                    DateTime currentTime = DateTime.Now;
                    TimeSpan timeDff     = currentTime - lastTimeMeasurment;

                    lastTimeMeasurment = currentTime;

                    int millisonds = (int)timeDff.TotalMilliseconds + millisecondsLeftUnprocessed;

                    millisecondsLeftUnprocessed = millisonds % millisecondsPerBackgroundUpdate;

                    int updateAmount = (int)(millisonds / millisecondsPerBackgroundUpdate);

                    backFrame = (byte *)backgroundFrame.ImageData.ToPointer();
                    currFrame = (byte *)motionFrame.ImageData.ToPointer();

                    for (int i = 0; i < frameSize; i++, backFrame++, currFrame++)
                    {
                        diff = *currFrame - *backFrame;
                        if (diff > 0)
                        {
                            (*backFrame) += (byte)((diff < updateAmount) ? diff : updateAmount);
                        }
                        else if (diff < 0)
                        {
                            (*backFrame) += (byte)((-diff < updateAmount) ? diff : -updateAmount);
                        }
                    }
                }

                backFrame = (byte *)backgroundFrame.ImageData.ToPointer();
                currFrame = (byte *)motionFrame.ImageData.ToPointer();

                for (int i = 0; i < frameSize; i++, backFrame++, currFrame++)
                {
                    diff = (int)*currFrame - (int)*backFrame;

                    *currFrame = ((diff >= differenceThreshold) || (diff <= differenceThresholdNeg)) ? (byte)255 : (byte)0;
                }

                if (suppressNoise)
                {
                    SystemTools.CopyUnmanagedMemory(tempFrame.ImageData, motionFrame.ImageData, frameSize);
                    erosionFilter.Apply(tempFrame, motionFrame);

                    if (keepObjectEdges)
                    {
                        SystemTools.CopyUnmanagedMemory(tempFrame.ImageData, motionFrame.ImageData, frameSize);
                        dilatationFilter.Apply(tempFrame, motionFrame);
                    }
                }

                pixelsChanged = 0;
                byte *motion = (byte *)motionFrame.ImageData.ToPointer();

                for (int i = 0; i < frameSize; i++, motion++)
                {
                    pixelsChanged += (*motion & 1);
                }
            }
        }
        private void HandleDataReceived(IntPtr device, IntPtr depthData, UInt32 timestamp)
        {
            int width  = depthModeInfo.Width;
            int height = depthModeInfo.Height;

            Bitmap     image = null;
            BitmapData data  = null;

            try
            {
                image = new Bitmap(width, height, (!provideOriginalDepthImage) ?
                                   PixelFormat.Format24bppRgb : PixelFormat.Format16bppGrayScale);

                data = image.LockBits(new Rectangle(0, 0, width, height),
                                      ImageLockMode.ReadWrite, image.PixelFormat);

                unsafe
                {
                    ushort *src = (ushort *)imageBuffer.ToPointer( );

                    if (!provideOriginalDepthImage)
                    {
                        // color the depth image into white->red->yellow->green->cyan->blue->black gradient
                        byte *dst = (byte *)data.Scan0.ToPointer( );
                        int   offset = data.Stride - width * 3;
                        byte  red, green, blue;

                        for (int y = 0; y < height; y++)
                        {
                            for (int x = 0; x < width; x++, src++, dst += 3)
                            {
                                ushort pval = gamma[*src];
                                ushort lb   = (ushort)(pval & 0xff);

                                switch (pval >> 8)
                                {
                                case 0:     // white to red
                                    red   = 255;
                                    green = (byte)(255 - lb);
                                    blue  = (byte)(255 - lb);
                                    break;

                                case 1:     // red to yellow
                                    red   = 255;
                                    green = (byte)lb;
                                    blue  = 0;
                                    break;

                                case 2:     // yellow to green
                                    red   = (byte)(255 - lb);
                                    green = 255;
                                    blue  = 0;
                                    break;

                                case 3:     // green to cyan
                                    red   = 0;
                                    green = 255;
                                    blue  = (byte)lb;
                                    break;

                                case 4:     // cyan to blue
                                    red   = 0;
                                    green = (byte)(255 - lb);
                                    blue  = 255;
                                    break;

                                case 5:     // blue to black
                                    red   = 0;
                                    green = 0;
                                    blue  = (byte)(255 - lb);
                                    break;

                                default:
                                    red = green = blue = 0;
                                    break;
                                }

                                dst[2] = red;
                                dst[1] = green;
                                dst[0] = blue;
                            }
                            dst += offset;
                        }
                    }
                    else
                    {
                        // copy original depth image
                        ushort *dst    = (ushort *)data.Scan0.ToPointer( );
                        int     offset = (data.Stride >> 1) - width;

                        if (offset == 0)
                        {
                            SystemTools.CopyUnmanagedMemory((byte *)dst, (byte *)src, height * width * 2);
                        }
                        else
                        {
                            for (int y = 0; y < height; y++)
                            {
                                SystemTools.CopyUnmanagedMemory((byte *)dst, (byte *)src, width * 2);

                                dst += width;
                                src += width;
                                dst += offset;
                            }
                        }
                    }
                }

                image.UnlockBits(data);
                framesReceived++;
                bytesReceived += (int)depthModeInfo.Bytes;
            }
            catch (Exception ex)
            {
                if (VideoSourceError != null)
                {
                    VideoSourceError(this, new VideoSourceErrorEventArgs(ex.Message));
                }

                if (image != null)
                {
                    if (data != null)
                    {
                        image.UnlockBits(data);
                    }
                    image.Dispose( );
                    image = null;
                }
            }

            if (image != null)
            {
                if (NewFrame != null)
                {
                    NewFrame(this, new NewFrameEventArgs(image));
                }
                image.Dispose( );
            }
        }
Beispiel #30
0
        private void HandleDataReceived(IntPtr device, IntPtr imageData, UInt32 timeStamp)
        {
            int width  = videoModeInfo.Width;
            int height = videoModeInfo.Height;

            Bitmap     image = null;
            BitmapData data  = null;

            try
            {
                image = (cameraMode == VideoCameraMode.Color) ?
                        new Bitmap(width, height, PixelFormat.Format24bppRgb) :
                        Accord.Imaging.Image.CreateGrayscaleImage(width, height);

                data = image.LockBits(new Rectangle(0, 0, width, height),
                                      ImageLockMode.ReadWrite, image.PixelFormat);

                unsafe
                {
                    byte *dst = (byte *)data.Scan0.ToPointer();
                    byte *src = (byte *)imageBuffer.ToPointer();

                    if (cameraMode == VideoCameraMode.Color)
                    {
                        // color RGB 24 mode
                        int offset = data.Stride - width * 3;

                        for (int y = 0; y < height; y++)
                        {
                            for (int x = 0; x < width; x++, src += 3, dst += 3)
                            {
                                dst[0] = src[2];
                                dst[1] = src[1];
                                dst[2] = src[0];
                            }
                            dst += offset;
                        }
                    }
                    else
                    {
                        // infra red mode - grayscale output
                        int stride = data.Stride;

                        if (stride != width)
                        {
                            for (int y = 0; y < height; y++)
                            {
                                SystemTools.CopyUnmanagedMemory(dst, src, width);
                                dst += stride;
                                src += width;
                            }
                        }
                        else
                        {
                            SystemTools.CopyUnmanagedMemory(dst, src, width * height);
                        }
                    }
                }
                image.UnlockBits(data);

                framesReceived++;
                bytesReceived += width * height;
            }
            catch (Exception ex)
            {
                if (VideoSourceError != null)
                {
                    VideoSourceError(this, new VideoSourceErrorEventArgs(ex.Message));
                }

                if (image != null)
                {
                    if (data != null)
                    {
                        image.UnlockBits(data);
                    }
                    image.Dispose();
                    image = null;
                }
            }

            if (image != null)
            {
                if (NewFrame != null)
                {
                    NewFrame(this, new NewFrameEventArgs(image));
                }

                image.Dispose();
            }
        }