Beispiel #1
0
        /// <summary>
        /// Converts a frame to a SoftwareBitmap of a valid format to display in an Image control.
        /// </summary>
        /// <param name="inputFrame">Frame to convert.</param>
        public static unsafe SoftwareBitmap ConvertToDisplayableImage(VideoMediaFrame inputFrame)
        {
            SoftwareBitmap result = null;

            using (var inputBitmap = inputFrame?.SoftwareBitmap)
            {
                if (inputBitmap != null)
                {
                    if (inputBitmap.BitmapPixelFormat == BitmapPixelFormat.Bgra8 &&
                        inputBitmap.BitmapAlphaMode == BitmapAlphaMode.Premultiplied)
                    {
                        // SoftwareBitmap is already in the correct format for an Image control, so just return a copy.
                        result = SoftwareBitmap.Copy(inputBitmap);
                    }
                    else if (inputBitmap.BitmapPixelFormat == BitmapPixelFormat.Gray16)
                    {
                        if (inputFrame.FrameReference.SourceKind == MediaFrameSourceKind.Depth)
                        {
                            // Use a special pseudo color to render 16 bits depth frame.
                            var depthScale = (float)inputFrame.DepthMediaFrame.DepthFormat.DepthScaleInMeters;
                            result = TransformBitmap(inputBitmap, (w, i, o) => PseudoColorHelper.PseudoColorForDepth(w, i, o, depthScale));
                        }
                        else
                        {
                            // Use pseudo color to render 16 bits frames.
                            result = TransformBitmap(inputBitmap, PseudoColorHelper.PseudoColorFor16BitInfrared);
                        }
                    }
                    else if (inputBitmap.BitmapPixelFormat == BitmapPixelFormat.Gray8)
                    {
                        // Use pseudo color to render 8 bits frames.
                        result = TransformBitmap(inputBitmap, PseudoColorHelper.PseudoColorFor8BitInfrared);
                    }
                    else
                    {
                        try
                        {
                            // Convert to Bgra8 Premultiplied SoftwareBitmap, so xaml can display in UI.
                            result = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                        }
                        catch (ArgumentException exception)
                        {
                            // Conversion of software bitmap format is not supported.  Drop this frame.
                            System.Diagnostics.Debug.WriteLine(exception.Message);
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Converts a frame to a SoftwareBitmap of a valid format to display in an Image control.
        /// </summary>
        /// <param name="inputFrame">Frame to convert.</param>
        public static unsafe SoftwareBitmap ConvertToDisplayableImage(VideoMediaFrame inputFrame)
        {
            SoftwareBitmap result = null;

            using (var inputBitmap = inputFrame?.SoftwareBitmap)
            {
                if (inputBitmap != null)
                {
                    switch (inputFrame.FrameReference.SourceKind)
                    {
                    case MediaFrameSourceKind.Color:
                        // XAML requires Bgra8 with premultiplied alpha.
                        // We requested Bgra8 from the MediaFrameReader, so all that's
                        // left is fixing the alpha channel if necessary.
                        if (inputBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8)
                        {
                            Debug.WriteLine("Color frame in unexpected format.");
                        }
                        else if (inputBitmap.BitmapAlphaMode == BitmapAlphaMode.Premultiplied)
                        {
                            // Already in the correct format.
                            result = SoftwareBitmap.Copy(inputBitmap);
                        }
                        else
                        {
                            // Convert to premultiplied alpha.
                            result = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                        }
                        break;

                    case MediaFrameSourceKind.Depth:
                        // We requested D16 from the MediaFrameReader, so the frame should
                        // be in Gray16 format.
                        if (inputBitmap.BitmapPixelFormat == BitmapPixelFormat.Gray16)
                        {
                            // Use a special pseudo color to render 16 bits depth frame.
                            var depthScale       = (float)inputFrame.DepthMediaFrame.DepthFormat.DepthScaleInMeters;
                            var minReliableDepth = inputFrame.DepthMediaFrame.MinReliableDepth;
                            var maxReliableDepth = inputFrame.DepthMediaFrame.MaxReliableDepth;
                            result = TransformBitmap(inputBitmap, (w, i, o) => PseudoColorHelper.PseudoColorForDepth(w, i, o, depthScale, minReliableDepth, maxReliableDepth));
                        }
                        else
                        {
                            Debug.WriteLine("Depth frame in unexpected format.");
                        }
                        break;

                    case MediaFrameSourceKind.Infrared:
                        // We requested L8 or L16 from the MediaFrameReader, so the frame should
                        // be in Gray8 or Gray16 format.
                        switch (inputBitmap.BitmapPixelFormat)
                        {
                        case BitmapPixelFormat.Gray16:
                            // Use pseudo color to render 16 bits frames.
                            result = TransformBitmap(inputBitmap, PseudoColorHelper.PseudoColorFor16BitInfrared);
                            break;

                        case BitmapPixelFormat.Gray8:

                            // Use pseudo color to render 8 bits frames.
                            result = TransformBitmap(inputBitmap, PseudoColorHelper.PseudoColorFor8BitInfrared);
                            break;

                        default:
                            Debug.WriteLine("Infrared frame in unexpected format.");
                            break;
                        }
                        break;
                    }
                }
            }
            return(result);
        }
        /// <summary>
        /// Converts a frame to a SoftwareBitmap of a valid format to display in an Image control.
        /// </summary>
        /// <param name="inputFrame">Frame to convert.</param>
        public static unsafe SoftwareBitmap ConvertToDisplayableImage(VideoMediaFrame inputFrame)
        {
            SoftwareBitmap result = null;

            using (var inputBitmap = inputFrame?.SoftwareBitmap)
            {
                if (inputBitmap != null)
                {
                    var depthScale = (float)inputFrame.DepthMediaFrame.DepthFormat.DepthScaleInMeters;

                    var minReliableDepth = inputFrame.DepthMediaFrame.MinReliableDepth;
                    var maxReliableDepth = inputFrame.DepthMediaFrame.MaxReliableDepth;
                    result = TransformBitmap(inputBitmap, (w, i, o) => PseudoColorHelper.PseudoColorForDepth(w, i, o, depthScale, minReliableDepth, maxReliableDepth));

                    /* switch (inputFrame.FrameReference.SourceKind)
                     * {
                     *   case MediaFrameSourceKind.Color:
                     *       // XAML requires Bgra8 with premultiplied alpha.
                     *       // We requested Bgra8 from the MediaFrameReader, so all that's
                     *       // left is fixing the alpha channel if necessary.
                     *       if (inputBitmap.BitmapAlphaMode == BitmapAlphaMode.Premultiplied)
                     *       {
                     *           // Already in the correct format.
                     *           result = SoftwareBitmap.Copy(inputBitmap);
                     *       }
                     *       else
                     *       {
                     *           // Convert to premultiplied alpha.
                     *           result = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                     *       }
                     *       break;
                     *
                     *   case MediaFrameSourceKind.Depth:
                     *       // We requested D16 from the MediaFrameReader, so the frame should
                     *       // be in Gray16 format.
                     *       if (inputBitmap.BitmapPixelFormat == BitmapPixelFormat.Gray16)
                     *       {
                     *           // Use a special pseudo color to render 16 bits depth frame.
                     *           var depthScale = (float)inputFrame.DepthMediaFrame.DepthFormat.DepthScaleInMeters;
                     *           var minReliableDepth = inputFrame.DepthMediaFrame.MinReliableDepth;
                     *           var maxReliableDepth = inputFrame.DepthMediaFrame.MaxReliableDepth;
                     *           result = TransformBitmap(inputBitmap, (w, i, o) => PseudoColorHelper.PseudoColorForDepth(w, i, o, depthScale, minReliableDepth, maxReliableDepth));
                     *       }
                     *
                     *       break;
                     *
                     *   case MediaFrameSourceKind.Infrared:
                     *       // We requested L8 or L16 from the MediaFrameReader, so the frame should
                     *       // be in Gray8 or Gray16 format.
                     *       switch (inputBitmap.BitmapPixelFormat)
                     *       {
                     *           case BitmapPixelFormat.Gray16:
                     *               // Use pseudo color to render 16 bits frames.
                     *               result = TransformBitmap(inputBitmap, PseudoColorHelper.PseudoColorFor16BitInfrared);
                     *               break;
                     *
                     *           case BitmapPixelFormat.Gray8:
                     *
                     *               // Use pseudo color to render 8 bits frames.
                     *               result = TransformBitmap(inputBitmap, PseudoColorHelper.PseudoColorFor8BitInfrared);
                     *               break;
                     *
                     *           default:
                     *
                     *               break;
                     *       }
                     *       break;
                     * }
                     */
                }
            }
            return(result);
        }
Beispiel #4
0
        /// <summary>
        /// Converts a frame to a SoftwareBitmap of a valid format to display in an Image control.
        /// </summary>
        /// <param name="inputFrame">Frame to convert.</param>
        public static unsafe SoftwareBitmap ConvertToDisplayableImage(VideoMediaFrame inputFrame)
        {
            SoftwareBitmap result = null;

            using (var inputBitmap = inputFrame?.SoftwareBitmap)
            {
                if (inputBitmap != null)
                {
                    switch (inputFrame.FrameReference.SourceKind)
                    {
                    case MediaFrameSourceKind.Color:
                        if (inputBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8)
                        {
                            Debug.WriteLine("Color frame in unexpected format.");
                        }
                        else if (inputBitmap.BitmapAlphaMode == BitmapAlphaMode.Premultiplied)
                        {
                            result = SoftwareBitmap.Copy(inputBitmap);
                        }
                        else
                        {
                            result = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                        }
                        break;

                    case MediaFrameSourceKind.Depth:
                        if (inputBitmap.BitmapPixelFormat == BitmapPixelFormat.Gray16)
                        {
                            var depthScale       = (float)inputFrame.DepthMediaFrame.DepthFormat.DepthScaleInMeters;
                            var minReliableDepth = inputFrame.DepthMediaFrame.MinReliableDepth;
                            var maxReliableDepth = inputFrame.DepthMediaFrame.MaxReliableDepth;
                            result = TransformBitmap(inputBitmap, (w, i, o) => PseudoColorHelper.PseudoColorForDepth(w, i, o, depthScale, minReliableDepth, maxReliableDepth));
                        }
                        else
                        {
                            Debug.WriteLine("Depth frame in unexpected format.");
                        }
                        break;

                    case MediaFrameSourceKind.Infrared:
                        switch (inputBitmap.BitmapPixelFormat)
                        {
                        case BitmapPixelFormat.Gray16:
                            result = TransformBitmap(inputBitmap, PseudoColorHelper.PseudoColorFor16BitInfrared);
                            break;

                        case BitmapPixelFormat.Gray8:

                            result = TransformBitmap(inputBitmap, PseudoColorHelper.PseudoColorFor8BitInfrared);
                            break;

                        default:
                            Debug.WriteLine("Infrared frame in unexpected format.");
                            break;
                        }
                        break;
                    }
                }
            }
            return(result);
        }