// -----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// -----------------------------------------------------------------------
namespace Microsoft.Samples.Kinect.Webserver.Sensor.Serialization
{
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Kinect.Toolkit.BackgroundRemoval;
///
/// Serializable representation of a background removed color stream message to send to client.
///
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter",
Justification = "Lower case names allowed for JSON serialization.")]
public class BackgroundRemovalStreamMessage : ImageHeaderStreamMessage
{
///
/// Bytes per pixel const.
///
private const int BytesPerPixel = 4;
///
/// Tracking ID of the player currently being tracked. Pixels that do not belong
/// to this player are removed.
///
///
/// This value will be 0 if no player is found in the corresponding color frame.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "tracked", Justification = "Lower case names allowed for JSON serialization.")]
public int trackedPlayerId { get; set; }
///
/// The average depth of the pixels corresponding to the foreground player.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "average", Justification = "Lower case names allowed for JSON serialization.")]
public short averageDepth { get; set; }
///
/// Buffer that holds background removed color image.
///
internal byte[] Buffer { get; private set; }
///
/// Update background removed color frame.
///
/// The input frame.
public void UpdateBackgroundRemovedColorFrame(BackgroundRemovedColorFrame frame)
{
if (frame == null)
{
throw new ArgumentNullException("frame");
}
this.timestamp = frame.Timestamp;
this.width = frame.Width;
this.height = frame.Height;
this.bufferLength = frame.PixelDataLength;
this.trackedPlayerId = frame.TrackedPlayerId;
this.averageDepth = frame.AverageDepth;
if ((this.Buffer == null) || (this.Buffer.Length != this.bufferLength))
{
this.Buffer = new byte[this.bufferLength];
}
unsafe
{
fixed (byte* messageDataPtr = this.Buffer)
{
fixed (byte* frameDataPtr = frame.GetRawPixelData())
{
byte* messageDataPixelPtr = messageDataPtr;
byte* frameDataPixelPtr = frameDataPtr;
byte* messageDataPixelPtrEnd = messageDataPixelPtr + this.bufferLength;
while (messageDataPixelPtr != messageDataPixelPtrEnd)
{
// Convert from BGRA to RGBA format
*(messageDataPixelPtr++) = *(frameDataPixelPtr + 2);
*(messageDataPixelPtr++) = *(frameDataPixelPtr + 1);
*(messageDataPixelPtr++) = *frameDataPixelPtr;
*(messageDataPixelPtr++) = *(frameDataPixelPtr + 3);
frameDataPixelPtr += BytesPerPixel;
}
}
}
}
}
}
}