/// <summary> /// Render Fusion depth float frame to UI /// </summary> /// <param name="depthFloatFrame">Fusion depth float frame</param> /// <param name="depthPixels">Pixel buffer for depth float frame with pixel in depth</param> /// <param name="colorPixels">Pixel buffer for depth float frame with pixel in colors</param> /// <param name="bitmap">Bitmap contains depth float frame data for rendering</param> /// <param name="image">UI image component to render depth float frame to</param> private static void RenderDepthFloatImage(FusionFloatImageFrame depthFloatFrame, ref float[] depthPixels, ref int[] colorPixels, ref WriteableBitmap bitmap, System.Windows.Controls.Image image) { if (null == depthFloatFrame) { return; } if (null == depthPixels || depthFloatFrame.PixelDataLength != depthPixels.Length) { // Create depth pixel array of correct format depthPixels = new float[depthFloatFrame.PixelDataLength]; } if (null == colorPixels || depthFloatFrame.PixelDataLength != colorPixels.Length) { // Create colored pixel array of correct format colorPixels = new int[depthFloatFrame.PixelDataLength]; } if (null == bitmap || depthFloatFrame.Width != bitmap.Width || depthFloatFrame.Height != bitmap.Height) { // Create bitmap of correct format bitmap = new WriteableBitmap(depthFloatFrame.Width, depthFloatFrame.Height, 96.0, 96.0, PixelFormats.Bgr32, null); // Set bitmap as source to UI image object image.Source = bitmap; } depthFloatFrame.CopyPixelDataTo(depthPixels); // Calculate color pixels based on depth of each pixel float range = 4.0f; float minRange = 0.0f; for (int i = 0; i < depthPixels.Length; i++) { float depth = depthPixels[i]; int intensity = (depth >= minRange) ? ((int)(((depth - minRange) / range) * 256.0f) % 256) : 0; colorPixels[i] = 0; colorPixels[i] += intensity; // blue intensity *= 256; colorPixels[i] += intensity; // green intensity *= 256; colorPixels[i] += intensity; // red } // Copy colored pixels to bitmap bitmap.WritePixels( new Int32Rect(0, 0, depthFloatFrame.Width, depthFloatFrame.Height), colorPixels, bitmap.PixelWidth * sizeof(int), 0); }
/// <summary> /// Render Fusion depth float frame to UI /// </summary> /// <param name="depthFloatFrame">Fusion depth float frame</param> /// <param name="bitmap">Bitmap contains depth float frame data for rendering</param> /// <param name="image">UI image component to render depth float frame to</param> private void RenderDepthFloatImage( FusionFloatImageFrame depthFloatFrame, ref WriteableBitmap bitmap, System.Windows.Controls.Image image) { if (null == depthFloatFrame) { return; } // PixelDataLength is the number of pixels, not bytes if (null == this.depthFloatFramePixelsArgb || depthFloatFrame.PixelDataLength != this.depthFloatFramePixelsArgb.Length) { // Create colored pixel array of correct format this.depthFloatFramePixelsArgb = new int[depthFloatFrame.PixelDataLength]; } if (null == this.depthFloatFrameDepthPixels || depthFloatFrame.PixelDataLength != this.depthFloatFrameDepthPixels.Length) { // Create colored pixel array of correct format this.depthFloatFrameDepthPixels = new float[depthFloatFrame.PixelDataLength]; } if (null == bitmap || depthFloatFrame.Width != bitmap.Width || depthFloatFrame.Height != bitmap.Height) { // Create bitmap of correct format bitmap = new WriteableBitmap(depthFloatFrame.Width, depthFloatFrame.Height, 96.0, 96.0, PixelFormats.Bgr32, null); // Set bitmap as source to UI image object image.Source = bitmap; } depthFloatFrame.CopyPixelDataTo(this.depthFloatFrameDepthPixels); // Calculate color of pixels based on depth of each pixel float range = 4.0f; float oneOverRange = 1.0f / range; float minRange = 0.0f; Parallel.For( 0, depthFloatFrame.Height, y => { int index = y * depthFloatFrame.Width; for (int x = 0; x < depthFloatFrame.Width; ++x, ++index) { float depth = this.depthFloatFrameDepthPixels[index]; int intensity = (depth >= minRange) ? ((int)(((depth - minRange) * oneOverRange) * 256.0f) % 256) : 0; this.depthFloatFramePixelsArgb[index] = (intensity << 16) | (intensity << 8) | intensity; // argb } }); // Copy colored pixels to bitmap bitmap.WritePixels( new Int32Rect(0, 0, depthFloatFrame.Width, depthFloatFrame.Height), this.depthFloatFramePixelsArgb, bitmap.PixelWidth * sizeof(int), 0); }
/// <summary> /// Render Fusion AlignDepthFloatToReconstruction float deltas frame to UI /// </summary> /// <param name="alignDeltasFloatFrame">Fusion depth float frame</param> /// <param name="bitmap">Bitmap contains float frame data for rendering</param> /// <param name="image">UI image component to render float frame to</param> private void RenderAlignDeltasFloatImage(FusionFloatImageFrame alignDeltasFloatFrame, ref WriteableBitmap bitmap, System.Windows.Controls.Image image) { if (null == alignDeltasFloatFrame) { return; } if (null == this.deltaFromReferenceFrameFloatPixels || alignDeltasFloatFrame.PixelDataLength != this.deltaFromReferenceFrameFloatPixels.Length) { // Create depth pixel array of correct format this.deltaFromReferenceFrameFloatPixels = new float[alignDeltasFloatFrame.PixelDataLength]; } if (null == this.deltaFromReferenceFramePixels || alignDeltasFloatFrame.PixelDataLength != this.deltaFromReferenceFramePixels.Length) { // Create colored pixel array of correct format this.deltaFromReferenceFramePixels = new int[alignDeltasFloatFrame.PixelDataLength]; } if (null == bitmap || alignDeltasFloatFrame.Width != bitmap.Width || alignDeltasFloatFrame.Height != bitmap.Height) { // Create bitmap of correct format bitmap = new WriteableBitmap(alignDeltasFloatFrame.Width, alignDeltasFloatFrame.Height, 96.0, 96.0, PixelFormats.Bgr32, null); // Set bitmap as source to UI image object image.Source = bitmap; } alignDeltasFloatFrame.CopyPixelDataTo(this.deltaFromReferenceFrameFloatPixels); Parallel.For( 0, alignDeltasFloatFrame.Height, y => { int index = y * alignDeltasFloatFrame.Width; for (int x = 0; x < alignDeltasFloatFrame.Width; ++x, ++index) { float residue = this.deltaFromReferenceFrameFloatPixels[index]; if (residue < 1.0f) { this.deltaFromReferenceFramePixels[index] = (byte)(255.0f * ClampFloat(1.0f - residue, 0.0f, 1.0f)); // blue this.deltaFromReferenceFramePixels[index] |= ((byte)(255.0f * ClampFloat(1.0f - Math.Abs(residue), 0.0f, 1.0f))) << 8; // green this.deltaFromReferenceFramePixels[index] |= ((byte)(255.0f * ClampFloat(1.0f + residue, 0.0f, 1.0f))) << 16; // red } else { this.deltaFromReferenceFramePixels[index] = 0; } } }); // Copy colored pixels to bitmap bitmap.WritePixels( new Int32Rect(0, 0, alignDeltasFloatFrame.Width, alignDeltasFloatFrame.Height), this.deltaFromReferenceFramePixels, bitmap.PixelWidth * sizeof(int), 0); }