Accord.Video.Kinect.KinectVideoCamera.HandleDataReceived C# (CSharp) Method

HandleDataReceived() private method

private HandleDataReceived ( IntPtr device, IntPtr imageData, UInt32 timeStamp ) : void
device System.IntPtr
imageData System.IntPtr
timeStamp System.UInt32
return void
        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();
            }
        }
    }